4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
56 #include "wine/winbase16.h"
57 #include "wine/winuser16.h"
58 #include "wine/unicode.h"
60 #include "user_private.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(edit);
64 WINE_DECLARE_DEBUG_CHANNEL(combo);
65 WINE_DECLARE_DEBUG_CHANNEL(relay);
67 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
68 FIXME: BTW, new specs say 65535 (do you dare ???) */
69 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
70 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
71 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
72 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
75 * extra flags for EDITSTATE.flags field
77 #define EF_MODIFIED 0x0001 /* text has been modified */
78 #define EF_FOCUSED 0x0002 /* we have input focus */
79 #define EF_UPDATE 0x0004 /* notify parent of changed state */
80 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
82 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
83 wrapped line, instead of in front of the next character */
84 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT buffer_size; /* the size of the buffer in characters */
109 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
110 HFONT font; /* NULL means standard system font */
111 INT x_offset; /* scroll offset for multi lines this is in pixels
112 for single lines it's in characters */
113 INT line_height; /* height of a screen line in pixels */
114 INT char_width; /* average character width in pixels */
115 DWORD style; /* sane version of wnd->dwStyle */
116 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
117 INT undo_insert_count; /* number of characters inserted in sequence */
118 UINT undo_position; /* character index of the insertion and deletion */
119 LPWSTR undo_text; /* deleted text */
120 UINT undo_buffer_size; /* size of the deleted text buffer */
121 INT selection_start; /* == selection_end if no selection */
122 INT selection_end; /* == current caret position */
123 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
124 INT left_margin; /* in pixels */
125 INT right_margin; /* in pixels */
127 INT text_width; /* width of the widest line in pixels for multi line controls
128 and just line width for single line controls */
129 INT region_posx; /* Position of cursor relative to region: */
130 INT region_posy; /* -1: to left, 0: within, 1: to right */
131 EDITWORDBREAKPROC16 word_break_proc16;
132 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
133 INT line_count; /* number of lines */
134 INT y_offset; /* scroll offset in number of lines */
135 BOOL bCaptureState; /* flag indicating whether mouse was captured */
136 BOOL bEnableState; /* flag keeping the enable state */
137 HWND hwndSelf; /* the our window handle */
138 HWND hwndParent; /* Handle of parent for sending EN_* messages.
139 Even if parent will change, EN_* messages
140 should be sent to the first parent. */
141 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
143 * only for multi line controls
145 INT lock_count; /* amount of re-entries in the EditWndProc */
148 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
149 HLOCAL hloc32W; /* our unicode local memory block */
150 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
152 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
157 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
158 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
160 /* used for disabled or read-only edit control */
161 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
163 { /* Notify parent which has created this edit control */ \
164 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
165 SendMessageW(es->hwndParent, WM_COMMAND, \
166 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
167 (LPARAM)(es->hwndSelf)); \
170 /*********************************************************************
177 * These functions have trivial implementations
178 * We still like to call them internally
179 * "static inline" makes them more like macro's
181 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
182 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
183 static inline void EDIT_WM_Clear(EDITSTATE *es);
184 static inline void EDIT_WM_Cut(EDITSTATE *es);
187 * Helper functions only valid for one type of control
189 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
190 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
191 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
192 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
193 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
194 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
195 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
197 * Helper functions valid for both single line _and_ multi line controls
199 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
200 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
201 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
202 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
203 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
204 static void EDIT_LockBuffer(EDITSTATE *es);
205 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
206 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
207 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
208 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
209 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
210 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
211 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
212 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
213 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
214 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
215 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
216 static void EDIT_AdjustFormatRect(EDITSTATE *es);
217 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
218 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
219 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
220 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
222 * EM_XXX message handlers
224 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
225 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
226 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
227 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
228 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
229 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
230 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
231 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
232 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
233 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
234 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
235 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
236 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
237 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
238 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
239 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
240 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
241 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
242 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
243 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right, BOOL repaint);
244 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
245 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
246 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
247 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
248 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
249 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
250 static BOOL EDIT_EM_Undo(EDITSTATE *es);
252 * WM_XXX message handlers
254 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
255 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
256 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
257 static void EDIT_WM_Copy(EDITSTATE *es);
258 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
259 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
260 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
261 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
262 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
263 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
264 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
265 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
266 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
267 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
268 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
269 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
270 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
271 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
272 static void EDIT_WM_Paste(EDITSTATE *es);
273 static void EDIT_WM_SetFocus(EDITSTATE *es);
274 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
275 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
276 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
277 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
278 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
279 static void EDIT_WM_Timer(EDITSTATE *es);
280 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
281 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
282 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
284 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
285 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
287 /*********************************************************************
288 * edit class descriptor
290 const struct builtin_class_descr EDIT_builtin_class =
293 CS_DBLCLKS | CS_PARENTDC, /* style */
294 EditWndProcA, /* procA */
295 EditWndProcW, /* procW */
296 sizeof(EDITSTATE *), /* extra */
297 IDC_IBEAM, /* cursor */
302 /*********************************************************************
307 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
309 return (es->undo_insert_count || strlenW(es->undo_text));
313 /*********************************************************************
318 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
320 es->undo_insert_count = 0;
321 *es->undo_text = '\0';
325 /*********************************************************************
330 static inline void EDIT_WM_Clear(EDITSTATE *es)
332 static const WCHAR empty_stringW[] = {0};
334 /* Protect read-only edit control from modification */
335 if(es->style & ES_READONLY)
338 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
342 /*********************************************************************
347 static inline void EDIT_WM_Cut(EDITSTATE *es)
354 /**********************************************************************
357 * Returns the window version in case Wine emulates a later version
358 * of windows than the application expects.
360 * In a number of cases when windows runs an application that was
361 * designed for an earlier windows version, windows reverts
362 * to "old" behaviour of that earlier version.
364 * An example is a disabled edit control that needs to be painted.
365 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
366 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
367 * applications with an expected version 0f 4.0 or higher.
370 static DWORD get_app_version(void)
372 static DWORD version;
375 DWORD dwEmulatedVersion;
377 DWORD dwProcVersion = GetProcessVersion(0);
379 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
380 GetVersionExW( &info );
381 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
382 /* FIXME: this may not be 100% correct; see discussion on the
383 * wine developer list in Nov 1999 */
384 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
390 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
394 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
395 msg = WM_CTLCOLORSTATIC;
397 msg = WM_CTLCOLOREDIT;
399 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
400 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
403 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
406 return DefWindowProcW(hwnd, msg, wParam, lParam);
408 return DefWindowProcA(hwnd, msg, wParam, lParam);
411 /*********************************************************************
415 * The messages are in the order of the actual integer values
416 * (which can be found in include/windows.h)
417 * Wherever possible the 16 bit versions are converted to
418 * the 32 bit ones, so that we can 'fall through' to the
419 * helper functions. These are mostly 32 bit (with a few
420 * exceptions, clearly indicated by a '16' extension to their
424 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
425 WPARAM wParam, LPARAM lParam, BOOL unicode )
427 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
430 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
432 if (!es && msg != WM_NCCREATE)
433 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
435 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
443 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
447 if ((short)LOWORD(lParam) == -1)
448 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
450 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
452 EDIT_EM_ScrollCaret(es);
456 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
457 EDIT_EM_ScrollCaret(es);
464 RECT16 *r16 = MapSL(lParam);
465 r16->left = es->format_rect.left;
466 r16->top = es->format_rect.top;
467 r16->right = es->format_rect.right;
468 r16->bottom = es->format_rect.bottom;
473 CopyRect((LPRECT)lParam, &es->format_rect);
477 if ((es->style & ES_MULTILINE) && lParam) {
479 RECT16 *r16 = MapSL(lParam);
482 rc.right = r16->right;
483 rc.bottom = r16->bottom;
484 EDIT_SetRectNP(es, &rc);
485 EDIT_UpdateText(es, NULL, TRUE);
489 if ((es->style & ES_MULTILINE) && lParam) {
490 EDIT_SetRectNP(es, (LPRECT)lParam);
491 EDIT_UpdateText(es, NULL, TRUE);
496 if ((es->style & ES_MULTILINE) && lParam) {
498 RECT16 *r16 = MapSL(lParam);
501 rc.right = r16->right;
502 rc.bottom = r16->bottom;
503 EDIT_SetRectNP(es, &rc);
507 if ((es->style & ES_MULTILINE) && lParam)
508 EDIT_SetRectNP(es, (LPRECT)lParam);
513 result = EDIT_EM_Scroll(es, (INT)wParam);
516 case EM_LINESCROLL16:
517 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
518 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
521 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
524 case EM_SCROLLCARET16:
526 EDIT_EM_ScrollCaret(es);
532 result = ((es->flags & EF_MODIFIED) != 0);
538 es->flags |= EF_MODIFIED;
540 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
543 case EM_GETLINECOUNT16:
544 case EM_GETLINECOUNT:
545 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
549 if ((INT16)wParam == -1)
553 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
557 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
560 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
564 result = (LRESULT)EDIT_EM_GetHandle16(es);
567 result = (LRESULT)EDIT_EM_GetHandle(es);
572 result = EDIT_EM_GetThumb(es);
575 /* these messages missing from specs */
584 FIXME("undocumented message 0x%x, please report\n", msg);
585 result = DefWindowProcW(hwnd, msg, wParam, lParam);
588 case EM_LINELENGTH16:
590 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
593 case EM_REPLACESEL16:
594 lParam = (LPARAM)MapSL(lParam);
595 unicode = FALSE; /* 16-bit message is always ascii */
602 textW = (LPWSTR)lParam;
605 LPSTR textA = (LPSTR)lParam;
606 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
607 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
608 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
611 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
615 HeapFree(GetProcessHeap(), 0, textW);
620 lParam = (LPARAM)MapSL(lParam);
621 unicode = FALSE; /* 16-bit message is always ascii */
624 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
628 case EM_SETLIMITTEXT:
629 EDIT_EM_SetLimitText(es, (INT)wParam);
634 result = (LRESULT)EDIT_EM_CanUndo(es);
640 result = (LRESULT)EDIT_EM_Undo(es);
645 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
648 case EM_LINEFROMCHAR16:
649 case EM_LINEFROMCHAR:
650 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
653 case EM_SETTABSTOPS16:
654 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
657 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
660 case EM_SETPASSWORDCHAR16:
661 unicode = FALSE; /* 16-bit message is always ascii */
663 case EM_SETPASSWORDCHAR:
668 charW = (WCHAR)wParam;
672 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
675 EDIT_EM_SetPasswordChar(es, charW);
679 case EM_EMPTYUNDOBUFFER16:
680 case EM_EMPTYUNDOBUFFER:
681 EDIT_EM_EmptyUndoBuffer(es);
684 case EM_GETFIRSTVISIBLELINE16:
685 result = es->y_offset;
687 case EM_GETFIRSTVISIBLELINE:
688 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
691 case EM_SETREADONLY16:
694 SetWindowLongW( hwnd, GWL_STYLE,
695 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
696 es->style |= ES_READONLY;
698 SetWindowLongW( hwnd, GWL_STYLE,
699 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
700 es->style &= ~ES_READONLY;
705 case EM_SETWORDBREAKPROC16:
706 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
708 case EM_SETWORDBREAKPROC:
709 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
712 case EM_GETWORDBREAKPROC16:
713 result = (LRESULT)es->word_break_proc16;
715 case EM_GETWORDBREAKPROC:
716 result = (LRESULT)es->word_break_proc;
719 case EM_GETPASSWORDCHAR16:
720 unicode = FALSE; /* 16-bit message is always ascii */
722 case EM_GETPASSWORDCHAR:
725 result = es->password_char;
728 WCHAR charW = es->password_char;
730 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
736 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
739 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam), TRUE);
743 result = MAKELONG(es->left_margin, es->right_margin);
746 case EM_GETLIMITTEXT:
747 result = es->buffer_limit;
751 result = strlenW(es->text);
752 if ((INT)wParam >= result) result = -1;
753 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
757 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
760 /* End of the EM_ messages which were in numerical order; what order
761 * are these in? vaguely alphabetical?
765 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
769 result = EDIT_WM_Destroy(es);
774 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
776 if (es->style & ES_MULTILINE)
778 result |= DLGC_WANTALLKEYS;
782 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
784 int vk = (int)((LPMSG)lParam)->wParam;
786 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
788 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
789 result |= DLGC_WANTMESSAGE;
800 strng[0] = wParam >> 8;
801 strng[1] = wParam & 0xff;
802 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
803 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
804 EDIT_WM_Char(es, charW);
817 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
820 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
822 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
823 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
826 EDIT_WM_Char(es, charW);
835 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
839 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
848 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
851 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
855 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
856 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
857 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
859 result = EDIT_WM_Create(es, nameW);
860 HeapFree(GetProcessHeap(), 0, nameW);
869 es->bEnableState = (BOOL) wParam;
870 EDIT_UpdateText(es, NULL, TRUE);
874 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
878 result = (LRESULT)es->font;
882 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
885 case WM_GETTEXTLENGTH:
886 if (unicode) result = strlenW(es->text);
887 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
888 NULL, 0, NULL, NULL );
892 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
896 result = EDIT_WM_KeyDown(es, (INT)wParam);
900 result = EDIT_WM_KillFocus(es);
903 case WM_LBUTTONDBLCLK:
904 result = EDIT_WM_LButtonDblClk(es);
908 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
912 result = EDIT_WM_LButtonUp(es);
916 result = EDIT_WM_MButtonDown(es);
920 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
924 EDIT_WM_Paint(es, (HDC)wParam);
932 EDIT_WM_SetFocus(es);
936 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
940 /* FIXME: actually set an internal flag and behave accordingly */
944 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
949 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
952 case WM_STYLECHANGED:
953 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
956 case WM_STYLECHANGING:
957 result = 0; /* See EDIT_WM_StyleChanged */
961 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
969 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
974 int gcWheelDelta = 0;
975 UINT pulScrollLines = 3;
976 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
978 if (wParam & (MK_SHIFT | MK_CONTROL)) {
979 result = DefWindowProcW(hwnd, msg, wParam, lParam);
982 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
983 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
985 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
986 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
987 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
992 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
996 if (es) EDIT_UnlockBuffer(es, FALSE);
998 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1003 /*********************************************************************
1005 * EditWndProcW (USER32.@)
1007 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1009 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1012 /*********************************************************************
1014 * EditWndProc (USER32.@)
1016 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1018 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1021 /*********************************************************************
1023 * EDIT_BuildLineDefs_ML
1025 * Build linked list of text lines.
1026 * Lines can end with '\0' (last line), a character (if it is wrapped),
1027 * a soft return '\r\r\n' or a hard return '\r\n'
1030 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1034 LPWSTR current_position, cp;
1036 LINEDEF *current_line;
1037 LINEDEF *previous_line;
1038 LINEDEF *start_line;
1039 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1040 INT line_count = es->line_count;
1041 INT orig_net_length;
1044 if (istart == iend && delta == 0)
1047 dc = GetDC(es->hwndSelf);
1049 old_font = SelectObject(dc, es->font);
1051 previous_line = NULL;
1052 current_line = es->first_line_def;
1054 /* Find starting line. istart must lie inside an existing line or
1055 * at the end of buffer */
1057 if (istart < current_line->index + current_line->length ||
1058 current_line->ending == END_0)
1061 previous_line = current_line;
1062 current_line = current_line->next;
1064 } while (current_line);
1066 if (!current_line) /* Error occurred start is not inside previous buffer */
1068 FIXME(" modification occurred outside buffer\n");
1069 ReleaseDC(es->hwndSelf, dc);
1073 /* Remember start of modifications in order to calculate update region */
1074 nstart_line = line_index;
1075 nstart_index = current_line->index;
1077 /* We must start to reformat from the previous line since the modifications
1078 * may have caused the line to wrap upwards. */
1079 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1082 current_line = previous_line;
1084 start_line = current_line;
1086 fw = es->format_rect.right - es->format_rect.left;
1087 current_position = es->text + current_line->index;
1089 if (current_line != start_line)
1091 if (!current_line || current_line->index + delta > current_position - es->text)
1093 /* The buffer has been expanded, create a new line and
1094 insert it into the link list */
1095 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1096 new_line->next = previous_line->next;
1097 previous_line->next = new_line;
1098 current_line = new_line;
1101 else if (current_line->index + delta < current_position - es->text)
1103 /* The previous line merged with this line so we delete this extra entry */
1104 previous_line->next = current_line->next;
1105 HeapFree(GetProcessHeap(), 0, current_line);
1106 current_line = previous_line->next;
1110 else /* current_line->index + delta == current_position */
1112 if (current_position - es->text > iend)
1113 break; /* We reached end of line modifications */
1114 /* else recalulate this line */
1118 current_line->index = current_position - es->text;
1119 orig_net_length = current_line->net_length;
1121 /* Find end of line */
1122 cp = current_position;
1124 if (*cp == '\n') break;
1125 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1130 /* Mark type of line termination */
1132 current_line->ending = END_0;
1133 current_line->net_length = strlenW(current_position);
1134 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1135 current_line->ending = END_SOFT;
1136 current_line->net_length = cp - current_position - 1;
1137 } else if (*cp == '\n') {
1138 current_line->ending = END_RICH;
1139 current_line->net_length = cp - current_position;
1141 current_line->ending = END_HARD;
1142 current_line->net_length = cp - current_position;
1145 /* Calculate line width */
1146 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1147 current_position, current_line->net_length,
1148 es->tabs_count, es->tabs));
1150 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1151 if (!(es->style & ES_AUTOHSCROLL)) {
1152 if (current_line->width > fw) {
1157 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1158 prev + 1, current_line->net_length, WB_RIGHT);
1159 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1160 current_position, next, es->tabs_count, es->tabs));
1161 } while (current_line->width <= fw);
1162 if (!prev) { /* Didn't find a line break so force a break */
1167 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1168 current_position, next, es->tabs_count, es->tabs));
1169 } while (current_line->width <= fw);
1174 /* If the first line we are calculating, wrapped before istart, we must
1175 * adjust istart in order for this to be reflected in the update region. */
1176 if (current_line->index == nstart_index && istart > current_line->index + prev)
1177 istart = current_line->index + prev;
1178 /* else if we are updating the previous line before the first line we
1179 * are re-calculating and it expanded */
1180 else if (current_line == start_line &&
1181 current_line->index != nstart_index && orig_net_length < prev)
1183 /* Line expanded due to an upwards line wrap so we must partially include
1184 * previous line in update region */
1185 nstart_line = line_index;
1186 nstart_index = current_line->index;
1187 istart = current_line->index + orig_net_length;
1190 current_line->net_length = prev;
1191 current_line->ending = END_WRAP;
1192 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1193 current_line->net_length, es->tabs_count, es->tabs));
1195 else if (orig_net_length < current_line->net_length &&
1196 current_line == start_line &&
1197 current_line->index != nstart_index) {
1198 /* The previous line expanded but it's still not as wide as the client rect */
1199 /* The expansion is due to an upwards line wrap so we must partially include
1200 it in the update region */
1201 nstart_line = line_index;
1202 nstart_index = current_line->index;
1203 istart = current_line->index + orig_net_length;
1208 /* Adjust length to include line termination */
1209 switch (current_line->ending) {
1211 current_line->length = current_line->net_length + 3;
1214 current_line->length = current_line->net_length + 1;
1217 current_line->length = current_line->net_length + 2;
1221 current_line->length = current_line->net_length;
1224 es->text_width = max(es->text_width, current_line->width);
1225 current_position += current_line->length;
1226 previous_line = current_line;
1227 current_line = current_line->next;
1229 } while (previous_line->ending != END_0);
1231 /* Finish adjusting line indexes by delta or remove hanging lines */
1232 if (previous_line->ending == END_0)
1234 LINEDEF *pnext = NULL;
1236 previous_line->next = NULL;
1237 while (current_line)
1239 pnext = current_line->next;
1240 HeapFree(GetProcessHeap(), 0, current_line);
1241 current_line = pnext;
1245 else if (delta != 0)
1247 while (current_line)
1249 current_line->index += delta;
1250 current_line = current_line->next;
1254 /* Calculate rest of modification rectangle */
1259 * We calculate two rectangles. One for the first line which may have
1260 * an indent with respect to the format rect. The other is a format-width
1261 * rectangle that spans the rest of the lines that changed or moved.
1263 rc.top = es->format_rect.top + nstart_line * es->line_height -
1264 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1265 rc.bottom = rc.top + es->line_height;
1266 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1267 rc.left = es->format_rect.left;
1269 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1270 es->text + nstart_index, istart - nstart_index,
1271 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1272 rc.right = es->format_rect.right;
1273 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1276 rc.left = es->format_rect.left;
1277 rc.right = es->format_rect.right;
1279 * If lines were added or removed we must re-paint the remainder of the
1280 * lines since the remaining lines were either shifted up or down.
1282 if (line_count < es->line_count) /* We added lines */
1283 rc.bottom = es->line_count * es->line_height;
1284 else if (line_count > es->line_count) /* We removed lines */
1285 rc.bottom = line_count * es->line_height;
1287 rc.bottom = line_index * es->line_height;
1288 rc.bottom += es->format_rect.top;
1289 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1290 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1291 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1292 DeleteObject(tmphrgn);
1296 SelectObject(dc, old_font);
1298 ReleaseDC(es->hwndSelf, dc);
1301 /*********************************************************************
1303 * EDIT_CalcLineWidth_SL
1306 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1313 text = EDIT_GetPasswordPointer_SL(es);
1315 dc = GetDC(es->hwndSelf);
1317 old_font = SelectObject(dc, es->font);
1319 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1322 SelectObject(dc, old_font);
1323 ReleaseDC(es->hwndSelf, dc);
1325 if (es->style & ES_PASSWORD)
1326 HeapFree(GetProcessHeap(), 0, text);
1328 es->text_width = size.cx;
1331 /*********************************************************************
1333 * EDIT_CallWordBreakProc
1335 * Call appropriate WordBreakProc (internal or external).
1337 * Note: The "start" argument should always be an index referring
1338 * to es->text. The actual wordbreak proc might be
1339 * 16 bit, so we can't always pass any 32 bit LPSTR.
1340 * Hence we assume that es->text is the buffer that holds
1341 * the string under examination (we can decide this for ourselves).
1344 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1348 if (es->word_break_proc16) {
1355 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1356 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1357 segptr = WOWGlobalLock16(hglob16);
1358 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1359 args[4] = SELECTOROF(segptr);
1360 args[3] = OFFSETOF(segptr);
1364 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1365 ret = LOWORD(result);
1366 GlobalUnlock16(hglob16);
1367 GlobalFree16(hglob16);
1369 else if (es->word_break_proc)
1373 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1375 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1376 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1377 ret = wbpW(es->text + start, index, count, action);
1381 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1385 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1386 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1387 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1388 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1389 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1390 ret = wbpA(textA, index, countA, action);
1391 HeapFree(GetProcessHeap(), 0, textA);
1395 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1401 /*********************************************************************
1405 * Beware: This is not the function called on EM_CHARFROMPOS
1406 * The position _can_ be outside the formatting / client
1408 * The return value is only the character index
1411 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1416 INT x_high = 0, x_low = 0;
1418 if (es->style & ES_MULTILINE) {
1419 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1421 LINEDEF *line_def = es->first_line_def;
1423 while ((line > 0) && line_def->next) {
1424 line_index += line_def->length;
1425 line_def = line_def->next;
1428 x += es->x_offset - es->format_rect.left;
1429 if (es->style & ES_RIGHT)
1430 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1431 else if (es->style & ES_CENTER)
1432 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1433 if (x >= line_def->width) {
1435 *after_wrap = (line_def->ending == END_WRAP);
1436 return line_index + line_def->net_length;
1440 *after_wrap = FALSE;
1443 dc = GetDC(es->hwndSelf);
1445 old_font = SelectObject(dc, es->font);
1447 high = line_index + line_def->net_length + 1;
1448 while (low < high - 1)
1450 INT mid = (low + high) / 2;
1451 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1460 if (abs(x_high - x) + 1 <= abs(x_low - x))
1466 *after_wrap = ((index == line_index + line_def->net_length) &&
1467 (line_def->ending == END_WRAP));
1472 *after_wrap = FALSE;
1473 x -= es->format_rect.left;
1475 return es->x_offset;
1479 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1480 if (es->style & ES_RIGHT)
1482 else if (es->style & ES_CENTER)
1486 text = EDIT_GetPasswordPointer_SL(es);
1487 dc = GetDC(es->hwndSelf);
1489 old_font = SelectObject(dc, es->font);
1493 INT high = es->x_offset;
1494 while (low < high - 1)
1496 INT mid = (low + high) / 2;
1497 GetTextExtentPoint32W( dc, text + mid,
1498 es->x_offset - mid, &size );
1507 if (abs(x_high + x) <= abs(x_low + x) + 1)
1514 INT low = es->x_offset;
1515 INT high = strlenW(es->text) + 1;
1516 while (low < high - 1)
1518 INT mid = (low + high) / 2;
1519 GetTextExtentPoint32W( dc, text + es->x_offset,
1520 mid - es->x_offset, &size );
1529 if (abs(x_high - x) <= abs(x_low - x) + 1)
1534 if (es->style & ES_PASSWORD)
1535 HeapFree(GetProcessHeap(), 0, text);
1538 SelectObject(dc, old_font);
1539 ReleaseDC(es->hwndSelf, dc);
1544 /*********************************************************************
1548 * adjusts the point to be within the formatting rectangle
1549 * (so CharFromPos returns the nearest _visible_ character)
1552 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1554 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1555 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1559 /*********************************************************************
1563 * Calculates the bounding rectangle for a line from a starting
1564 * column to an ending column.
1567 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1569 INT line_index = EDIT_EM_LineIndex(es, line);
1571 if (es->style & ES_MULTILINE)
1572 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1574 rc->top = es->format_rect.top;
1575 rc->bottom = rc->top + es->line_height;
1576 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1577 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1581 /*********************************************************************
1583 * EDIT_GetPasswordPointer_SL
1585 * note: caller should free the (optionally) allocated buffer
1588 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1590 if (es->style & ES_PASSWORD) {
1591 INT len = strlenW(es->text);
1592 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1594 while(len) text[--len] = es->password_char;
1601 /*********************************************************************
1605 * This acts as a LocalLock16(), but it locks only once. This way
1606 * you can call it whenever you like, without unlocking.
1608 * Initially the edit control allocates a HLOCAL32 buffer
1609 * (32 bit linear memory handler). However, 16 bit application
1610 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1611 * handler). From that moment on we have to keep using this 16 bit memory
1612 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1613 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1617 static void EDIT_LockBuffer(EDITSTATE *es)
1619 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1620 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1625 BOOL _16bit = FALSE;
1631 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1632 textA = LocalLock(es->hloc32A);
1633 countA = strlen(textA) + 1;
1637 HANDLE16 oldDS = stack16->ds;
1638 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1639 stack16->ds = hInstance;
1640 textA = MapSL(LocalLock16(es->hloc16));
1641 stack16->ds = oldDS;
1642 countA = strlen(textA) + 1;
1647 ERR("no buffer ... please report\n");
1654 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1655 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1656 if(countW_new > es->buffer_size + 1)
1658 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1659 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1660 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1663 es->hloc32W = hloc32W_new;
1664 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1665 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1668 WARN("FAILED! Will synchronize partially\n");
1672 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1673 es->text = LocalLock(es->hloc32W);
1677 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1680 HANDLE16 oldDS = stack16->ds;
1681 stack16->ds = hInstance;
1682 LocalUnlock16(es->hloc16);
1683 stack16->ds = oldDS;
1686 LocalUnlock(es->hloc32A);
1693 /*********************************************************************
1695 * EDIT_SL_InvalidateText
1697 * Called from EDIT_InvalidateText().
1698 * Does the job for single-line controls only.
1701 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1706 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1707 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1708 EDIT_UpdateText(es, &rc, TRUE);
1712 /*********************************************************************
1714 * EDIT_ML_InvalidateText
1716 * Called from EDIT_InvalidateText().
1717 * Does the job for multi-line controls only.
1720 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1722 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1723 INT sl = EDIT_EM_LineFromChar(es, start);
1724 INT el = EDIT_EM_LineFromChar(es, end);
1733 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1736 sc = start - EDIT_EM_LineIndex(es, sl);
1737 ec = end - EDIT_EM_LineIndex(es, el);
1738 if (sl < es->y_offset) {
1742 if (el > es->y_offset + vlc) {
1743 el = es->y_offset + vlc;
1744 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1746 GetClientRect(es->hwndSelf, &rc1);
1747 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1749 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1750 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1751 EDIT_UpdateText(es, &rcUpdate, TRUE);
1753 EDIT_GetLineRect(es, sl, sc,
1754 EDIT_EM_LineLength(es,
1755 EDIT_EM_LineIndex(es, sl)),
1757 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1758 EDIT_UpdateText(es, &rcUpdate, TRUE);
1759 for (l = sl + 1 ; l < el ; l++) {
1760 EDIT_GetLineRect(es, l, 0,
1761 EDIT_EM_LineLength(es,
1762 EDIT_EM_LineIndex(es, l)),
1764 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1765 EDIT_UpdateText(es, &rcUpdate, TRUE);
1767 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1768 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1769 EDIT_UpdateText(es, &rcUpdate, TRUE);
1774 /*********************************************************************
1776 * EDIT_InvalidateText
1778 * Invalidate the text from offset start upto, but not including,
1779 * offset end. Useful for (re)painting the selection.
1780 * Regions outside the linewidth are not invalidated.
1781 * end == -1 means end == TextLength.
1782 * start and end need not be ordered.
1785 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1791 end = strlenW(es->text);
1799 if (es->style & ES_MULTILINE)
1800 EDIT_ML_InvalidateText(es, start, end);
1802 EDIT_SL_InvalidateText(es, start, end);
1806 /*********************************************************************
1810 * Try to fit size + 1 characters in the buffer.
1812 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1816 if (size <= es->buffer_size)
1819 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1821 /* Force edit to unlock it's buffer. es->text now NULL */
1822 EDIT_UnlockBuffer(es, TRUE);
1825 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1826 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1827 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1828 es->hloc32W = hNew32W;
1829 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1833 EDIT_LockBuffer(es);
1835 if (es->buffer_size < size) {
1836 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1837 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1840 TRACE("We now have %d+1\n", es->buffer_size);
1846 /*********************************************************************
1850 * Try to fit size + 1 bytes in the undo buffer.
1853 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1857 if (size <= es->undo_buffer_size)
1860 TRACE("trying to ReAlloc to %d+1\n", size);
1862 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1863 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1864 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1869 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1875 /*********************************************************************
1880 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1882 INT e = es->selection_end;
1886 if ((es->style & ES_MULTILINE) && e &&
1887 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1889 if (e && (es->text[e - 1] == '\r'))
1893 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1894 EDIT_EM_ScrollCaret(es);
1898 /*********************************************************************
1902 * Only for multi line controls
1903 * Move the caret one line down, on a column with the nearest
1904 * x coordinate on the screen (might be a different column).
1907 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1909 INT s = es->selection_start;
1910 INT e = es->selection_end;
1911 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1912 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1913 INT x = (short)LOWORD(pos);
1914 INT y = (short)HIWORD(pos);
1916 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1919 EDIT_EM_SetSel(es, s, e, after_wrap);
1920 EDIT_EM_ScrollCaret(es);
1924 /*********************************************************************
1929 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1931 BOOL after_wrap = FALSE;
1934 /* Pass a high value in x to make sure of receiving the end of the line */
1935 if (es->style & ES_MULTILINE)
1936 e = EDIT_CharFromPos(es, 0x3fffffff,
1937 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1939 e = strlenW(es->text);
1940 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1941 EDIT_EM_ScrollCaret(es);
1945 /*********************************************************************
1950 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1952 INT e = es->selection_end;
1956 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1957 if (es->text[e] == '\n')
1959 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1963 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1964 EDIT_EM_ScrollCaret(es);
1968 /*********************************************************************
1972 * Home key: move to beginning of line.
1975 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1979 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1980 if (es->style & ES_MULTILINE)
1981 e = EDIT_CharFromPos(es, -es->x_offset,
1982 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1985 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1986 EDIT_EM_ScrollCaret(es);
1990 /*********************************************************************
1992 * EDIT_MovePageDown_ML
1994 * Only for multi line controls
1995 * Move the caret one page down, on a column with the nearest
1996 * x coordinate on the screen (might be a different column).
1999 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2001 INT s = es->selection_start;
2002 INT e = es->selection_end;
2003 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2004 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2005 INT x = (short)LOWORD(pos);
2006 INT y = (short)HIWORD(pos);
2008 e = EDIT_CharFromPos(es, x,
2009 y + (es->format_rect.bottom - es->format_rect.top),
2013 EDIT_EM_SetSel(es, s, e, after_wrap);
2014 EDIT_EM_ScrollCaret(es);
2018 /*********************************************************************
2020 * EDIT_MovePageUp_ML
2022 * Only for multi line controls
2023 * Move the caret one page up, on a column with the nearest
2024 * x coordinate on the screen (might be a different column).
2027 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2029 INT s = es->selection_start;
2030 INT e = es->selection_end;
2031 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2032 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2033 INT x = (short)LOWORD(pos);
2034 INT y = (short)HIWORD(pos);
2036 e = EDIT_CharFromPos(es, x,
2037 y - (es->format_rect.bottom - es->format_rect.top),
2041 EDIT_EM_SetSel(es, s, e, after_wrap);
2042 EDIT_EM_ScrollCaret(es);
2046 /*********************************************************************
2050 * Only for multi line controls
2051 * Move the caret one line up, on a column with the nearest
2052 * x coordinate on the screen (might be a different column).
2055 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2057 INT s = es->selection_start;
2058 INT e = es->selection_end;
2059 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2060 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2061 INT x = (short)LOWORD(pos);
2062 INT y = (short)HIWORD(pos);
2064 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2067 EDIT_EM_SetSel(es, s, e, after_wrap);
2068 EDIT_EM_ScrollCaret(es);
2072 /*********************************************************************
2074 * EDIT_MoveWordBackward
2077 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2079 INT s = es->selection_start;
2080 INT e = es->selection_end;
2085 l = EDIT_EM_LineFromChar(es, e);
2086 ll = EDIT_EM_LineLength(es, e);
2087 li = EDIT_EM_LineIndex(es, l);
2090 li = EDIT_EM_LineIndex(es, l - 1);
2091 e = li + EDIT_EM_LineLength(es, li);
2094 e = li + (INT)EDIT_CallWordBreakProc(es,
2095 li, e - li, ll, WB_LEFT);
2099 EDIT_EM_SetSel(es, s, e, FALSE);
2100 EDIT_EM_ScrollCaret(es);
2104 /*********************************************************************
2106 * EDIT_MoveWordForward
2109 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2111 INT s = es->selection_start;
2112 INT e = es->selection_end;
2117 l = EDIT_EM_LineFromChar(es, e);
2118 ll = EDIT_EM_LineLength(es, e);
2119 li = EDIT_EM_LineIndex(es, l);
2121 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2122 e = EDIT_EM_LineIndex(es, l + 1);
2124 e = li + EDIT_CallWordBreakProc(es,
2125 li, e - li + 1, ll, WB_RIGHT);
2129 EDIT_EM_SetSel(es, s, e, FALSE);
2130 EDIT_EM_ScrollCaret(es);
2134 /*********************************************************************
2139 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2141 INT s = es->selection_start;
2142 INT e = es->selection_end;
2149 if (es->style & ES_MULTILINE) {
2150 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2151 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2156 TRACE("line=%d\n", line);
2158 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2159 x = (short)LOWORD(pos);
2160 y = (short)HIWORD(pos);
2161 li = EDIT_EM_LineIndex(es, line);
2162 ll = EDIT_EM_LineLength(es, li);
2163 s = min(es->selection_start, es->selection_end);
2164 e = max(es->selection_start, es->selection_end);
2165 s = min(li + ll, max(li, s));
2166 e = min(li + ll, max(li, e));
2167 if (rev && (s != e) &&
2168 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2169 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2170 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2171 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2173 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2177 /*********************************************************************
2182 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2193 BkMode = GetBkMode(dc);
2194 BkColor = GetBkColor(dc);
2195 TextColor = GetTextColor(dc);
2197 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2198 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2199 SetBkMode( dc, OPAQUE);
2201 li = EDIT_EM_LineIndex(es, line);
2202 if (es->style & ES_MULTILINE) {
2203 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2204 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2206 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2207 TextOutW(dc, x, y, text + li + col, count);
2208 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2210 if (es->style & ES_PASSWORD)
2211 HeapFree(GetProcessHeap(), 0, text);
2214 SetBkColor(dc, BkColor);
2215 SetTextColor(dc, TextColor);
2216 SetBkMode( dc, BkMode);
2222 /*********************************************************************
2227 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2230 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2231 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2232 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2236 /*********************************************************************
2238 * EDIT_AdjustFormatRect
2240 * Adjusts the format rectangle for the current font and the
2241 * current client rectangle.
2244 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2248 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2249 if (es->style & ES_MULTILINE)
2251 INT fw, vlc, max_x_offset, max_y_offset;
2253 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2254 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2256 /* correct es->x_offset */
2257 fw = es->format_rect.right - es->format_rect.left;
2258 max_x_offset = es->text_width - fw;
2259 if(max_x_offset < 0) max_x_offset = 0;
2260 if(es->x_offset > max_x_offset)
2261 es->x_offset = max_x_offset;
2263 /* correct es->y_offset */
2264 max_y_offset = es->line_count - vlc;
2265 if(max_y_offset < 0) max_y_offset = 0;
2266 if(es->y_offset > max_y_offset)
2267 es->y_offset = max_y_offset;
2269 /* force scroll info update */
2270 EDIT_UpdateScrollInfo(es);
2273 /* Windows doesn't care to fix text placement for SL controls */
2274 es->format_rect.bottom = es->format_rect.top + es->line_height;
2276 /* Always stay within the client area */
2277 GetClientRect(es->hwndSelf, &ClientRect);
2278 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2280 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2281 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2283 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2287 /*********************************************************************
2291 * note: this is not (exactly) the handler called on EM_SETRECTNP
2292 * it is also used to set the rect of a single line control
2295 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2299 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2301 CopyRect(&es->format_rect, rc);
2303 if (ExStyle & WS_EX_CLIENTEDGE) {
2304 es->format_rect.left++;
2305 es->format_rect.right--;
2307 if (es->format_rect.bottom - es->format_rect.top
2308 >= es->line_height + 2)
2310 es->format_rect.top++;
2311 es->format_rect.bottom--;
2314 else if (es->style & WS_BORDER) {
2315 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2316 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2317 es->format_rect.left += bw;
2318 es->format_rect.right -= bw;
2319 if (es->format_rect.bottom - es->format_rect.top
2320 >= es->line_height + 2 * bh)
2322 es->format_rect.top += bh;
2323 es->format_rect.bottom -= bh;
2327 es->format_rect.left += es->left_margin;
2328 es->format_rect.right -= es->right_margin;
2329 EDIT_AdjustFormatRect(es);
2333 /*********************************************************************
2338 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2341 /* Edit window might be already destroyed */
2342 if(!IsWindow(es->hwndSelf))
2344 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2348 if (!es->lock_count) {
2349 ERR("lock_count == 0 ... please report\n");
2353 ERR("es->text == 0 ... please report\n");
2357 if (force || (es->lock_count == 1)) {
2361 UINT countW = strlenW(es->text) + 1;
2362 STACK16FRAME* stack16 = NULL;
2367 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2368 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2369 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2370 countA = LocalSize(es->hloc32A);
2371 if(countA_new > countA)
2374 UINT alloc_size = ROUND_TO_GROW(countA_new);
2375 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2376 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2379 es->hloc32A = hloc32A_new;
2380 countA = LocalSize(hloc32A_new);
2381 TRACE("Real new size %d bytes\n", countA);
2384 WARN("FAILED! Will synchronize partially\n");
2386 textA = LocalLock(es->hloc32A);
2390 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2392 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2393 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2395 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2396 oldDS = stack16->ds;
2397 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2399 countA = LocalSize16(es->hloc16);
2400 if(countA_new > countA)
2402 HLOCAL16 hloc16_new;
2403 UINT alloc_size = ROUND_TO_GROW(countA_new);
2404 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2405 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2408 es->hloc16 = hloc16_new;
2409 countA = LocalSize16(hloc16_new);
2410 TRACE("Real new size %d bytes\n", countA);
2413 WARN("FAILED! Will synchronize partially\n");
2415 textA = MapSL(LocalLock16(es->hloc16));
2420 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2422 LocalUnlock16(es->hloc16);
2424 LocalUnlock(es->hloc32A);
2427 if (stack16) stack16->ds = oldDS;
2428 LocalUnlock(es->hloc32W);
2432 ERR("no buffer ... please report\n");
2440 /*********************************************************************
2442 * EDIT_UpdateScrollInfo
2445 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2447 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2450 si.cbSize = sizeof(SCROLLINFO);
2451 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2453 si.nMax = es->line_count - 1;
2454 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2455 si.nPos = es->y_offset;
2456 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2457 si.nMin, si.nMax, si.nPage, si.nPos);
2458 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2461 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2464 si.cbSize = sizeof(SCROLLINFO);
2465 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2467 si.nMax = es->text_width - 1;
2468 si.nPage = es->format_rect.right - es->format_rect.left;
2469 si.nPos = es->x_offset;
2470 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2471 si.nMin, si.nMax, si.nPage, si.nPos);
2472 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2476 /*********************************************************************
2478 * EDIT_WordBreakProc
2480 * Find the beginning of words.
2481 * Note: unlike the specs for a WordBreakProc, this function only
2482 * allows to be called without linebreaks between s[0] upto
2483 * s[count - 1]. Remember it is only called
2484 * internally, so we can decide this for ourselves.
2487 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2491 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2501 if (s[index] == ' ') {
2502 while (index && (s[index] == ' '))
2505 while (index && (s[index] != ' '))
2507 if (s[index] == ' ')
2511 while (index && (s[index] != ' '))
2513 if (s[index] == ' ')
2523 if (s[index] == ' ')
2524 while ((index < count) && (s[index] == ' ')) index++;
2526 while (s[index] && (s[index] != ' ') && (index < count))
2528 while ((s[index] == ' ') && (index < count)) index++;
2532 case WB_ISDELIMITER:
2533 ret = (s[index] == ' ');
2536 ERR("unknown action code, please report !\n");
2543 /*********************************************************************
2547 * returns line number (not index) in high-order word of result.
2548 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2549 * to Richedit, not to the edit control. Original documentation is valid.
2550 * FIXME: do the specs mean to return -1 if outside client area or
2551 * if outside formatting rectangle ???
2554 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2562 GetClientRect(es->hwndSelf, &rc);
2563 if (!PtInRect(&rc, pt))
2566 index = EDIT_CharFromPos(es, x, y, NULL);
2567 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2571 /*********************************************************************
2575 * Enable or disable soft breaks.
2577 * This means: insert or remove the soft linebreak character (\r\r\n).
2578 * Take care to check if the text still fits the buffer after insertion.
2579 * If not, notify with EN_ERRSPACE.
2582 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2584 es->flags &= ~EF_USE_SOFTBRK;
2586 es->flags |= EF_USE_SOFTBRK;
2587 FIXME("soft break enabled, not implemented\n");
2593 /*********************************************************************
2597 * Hopefully this won't fire back at us.
2598 * We always start with a fixed buffer in the local heap.
2599 * Despite of the documentation says that the local heap is used
2600 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2601 * buffer on the local heap.
2604 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2608 if (!(es->style & ES_MULTILINE))
2612 hLocal = es->hloc32W;
2618 UINT countA, alloc_size;
2619 TRACE("Allocating 32-bit ANSI alias buffer\n");
2620 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2621 alloc_size = ROUND_TO_GROW(countA);
2622 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2624 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2627 textA = LocalLock(es->hloc32A);
2628 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2629 LocalUnlock(es->hloc32A);
2631 hLocal = es->hloc32A;
2634 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2639 /*********************************************************************
2643 * Hopefully this won't fire back at us.
2644 * We always start with a buffer in 32 bit linear memory.
2645 * However, with this message a 16 bit application requests
2646 * a handle of 16 bit local heap memory, where it expects to find
2648 * It's a pitty that from this moment on we have to use this
2649 * local heap, because applications may rely on the handle
2652 * In this function we'll try to switch to local heap.
2654 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2657 UINT countA, alloc_size;
2658 STACK16FRAME* stack16;
2661 if (!(es->style & ES_MULTILINE))
2667 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2668 oldDS = stack16->ds;
2669 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2671 if (!LocalHeapSize16()) {
2673 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2674 ERR("could not initialize local heap\n");
2677 TRACE("local heap initialized\n");
2680 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2681 alloc_size = ROUND_TO_GROW(countA);
2683 TRACE("Allocating 16-bit ANSI alias buffer\n");
2684 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2685 ERR("could not allocate new 16 bit buffer\n");
2689 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2690 ERR("could not lock new 16 bit buffer\n");
2691 LocalFree16(es->hloc16);
2696 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2697 LocalUnlock16(es->hloc16);
2699 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2702 stack16->ds = oldDS;
2707 /*********************************************************************
2712 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2715 INT line_len, dst_len;
2718 if (es->style & ES_MULTILINE) {
2719 if (line >= es->line_count)
2723 i = EDIT_EM_LineIndex(es, line);
2725 line_len = EDIT_EM_LineLength(es, i);
2726 dst_len = *(WORD *)dst;
2729 if(dst_len <= line_len)
2731 memcpy(dst, src, dst_len * sizeof(WCHAR));
2734 else /* Append 0 if enough space */
2736 memcpy(dst, src, line_len * sizeof(WCHAR));
2743 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2744 if(!ret) /* Insufficient buffer size */
2746 if(ret < dst_len) /* Append 0 if enough space */
2747 ((LPSTR)dst)[ret] = 0;
2753 /*********************************************************************
2758 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2760 UINT s = es->selection_start;
2761 UINT e = es->selection_end;
2768 return MAKELONG(s, e);
2772 /*********************************************************************
2776 * FIXME: is this right ? (or should it be only VSCROLL)
2777 * (and maybe only for edit controls that really have their
2778 * own scrollbars) (and maybe only for multiline controls ?)
2779 * All in all: very poorly documented
2782 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2784 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2785 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2789 /*********************************************************************
2794 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2799 if (!(es->style & ES_MULTILINE))
2801 if (index > (INT)strlenW(es->text))
2802 return es->line_count - 1;
2804 index = min(es->selection_start, es->selection_end);
2807 line_def = es->first_line_def;
2808 index -= line_def->length;
2809 while ((index >= 0) && line_def->next) {
2811 line_def = line_def->next;
2812 index -= line_def->length;
2818 /*********************************************************************
2823 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2828 if (!(es->style & ES_MULTILINE))
2830 if (line >= es->line_count)
2834 line_def = es->first_line_def;
2836 INT index = es->selection_end - line_def->length;
2837 while ((index >= 0) && line_def->next) {
2838 line_index += line_def->length;
2839 line_def = line_def->next;
2840 index -= line_def->length;
2844 line_index += line_def->length;
2845 line_def = line_def->next;
2853 /*********************************************************************
2858 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2862 if (!(es->style & ES_MULTILINE))
2863 return strlenW(es->text);
2866 /* get the number of remaining non-selected chars of selected lines */
2867 INT32 l; /* line number */
2868 INT32 li; /* index of first char in line */
2870 l = EDIT_EM_LineFromChar(es, es->selection_start);
2871 /* # chars before start of selection area */
2872 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2873 l = EDIT_EM_LineFromChar(es, es->selection_end);
2874 /* # chars after end of selection */
2875 li = EDIT_EM_LineIndex(es, l);
2876 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2879 line_def = es->first_line_def;
2880 index -= line_def->length;
2881 while ((index >= 0) && line_def->next) {
2882 line_def = line_def->next;
2883 index -= line_def->length;
2885 return line_def->net_length;
2889 /*********************************************************************
2893 * NOTE: dx is in average character widths, dy - in lines;
2896 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2898 if (!(es->style & ES_MULTILINE))
2901 dx *= es->char_width;
2902 return EDIT_EM_LineScroll_internal(es, dx, dy);
2905 /*********************************************************************
2907 * EDIT_EM_LineScroll_internal
2909 * Version of EDIT_EM_LineScroll for internal use.
2910 * It doesn't refuse if ES_MULTILINE is set and assumes that
2911 * dx is in pixels, dy - in lines.
2914 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2917 INT x_offset_in_pixels;
2918 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2921 if (es->style & ES_MULTILINE)
2923 x_offset_in_pixels = es->x_offset;
2928 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2931 if (-dx > x_offset_in_pixels)
2932 dx = -x_offset_in_pixels;
2933 if (dx > es->text_width - x_offset_in_pixels)
2934 dx = es->text_width - x_offset_in_pixels;
2935 nyoff = max(0, es->y_offset + dy);
2936 if (nyoff >= es->line_count - lines_per_page)
2937 nyoff = max(0, es->line_count - lines_per_page);
2938 dy = (es->y_offset - nyoff) * es->line_height;
2943 es->y_offset = nyoff;
2944 if(es->style & ES_MULTILINE)
2947 es->x_offset += dx / es->char_width;
2949 GetClientRect(es->hwndSelf, &rc1);
2950 IntersectRect(&rc, &rc1, &es->format_rect);
2951 ScrollWindowEx(es->hwndSelf, -dx, dy,
2952 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2953 /* force scroll info update */
2954 EDIT_UpdateScrollInfo(es);
2956 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2957 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
2958 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2959 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
2964 /*********************************************************************
2969 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2971 INT len = strlenW(es->text);
2984 index = min(index, len);
2985 dc = GetDC(es->hwndSelf);
2987 old_font = SelectObject(dc, es->font);
2988 if (es->style & ES_MULTILINE) {
2989 l = EDIT_EM_LineFromChar(es, index);
2990 y = (l - es->y_offset) * es->line_height;
2991 li = EDIT_EM_LineIndex(es, l);
2992 if (after_wrap && (li == index) && l) {
2994 line_def = es->first_line_def;
2996 line_def = line_def->next;
2999 if (line_def->ending == END_WRAP) {
3001 y -= es->line_height;
3002 li = EDIT_EM_LineIndex(es, l);
3006 line_def = es->first_line_def;
3007 while (line_def->index != li)
3008 line_def = line_def->next;
3010 ll = line_def->net_length;
3011 lw = line_def->width;
3013 w = es->format_rect.right - es->format_rect.left;
3014 if (es->style & ES_RIGHT)
3016 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3017 es->tabs_count, es->tabs)) - es->x_offset;
3020 else if (es->style & ES_CENTER)
3022 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3023 es->tabs_count, es->tabs)) - es->x_offset;
3028 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3029 es->tabs_count, es->tabs)) - es->x_offset;
3032 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3033 if (index < es->x_offset) {
3034 GetTextExtentPoint32W(dc, text + index,
3035 es->x_offset - index, &size);
3038 GetTextExtentPoint32W(dc, text + es->x_offset,
3039 index - es->x_offset, &size);
3042 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3044 w = es->format_rect.right - es->format_rect.left;
3045 if (w > es->text_width)
3047 if (es->style & ES_RIGHT)
3048 x += w - es->text_width;
3049 else if (es->style & ES_CENTER)
3050 x += (w - es->text_width) / 2;
3055 if (es->style & ES_PASSWORD)
3056 HeapFree(GetProcessHeap(), 0, text);
3058 x += es->format_rect.left;
3059 y += es->format_rect.top;
3061 SelectObject(dc, old_font);
3062 ReleaseDC(es->hwndSelf, dc);
3063 return MAKELONG((INT16)x, (INT16)y);
3067 /*********************************************************************
3071 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3074 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3076 UINT strl = strlenW(lpsz_replace);
3077 UINT tl = strlenW(es->text);
3088 TRACE("%s, can_undo %d, send_update %d\n",
3089 debugstr_w(lpsz_replace), can_undo, send_update);
3091 s = es->selection_start;
3092 e = es->selection_end;
3094 if ((s == e) && !strl)
3099 size = tl - (e - s) + strl;
3103 /* Issue the EN_MAXTEXT notification and continue with replacing text
3104 * such that buffer limit is honored. */
3105 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3106 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3107 strl = es->buffer_limit - (tl - (e-s));
3110 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3114 /* there is something to be deleted */
3115 TRACE("deleting stuff.\n");
3117 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3119 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3120 buf[bufl] = 0; /* ensure 0 termination */
3122 strcpyW(es->text + s, es->text + e);
3125 /* there is an insertion */
3126 tl = strlenW(es->text);
3127 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));
3128 for (p = es->text + tl ; p >= es->text + s ; p--)
3130 for (i = 0 , p = es->text + s ; i < strl ; i++)
3131 p[i] = lpsz_replace[i];
3132 if(es->style & ES_UPPERCASE)
3133 CharUpperBuffW(p, strl);
3134 else if(es->style & ES_LOWERCASE)
3135 CharLowerBuffW(p, strl);
3137 if (es->style & ES_MULTILINE)
3139 INT st = min(es->selection_start, es->selection_end);
3140 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3142 hrgn = CreateRectRgn(0, 0, 0, 0);
3143 EDIT_BuildLineDefs_ML(es, st, st + strl,
3144 strl - abs(es->selection_end - es->selection_start), hrgn);
3145 /* if text is too long undo all changes */
3146 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3148 strcpyW(es->text + e, es->text + e + strl);
3150 for (i = 0 , p = es->text ; i < e - s ; i++)
3152 EDIT_BuildLineDefs_ML(es, s, e,
3153 abs(es->selection_end - es->selection_start) - strl, hrgn);
3156 hrgn = CreateRectRgn(0, 0, 0, 0);
3157 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3161 INT fw = es->format_rect.right - es->format_rect.left;
3162 EDIT_CalcLineWidth_SL(es);
3163 /* remove chars that don't fit */
3164 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3165 while ((es->text_width > fw) && s + strl >= s) {
3166 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3168 EDIT_CalcLineWidth_SL(es);
3170 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3176 utl = strlenW(es->undo_text);
3177 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3178 /* undo-buffer is extended to the right */
3179 EDIT_MakeUndoFit(es, utl + e - s);
3180 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3181 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3182 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3183 /* undo-buffer is extended to the left */
3184 EDIT_MakeUndoFit(es, utl + e - s);
3185 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3187 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3189 es->undo_position = s;
3191 /* new undo-buffer */
3192 EDIT_MakeUndoFit(es, e - s);
3193 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3194 es->undo_text[e - s] = 0; /* ensure 0 termination */
3195 es->undo_position = s;
3197 /* any deletion makes the old insertion-undo invalid */
3198 es->undo_insert_count = 0;
3200 EDIT_EM_EmptyUndoBuffer(es);
3204 if ((s == es->undo_position) ||
3205 ((es->undo_insert_count) &&
3206 (s == es->undo_position + es->undo_insert_count)))
3208 * insertion is new and at delete position or
3209 * an extension to either left or right
3211 es->undo_insert_count += strl;
3213 /* new insertion undo */
3214 es->undo_position = s;
3215 es->undo_insert_count = strl;
3216 /* new insertion makes old delete-buffer invalid */
3217 *es->undo_text = '\0';
3220 EDIT_EM_EmptyUndoBuffer(es);
3224 HeapFree(GetProcessHeap(), 0, buf);
3228 /* If text has been deleted and we're right or center aligned then scroll rightward */
3229 if (es->style & (ES_RIGHT | ES_CENTER))
3231 INT delta = strl - abs(es->selection_end - es->selection_start);
3233 if (delta < 0 && es->x_offset)
3235 if (abs(delta) > es->x_offset)
3238 es->x_offset += delta;
3242 EDIT_EM_SetSel(es, s, s, FALSE);
3243 es->flags |= EF_MODIFIED;
3244 if (send_update) es->flags |= EF_UPDATE;
3247 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3251 EDIT_UpdateText(es, NULL, TRUE);
3253 EDIT_EM_ScrollCaret(es);
3255 /* force scroll info update */
3256 EDIT_UpdateScrollInfo(es);
3259 if(send_update || (es->flags & EF_UPDATE))
3261 es->flags &= ~EF_UPDATE;
3262 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3267 /*********************************************************************
3272 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3276 if (!(es->style & ES_MULTILINE))
3277 return (LRESULT)FALSE;
3287 if (es->y_offset < es->line_count - 1)
3292 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3295 if (es->y_offset < es->line_count - 1)
3296 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3299 return (LRESULT)FALSE;
3302 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3303 /* check if we are going to move too far */
3304 if(es->y_offset + dy > es->line_count - vlc)
3305 dy = es->line_count - vlc - es->y_offset;
3307 /* Notification is done in EDIT_EM_LineScroll */
3309 EDIT_EM_LineScroll(es, 0, dy);
3311 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3315 /*********************************************************************
3320 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3322 if (es->style & ES_MULTILINE) {
3327 INT cw = es->char_width;
3332 l = EDIT_EM_LineFromChar(es, es->selection_end);
3333 li = EDIT_EM_LineIndex(es, l);
3334 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3335 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3336 if (l >= es->y_offset + vlc)
3337 dy = l - vlc + 1 - es->y_offset;
3338 if (l < es->y_offset)
3339 dy = l - es->y_offset;
3340 ww = es->format_rect.right - es->format_rect.left;
3341 if (x < es->format_rect.left)
3342 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3343 if (x > es->format_rect.right)
3344 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3345 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3347 /* check if we are going to move too far */
3348 if(es->x_offset + dx + ww > es->text_width)
3349 dx = es->text_width - ww - es->x_offset;
3350 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3351 EDIT_EM_LineScroll_internal(es, dx, dy);
3358 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3359 format_width = es->format_rect.right - es->format_rect.left;
3360 if (x < es->format_rect.left) {
3361 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3364 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3365 } while ((x < goal) && es->x_offset);
3366 /* FIXME: use ScrollWindow() somehow to improve performance */
3367 EDIT_UpdateText(es, NULL, TRUE);
3368 } else if (x > es->format_rect.right) {
3370 INT len = strlenW(es->text);
3371 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3374 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3375 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3376 } while ((x > goal) && (x_last > es->format_rect.right));
3377 /* FIXME: use ScrollWindow() somehow to improve performance */
3378 EDIT_UpdateText(es, NULL, TRUE);
3382 if(es->flags & EF_FOCUSED)
3383 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3387 /*********************************************************************
3391 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3394 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3396 if (!(es->style & ES_MULTILINE))
3400 WARN("called with NULL handle\n");
3404 EDIT_UnlockBuffer(es, TRUE);
3408 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3409 HANDLE16 oldDS = stack16->ds;
3411 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3412 LocalFree16(es->hloc16);
3413 stack16->ds = oldDS;
3421 LocalFree(es->hloc32A);
3433 countA = LocalSize(hloc);
3434 textA = LocalLock(hloc);
3435 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3436 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3438 ERR("Could not allocate new unicode buffer\n");
3441 textW = LocalLock(hloc32W_new);
3442 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3443 LocalUnlock(hloc32W_new);
3447 LocalFree(es->hloc32W);
3449 es->hloc32W = hloc32W_new;
3453 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3455 EDIT_LockBuffer(es);
3457 es->x_offset = es->y_offset = 0;
3458 es->selection_start = es->selection_end = 0;
3459 EDIT_EM_EmptyUndoBuffer(es);
3460 es->flags &= ~EF_MODIFIED;
3461 es->flags &= ~EF_UPDATE;
3462 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3463 EDIT_UpdateText(es, NULL, TRUE);
3464 EDIT_EM_ScrollCaret(es);
3465 /* force scroll info update */
3466 EDIT_UpdateScrollInfo(es);
3470 /*********************************************************************
3474 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3477 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3479 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3480 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3481 HANDLE16 oldDS = stack16->ds;
3487 if (!(es->style & ES_MULTILINE))
3491 WARN("called with NULL handle\n");
3495 EDIT_UnlockBuffer(es, TRUE);
3499 LocalFree(es->hloc32A);
3503 stack16->ds = hInstance;
3504 countA = LocalSize16(hloc);
3505 textA = MapSL(LocalLock16(hloc));
3506 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3507 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3509 ERR("Could not allocate new unicode buffer\n");
3512 textW = LocalLock(hloc32W_new);
3513 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3514 LocalUnlock(hloc32W_new);
3515 LocalUnlock16(hloc);
3516 stack16->ds = oldDS;
3519 LocalFree(es->hloc32W);
3521 es->hloc32W = hloc32W_new;
3524 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3526 EDIT_LockBuffer(es);
3528 es->x_offset = es->y_offset = 0;
3529 es->selection_start = es->selection_end = 0;
3530 EDIT_EM_EmptyUndoBuffer(es);
3531 es->flags &= ~EF_MODIFIED;
3532 es->flags &= ~EF_UPDATE;
3533 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3534 EDIT_UpdateText(es, NULL, TRUE);
3535 EDIT_EM_ScrollCaret(es);
3536 /* force scroll info update */
3537 EDIT_UpdateScrollInfo(es);
3541 /*********************************************************************
3545 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3546 * However, the windows version is not complied to yet in all of edit.c
3548 * Additionally as the wrapper for RichEdit controls we need larger buffers
3549 * at present -1 will represent nolimit
3551 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3553 if (limit == 0xFFFFFFFF)
3554 es->buffer_limit = -1;
3555 else if (es->style & ES_MULTILINE) {
3557 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3559 es->buffer_limit = BUFLIMIT_MULTI;
3562 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3564 es->buffer_limit = BUFLIMIT_SINGLE;
3569 /*********************************************************************
3573 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3574 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3575 * margin according to the textmetrics of the current font.
3577 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3578 * of the char's width as the margin, but this is not how Windows handles this.
3579 * For all other fonts Windows sets the margins to zero.
3582 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3583 INT left, INT right, BOOL repaint)
3586 INT default_left_margin = 0; /* in pixels */
3587 INT default_right_margin = 0; /* in pixels */
3589 /* Set the default margins depending on the font */
3590 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3591 HDC dc = GetDC(es->hwndSelf);
3592 HFONT old_font = SelectObject(dc, es->font);
3593 GetTextMetricsW(dc, &tm);
3594 /* The default margins are only non zero for TrueType or Vector fonts */
3595 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3596 /* This must be calculated more exactly! But how? */
3597 default_left_margin = tm.tmAveCharWidth / 3;
3598 default_right_margin = tm.tmAveCharWidth / 3;
3600 SelectObject(dc, old_font);
3601 ReleaseDC(es->hwndSelf, dc);
3604 if (action & EC_LEFTMARGIN) {
3605 es->format_rect.left -= es->left_margin;
3606 if (left != EC_USEFONTINFO)
3607 es->left_margin = left;
3609 es->left_margin = default_left_margin;
3610 es->format_rect.left += es->left_margin;
3613 if (action & EC_RIGHTMARGIN) {
3614 es->format_rect.right += es->right_margin;
3615 if (right != EC_USEFONTINFO)
3616 es->right_margin = right;
3618 es->right_margin = default_right_margin;
3619 es->format_rect.right -= es->right_margin;
3622 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3623 EDIT_AdjustFormatRect(es);
3624 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3627 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3631 /*********************************************************************
3633 * EM_SETPASSWORDCHAR
3636 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3640 if (es->style & ES_MULTILINE)
3643 if (es->password_char == c)
3646 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3647 es->password_char = c;
3649 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3650 es->style |= ES_PASSWORD;
3652 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3653 es->style &= ~ES_PASSWORD;
3655 EDIT_UpdateText(es, NULL, TRUE);
3659 /*********************************************************************
3663 * note: unlike the specs say: the order of start and end
3664 * _is_ preserved in Windows. (i.e. start can be > end)
3665 * In other words: this handler is OK
3668 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3670 UINT old_start = es->selection_start;
3671 UINT old_end = es->selection_end;
3672 UINT len = strlenW(es->text);
3674 if (start == (UINT)-1) {
3675 start = es->selection_end;
3676 end = es->selection_end;
3678 start = min(start, len);
3679 end = min(end, len);
3681 es->selection_start = start;
3682 es->selection_end = end;
3684 es->flags |= EF_AFTER_WRAP;
3686 es->flags &= ~EF_AFTER_WRAP;
3687 /* Compute the necessary invalidation region. */
3688 /* Note that we don't need to invalidate regions which have
3689 * "never" been selected, or those which are "still" selected.
3690 * In fact, every time we hit a selection boundary, we can
3691 * *toggle* whether we need to invalidate. Thus we can optimize by
3692 * *sorting* the interval endpoints. Let's assume that we sort them
3694 * start <= end <= old_start <= old_end
3695 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3696 * in 5 comparisons; ie it's impossible to do better than the
3698 ORDER_UINT(end, old_end);
3699 ORDER_UINT(start, old_start);
3700 ORDER_UINT(old_start, old_end);
3701 ORDER_UINT(start, end);
3702 /* Note that at this point 'end' and 'old_start' are not in order, but
3703 * start is definitely the min. and old_end is definitely the max. */
3704 if (end != old_start)
3708 * ORDER_UINT32(end, old_start);
3709 * EDIT_InvalidateText(es, start, end);
3710 * EDIT_InvalidateText(es, old_start, old_end);
3711 * in place of the following if statement.
3712 * (That would complete the optimal five-comparison four-element sort.)
3714 if (old_start > end )
3716 EDIT_InvalidateText(es, start, end);
3717 EDIT_InvalidateText(es, old_start, old_end);
3721 EDIT_InvalidateText(es, start, old_start);
3722 EDIT_InvalidateText(es, end, old_end);
3725 else EDIT_InvalidateText(es, start, old_end);
3729 /*********************************************************************
3734 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3736 if (!(es->style & ES_MULTILINE))
3738 HeapFree(GetProcessHeap(), 0, es->tabs);
3739 es->tabs_count = count;
3743 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3744 memcpy(es->tabs, tabs, count * sizeof(INT));
3750 /*********************************************************************
3755 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3757 if (!(es->style & ES_MULTILINE))
3759 HeapFree(GetProcessHeap(), 0, es->tabs);
3760 es->tabs_count = count;
3765 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3766 for (i = 0 ; i < count ; i++)
3767 es->tabs[i] = *tabs++;
3773 /*********************************************************************
3775 * EM_SETWORDBREAKPROC
3778 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3780 if (es->word_break_proc == wbp)
3783 es->word_break_proc = wbp;
3784 es->word_break_proc16 = NULL;
3786 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3787 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3788 EDIT_UpdateText(es, NULL, TRUE);
3793 /*********************************************************************
3795 * EM_SETWORDBREAKPROC16
3798 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3800 if (es->word_break_proc16 == wbp)
3803 es->word_break_proc = NULL;
3804 es->word_break_proc16 = wbp;
3805 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3806 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3807 EDIT_UpdateText(es, NULL, TRUE);
3812 /*********************************************************************
3817 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3822 /* As per MSDN spec, for a single-line edit control,
3823 the return value is always TRUE */
3824 if( es->style & ES_READONLY )
3825 return !(es->style & ES_MULTILINE);
3827 ulength = strlenW(es->undo_text);
3829 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3831 strcpyW(utext, es->undo_text);
3833 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3834 es->undo_insert_count, debugstr_w(utext));
3836 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3837 EDIT_EM_EmptyUndoBuffer(es);
3838 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3839 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3840 /* send the notification after the selection start and end are set */
3841 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3842 EDIT_EM_ScrollCaret(es);
3843 HeapFree(GetProcessHeap(), 0, utext);
3845 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3846 es->undo_insert_count, debugstr_w(es->undo_text));
3851 /*********************************************************************
3856 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3860 control = GetKeyState(VK_CONTROL) & 0x8000;
3864 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3865 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3868 if (es->style & ES_MULTILINE) {
3869 if (es->style & ES_READONLY) {
3870 EDIT_MoveHome(es, FALSE);
3871 EDIT_MoveDown_ML(es, FALSE);
3873 static const WCHAR cr_lfW[] = {'\r','\n',0};
3874 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3879 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3881 static const WCHAR tabW[] = {'\t',0};
3882 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3886 if (!(es->style & ES_READONLY) && !control) {
3887 if (es->selection_start != es->selection_end)
3890 /* delete character left of caret */
3891 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3892 EDIT_MoveBackward(es, TRUE);
3898 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3901 if (!(es->style & ES_READONLY))
3902 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3905 if (!(es->style & ES_READONLY))
3906 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3910 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3911 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3914 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3918 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3925 /*********************************************************************
3930 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3932 if (code || control)
3952 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3953 EDIT_EM_ScrollCaret(es);
3956 ERR("unknown menu item, please report\n");
3962 /*********************************************************************
3966 * Note: the resource files resource/sysres_??.rc cannot define a
3967 * single popup menu. Hence we use a (dummy) menubar
3968 * containing the single popup menu as its first item.
3970 * FIXME: the message identifiers have been chosen arbitrarily,
3971 * hence we use MF_BYPOSITION.
3972 * We might as well use the "real" values (anybody knows ?)
3973 * The menu definition is in resources/sysres_??.rc.
3974 * Once these are OK, we better use MF_BYCOMMAND here
3975 * (as we do in EDIT_WM_Command()).
3978 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3980 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3981 HMENU popup = GetSubMenu(menu, 0);
3982 UINT start = es->selection_start;
3983 UINT end = es->selection_end;
3985 ORDER_UINT(start, end);
3988 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3990 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3992 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3994 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3996 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3998 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
4000 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4005 /*********************************************************************
4010 static void EDIT_WM_Copy(EDITSTATE *es)
4012 INT s = min(es->selection_start, es->selection_end);
4013 INT e = max(es->selection_start, es->selection_end);
4021 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4022 dst = GlobalLock(hdst);
4023 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4024 dst[len] = 0; /* ensure 0 termination */
4025 TRACE("%s\n", debugstr_w(dst));
4027 OpenClipboard(es->hwndSelf);
4029 SetClipboardData(CF_UNICODETEXT, hdst);
4034 /*********************************************************************
4039 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4043 TRACE("%s\n", debugstr_w(name));
4045 * To initialize some final structure members, we call some helper
4046 * functions. However, since the EDITSTATE is not consistent (i.e.
4047 * not fully initialized), we should be very careful which
4048 * functions can be called, and in what order.
4050 EDIT_WM_SetFont(es, 0, FALSE);
4051 EDIT_EM_EmptyUndoBuffer(es);
4053 /* We need to calculate the format rect
4054 (applications may send EM_SETMARGINS before the control gets visible) */
4055 GetClientRect(es->hwndSelf, &clientRect);
4056 EDIT_SetRectNP(es, &clientRect);
4058 if (name && *name) {
4059 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
4060 /* if we insert text to the editline, the text scrolls out
4061 * of the window, as the caret is placed after the insert
4062 * pos normally; thus we reset es->selection... to 0 and
4065 es->selection_start = es->selection_end = 0;
4066 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4067 * Messages are only to be sent when the USER does something to
4068 * change the contents. So I am removing this EN_CHANGE
4070 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4072 EDIT_EM_ScrollCaret(es);
4074 /* force scroll info update */
4075 EDIT_UpdateScrollInfo(es);
4076 /* The rule seems to return 1 here for success */
4077 /* Power Builder masked edit controls will crash */
4079 /* FIXME: is that in all cases so ? */
4084 /*********************************************************************
4089 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4094 while (LocalUnlock(es->hloc32W)) ;
4095 LocalFree(es->hloc32W);
4098 while (LocalUnlock(es->hloc32A)) ;
4099 LocalFree(es->hloc32A);
4102 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
4103 HANDLE16 oldDS = stack16->ds;
4105 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4106 while (LocalUnlock16(es->hloc16)) ;
4107 LocalFree16(es->hloc16);
4108 stack16->ds = oldDS;
4111 pc = es->first_line_def;
4115 HeapFree(GetProcessHeap(), 0, pc);
4119 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4120 HeapFree(GetProcessHeap(), 0, es);
4126 /*********************************************************************
4131 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4133 /* we do the proper erase in EDIT_WM_Paint */
4138 /*********************************************************************
4143 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4145 if(!count) return 0;
4149 lstrcpynW(dst, es->text, count);
4150 return strlenW(dst);
4154 LPSTR textA = (LPSTR)dst;
4155 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4156 textA[count - 1] = 0; /* ensure 0 termination */
4157 return strlen(textA);
4161 /*********************************************************************
4166 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4171 if (!(es->style & ES_MULTILINE))
4174 if (!(es->style & ES_AUTOHSCROLL))
4178 fw = es->format_rect.right - es->format_rect.left;
4181 TRACE("SB_LINELEFT\n");
4183 dx = -es->char_width;
4186 TRACE("SB_LINERIGHT\n");
4187 if (es->x_offset < es->text_width)
4188 dx = es->char_width;
4191 TRACE("SB_PAGELEFT\n");
4193 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4196 TRACE("SB_PAGERIGHT\n");
4197 if (es->x_offset < es->text_width)
4198 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4206 TRACE("SB_RIGHT\n");
4207 if (es->x_offset < es->text_width)
4208 dx = es->text_width - es->x_offset;
4211 TRACE("SB_THUMBTRACK %d\n", pos);
4212 es->flags |= EF_HSCROLL_TRACK;
4213 if(es->style & WS_HSCROLL)
4214 dx = pos - es->x_offset;
4219 if(pos < 0 || pos > 100) return 0;
4220 /* Assume default scroll range 0-100 */
4221 fw = es->format_rect.right - es->format_rect.left;
4222 new_x = pos * (es->text_width - fw) / 100;
4223 dx = es->text_width ? (new_x - es->x_offset) : 0;
4226 case SB_THUMBPOSITION:
4227 TRACE("SB_THUMBPOSITION %d\n", pos);
4228 es->flags &= ~EF_HSCROLL_TRACK;
4229 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4230 dx = pos - es->x_offset;
4235 if(pos < 0 || pos > 100) return 0;
4236 /* Assume default scroll range 0-100 */
4237 fw = es->format_rect.right - es->format_rect.left;
4238 new_x = pos * (es->text_width - fw) / 100;
4239 dx = es->text_width ? (new_x - es->x_offset) : 0;
4242 /* force scroll info update */
4243 EDIT_UpdateScrollInfo(es);
4244 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4248 TRACE("SB_ENDSCROLL\n");
4251 * FIXME : the next two are undocumented !
4252 * Are we doing the right thing ?
4253 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4254 * although it's also a regular control message.
4256 case EM_GETTHUMB: /* this one is used by NT notepad */
4260 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4261 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4264 /* Assume default scroll range 0-100 */
4265 INT fw = es->format_rect.right - es->format_rect.left;
4266 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4268 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4271 case EM_LINESCROLL16:
4272 TRACE("EM_LINESCROLL16\n");
4277 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4283 INT fw = es->format_rect.right - es->format_rect.left;
4284 /* check if we are going to move too far */
4285 if(es->x_offset + dx + fw > es->text_width)
4286 dx = es->text_width - fw - es->x_offset;
4288 EDIT_EM_LineScroll_internal(es, dx, 0);
4294 /*********************************************************************
4299 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4301 HWND hLBox = es->hwndListBox;
4309 hCombo = GetParent(es->hwndSelf);
4313 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4315 if (key == VK_UP || key == VK_DOWN)
4317 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4320 if (msg == WM_KEYDOWN || nEUI)
4321 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4327 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4329 /* make sure ComboLBox pops up */
4330 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4335 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4338 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4340 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4342 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4347 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4353 /*********************************************************************
4357 * Handling of special keys that don't produce a WM_CHAR
4358 * (i.e. non-printable keys) & Backspace & Delete
4361 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4366 if (GetKeyState(VK_MENU) & 0x8000)
4369 shift = GetKeyState(VK_SHIFT) & 0x8000;
4370 control = GetKeyState(VK_CONTROL) & 0x8000;
4375 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4380 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4381 EDIT_MoveUp_ML(es, shift);
4384 EDIT_MoveWordBackward(es, shift);
4386 EDIT_MoveBackward(es, shift);
4389 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4393 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4394 EDIT_MoveDown_ML(es, shift);
4396 EDIT_MoveWordForward(es, shift);
4398 EDIT_MoveForward(es, shift);
4401 EDIT_MoveHome(es, shift);
4404 EDIT_MoveEnd(es, shift);
4407 if (es->style & ES_MULTILINE)
4408 EDIT_MovePageUp_ML(es, shift);
4410 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4413 if (es->style & ES_MULTILINE)
4414 EDIT_MovePageDown_ML(es, shift);
4416 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4419 if (!(es->style & ES_READONLY) && !(shift && control)) {
4420 if (es->selection_start != es->selection_end) {
4427 /* delete character left of caret */
4428 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4429 EDIT_MoveBackward(es, TRUE);
4431 } else if (control) {
4432 /* delete to end of line */
4433 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4434 EDIT_MoveEnd(es, TRUE);
4437 /* delete character right of caret */
4438 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4439 EDIT_MoveForward(es, TRUE);
4447 if (!(es->style & ES_READONLY))
4453 /* If the edit doesn't want the return send a message to the default object */
4454 if(!(es->style & ES_WANTRETURN))
4456 HWND hwndParent = GetParent(es->hwndSelf);
4457 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4458 if (HIWORD(dw) == DC_HASDEFID)
4460 SendMessageW( hwndParent, WM_COMMAND,
4461 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4462 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4471 /*********************************************************************
4476 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4478 es->flags &= ~EF_FOCUSED;
4480 if(!(es->style & ES_NOHIDESEL))
4481 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4482 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4487 /*********************************************************************
4491 * The caret position has been set on the WM_LBUTTONDOWN message
4494 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4497 INT e = es->selection_end;
4502 if (!(es->flags & EF_FOCUSED))
4505 l = EDIT_EM_LineFromChar(es, e);
4506 li = EDIT_EM_LineIndex(es, l);
4507 ll = EDIT_EM_LineLength(es, e);
4508 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4509 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4510 EDIT_EM_SetSel(es, s, e, FALSE);
4511 EDIT_EM_ScrollCaret(es);
4516 /*********************************************************************
4521 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4526 SetFocus(es->hwndSelf);
4527 if (!(es->flags & EF_FOCUSED))
4530 es->bCaptureState = TRUE;
4531 SetCapture(es->hwndSelf);
4532 EDIT_ConfinePoint(es, &x, &y);
4533 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4534 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4535 EDIT_EM_ScrollCaret(es);
4536 es->region_posx = es->region_posy = 0;
4537 SetTimer(es->hwndSelf, 0, 100, NULL);
4542 /*********************************************************************
4547 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4549 if (es->bCaptureState) {
4550 KillTimer(es->hwndSelf, 0);
4551 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4553 es->bCaptureState = FALSE;
4558 /*********************************************************************
4563 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4565 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4570 /*********************************************************************
4575 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4581 /* If the mouse has been captured by process other than the edit control itself,
4582 * the windows edit controls will not select the strings with mouse move.
4584 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4588 * FIXME: gotta do some scrolling if outside client
4589 * area. Maybe reset the timer ?
4592 EDIT_ConfinePoint(es, &x, &y);
4593 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4594 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4595 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4596 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4597 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4602 /*********************************************************************
4606 * See also EDIT_WM_StyleChanged
4608 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4613 TRACE("Creating %s edit control, style = %08lx\n",
4614 unicode ? "Unicode" : "ANSI", lpcs->style);
4616 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4618 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4621 * Note: since the EDITSTATE has not been fully initialized yet,
4622 * we can't use any API calls that may send
4623 * WM_XXX messages before WM_NCCREATE is completed.
4626 es->is_unicode = unicode;
4627 es->style = lpcs->style;
4629 es->bEnableState = !(es->style & WS_DISABLED);
4631 es->hwndSelf = hwnd;
4632 /* Save parent, which will be notified by EN_* messages */
4633 es->hwndParent = lpcs->hwndParent;
4635 if (es->style & ES_COMBO)
4636 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4638 /* Number overrides lowercase overrides uppercase (at least it
4639 * does in Win95). However I'll bet that ES_NUMBER would be
4640 * invalid under Win 3.1.
4642 if (es->style & ES_NUMBER) {
4643 ; /* do not override the ES_NUMBER */
4644 } else if (es->style & ES_LOWERCASE) {
4645 es->style &= ~ES_UPPERCASE;
4647 if (es->style & ES_MULTILINE) {
4648 es->buffer_limit = BUFLIMIT_MULTI;
4649 if (es->style & WS_VSCROLL)
4650 es->style |= ES_AUTOVSCROLL;
4651 if (es->style & WS_HSCROLL)
4652 es->style |= ES_AUTOHSCROLL;
4653 es->style &= ~ES_PASSWORD;
4654 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4655 /* Confirmed - RIGHT overrides CENTER */
4656 if (es->style & ES_RIGHT)
4657 es->style &= ~ES_CENTER;
4658 es->style &= ~WS_HSCROLL;
4659 es->style &= ~ES_AUTOHSCROLL;
4662 es->buffer_limit = BUFLIMIT_SINGLE;
4663 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4664 es->style &= ~ES_CENTER;
4665 es->style &= ~WS_HSCROLL;
4666 es->style &= ~WS_VSCROLL;
4667 if (es->style & ES_PASSWORD)
4668 es->password_char = '*';
4671 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4672 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4674 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4676 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4678 es->undo_buffer_size = es->buffer_size;
4680 if (es->style & ES_MULTILINE)
4681 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4686 * In Win95 look and feel, the WS_BORDER style is replaced by the
4687 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4688 * control a nonclient area so we don't need to draw the border.
4689 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4690 * a nonclient area and we should handle painting the border ourselves.
4692 * When making modifications please ensure that the code still works
4693 * for edit controls created directly with style 0x50800000, exStyle 0
4694 * (which should have a single pixel border)
4696 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4697 es->style &= ~WS_BORDER;
4698 else if (es->style & WS_BORDER)
4699 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4704 /*********************************************************************
4709 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4722 BOOL rev = es->bEnableState &&
4723 ((es->flags & EF_FOCUSED) ||
4724 (es->style & ES_NOHIDESEL));
4725 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4727 GetClientRect(es->hwndSelf, &rcClient);
4729 /* get the background brush */
4730 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
4731 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
4733 /* paint the border and the background */
4734 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4736 if(es->style & WS_BORDER) {
4737 bw = GetSystemMetrics(SM_CXBORDER);
4738 bh = GetSystemMetrics(SM_CYBORDER);
4740 if(es->style & ES_MULTILINE) {
4741 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4742 if(es->style & WS_VSCROLL) rc.right+=bw;
4745 /* Draw the frame. Same code as in nonclient.c */
4746 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4747 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4748 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4749 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4750 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4751 SelectObject(dc, old_brush);
4753 /* Keep the border clean */
4754 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4755 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4758 GetClipBox(dc, &rc);
4759 FillRect(dc, &rc, brush);
4761 IntersectClipRect(dc, es->format_rect.left,
4762 es->format_rect.top,
4763 es->format_rect.right,
4764 es->format_rect.bottom);
4765 if (es->style & ES_MULTILINE) {
4767 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4770 old_font = SelectObject(dc, es->font);
4771 EDIT_NotifyCtlColor(es, dc);
4773 if (!es->bEnableState)
4774 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4775 GetClipBox(dc, &rcRgn);
4776 if (es->style & ES_MULTILINE) {
4777 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4778 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4779 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4780 if (IntersectRect(&rc, &rcRgn, &rcLine))
4781 EDIT_PaintLine(es, dc, i, rev);
4784 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4785 if (IntersectRect(&rc, &rcRgn, &rcLine))
4786 EDIT_PaintLine(es, dc, 0, rev);
4789 SelectObject(dc, old_font);
4792 EndPaint(es->hwndSelf, &ps);
4796 /*********************************************************************
4801 static void EDIT_WM_Paste(EDITSTATE *es)
4806 /* Protect read-only edit control from modification */
4807 if(es->style & ES_READONLY)
4810 OpenClipboard(es->hwndSelf);
4811 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4812 src = (LPWSTR)GlobalLock(hsrc);
4813 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4820 /*********************************************************************
4825 static void EDIT_WM_SetFocus(EDITSTATE *es)
4827 es->flags |= EF_FOCUSED;
4828 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4829 EDIT_SetCaretPos(es, es->selection_end,
4830 es->flags & EF_AFTER_WRAP);
4831 if(!(es->style & ES_NOHIDESEL))
4832 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4833 ShowCaret(es->hwndSelf);
4834 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
4838 /*********************************************************************
4842 * With Win95 look the margins are set to default font value unless
4843 * the system font (font == 0) is being set, in which case they are left
4847 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4855 dc = GetDC(es->hwndSelf);
4857 old_font = SelectObject(dc, font);
4858 GetTextMetricsW(dc, &tm);
4859 es->line_height = tm.tmHeight;
4860 es->char_width = tm.tmAveCharWidth;
4862 SelectObject(dc, old_font);
4863 ReleaseDC(es->hwndSelf, dc);
4865 /* Reset the format rect and the margins */
4866 GetClientRect(es->hwndSelf, &clientRect);
4867 EDIT_SetRectNP(es, &clientRect);
4868 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4869 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
4871 if (es->style & ES_MULTILINE)
4872 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4874 EDIT_CalcLineWidth_SL(es);
4877 EDIT_UpdateText(es, NULL, TRUE);
4878 if (es->flags & EF_FOCUSED) {
4880 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4881 EDIT_SetCaretPos(es, es->selection_end,
4882 es->flags & EF_AFTER_WRAP);
4883 ShowCaret(es->hwndSelf);
4888 /*********************************************************************
4893 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4894 * The modified flag is reset. No notifications are sent.
4896 * For single-line controls, reception of WM_SETTEXT triggers:
4897 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4900 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4902 if (!unicode && text)
4904 LPCSTR textA = (LPCSTR)text;
4905 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4906 LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
4908 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4912 if (es->flags & EF_UPDATE)
4913 /* fixed this bug once; complain if we see it about to happen again. */
4914 ERR("SetSel may generate UPDATE message whose handler may reset "
4917 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4920 TRACE("%s\n", debugstr_w(text));
4921 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4923 HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
4927 static const WCHAR empty_stringW[] = {0};
4929 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4932 es->flags &= ~EF_MODIFIED;
4933 EDIT_EM_SetSel(es, 0, 0, FALSE);
4935 /* Send the notification after the selection start and end have been set
4936 * edit control doesn't send notification on WM_SETTEXT
4937 * if it is multiline, or it is part of combobox
4939 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4941 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
4942 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4944 EDIT_EM_ScrollCaret(es);
4945 EDIT_UpdateScrollInfo(es);
4949 /*********************************************************************
4954 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4956 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4958 TRACE("width = %d, height = %d\n", width, height);
4959 SetRect(&rc, 0, 0, width, height);
4960 EDIT_SetRectNP(es, &rc);
4961 EDIT_UpdateText(es, NULL, TRUE);
4966 /*********************************************************************
4970 * This message is sent by SetWindowLong on having changed either the Style
4971 * or the extended style.
4973 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4975 * See also EDIT_WM_NCCreate
4977 * It appears that the Windows version of the edit control allows the style
4978 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4979 * style variable which will generally be different. In this function we
4980 * update the internal style based on what changed in the externally visible
4983 * Much of this content as based upon the MSDN, especially:
4984 * Platform SDK Documentation -> User Interface Services ->
4985 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4986 * Edit Control Styles
4988 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4990 if (GWL_STYLE == which) {
4991 DWORD style_change_mask;
4993 /* Only a subset of changes can be applied after the control
4996 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4998 if (es->style & ES_MULTILINE)
4999 style_change_mask |= ES_WANTRETURN;
5001 new_style = style->styleNew & style_change_mask;
5003 /* Number overrides lowercase overrides uppercase (at least it
5004 * does in Win95). However I'll bet that ES_NUMBER would be
5005 * invalid under Win 3.1.
5007 if (new_style & ES_NUMBER) {
5008 ; /* do not override the ES_NUMBER */
5009 } else if (new_style & ES_LOWERCASE) {
5010 new_style &= ~ES_UPPERCASE;
5013 es->style = (es->style & ~style_change_mask) | new_style;
5014 } else if (GWL_EXSTYLE == which) {
5015 ; /* FIXME - what is needed here */
5017 WARN ("Invalid style change %d\n",which);
5023 /*********************************************************************
5028 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5030 if ((key == VK_BACK) && (key_data & 0x2000)) {
5031 if (EDIT_EM_CanUndo(es))
5034 } else if (key == VK_UP || key == VK_DOWN) {
5035 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5038 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5042 /*********************************************************************
5047 static void EDIT_WM_Timer(EDITSTATE *es)
5049 if (es->region_posx < 0) {
5050 EDIT_MoveBackward(es, TRUE);
5051 } else if (es->region_posx > 0) {
5052 EDIT_MoveForward(es, TRUE);
5055 * FIXME: gotta do some vertical scrolling here, like
5056 * EDIT_EM_LineScroll(hwnd, 0, 1);
5060 /*********************************************************************
5065 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5069 if (!(es->style & ES_MULTILINE))
5072 if (!(es->style & ES_AUTOVSCROLL))
5081 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5082 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5083 (action == SB_PAGEUP ? "SB_PAGEUP" :
5085 EDIT_EM_Scroll(es, action);
5092 TRACE("SB_BOTTOM\n");
5093 dy = es->line_count - 1 - es->y_offset;
5096 TRACE("SB_THUMBTRACK %d\n", pos);
5097 es->flags |= EF_VSCROLL_TRACK;
5098 if(es->style & WS_VSCROLL)
5099 dy = pos - es->y_offset;
5102 /* Assume default scroll range 0-100 */
5105 if(pos < 0 || pos > 100) return 0;
5106 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5107 new_y = pos * (es->line_count - vlc) / 100;
5108 dy = es->line_count ? (new_y - es->y_offset) : 0;
5109 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5110 es->line_count, es->y_offset, pos, dy);
5113 case SB_THUMBPOSITION:
5114 TRACE("SB_THUMBPOSITION %d\n", pos);
5115 es->flags &= ~EF_VSCROLL_TRACK;
5116 if(es->style & WS_VSCROLL)
5117 dy = pos - es->y_offset;
5120 /* Assume default scroll range 0-100 */
5123 if(pos < 0 || pos > 100) return 0;
5124 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5125 new_y = pos * (es->line_count - vlc) / 100;
5126 dy = es->line_count ? (new_y - es->y_offset) : 0;
5127 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5128 es->line_count, es->y_offset, pos, dy);
5132 /* force scroll info update */
5133 EDIT_UpdateScrollInfo(es);
5134 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5138 TRACE("SB_ENDSCROLL\n");
5141 * FIXME : the next two are undocumented !
5142 * Are we doing the right thing ?
5143 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5144 * although it's also a regular control message.
5146 case EM_GETTHUMB: /* this one is used by NT notepad */
5150 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5151 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5154 /* Assume default scroll range 0-100 */
5155 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5156 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5158 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5161 case EM_LINESCROLL16:
5162 TRACE("EM_LINESCROLL16 %d\n", pos);
5167 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5172 EDIT_EM_LineScroll(es, 0, dy);
5176 /*********************************************************************
5181 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5183 if (es->flags & EF_UPDATE) {
5184 es->flags &= ~EF_UPDATE;
5185 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5187 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5191 /*********************************************************************
5196 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5198 if (es->flags & EF_UPDATE) {
5199 es->flags &= ~EF_UPDATE;
5200 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5202 InvalidateRect(es->hwndSelf, rc, bErase);