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
27 * -!ES_AUTOVSCROLL (every multi line control *is* auto vscroll)
28 * -!ES_AUTOHSCROLL (every single line control *is* auto hscroll)
30 * When there is no autoscrolling, the control should first check whether
31 * the new text would fit. If not, an EN_MAXTEXT should be sent.
32 * However, currently this would require the actual change to be made,
33 * then call EDIT_BuildLineDefs() and then find out that the new text doesn't
34 * fit. After all this, things should be put back in the state before the
35 * changes. Note that for multi line controls !ES_AUTOHSCROLL works : wordwrap.
50 #include "wine/winbase16.h"
51 #include "wine/winuser16.h"
52 #include "wine/unicode.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(edit);
60 WINE_DECLARE_DEBUG_CHANNEL(combo);
61 WINE_DECLARE_DEBUG_CHANNEL(relay);
63 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
64 FIXME: BTW, new specs say 65535 (do you dare ???) */
65 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
66 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
67 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
68 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
71 * extra flags for EDITSTATE.flags field
73 #define EF_MODIFIED 0x0001 /* text has been modified */
74 #define EF_FOCUSED 0x0002 /* we have input focus */
75 #define EF_UPDATE 0x0004 /* notify parent of changed state */
76 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
77 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
79 wrapped line, instead of in front of the next character */
80 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
84 END_0 = 0, /* line ends with terminating '\0' character */
85 END_WRAP, /* line is wrapped */
86 END_HARD, /* line ends with a hard return '\r\n' */
87 END_SOFT, /* line ends with a soft return '\r\r\n' */
88 END_RICH /* line ends with a single '\n' */
91 typedef struct tagLINEDEF {
92 INT length; /* bruto length of a line in bytes */
93 INT net_length; /* netto length of a line in visible characters */
95 INT width; /* width of the line in pixels */
96 INT index; /* line index into the buffer */
97 struct tagLINEDEF *next;
102 BOOL is_unicode; /* how the control was created */
103 LPWSTR text; /* the actual contents of the control */
104 UINT buffer_size; /* the size of the buffer in characters */
105 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
106 HFONT font; /* NULL means standard system font */
107 INT x_offset; /* scroll offset for multi lines this is in pixels
108 for single lines it's in characters */
109 INT line_height; /* height of a screen line in pixels */
110 INT char_width; /* average character width in pixels */
111 DWORD style; /* sane version of wnd->dwStyle */
112 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
113 INT undo_insert_count; /* number of characters inserted in sequence */
114 UINT undo_position; /* character index of the insertion and deletion */
115 LPWSTR undo_text; /* deleted text */
116 UINT undo_buffer_size; /* size of the deleted text buffer */
117 INT selection_start; /* == selection_end if no selection */
118 INT selection_end; /* == current caret position */
119 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
120 INT left_margin; /* in pixels */
121 INT right_margin; /* in pixels */
123 INT text_width; /* width of the widest line in pixels for multi line controls
124 and just line width for single line controls */
125 INT region_posx; /* Position of cursor relative to region: */
126 INT region_posy; /* -1: to left, 0: within, 1: to right */
127 EDITWORDBREAKPROC16 word_break_proc16;
128 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
129 INT line_count; /* number of lines */
130 INT y_offset; /* scroll offset in number of lines */
131 BOOL bCaptureState; /* flag indicating whether mouse was captured */
132 BOOL bEnableState; /* flag keeping the enable state */
133 HWND hwndSelf; /* the our window handle */
134 HWND hwndParent; /* Handle of parent for sending EN_* messages.
135 Even if parent will change, EN_* messages
136 should be sent to the first parent. */
137 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
139 * only for multi line controls
141 INT lock_count; /* amount of re-entries in the EditWndProc */
144 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
145 HLOCAL hloc32W; /* our unicode local memory block */
146 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
148 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
153 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
154 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
156 /* used for disabled or read-only edit control */
157 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
159 { /* Notify parent which has created this edit control */ \
160 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
161 SendMessageW(es->hwndParent, WM_COMMAND, \
162 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
163 (LPARAM)(es->hwndSelf)); \
166 /*********************************************************************
173 * These functions have trivial implementations
174 * We still like to call them internally
175 * "static inline" makes them more like macro's
177 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
178 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
179 static inline void EDIT_WM_Clear(EDITSTATE *es);
180 static inline void EDIT_WM_Cut(EDITSTATE *es);
183 * Helper functions only valid for one type of control
185 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
186 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
187 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
188 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
189 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
190 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
191 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
193 * Helper functions valid for both single line _and_ multi line controls
195 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
196 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
197 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
198 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
199 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
200 static void EDIT_LockBuffer(EDITSTATE *es);
201 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
202 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
203 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
204 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
205 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
206 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
207 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
208 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
209 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
210 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
211 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
212 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
213 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
214 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
215 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
217 * EM_XXX message handlers
219 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
220 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
221 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
222 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
223 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
224 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
225 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
226 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
227 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
228 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
229 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
230 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
231 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
232 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
233 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
234 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
235 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
236 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
237 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
238 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
239 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
240 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
241 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
242 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
243 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam);
244 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
245 static BOOL EDIT_EM_Undo(EDITSTATE *es);
247 * WM_XXX message handlers
249 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
250 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
251 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
252 static void EDIT_WM_Copy(EDITSTATE *es);
253 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
254 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
255 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
256 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
257 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
258 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
259 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
260 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
261 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
262 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
263 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
264 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
265 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
266 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam);
267 static void EDIT_WM_Paste(EDITSTATE *es);
268 static void EDIT_WM_SetFocus(EDITSTATE *es);
269 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
270 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode);
271 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
272 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
273 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
274 static void EDIT_WM_Timer(EDITSTATE *es);
275 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
276 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
277 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
279 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
280 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
282 /*********************************************************************
283 * edit class descriptor
285 const struct builtin_class_descr EDIT_builtin_class =
288 CS_DBLCLKS | CS_PARENTDC, /* style */
289 EditWndProcA, /* procA */
290 EditWndProcW, /* procW */
291 sizeof(EDITSTATE *), /* extra */
292 IDC_IBEAM, /* cursor */
297 /*********************************************************************
302 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
304 return (es->undo_insert_count || strlenW(es->undo_text));
308 /*********************************************************************
313 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
315 es->undo_insert_count = 0;
316 *es->undo_text = '\0';
320 /*********************************************************************
325 static inline void EDIT_WM_Clear(EDITSTATE *es)
327 static const WCHAR empty_stringW[] = {0};
329 /* Protect read-only edit control from modification */
330 if(es->style & ES_READONLY)
333 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
337 /*********************************************************************
342 static inline void EDIT_WM_Cut(EDITSTATE *es)
349 /**********************************************************************
352 * Returns the window version in case Wine emulates a later version
353 * of windows than the application expects.
355 * In a number of cases when windows runs an application that was
356 * designed for an earlier windows version, windows reverts
357 * to "old" behaviour of that earlier version.
359 * An example is a disabled edit control that needs to be painted.
360 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
361 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
362 * applications with an expected version 0f 4.0 or higher.
365 static DWORD get_app_version(void)
367 static DWORD version;
370 DWORD dwEmulatedVersion;
372 DWORD dwProcVersion = GetProcessVersion(0);
374 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
375 GetVersionExW( &info );
376 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
377 /* FIXME: this may not be 100% correct; see discussion on the
378 * wine developer list in Nov 1999 */
379 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
385 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
389 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
390 msg = WM_CTLCOLORSTATIC;
392 msg = WM_CTLCOLOREDIT;
394 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
395 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
398 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
401 return DefWindowProcW(hwnd, msg, wParam, lParam);
403 return DefWindowProcA(hwnd, msg, wParam, lParam);
406 /*********************************************************************
410 * The messages are in the order of the actual integer values
411 * (which can be found in include/windows.h)
412 * Wherever possible the 16 bit versions are converted to
413 * the 32 bit ones, so that we can 'fall through' to the
414 * helper functions. These are mostly 32 bit (with a few
415 * exceptions, clearly indicated by a '16' extension to their
419 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
420 WPARAM wParam, LPARAM lParam, BOOL unicode )
422 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
425 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
427 if (!es && msg != WM_NCCREATE)
428 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
429 else if (msg == WM_NCCREATE)
430 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
431 else if (msg == WM_DESTROY)
432 return EDIT_WM_Destroy(es);
435 if (es) 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, 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, 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));
743 result = MAKELONG(es->left_margin, es->right_margin);
746 case EM_GETLIMITTEXT:
747 result = es->buffer_limit;
751 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
755 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
758 /* End of the EM_ messages which were in numerical order; what order
759 * are these in? vaguely alphabetical?
763 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
765 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
767 int vk = (int)((LPMSG)lParam)->wParam;
769 if (vk == VK_RETURN && (GetWindowLongW( hwnd, GWL_STYLE ) & ES_WANTRETURN))
771 result |= DLGC_WANTMESSAGE;
773 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
775 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
776 result |= DLGC_WANTMESSAGE;
787 strng[0] = wParam >> 8;
788 strng[1] = wParam & 0xff;
789 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
790 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
791 EDIT_WM_Char(es, charW);
804 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
807 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
809 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
810 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
813 EDIT_WM_Char(es, charW);
822 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
826 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
835 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
838 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
842 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
843 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
844 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
846 result = EDIT_WM_Create(es, nameW);
848 HeapFree(GetProcessHeap(), 0, nameW);
857 es->bEnableState = (BOOL) wParam;
858 EDIT_UpdateText(es, NULL, TRUE);
862 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
866 result = (LRESULT)es->font;
870 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
873 case WM_GETTEXTLENGTH:
874 if (unicode) result = strlenW(es->text);
875 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
876 NULL, 0, NULL, NULL );
880 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
884 result = EDIT_WM_KeyDown(es, (INT)wParam);
888 result = EDIT_WM_KillFocus(es);
891 case WM_LBUTTONDBLCLK:
892 result = EDIT_WM_LButtonDblClk(es);
896 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
900 result = EDIT_WM_LButtonUp(es);
904 result = EDIT_WM_MButtonDown(es);
907 case WM_MOUSEACTIVATE:
908 result = MA_ACTIVATE;
912 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
916 EDIT_WM_Paint(es, wParam);
924 EDIT_WM_SetFocus(es);
928 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
932 /* FIXME: actually set an internal flag and behave accordingly */
936 EDIT_WM_SetText(es, lParam, unicode);
941 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
944 case WM_STYLECHANGED:
945 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
948 case WM_STYLECHANGING:
949 result = 0; /* See EDIT_WM_StyleChanged */
953 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
961 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
966 int gcWheelDelta = 0;
967 UINT pulScrollLines = 3;
968 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
970 if (wParam & (MK_SHIFT | MK_CONTROL)) {
971 result = DefWindowProcW(hwnd, msg, wParam, lParam);
974 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
975 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
977 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
978 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
979 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
984 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
988 if (es) EDIT_UnlockBuffer(es, FALSE);
990 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
995 /*********************************************************************
997 * EditWndProcW (USER32.@)
999 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1001 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1004 /*********************************************************************
1006 * EditWndProc (USER32.@)
1008 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1010 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1013 /*********************************************************************
1015 * EDIT_BuildLineDefs_ML
1017 * Build linked list of text lines.
1018 * Lines can end with '\0' (last line), a character (if it is wrapped),
1019 * a soft return '\r\r\n' or a hard return '\r\n'
1022 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1026 LPWSTR current_position, cp;
1028 LINEDEF *current_line;
1029 LINEDEF *previous_line;
1030 LINEDEF *start_line;
1031 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1032 INT line_count = es->line_count;
1033 INT orig_net_length;
1036 if (istart == iend && delta == 0)
1039 dc = GetDC(es->hwndSelf);
1041 old_font = SelectObject(dc, es->font);
1043 previous_line = NULL;
1044 current_line = es->first_line_def;
1046 /* Find starting line. istart must lie inside an existing line or
1047 * at the end of buffer */
1049 if (istart < current_line->index + current_line->length ||
1050 current_line->ending == END_0)
1053 previous_line = current_line;
1054 current_line = current_line->next;
1056 } while (current_line);
1058 if (!current_line) /* Error occurred start is not inside previous buffer */
1060 FIXME(" modification occurred outside buffer\n");
1061 ReleaseDC(es->hwndSelf, dc);
1065 /* Remember start of modifications in order to calculate update region */
1066 nstart_line = line_index;
1067 nstart_index = current_line->index;
1069 /* We must start to reformat from the previous line since the modifications
1070 * may have caused the line to wrap upwards. */
1071 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1074 current_line = previous_line;
1076 start_line = current_line;
1078 fw = es->format_rect.right - es->format_rect.left;
1079 current_position = es->text + current_line->index;
1081 if (current_line != start_line)
1083 if (!current_line || current_line->index + delta > current_position - es->text)
1085 /* The buffer has been expanded, create a new line and
1086 insert it into the link list */
1087 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1088 new_line->next = previous_line->next;
1089 previous_line->next = new_line;
1090 current_line = new_line;
1093 else if (current_line->index + delta < current_position - es->text)
1095 /* The previous line merged with this line so we delete this extra entry */
1096 previous_line->next = current_line->next;
1097 HeapFree(GetProcessHeap(), 0, current_line);
1098 current_line = previous_line->next;
1102 else /* current_line->index + delta == current_position */
1104 if (current_position - es->text > iend)
1105 break; /* We reached end of line modifications */
1106 /* else recalulate this line */
1110 current_line->index = current_position - es->text;
1111 orig_net_length = current_line->net_length;
1113 /* Find end of line */
1114 cp = current_position;
1116 if (*cp == '\n') break;
1117 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1122 /* Mark type of line termination */
1124 current_line->ending = END_0;
1125 current_line->net_length = strlenW(current_position);
1126 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1127 current_line->ending = END_SOFT;
1128 current_line->net_length = cp - current_position - 1;
1129 } else if (*cp == '\n') {
1130 current_line->ending = END_RICH;
1131 current_line->net_length = cp - current_position;
1133 current_line->ending = END_HARD;
1134 current_line->net_length = cp - current_position;
1137 /* Calculate line width */
1138 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1139 current_position, current_line->net_length,
1140 es->tabs_count, es->tabs));
1142 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1143 if (!(es->style & ES_AUTOHSCROLL)) {
1144 if (current_line->width > fw) {
1149 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1150 prev + 1, current_line->net_length, WB_RIGHT);
1151 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1152 current_position, next, es->tabs_count, es->tabs));
1153 } while (current_line->width <= fw);
1154 if (!prev) { /* Didn't find a line break so force a break */
1159 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1160 current_position, next, es->tabs_count, es->tabs));
1161 } while (current_line->width <= fw);
1166 /* If the first line we are calculating, wrapped before istart, we must
1167 * adjust istart in order for this to be reflected in the update region. */
1168 if (current_line->index == nstart_index && istart > current_line->index + prev)
1169 istart = current_line->index + prev;
1170 /* else if we are updating the previous line before the first line we
1171 * are re-calculating and it expanded */
1172 else if (current_line == start_line &&
1173 current_line->index != nstart_index && orig_net_length < prev)
1175 /* Line expanded due to an upwards line wrap so we must partially include
1176 * previous line in update region */
1177 nstart_line = line_index;
1178 nstart_index = current_line->index;
1179 istart = current_line->index + orig_net_length;
1182 current_line->net_length = prev;
1183 current_line->ending = END_WRAP;
1184 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1185 current_line->net_length, es->tabs_count, es->tabs));
1187 else if (orig_net_length < current_line->net_length &&
1188 current_line == start_line &&
1189 current_line->index != nstart_index) {
1190 /* The previous line expanded but it's still not as wide as the client rect */
1191 /* The expansion is due to an upwards line wrap so we must partially include
1192 it in the update region */
1193 nstart_line = line_index;
1194 nstart_index = current_line->index;
1195 istart = current_line->index + orig_net_length;
1200 /* Adjust length to include line termination */
1201 switch (current_line->ending) {
1203 current_line->length = current_line->net_length + 3;
1206 current_line->length = current_line->net_length + 1;
1209 current_line->length = current_line->net_length + 2;
1213 current_line->length = current_line->net_length;
1216 es->text_width = max(es->text_width, current_line->width);
1217 current_position += current_line->length;
1218 previous_line = current_line;
1219 current_line = current_line->next;
1221 } while (previous_line->ending != END_0);
1223 /* Finish adjusting line indexes by delta or remove hanging lines */
1224 if (previous_line->ending == END_0)
1226 LINEDEF *pnext = NULL;
1228 previous_line->next = NULL;
1229 while (current_line)
1231 pnext = current_line->next;
1232 HeapFree(GetProcessHeap(), 0, current_line);
1233 current_line = pnext;
1239 while (current_line)
1241 current_line->index += delta;
1242 current_line = current_line->next;
1246 /* Calculate rest of modification rectangle */
1251 * We calculate two rectangles. One for the first line which may have
1252 * an indent with respect to the format rect. The other is a format-width
1253 * rectangle that spans the rest of the lines that changed or moved.
1255 rc.top = es->format_rect.top + nstart_line * es->line_height -
1256 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1257 rc.bottom = rc.top + es->line_height;
1258 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1259 es->text + nstart_index, istart - nstart_index,
1260 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1261 rc.right = es->format_rect.right;
1262 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1265 rc.left = es->format_rect.left;
1266 rc.right = es->format_rect.right;
1268 * If lines were added or removed we must re-paint the remainder of the
1269 * lines since the remaining lines were either shifted up or down.
1271 if (line_count < es->line_count) /* We added lines */
1272 rc.bottom = es->line_count * es->line_height;
1273 else if (line_count > es->line_count) /* We removed lines */
1274 rc.bottom = line_count * es->line_height;
1276 rc.bottom = line_index * es->line_height;
1277 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1278 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1279 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1280 DeleteObject(tmphrgn);
1284 SelectObject(dc, old_font);
1286 ReleaseDC(es->hwndSelf, dc);
1289 /*********************************************************************
1291 * EDIT_CalcLineWidth_SL
1294 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1296 es->text_width = (short)LOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1299 /*********************************************************************
1301 * EDIT_CallWordBreakProc
1303 * Call appropriate WordBreakProc (internal or external).
1305 * Note: The "start" argument should always be an index referring
1306 * to es->text. The actual wordbreak proc might be
1307 * 16 bit, so we can't always pass any 32 bit LPSTR.
1308 * Hence we assume that es->text is the buffer that holds
1309 * the string under examination (we can decide this for ourselves).
1312 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1314 INT ret, iWndsLocks;
1316 /* To avoid any deadlocks, all the locks on the window structures
1317 must be suspended before the control is passed to the application */
1318 iWndsLocks = WIN_SuspendWndsLock();
1320 if (es->word_break_proc16) {
1327 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1328 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1329 segptr = K32WOWGlobalLock16(hglob16);
1330 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1331 args[4] = SELECTOROF(segptr);
1332 args[3] = OFFSETOF(segptr);
1336 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1337 ret = LOWORD(result);
1338 GlobalUnlock16(hglob16);
1339 GlobalFree16(hglob16);
1341 else if (es->word_break_proc)
1345 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1347 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1348 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1349 ret = wbpW(es->text + start, index, count, action);
1353 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1357 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1358 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1359 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1360 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1361 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1362 ret = wbpA(textA, index, countA, action);
1363 HeapFree(GetProcessHeap(), 0, textA);
1367 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1369 WIN_RestoreWndsLock(iWndsLocks);
1374 /*********************************************************************
1378 * Beware: This is not the function called on EM_CHARFROMPOS
1379 * The position _can_ be outside the formatting / client
1381 * The return value is only the character index
1384 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1390 if (es->style & ES_MULTILINE) {
1391 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1393 LINEDEF *line_def = es->first_line_def;
1395 while ((line > 0) && line_def->next) {
1396 line_index += line_def->length;
1397 line_def = line_def->next;
1400 x += es->x_offset - es->format_rect.left;
1401 if (x >= line_def->width) {
1403 *after_wrap = (line_def->ending == END_WRAP);
1404 return line_index + line_def->net_length;
1408 *after_wrap = FALSE;
1411 dc = GetDC(es->hwndSelf);
1413 old_font = SelectObject(dc, es->font);
1414 low = line_index + 1;
1415 high = line_index + line_def->net_length + 1;
1416 while (low < high - 1)
1418 INT mid = (low + high) / 2;
1419 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1425 *after_wrap = ((index == line_index + line_def->net_length) &&
1426 (line_def->ending == END_WRAP));
1431 *after_wrap = FALSE;
1432 x -= es->format_rect.left;
1434 return es->x_offset;
1435 text = EDIT_GetPasswordPointer_SL(es);
1436 dc = GetDC(es->hwndSelf);
1438 old_font = SelectObject(dc, es->font);
1442 INT high = es->x_offset;
1443 while (low < high - 1)
1445 INT mid = (low + high) / 2;
1446 GetTextExtentPoint32W( dc, text + mid,
1447 es->x_offset - mid, &size );
1448 if (size.cx > -x) low = mid;
1455 INT low = es->x_offset;
1456 INT high = strlenW(es->text) + 1;
1457 while (low < high - 1)
1459 INT mid = (low + high) / 2;
1460 GetTextExtentPoint32W( dc, text + es->x_offset,
1461 mid - es->x_offset, &size );
1462 if (size.cx > x) high = mid;
1467 if (es->style & ES_PASSWORD)
1468 HeapFree(GetProcessHeap(), 0, text);
1471 SelectObject(dc, old_font);
1472 ReleaseDC(es->hwndSelf, dc);
1477 /*********************************************************************
1481 * adjusts the point to be within the formatting rectangle
1482 * (so CharFromPos returns the nearest _visible_ character)
1485 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1487 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1488 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1492 /*********************************************************************
1496 * Calculates the bounding rectangle for a line from a starting
1497 * column to an ending column.
1500 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1502 INT line_index = EDIT_EM_LineIndex(es, line);
1504 if (es->style & ES_MULTILINE)
1505 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1507 rc->top = es->format_rect.top;
1508 rc->bottom = rc->top + es->line_height;
1509 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1510 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1514 /*********************************************************************
1516 * EDIT_GetPasswordPointer_SL
1518 * note: caller should free the (optionally) allocated buffer
1521 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1523 if (es->style & ES_PASSWORD) {
1524 INT len = strlenW(es->text);
1525 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1527 while(len) text[--len] = es->password_char;
1534 /*********************************************************************
1538 * This acts as a LOCAL_Lock(), but it locks only once. This way
1539 * you can call it whenever you like, without unlocking.
1541 * Initially the edit control allocates a HLOCAL32 buffer
1542 * (32 bit linear memory handler). However, 16 bit application
1543 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1544 * handler). From that moment on we have to keep using this 16 bit memory
1545 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1546 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1550 static void EDIT_LockBuffer(EDITSTATE *es)
1552 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1556 BOOL _16bit = FALSE;
1562 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1563 textA = LocalLock(es->hloc32A);
1564 countA = strlen(textA) + 1;
1568 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1569 textA = LOCAL_Lock(hInstance, es->hloc16);
1570 countA = strlen(textA) + 1;
1575 ERR("no buffer ... please report\n");
1582 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1583 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1584 if(countW_new > es->buffer_size + 1)
1586 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1587 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1588 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1591 es->hloc32W = hloc32W_new;
1592 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1593 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1596 WARN("FAILED! Will synchronize partially\n");
1600 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1601 es->text = LocalLock(es->hloc32W);
1605 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1607 LOCAL_Unlock(hInstance, es->hloc16);
1609 LocalUnlock(es->hloc32A);
1616 /*********************************************************************
1618 * EDIT_SL_InvalidateText
1620 * Called from EDIT_InvalidateText().
1621 * Does the job for single-line controls only.
1624 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1629 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1630 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1631 EDIT_UpdateText(es, &rc, TRUE);
1635 /*********************************************************************
1637 * EDIT_ML_InvalidateText
1639 * Called from EDIT_InvalidateText().
1640 * Does the job for multi-line controls only.
1643 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1645 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1646 INT sl = EDIT_EM_LineFromChar(es, start);
1647 INT el = EDIT_EM_LineFromChar(es, end);
1656 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1659 sc = start - EDIT_EM_LineIndex(es, sl);
1660 ec = end - EDIT_EM_LineIndex(es, el);
1661 if (sl < es->y_offset) {
1665 if (el > es->y_offset + vlc) {
1666 el = es->y_offset + vlc;
1667 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1669 GetClientRect(es->hwndSelf, &rc1);
1670 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1672 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1673 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1674 EDIT_UpdateText(es, &rcUpdate, TRUE);
1676 EDIT_GetLineRect(es, sl, sc,
1677 EDIT_EM_LineLength(es,
1678 EDIT_EM_LineIndex(es, sl)),
1680 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1681 EDIT_UpdateText(es, &rcUpdate, TRUE);
1682 for (l = sl + 1 ; l < el ; l++) {
1683 EDIT_GetLineRect(es, l, 0,
1684 EDIT_EM_LineLength(es,
1685 EDIT_EM_LineIndex(es, l)),
1687 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1688 EDIT_UpdateText(es, &rcUpdate, TRUE);
1690 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1691 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1692 EDIT_UpdateText(es, &rcUpdate, TRUE);
1697 /*********************************************************************
1699 * EDIT_InvalidateText
1701 * Invalidate the text from offset start upto, but not including,
1702 * offset end. Useful for (re)painting the selection.
1703 * Regions outside the linewidth are not invalidated.
1704 * end == -1 means end == TextLength.
1705 * start and end need not be ordered.
1708 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1714 end = strlenW(es->text);
1722 if (es->style & ES_MULTILINE)
1723 EDIT_ML_InvalidateText(es, start, end);
1725 EDIT_SL_InvalidateText(es, start, end);
1729 /*********************************************************************
1733 * Try to fit size + 1 characters in the buffer.
1735 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1739 if (size <= es->buffer_size)
1742 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1744 /* Force edit to unlock it's buffer. es->text now NULL */
1745 EDIT_UnlockBuffer(es, TRUE);
1748 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1749 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1750 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1751 es->hloc32W = hNew32W;
1752 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1756 EDIT_LockBuffer(es);
1758 if (es->buffer_size < size) {
1759 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1760 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1763 TRACE("We now have %d+1\n", es->buffer_size);
1769 /*********************************************************************
1773 * Try to fit size + 1 bytes in the undo buffer.
1776 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1780 if (size <= es->undo_buffer_size)
1783 TRACE("trying to ReAlloc to %d+1\n", size);
1785 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1786 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1787 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1792 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1798 /*********************************************************************
1803 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1805 INT e = es->selection_end;
1809 if ((es->style & ES_MULTILINE) && e &&
1810 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1812 if (e && (es->text[e - 1] == '\r'))
1816 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1817 EDIT_EM_ScrollCaret(es);
1821 /*********************************************************************
1825 * Only for multi line controls
1826 * Move the caret one line down, on a column with the nearest
1827 * x coordinate on the screen (might be a different column).
1830 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1832 INT s = es->selection_start;
1833 INT e = es->selection_end;
1834 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1835 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1836 INT x = (short)LOWORD(pos);
1837 INT y = (short)HIWORD(pos);
1839 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1842 EDIT_EM_SetSel(es, s, e, after_wrap);
1843 EDIT_EM_ScrollCaret(es);
1847 /*********************************************************************
1852 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1854 BOOL after_wrap = FALSE;
1857 /* Pass a high value in x to make sure of receiving the end of the line */
1858 if (es->style & ES_MULTILINE)
1859 e = EDIT_CharFromPos(es, 0x3fffffff,
1860 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1862 e = strlenW(es->text);
1863 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1864 EDIT_EM_ScrollCaret(es);
1868 /*********************************************************************
1873 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1875 INT e = es->selection_end;
1879 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1880 if (es->text[e] == '\n')
1882 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1886 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1887 EDIT_EM_ScrollCaret(es);
1891 /*********************************************************************
1895 * Home key: move to beginning of line.
1898 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1902 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1903 if (es->style & ES_MULTILINE)
1904 e = EDIT_CharFromPos(es, -es->x_offset,
1905 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1908 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1909 EDIT_EM_ScrollCaret(es);
1913 /*********************************************************************
1915 * EDIT_MovePageDown_ML
1917 * Only for multi line controls
1918 * Move the caret one page down, on a column with the nearest
1919 * x coordinate on the screen (might be a different column).
1922 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1924 INT s = es->selection_start;
1925 INT e = es->selection_end;
1926 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1927 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1928 INT x = (short)LOWORD(pos);
1929 INT y = (short)HIWORD(pos);
1931 e = EDIT_CharFromPos(es, x,
1932 y + (es->format_rect.bottom - es->format_rect.top),
1936 EDIT_EM_SetSel(es, s, e, after_wrap);
1937 EDIT_EM_ScrollCaret(es);
1941 /*********************************************************************
1943 * EDIT_MovePageUp_ML
1945 * Only for multi line controls
1946 * Move the caret one page up, on a column with the nearest
1947 * x coordinate on the screen (might be a different column).
1950 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1952 INT s = es->selection_start;
1953 INT e = es->selection_end;
1954 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1955 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1956 INT x = (short)LOWORD(pos);
1957 INT y = (short)HIWORD(pos);
1959 e = EDIT_CharFromPos(es, x,
1960 y - (es->format_rect.bottom - es->format_rect.top),
1964 EDIT_EM_SetSel(es, s, e, after_wrap);
1965 EDIT_EM_ScrollCaret(es);
1969 /*********************************************************************
1973 * Only for multi line controls
1974 * Move the caret one line up, on a column with the nearest
1975 * x coordinate on the screen (might be a different column).
1978 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1980 INT s = es->selection_start;
1981 INT e = es->selection_end;
1982 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1983 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1984 INT x = (short)LOWORD(pos);
1985 INT y = (short)HIWORD(pos);
1987 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1990 EDIT_EM_SetSel(es, s, e, after_wrap);
1991 EDIT_EM_ScrollCaret(es);
1995 /*********************************************************************
1997 * EDIT_MoveWordBackward
2000 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2002 INT s = es->selection_start;
2003 INT e = es->selection_end;
2008 l = EDIT_EM_LineFromChar(es, e);
2009 ll = EDIT_EM_LineLength(es, e);
2010 li = EDIT_EM_LineIndex(es, l);
2013 li = EDIT_EM_LineIndex(es, l - 1);
2014 e = li + EDIT_EM_LineLength(es, li);
2017 e = li + (INT)EDIT_CallWordBreakProc(es,
2018 li, e - li, ll, WB_LEFT);
2022 EDIT_EM_SetSel(es, s, e, FALSE);
2023 EDIT_EM_ScrollCaret(es);
2027 /*********************************************************************
2029 * EDIT_MoveWordForward
2032 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2034 INT s = es->selection_start;
2035 INT e = es->selection_end;
2040 l = EDIT_EM_LineFromChar(es, e);
2041 ll = EDIT_EM_LineLength(es, e);
2042 li = EDIT_EM_LineIndex(es, l);
2044 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2045 e = EDIT_EM_LineIndex(es, l + 1);
2047 e = li + EDIT_CallWordBreakProc(es,
2048 li, e - li + 1, ll, WB_RIGHT);
2052 EDIT_EM_SetSel(es, s, e, FALSE);
2053 EDIT_EM_ScrollCaret(es);
2057 /*********************************************************************
2062 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2064 INT s = es->selection_start;
2065 INT e = es->selection_end;
2072 if (es->style & ES_MULTILINE) {
2073 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2074 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2079 TRACE("line=%d\n", line);
2081 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2082 x = (short)LOWORD(pos);
2083 y = (short)HIWORD(pos);
2084 li = EDIT_EM_LineIndex(es, line);
2085 ll = EDIT_EM_LineLength(es, li);
2086 s = min(es->selection_start, es->selection_end);
2087 e = max(es->selection_start, es->selection_end);
2088 s = min(li + ll, max(li, s));
2089 e = min(li + ll, max(li, e));
2090 if (rev && (s != e) &&
2091 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2092 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2093 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2094 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2096 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2100 /*********************************************************************
2105 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2116 BkMode = GetBkMode(dc);
2117 BkColor = GetBkColor(dc);
2118 TextColor = GetTextColor(dc);
2120 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2121 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2122 SetBkMode( dc, OPAQUE);
2124 li = EDIT_EM_LineIndex(es, line);
2125 if (es->style & ES_MULTILINE) {
2126 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2127 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2129 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2130 TextOutW(dc, x, y, text + li + col, count);
2131 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2133 if (es->style & ES_PASSWORD)
2134 HeapFree(GetProcessHeap(), 0, text);
2137 SetBkColor(dc, BkColor);
2138 SetTextColor(dc, TextColor);
2139 SetBkMode( dc, BkMode);
2145 /*********************************************************************
2150 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2153 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2154 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2155 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2159 /*********************************************************************
2163 * note: this is not (exactly) the handler called on EM_SETRECTNP
2164 * it is also used to set the rect of a single line control
2167 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2169 CopyRect(&es->format_rect, rc);
2170 if (es->style & WS_BORDER) {
2171 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2172 es->format_rect.left += bw;
2173 es->format_rect.top += bw;
2174 es->format_rect.right -= bw;
2175 es->format_rect.bottom -= bw;
2177 es->format_rect.left += es->left_margin;
2178 es->format_rect.right -= es->right_margin;
2179 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2180 if (es->style & ES_MULTILINE)
2182 INT fw, vlc, max_x_offset, max_y_offset;
2184 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2185 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2187 /* correct es->x_offset */
2188 fw = es->format_rect.right - es->format_rect.left;
2189 max_x_offset = es->text_width - fw;
2190 if(max_x_offset < 0) max_x_offset = 0;
2191 if(es->x_offset > max_x_offset)
2192 es->x_offset = max_x_offset;
2194 /* correct es->y_offset */
2195 max_y_offset = es->line_count - vlc;
2196 if(max_y_offset < 0) max_y_offset = 0;
2197 if(es->y_offset > max_y_offset)
2198 es->y_offset = max_y_offset;
2200 /* force scroll info update */
2201 EDIT_UpdateScrollInfo(es);
2204 /* Windows doesn't care to fix text placement for SL controls */
2205 es->format_rect.bottom = es->format_rect.top + es->line_height;
2207 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2208 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2212 /*********************************************************************
2217 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2219 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2221 /* Edit window might be already destroyed */
2222 if(!IsWindow(es->hwndSelf))
2224 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2228 if (!es->lock_count) {
2229 ERR("lock_count == 0 ... please report\n");
2233 ERR("es->text == 0 ... please report\n");
2237 if (force || (es->lock_count == 1)) {
2240 BOOL _16bit = FALSE;
2242 UINT countW = strlenW(es->text) + 1;
2246 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2247 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2248 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2249 countA = LocalSize(es->hloc32A);
2250 if(countA_new > countA)
2253 UINT alloc_size = ROUND_TO_GROW(countA_new);
2254 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2255 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2258 es->hloc32A = hloc32A_new;
2259 countA = LocalSize(hloc32A_new);
2260 TRACE("Real new size %d bytes\n", countA);
2263 WARN("FAILED! Will synchronize partially\n");
2265 textA = LocalLock(es->hloc32A);
2269 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2270 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2271 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2272 countA = LOCAL_Size(hInstance, es->hloc16);
2273 if(countA_new > countA)
2275 HLOCAL16 hloc16_new;
2276 UINT alloc_size = ROUND_TO_GROW(countA_new);
2277 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2278 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2281 es->hloc16 = hloc16_new;
2282 countA = LOCAL_Size(hInstance, hloc16_new);
2283 TRACE("Real new size %d bytes\n", countA);
2286 WARN("FAILED! Will synchronize partially\n");
2288 textA = LOCAL_Lock(hInstance, es->hloc16);
2294 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2296 LOCAL_Unlock(hInstance, es->hloc16);
2298 LocalUnlock(es->hloc32A);
2301 LocalUnlock(es->hloc32W);
2305 ERR("no buffer ... please report\n");
2313 /*********************************************************************
2315 * EDIT_UpdateScrollInfo
2318 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2320 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2323 si.cbSize = sizeof(SCROLLINFO);
2324 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2326 si.nMax = es->line_count - 1;
2327 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2328 si.nPos = es->y_offset;
2329 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2330 si.nMin, si.nMax, si.nPage, si.nPos);
2331 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2334 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2337 si.cbSize = sizeof(SCROLLINFO);
2338 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2340 si.nMax = es->text_width - 1;
2341 si.nPage = es->format_rect.right - es->format_rect.left;
2342 si.nPos = es->x_offset;
2343 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2344 si.nMin, si.nMax, si.nPage, si.nPos);
2345 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2349 /*********************************************************************
2351 * EDIT_WordBreakProc
2353 * Find the beginning of words.
2354 * Note: unlike the specs for a WordBreakProc, this function only
2355 * allows to be called without linebreaks between s[0] upto
2356 * s[count - 1]. Remember it is only called
2357 * internally, so we can decide this for ourselves.
2360 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2364 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2374 if (s[index] == ' ') {
2375 while (index && (s[index] == ' '))
2378 while (index && (s[index] != ' '))
2380 if (s[index] == ' ')
2384 while (index && (s[index] != ' '))
2386 if (s[index] == ' ')
2396 if (s[index] == ' ')
2397 while ((index < count) && (s[index] == ' ')) index++;
2399 while (s[index] && (s[index] != ' ') && (index < count))
2401 while ((s[index] == ' ') && (index < count)) index++;
2405 case WB_ISDELIMITER:
2406 ret = (s[index] == ' ');
2409 ERR("unknown action code, please report !\n");
2416 /*********************************************************************
2420 * returns line number (not index) in high-order word of result.
2421 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2422 * to Richedit, not to the edit control. Original documentation is valid.
2423 * FIXME: do the specs mean to return -1 if outside client area or
2424 * if outside formatting rectangle ???
2427 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2435 GetClientRect(es->hwndSelf, &rc);
2436 if (!PtInRect(&rc, pt))
2439 index = EDIT_CharFromPos(es, x, y, NULL);
2440 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2444 /*********************************************************************
2448 * Enable or disable soft breaks.
2450 * This means: insert or remove the soft linebreak character (\r\r\n).
2451 * Take care to check if the text still fits the buffer after insertion.
2452 * If not, notify with EN_ERRSPACE.
2455 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2457 es->flags &= ~EF_USE_SOFTBRK;
2459 es->flags |= EF_USE_SOFTBRK;
2460 FIXME("soft break enabled, not implemented\n");
2466 /*********************************************************************
2470 * Hopefully this won't fire back at us.
2471 * We always start with a fixed buffer in the local heap.
2472 * Despite of the documentation says that the local heap is used
2473 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2474 * buffer on the local heap.
2477 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2481 if (!(es->style & ES_MULTILINE))
2485 hLocal = es->hloc32W;
2491 UINT countA, alloc_size;
2492 TRACE("Allocating 32-bit ANSI alias buffer\n");
2493 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2494 alloc_size = ROUND_TO_GROW(countA);
2495 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2497 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2500 textA = LocalLock(es->hloc32A);
2501 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2502 LocalUnlock(es->hloc32A);
2504 hLocal = es->hloc32A;
2507 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2512 /*********************************************************************
2516 * Hopefully this won't fire back at us.
2517 * We always start with a buffer in 32 bit linear memory.
2518 * However, with this message a 16 bit application requests
2519 * a handle of 16 bit local heap memory, where it expects to find
2521 * It's a pitty that from this moment on we have to use this
2522 * local heap, because applications may rely on the handle
2525 * In this function we'll try to switch to local heap.
2527 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2529 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2531 UINT countA, alloc_size;
2533 if (!(es->style & ES_MULTILINE))
2539 if (!LOCAL_HeapSize(hInstance)) {
2540 if (!LocalInit16(hInstance, 0,
2541 GlobalSize16(hInstance))) {
2542 ERR("could not initialize local heap\n");
2545 TRACE("local heap initialized\n");
2548 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2549 alloc_size = ROUND_TO_GROW(countA);
2551 TRACE("Allocating 16-bit ANSI alias buffer\n");
2552 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2553 ERR("could not allocate new 16 bit buffer\n");
2557 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2558 ERR("could not lock new 16 bit buffer\n");
2559 LOCAL_Free(hInstance, es->hloc16);
2564 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2565 LOCAL_Unlock(hInstance, es->hloc16);
2567 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2572 /*********************************************************************
2577 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2580 INT line_len, dst_len;
2583 if (es->style & ES_MULTILINE) {
2584 if (line >= es->line_count)
2588 i = EDIT_EM_LineIndex(es, line);
2590 line_len = EDIT_EM_LineLength(es, i);
2591 dst_len = *(WORD *)lParam;
2594 LPWSTR dst = (LPWSTR)lParam;
2595 if(dst_len <= line_len)
2597 memcpy(dst, src, dst_len * sizeof(WCHAR));
2600 else /* Append 0 if enough space */
2602 memcpy(dst, src, line_len * sizeof(WCHAR));
2609 LPSTR dst = (LPSTR)lParam;
2611 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2612 if(!ret) /* Insufficient buffer size */
2614 if(ret < dst_len) /* Append 0 if enough space */
2621 /*********************************************************************
2626 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2628 UINT s = es->selection_start;
2629 UINT e = es->selection_end;
2636 return MAKELONG(s, e);
2640 /*********************************************************************
2644 * FIXME: is this right ? (or should it be only VSCROLL)
2645 * (and maybe only for edit controls that really have their
2646 * own scrollbars) (and maybe only for multiline controls ?)
2647 * All in all: very poorly documented
2650 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2652 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2653 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2657 /*********************************************************************
2662 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2667 if (!(es->style & ES_MULTILINE))
2669 if (index > (INT)strlenW(es->text))
2670 return es->line_count - 1;
2672 index = min(es->selection_start, es->selection_end);
2675 line_def = es->first_line_def;
2676 index -= line_def->length;
2677 while ((index >= 0) && line_def->next) {
2679 line_def = line_def->next;
2680 index -= line_def->length;
2686 /*********************************************************************
2691 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2696 if (!(es->style & ES_MULTILINE))
2698 if (line >= es->line_count)
2702 line_def = es->first_line_def;
2704 INT index = es->selection_end - line_def->length;
2705 while ((index >= 0) && line_def->next) {
2706 line_index += line_def->length;
2707 line_def = line_def->next;
2708 index -= line_def->length;
2712 line_index += line_def->length;
2713 line_def = line_def->next;
2721 /*********************************************************************
2726 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2730 if (!(es->style & ES_MULTILINE))
2731 return strlenW(es->text);
2734 /* get the number of remaining non-selected chars of selected lines */
2735 INT32 l; /* line number */
2736 INT32 li; /* index of first char in line */
2738 l = EDIT_EM_LineFromChar(es, es->selection_start);
2739 /* # chars before start of selection area */
2740 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2741 l = EDIT_EM_LineFromChar(es, es->selection_end);
2742 /* # chars after end of selection */
2743 li = EDIT_EM_LineIndex(es, l);
2744 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2747 line_def = es->first_line_def;
2748 index -= line_def->length;
2749 while ((index >= 0) && line_def->next) {
2750 line_def = line_def->next;
2751 index -= line_def->length;
2753 return line_def->net_length;
2757 /*********************************************************************
2761 * NOTE: dx is in average character widths, dy - in lines;
2764 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2766 if (!(es->style & ES_MULTILINE))
2769 dx *= es->char_width;
2770 return EDIT_EM_LineScroll_internal(es, dx, dy);
2773 /*********************************************************************
2775 * EDIT_EM_LineScroll_internal
2777 * Version of EDIT_EM_LineScroll for internal use.
2778 * It doesn't refuse if ES_MULTILINE is set and assumes that
2779 * dx is in pixels, dy - in lines.
2782 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2785 INT x_offset_in_pixels;
2786 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2789 if (es->style & ES_MULTILINE)
2791 x_offset_in_pixels = es->x_offset;
2796 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2799 if (-dx > x_offset_in_pixels)
2800 dx = -x_offset_in_pixels;
2801 if (dx > es->text_width - x_offset_in_pixels)
2802 dx = es->text_width - x_offset_in_pixels;
2803 nyoff = max(0, es->y_offset + dy);
2804 if (nyoff >= es->line_count - lines_per_page)
2805 nyoff = es->line_count - lines_per_page;
2806 dy = (es->y_offset - nyoff) * es->line_height;
2811 es->y_offset = nyoff;
2812 if(es->style & ES_MULTILINE)
2815 es->x_offset += dx / es->char_width;
2817 GetClientRect(es->hwndSelf, &rc1);
2818 IntersectRect(&rc, &rc1, &es->format_rect);
2819 ScrollWindowEx(es->hwndSelf, -dx, dy,
2820 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2821 /* force scroll info update */
2822 EDIT_UpdateScrollInfo(es);
2824 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2825 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2826 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2827 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2832 /*********************************************************************
2837 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2839 INT len = strlenW(es->text);
2848 index = min(index, len);
2849 dc = GetDC(es->hwndSelf);
2851 old_font = SelectObject(dc, es->font);
2852 if (es->style & ES_MULTILINE) {
2853 l = EDIT_EM_LineFromChar(es, index);
2854 y = (l - es->y_offset) * es->line_height;
2855 li = EDIT_EM_LineIndex(es, l);
2856 if (after_wrap && (li == index) && l) {
2858 LINEDEF *line_def = es->first_line_def;
2860 line_def = line_def->next;
2863 if (line_def->ending == END_WRAP) {
2865 y -= es->line_height;
2866 li = EDIT_EM_LineIndex(es, l);
2869 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2870 es->tabs_count, es->tabs)) - es->x_offset;
2872 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2873 if (index < es->x_offset) {
2874 GetTextExtentPoint32W(dc, text + index,
2875 es->x_offset - index, &size);
2878 GetTextExtentPoint32W(dc, text + es->x_offset,
2879 index - es->x_offset, &size);
2883 if (es->style & ES_PASSWORD)
2884 HeapFree(GetProcessHeap(), 0, text);
2886 x += es->format_rect.left;
2887 y += es->format_rect.top;
2889 SelectObject(dc, old_font);
2890 ReleaseDC(es->hwndSelf, dc);
2891 return MAKELONG((INT16)x, (INT16)y);
2895 /*********************************************************************
2899 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2902 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2904 UINT strl = strlenW(lpsz_replace);
2905 UINT tl = strlenW(es->text);
2914 TRACE("%s, can_undo %d, send_update %d\n",
2915 debugstr_w(lpsz_replace), can_undo, send_update);
2917 s = es->selection_start;
2918 e = es->selection_end;
2920 if ((s == e) && !strl)
2925 /* Issue the EN_MAXTEXT notification and continue with replacing text
2926 * such that buffer limit is honored. */
2927 size = tl - (e - s) + strl;
2928 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
2929 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
2930 strl = es->buffer_limit - (tl - (e-s));
2933 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2937 /* there is something to be deleted */
2938 TRACE("deleting stuff.\n");
2940 utl = strlenW(es->undo_text);
2941 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2942 /* undo-buffer is extended to the right */
2943 EDIT_MakeUndoFit(es, utl + e - s);
2944 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2945 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2946 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2947 /* undo-buffer is extended to the left */
2948 EDIT_MakeUndoFit(es, utl + e - s);
2949 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2951 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2952 p[i] = (es->text + s)[i];
2953 es->undo_position = s;
2955 /* new undo-buffer */
2956 EDIT_MakeUndoFit(es, e - s);
2957 strncpyW(es->undo_text, es->text + s, e - s + 1);
2958 es->undo_text[e - s] = 0; /* ensure 0 termination */
2959 es->undo_position = s;
2961 /* any deletion makes the old insertion-undo invalid */
2962 es->undo_insert_count = 0;
2964 EDIT_EM_EmptyUndoBuffer(es);
2967 strcpyW(es->text + s, es->text + e);
2970 /* there is an insertion */
2972 if ((s == es->undo_position) ||
2973 ((es->undo_insert_count) &&
2974 (s == es->undo_position + es->undo_insert_count)))
2976 * insertion is new and at delete position or
2977 * an extension to either left or right
2979 es->undo_insert_count += strl;
2981 /* new insertion undo */
2982 es->undo_position = s;
2983 es->undo_insert_count = strl;
2984 /* new insertion makes old delete-buffer invalid */
2985 *es->undo_text = '\0';
2988 EDIT_EM_EmptyUndoBuffer(es);
2991 tl = strlenW(es->text);
2992 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));
2993 for (p = es->text + tl ; p >= es->text + s ; p--)
2995 for (i = 0 , p = es->text + s ; i < strl ; i++)
2996 p[i] = lpsz_replace[i];
2997 if(es->style & ES_UPPERCASE)
2998 CharUpperBuffW(p, strl);
2999 else if(es->style & ES_LOWERCASE)
3000 CharLowerBuffW(p, strl);
3003 if (es->style & ES_MULTILINE)
3005 INT s = min(es->selection_start, es->selection_end);
3007 hrgn = CreateRectRgn(0, 0, 0, 0);
3008 EDIT_BuildLineDefs_ML(es, s, s + strl,
3009 strl - abs(es->selection_end - es->selection_start), hrgn);
3012 EDIT_CalcLineWidth_SL(es);
3014 EDIT_EM_SetSel(es, s, s, FALSE);
3015 es->flags |= EF_MODIFIED;
3016 if (send_update) es->flags |= EF_UPDATE;
3019 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3023 EDIT_UpdateText(es, NULL, TRUE);
3025 EDIT_EM_ScrollCaret(es);
3027 /* force scroll info update */
3028 EDIT_UpdateScrollInfo(es);
3031 if(es->flags & EF_UPDATE)
3033 es->flags &= ~EF_UPDATE;
3034 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3039 /*********************************************************************
3044 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3048 if (!(es->style & ES_MULTILINE))
3049 return (LRESULT)FALSE;
3059 if (es->y_offset < es->line_count - 1)
3064 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3067 if (es->y_offset < es->line_count - 1)
3068 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3071 return (LRESULT)FALSE;
3074 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3075 /* check if we are going to move too far */
3076 if(es->y_offset + dy > es->line_count - vlc)
3077 dy = es->line_count - vlc - es->y_offset;
3079 /* Notification is done in EDIT_EM_LineScroll */
3081 EDIT_EM_LineScroll(es, 0, dy);
3083 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3087 /*********************************************************************
3092 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3094 if (es->style & ES_MULTILINE) {
3099 INT cw = es->char_width;
3104 l = EDIT_EM_LineFromChar(es, es->selection_end);
3105 li = EDIT_EM_LineIndex(es, l);
3106 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3107 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3108 if (l >= es->y_offset + vlc)
3109 dy = l - vlc + 1 - es->y_offset;
3110 if (l < es->y_offset)
3111 dy = l - es->y_offset;
3112 ww = es->format_rect.right - es->format_rect.left;
3113 if (x < es->format_rect.left)
3114 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3115 if (x > es->format_rect.right)
3116 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3119 /* check if we are going to move too far */
3120 if(es->x_offset + dx + ww > es->text_width)
3121 dx = es->text_width - ww - es->x_offset;
3123 EDIT_EM_LineScroll_internal(es, dx, dy);
3130 if (!(es->style & ES_AUTOHSCROLL))
3133 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3134 format_width = es->format_rect.right - es->format_rect.left;
3135 if (x < es->format_rect.left) {
3136 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3139 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3140 } while ((x < goal) && es->x_offset);
3141 /* FIXME: use ScrollWindow() somehow to improve performance */
3142 EDIT_UpdateText(es, NULL, TRUE);
3143 } else if (x > es->format_rect.right) {
3145 INT len = strlenW(es->text);
3146 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3149 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3150 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3151 } while ((x > goal) && (x_last > es->format_rect.right));
3152 /* FIXME: use ScrollWindow() somehow to improve performance */
3153 EDIT_UpdateText(es, NULL, TRUE);
3157 if(es->flags & EF_FOCUSED)
3158 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3162 /*********************************************************************
3166 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3169 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3171 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3173 if (!(es->style & ES_MULTILINE))
3177 WARN("called with NULL handle\n");
3181 EDIT_UnlockBuffer(es, TRUE);
3185 LOCAL_Free(hInstance, es->hloc16);
3186 es->hloc16 = (HLOCAL16)NULL;
3193 LocalFree(es->hloc32A);
3205 countA = LocalSize(hloc);
3206 textA = LocalLock(hloc);
3207 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3208 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3210 ERR("Could not allocate new unicode buffer\n");
3213 textW = LocalLock(hloc32W_new);
3214 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3215 LocalUnlock(hloc32W_new);
3219 LocalFree(es->hloc32W);
3221 es->hloc32W = hloc32W_new;
3225 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3227 EDIT_LockBuffer(es);
3229 es->x_offset = es->y_offset = 0;
3230 es->selection_start = es->selection_end = 0;
3231 EDIT_EM_EmptyUndoBuffer(es);
3232 es->flags &= ~EF_MODIFIED;
3233 es->flags &= ~EF_UPDATE;
3234 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3235 EDIT_UpdateText(es, NULL, TRUE);
3236 EDIT_EM_ScrollCaret(es);
3237 /* force scroll info update */
3238 EDIT_UpdateScrollInfo(es);
3242 /*********************************************************************
3246 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3249 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3251 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3257 if (!(es->style & ES_MULTILINE))
3261 WARN("called with NULL handle\n");
3265 EDIT_UnlockBuffer(es, TRUE);
3269 LocalFree(es->hloc32A);
3273 countA = LOCAL_Size(hInstance, hloc);
3274 textA = LOCAL_Lock(hInstance, hloc);
3275 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3276 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3278 ERR("Could not allocate new unicode buffer\n");
3281 textW = LocalLock(hloc32W_new);
3282 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3283 LocalUnlock(hloc32W_new);
3284 LOCAL_Unlock(hInstance, hloc);
3287 LocalFree(es->hloc32W);
3289 es->hloc32W = hloc32W_new;
3292 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3294 EDIT_LockBuffer(es);
3296 es->x_offset = es->y_offset = 0;
3297 es->selection_start = es->selection_end = 0;
3298 EDIT_EM_EmptyUndoBuffer(es);
3299 es->flags &= ~EF_MODIFIED;
3300 es->flags &= ~EF_UPDATE;
3301 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3302 EDIT_UpdateText(es, NULL, TRUE);
3303 EDIT_EM_ScrollCaret(es);
3304 /* force scroll info update */
3305 EDIT_UpdateScrollInfo(es);
3309 /*********************************************************************
3313 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3314 * However, the windows version is not complied to yet in all of edit.c
3316 * Additionally as the wrapper for RichEdit controls we need larger buffers
3317 * at present -1 will represent nolimit
3319 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3321 if (limit == 0xFFFFFFFF)
3322 es->buffer_limit = -1;
3323 else if (es->style & ES_MULTILINE) {
3325 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3327 es->buffer_limit = BUFLIMIT_MULTI;
3330 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3332 es->buffer_limit = BUFLIMIT_SINGLE;
3337 /*********************************************************************
3341 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3342 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3343 * margin according to the textmetrics of the current font.
3345 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3346 * of the char's width as the margin, but this is not how Windows handles this.
3347 * For all other fonts Windows sets the margins to zero.
3350 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3351 INT left, INT right)
3354 INT default_left_margin = 0; /* in pixels */
3355 INT default_right_margin = 0; /* in pixels */
3357 /* Set the default margins depending on the font */
3358 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3359 HDC dc = GetDC(es->hwndSelf);
3360 HFONT old_font = SelectObject(dc, es->font);
3361 GetTextMetricsW(dc, &tm);
3362 /* The default margins are only non zero for TrueType or Vector fonts */
3363 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3364 /* This must be calculated more exactly! But how? */
3365 default_left_margin = tm.tmAveCharWidth / 3;
3366 default_right_margin = tm.tmAveCharWidth / 3;
3368 SelectObject(dc, old_font);
3369 ReleaseDC(es->hwndSelf, dc);
3372 if (action & EC_LEFTMARGIN) {
3373 if (left != EC_USEFONTINFO)
3374 es->left_margin = left;
3376 es->left_margin = default_left_margin;
3379 if (action & EC_RIGHTMARGIN) {
3380 if (right != EC_USEFONTINFO)
3381 es->right_margin = right;
3383 es->right_margin = default_right_margin;
3385 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3389 /*********************************************************************
3391 * EM_SETPASSWORDCHAR
3394 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3398 if (es->style & ES_MULTILINE)
3401 if (es->password_char == c)
3404 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3405 es->password_char = c;
3407 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3408 es->style |= ES_PASSWORD;
3410 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3411 es->style &= ~ES_PASSWORD;
3413 EDIT_UpdateText(es, NULL, TRUE);
3417 /*********************************************************************
3421 * note: unlike the specs say: the order of start and end
3422 * _is_ preserved in Windows. (i.e. start can be > end)
3423 * In other words: this handler is OK
3426 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3428 UINT old_start = es->selection_start;
3429 UINT old_end = es->selection_end;
3430 UINT len = strlenW(es->text);
3432 if (start == (UINT)-1) {
3433 start = es->selection_end;
3434 end = es->selection_end;
3436 start = min(start, len);
3437 end = min(end, len);
3439 es->selection_start = start;
3440 es->selection_end = end;
3442 es->flags |= EF_AFTER_WRAP;
3444 es->flags &= ~EF_AFTER_WRAP;
3445 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3446 ORDER_UINT(start, end);
3447 ORDER_UINT(end, old_end);
3448 ORDER_UINT(start, old_start);
3449 ORDER_UINT(old_start, old_end);
3450 if (end != old_start)
3454 * ORDER_UINT32(end, old_start);
3455 * EDIT_InvalidateText(es, start, end);
3456 * EDIT_InvalidateText(es, old_start, old_end);
3457 * in place of the following if statement.
3459 if (old_start > end )
3461 EDIT_InvalidateText(es, start, end);
3462 EDIT_InvalidateText(es, old_start, old_end);
3466 EDIT_InvalidateText(es, start, old_start);
3467 EDIT_InvalidateText(es, end, old_end);
3470 else EDIT_InvalidateText(es, start, old_end);
3474 /*********************************************************************
3479 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3481 if (!(es->style & ES_MULTILINE))
3484 HeapFree(GetProcessHeap(), 0, es->tabs);
3485 es->tabs_count = count;
3489 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3490 memcpy(es->tabs, tabs, count * sizeof(INT));
3496 /*********************************************************************
3501 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3503 if (!(es->style & ES_MULTILINE))
3506 HeapFree(GetProcessHeap(), 0, es->tabs);
3507 es->tabs_count = count;
3512 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3513 for (i = 0 ; i < count ; i++)
3514 es->tabs[i] = *tabs++;
3520 /*********************************************************************
3522 * EM_SETWORDBREAKPROC
3525 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam)
3527 if (es->word_break_proc == (void *)lParam)
3530 es->word_break_proc = (void *)lParam;
3531 es->word_break_proc16 = NULL;
3533 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3534 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3535 EDIT_UpdateText(es, NULL, TRUE);
3540 /*********************************************************************
3542 * EM_SETWORDBREAKPROC16
3545 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3547 if (es->word_break_proc16 == wbp)
3550 es->word_break_proc = NULL;
3551 es->word_break_proc16 = wbp;
3552 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3553 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3554 EDIT_UpdateText(es, NULL, TRUE);
3559 /*********************************************************************
3564 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3569 /* As per MSDN spec, for a single-line edit control,
3570 the return value is always TRUE */
3571 if( es->style & ES_READONLY )
3572 return !(es->style & ES_MULTILINE);
3574 ulength = strlenW(es->undo_text);
3576 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3578 strcpyW(utext, es->undo_text);
3580 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3581 es->undo_insert_count, debugstr_w(utext));
3583 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3584 EDIT_EM_EmptyUndoBuffer(es);
3585 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3586 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3587 /* send the notification after the selection start and end are set */
3588 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3589 EDIT_EM_ScrollCaret(es);
3590 HeapFree(GetProcessHeap(), 0, utext);
3592 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3593 es->undo_insert_count, debugstr_w(es->undo_text));
3598 /*********************************************************************
3603 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3607 control = GetKeyState(VK_CONTROL) & 0x8000;
3611 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3612 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3615 if (es->style & ES_MULTILINE) {
3616 if (es->style & ES_READONLY) {
3617 EDIT_MoveHome(es, FALSE);
3618 EDIT_MoveDown_ML(es, FALSE);
3620 static const WCHAR cr_lfW[] = {'\r','\n',0};
3621 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3626 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3628 static const WCHAR tabW[] = {'\t',0};
3629 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3633 if (!(es->style & ES_READONLY) && !control) {
3634 if (es->selection_start != es->selection_end)
3637 /* delete character left of caret */
3638 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3639 EDIT_MoveBackward(es, TRUE);
3645 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3648 if (!(es->style & ES_READONLY))
3649 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3652 if (!(es->style & ES_READONLY))
3653 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3657 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3658 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3661 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3665 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3672 /*********************************************************************
3677 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3679 if (code || control)
3699 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3700 EDIT_EM_ScrollCaret(es);
3703 ERR("unknown menu item, please report\n");
3709 /*********************************************************************
3713 * Note: the resource files resource/sysres_??.rc cannot define a
3714 * single popup menu. Hence we use a (dummy) menubar
3715 * containing the single popup menu as its first item.
3717 * FIXME: the message identifiers have been chosen arbitrarily,
3718 * hence we use MF_BYPOSITION.
3719 * We might as well use the "real" values (anybody knows ?)
3720 * The menu definition is in resources/sysres_??.rc.
3721 * Once these are OK, we better use MF_BYCOMMAND here
3722 * (as we do in EDIT_WM_Command()).
3725 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3727 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3728 HMENU popup = GetSubMenu(menu, 0);
3729 UINT start = es->selection_start;
3730 UINT end = es->selection_end;
3732 ORDER_UINT(start, end);
3735 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3737 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3739 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3741 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3743 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3745 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3747 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3752 /*********************************************************************
3757 static void EDIT_WM_Copy(EDITSTATE *es)
3759 INT s = min(es->selection_start, es->selection_end);
3760 INT e = max(es->selection_start, es->selection_end);
3766 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3767 dst = GlobalLock(hdst);
3768 strncpyW(dst, es->text + s, e - s);
3769 dst[e - s] = 0; /* ensure 0 termination */
3770 TRACE("%s\n", debugstr_w(dst));
3772 OpenClipboard(es->hwndSelf);
3774 SetClipboardData(CF_UNICODETEXT, hdst);
3779 /*********************************************************************
3784 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3786 TRACE("%s\n", debugstr_w(name));
3788 * To initialize some final structure members, we call some helper
3789 * functions. However, since the EDITSTATE is not consistent (i.e.
3790 * not fully initialized), we should be very careful which
3791 * functions can be called, and in what order.
3793 EDIT_WM_SetFont(es, 0, FALSE);
3794 EDIT_EM_EmptyUndoBuffer(es);
3796 if (name && *name) {
3797 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3798 /* if we insert text to the editline, the text scrolls out
3799 * of the window, as the caret is placed after the insert
3800 * pos normally; thus we reset es->selection... to 0 and
3803 es->selection_start = es->selection_end = 0;
3804 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3805 * Messages are only to be sent when the USER does something to
3806 * change the contents. So I am removing this EN_CHANGE
3808 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3810 EDIT_EM_ScrollCaret(es);
3812 /* force scroll info update */
3813 EDIT_UpdateScrollInfo(es);
3814 /* The rule seems to return 1 here for success */
3815 /* Power Builder masked edit controls will crash */
3817 /* FIXME: is that in all cases so ? */
3822 /*********************************************************************
3827 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3832 while (LocalUnlock(es->hloc32W)) ;
3833 LocalFree(es->hloc32W);
3836 while (LocalUnlock(es->hloc32A)) ;
3837 LocalFree(es->hloc32A);
3840 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3841 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3842 LOCAL_Free(hInstance, es->hloc16);
3845 pc = es->first_line_def;
3849 HeapFree(GetProcessHeap(), 0, pc);
3853 SetWindowLongW( es->hwndSelf, 0, 0 );
3854 HeapFree(GetProcessHeap(), 0, es);
3860 /*********************************************************************
3865 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3867 /* we do the proper erase in EDIT_WM_Paint */
3872 /*********************************************************************
3877 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3879 if(!count) return 0;
3883 LPWSTR textW = (LPWSTR)lParam;
3884 lstrcpynW(textW, es->text, count);
3885 return strlenW(textW);
3889 LPSTR textA = (LPSTR)lParam;
3890 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3891 textA[count - 1] = 0; /* ensure 0 termination */
3892 return strlen(textA);
3896 /*********************************************************************
3901 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3906 if (!(es->style & ES_MULTILINE))
3909 if (!(es->style & ES_AUTOHSCROLL))
3913 fw = es->format_rect.right - es->format_rect.left;
3916 TRACE("SB_LINELEFT\n");
3918 dx = -es->char_width;
3921 TRACE("SB_LINERIGHT\n");
3922 if (es->x_offset < es->text_width)
3923 dx = es->char_width;
3926 TRACE("SB_PAGELEFT\n");
3928 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3931 TRACE("SB_PAGERIGHT\n");
3932 if (es->x_offset < es->text_width)
3933 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3941 TRACE("SB_RIGHT\n");
3942 if (es->x_offset < es->text_width)
3943 dx = es->text_width - es->x_offset;
3946 TRACE("SB_THUMBTRACK %d\n", pos);
3947 es->flags |= EF_HSCROLL_TRACK;
3948 if(es->style & WS_HSCROLL)
3949 dx = pos - es->x_offset;
3954 if(pos < 0 || pos > 100) return 0;
3955 /* Assume default scroll range 0-100 */
3956 fw = es->format_rect.right - es->format_rect.left;
3957 new_x = pos * (es->text_width - fw) / 100;
3958 dx = es->text_width ? (new_x - es->x_offset) : 0;
3961 case SB_THUMBPOSITION:
3962 TRACE("SB_THUMBPOSITION %d\n", pos);
3963 es->flags &= ~EF_HSCROLL_TRACK;
3964 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3965 dx = pos - es->x_offset;
3970 if(pos < 0 || pos > 100) return 0;
3971 /* Assume default scroll range 0-100 */
3972 fw = es->format_rect.right - es->format_rect.left;
3973 new_x = pos * (es->text_width - fw) / 100;
3974 dx = es->text_width ? (new_x - es->x_offset) : 0;
3977 /* force scroll info update */
3978 EDIT_UpdateScrollInfo(es);
3979 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
3983 TRACE("SB_ENDSCROLL\n");
3986 * FIXME : the next two are undocumented !
3987 * Are we doing the right thing ?
3988 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3989 * although it's also a regular control message.
3991 case EM_GETTHUMB: /* this one is used by NT notepad */
3995 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3996 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3999 /* Assume default scroll range 0-100 */
4000 INT fw = es->format_rect.right - es->format_rect.left;
4001 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4003 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4006 case EM_LINESCROLL16:
4007 TRACE("EM_LINESCROLL16\n");
4012 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4018 INT fw = es->format_rect.right - es->format_rect.left;
4019 /* check if we are going to move too far */
4020 if(es->x_offset + dx + fw > es->text_width)
4021 dx = es->text_width - fw - es->x_offset;
4023 EDIT_EM_LineScroll_internal(es, dx, 0);
4029 /*********************************************************************
4034 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4036 HWND hLBox = es->hwndListBox;
4044 hCombo = GetParent(es->hwndSelf);
4048 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4050 if (key == VK_UP || key == VK_DOWN)
4052 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4055 if (msg == WM_KEYDOWN || nEUI)
4056 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4062 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4064 /* make sure ComboLBox pops up */
4065 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4070 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4073 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4075 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4077 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4082 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4088 /*********************************************************************
4092 * Handling of special keys that don't produce a WM_CHAR
4093 * (i.e. non-printable keys) & Backspace & Delete
4096 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4101 if (GetKeyState(VK_MENU) & 0x8000)
4104 shift = GetKeyState(VK_SHIFT) & 0x8000;
4105 control = GetKeyState(VK_CONTROL) & 0x8000;
4110 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4115 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4116 EDIT_MoveUp_ML(es, shift);
4119 EDIT_MoveWordBackward(es, shift);
4121 EDIT_MoveBackward(es, shift);
4124 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4128 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4129 EDIT_MoveDown_ML(es, shift);
4131 EDIT_MoveWordForward(es, shift);
4133 EDIT_MoveForward(es, shift);
4136 EDIT_MoveHome(es, shift);
4139 EDIT_MoveEnd(es, shift);
4142 if (es->style & ES_MULTILINE)
4143 EDIT_MovePageUp_ML(es, shift);
4145 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4148 if (es->style & ES_MULTILINE)
4149 EDIT_MovePageDown_ML(es, shift);
4151 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4154 if (!(es->style & ES_READONLY) && !(shift && control)) {
4155 if (es->selection_start != es->selection_end) {
4162 /* delete character left of caret */
4163 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4164 EDIT_MoveBackward(es, TRUE);
4166 } else if (control) {
4167 /* delete to end of line */
4168 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4169 EDIT_MoveEnd(es, TRUE);
4172 /* delete character right of caret */
4173 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4174 EDIT_MoveForward(es, TRUE);
4182 if (!(es->style & ES_READONLY))
4188 /* If the edit doesn't want the return send a message to the default object */
4189 if(!(es->style & ES_WANTRETURN))
4191 HWND hwndParent = GetParent(es->hwndSelf);
4192 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4193 if (HIWORD(dw) == DC_HASDEFID)
4195 SendMessageW( hwndParent, WM_COMMAND,
4196 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4197 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4206 /*********************************************************************
4211 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4213 es->flags &= ~EF_FOCUSED;
4215 if(!(es->style & ES_NOHIDESEL))
4216 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4217 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4222 /*********************************************************************
4226 * The caret position has been set on the WM_LBUTTONDOWN message
4229 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4232 INT e = es->selection_end;
4237 if (!(es->flags & EF_FOCUSED))
4240 l = EDIT_EM_LineFromChar(es, e);
4241 li = EDIT_EM_LineIndex(es, l);
4242 ll = EDIT_EM_LineLength(es, e);
4243 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4244 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4245 EDIT_EM_SetSel(es, s, e, FALSE);
4246 EDIT_EM_ScrollCaret(es);
4251 /*********************************************************************
4256 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4261 SetFocus(es->hwndSelf);
4262 if (!(es->flags & EF_FOCUSED))
4265 es->bCaptureState = TRUE;
4266 SetCapture(es->hwndSelf);
4267 EDIT_ConfinePoint(es, &x, &y);
4268 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4269 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4270 EDIT_EM_ScrollCaret(es);
4271 es->region_posx = es->region_posy = 0;
4272 SetTimer(es->hwndSelf, 0, 100, NULL);
4277 /*********************************************************************
4282 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4284 if (es->bCaptureState) {
4285 KillTimer(es->hwndSelf, 0);
4286 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4288 es->bCaptureState = FALSE;
4293 /*********************************************************************
4298 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4300 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4305 /*********************************************************************
4310 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4316 if (GetCapture() != es->hwndSelf)
4320 * FIXME: gotta do some scrolling if outside client
4321 * area. Maybe reset the timer ?
4324 EDIT_ConfinePoint(es, &x, &y);
4325 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4326 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4327 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4328 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4329 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4334 /*********************************************************************
4338 * See also EDIT_WM_StyleChanged
4340 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4345 TRACE("Creating %s edit control, style = %08lx\n",
4346 unicode ? "Unicode" : "ANSI", lpcs->style);
4348 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4350 SetWindowLongW( hwnd, 0, (LONG)es );
4353 * Note: since the EDITSTATE has not been fully initialized yet,
4354 * we can't use any API calls that may send
4355 * WM_XXX messages before WM_NCCREATE is completed.
4358 es->is_unicode = unicode;
4359 es->style = lpcs->style;
4361 es->bEnableState = !(es->style & WS_DISABLED);
4363 es->hwndSelf = hwnd;
4364 /* Save parent, which will be notified by EN_* messages */
4365 es->hwndParent = lpcs->hwndParent;
4367 if (es->style & ES_COMBO)
4368 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4370 /* Number overrides lowercase overrides uppercase (at least it
4371 * does in Win95). However I'll bet that ES_NUMBER would be
4372 * invalid under Win 3.1.
4374 if (es->style & ES_NUMBER) {
4375 ; /* do not override the ES_NUMBER */
4376 } else if (es->style & ES_LOWERCASE) {
4377 es->style &= ~ES_UPPERCASE;
4379 if (es->style & ES_MULTILINE) {
4380 es->buffer_limit = BUFLIMIT_MULTI;
4381 if (es->style & WS_VSCROLL)
4382 es->style |= ES_AUTOVSCROLL;
4383 if (es->style & WS_HSCROLL)
4384 es->style |= ES_AUTOHSCROLL;
4385 es->style &= ~ES_PASSWORD;
4386 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4387 /* Confirmed - RIGHT overrides CENTER */
4388 if (es->style & ES_RIGHT)
4389 es->style &= ~ES_CENTER;
4390 es->style &= ~WS_HSCROLL;
4391 es->style &= ~ES_AUTOHSCROLL;
4394 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4395 es->style |= ES_AUTOVSCROLL;
4397 es->buffer_limit = BUFLIMIT_SINGLE;
4398 es->style &= ~ES_CENTER;
4399 es->style &= ~ES_RIGHT;
4400 es->style &= ~WS_HSCROLL;
4401 es->style &= ~WS_VSCROLL;
4402 es->style &= ~ES_AUTOVSCROLL;
4403 es->style &= ~ES_WANTRETURN;
4404 if (es->style & ES_PASSWORD)
4405 es->password_char = '*';
4407 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4408 es->style |= ES_AUTOHSCROLL;
4411 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4412 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4414 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4416 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4418 es->undo_buffer_size = es->buffer_size;
4420 if (es->style & ES_MULTILINE)
4421 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4426 * In Win95 look and feel, the WS_BORDER style is replaced by the
4427 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4428 * control a nonclient area so we don't need to draw the border.
4429 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4430 * a nonclient area and we should handle painting the border ourselves.
4432 * When making modifications please ensure that the code still works
4433 * for edit controls created directly with style 0x50800000, exStyle 0
4434 * (which should have a single pixel border)
4436 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4437 es->style &= ~WS_BORDER;
4438 else if (es->style & WS_BORDER)
4439 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4444 /*********************************************************************
4449 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam)
4460 BOOL rev = es->bEnableState &&
4461 ((es->flags & EF_FOCUSED) ||
4462 (es->style & ES_NOHIDESEL));
4464 dc = BeginPaint(es->hwndSelf, &ps);
4468 GetClientRect(es->hwndSelf, &rcClient);
4470 /* paint the background */
4471 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
4472 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
4473 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4474 GetClipBox(dc, &rc);
4475 FillRect(dc, &rc, brush);
4477 /* draw the border */
4478 if(es->style & WS_BORDER) {
4480 if(es->style & ES_MULTILINE) {
4481 if(es->style & WS_HSCROLL) rc.bottom++;
4482 if(es->style & WS_VSCROLL) rc.right++;
4484 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4486 IntersectClipRect(dc, es->format_rect.left,
4487 es->format_rect.top,
4488 es->format_rect.right,
4489 es->format_rect.bottom);
4490 if (es->style & ES_MULTILINE) {
4492 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4495 old_font = SelectObject(dc, es->font);
4496 EDIT_NotifyCtlColor(es, dc);
4498 if (!es->bEnableState)
4499 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4500 GetClipBox(dc, &rcRgn);
4501 if (es->style & ES_MULTILINE) {
4502 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4503 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4504 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4505 if (IntersectRect(&rc, &rcRgn, &rcLine))
4506 EDIT_PaintLine(es, dc, i, rev);
4509 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4510 if (IntersectRect(&rc, &rcRgn, &rcLine))
4511 EDIT_PaintLine(es, dc, 0, rev);
4514 SelectObject(dc, old_font);
4517 EndPaint(es->hwndSelf, &ps);
4521 /*********************************************************************
4526 static void EDIT_WM_Paste(EDITSTATE *es)
4531 /* Protect read-only edit control from modification */
4532 if(es->style & ES_READONLY)
4535 OpenClipboard(es->hwndSelf);
4536 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4537 src = (LPWSTR)GlobalLock(hsrc);
4538 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4545 /*********************************************************************
4550 static void EDIT_WM_SetFocus(EDITSTATE *es)
4552 es->flags |= EF_FOCUSED;
4553 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4554 EDIT_SetCaretPos(es, es->selection_end,
4555 es->flags & EF_AFTER_WRAP);
4556 if(!(es->style & ES_NOHIDESEL))
4557 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4558 ShowCaret(es->hwndSelf);
4559 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4563 /*********************************************************************
4567 * With Win95 look the margins are set to default font value unless
4568 * the system font (font == 0) is being set, in which case they are left
4572 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4580 dc = GetDC(es->hwndSelf);
4582 old_font = SelectObject(dc, font);
4583 GetTextMetricsW(dc, &tm);
4584 es->line_height = tm.tmHeight;
4585 es->char_width = tm.tmAveCharWidth;
4587 SelectObject(dc, old_font);
4588 ReleaseDC(es->hwndSelf, dc);
4589 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4590 EC_USEFONTINFO, EC_USEFONTINFO);
4592 /* Force the recalculation of the format rect for each font change */
4593 GetClientRect(es->hwndSelf, &r);
4594 EDIT_SetRectNP(es, &r);
4596 if (es->style & ES_MULTILINE)
4597 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4599 EDIT_CalcLineWidth_SL(es);
4602 EDIT_UpdateText(es, NULL, TRUE);
4603 if (es->flags & EF_FOCUSED) {
4605 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4606 EDIT_SetCaretPos(es, es->selection_end,
4607 es->flags & EF_AFTER_WRAP);
4608 ShowCaret(es->hwndSelf);
4613 /*********************************************************************
4618 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4619 * The modified flag is reset. No notifications are sent.
4621 * For single-line controls, reception of WM_SETTEXT triggers:
4622 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4625 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode)
4630 text = (LPWSTR)lParam;
4633 LPCSTR textA = (LPCSTR)lParam;
4634 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4635 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4636 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4639 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4641 TRACE("%s\n", debugstr_w(text));
4642 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4644 HeapFree(GetProcessHeap(), 0, text);
4646 static const WCHAR empty_stringW[] = {0};
4648 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4651 es->flags &= ~EF_MODIFIED;
4652 EDIT_EM_SetSel(es, 0, 0, FALSE);
4653 /* Send the notification after the selection start and end have been set
4654 * edit control doesn't send notification on WM_SETTEXT
4655 * if it is multiline, or it is part of combobox
4657 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4659 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4660 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4662 EDIT_EM_ScrollCaret(es);
4666 /*********************************************************************
4671 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4673 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4675 TRACE("width = %d, height = %d\n", width, height);
4676 SetRect(&rc, 0, 0, width, height);
4677 EDIT_SetRectNP(es, &rc);
4678 EDIT_UpdateText(es, NULL, TRUE);
4683 /*********************************************************************
4687 * This message is sent by SetWindowLong on having changed either the Style
4688 * or the extended style.
4690 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4692 * See also EDIT_WM_NCCreate
4694 * It appears that the Windows version of the edit control allows the style
4695 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4696 * style variable which will generally be different. In this function we
4697 * update the internal style based on what changed in the externally visible
4700 * Much of this content as based upon the MSDN, especially:
4701 * Platform SDK Documentation -> User Interface Services ->
4702 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4703 * Edit Control Styles
4705 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4707 if (GWL_STYLE == which) {
4708 DWORD style_change_mask;
4710 /* Only a subset of changes can be applied after the control
4713 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4715 if (es->style & ES_MULTILINE)
4716 style_change_mask |= ES_WANTRETURN;
4718 new_style = style->styleNew & style_change_mask;
4720 /* Number overrides lowercase overrides uppercase (at least it
4721 * does in Win95). However I'll bet that ES_NUMBER would be
4722 * invalid under Win 3.1.
4724 if (new_style & ES_NUMBER) {
4725 ; /* do not override the ES_NUMBER */
4726 } else if (new_style & ES_LOWERCASE) {
4727 new_style &= ~ES_UPPERCASE;
4730 es->style = (es->style & ~style_change_mask) | new_style;
4731 } else if (GWL_EXSTYLE == which) {
4732 ; /* FIXME - what is needed here */
4734 WARN ("Invalid style change %d\n",which);
4740 /*********************************************************************
4745 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4747 if ((key == VK_BACK) && (key_data & 0x2000)) {
4748 if (EDIT_EM_CanUndo(es))
4751 } else if (key == VK_UP || key == VK_DOWN) {
4752 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4755 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4759 /*********************************************************************
4764 static void EDIT_WM_Timer(EDITSTATE *es)
4766 if (es->region_posx < 0) {
4767 EDIT_MoveBackward(es, TRUE);
4768 } else if (es->region_posx > 0) {
4769 EDIT_MoveForward(es, TRUE);
4772 * FIXME: gotta do some vertical scrolling here, like
4773 * EDIT_EM_LineScroll(hwnd, 0, 1);
4777 /*********************************************************************
4782 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4786 if (!(es->style & ES_MULTILINE))
4789 if (!(es->style & ES_AUTOVSCROLL))
4798 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4799 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4800 (action == SB_PAGEUP ? "SB_PAGEUP" :
4802 EDIT_EM_Scroll(es, action);
4809 TRACE("SB_BOTTOM\n");
4810 dy = es->line_count - 1 - es->y_offset;
4813 TRACE("SB_THUMBTRACK %d\n", pos);
4814 es->flags |= EF_VSCROLL_TRACK;
4815 if(es->style & WS_VSCROLL)
4816 dy = pos - es->y_offset;
4819 /* Assume default scroll range 0-100 */
4822 if(pos < 0 || pos > 100) return 0;
4823 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4824 new_y = pos * (es->line_count - vlc) / 100;
4825 dy = es->line_count ? (new_y - es->y_offset) : 0;
4826 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4827 es->line_count, es->y_offset, pos, dy);
4830 case SB_THUMBPOSITION:
4831 TRACE("SB_THUMBPOSITION %d\n", pos);
4832 es->flags &= ~EF_VSCROLL_TRACK;
4833 if(es->style & WS_VSCROLL)
4834 dy = pos - es->y_offset;
4837 /* Assume default scroll range 0-100 */
4840 if(pos < 0 || pos > 100) return 0;
4841 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4842 new_y = pos * (es->line_count - vlc) / 100;
4843 dy = es->line_count ? (new_y - es->y_offset) : 0;
4844 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4845 es->line_count, es->y_offset, pos, dy);
4849 /* force scroll info update */
4850 EDIT_UpdateScrollInfo(es);
4851 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4855 TRACE("SB_ENDSCROLL\n");
4858 * FIXME : the next two are undocumented !
4859 * Are we doing the right thing ?
4860 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4861 * although it's also a regular control message.
4863 case EM_GETTHUMB: /* this one is used by NT notepad */
4867 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4868 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4871 /* Assume default scroll range 0-100 */
4872 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4873 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4875 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4878 case EM_LINESCROLL16:
4879 TRACE("EM_LINESCROLL16 %d\n", pos);
4884 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4889 EDIT_EM_LineScroll(es, 0, dy);
4893 /*********************************************************************
4898 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4900 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4901 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4905 /*********************************************************************
4910 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4912 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4913 InvalidateRect(es->hwndSelf, rc, bErase);