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
26 * - ES_NUMBER (new since win95)
28 * -!ES_AUTOVSCROLL (every multi line control *is* auto vscroll)
29 * -!ES_AUTOHSCROLL (every single line control *is* auto hscroll)
31 * When there is no autoscrolling, the control should first check whether
32 * the new text would fit. If not, an EN_MAXTEXT should be sent.
33 * However, currently this would require the actual change to be made,
34 * then call EDIT_BuildLineDefs() and then find out that the new text doesn't
35 * fit. After all this, things should be put back in the state before the
36 * changes. Note that for multi line controls !ES_AUTOHSCROLL works : wordwrap.
49 #include "wine/winbase16.h"
50 #include "wine/winuser16.h"
51 #include "wine/unicode.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(edit);
58 WINE_DECLARE_DEBUG_CHANNEL(combo);
59 WINE_DECLARE_DEBUG_CHANNEL(relay);
61 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
62 FIXME: BTW, new specs say 65535 (do you dare ???) */
63 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
64 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
65 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
66 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
69 * extra flags for EDITSTATE.flags field
71 #define EF_MODIFIED 0x0001 /* text has been modified */
72 #define EF_FOCUSED 0x0002 /* we have input focus */
73 #define EF_UPDATE 0x0004 /* notify parent of changed state */
74 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
75 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
76 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
77 wrapped line, instead of in front of the next character */
78 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
82 END_0 = 0, /* line ends with terminating '\0' character */
83 END_WRAP, /* line is wrapped */
84 END_HARD, /* line ends with a hard return '\r\n' */
85 END_SOFT, /* line ends with a soft return '\r\r\n' */
86 END_RICH /* line ends with a single '\n' */
89 typedef struct tagLINEDEF {
90 INT length; /* bruto length of a line in bytes */
91 INT net_length; /* netto length of a line in visible characters */
93 INT width; /* width of the line in pixels */
94 INT index; /* line index into the buffer */
95 struct tagLINEDEF *next;
100 BOOL is_unicode; /* how the control was created */
101 LPWSTR text; /* the actual contents of the control */
102 UINT buffer_size; /* the size of the buffer in characters */
103 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
104 HFONT font; /* NULL means standard system font */
105 INT x_offset; /* scroll offset for multi lines this is in pixels
106 for single lines it's in characters */
107 INT line_height; /* height of a screen line in pixels */
108 INT char_width; /* average character width in pixels */
109 DWORD style; /* sane version of wnd->dwStyle */
110 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
111 INT undo_insert_count; /* number of characters inserted in sequence */
112 UINT undo_position; /* character index of the insertion and deletion */
113 LPWSTR undo_text; /* deleted text */
114 UINT undo_buffer_size; /* size of the deleted text buffer */
115 INT selection_start; /* == selection_end if no selection */
116 INT selection_end; /* == current caret position */
117 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
118 INT left_margin; /* in pixels */
119 INT right_margin; /* in pixels */
121 INT text_width; /* width of the widest line in pixels for multi line controls
122 and just line width for single line controls */
123 INT region_posx; /* Position of cursor relative to region: */
124 INT region_posy; /* -1: to left, 0: within, 1: to right */
125 EDITWORDBREAKPROC16 word_break_proc16;
126 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
127 INT line_count; /* number of lines */
128 INT y_offset; /* scroll offset in number of lines */
129 BOOL bCaptureState; /* flag indicating whether mouse was captured */
130 BOOL bEnableState; /* flag keeping the enable state */
131 HWND hwndSelf; /* the our window handle */
132 HWND hwndParent; /* Handle of parent for sending EN_* messages.
133 Even if parent will change, EN_* messages
134 should be sent to the first parent. */
135 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
137 * only for multi line controls
139 INT lock_count; /* amount of re-entries in the EditWndProc */
142 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
143 HLOCAL hloc32W; /* our unicode local memory block */
144 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
146 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
151 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
152 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
154 /* used for disabled or read-only edit control */
155 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
157 { /* Notify parent which has created this edit control */ \
158 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
159 SendMessageW(es->hwndParent, WM_COMMAND, \
160 MAKEWPARAM(GetWindowLongW((es->hwndSelf),GWL_ID), wNotifyCode), \
161 (LPARAM)(es->hwndSelf)); \
164 /*********************************************************************
171 * These functions have trivial implementations
172 * We still like to call them internally
173 * "static inline" makes them more like macro's
175 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
176 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
177 static inline void EDIT_WM_Clear(EDITSTATE *es);
178 static inline void EDIT_WM_Cut(EDITSTATE *es);
181 * Helper functions only valid for one type of control
183 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
184 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
185 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
186 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
187 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
188 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
189 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
191 * Helper functions valid for both single line _and_ multi line controls
193 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
194 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
195 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
196 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
197 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
198 static void EDIT_LockBuffer(EDITSTATE *es);
199 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size, BOOL honor_limit);
200 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
201 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
202 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
203 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
204 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
205 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
206 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
207 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
208 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
209 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
210 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
211 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
212 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
213 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
215 * EM_XXX message handlers
217 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
218 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
219 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
220 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
221 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
222 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
223 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
224 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
225 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
226 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
227 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
228 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
229 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
230 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
231 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
232 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
233 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
234 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
235 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
236 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
237 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
238 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
239 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
240 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
241 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam);
242 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
243 static BOOL EDIT_EM_Undo(EDITSTATE *es);
245 * WM_XXX message handlers
247 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
248 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
249 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
250 static void EDIT_WM_Copy(EDITSTATE *es);
251 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
252 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
253 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
254 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
255 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
256 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
257 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
258 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
259 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
260 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
261 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
262 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
263 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
264 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam);
265 static void EDIT_WM_Paste(EDITSTATE *es);
266 static void EDIT_WM_SetFocus(EDITSTATE *es);
267 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
268 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode);
269 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
270 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
271 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
272 static void EDIT_WM_Timer(EDITSTATE *es);
273 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
274 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
275 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
277 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
278 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
280 /*********************************************************************
281 * edit class descriptor
283 const struct builtin_class_descr EDIT_builtin_class =
286 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
287 EditWndProcA, /* procA */
288 EditWndProcW, /* procW */
289 sizeof(EDITSTATE *), /* extra */
290 IDC_IBEAMA, /* cursor */
295 /*********************************************************************
300 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
302 return (es->undo_insert_count || strlenW(es->undo_text));
306 /*********************************************************************
311 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
313 es->undo_insert_count = 0;
314 *es->undo_text = '\0';
318 /*********************************************************************
323 static inline void EDIT_WM_Clear(EDITSTATE *es)
325 static const WCHAR empty_stringW[] = {0};
327 /* Protect read-only edit control from modification */
328 if(es->style & ES_READONLY)
331 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
335 /*********************************************************************
340 static inline void EDIT_WM_Cut(EDITSTATE *es)
347 /**********************************************************************
350 * Returns the window version in case Wine emulates a later version
351 * of windows then the application expects.
353 * In a number of cases when windows runs an application that was
354 * designed for an earlier windows version, windows reverts
355 * to "old" behaviour of that earlier version.
357 * An example is a disabled edit control that needs to be painted.
358 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
359 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
360 * applications with an expected version 0f 4.0 or higher.
363 static DWORD get_app_version(void)
365 static DWORD version;
368 DWORD dwEmulatedVersion;
370 DWORD dwProcVersion = GetProcessVersion(0);
372 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
373 GetVersionExW( &info );
374 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
375 /* FIXME: this may not be 100% correct; see discussion on the
376 * wine developer list in Nov 1999 */
377 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
383 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
387 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
388 msg = WM_CTLCOLORSTATIC;
390 msg = WM_CTLCOLOREDIT;
392 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
393 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
396 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
399 return DefWindowProcW(hwnd, msg, wParam, lParam);
401 return DefWindowProcA(hwnd, msg, wParam, lParam);
404 /*********************************************************************
408 * The messages are in the order of the actual integer values
409 * (which can be found in include/windows.h)
410 * Wherever possible the 16 bit versions are converted to
411 * the 32 bit ones, so that we can 'fall through' to the
412 * helper functions. These are mostly 32 bit (with a few
413 * exceptions, clearly indicated by a '16' extension to their
417 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
418 WPARAM wParam, LPARAM lParam, BOOL unicode )
420 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
423 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, msg, wParam, lParam);
425 if (!es && msg != WM_NCCREATE)
426 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
427 else if (msg == WM_NCCREATE)
428 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
429 else if (msg == WM_DESTROY)
430 return EDIT_WM_Destroy(es);
433 if (es) EDIT_LockBuffer(es);
441 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
445 if (SLOWORD(lParam) == -1)
446 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
448 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
450 EDIT_EM_ScrollCaret(es);
454 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
455 EDIT_EM_ScrollCaret(es);
461 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
465 CopyRect((LPRECT)lParam, &es->format_rect);
469 if ((es->style & ES_MULTILINE) && lParam) {
471 CONV_RECT16TO32(MapSL(lParam), &rc);
472 EDIT_SetRectNP(es, &rc);
473 EDIT_UpdateText(es, NULL, TRUE);
477 if ((es->style & ES_MULTILINE) && lParam) {
478 EDIT_SetRectNP(es, (LPRECT)lParam);
479 EDIT_UpdateText(es, NULL, TRUE);
484 if ((es->style & ES_MULTILINE) && lParam) {
486 CONV_RECT16TO32(MapSL(lParam), &rc);
487 EDIT_SetRectNP(es, &rc);
491 if ((es->style & ES_MULTILINE) && lParam)
492 EDIT_SetRectNP(es, (LPRECT)lParam);
497 result = EDIT_EM_Scroll(es, (INT)wParam);
500 case EM_LINESCROLL16:
501 wParam = (WPARAM)(INT)SHIWORD(lParam);
502 lParam = (LPARAM)(INT)SLOWORD(lParam);
505 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
508 case EM_SCROLLCARET16:
510 EDIT_EM_ScrollCaret(es);
516 result = ((es->flags & EF_MODIFIED) != 0);
522 es->flags |= EF_MODIFIED;
524 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
527 case EM_GETLINECOUNT16:
528 case EM_GETLINECOUNT:
529 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
533 if ((INT16)wParam == -1)
537 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
541 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
544 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
548 result = (LRESULT)EDIT_EM_GetHandle16(es);
551 result = (LRESULT)EDIT_EM_GetHandle(es);
556 result = EDIT_EM_GetThumb(es);
559 /* these messages missing from specs */
568 FIXME("undocumented message 0x%x, please report\n", msg);
569 result = DefWindowProcW(hwnd, msg, wParam, lParam);
572 case EM_LINELENGTH16:
574 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
577 case EM_REPLACESEL16:
578 lParam = (LPARAM)MapSL(lParam);
579 unicode = FALSE; /* 16-bit message is always ascii */
586 textW = (LPWSTR)lParam;
589 LPSTR textA = (LPSTR)lParam;
590 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
591 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
592 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
595 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
599 HeapFree(GetProcessHeap(), 0, textW);
604 lParam = (LPARAM)MapSL(lParam);
605 unicode = FALSE; /* 16-bit message is always ascii */
608 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
612 case EM_SETLIMITTEXT:
613 EDIT_EM_SetLimitText(es, (INT)wParam);
618 result = (LRESULT)EDIT_EM_CanUndo(es);
624 result = (LRESULT)EDIT_EM_Undo(es);
629 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
632 case EM_LINEFROMCHAR16:
633 case EM_LINEFROMCHAR:
634 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
637 case EM_SETTABSTOPS16:
638 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
641 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
644 case EM_SETPASSWORDCHAR16:
645 unicode = FALSE; /* 16-bit message is always ascii */
647 case EM_SETPASSWORDCHAR:
652 charW = (WCHAR)wParam;
656 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
659 EDIT_EM_SetPasswordChar(es, charW);
663 case EM_EMPTYUNDOBUFFER16:
664 case EM_EMPTYUNDOBUFFER:
665 EDIT_EM_EmptyUndoBuffer(es);
668 case EM_GETFIRSTVISIBLELINE16:
669 result = es->y_offset;
671 case EM_GETFIRSTVISIBLELINE:
672 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
675 case EM_SETREADONLY16:
678 SetWindowLongW( hwnd, GWL_STYLE,
679 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
680 es->style |= ES_READONLY;
682 SetWindowLongW( hwnd, GWL_STYLE,
683 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
684 es->style &= ~ES_READONLY;
689 case EM_SETWORDBREAKPROC16:
690 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
692 case EM_SETWORDBREAKPROC:
693 EDIT_EM_SetWordBreakProc(es, lParam);
696 case EM_GETWORDBREAKPROC16:
697 result = (LRESULT)es->word_break_proc16;
699 case EM_GETWORDBREAKPROC:
700 result = (LRESULT)es->word_break_proc;
703 case EM_GETPASSWORDCHAR16:
704 unicode = FALSE; /* 16-bit message is always ascii */
706 case EM_GETPASSWORDCHAR:
709 result = es->password_char;
712 WCHAR charW = es->password_char;
714 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
720 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
723 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
727 result = MAKELONG(es->left_margin, es->right_margin);
730 case EM_GETLIMITTEXT:
731 result = es->buffer_limit;
735 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
739 result = EDIT_EM_CharFromPos(es, SLOWORD(lParam), SHIWORD(lParam));
742 /* End of the EM_ messages which were in numerical order; what order
743 * are these in? vaguely alphabetical?
747 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
749 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
751 int vk = (int)((LPMSG)lParam)->wParam;
753 if (vk == VK_RETURN && (GetWindowLongW( hwnd, GWL_STYLE ) & ES_WANTRETURN))
755 result |= DLGC_WANTMESSAGE;
757 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
759 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
760 result |= DLGC_WANTMESSAGE;
774 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
777 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
779 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
780 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
783 EDIT_WM_Char(es, charW);
792 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
796 EDIT_WM_ContextMenu(es, SLOWORD(lParam), SHIWORD(lParam));
805 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
808 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
812 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
813 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
814 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
816 result = EDIT_WM_Create(es, nameW);
818 HeapFree(GetProcessHeap(), 0, nameW);
827 es->bEnableState = (BOOL) wParam;
828 EDIT_UpdateText(es, NULL, TRUE);
832 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
836 result = (LRESULT)es->font;
840 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
843 case WM_GETTEXTLENGTH:
844 if (unicode) result = strlenW(es->text);
845 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
846 NULL, 0, NULL, NULL );
850 result = EDIT_WM_HScroll(es, LOWORD(wParam), SHIWORD(wParam));
854 result = EDIT_WM_KeyDown(es, (INT)wParam);
858 result = EDIT_WM_KillFocus(es);
861 case WM_LBUTTONDBLCLK:
862 result = EDIT_WM_LButtonDblClk(es);
866 result = EDIT_WM_LButtonDown(es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
870 result = EDIT_WM_LButtonUp(es);
874 result = EDIT_WM_MButtonDown(es);
877 case WM_MOUSEACTIVATE:
879 * FIXME: maybe DefWindowProc() screws up, but it seems that
880 * modeless dialog boxes need this. If we don't do this, the focus
881 * will _not_ be set by DefWindowProc() for edit controls in a
882 * modeless dialog box ???
885 result = MA_ACTIVATE;
889 result = EDIT_WM_MouseMove(es, SLOWORD(lParam), SHIWORD(lParam));
893 EDIT_WM_Paint(es, wParam);
901 EDIT_WM_SetFocus(es);
905 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
909 /* FIXME: actually set an internal flag and behave accordingly */
913 EDIT_WM_SetText(es, lParam, unicode);
918 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
921 case WM_STYLECHANGED:
922 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
925 case WM_STYLECHANGING:
926 result = 0; /* See EDIT_WM_StyleChanged */
930 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
938 result = EDIT_WM_VScroll(es, LOWORD(wParam), SHIWORD(wParam));
943 int gcWheelDelta = 0;
944 UINT pulScrollLines = 3;
945 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
947 if (wParam & (MK_SHIFT | MK_CONTROL)) {
948 result = DefWindowProcW(hwnd, msg, wParam, lParam);
951 gcWheelDelta -= SHIWORD(wParam);
952 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
954 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
955 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
956 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
961 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
965 if (es) EDIT_UnlockBuffer(es, FALSE);
970 /*********************************************************************
972 * EditWndProcW (USER32.@)
974 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
976 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
979 /*********************************************************************
981 * EditWndProc (USER32.@)
983 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
985 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
988 /*********************************************************************
990 * EDIT_BuildLineDefs_ML
992 * Build linked list of text lines.
993 * Lines can end with '\0' (last line), a character (if it is wrapped),
994 * a soft return '\r\r\n' or a hard return '\r\n'
997 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1001 LPWSTR current_position, cp;
1003 LINEDEF *current_line;
1004 LINEDEF *previous_line;
1005 LINEDEF *start_line;
1006 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1007 INT line_count = es->line_count;
1008 INT orig_net_length;
1011 if (istart == iend && delta == 0)
1014 dc = GetDC(es->hwndSelf);
1016 old_font = SelectObject(dc, es->font);
1018 previous_line = NULL;
1019 current_line = es->first_line_def;
1021 /* Find starting line. istart must lie inside an existing line or
1022 * at the end of buffer */
1024 if (istart < current_line->index + current_line->length ||
1025 current_line->ending == END_0)
1028 previous_line = current_line;
1029 current_line = current_line->next;
1031 } while (current_line);
1033 if (!current_line) /* Error occurred start is not inside previous buffer */
1035 FIXME(" modification occurred outside buffer\n");
1039 /* Remember start of modifications in order to calculate update region */
1040 nstart_line = line_index;
1041 nstart_index = current_line->index;
1043 /* We must start to reformat from the previous line since the modifications
1044 * may have caused the line to wrap upwards. */
1045 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1048 current_line = previous_line;
1050 start_line = current_line;
1052 fw = es->format_rect.right - es->format_rect.left;
1053 current_position = es->text + current_line->index;
1055 if (current_line != start_line)
1057 if (!current_line || current_line->index + delta > current_position - es->text)
1059 /* The buffer has been expanded, create a new line and
1060 insert it into the link list */
1061 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1062 new_line->next = previous_line->next;
1063 previous_line->next = new_line;
1064 current_line = new_line;
1067 else if (current_line->index + delta < current_position - es->text)
1069 /* The previous line merged with this line so we delete this extra entry */
1070 previous_line->next = current_line->next;
1071 HeapFree(GetProcessHeap(), 0, current_line);
1072 current_line = previous_line->next;
1076 else /* current_line->index + delta == current_position */
1078 if (current_position - es->text > iend)
1079 break; /* We reached end of line modifications */
1080 /* else recalulate this line */
1084 current_line->index = current_position - es->text;
1085 orig_net_length = current_line->net_length;
1087 /* Find end of line */
1088 cp = current_position;
1090 if (*cp == '\n') break;
1091 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1096 /* Mark type of line termination */
1098 current_line->ending = END_0;
1099 current_line->net_length = strlenW(current_position);
1100 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1101 current_line->ending = END_SOFT;
1102 current_line->net_length = cp - current_position - 1;
1103 } else if (*cp == '\n') {
1104 current_line->ending = END_RICH;
1105 current_line->net_length = cp - current_position;
1107 current_line->ending = END_HARD;
1108 current_line->net_length = cp - current_position;
1111 /* Calculate line width */
1112 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1113 current_position, current_line->net_length,
1114 es->tabs_count, es->tabs));
1116 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1117 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1122 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1123 prev + 1, current_line->net_length, WB_RIGHT);
1124 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1125 current_position, next, es->tabs_count, es->tabs));
1126 } while (current_line->width <= fw);
1127 if (!prev) { /* Didn't find a line break so force a break */
1132 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1133 current_position, next, es->tabs_count, es->tabs));
1134 } while (current_line->width <= fw);
1139 /* If the first line we are calculating, wrapped before istart, we must
1140 * adjust istart in order for this to be reflected in the update region. */
1141 if (current_line->index == nstart_index && istart > current_line->index + prev)
1142 istart = current_line->index + prev;
1143 /* else if we are updating the previous line before the first line we
1144 * are re-calculating and it expanded */
1145 else if (current_line == start_line &&
1146 current_line->index != nstart_index && orig_net_length < prev)
1148 /* Line expanded due to an upwards line wrap so we must partially include
1149 * previous line in update region */
1150 nstart_line = line_index;
1151 nstart_index = current_line->index;
1152 istart = current_line->index + orig_net_length;
1155 current_line->net_length = prev;
1156 current_line->ending = END_WRAP;
1157 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1158 current_line->net_length, es->tabs_count, es->tabs));
1162 /* Adjust length to include line termination */
1163 switch (current_line->ending) {
1165 current_line->length = current_line->net_length + 3;
1168 current_line->length = current_line->net_length + 1;
1171 current_line->length = current_line->net_length + 2;
1175 current_line->length = current_line->net_length;
1178 es->text_width = max(es->text_width, current_line->width);
1179 current_position += current_line->length;
1180 previous_line = current_line;
1181 current_line = current_line->next;
1183 } while (previous_line->ending != END_0);
1185 /* Finish adjusting line indexes by delta or remove hanging lines */
1186 if (previous_line->ending == END_0)
1188 LINEDEF *pnext = NULL;
1190 previous_line->next = NULL;
1191 while (current_line)
1193 pnext = current_line->next;
1194 HeapFree(GetProcessHeap(), 0, current_line);
1195 current_line = pnext;
1201 while (current_line)
1203 current_line->index += delta;
1204 current_line = current_line->next;
1208 /* Calculate rest of modification rectangle */
1213 * We calculate two rectangles. One for the first line which may have
1214 * an indent with respect to the format rect. The other is a format-width
1215 * rectangle that spans the rest of the lines that changed or moved.
1217 rc.top = es->format_rect.top + nstart_line * es->line_height -
1218 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1219 rc.bottom = rc.top + es->line_height;
1220 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1221 es->text + nstart_index, istart - nstart_index,
1222 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1223 rc.right = es->format_rect.right;
1224 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1227 rc.left = es->format_rect.left;
1228 rc.right = es->format_rect.right;
1230 * If lines were added or removed we must re-paint the remainder of the
1231 * lines since the remaining lines were either shifted up or down.
1233 if (line_count < es->line_count) /* We added lines */
1234 rc.bottom = es->line_count * es->line_height;
1235 else if (line_count > es->line_count) /* We removed lines */
1236 rc.bottom = line_count * es->line_height;
1238 rc.bottom = line_index * es->line_height;
1239 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1240 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1241 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1242 DeleteObject(tmphrgn);
1246 SelectObject(dc, old_font);
1248 ReleaseDC(es->hwndSelf, dc);
1251 /*********************************************************************
1253 * EDIT_CalcLineWidth_SL
1256 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1258 es->text_width = SLOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1261 /*********************************************************************
1263 * EDIT_CallWordBreakProc
1265 * Call appropriate WordBreakProc (internal or external).
1267 * Note: The "start" argument should always be an index referring
1268 * to es->text. The actual wordbreak proc might be
1269 * 16 bit, so we can't always pass any 32 bit LPSTR.
1270 * Hence we assume that es->text is the buffer that holds
1271 * the string under examination (we can decide this for ourselves).
1274 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1276 INT ret, iWndsLocks;
1278 /* To avoid any deadlocks, all the locks on the window structures
1279 must be suspended before the control is passed to the application */
1280 iWndsLocks = WIN_SuspendWndsLock();
1282 if (es->word_break_proc16) {
1289 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1290 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1291 segptr = K32WOWGlobalLock16(hglob16);
1292 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1293 args[4] = SELECTOROF(segptr);
1294 args[3] = OFFSETOF(segptr);
1298 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1299 ret = LOWORD(result);
1300 GlobalUnlock16(hglob16);
1301 GlobalFree16(hglob16);
1303 else if (es->word_break_proc)
1307 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1309 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1310 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1311 ret = wbpW(es->text + start, index, count, action);
1315 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1319 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1320 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1321 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1322 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1323 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1324 ret = wbpA(textA, index, countA, action);
1325 HeapFree(GetProcessHeap(), 0, textA);
1329 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1331 WIN_RestoreWndsLock(iWndsLocks);
1336 /*********************************************************************
1340 * Beware: This is not the function called on EM_CHARFROMPOS
1341 * The position _can_ be outside the formatting / client
1343 * The return value is only the character index
1346 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1352 if (es->style & ES_MULTILINE) {
1353 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1355 LINEDEF *line_def = es->first_line_def;
1357 while ((line > 0) && line_def->next) {
1358 line_index += line_def->length;
1359 line_def = line_def->next;
1362 x += es->x_offset - es->format_rect.left;
1363 if (x >= line_def->width) {
1365 *after_wrap = (line_def->ending == END_WRAP);
1366 return line_index + line_def->net_length;
1370 *after_wrap = FALSE;
1373 dc = GetDC(es->hwndSelf);
1375 old_font = SelectObject(dc, es->font);
1376 low = line_index + 1;
1377 high = line_index + line_def->net_length + 1;
1378 while (low < high - 1)
1380 INT mid = (low + high) / 2;
1381 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1387 *after_wrap = ((index == line_index + line_def->net_length) &&
1388 (line_def->ending == END_WRAP));
1393 *after_wrap = FALSE;
1394 x -= es->format_rect.left;
1396 return es->x_offset;
1397 text = EDIT_GetPasswordPointer_SL(es);
1398 dc = GetDC(es->hwndSelf);
1400 old_font = SelectObject(dc, es->font);
1404 INT high = es->x_offset;
1405 while (low < high - 1)
1407 INT mid = (low + high) / 2;
1408 GetTextExtentPoint32W( dc, text + mid,
1409 es->x_offset - mid, &size );
1410 if (size.cx > -x) low = mid;
1417 INT low = es->x_offset;
1418 INT high = strlenW(es->text) + 1;
1419 while (low < high - 1)
1421 INT mid = (low + high) / 2;
1422 GetTextExtentPoint32W( dc, text + es->x_offset,
1423 mid - es->x_offset, &size );
1424 if (size.cx > x) high = mid;
1429 if (es->style & ES_PASSWORD)
1430 HeapFree(GetProcessHeap(), 0, text);
1433 SelectObject(dc, old_font);
1434 ReleaseDC(es->hwndSelf, dc);
1439 /*********************************************************************
1443 * adjusts the point to be within the formatting rectangle
1444 * (so CharFromPos returns the nearest _visible_ character)
1447 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1449 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1450 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1454 /*********************************************************************
1458 * Calculates the bounding rectangle for a line from a starting
1459 * column to an ending column.
1462 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1464 INT line_index = EDIT_EM_LineIndex(es, line);
1466 if (es->style & ES_MULTILINE)
1467 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1469 rc->top = es->format_rect.top;
1470 rc->bottom = rc->top + es->line_height;
1471 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1472 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1476 /*********************************************************************
1478 * EDIT_GetPasswordPointer_SL
1480 * note: caller should free the (optionally) allocated buffer
1483 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1485 if (es->style & ES_PASSWORD) {
1486 INT len = strlenW(es->text);
1487 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1489 while(len) text[--len] = es->password_char;
1496 /*********************************************************************
1500 * This acts as a LOCAL_Lock(), but it locks only once. This way
1501 * you can call it whenever you like, without unlocking.
1503 * Initially the edit control allocates a HLOCAL32 buffer
1504 * (32 bit linear memory handler). However, 16 bit application
1505 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1506 * handler). From that moment on we have to keep using this 16 bit memory
1507 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1508 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1512 static void EDIT_LockBuffer(EDITSTATE *es)
1514 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
1518 BOOL _16bit = FALSE;
1524 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1525 textA = LocalLock(es->hloc32A);
1526 countA = strlen(textA) + 1;
1530 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1531 textA = LOCAL_Lock(hInstance, es->hloc16);
1532 countA = strlen(textA) + 1;
1537 ERR("no buffer ... please report\n");
1544 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1545 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1546 if(countW_new > es->buffer_size + 1)
1548 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1549 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1550 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1553 es->hloc32W = hloc32W_new;
1554 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1555 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1558 WARN("FAILED! Will synchronize partially\n");
1562 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1563 es->text = LocalLock(es->hloc32W);
1567 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1569 LOCAL_Unlock(hInstance, es->hloc16);
1571 LocalUnlock(es->hloc32A);
1578 /*********************************************************************
1580 * EDIT_SL_InvalidateText
1582 * Called from EDIT_InvalidateText().
1583 * Does the job for single-line controls only.
1586 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1591 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1592 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1593 EDIT_UpdateText(es, &rc, TRUE);
1597 /*********************************************************************
1599 * EDIT_ML_InvalidateText
1601 * Called from EDIT_InvalidateText().
1602 * Does the job for multi-line controls only.
1605 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1607 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1608 INT sl = EDIT_EM_LineFromChar(es, start);
1609 INT el = EDIT_EM_LineFromChar(es, end);
1618 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1621 sc = start - EDIT_EM_LineIndex(es, sl);
1622 ec = end - EDIT_EM_LineIndex(es, el);
1623 if (sl < es->y_offset) {
1627 if (el > es->y_offset + vlc) {
1628 el = es->y_offset + vlc;
1629 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1631 GetClientRect(es->hwndSelf, &rc1);
1632 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1634 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1635 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1636 EDIT_UpdateText(es, &rcUpdate, TRUE);
1638 EDIT_GetLineRect(es, sl, sc,
1639 EDIT_EM_LineLength(es,
1640 EDIT_EM_LineIndex(es, sl)),
1642 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1643 EDIT_UpdateText(es, &rcUpdate, TRUE);
1644 for (l = sl + 1 ; l < el ; l++) {
1645 EDIT_GetLineRect(es, l, 0,
1646 EDIT_EM_LineLength(es,
1647 EDIT_EM_LineIndex(es, l)),
1649 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1650 EDIT_UpdateText(es, &rcUpdate, TRUE);
1652 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1653 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1654 EDIT_UpdateText(es, &rcUpdate, TRUE);
1659 /*********************************************************************
1661 * EDIT_InvalidateText
1663 * Invalidate the text from offset start upto, but not including,
1664 * offset end. Useful for (re)painting the selection.
1665 * Regions outside the linewidth are not invalidated.
1666 * end == -1 means end == TextLength.
1667 * start and end need not be ordered.
1670 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1676 end = strlenW(es->text);
1684 if (es->style & ES_MULTILINE)
1685 EDIT_ML_InvalidateText(es, start, end);
1687 EDIT_SL_InvalidateText(es, start, end);
1691 /*********************************************************************
1695 * Try to fit size + 1 characters in the buffer.
1696 * Constrain to limits if honor_limit is TRUE.
1698 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size, BOOL honor_limit)
1702 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
1703 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
1707 if (size <= es->buffer_size)
1710 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1712 /* Force edit to unlock it's buffer. es->text now NULL */
1713 EDIT_UnlockBuffer(es, TRUE);
1716 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1717 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1718 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1719 es->hloc32W = hNew32W;
1720 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1724 EDIT_LockBuffer(es);
1726 if (es->buffer_size < size) {
1727 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1728 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1731 TRACE("We now have %d+1\n", es->buffer_size);
1737 /*********************************************************************
1741 * Try to fit size + 1 bytes in the undo buffer.
1744 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1748 if (size <= es->undo_buffer_size)
1751 TRACE("trying to ReAlloc to %d+1\n", size);
1753 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1754 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1755 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1760 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1766 /*********************************************************************
1771 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1773 INT e = es->selection_end;
1777 if ((es->style & ES_MULTILINE) && e &&
1778 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1780 if (e && (es->text[e - 1] == '\r'))
1784 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1785 EDIT_EM_ScrollCaret(es);
1789 /*********************************************************************
1793 * Only for multi line controls
1794 * Move the caret one line down, on a column with the nearest
1795 * x coordinate on the screen (might be a different column).
1798 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1800 INT s = es->selection_start;
1801 INT e = es->selection_end;
1802 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1803 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1804 INT x = SLOWORD(pos);
1805 INT y = SHIWORD(pos);
1807 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1810 EDIT_EM_SetSel(es, s, e, after_wrap);
1811 EDIT_EM_ScrollCaret(es);
1815 /*********************************************************************
1820 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1822 BOOL after_wrap = FALSE;
1825 /* Pass a high value in x to make sure of receiving the end of the line */
1826 if (es->style & ES_MULTILINE)
1827 e = EDIT_CharFromPos(es, 0x3fffffff,
1828 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1830 e = strlenW(es->text);
1831 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1832 EDIT_EM_ScrollCaret(es);
1836 /*********************************************************************
1841 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1843 INT e = es->selection_end;
1847 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1848 if (es->text[e] == '\n')
1850 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1854 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1855 EDIT_EM_ScrollCaret(es);
1859 /*********************************************************************
1863 * Home key: move to beginning of line.
1866 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1870 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1871 if (es->style & ES_MULTILINE)
1872 e = EDIT_CharFromPos(es, -es->x_offset,
1873 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1876 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1877 EDIT_EM_ScrollCaret(es);
1881 /*********************************************************************
1883 * EDIT_MovePageDown_ML
1885 * Only for multi line controls
1886 * Move the caret one page down, on a column with the nearest
1887 * x coordinate on the screen (might be a different column).
1890 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1892 INT s = es->selection_start;
1893 INT e = es->selection_end;
1894 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1895 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1896 INT x = SLOWORD(pos);
1897 INT y = SHIWORD(pos);
1899 e = EDIT_CharFromPos(es, x,
1900 y + (es->format_rect.bottom - es->format_rect.top),
1904 EDIT_EM_SetSel(es, s, e, after_wrap);
1905 EDIT_EM_ScrollCaret(es);
1909 /*********************************************************************
1911 * EDIT_MovePageUp_ML
1913 * Only for multi line controls
1914 * Move the caret one page up, on a column with the nearest
1915 * x coordinate on the screen (might be a different column).
1918 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1920 INT s = es->selection_start;
1921 INT e = es->selection_end;
1922 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1923 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1924 INT x = SLOWORD(pos);
1925 INT y = SHIWORD(pos);
1927 e = EDIT_CharFromPos(es, x,
1928 y - (es->format_rect.bottom - es->format_rect.top),
1932 EDIT_EM_SetSel(es, s, e, after_wrap);
1933 EDIT_EM_ScrollCaret(es);
1937 /*********************************************************************
1941 * Only for multi line controls
1942 * Move the caret one line up, on a column with the nearest
1943 * x coordinate on the screen (might be a different column).
1946 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1948 INT s = es->selection_start;
1949 INT e = es->selection_end;
1950 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1951 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1952 INT x = SLOWORD(pos);
1953 INT y = SHIWORD(pos);
1955 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1958 EDIT_EM_SetSel(es, s, e, after_wrap);
1959 EDIT_EM_ScrollCaret(es);
1963 /*********************************************************************
1965 * EDIT_MoveWordBackward
1968 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1970 INT s = es->selection_start;
1971 INT e = es->selection_end;
1976 l = EDIT_EM_LineFromChar(es, e);
1977 ll = EDIT_EM_LineLength(es, e);
1978 li = EDIT_EM_LineIndex(es, l);
1981 li = EDIT_EM_LineIndex(es, l - 1);
1982 e = li + EDIT_EM_LineLength(es, li);
1985 e = li + (INT)EDIT_CallWordBreakProc(es,
1986 li, e - li, ll, WB_LEFT);
1990 EDIT_EM_SetSel(es, s, e, FALSE);
1991 EDIT_EM_ScrollCaret(es);
1995 /*********************************************************************
1997 * EDIT_MoveWordForward
2000 static void EDIT_MoveWordForward(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);
2012 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2013 e = EDIT_EM_LineIndex(es, l + 1);
2015 e = li + EDIT_CallWordBreakProc(es,
2016 li, e - li + 1, ll, WB_RIGHT);
2020 EDIT_EM_SetSel(es, s, e, FALSE);
2021 EDIT_EM_ScrollCaret(es);
2025 /*********************************************************************
2030 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2032 INT s = es->selection_start;
2033 INT e = es->selection_end;
2040 if (es->style & ES_MULTILINE) {
2041 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2042 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2047 TRACE("line=%d\n", line);
2049 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2052 li = EDIT_EM_LineIndex(es, line);
2053 ll = EDIT_EM_LineLength(es, li);
2054 s = min(es->selection_start, es->selection_end);
2055 e = max(es->selection_start, es->selection_end);
2056 s = min(li + ll, max(li, s));
2057 e = min(li + ll, max(li, e));
2058 if (rev && (s != e) &&
2059 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2060 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2061 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2062 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2064 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2068 /*********************************************************************
2073 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2084 BkMode = GetBkMode(dc);
2085 BkColor = GetBkColor(dc);
2086 TextColor = GetTextColor(dc);
2088 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2089 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2090 SetBkMode( dc, OPAQUE);
2092 li = EDIT_EM_LineIndex(es, line);
2093 if (es->style & ES_MULTILINE) {
2094 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2095 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2097 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2098 TextOutW(dc, x, y, text + li + col, count);
2099 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2101 if (es->style & ES_PASSWORD)
2102 HeapFree(GetProcessHeap(), 0, text);
2105 SetBkColor(dc, BkColor);
2106 SetTextColor(dc, TextColor);
2107 SetBkMode( dc, BkMode);
2113 /*********************************************************************
2118 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2121 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2122 SetCaretPos(SLOWORD(res), SHIWORD(res));
2126 /*********************************************************************
2130 * note: this is not (exactly) the handler called on EM_SETRECTNP
2131 * it is also used to set the rect of a single line control
2134 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2136 CopyRect(&es->format_rect, rc);
2137 if (es->style & WS_BORDER) {
2138 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2139 if(TWEAK_WineLook == WIN31_LOOK)
2141 es->format_rect.left += bw;
2142 es->format_rect.top += bw;
2143 es->format_rect.right -= bw;
2144 es->format_rect.bottom -= bw;
2146 es->format_rect.left += es->left_margin;
2147 es->format_rect.right -= es->right_margin;
2148 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2149 if (es->style & ES_MULTILINE)
2151 INT fw, vlc, max_x_offset, max_y_offset;
2153 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2154 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2156 /* correct es->x_offset */
2157 fw = es->format_rect.right - es->format_rect.left;
2158 max_x_offset = es->text_width - fw;
2159 if(max_x_offset < 0) max_x_offset = 0;
2160 if(es->x_offset > max_x_offset)
2161 es->x_offset = max_x_offset;
2163 /* correct es->y_offset */
2164 max_y_offset = es->line_count - vlc;
2165 if(max_y_offset < 0) max_y_offset = 0;
2166 if(es->y_offset > max_y_offset)
2167 es->y_offset = max_y_offset;
2169 /* force scroll info update */
2170 EDIT_UpdateScrollInfo(es);
2173 /* Windows doesn't care to fix text placement for SL controls */
2174 es->format_rect.bottom = es->format_rect.top + es->line_height;
2176 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2177 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2181 /*********************************************************************
2186 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2188 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2190 /* Edit window might be already destroyed */
2191 if(!IsWindow(es->hwndSelf))
2193 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2197 if (!es->lock_count) {
2198 ERR("lock_count == 0 ... please report\n");
2202 ERR("es->text == 0 ... please report\n");
2206 if (force || (es->lock_count == 1)) {
2209 BOOL _16bit = FALSE;
2211 UINT countW = strlenW(es->text) + 1;
2215 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2216 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2217 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2218 countA = LocalSize(es->hloc32A);
2219 if(countA_new > countA)
2222 UINT alloc_size = ROUND_TO_GROW(countA_new);
2223 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2224 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2227 es->hloc32A = hloc32A_new;
2228 countA = LocalSize(hloc32A_new);
2229 TRACE("Real new size %d bytes\n", countA);
2232 WARN("FAILED! Will synchronize partially\n");
2234 textA = LocalLock(es->hloc32A);
2238 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2239 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2240 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2241 countA = LOCAL_Size(hInstance, es->hloc16);
2242 if(countA_new > countA)
2244 HLOCAL16 hloc16_new;
2245 UINT alloc_size = ROUND_TO_GROW(countA_new);
2246 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2247 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2250 es->hloc16 = hloc16_new;
2251 countA = LOCAL_Size(hInstance, hloc16_new);
2252 TRACE("Real new size %d bytes\n", countA);
2255 WARN("FAILED! Will synchronize partially\n");
2257 textA = LOCAL_Lock(hInstance, es->hloc16);
2263 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2265 LOCAL_Unlock(hInstance, es->hloc16);
2267 LocalUnlock(es->hloc32A);
2270 LocalUnlock(es->hloc32W);
2274 ERR("no buffer ... please report\n");
2282 /*********************************************************************
2284 * EDIT_UpdateScrollInfo
2287 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2289 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2292 si.cbSize = sizeof(SCROLLINFO);
2293 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2295 si.nMax = es->line_count - 1;
2296 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2297 si.nPos = es->y_offset;
2298 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2299 si.nMin, si.nMax, si.nPage, si.nPos);
2300 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2303 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2306 si.cbSize = sizeof(SCROLLINFO);
2307 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2309 si.nMax = es->text_width - 1;
2310 si.nPage = es->format_rect.right - es->format_rect.left;
2311 si.nPos = es->x_offset;
2312 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2313 si.nMin, si.nMax, si.nPage, si.nPos);
2314 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2318 /*********************************************************************
2320 * EDIT_WordBreakProc
2322 * Find the beginning of words.
2323 * Note: unlike the specs for a WordBreakProc, this function only
2324 * allows to be called without linebreaks between s[0] upto
2325 * s[count - 1]. Remember it is only called
2326 * internally, so we can decide this for ourselves.
2329 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2333 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2343 if (s[index] == ' ') {
2344 while (index && (s[index] == ' '))
2347 while (index && (s[index] != ' '))
2349 if (s[index] == ' ')
2353 while (index && (s[index] != ' '))
2355 if (s[index] == ' ')
2365 if (s[index] == ' ')
2366 while ((index < count) && (s[index] == ' ')) index++;
2368 while (s[index] && (s[index] != ' ') && (index < count))
2370 while ((s[index] == ' ') && (index < count)) index++;
2374 case WB_ISDELIMITER:
2375 ret = (s[index] == ' ');
2378 ERR("unknown action code, please report !\n");
2385 /*********************************************************************
2389 * returns line number (not index) in high-order word of result.
2390 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2391 * to Richedit, not to the edit control. Original documentation is valid.
2392 * FIXME: do the specs mean to return -1 if outside client area or
2393 * if outside formatting rectangle ???
2396 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2404 GetClientRect(es->hwndSelf, &rc);
2405 if (!PtInRect(&rc, pt))
2408 index = EDIT_CharFromPos(es, x, y, NULL);
2409 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2413 /*********************************************************************
2417 * Enable or disable soft breaks.
2419 * This means: insert or remove the soft linebreak character (\r\r\n).
2420 * Take care to check if the text still fits the buffer after insertion.
2421 * If not, notify with EN_ERRSPACE.
2424 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2426 es->flags &= ~EF_USE_SOFTBRK;
2428 es->flags |= EF_USE_SOFTBRK;
2429 FIXME("soft break enabled, not implemented\n");
2435 /*********************************************************************
2439 * Hopefully this won't fire back at us.
2440 * We always start with a fixed buffer in the local heap.
2441 * Despite of the documentation says that the local heap is used
2442 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2443 * buffer on the local heap.
2446 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2450 if (!(es->style & ES_MULTILINE))
2454 hLocal = es->hloc32W;
2460 UINT countA, alloc_size;
2461 TRACE("Allocating 32-bit ANSI alias buffer\n");
2462 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2463 alloc_size = ROUND_TO_GROW(countA);
2464 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2466 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2469 textA = LocalLock(es->hloc32A);
2470 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2471 LocalUnlock(es->hloc32A);
2473 hLocal = es->hloc32A;
2476 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2481 /*********************************************************************
2485 * Hopefully this won't fire back at us.
2486 * We always start with a buffer in 32 bit linear memory.
2487 * However, with this message a 16 bit application requests
2488 * a handle of 16 bit local heap memory, where it expects to find
2490 * It's a pitty that from this moment on we have to use this
2491 * local heap, because applications may rely on the handle
2494 * In this function we'll try to switch to local heap.
2496 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2498 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2500 UINT countA, alloc_size;
2502 if (!(es->style & ES_MULTILINE))
2508 if (!LOCAL_HeapSize(hInstance)) {
2509 if (!LocalInit16(hInstance, 0,
2510 GlobalSize16(hInstance))) {
2511 ERR("could not initialize local heap\n");
2514 TRACE("local heap initialized\n");
2517 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2518 alloc_size = ROUND_TO_GROW(countA);
2520 TRACE("Allocating 16-bit ANSI alias buffer\n");
2521 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2522 ERR("could not allocate new 16 bit buffer\n");
2526 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2527 ERR("could not lock new 16 bit buffer\n");
2528 LOCAL_Free(hInstance, es->hloc16);
2533 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2534 LOCAL_Unlock(hInstance, es->hloc16);
2536 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2541 /*********************************************************************
2546 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2549 INT line_len, dst_len;
2552 if (es->style & ES_MULTILINE) {
2553 if (line >= es->line_count)
2557 i = EDIT_EM_LineIndex(es, line);
2559 line_len = EDIT_EM_LineLength(es, i);
2560 dst_len = *(WORD *)lParam;
2563 LPWSTR dst = (LPWSTR)lParam;
2564 if(dst_len <= line_len)
2566 memcpy(dst, src, dst_len * sizeof(WCHAR));
2569 else /* Append 0 if enough space */
2571 memcpy(dst, src, line_len * sizeof(WCHAR));
2578 LPSTR dst = (LPSTR)lParam;
2580 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2581 if(!ret) /* Insufficient buffer size */
2583 if(ret < dst_len) /* Append 0 if enough space */
2590 /*********************************************************************
2595 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2597 UINT s = es->selection_start;
2598 UINT e = es->selection_end;
2605 return MAKELONG(s, e);
2609 /*********************************************************************
2613 * FIXME: is this right ? (or should it be only VSCROLL)
2614 * (and maybe only for edit controls that really have their
2615 * own scrollbars) (and maybe only for multiline controls ?)
2616 * All in all: very poorly documented
2619 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2621 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2622 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2626 /*********************************************************************
2631 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2636 if (!(es->style & ES_MULTILINE))
2638 if (index > (INT)strlenW(es->text))
2639 return es->line_count - 1;
2641 index = min(es->selection_start, es->selection_end);
2644 line_def = es->first_line_def;
2645 index -= line_def->length;
2646 while ((index >= 0) && line_def->next) {
2648 line_def = line_def->next;
2649 index -= line_def->length;
2655 /*********************************************************************
2660 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2665 if (!(es->style & ES_MULTILINE))
2667 if (line >= es->line_count)
2671 line_def = es->first_line_def;
2673 INT index = es->selection_end - line_def->length;
2674 while ((index >= 0) && line_def->next) {
2675 line_index += line_def->length;
2676 line_def = line_def->next;
2677 index -= line_def->length;
2681 line_index += line_def->length;
2682 line_def = line_def->next;
2690 /*********************************************************************
2695 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2699 if (!(es->style & ES_MULTILINE))
2700 return strlenW(es->text);
2703 /* get the number of remaining non-selected chars of selected lines */
2704 INT32 l; /* line number */
2705 INT32 li; /* index of first char in line */
2707 l = EDIT_EM_LineFromChar(es, es->selection_start);
2708 /* # chars before start of selection area */
2709 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2710 l = EDIT_EM_LineFromChar(es, es->selection_end);
2711 /* # chars after end of selection */
2712 li = EDIT_EM_LineIndex(es, l);
2713 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2716 line_def = es->first_line_def;
2717 index -= line_def->length;
2718 while ((index >= 0) && line_def->next) {
2719 line_def = line_def->next;
2720 index -= line_def->length;
2722 return line_def->net_length;
2726 /*********************************************************************
2730 * NOTE: dx is in average character widths, dy - in lines;
2733 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2735 if (!(es->style & ES_MULTILINE))
2738 dx *= es->char_width;
2739 return EDIT_EM_LineScroll_internal(es, dx, dy);
2742 /*********************************************************************
2744 * EDIT_EM_LineScroll_internal
2746 * Version of EDIT_EM_LineScroll for internal use.
2747 * It doesn't refuse if ES_MULTILINE is set and assumes that
2748 * dx is in pixels, dy - in lines.
2751 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2754 INT x_offset_in_pixels;
2756 if (es->style & ES_MULTILINE)
2758 x_offset_in_pixels = es->x_offset;
2763 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2766 if (-dx > x_offset_in_pixels)
2767 dx = -x_offset_in_pixels;
2768 if (dx > es->text_width - x_offset_in_pixels)
2769 dx = es->text_width - x_offset_in_pixels;
2770 nyoff = max(0, es->y_offset + dy);
2771 if (nyoff >= es->line_count)
2772 nyoff = es->line_count - 1;
2773 dy = (es->y_offset - nyoff) * es->line_height;
2778 es->y_offset = nyoff;
2779 if(es->style & ES_MULTILINE)
2782 es->x_offset += dx / es->char_width;
2784 GetClientRect(es->hwndSelf, &rc1);
2785 IntersectRect(&rc, &rc1, &es->format_rect);
2786 ScrollWindowEx(es->hwndSelf, -dx, dy,
2787 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2788 /* force scroll info update */
2789 EDIT_UpdateScrollInfo(es);
2791 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2792 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2793 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2794 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2799 /*********************************************************************
2804 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2806 INT len = strlenW(es->text);
2815 index = min(index, len);
2816 dc = GetDC(es->hwndSelf);
2818 old_font = SelectObject(dc, es->font);
2819 if (es->style & ES_MULTILINE) {
2820 l = EDIT_EM_LineFromChar(es, index);
2821 y = (l - es->y_offset) * es->line_height;
2822 li = EDIT_EM_LineIndex(es, l);
2823 if (after_wrap && (li == index) && l) {
2825 LINEDEF *line_def = es->first_line_def;
2827 line_def = line_def->next;
2830 if (line_def->ending == END_WRAP) {
2832 y -= es->line_height;
2833 li = EDIT_EM_LineIndex(es, l);
2836 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2837 es->tabs_count, es->tabs)) - es->x_offset;
2839 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2840 if (index < es->x_offset) {
2841 GetTextExtentPoint32W(dc, text + index,
2842 es->x_offset - index, &size);
2845 GetTextExtentPoint32W(dc, text + es->x_offset,
2846 index - es->x_offset, &size);
2850 if (es->style & ES_PASSWORD)
2851 HeapFree(GetProcessHeap(), 0, text);
2853 x += es->format_rect.left;
2854 y += es->format_rect.top;
2856 SelectObject(dc, old_font);
2857 ReleaseDC(es->hwndSelf, dc);
2858 return MAKELONG((INT16)x, (INT16)y);
2862 /*********************************************************************
2866 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2869 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2871 UINT strl = strlenW(lpsz_replace);
2872 UINT tl = strlenW(es->text);
2880 TRACE("%s, can_undo %d, send_update %d\n",
2881 debugstr_w(lpsz_replace), can_undo, send_update);
2883 s = es->selection_start;
2884 e = es->selection_end;
2886 if ((s == e) && !strl)
2891 if (!EDIT_MakeFit(es, tl - (e - s) + strl, honor_limit))
2895 /* there is something to be deleted */
2896 TRACE("deleting stuff.\n");
2898 utl = strlenW(es->undo_text);
2899 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2900 /* undo-buffer is extended to the right */
2901 EDIT_MakeUndoFit(es, utl + e - s);
2902 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2903 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2904 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2905 /* undo-buffer is extended to the left */
2906 EDIT_MakeUndoFit(es, utl + e - s);
2907 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2909 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2910 p[i] = (es->text + s)[i];
2911 es->undo_position = s;
2913 /* new undo-buffer */
2914 EDIT_MakeUndoFit(es, e - s);
2915 strncpyW(es->undo_text, es->text + s, e - s + 1);
2916 es->undo_text[e - s] = 0; /* ensure 0 termination */
2917 es->undo_position = s;
2919 /* any deletion makes the old insertion-undo invalid */
2920 es->undo_insert_count = 0;
2922 EDIT_EM_EmptyUndoBuffer(es);
2925 strcpyW(es->text + s, es->text + e);
2928 /* there is an insertion */
2930 if ((s == es->undo_position) ||
2931 ((es->undo_insert_count) &&
2932 (s == es->undo_position + es->undo_insert_count)))
2934 * insertion is new and at delete position or
2935 * an extension to either left or right
2937 es->undo_insert_count += strl;
2939 /* new insertion undo */
2940 es->undo_position = s;
2941 es->undo_insert_count = strl;
2942 /* new insertion makes old delete-buffer invalid */
2943 *es->undo_text = '\0';
2946 EDIT_EM_EmptyUndoBuffer(es);
2949 tl = strlenW(es->text);
2950 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));
2951 for (p = es->text + tl ; p >= es->text + s ; p--)
2953 for (i = 0 , p = es->text + s ; i < strl ; i++)
2954 p[i] = lpsz_replace[i];
2955 if(es->style & ES_UPPERCASE)
2956 CharUpperBuffW(p, strl);
2957 else if(es->style & ES_LOWERCASE)
2958 CharLowerBuffW(p, strl);
2961 if (es->style & ES_MULTILINE)
2963 INT s = min(es->selection_start, es->selection_end);
2965 hrgn = CreateRectRgn(0, 0, 0, 0);
2966 EDIT_BuildLineDefs_ML(es, s, s + strl,
2967 strl - abs(es->selection_end - es->selection_start), hrgn);
2970 EDIT_CalcLineWidth_SL(es);
2972 EDIT_EM_SetSel(es, s, s, FALSE);
2973 es->flags |= EF_MODIFIED;
2974 if (send_update) es->flags |= EF_UPDATE;
2975 EDIT_EM_ScrollCaret(es);
2977 /* force scroll info update */
2978 EDIT_UpdateScrollInfo(es);
2982 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2986 EDIT_UpdateText(es, NULL, TRUE);
2988 if(es->flags & EF_UPDATE)
2990 es->flags &= ~EF_UPDATE;
2991 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
2996 /*********************************************************************
3001 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3005 if (!(es->style & ES_MULTILINE))
3006 return (LRESULT)FALSE;
3016 if (es->y_offset < es->line_count - 1)
3021 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3024 if (es->y_offset < es->line_count - 1)
3025 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3028 return (LRESULT)FALSE;
3031 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3032 /* check if we are going to move too far */
3033 if(es->y_offset + dy > es->line_count - vlc)
3034 dy = es->line_count - vlc - es->y_offset;
3036 /* Notification is done in EDIT_EM_LineScroll */
3038 EDIT_EM_LineScroll(es, 0, dy);
3040 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3044 /*********************************************************************
3049 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3051 if (es->style & ES_MULTILINE) {
3056 INT cw = es->char_width;
3061 l = EDIT_EM_LineFromChar(es, es->selection_end);
3062 li = EDIT_EM_LineIndex(es, l);
3063 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3064 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3065 if (l >= es->y_offset + vlc)
3066 dy = l - vlc + 1 - es->y_offset;
3067 if (l < es->y_offset)
3068 dy = l - es->y_offset;
3069 ww = es->format_rect.right - es->format_rect.left;
3070 if (x < es->format_rect.left)
3071 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3072 if (x > es->format_rect.right)
3073 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3076 /* check if we are going to move too far */
3077 if(es->x_offset + dx + ww > es->text_width)
3078 dx = es->text_width - ww - es->x_offset;
3080 EDIT_EM_LineScroll_internal(es, dx, dy);
3087 if (!(es->style & ES_AUTOHSCROLL))
3090 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3091 format_width = es->format_rect.right - es->format_rect.left;
3092 if (x < es->format_rect.left) {
3093 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3096 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3097 } while ((x < goal) && es->x_offset);
3098 /* FIXME: use ScrollWindow() somehow to improve performance */
3099 EDIT_UpdateText(es, NULL, TRUE);
3100 } else if (x > es->format_rect.right) {
3102 INT len = strlenW(es->text);
3103 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3106 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3107 x_last = SLOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3108 } while ((x > goal) && (x_last > es->format_rect.right));
3109 /* FIXME: use ScrollWindow() somehow to improve performance */
3110 EDIT_UpdateText(es, NULL, TRUE);
3114 if(es->flags & EF_FOCUSED)
3115 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3119 /*********************************************************************
3123 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3126 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3128 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3130 if (!(es->style & ES_MULTILINE))
3134 WARN("called with NULL handle\n");
3138 EDIT_UnlockBuffer(es, TRUE);
3142 LOCAL_Free(hInstance, es->hloc16);
3143 es->hloc16 = (HLOCAL16)NULL;
3150 LocalFree(es->hloc32A);
3162 countA = LocalSize(hloc);
3163 textA = LocalLock(hloc);
3164 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3165 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3167 ERR("Could not allocate new unicode buffer\n");
3170 textW = LocalLock(hloc32W_new);
3171 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3172 LocalUnlock(hloc32W_new);
3176 LocalFree(es->hloc32W);
3178 es->hloc32W = hloc32W_new;
3182 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3184 EDIT_LockBuffer(es);
3186 es->x_offset = es->y_offset = 0;
3187 es->selection_start = es->selection_end = 0;
3188 EDIT_EM_EmptyUndoBuffer(es);
3189 es->flags &= ~EF_MODIFIED;
3190 es->flags &= ~EF_UPDATE;
3191 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3192 EDIT_UpdateText(es, NULL, TRUE);
3193 EDIT_EM_ScrollCaret(es);
3194 /* force scroll info update */
3195 EDIT_UpdateScrollInfo(es);
3199 /*********************************************************************
3203 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3206 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3208 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3214 if (!(es->style & ES_MULTILINE))
3218 WARN("called with NULL handle\n");
3222 EDIT_UnlockBuffer(es, TRUE);
3226 LocalFree(es->hloc32A);
3230 countA = LOCAL_Size(hInstance, hloc);
3231 textA = LOCAL_Lock(hInstance, hloc);
3232 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3233 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3235 ERR("Could not allocate new unicode buffer\n");
3238 textW = LocalLock(hloc32W_new);
3239 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3240 LocalUnlock(hloc32W_new);
3241 LOCAL_Unlock(hInstance, hloc);
3244 LocalFree(es->hloc32W);
3246 es->hloc32W = hloc32W_new;
3249 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3251 EDIT_LockBuffer(es);
3253 es->x_offset = es->y_offset = 0;
3254 es->selection_start = es->selection_end = 0;
3255 EDIT_EM_EmptyUndoBuffer(es);
3256 es->flags &= ~EF_MODIFIED;
3257 es->flags &= ~EF_UPDATE;
3258 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3259 EDIT_UpdateText(es, NULL, TRUE);
3260 EDIT_EM_ScrollCaret(es);
3261 /* force scroll info update */
3262 EDIT_UpdateScrollInfo(es);
3266 /*********************************************************************
3270 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3271 * However, the windows version is not complied to yet in all of edit.c
3273 * Additionally as the wrapper for RichEdit controls we need larger buffers
3274 * at present -1 will represent nolimit
3276 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3278 if (limit == 0xFFFFFFFF)
3279 es->buffer_limit = -1;
3280 else if (es->style & ES_MULTILINE) {
3282 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3284 es->buffer_limit = BUFLIMIT_MULTI;
3287 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3289 es->buffer_limit = BUFLIMIT_SINGLE;
3294 /*********************************************************************
3298 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3299 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3300 * margin according to the textmetrics of the current font.
3302 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3303 * of the char's width as the margin, but this is not how Windows handles this.
3304 * For all other fonts Windows sets the margins to zero.
3307 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3308 INT left, INT right)
3311 INT default_left_margin = 0; /* in pixels */
3312 INT default_right_margin = 0; /* in pixels */
3314 /* Set the default margins depending on the font */
3315 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3316 HDC dc = GetDC(es->hwndSelf);
3317 HFONT old_font = SelectObject(dc, es->font);
3318 GetTextMetricsW(dc, &tm);
3319 /* The default margins are only non zero for TrueType or Vector fonts */
3320 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3321 /* This must be calculated more exactly! But how? */
3322 default_left_margin = tm.tmAveCharWidth / 3;
3323 default_right_margin = tm.tmAveCharWidth / 3;
3325 SelectObject(dc, old_font);
3326 ReleaseDC(es->hwndSelf, dc);
3329 if (action & EC_LEFTMARGIN) {
3330 if (left != EC_USEFONTINFO)
3331 es->left_margin = left;
3333 es->left_margin = default_left_margin;
3336 if (action & EC_RIGHTMARGIN) {
3337 if (right != EC_USEFONTINFO)
3338 es->right_margin = right;
3340 es->right_margin = default_right_margin;
3342 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3346 /*********************************************************************
3348 * EM_SETPASSWORDCHAR
3351 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3355 if (es->style & ES_MULTILINE)
3358 if (es->password_char == c)
3361 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3362 es->password_char = c;
3364 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3365 es->style |= ES_PASSWORD;
3367 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3368 es->style &= ~ES_PASSWORD;
3370 EDIT_UpdateText(es, NULL, TRUE);
3374 /*********************************************************************
3378 * note: unlike the specs say: the order of start and end
3379 * _is_ preserved in Windows. (i.e. start can be > end)
3380 * In other words: this handler is OK
3383 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3385 UINT old_start = es->selection_start;
3386 UINT old_end = es->selection_end;
3387 UINT len = strlenW(es->text);
3389 if (start == (UINT)-1) {
3390 start = es->selection_end;
3391 end = es->selection_end;
3393 start = min(start, len);
3394 end = min(end, len);
3396 es->selection_start = start;
3397 es->selection_end = end;
3399 es->flags |= EF_AFTER_WRAP;
3401 es->flags &= ~EF_AFTER_WRAP;
3402 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3403 ORDER_UINT(start, end);
3404 ORDER_UINT(end, old_end);
3405 ORDER_UINT(start, old_start);
3406 ORDER_UINT(old_start, old_end);
3407 if (end != old_start)
3411 * ORDER_UINT32(end, old_start);
3412 * EDIT_InvalidateText(es, start, end);
3413 * EDIT_InvalidateText(es, old_start, old_end);
3414 * in place of the following if statement.
3416 if (old_start > end )
3418 EDIT_InvalidateText(es, start, end);
3419 EDIT_InvalidateText(es, old_start, old_end);
3423 EDIT_InvalidateText(es, start, old_start);
3424 EDIT_InvalidateText(es, end, old_end);
3427 else EDIT_InvalidateText(es, start, old_end);
3431 /*********************************************************************
3436 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3438 if (!(es->style & ES_MULTILINE))
3441 HeapFree(GetProcessHeap(), 0, es->tabs);
3442 es->tabs_count = count;
3446 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3447 memcpy(es->tabs, tabs, count * sizeof(INT));
3453 /*********************************************************************
3458 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3460 if (!(es->style & ES_MULTILINE))
3463 HeapFree(GetProcessHeap(), 0, es->tabs);
3464 es->tabs_count = count;
3469 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3470 for (i = 0 ; i < count ; i++)
3471 es->tabs[i] = *tabs++;
3477 /*********************************************************************
3479 * EM_SETWORDBREAKPROC
3482 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam)
3484 if (es->word_break_proc == (void *)lParam)
3487 es->word_break_proc = (void *)lParam;
3488 es->word_break_proc16 = NULL;
3490 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3491 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3492 EDIT_UpdateText(es, NULL, TRUE);
3497 /*********************************************************************
3499 * EM_SETWORDBREAKPROC16
3502 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3504 if (es->word_break_proc16 == wbp)
3507 es->word_break_proc = NULL;
3508 es->word_break_proc16 = wbp;
3509 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3510 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3511 EDIT_UpdateText(es, NULL, TRUE);
3516 /*********************************************************************
3521 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3526 /* Protect read-only edit control from modification */
3527 if(es->style & ES_READONLY)
3530 ulength = strlenW(es->undo_text);
3531 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3533 strcpyW(utext, es->undo_text);
3535 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3536 es->undo_insert_count, debugstr_w(utext));
3538 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3539 EDIT_EM_EmptyUndoBuffer(es);
3540 EDIT_EM_ReplaceSel(es, TRUE, utext, FALSE, TRUE);
3541 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3542 /* send the notification after the selection start and end are set */
3543 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3544 EDIT_EM_ScrollCaret(es);
3545 HeapFree(GetProcessHeap(), 0, utext);
3547 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3548 es->undo_insert_count, debugstr_w(es->undo_text));
3553 /*********************************************************************
3558 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3562 /* Protect read-only edit control from modification */
3563 if(es->style & ES_READONLY)
3566 control = GetKeyState(VK_CONTROL) & 0x8000;
3570 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3571 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3574 if (es->style & ES_MULTILINE) {
3575 if (es->style & ES_READONLY) {
3576 EDIT_MoveHome(es, FALSE);
3577 EDIT_MoveDown_ML(es, FALSE);
3579 static const WCHAR cr_lfW[] = {'\r','\n',0};
3580 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3585 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3587 static const WCHAR tabW[] = {'\t',0};
3588 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3592 if (!(es->style & ES_READONLY) && !control) {
3593 if (es->selection_start != es->selection_end)
3596 /* delete character left of caret */
3597 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3598 EDIT_MoveBackward(es, TRUE);
3604 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3607 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3610 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3614 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3618 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3625 /*********************************************************************
3630 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3632 if (code || control)
3652 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3653 EDIT_EM_ScrollCaret(es);
3656 ERR("unknown menu item, please report\n");
3662 /*********************************************************************
3666 * Note: the resource files resource/sysres_??.rc cannot define a
3667 * single popup menu. Hence we use a (dummy) menubar
3668 * containing the single popup menu as its first item.
3670 * FIXME: the message identifiers have been chosen arbitrarily,
3671 * hence we use MF_BYPOSITION.
3672 * We might as well use the "real" values (anybody knows ?)
3673 * The menu definition is in resources/sysres_??.rc.
3674 * Once these are OK, we better use MF_BYCOMMAND here
3675 * (as we do in EDIT_WM_Command()).
3678 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3680 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3681 HMENU popup = GetSubMenu(menu, 0);
3682 UINT start = es->selection_start;
3683 UINT end = es->selection_end;
3685 ORDER_UINT(start, end);
3688 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3690 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3692 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3694 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3696 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3698 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3700 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3705 /*********************************************************************
3710 static void EDIT_WM_Copy(EDITSTATE *es)
3712 INT s = min(es->selection_start, es->selection_end);
3713 INT e = max(es->selection_start, es->selection_end);
3719 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3720 dst = GlobalLock(hdst);
3721 strncpyW(dst, es->text + s, e - s);
3722 dst[e - s] = 0; /* ensure 0 termination */
3723 TRACE("%s\n", debugstr_w(dst));
3725 OpenClipboard(es->hwndSelf);
3727 SetClipboardData(CF_UNICODETEXT, hdst);
3732 /*********************************************************************
3737 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3739 TRACE("%s\n", debugstr_w(name));
3741 * To initialize some final structure members, we call some helper
3742 * functions. However, since the EDITSTATE is not consistent (i.e.
3743 * not fully initialized), we should be very careful which
3744 * functions can be called, and in what order.
3746 EDIT_WM_SetFont(es, 0, FALSE);
3747 EDIT_EM_EmptyUndoBuffer(es);
3749 if (name && *name) {
3750 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3751 /* if we insert text to the editline, the text scrolls out
3752 * of the window, as the caret is placed after the insert
3753 * pos normally; thus we reset es->selection... to 0 and
3756 es->selection_start = es->selection_end = 0;
3757 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3758 * Messages are only to be sent when the USER does something to
3759 * change the contents. So I am removing this EN_CHANGE
3761 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3763 EDIT_EM_ScrollCaret(es);
3765 /* force scroll info update */
3766 EDIT_UpdateScrollInfo(es);
3771 /*********************************************************************
3776 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3781 while (LocalUnlock(es->hloc32W)) ;
3782 LocalFree(es->hloc32W);
3785 while (LocalUnlock(es->hloc32A)) ;
3786 LocalFree(es->hloc32A);
3789 HINSTANCE16 hInstance = GetWindowWord( es->hwndSelf, GWL_HINSTANCE );
3790 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3791 LOCAL_Free(hInstance, es->hloc16);
3794 pc = es->first_line_def;
3798 HeapFree(GetProcessHeap(), 0, pc);
3802 SetWindowLongW( es->hwndSelf, 0, 0 );
3803 HeapFree(GetProcessHeap(), 0, es);
3809 /*********************************************************************
3814 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3819 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
3820 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3822 GetClientRect(es->hwndSelf, &rc);
3823 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3824 GetClipBox(dc, &rc);
3826 * FIXME: specs say that we should UnrealizeObject() the brush,
3827 * but the specs of UnrealizeObject() say that we shouldn't
3828 * unrealize a stock object. The default brush that
3829 * DefWndProc() returns is ... a stock object.
3831 FillRect(dc, &rc, brush);
3836 /*********************************************************************
3841 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3843 if(!count) return 0;
3847 LPWSTR textW = (LPWSTR)lParam;
3848 lstrcpynW(textW, es->text, count);
3849 return strlenW(textW);
3853 LPSTR textA = (LPSTR)lParam;
3854 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3855 textA[count - 1] = 0; /* ensure 0 termination */
3856 return strlen(textA);
3860 /*********************************************************************
3865 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3870 if (!(es->style & ES_MULTILINE))
3873 if (!(es->style & ES_AUTOHSCROLL))
3877 fw = es->format_rect.right - es->format_rect.left;
3880 TRACE("SB_LINELEFT\n");
3882 dx = -es->char_width;
3885 TRACE("SB_LINERIGHT\n");
3886 if (es->x_offset < es->text_width)
3887 dx = es->char_width;
3890 TRACE("SB_PAGELEFT\n");
3892 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3895 TRACE("SB_PAGERIGHT\n");
3896 if (es->x_offset < es->text_width)
3897 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3905 TRACE("SB_RIGHT\n");
3906 if (es->x_offset < es->text_width)
3907 dx = es->text_width - es->x_offset;
3910 TRACE("SB_THUMBTRACK %d\n", pos);
3911 es->flags |= EF_HSCROLL_TRACK;
3912 if(es->style & WS_HSCROLL)
3913 dx = pos - es->x_offset;
3918 if(pos < 0 || pos > 100) return 0;
3919 /* Assume default scroll range 0-100 */
3920 fw = es->format_rect.right - es->format_rect.left;
3921 new_x = pos * (es->text_width - fw) / 100;
3922 dx = es->text_width ? (new_x - es->x_offset) : 0;
3925 case SB_THUMBPOSITION:
3926 TRACE("SB_THUMBPOSITION %d\n", pos);
3927 es->flags &= ~EF_HSCROLL_TRACK;
3928 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3929 dx = pos - es->x_offset;
3934 if(pos < 0 || pos > 100) return 0;
3935 /* Assume default scroll range 0-100 */
3936 fw = es->format_rect.right - es->format_rect.left;
3937 new_x = pos * (es->text_width - fw) / 100;
3938 dx = es->text_width ? (new_x - es->x_offset) : 0;
3941 /* force scroll info update */
3942 EDIT_UpdateScrollInfo(es);
3943 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
3947 TRACE("SB_ENDSCROLL\n");
3950 * FIXME : the next two are undocumented !
3951 * Are we doing the right thing ?
3952 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3953 * although it's also a regular control message.
3955 case EM_GETTHUMB: /* this one is used by NT notepad */
3959 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3960 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3963 /* Assume default scroll range 0-100 */
3964 INT fw = es->format_rect.right - es->format_rect.left;
3965 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
3967 TRACE("EM_GETTHUMB: returning %ld\n", ret);
3970 case EM_LINESCROLL16:
3971 TRACE("EM_LINESCROLL16\n");
3976 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
3982 INT fw = es->format_rect.right - es->format_rect.left;
3983 /* check if we are going to move too far */
3984 if(es->x_offset + dx + fw > es->text_width)
3985 dx = es->text_width - fw - es->x_offset;
3987 EDIT_EM_LineScroll_internal(es, dx, 0);
3993 /*********************************************************************
3998 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4000 HWND hLBox = es->hwndListBox;
4008 hCombo = GetParent(es->hwndSelf);
4012 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4014 if (key == VK_UP || key == VK_DOWN)
4016 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4019 if (msg == WM_KEYDOWN || nEUI)
4020 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4026 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4028 /* make sure ComboLBox pops up */
4029 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4034 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4037 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4039 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4041 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4046 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4052 /*********************************************************************
4056 * Handling of special keys that don't produce a WM_CHAR
4057 * (i.e. non-printable keys) & Backspace & Delete
4060 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4065 if (GetKeyState(VK_MENU) & 0x8000)
4068 shift = GetKeyState(VK_SHIFT) & 0x8000;
4069 control = GetKeyState(VK_CONTROL) & 0x8000;
4074 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4079 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4080 EDIT_MoveUp_ML(es, shift);
4083 EDIT_MoveWordBackward(es, shift);
4085 EDIT_MoveBackward(es, shift);
4088 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4092 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4093 EDIT_MoveDown_ML(es, shift);
4095 EDIT_MoveWordForward(es, shift);
4097 EDIT_MoveForward(es, shift);
4100 EDIT_MoveHome(es, shift);
4103 EDIT_MoveEnd(es, shift);
4106 if (es->style & ES_MULTILINE)
4107 EDIT_MovePageUp_ML(es, shift);
4109 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4112 if (es->style & ES_MULTILINE)
4113 EDIT_MovePageDown_ML(es, shift);
4115 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4118 if (!(es->style & ES_READONLY) && !(shift && control)) {
4119 if (es->selection_start != es->selection_end) {
4126 /* delete character left of caret */
4127 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4128 EDIT_MoveBackward(es, TRUE);
4130 } else if (control) {
4131 /* delete to end of line */
4132 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4133 EDIT_MoveEnd(es, TRUE);
4136 /* delete character right of caret */
4137 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4138 EDIT_MoveForward(es, TRUE);
4146 if (!(es->style & ES_READONLY))
4152 /* If the edit doesn't want the return send a message to the default object */
4153 if(!(es->style & ES_WANTRETURN))
4155 HWND hwndParent = GetParent(es->hwndSelf);
4156 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4157 if (HIWORD(dw) == DC_HASDEFID)
4159 SendMessageW( hwndParent, WM_COMMAND,
4160 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4161 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4170 /*********************************************************************
4175 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4177 es->flags &= ~EF_FOCUSED;
4179 if(!(es->style & ES_NOHIDESEL))
4180 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4181 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4186 /*********************************************************************
4190 * The caret position has been set on the WM_LBUTTONDOWN message
4193 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4196 INT e = es->selection_end;
4201 if (!(es->flags & EF_FOCUSED))
4204 l = EDIT_EM_LineFromChar(es, e);
4205 li = EDIT_EM_LineIndex(es, l);
4206 ll = EDIT_EM_LineLength(es, e);
4207 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4208 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4209 EDIT_EM_SetSel(es, s, e, FALSE);
4210 EDIT_EM_ScrollCaret(es);
4215 /*********************************************************************
4220 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4225 if (!(es->flags & EF_FOCUSED))
4228 es->bCaptureState = TRUE;
4229 SetCapture(es->hwndSelf);
4230 EDIT_ConfinePoint(es, &x, &y);
4231 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4232 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4233 EDIT_EM_ScrollCaret(es);
4234 es->region_posx = es->region_posy = 0;
4235 SetTimer(es->hwndSelf, 0, 100, NULL);
4240 /*********************************************************************
4245 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4247 if (es->bCaptureState) {
4248 KillTimer(es->hwndSelf, 0);
4249 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4251 es->bCaptureState = FALSE;
4256 /*********************************************************************
4261 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4263 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4268 /*********************************************************************
4273 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4279 if (GetCapture() != es->hwndSelf)
4283 * FIXME: gotta do some scrolling if outside client
4284 * area. Maybe reset the timer ?
4287 EDIT_ConfinePoint(es, &x, &y);
4288 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4289 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4290 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4291 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4292 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4297 /*********************************************************************
4301 * See also EDIT_WM_StyleChanged
4303 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4308 TRACE("Creating %s edit control, style = %08lx\n",
4309 unicode ? "Unicode" : "ANSI", lpcs->style);
4311 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4313 SetWindowLongW( hwnd, 0, (LONG)es );
4316 * Note: since the EDITSTATE has not been fully initialized yet,
4317 * we can't use any API calls that may send
4318 * WM_XXX messages before WM_NCCREATE is completed.
4321 es->is_unicode = unicode;
4322 es->style = lpcs->style;
4324 es->bEnableState = !(es->style & WS_DISABLED);
4326 es->hwndSelf = hwnd;
4327 /* Save parent, which will be notified by EN_* messages */
4328 es->hwndParent = lpcs->hwndParent;
4330 if (es->style & ES_COMBO)
4331 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4333 /* Number overrides lowercase overrides uppercase (at least it
4334 * does in Win95). However I'll bet that ES_NUMBER would be
4335 * invalid under Win 3.1.
4337 if (es->style & ES_NUMBER) {
4338 ; /* do not override the ES_NUMBER */
4339 } else if (es->style & ES_LOWERCASE) {
4340 es->style &= ~ES_UPPERCASE;
4342 if (es->style & ES_MULTILINE) {
4343 es->buffer_limit = BUFLIMIT_MULTI;
4344 if (es->style & WS_VSCROLL)
4345 es->style |= ES_AUTOVSCROLL;
4346 if (es->style & WS_HSCROLL)
4347 es->style |= ES_AUTOHSCROLL;
4348 es->style &= ~ES_PASSWORD;
4349 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4350 /* Confirmed - RIGHT overrides CENTER */
4351 if (es->style & ES_RIGHT)
4352 es->style &= ~ES_CENTER;
4353 es->style &= ~WS_HSCROLL;
4354 es->style &= ~ES_AUTOHSCROLL;
4357 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4358 es->style |= ES_AUTOVSCROLL;
4360 es->buffer_limit = BUFLIMIT_SINGLE;
4361 if (WIN31_LOOK == TWEAK_WineLook ||
4362 WIN95_LOOK == TWEAK_WineLook) {
4363 es->style &= ~ES_CENTER;
4364 es->style &= ~ES_RIGHT;
4366 if (es->style & ES_RIGHT)
4367 es->style &= ~ES_CENTER;
4369 es->style &= ~WS_HSCROLL;
4370 es->style &= ~WS_VSCROLL;
4371 es->style &= ~ES_AUTOVSCROLL;
4372 es->style &= ~ES_WANTRETURN;
4373 if (es->style & ES_PASSWORD)
4374 es->password_char = '*';
4376 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4377 es->style |= ES_AUTOHSCROLL;
4380 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4381 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4383 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4385 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4387 es->undo_buffer_size = es->buffer_size;
4389 if (es->style & ES_MULTILINE)
4390 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4395 * In Win95 look and feel, the WS_BORDER style is replaced by the
4396 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4397 * control a non client area. Not always. This coordinates in some
4398 * way with the window creation code in dialog.c When making
4399 * modifications please ensure that the code still works for edit
4400 * controls created directly with style 0x50800000, exStyle 0 (
4401 * which should have a single pixel border)
4403 if (TWEAK_WineLook != WIN31_LOOK)
4405 es->style &= ~WS_BORDER;
4409 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4410 SetWindowLongW( hwnd, GWL_STYLE,
4411 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_BORDER );
4417 /*********************************************************************
4422 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam)
4431 BOOL rev = es->bEnableState &&
4432 ((es->flags & EF_FOCUSED) ||
4433 (es->style & ES_NOHIDESEL));
4435 dc = BeginPaint(es->hwndSelf, &ps);
4438 if(es->style & WS_BORDER) {
4439 GetClientRect(es->hwndSelf, &rc);
4440 if(es->style & ES_MULTILINE) {
4441 if(es->style & WS_HSCROLL) rc.bottom++;
4442 if(es->style & WS_VSCROLL) rc.right++;
4444 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4446 IntersectClipRect(dc, es->format_rect.left,
4447 es->format_rect.top,
4448 es->format_rect.right,
4449 es->format_rect.bottom);
4450 if (es->style & ES_MULTILINE) {
4451 GetClientRect(es->hwndSelf, &rc);
4452 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4455 old_font = SelectObject(dc, es->font);
4456 EDIT_NotifyCtlColor(es, dc);
4458 if (!es->bEnableState)
4459 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4460 GetClipBox(dc, &rcRgn);
4461 if (es->style & ES_MULTILINE) {
4462 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4463 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4464 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4465 if (IntersectRect(&rc, &rcRgn, &rcLine))
4466 EDIT_PaintLine(es, dc, i, rev);
4469 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4470 if (IntersectRect(&rc, &rcRgn, &rcLine))
4471 EDIT_PaintLine(es, dc, 0, rev);
4474 SelectObject(dc, old_font);
4477 EndPaint(es->hwndSelf, &ps);
4481 /*********************************************************************
4486 static void EDIT_WM_Paste(EDITSTATE *es)
4491 /* Protect read-only edit control from modification */
4492 if(es->style & ES_READONLY)
4495 OpenClipboard(es->hwndSelf);
4496 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4497 src = (LPWSTR)GlobalLock(hsrc);
4498 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4505 /*********************************************************************
4510 static void EDIT_WM_SetFocus(EDITSTATE *es)
4512 es->flags |= EF_FOCUSED;
4513 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4514 EDIT_SetCaretPos(es, es->selection_end,
4515 es->flags & EF_AFTER_WRAP);
4516 if(!(es->style & ES_NOHIDESEL))
4517 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4518 ShowCaret(es->hwndSelf);
4519 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4523 /*********************************************************************
4527 * With Win95 look the margins are set to default font value unless
4528 * the system font (font == 0) is being set, in which case they are left
4532 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4540 dc = GetDC(es->hwndSelf);
4542 old_font = SelectObject(dc, font);
4543 GetTextMetricsW(dc, &tm);
4544 es->line_height = tm.tmHeight;
4545 es->char_width = tm.tmAveCharWidth;
4547 SelectObject(dc, old_font);
4548 ReleaseDC(es->hwndSelf, dc);
4549 if (font && (TWEAK_WineLook > WIN31_LOOK))
4550 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4551 EC_USEFONTINFO, EC_USEFONTINFO);
4553 /* Force the recalculation of the format rect for each font change */
4554 GetClientRect(es->hwndSelf, &r);
4555 EDIT_SetRectNP(es, &r);
4557 if (es->style & ES_MULTILINE)
4558 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4560 EDIT_CalcLineWidth_SL(es);
4563 EDIT_UpdateText(es, NULL, TRUE);
4564 if (es->flags & EF_FOCUSED) {
4566 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4567 EDIT_SetCaretPos(es, es->selection_end,
4568 es->flags & EF_AFTER_WRAP);
4569 ShowCaret(es->hwndSelf);
4574 /*********************************************************************
4579 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4580 * The modified flag is reset. No notifications are sent.
4582 * For single-line controls, reception of WM_SETTEXT triggers:
4583 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4586 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode)
4591 text = (LPWSTR)lParam;
4594 LPCSTR textA = (LPCSTR)lParam;
4595 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4596 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4597 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4600 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4602 TRACE("%s\n", debugstr_w(text));
4603 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4605 HeapFree(GetProcessHeap(), 0, text);
4607 static const WCHAR empty_stringW[] = {0};
4609 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4612 es->flags &= ~EF_MODIFIED;
4613 EDIT_EM_SetSel(es, 0, 0, FALSE);
4614 /* Send the notification after the selection start and end have been set
4615 * edit control doesn't send notification on WM_SETTEXT
4616 * if it is multiline, or it is part of combobox
4618 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4620 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4621 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4623 EDIT_EM_ScrollCaret(es);
4627 /*********************************************************************
4632 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4634 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4636 TRACE("width = %d, height = %d\n", width, height);
4637 SetRect(&rc, 0, 0, width, height);
4638 EDIT_SetRectNP(es, &rc);
4639 EDIT_UpdateText(es, NULL, TRUE);
4644 /*********************************************************************
4648 * This message is sent by SetWindowLong on having changed either the Style
4649 * or the extended style.
4651 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4653 * See also EDIT_WM_NCCreate
4655 * It appears that the Windows version of the edit control allows the style
4656 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4657 * style variable which will generally be different. In this function we
4658 * update the internal style based on what changed in the externally visible
4661 * Much of this content as based upon the MSDN, especially:
4662 * Platform SDK Documentation -> User Interface Services ->
4663 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4664 * Edit Control Styles
4666 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4668 if (GWL_STYLE == which) {
4669 DWORD style_change_mask;
4671 /* Only a subset of changes can be applied after the control
4674 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4676 if (es->style & ES_MULTILINE)
4677 style_change_mask |= ES_WANTRETURN;
4679 new_style = style->styleNew & style_change_mask;
4681 /* Number overrides lowercase overrides uppercase (at least it
4682 * does in Win95). However I'll bet that ES_NUMBER would be
4683 * invalid under Win 3.1.
4685 if (new_style & ES_NUMBER) {
4686 ; /* do not override the ES_NUMBER */
4687 } else if (new_style & ES_LOWERCASE) {
4688 new_style &= ~ES_UPPERCASE;
4691 es->style = (es->style & ~style_change_mask) | new_style;
4692 } else if (GWL_EXSTYLE == which) {
4693 ; /* FIXME - what is needed here */
4695 WARN ("Invalid style change %d\n",which);
4701 /*********************************************************************
4706 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4708 if ((key == VK_BACK) && (key_data & 0x2000)) {
4709 if (EDIT_EM_CanUndo(es))
4712 } else if (key == VK_UP || key == VK_DOWN) {
4713 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4716 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4720 /*********************************************************************
4725 static void EDIT_WM_Timer(EDITSTATE *es)
4727 if (es->region_posx < 0) {
4728 EDIT_MoveBackward(es, TRUE);
4729 } else if (es->region_posx > 0) {
4730 EDIT_MoveForward(es, TRUE);
4733 * FIXME: gotta do some vertical scrolling here, like
4734 * EDIT_EM_LineScroll(hwnd, 0, 1);
4738 /*********************************************************************
4743 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4747 if (!(es->style & ES_MULTILINE))
4750 if (!(es->style & ES_AUTOVSCROLL))
4759 TRACE("action %d\n", action);
4760 EDIT_EM_Scroll(es, action);
4767 TRACE("SB_BOTTOM\n");
4768 dy = es->line_count - 1 - es->y_offset;
4771 TRACE("SB_THUMBTRACK %d\n", pos);
4772 es->flags |= EF_VSCROLL_TRACK;
4773 if(es->style & WS_VSCROLL)
4774 dy = pos - es->y_offset;
4777 /* Assume default scroll range 0-100 */
4780 if(pos < 0 || pos > 100) return 0;
4781 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4782 new_y = pos * (es->line_count - vlc) / 100;
4783 dy = es->line_count ? (new_y - es->y_offset) : 0;
4784 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4785 es->line_count, es->y_offset, pos, dy);
4788 case SB_THUMBPOSITION:
4789 TRACE("SB_THUMBPOSITION %d\n", pos);
4790 es->flags &= ~EF_VSCROLL_TRACK;
4791 if(es->style & WS_VSCROLL)
4792 dy = pos - es->y_offset;
4795 /* Assume default scroll range 0-100 */
4798 if(pos < 0 || pos > 100) return 0;
4799 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4800 new_y = pos * (es->line_count - vlc) / 100;
4801 dy = es->line_count ? (new_y - es->y_offset) : 0;
4802 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4803 es->line_count, es->y_offset, pos, dy);
4807 /* force scroll info update */
4808 EDIT_UpdateScrollInfo(es);
4809 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4813 TRACE("SB_ENDSCROLL\n");
4816 * FIXME : the next two are undocumented !
4817 * Are we doing the right thing ?
4818 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4819 * although it's also a regular control message.
4821 case EM_GETTHUMB: /* this one is used by NT notepad */
4825 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4826 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4829 /* Assume default scroll range 0-100 */
4830 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4831 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4833 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4836 case EM_LINESCROLL16:
4837 TRACE("EM_LINESCROLL16 %d\n", pos);
4842 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4847 EDIT_EM_LineScroll(es, 0, dy);
4851 /*********************************************************************
4856 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4858 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4859 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4863 /*********************************************************************
4868 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4870 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4871 InvalidateRect(es->hwndSelf, rc, bErase);