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.
51 #include "wine/winbase16.h"
52 #include "wine/winuser16.h"
53 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(edit);
61 WINE_DECLARE_DEBUG_CHANNEL(combo);
62 WINE_DECLARE_DEBUG_CHANNEL(relay);
64 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
65 FIXME: BTW, new specs say 65535 (do you dare ???) */
66 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
67 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
68 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
69 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
72 * extra flags for EDITSTATE.flags field
74 #define EF_MODIFIED 0x0001 /* text has been modified */
75 #define EF_FOCUSED 0x0002 /* we have input focus */
76 #define EF_UPDATE 0x0004 /* notify parent of changed state */
77 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
79 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
80 wrapped line, instead of in front of the next character */
81 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
85 END_0 = 0, /* line ends with terminating '\0' character */
86 END_WRAP, /* line is wrapped */
87 END_HARD, /* line ends with a hard return '\r\n' */
88 END_SOFT, /* line ends with a soft return '\r\r\n' */
89 END_RICH /* line ends with a single '\n' */
92 typedef struct tagLINEDEF {
93 INT length; /* bruto length of a line in bytes */
94 INT net_length; /* netto length of a line in visible characters */
96 INT width; /* width of the line in pixels */
97 INT index; /* line index into the buffer */
98 struct tagLINEDEF *next;
103 BOOL is_unicode; /* how the control was created */
104 LPWSTR text; /* the actual contents of the control */
105 UINT buffer_size; /* the size of the buffer in characters */
106 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
107 HFONT font; /* NULL means standard system font */
108 INT x_offset; /* scroll offset for multi lines this is in pixels
109 for single lines it's in characters */
110 INT line_height; /* height of a screen line in pixels */
111 INT char_width; /* average character width in pixels */
112 DWORD style; /* sane version of wnd->dwStyle */
113 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
114 INT undo_insert_count; /* number of characters inserted in sequence */
115 UINT undo_position; /* character index of the insertion and deletion */
116 LPWSTR undo_text; /* deleted text */
117 UINT undo_buffer_size; /* size of the deleted text buffer */
118 INT selection_start; /* == selection_end if no selection */
119 INT selection_end; /* == current caret position */
120 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
121 INT left_margin; /* in pixels */
122 INT right_margin; /* in pixels */
124 INT text_width; /* width of the widest line in pixels for multi line controls
125 and just line width for single line controls */
126 INT region_posx; /* Position of cursor relative to region: */
127 INT region_posy; /* -1: to left, 0: within, 1: to right */
128 EDITWORDBREAKPROC16 word_break_proc16;
129 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
130 INT line_count; /* number of lines */
131 INT y_offset; /* scroll offset in number of lines */
132 BOOL bCaptureState; /* flag indicating whether mouse was captured */
133 BOOL bEnableState; /* flag keeping the enable state */
134 HWND hwndSelf; /* the our window handle */
135 HWND hwndParent; /* Handle of parent for sending EN_* messages.
136 Even if parent will change, EN_* messages
137 should be sent to the first parent. */
138 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
140 * only for multi line controls
142 INT lock_count; /* amount of re-entries in the EditWndProc */
145 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
146 HLOCAL hloc32W; /* our unicode local memory block */
147 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
149 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
154 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
155 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
157 /* used for disabled or read-only edit control */
158 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
160 { /* Notify parent which has created this edit control */ \
161 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
162 SendMessageW(es->hwndParent, WM_COMMAND, \
163 MAKEWPARAM(GetWindowLongW((es->hwndSelf),GWL_ID), wNotifyCode), \
164 (LPARAM)(es->hwndSelf)); \
167 /*********************************************************************
174 * These functions have trivial implementations
175 * We still like to call them internally
176 * "static inline" makes them more like macro's
178 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
179 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
180 static inline void EDIT_WM_Clear(EDITSTATE *es);
181 static inline void EDIT_WM_Cut(EDITSTATE *es);
184 * Helper functions only valid for one type of control
186 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
187 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
188 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
189 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
190 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
191 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
192 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
194 * Helper functions valid for both single line _and_ multi line controls
196 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
197 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
198 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
199 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
200 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
201 static void EDIT_LockBuffer(EDITSTATE *es);
202 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
203 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
204 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
205 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
206 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
207 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
208 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
209 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
210 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
211 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
212 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
213 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
214 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
215 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
216 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
218 * EM_XXX message handlers
220 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
221 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
222 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
223 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
224 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
225 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
226 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
227 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
228 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
229 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
230 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
231 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
232 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
233 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
234 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
235 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
236 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
237 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
238 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
239 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
240 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
241 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
242 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
243 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
244 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam);
245 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
246 static BOOL EDIT_EM_Undo(EDITSTATE *es);
248 * WM_XXX message handlers
250 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
251 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
252 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
253 static void EDIT_WM_Copy(EDITSTATE *es);
254 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
255 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
256 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
257 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
258 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
259 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
260 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
261 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
262 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
263 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
264 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
265 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
266 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
267 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam);
268 static void EDIT_WM_Paste(EDITSTATE *es);
269 static void EDIT_WM_SetFocus(EDITSTATE *es);
270 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
271 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode);
272 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
273 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
274 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
275 static void EDIT_WM_Timer(EDITSTATE *es);
276 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
277 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
278 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
280 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
281 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
283 /*********************************************************************
284 * edit class descriptor
286 const struct builtin_class_descr EDIT_builtin_class =
289 CS_DBLCLKS | CS_PARENTDC, /* style */
290 EditWndProcA, /* procA */
291 EditWndProcW, /* procW */
292 sizeof(EDITSTATE *), /* extra */
293 IDC_IBEAM, /* cursor */
298 /*********************************************************************
303 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
305 return (es->undo_insert_count || strlenW(es->undo_text));
309 /*********************************************************************
314 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
316 es->undo_insert_count = 0;
317 *es->undo_text = '\0';
321 /*********************************************************************
326 static inline void EDIT_WM_Clear(EDITSTATE *es)
328 static const WCHAR empty_stringW[] = {0};
330 /* Protect read-only edit control from modification */
331 if(es->style & ES_READONLY)
334 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
338 /*********************************************************************
343 static inline void EDIT_WM_Cut(EDITSTATE *es)
350 /**********************************************************************
353 * Returns the window version in case Wine emulates a later version
354 * of windows than the application expects.
356 * In a number of cases when windows runs an application that was
357 * designed for an earlier windows version, windows reverts
358 * to "old" behaviour of that earlier version.
360 * An example is a disabled edit control that needs to be painted.
361 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
362 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
363 * applications with an expected version 0f 4.0 or higher.
366 static DWORD get_app_version(void)
368 static DWORD version;
371 DWORD dwEmulatedVersion;
373 DWORD dwProcVersion = GetProcessVersion(0);
375 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
376 GetVersionExW( &info );
377 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
378 /* FIXME: this may not be 100% correct; see discussion on the
379 * wine developer list in Nov 1999 */
380 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
386 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
390 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
391 msg = WM_CTLCOLORSTATIC;
393 msg = WM_CTLCOLOREDIT;
395 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
396 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
399 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
402 return DefWindowProcW(hwnd, msg, wParam, lParam);
404 return DefWindowProcA(hwnd, msg, wParam, lParam);
407 /*********************************************************************
411 * The messages are in the order of the actual integer values
412 * (which can be found in include/windows.h)
413 * Wherever possible the 16 bit versions are converted to
414 * the 32 bit ones, so that we can 'fall through' to the
415 * helper functions. These are mostly 32 bit (with a few
416 * exceptions, clearly indicated by a '16' extension to their
420 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
421 WPARAM wParam, LPARAM lParam, BOOL unicode )
423 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
426 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
428 if (!es && msg != WM_NCCREATE)
429 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
430 else if (msg == WM_NCCREATE)
431 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
432 else if (msg == WM_DESTROY)
433 return EDIT_WM_Destroy(es);
436 if (es) EDIT_LockBuffer(es);
444 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
448 if ((short)LOWORD(lParam) == -1)
449 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
451 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
453 EDIT_EM_ScrollCaret(es);
457 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
458 EDIT_EM_ScrollCaret(es);
464 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
468 CopyRect((LPRECT)lParam, &es->format_rect);
472 if ((es->style & ES_MULTILINE) && lParam) {
474 CONV_RECT16TO32(MapSL(lParam), &rc);
475 EDIT_SetRectNP(es, &rc);
476 EDIT_UpdateText(es, NULL, TRUE);
480 if ((es->style & ES_MULTILINE) && lParam) {
481 EDIT_SetRectNP(es, (LPRECT)lParam);
482 EDIT_UpdateText(es, NULL, TRUE);
487 if ((es->style & ES_MULTILINE) && lParam) {
489 CONV_RECT16TO32(MapSL(lParam), &rc);
490 EDIT_SetRectNP(es, &rc);
494 if ((es->style & ES_MULTILINE) && lParam)
495 EDIT_SetRectNP(es, (LPRECT)lParam);
500 result = EDIT_EM_Scroll(es, (INT)wParam);
503 case EM_LINESCROLL16:
504 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
505 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
508 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
511 case EM_SCROLLCARET16:
513 EDIT_EM_ScrollCaret(es);
519 result = ((es->flags & EF_MODIFIED) != 0);
525 es->flags |= EF_MODIFIED;
527 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
530 case EM_GETLINECOUNT16:
531 case EM_GETLINECOUNT:
532 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
536 if ((INT16)wParam == -1)
540 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
544 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
547 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
551 result = (LRESULT)EDIT_EM_GetHandle16(es);
554 result = (LRESULT)EDIT_EM_GetHandle(es);
559 result = EDIT_EM_GetThumb(es);
562 /* these messages missing from specs */
571 FIXME("undocumented message 0x%x, please report\n", msg);
572 result = DefWindowProcW(hwnd, msg, wParam, lParam);
575 case EM_LINELENGTH16:
577 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
580 case EM_REPLACESEL16:
581 lParam = (LPARAM)MapSL(lParam);
582 unicode = FALSE; /* 16-bit message is always ascii */
589 textW = (LPWSTR)lParam;
592 LPSTR textA = (LPSTR)lParam;
593 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
594 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
595 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
598 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
602 HeapFree(GetProcessHeap(), 0, textW);
607 lParam = (LPARAM)MapSL(lParam);
608 unicode = FALSE; /* 16-bit message is always ascii */
611 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
615 case EM_SETLIMITTEXT:
616 EDIT_EM_SetLimitText(es, (INT)wParam);
621 result = (LRESULT)EDIT_EM_CanUndo(es);
627 result = (LRESULT)EDIT_EM_Undo(es);
632 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
635 case EM_LINEFROMCHAR16:
636 case EM_LINEFROMCHAR:
637 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
640 case EM_SETTABSTOPS16:
641 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
644 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
647 case EM_SETPASSWORDCHAR16:
648 unicode = FALSE; /* 16-bit message is always ascii */
650 case EM_SETPASSWORDCHAR:
655 charW = (WCHAR)wParam;
659 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
662 EDIT_EM_SetPasswordChar(es, charW);
666 case EM_EMPTYUNDOBUFFER16:
667 case EM_EMPTYUNDOBUFFER:
668 EDIT_EM_EmptyUndoBuffer(es);
671 case EM_GETFIRSTVISIBLELINE16:
672 result = es->y_offset;
674 case EM_GETFIRSTVISIBLELINE:
675 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
678 case EM_SETREADONLY16:
681 SetWindowLongW( hwnd, GWL_STYLE,
682 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
683 es->style |= ES_READONLY;
685 SetWindowLongW( hwnd, GWL_STYLE,
686 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
687 es->style &= ~ES_READONLY;
692 case EM_SETWORDBREAKPROC16:
693 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
695 case EM_SETWORDBREAKPROC:
696 EDIT_EM_SetWordBreakProc(es, lParam);
699 case EM_GETWORDBREAKPROC16:
700 result = (LRESULT)es->word_break_proc16;
702 case EM_GETWORDBREAKPROC:
703 result = (LRESULT)es->word_break_proc;
706 case EM_GETPASSWORDCHAR16:
707 unicode = FALSE; /* 16-bit message is always ascii */
709 case EM_GETPASSWORDCHAR:
712 result = es->password_char;
715 WCHAR charW = es->password_char;
717 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
723 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
726 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
730 result = MAKELONG(es->left_margin, es->right_margin);
733 case EM_GETLIMITTEXT:
734 result = es->buffer_limit;
738 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
742 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
745 /* End of the EM_ messages which were in numerical order; what order
746 * are these in? vaguely alphabetical?
750 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
752 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
754 int vk = (int)((LPMSG)lParam)->wParam;
756 if (vk == VK_RETURN && (GetWindowLongW( hwnd, GWL_STYLE ) & ES_WANTRETURN))
758 result |= DLGC_WANTMESSAGE;
760 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
762 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
763 result |= DLGC_WANTMESSAGE;
774 strng[0] = wParam >> 8;
775 strng[1] = wParam & 0xff;
776 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
777 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
778 EDIT_WM_Char(es, charW);
791 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
794 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
796 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
797 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
800 EDIT_WM_Char(es, charW);
809 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
813 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
822 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
825 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
829 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
830 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
831 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
833 result = EDIT_WM_Create(es, nameW);
835 HeapFree(GetProcessHeap(), 0, nameW);
844 es->bEnableState = (BOOL) wParam;
845 EDIT_UpdateText(es, NULL, TRUE);
849 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
853 result = (LRESULT)es->font;
857 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
860 case WM_GETTEXTLENGTH:
861 if (unicode) result = strlenW(es->text);
862 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
863 NULL, 0, NULL, NULL );
867 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
871 result = EDIT_WM_KeyDown(es, (INT)wParam);
875 result = EDIT_WM_KillFocus(es);
878 case WM_LBUTTONDBLCLK:
879 result = EDIT_WM_LButtonDblClk(es);
883 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
887 result = EDIT_WM_LButtonUp(es);
891 result = EDIT_WM_MButtonDown(es);
894 case WM_MOUSEACTIVATE:
895 result = MA_ACTIVATE;
899 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
903 EDIT_WM_Paint(es, wParam);
911 EDIT_WM_SetFocus(es);
915 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
919 /* FIXME: actually set an internal flag and behave accordingly */
923 EDIT_WM_SetText(es, lParam, unicode);
928 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
931 case WM_STYLECHANGED:
932 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
935 case WM_STYLECHANGING:
936 result = 0; /* See EDIT_WM_StyleChanged */
940 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
948 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
953 int gcWheelDelta = 0;
954 UINT pulScrollLines = 3;
955 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
957 if (wParam & (MK_SHIFT | MK_CONTROL)) {
958 result = DefWindowProcW(hwnd, msg, wParam, lParam);
961 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
962 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
964 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
965 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
966 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
971 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
975 if (es) EDIT_UnlockBuffer(es, FALSE);
980 /*********************************************************************
982 * EditWndProcW (USER32.@)
984 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
986 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
989 /*********************************************************************
991 * EditWndProc (USER32.@)
993 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
995 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
998 /*********************************************************************
1000 * EDIT_BuildLineDefs_ML
1002 * Build linked list of text lines.
1003 * Lines can end with '\0' (last line), a character (if it is wrapped),
1004 * a soft return '\r\r\n' or a hard return '\r\n'
1007 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1011 LPWSTR current_position, cp;
1013 LINEDEF *current_line;
1014 LINEDEF *previous_line;
1015 LINEDEF *start_line;
1016 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1017 INT line_count = es->line_count;
1018 INT orig_net_length;
1021 if (istart == iend && delta == 0)
1024 dc = GetDC(es->hwndSelf);
1026 old_font = SelectObject(dc, es->font);
1028 previous_line = NULL;
1029 current_line = es->first_line_def;
1031 /* Find starting line. istart must lie inside an existing line or
1032 * at the end of buffer */
1034 if (istart < current_line->index + current_line->length ||
1035 current_line->ending == END_0)
1038 previous_line = current_line;
1039 current_line = current_line->next;
1041 } while (current_line);
1043 if (!current_line) /* Error occurred start is not inside previous buffer */
1045 FIXME(" modification occurred outside buffer\n");
1046 ReleaseDC(es->hwndSelf, dc);
1050 /* Remember start of modifications in order to calculate update region */
1051 nstart_line = line_index;
1052 nstart_index = current_line->index;
1054 /* We must start to reformat from the previous line since the modifications
1055 * may have caused the line to wrap upwards. */
1056 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1059 current_line = previous_line;
1061 start_line = current_line;
1063 fw = es->format_rect.right - es->format_rect.left;
1064 current_position = es->text + current_line->index;
1066 if (current_line != start_line)
1068 if (!current_line || current_line->index + delta > current_position - es->text)
1070 /* The buffer has been expanded, create a new line and
1071 insert it into the link list */
1072 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1073 new_line->next = previous_line->next;
1074 previous_line->next = new_line;
1075 current_line = new_line;
1078 else if (current_line->index + delta < current_position - es->text)
1080 /* The previous line merged with this line so we delete this extra entry */
1081 previous_line->next = current_line->next;
1082 HeapFree(GetProcessHeap(), 0, current_line);
1083 current_line = previous_line->next;
1087 else /* current_line->index + delta == current_position */
1089 if (current_position - es->text > iend)
1090 break; /* We reached end of line modifications */
1091 /* else recalulate this line */
1095 current_line->index = current_position - es->text;
1096 orig_net_length = current_line->net_length;
1098 /* Find end of line */
1099 cp = current_position;
1101 if (*cp == '\n') break;
1102 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1107 /* Mark type of line termination */
1109 current_line->ending = END_0;
1110 current_line->net_length = strlenW(current_position);
1111 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1112 current_line->ending = END_SOFT;
1113 current_line->net_length = cp - current_position - 1;
1114 } else if (*cp == '\n') {
1115 current_line->ending = END_RICH;
1116 current_line->net_length = cp - current_position;
1118 current_line->ending = END_HARD;
1119 current_line->net_length = cp - current_position;
1122 /* Calculate line width */
1123 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1124 current_position, current_line->net_length,
1125 es->tabs_count, es->tabs));
1127 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1128 if (!(es->style & ES_AUTOHSCROLL)) {
1129 if (current_line->width > fw) {
1134 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1135 prev + 1, current_line->net_length, WB_RIGHT);
1136 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1137 current_position, next, es->tabs_count, es->tabs));
1138 } while (current_line->width <= fw);
1139 if (!prev) { /* Didn't find a line break so force a break */
1144 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1145 current_position, next, es->tabs_count, es->tabs));
1146 } while (current_line->width <= fw);
1151 /* If the first line we are calculating, wrapped before istart, we must
1152 * adjust istart in order for this to be reflected in the update region. */
1153 if (current_line->index == nstart_index && istart > current_line->index + prev)
1154 istart = current_line->index + prev;
1155 /* else if we are updating the previous line before the first line we
1156 * are re-calculating and it expanded */
1157 else if (current_line == start_line &&
1158 current_line->index != nstart_index && orig_net_length < prev)
1160 /* Line expanded due to an upwards line wrap so we must partially include
1161 * previous line in update region */
1162 nstart_line = line_index;
1163 nstart_index = current_line->index;
1164 istart = current_line->index + orig_net_length;
1167 current_line->net_length = prev;
1168 current_line->ending = END_WRAP;
1169 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1170 current_line->net_length, es->tabs_count, es->tabs));
1172 else if (orig_net_length < current_line->net_length &&
1173 current_line == start_line &&
1174 current_line->index != nstart_index) {
1175 /* The previous line expanded but it's still not as wide as the client rect */
1176 /* The expansion is due to an upwards line wrap so we must partially include
1177 it in the update region */
1178 nstart_line = line_index;
1179 nstart_index = current_line->index;
1180 istart = current_line->index + orig_net_length;
1185 /* Adjust length to include line termination */
1186 switch (current_line->ending) {
1188 current_line->length = current_line->net_length + 3;
1191 current_line->length = current_line->net_length + 1;
1194 current_line->length = current_line->net_length + 2;
1198 current_line->length = current_line->net_length;
1201 es->text_width = max(es->text_width, current_line->width);
1202 current_position += current_line->length;
1203 previous_line = current_line;
1204 current_line = current_line->next;
1206 } while (previous_line->ending != END_0);
1208 /* Finish adjusting line indexes by delta or remove hanging lines */
1209 if (previous_line->ending == END_0)
1211 LINEDEF *pnext = NULL;
1213 previous_line->next = NULL;
1214 while (current_line)
1216 pnext = current_line->next;
1217 HeapFree(GetProcessHeap(), 0, current_line);
1218 current_line = pnext;
1224 while (current_line)
1226 current_line->index += delta;
1227 current_line = current_line->next;
1231 /* Calculate rest of modification rectangle */
1236 * We calculate two rectangles. One for the first line which may have
1237 * an indent with respect to the format rect. The other is a format-width
1238 * rectangle that spans the rest of the lines that changed or moved.
1240 rc.top = es->format_rect.top + nstart_line * es->line_height -
1241 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1242 rc.bottom = rc.top + es->line_height;
1243 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1244 es->text + nstart_index, istart - nstart_index,
1245 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1246 rc.right = es->format_rect.right;
1247 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1250 rc.left = es->format_rect.left;
1251 rc.right = es->format_rect.right;
1253 * If lines were added or removed we must re-paint the remainder of the
1254 * lines since the remaining lines were either shifted up or down.
1256 if (line_count < es->line_count) /* We added lines */
1257 rc.bottom = es->line_count * es->line_height;
1258 else if (line_count > es->line_count) /* We removed lines */
1259 rc.bottom = line_count * es->line_height;
1261 rc.bottom = line_index * es->line_height;
1262 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1263 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1264 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1265 DeleteObject(tmphrgn);
1269 SelectObject(dc, old_font);
1271 ReleaseDC(es->hwndSelf, dc);
1274 /*********************************************************************
1276 * EDIT_CalcLineWidth_SL
1279 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1281 es->text_width = (short)LOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1284 /*********************************************************************
1286 * EDIT_CallWordBreakProc
1288 * Call appropriate WordBreakProc (internal or external).
1290 * Note: The "start" argument should always be an index referring
1291 * to es->text. The actual wordbreak proc might be
1292 * 16 bit, so we can't always pass any 32 bit LPSTR.
1293 * Hence we assume that es->text is the buffer that holds
1294 * the string under examination (we can decide this for ourselves).
1297 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1299 INT ret, iWndsLocks;
1301 /* To avoid any deadlocks, all the locks on the window structures
1302 must be suspended before the control is passed to the application */
1303 iWndsLocks = WIN_SuspendWndsLock();
1305 if (es->word_break_proc16) {
1312 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1313 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1314 segptr = K32WOWGlobalLock16(hglob16);
1315 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1316 args[4] = SELECTOROF(segptr);
1317 args[3] = OFFSETOF(segptr);
1321 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1322 ret = LOWORD(result);
1323 GlobalUnlock16(hglob16);
1324 GlobalFree16(hglob16);
1326 else if (es->word_break_proc)
1330 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1332 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1333 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1334 ret = wbpW(es->text + start, index, count, action);
1338 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1342 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1343 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1344 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1345 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1346 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1347 ret = wbpA(textA, index, countA, action);
1348 HeapFree(GetProcessHeap(), 0, textA);
1352 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1354 WIN_RestoreWndsLock(iWndsLocks);
1359 /*********************************************************************
1363 * Beware: This is not the function called on EM_CHARFROMPOS
1364 * The position _can_ be outside the formatting / client
1366 * The return value is only the character index
1369 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1375 if (es->style & ES_MULTILINE) {
1376 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1378 LINEDEF *line_def = es->first_line_def;
1380 while ((line > 0) && line_def->next) {
1381 line_index += line_def->length;
1382 line_def = line_def->next;
1385 x += es->x_offset - es->format_rect.left;
1386 if (x >= line_def->width) {
1388 *after_wrap = (line_def->ending == END_WRAP);
1389 return line_index + line_def->net_length;
1393 *after_wrap = FALSE;
1396 dc = GetDC(es->hwndSelf);
1398 old_font = SelectObject(dc, es->font);
1399 low = line_index + 1;
1400 high = line_index + line_def->net_length + 1;
1401 while (low < high - 1)
1403 INT mid = (low + high) / 2;
1404 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1410 *after_wrap = ((index == line_index + line_def->net_length) &&
1411 (line_def->ending == END_WRAP));
1416 *after_wrap = FALSE;
1417 x -= es->format_rect.left;
1419 return es->x_offset;
1420 text = EDIT_GetPasswordPointer_SL(es);
1421 dc = GetDC(es->hwndSelf);
1423 old_font = SelectObject(dc, es->font);
1427 INT high = es->x_offset;
1428 while (low < high - 1)
1430 INT mid = (low + high) / 2;
1431 GetTextExtentPoint32W( dc, text + mid,
1432 es->x_offset - mid, &size );
1433 if (size.cx > -x) low = mid;
1440 INT low = es->x_offset;
1441 INT high = strlenW(es->text) + 1;
1442 while (low < high - 1)
1444 INT mid = (low + high) / 2;
1445 GetTextExtentPoint32W( dc, text + es->x_offset,
1446 mid - es->x_offset, &size );
1447 if (size.cx > x) high = mid;
1452 if (es->style & ES_PASSWORD)
1453 HeapFree(GetProcessHeap(), 0, text);
1456 SelectObject(dc, old_font);
1457 ReleaseDC(es->hwndSelf, dc);
1462 /*********************************************************************
1466 * adjusts the point to be within the formatting rectangle
1467 * (so CharFromPos returns the nearest _visible_ character)
1470 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1472 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1473 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1477 /*********************************************************************
1481 * Calculates the bounding rectangle for a line from a starting
1482 * column to an ending column.
1485 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1487 INT line_index = EDIT_EM_LineIndex(es, line);
1489 if (es->style & ES_MULTILINE)
1490 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1492 rc->top = es->format_rect.top;
1493 rc->bottom = rc->top + es->line_height;
1494 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1495 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1499 /*********************************************************************
1501 * EDIT_GetPasswordPointer_SL
1503 * note: caller should free the (optionally) allocated buffer
1506 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1508 if (es->style & ES_PASSWORD) {
1509 INT len = strlenW(es->text);
1510 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1512 while(len) text[--len] = es->password_char;
1519 /*********************************************************************
1523 * This acts as a LOCAL_Lock(), but it locks only once. This way
1524 * you can call it whenever you like, without unlocking.
1526 * Initially the edit control allocates a HLOCAL32 buffer
1527 * (32 bit linear memory handler). However, 16 bit application
1528 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1529 * handler). From that moment on we have to keep using this 16 bit memory
1530 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1531 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1535 static void EDIT_LockBuffer(EDITSTATE *es)
1537 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
1541 BOOL _16bit = FALSE;
1547 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1548 textA = LocalLock(es->hloc32A);
1549 countA = strlen(textA) + 1;
1553 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1554 textA = LOCAL_Lock(hInstance, es->hloc16);
1555 countA = strlen(textA) + 1;
1560 ERR("no buffer ... please report\n");
1567 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1568 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1569 if(countW_new > es->buffer_size + 1)
1571 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1572 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1573 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1576 es->hloc32W = hloc32W_new;
1577 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1578 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1581 WARN("FAILED! Will synchronize partially\n");
1585 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1586 es->text = LocalLock(es->hloc32W);
1590 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1592 LOCAL_Unlock(hInstance, es->hloc16);
1594 LocalUnlock(es->hloc32A);
1601 /*********************************************************************
1603 * EDIT_SL_InvalidateText
1605 * Called from EDIT_InvalidateText().
1606 * Does the job for single-line controls only.
1609 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1614 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1615 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1616 EDIT_UpdateText(es, &rc, TRUE);
1620 /*********************************************************************
1622 * EDIT_ML_InvalidateText
1624 * Called from EDIT_InvalidateText().
1625 * Does the job for multi-line controls only.
1628 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1630 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1631 INT sl = EDIT_EM_LineFromChar(es, start);
1632 INT el = EDIT_EM_LineFromChar(es, end);
1641 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1644 sc = start - EDIT_EM_LineIndex(es, sl);
1645 ec = end - EDIT_EM_LineIndex(es, el);
1646 if (sl < es->y_offset) {
1650 if (el > es->y_offset + vlc) {
1651 el = es->y_offset + vlc;
1652 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1654 GetClientRect(es->hwndSelf, &rc1);
1655 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1657 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1658 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1659 EDIT_UpdateText(es, &rcUpdate, TRUE);
1661 EDIT_GetLineRect(es, sl, sc,
1662 EDIT_EM_LineLength(es,
1663 EDIT_EM_LineIndex(es, sl)),
1665 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1666 EDIT_UpdateText(es, &rcUpdate, TRUE);
1667 for (l = sl + 1 ; l < el ; l++) {
1668 EDIT_GetLineRect(es, l, 0,
1669 EDIT_EM_LineLength(es,
1670 EDIT_EM_LineIndex(es, l)),
1672 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1673 EDIT_UpdateText(es, &rcUpdate, TRUE);
1675 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1676 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1677 EDIT_UpdateText(es, &rcUpdate, TRUE);
1682 /*********************************************************************
1684 * EDIT_InvalidateText
1686 * Invalidate the text from offset start upto, but not including,
1687 * offset end. Useful for (re)painting the selection.
1688 * Regions outside the linewidth are not invalidated.
1689 * end == -1 means end == TextLength.
1690 * start and end need not be ordered.
1693 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1699 end = strlenW(es->text);
1707 if (es->style & ES_MULTILINE)
1708 EDIT_ML_InvalidateText(es, start, end);
1710 EDIT_SL_InvalidateText(es, start, end);
1714 /*********************************************************************
1718 * Try to fit size + 1 characters in the buffer.
1720 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1724 if (size <= es->buffer_size)
1727 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1729 /* Force edit to unlock it's buffer. es->text now NULL */
1730 EDIT_UnlockBuffer(es, TRUE);
1733 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1734 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1735 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1736 es->hloc32W = hNew32W;
1737 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1741 EDIT_LockBuffer(es);
1743 if (es->buffer_size < size) {
1744 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1745 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1748 TRACE("We now have %d+1\n", es->buffer_size);
1754 /*********************************************************************
1758 * Try to fit size + 1 bytes in the undo buffer.
1761 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1765 if (size <= es->undo_buffer_size)
1768 TRACE("trying to ReAlloc to %d+1\n", size);
1770 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1771 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1772 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1777 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1783 /*********************************************************************
1788 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1790 INT e = es->selection_end;
1794 if ((es->style & ES_MULTILINE) && e &&
1795 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1797 if (e && (es->text[e - 1] == '\r'))
1801 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1802 EDIT_EM_ScrollCaret(es);
1806 /*********************************************************************
1810 * Only for multi line controls
1811 * Move the caret one line down, on a column with the nearest
1812 * x coordinate on the screen (might be a different column).
1815 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1817 INT s = es->selection_start;
1818 INT e = es->selection_end;
1819 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1820 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1821 INT x = (short)LOWORD(pos);
1822 INT y = (short)HIWORD(pos);
1824 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1827 EDIT_EM_SetSel(es, s, e, after_wrap);
1828 EDIT_EM_ScrollCaret(es);
1832 /*********************************************************************
1837 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1839 BOOL after_wrap = FALSE;
1842 /* Pass a high value in x to make sure of receiving the end of the line */
1843 if (es->style & ES_MULTILINE)
1844 e = EDIT_CharFromPos(es, 0x3fffffff,
1845 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1847 e = strlenW(es->text);
1848 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1849 EDIT_EM_ScrollCaret(es);
1853 /*********************************************************************
1858 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1860 INT e = es->selection_end;
1864 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1865 if (es->text[e] == '\n')
1867 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1871 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1872 EDIT_EM_ScrollCaret(es);
1876 /*********************************************************************
1880 * Home key: move to beginning of line.
1883 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1887 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1888 if (es->style & ES_MULTILINE)
1889 e = EDIT_CharFromPos(es, -es->x_offset,
1890 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1893 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1894 EDIT_EM_ScrollCaret(es);
1898 /*********************************************************************
1900 * EDIT_MovePageDown_ML
1902 * Only for multi line controls
1903 * Move the caret one page down, on a column with the nearest
1904 * x coordinate on the screen (might be a different column).
1907 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1909 INT s = es->selection_start;
1910 INT e = es->selection_end;
1911 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1912 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1913 INT x = (short)LOWORD(pos);
1914 INT y = (short)HIWORD(pos);
1916 e = EDIT_CharFromPos(es, x,
1917 y + (es->format_rect.bottom - es->format_rect.top),
1921 EDIT_EM_SetSel(es, s, e, after_wrap);
1922 EDIT_EM_ScrollCaret(es);
1926 /*********************************************************************
1928 * EDIT_MovePageUp_ML
1930 * Only for multi line controls
1931 * Move the caret one page up, on a column with the nearest
1932 * x coordinate on the screen (might be a different column).
1935 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1937 INT s = es->selection_start;
1938 INT e = es->selection_end;
1939 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1940 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1941 INT x = (short)LOWORD(pos);
1942 INT y = (short)HIWORD(pos);
1944 e = EDIT_CharFromPos(es, x,
1945 y - (es->format_rect.bottom - es->format_rect.top),
1949 EDIT_EM_SetSel(es, s, e, after_wrap);
1950 EDIT_EM_ScrollCaret(es);
1954 /*********************************************************************
1958 * Only for multi line controls
1959 * Move the caret one line up, on a column with the nearest
1960 * x coordinate on the screen (might be a different column).
1963 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1965 INT s = es->selection_start;
1966 INT e = es->selection_end;
1967 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1968 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1969 INT x = (short)LOWORD(pos);
1970 INT y = (short)HIWORD(pos);
1972 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1975 EDIT_EM_SetSel(es, s, e, after_wrap);
1976 EDIT_EM_ScrollCaret(es);
1980 /*********************************************************************
1982 * EDIT_MoveWordBackward
1985 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1987 INT s = es->selection_start;
1988 INT e = es->selection_end;
1993 l = EDIT_EM_LineFromChar(es, e);
1994 ll = EDIT_EM_LineLength(es, e);
1995 li = EDIT_EM_LineIndex(es, l);
1998 li = EDIT_EM_LineIndex(es, l - 1);
1999 e = li + EDIT_EM_LineLength(es, li);
2002 e = li + (INT)EDIT_CallWordBreakProc(es,
2003 li, e - li, ll, WB_LEFT);
2007 EDIT_EM_SetSel(es, s, e, FALSE);
2008 EDIT_EM_ScrollCaret(es);
2012 /*********************************************************************
2014 * EDIT_MoveWordForward
2017 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2019 INT s = es->selection_start;
2020 INT e = es->selection_end;
2025 l = EDIT_EM_LineFromChar(es, e);
2026 ll = EDIT_EM_LineLength(es, e);
2027 li = EDIT_EM_LineIndex(es, l);
2029 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2030 e = EDIT_EM_LineIndex(es, l + 1);
2032 e = li + EDIT_CallWordBreakProc(es,
2033 li, e - li + 1, ll, WB_RIGHT);
2037 EDIT_EM_SetSel(es, s, e, FALSE);
2038 EDIT_EM_ScrollCaret(es);
2042 /*********************************************************************
2047 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2049 INT s = es->selection_start;
2050 INT e = es->selection_end;
2057 if (es->style & ES_MULTILINE) {
2058 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2059 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2064 TRACE("line=%d\n", line);
2066 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2067 x = (short)LOWORD(pos);
2068 y = (short)HIWORD(pos);
2069 li = EDIT_EM_LineIndex(es, line);
2070 ll = EDIT_EM_LineLength(es, li);
2071 s = min(es->selection_start, es->selection_end);
2072 e = max(es->selection_start, es->selection_end);
2073 s = min(li + ll, max(li, s));
2074 e = min(li + ll, max(li, e));
2075 if (rev && (s != e) &&
2076 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2077 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2078 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2079 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2081 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2085 /*********************************************************************
2090 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2101 BkMode = GetBkMode(dc);
2102 BkColor = GetBkColor(dc);
2103 TextColor = GetTextColor(dc);
2105 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2106 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2107 SetBkMode( dc, OPAQUE);
2109 li = EDIT_EM_LineIndex(es, line);
2110 if (es->style & ES_MULTILINE) {
2111 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2112 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2114 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2115 TextOutW(dc, x, y, text + li + col, count);
2116 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2118 if (es->style & ES_PASSWORD)
2119 HeapFree(GetProcessHeap(), 0, text);
2122 SetBkColor(dc, BkColor);
2123 SetTextColor(dc, TextColor);
2124 SetBkMode( dc, BkMode);
2130 /*********************************************************************
2135 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2138 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2139 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2140 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2144 /*********************************************************************
2148 * note: this is not (exactly) the handler called on EM_SETRECTNP
2149 * it is also used to set the rect of a single line control
2152 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2154 CopyRect(&es->format_rect, rc);
2155 if (es->style & WS_BORDER) {
2156 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2157 es->format_rect.left += bw;
2158 es->format_rect.top += bw;
2159 es->format_rect.right -= bw;
2160 es->format_rect.bottom -= bw;
2162 es->format_rect.left += es->left_margin;
2163 es->format_rect.right -= es->right_margin;
2164 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2165 if (es->style & ES_MULTILINE)
2167 INT fw, vlc, max_x_offset, max_y_offset;
2169 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2170 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2172 /* correct es->x_offset */
2173 fw = es->format_rect.right - es->format_rect.left;
2174 max_x_offset = es->text_width - fw;
2175 if(max_x_offset < 0) max_x_offset = 0;
2176 if(es->x_offset > max_x_offset)
2177 es->x_offset = max_x_offset;
2179 /* correct es->y_offset */
2180 max_y_offset = es->line_count - vlc;
2181 if(max_y_offset < 0) max_y_offset = 0;
2182 if(es->y_offset > max_y_offset)
2183 es->y_offset = max_y_offset;
2185 /* force scroll info update */
2186 EDIT_UpdateScrollInfo(es);
2189 /* Windows doesn't care to fix text placement for SL controls */
2190 es->format_rect.bottom = es->format_rect.top + es->line_height;
2192 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2193 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2197 /*********************************************************************
2202 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2204 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2206 /* Edit window might be already destroyed */
2207 if(!IsWindow(es->hwndSelf))
2209 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2213 if (!es->lock_count) {
2214 ERR("lock_count == 0 ... please report\n");
2218 ERR("es->text == 0 ... please report\n");
2222 if (force || (es->lock_count == 1)) {
2225 BOOL _16bit = FALSE;
2227 UINT countW = strlenW(es->text) + 1;
2231 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2232 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2233 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2234 countA = LocalSize(es->hloc32A);
2235 if(countA_new > countA)
2238 UINT alloc_size = ROUND_TO_GROW(countA_new);
2239 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2240 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2243 es->hloc32A = hloc32A_new;
2244 countA = LocalSize(hloc32A_new);
2245 TRACE("Real new size %d bytes\n", countA);
2248 WARN("FAILED! Will synchronize partially\n");
2250 textA = LocalLock(es->hloc32A);
2254 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2255 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2256 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2257 countA = LOCAL_Size(hInstance, es->hloc16);
2258 if(countA_new > countA)
2260 HLOCAL16 hloc16_new;
2261 UINT alloc_size = ROUND_TO_GROW(countA_new);
2262 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2263 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2266 es->hloc16 = hloc16_new;
2267 countA = LOCAL_Size(hInstance, hloc16_new);
2268 TRACE("Real new size %d bytes\n", countA);
2271 WARN("FAILED! Will synchronize partially\n");
2273 textA = LOCAL_Lock(hInstance, es->hloc16);
2279 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2281 LOCAL_Unlock(hInstance, es->hloc16);
2283 LocalUnlock(es->hloc32A);
2286 LocalUnlock(es->hloc32W);
2290 ERR("no buffer ... please report\n");
2298 /*********************************************************************
2300 * EDIT_UpdateScrollInfo
2303 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2305 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2308 si.cbSize = sizeof(SCROLLINFO);
2309 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2311 si.nMax = es->line_count - 1;
2312 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2313 si.nPos = es->y_offset;
2314 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2315 si.nMin, si.nMax, si.nPage, si.nPos);
2316 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2319 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2322 si.cbSize = sizeof(SCROLLINFO);
2323 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2325 si.nMax = es->text_width - 1;
2326 si.nPage = es->format_rect.right - es->format_rect.left;
2327 si.nPos = es->x_offset;
2328 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2329 si.nMin, si.nMax, si.nPage, si.nPos);
2330 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2334 /*********************************************************************
2336 * EDIT_WordBreakProc
2338 * Find the beginning of words.
2339 * Note: unlike the specs for a WordBreakProc, this function only
2340 * allows to be called without linebreaks between s[0] upto
2341 * s[count - 1]. Remember it is only called
2342 * internally, so we can decide this for ourselves.
2345 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2349 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2359 if (s[index] == ' ') {
2360 while (index && (s[index] == ' '))
2363 while (index && (s[index] != ' '))
2365 if (s[index] == ' ')
2369 while (index && (s[index] != ' '))
2371 if (s[index] == ' ')
2381 if (s[index] == ' ')
2382 while ((index < count) && (s[index] == ' ')) index++;
2384 while (s[index] && (s[index] != ' ') && (index < count))
2386 while ((s[index] == ' ') && (index < count)) index++;
2390 case WB_ISDELIMITER:
2391 ret = (s[index] == ' ');
2394 ERR("unknown action code, please report !\n");
2401 /*********************************************************************
2405 * returns line number (not index) in high-order word of result.
2406 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2407 * to Richedit, not to the edit control. Original documentation is valid.
2408 * FIXME: do the specs mean to return -1 if outside client area or
2409 * if outside formatting rectangle ???
2412 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2420 GetClientRect(es->hwndSelf, &rc);
2421 if (!PtInRect(&rc, pt))
2424 index = EDIT_CharFromPos(es, x, y, NULL);
2425 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2429 /*********************************************************************
2433 * Enable or disable soft breaks.
2435 * This means: insert or remove the soft linebreak character (\r\r\n).
2436 * Take care to check if the text still fits the buffer after insertion.
2437 * If not, notify with EN_ERRSPACE.
2440 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2442 es->flags &= ~EF_USE_SOFTBRK;
2444 es->flags |= EF_USE_SOFTBRK;
2445 FIXME("soft break enabled, not implemented\n");
2451 /*********************************************************************
2455 * Hopefully this won't fire back at us.
2456 * We always start with a fixed buffer in the local heap.
2457 * Despite of the documentation says that the local heap is used
2458 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2459 * buffer on the local heap.
2462 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2466 if (!(es->style & ES_MULTILINE))
2470 hLocal = es->hloc32W;
2476 UINT countA, alloc_size;
2477 TRACE("Allocating 32-bit ANSI alias buffer\n");
2478 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2479 alloc_size = ROUND_TO_GROW(countA);
2480 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2482 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2485 textA = LocalLock(es->hloc32A);
2486 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2487 LocalUnlock(es->hloc32A);
2489 hLocal = es->hloc32A;
2492 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2497 /*********************************************************************
2501 * Hopefully this won't fire back at us.
2502 * We always start with a buffer in 32 bit linear memory.
2503 * However, with this message a 16 bit application requests
2504 * a handle of 16 bit local heap memory, where it expects to find
2506 * It's a pitty that from this moment on we have to use this
2507 * local heap, because applications may rely on the handle
2510 * In this function we'll try to switch to local heap.
2512 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2514 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2516 UINT countA, alloc_size;
2518 if (!(es->style & ES_MULTILINE))
2524 if (!LOCAL_HeapSize(hInstance)) {
2525 if (!LocalInit16(hInstance, 0,
2526 GlobalSize16(hInstance))) {
2527 ERR("could not initialize local heap\n");
2530 TRACE("local heap initialized\n");
2533 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2534 alloc_size = ROUND_TO_GROW(countA);
2536 TRACE("Allocating 16-bit ANSI alias buffer\n");
2537 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2538 ERR("could not allocate new 16 bit buffer\n");
2542 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2543 ERR("could not lock new 16 bit buffer\n");
2544 LOCAL_Free(hInstance, es->hloc16);
2549 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2550 LOCAL_Unlock(hInstance, es->hloc16);
2552 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2557 /*********************************************************************
2562 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2565 INT line_len, dst_len;
2568 if (es->style & ES_MULTILINE) {
2569 if (line >= es->line_count)
2573 i = EDIT_EM_LineIndex(es, line);
2575 line_len = EDIT_EM_LineLength(es, i);
2576 dst_len = *(WORD *)lParam;
2579 LPWSTR dst = (LPWSTR)lParam;
2580 if(dst_len <= line_len)
2582 memcpy(dst, src, dst_len * sizeof(WCHAR));
2585 else /* Append 0 if enough space */
2587 memcpy(dst, src, line_len * sizeof(WCHAR));
2594 LPSTR dst = (LPSTR)lParam;
2596 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2597 if(!ret) /* Insufficient buffer size */
2599 if(ret < dst_len) /* Append 0 if enough space */
2606 /*********************************************************************
2611 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2613 UINT s = es->selection_start;
2614 UINT e = es->selection_end;
2621 return MAKELONG(s, e);
2625 /*********************************************************************
2629 * FIXME: is this right ? (or should it be only VSCROLL)
2630 * (and maybe only for edit controls that really have their
2631 * own scrollbars) (and maybe only for multiline controls ?)
2632 * All in all: very poorly documented
2635 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2637 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2638 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2642 /*********************************************************************
2647 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2652 if (!(es->style & ES_MULTILINE))
2654 if (index > (INT)strlenW(es->text))
2655 return es->line_count - 1;
2657 index = min(es->selection_start, es->selection_end);
2660 line_def = es->first_line_def;
2661 index -= line_def->length;
2662 while ((index >= 0) && line_def->next) {
2664 line_def = line_def->next;
2665 index -= line_def->length;
2671 /*********************************************************************
2676 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2681 if (!(es->style & ES_MULTILINE))
2683 if (line >= es->line_count)
2687 line_def = es->first_line_def;
2689 INT index = es->selection_end - line_def->length;
2690 while ((index >= 0) && line_def->next) {
2691 line_index += line_def->length;
2692 line_def = line_def->next;
2693 index -= line_def->length;
2697 line_index += line_def->length;
2698 line_def = line_def->next;
2706 /*********************************************************************
2711 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2715 if (!(es->style & ES_MULTILINE))
2716 return strlenW(es->text);
2719 /* get the number of remaining non-selected chars of selected lines */
2720 INT32 l; /* line number */
2721 INT32 li; /* index of first char in line */
2723 l = EDIT_EM_LineFromChar(es, es->selection_start);
2724 /* # chars before start of selection area */
2725 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2726 l = EDIT_EM_LineFromChar(es, es->selection_end);
2727 /* # chars after end of selection */
2728 li = EDIT_EM_LineIndex(es, l);
2729 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2732 line_def = es->first_line_def;
2733 index -= line_def->length;
2734 while ((index >= 0) && line_def->next) {
2735 line_def = line_def->next;
2736 index -= line_def->length;
2738 return line_def->net_length;
2742 /*********************************************************************
2746 * NOTE: dx is in average character widths, dy - in lines;
2749 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2751 if (!(es->style & ES_MULTILINE))
2754 dx *= es->char_width;
2755 return EDIT_EM_LineScroll_internal(es, dx, dy);
2758 /*********************************************************************
2760 * EDIT_EM_LineScroll_internal
2762 * Version of EDIT_EM_LineScroll for internal use.
2763 * It doesn't refuse if ES_MULTILINE is set and assumes that
2764 * dx is in pixels, dy - in lines.
2767 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2770 INT x_offset_in_pixels;
2772 if (es->style & ES_MULTILINE)
2774 x_offset_in_pixels = es->x_offset;
2779 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2782 if (-dx > x_offset_in_pixels)
2783 dx = -x_offset_in_pixels;
2784 if (dx > es->text_width - x_offset_in_pixels)
2785 dx = es->text_width - x_offset_in_pixels;
2786 nyoff = max(0, es->y_offset + dy);
2787 if (nyoff >= es->line_count)
2788 nyoff = es->line_count - 1;
2789 dy = (es->y_offset - nyoff) * es->line_height;
2794 es->y_offset = nyoff;
2795 if(es->style & ES_MULTILINE)
2798 es->x_offset += dx / es->char_width;
2800 GetClientRect(es->hwndSelf, &rc1);
2801 IntersectRect(&rc, &rc1, &es->format_rect);
2802 ScrollWindowEx(es->hwndSelf, -dx, dy,
2803 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2804 /* force scroll info update */
2805 EDIT_UpdateScrollInfo(es);
2807 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2808 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2809 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2810 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2815 /*********************************************************************
2820 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2822 INT len = strlenW(es->text);
2831 index = min(index, len);
2832 dc = GetDC(es->hwndSelf);
2834 old_font = SelectObject(dc, es->font);
2835 if (es->style & ES_MULTILINE) {
2836 l = EDIT_EM_LineFromChar(es, index);
2837 y = (l - es->y_offset) * es->line_height;
2838 li = EDIT_EM_LineIndex(es, l);
2839 if (after_wrap && (li == index) && l) {
2841 LINEDEF *line_def = es->first_line_def;
2843 line_def = line_def->next;
2846 if (line_def->ending == END_WRAP) {
2848 y -= es->line_height;
2849 li = EDIT_EM_LineIndex(es, l);
2852 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2853 es->tabs_count, es->tabs)) - es->x_offset;
2855 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2856 if (index < es->x_offset) {
2857 GetTextExtentPoint32W(dc, text + index,
2858 es->x_offset - index, &size);
2861 GetTextExtentPoint32W(dc, text + es->x_offset,
2862 index - es->x_offset, &size);
2866 if (es->style & ES_PASSWORD)
2867 HeapFree(GetProcessHeap(), 0, text);
2869 x += es->format_rect.left;
2870 y += es->format_rect.top;
2872 SelectObject(dc, old_font);
2873 ReleaseDC(es->hwndSelf, dc);
2874 return MAKELONG((INT16)x, (INT16)y);
2878 /*********************************************************************
2882 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2885 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2887 UINT strl = strlenW(lpsz_replace);
2888 UINT tl = strlenW(es->text);
2897 TRACE("%s, can_undo %d, send_update %d\n",
2898 debugstr_w(lpsz_replace), can_undo, send_update);
2900 s = es->selection_start;
2901 e = es->selection_end;
2903 if ((s == e) && !strl)
2908 /* Issue the EN_MAXTEXT notification and continue with replacing text
2909 * such that buffer limit is honored. */
2910 size = tl - (e - s) + strl;
2911 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
2912 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
2913 strl = es->buffer_limit - (tl - (e-s));
2916 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2920 /* there is something to be deleted */
2921 TRACE("deleting stuff.\n");
2923 utl = strlenW(es->undo_text);
2924 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2925 /* undo-buffer is extended to the right */
2926 EDIT_MakeUndoFit(es, utl + e - s);
2927 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2928 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2929 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2930 /* undo-buffer is extended to the left */
2931 EDIT_MakeUndoFit(es, utl + e - s);
2932 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2934 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2935 p[i] = (es->text + s)[i];
2936 es->undo_position = s;
2938 /* new undo-buffer */
2939 EDIT_MakeUndoFit(es, e - s);
2940 strncpyW(es->undo_text, es->text + s, e - s + 1);
2941 es->undo_text[e - s] = 0; /* ensure 0 termination */
2942 es->undo_position = s;
2944 /* any deletion makes the old insertion-undo invalid */
2945 es->undo_insert_count = 0;
2947 EDIT_EM_EmptyUndoBuffer(es);
2950 strcpyW(es->text + s, es->text + e);
2953 /* there is an insertion */
2955 if ((s == es->undo_position) ||
2956 ((es->undo_insert_count) &&
2957 (s == es->undo_position + es->undo_insert_count)))
2959 * insertion is new and at delete position or
2960 * an extension to either left or right
2962 es->undo_insert_count += strl;
2964 /* new insertion undo */
2965 es->undo_position = s;
2966 es->undo_insert_count = strl;
2967 /* new insertion makes old delete-buffer invalid */
2968 *es->undo_text = '\0';
2971 EDIT_EM_EmptyUndoBuffer(es);
2974 tl = strlenW(es->text);
2975 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));
2976 for (p = es->text + tl ; p >= es->text + s ; p--)
2978 for (i = 0 , p = es->text + s ; i < strl ; i++)
2979 p[i] = lpsz_replace[i];
2980 if(es->style & ES_UPPERCASE)
2981 CharUpperBuffW(p, strl);
2982 else if(es->style & ES_LOWERCASE)
2983 CharLowerBuffW(p, strl);
2986 if (es->style & ES_MULTILINE)
2988 INT s = min(es->selection_start, es->selection_end);
2990 hrgn = CreateRectRgn(0, 0, 0, 0);
2991 EDIT_BuildLineDefs_ML(es, s, s + strl,
2992 strl - abs(es->selection_end - es->selection_start), hrgn);
2995 EDIT_CalcLineWidth_SL(es);
2997 EDIT_EM_SetSel(es, s, s, FALSE);
2998 es->flags |= EF_MODIFIED;
2999 if (send_update) es->flags |= EF_UPDATE;
3002 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3006 EDIT_UpdateText(es, NULL, TRUE);
3008 EDIT_EM_ScrollCaret(es);
3010 /* force scroll info update */
3011 EDIT_UpdateScrollInfo(es);
3014 if(es->flags & EF_UPDATE)
3016 es->flags &= ~EF_UPDATE;
3017 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3022 /*********************************************************************
3027 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3031 if (!(es->style & ES_MULTILINE))
3032 return (LRESULT)FALSE;
3042 if (es->y_offset < es->line_count - 1)
3047 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3050 if (es->y_offset < es->line_count - 1)
3051 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3054 return (LRESULT)FALSE;
3057 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3058 /* check if we are going to move too far */
3059 if(es->y_offset + dy > es->line_count - vlc)
3060 dy = es->line_count - vlc - es->y_offset;
3062 /* Notification is done in EDIT_EM_LineScroll */
3064 EDIT_EM_LineScroll(es, 0, dy);
3066 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3070 /*********************************************************************
3075 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3077 if (es->style & ES_MULTILINE) {
3082 INT cw = es->char_width;
3087 l = EDIT_EM_LineFromChar(es, es->selection_end);
3088 li = EDIT_EM_LineIndex(es, l);
3089 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3090 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3091 if (l >= es->y_offset + vlc)
3092 dy = l - vlc + 1 - es->y_offset;
3093 if (l < es->y_offset)
3094 dy = l - es->y_offset;
3095 ww = es->format_rect.right - es->format_rect.left;
3096 if (x < es->format_rect.left)
3097 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3098 if (x > es->format_rect.right)
3099 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3102 /* check if we are going to move too far */
3103 if(es->x_offset + dx + ww > es->text_width)
3104 dx = es->text_width - ww - es->x_offset;
3106 EDIT_EM_LineScroll_internal(es, dx, dy);
3113 if (!(es->style & ES_AUTOHSCROLL))
3116 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3117 format_width = es->format_rect.right - es->format_rect.left;
3118 if (x < es->format_rect.left) {
3119 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3122 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3123 } while ((x < goal) && es->x_offset);
3124 /* FIXME: use ScrollWindow() somehow to improve performance */
3125 EDIT_UpdateText(es, NULL, TRUE);
3126 } else if (x > es->format_rect.right) {
3128 INT len = strlenW(es->text);
3129 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3132 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3133 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3134 } while ((x > goal) && (x_last > es->format_rect.right));
3135 /* FIXME: use ScrollWindow() somehow to improve performance */
3136 EDIT_UpdateText(es, NULL, TRUE);
3140 if(es->flags & EF_FOCUSED)
3141 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3145 /*********************************************************************
3149 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3152 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3154 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3156 if (!(es->style & ES_MULTILINE))
3160 WARN("called with NULL handle\n");
3164 EDIT_UnlockBuffer(es, TRUE);
3168 LOCAL_Free(hInstance, es->hloc16);
3169 es->hloc16 = (HLOCAL16)NULL;
3176 LocalFree(es->hloc32A);
3188 countA = LocalSize(hloc);
3189 textA = LocalLock(hloc);
3190 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3191 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3193 ERR("Could not allocate new unicode buffer\n");
3196 textW = LocalLock(hloc32W_new);
3197 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3198 LocalUnlock(hloc32W_new);
3202 LocalFree(es->hloc32W);
3204 es->hloc32W = hloc32W_new;
3208 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3210 EDIT_LockBuffer(es);
3212 es->x_offset = es->y_offset = 0;
3213 es->selection_start = es->selection_end = 0;
3214 EDIT_EM_EmptyUndoBuffer(es);
3215 es->flags &= ~EF_MODIFIED;
3216 es->flags &= ~EF_UPDATE;
3217 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3218 EDIT_UpdateText(es, NULL, TRUE);
3219 EDIT_EM_ScrollCaret(es);
3220 /* force scroll info update */
3221 EDIT_UpdateScrollInfo(es);
3225 /*********************************************************************
3229 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3232 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3234 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3240 if (!(es->style & ES_MULTILINE))
3244 WARN("called with NULL handle\n");
3248 EDIT_UnlockBuffer(es, TRUE);
3252 LocalFree(es->hloc32A);
3256 countA = LOCAL_Size(hInstance, hloc);
3257 textA = LOCAL_Lock(hInstance, hloc);
3258 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3259 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3261 ERR("Could not allocate new unicode buffer\n");
3264 textW = LocalLock(hloc32W_new);
3265 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3266 LocalUnlock(hloc32W_new);
3267 LOCAL_Unlock(hInstance, hloc);
3270 LocalFree(es->hloc32W);
3272 es->hloc32W = hloc32W_new;
3275 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3277 EDIT_LockBuffer(es);
3279 es->x_offset = es->y_offset = 0;
3280 es->selection_start = es->selection_end = 0;
3281 EDIT_EM_EmptyUndoBuffer(es);
3282 es->flags &= ~EF_MODIFIED;
3283 es->flags &= ~EF_UPDATE;
3284 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3285 EDIT_UpdateText(es, NULL, TRUE);
3286 EDIT_EM_ScrollCaret(es);
3287 /* force scroll info update */
3288 EDIT_UpdateScrollInfo(es);
3292 /*********************************************************************
3296 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3297 * However, the windows version is not complied to yet in all of edit.c
3299 * Additionally as the wrapper for RichEdit controls we need larger buffers
3300 * at present -1 will represent nolimit
3302 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3304 if (limit == 0xFFFFFFFF)
3305 es->buffer_limit = -1;
3306 else if (es->style & ES_MULTILINE) {
3308 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3310 es->buffer_limit = BUFLIMIT_MULTI;
3313 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3315 es->buffer_limit = BUFLIMIT_SINGLE;
3320 /*********************************************************************
3324 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3325 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3326 * margin according to the textmetrics of the current font.
3328 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3329 * of the char's width as the margin, but this is not how Windows handles this.
3330 * For all other fonts Windows sets the margins to zero.
3333 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3334 INT left, INT right)
3337 INT default_left_margin = 0; /* in pixels */
3338 INT default_right_margin = 0; /* in pixels */
3340 /* Set the default margins depending on the font */
3341 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3342 HDC dc = GetDC(es->hwndSelf);
3343 HFONT old_font = SelectObject(dc, es->font);
3344 GetTextMetricsW(dc, &tm);
3345 /* The default margins are only non zero for TrueType or Vector fonts */
3346 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3347 /* This must be calculated more exactly! But how? */
3348 default_left_margin = tm.tmAveCharWidth / 3;
3349 default_right_margin = tm.tmAveCharWidth / 3;
3351 SelectObject(dc, old_font);
3352 ReleaseDC(es->hwndSelf, dc);
3355 if (action & EC_LEFTMARGIN) {
3356 if (left != EC_USEFONTINFO)
3357 es->left_margin = left;
3359 es->left_margin = default_left_margin;
3362 if (action & EC_RIGHTMARGIN) {
3363 if (right != EC_USEFONTINFO)
3364 es->right_margin = right;
3366 es->right_margin = default_right_margin;
3368 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3372 /*********************************************************************
3374 * EM_SETPASSWORDCHAR
3377 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3381 if (es->style & ES_MULTILINE)
3384 if (es->password_char == c)
3387 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3388 es->password_char = c;
3390 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3391 es->style |= ES_PASSWORD;
3393 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3394 es->style &= ~ES_PASSWORD;
3396 EDIT_UpdateText(es, NULL, TRUE);
3400 /*********************************************************************
3404 * note: unlike the specs say: the order of start and end
3405 * _is_ preserved in Windows. (i.e. start can be > end)
3406 * In other words: this handler is OK
3409 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3411 UINT old_start = es->selection_start;
3412 UINT old_end = es->selection_end;
3413 UINT len = strlenW(es->text);
3415 if (start == (UINT)-1) {
3416 start = es->selection_end;
3417 end = es->selection_end;
3419 start = min(start, len);
3420 end = min(end, len);
3422 es->selection_start = start;
3423 es->selection_end = end;
3425 es->flags |= EF_AFTER_WRAP;
3427 es->flags &= ~EF_AFTER_WRAP;
3428 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3429 ORDER_UINT(start, end);
3430 ORDER_UINT(end, old_end);
3431 ORDER_UINT(start, old_start);
3432 ORDER_UINT(old_start, old_end);
3433 if (end != old_start)
3437 * ORDER_UINT32(end, old_start);
3438 * EDIT_InvalidateText(es, start, end);
3439 * EDIT_InvalidateText(es, old_start, old_end);
3440 * in place of the following if statement.
3442 if (old_start > end )
3444 EDIT_InvalidateText(es, start, end);
3445 EDIT_InvalidateText(es, old_start, old_end);
3449 EDIT_InvalidateText(es, start, old_start);
3450 EDIT_InvalidateText(es, end, old_end);
3453 else EDIT_InvalidateText(es, start, old_end);
3457 /*********************************************************************
3462 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3464 if (!(es->style & ES_MULTILINE))
3467 HeapFree(GetProcessHeap(), 0, es->tabs);
3468 es->tabs_count = count;
3472 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3473 memcpy(es->tabs, tabs, count * sizeof(INT));
3479 /*********************************************************************
3484 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3486 if (!(es->style & ES_MULTILINE))
3489 HeapFree(GetProcessHeap(), 0, es->tabs);
3490 es->tabs_count = count;
3495 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3496 for (i = 0 ; i < count ; i++)
3497 es->tabs[i] = *tabs++;
3503 /*********************************************************************
3505 * EM_SETWORDBREAKPROC
3508 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam)
3510 if (es->word_break_proc == (void *)lParam)
3513 es->word_break_proc = (void *)lParam;
3514 es->word_break_proc16 = NULL;
3516 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3517 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3518 EDIT_UpdateText(es, NULL, TRUE);
3523 /*********************************************************************
3525 * EM_SETWORDBREAKPROC16
3528 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3530 if (es->word_break_proc16 == wbp)
3533 es->word_break_proc = NULL;
3534 es->word_break_proc16 = wbp;
3535 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3536 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3537 EDIT_UpdateText(es, NULL, TRUE);
3542 /*********************************************************************
3547 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3552 /* As per MSDN spec, for a single-line edit control,
3553 the return value is always TRUE */
3554 if( es->style & ES_READONLY )
3555 return !(es->style & ES_MULTILINE);
3557 ulength = strlenW(es->undo_text);
3559 return !(es->style & ES_MULTILINE);
3561 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3563 strcpyW(utext, es->undo_text);
3565 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3566 es->undo_insert_count, debugstr_w(utext));
3568 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3569 EDIT_EM_EmptyUndoBuffer(es);
3570 EDIT_EM_ReplaceSel(es, TRUE, utext, FALSE, TRUE);
3571 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3572 /* send the notification after the selection start and end are set */
3573 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3574 EDIT_EM_ScrollCaret(es);
3575 HeapFree(GetProcessHeap(), 0, utext);
3577 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3578 es->undo_insert_count, debugstr_w(es->undo_text));
3583 /*********************************************************************
3588 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3592 /* Protect read-only edit control from modification */
3593 if(es->style & ES_READONLY)
3596 control = GetKeyState(VK_CONTROL) & 0x8000;
3600 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3601 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3604 if (es->style & ES_MULTILINE) {
3605 if (es->style & ES_READONLY) {
3606 EDIT_MoveHome(es, FALSE);
3607 EDIT_MoveDown_ML(es, FALSE);
3609 static const WCHAR cr_lfW[] = {'\r','\n',0};
3610 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3615 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3617 static const WCHAR tabW[] = {'\t',0};
3618 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3622 if (!(es->style & ES_READONLY) && !control) {
3623 if (es->selection_start != es->selection_end)
3626 /* delete character left of caret */
3627 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3628 EDIT_MoveBackward(es, TRUE);
3634 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3637 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3640 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3644 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3648 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3655 /*********************************************************************
3660 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3662 if (code || control)
3682 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3683 EDIT_EM_ScrollCaret(es);
3686 ERR("unknown menu item, please report\n");
3692 /*********************************************************************
3696 * Note: the resource files resource/sysres_??.rc cannot define a
3697 * single popup menu. Hence we use a (dummy) menubar
3698 * containing the single popup menu as its first item.
3700 * FIXME: the message identifiers have been chosen arbitrarily,
3701 * hence we use MF_BYPOSITION.
3702 * We might as well use the "real" values (anybody knows ?)
3703 * The menu definition is in resources/sysres_??.rc.
3704 * Once these are OK, we better use MF_BYCOMMAND here
3705 * (as we do in EDIT_WM_Command()).
3708 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3710 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3711 HMENU popup = GetSubMenu(menu, 0);
3712 UINT start = es->selection_start;
3713 UINT end = es->selection_end;
3715 ORDER_UINT(start, end);
3718 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3720 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3722 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3724 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3726 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3728 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3730 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3735 /*********************************************************************
3740 static void EDIT_WM_Copy(EDITSTATE *es)
3742 INT s = min(es->selection_start, es->selection_end);
3743 INT e = max(es->selection_start, es->selection_end);
3749 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3750 dst = GlobalLock(hdst);
3751 strncpyW(dst, es->text + s, e - s);
3752 dst[e - s] = 0; /* ensure 0 termination */
3753 TRACE("%s\n", debugstr_w(dst));
3755 OpenClipboard(es->hwndSelf);
3757 SetClipboardData(CF_UNICODETEXT, hdst);
3762 /*********************************************************************
3767 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3769 TRACE("%s\n", debugstr_w(name));
3771 * To initialize some final structure members, we call some helper
3772 * functions. However, since the EDITSTATE is not consistent (i.e.
3773 * not fully initialized), we should be very careful which
3774 * functions can be called, and in what order.
3776 EDIT_WM_SetFont(es, 0, FALSE);
3777 EDIT_EM_EmptyUndoBuffer(es);
3779 if (name && *name) {
3780 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3781 /* if we insert text to the editline, the text scrolls out
3782 * of the window, as the caret is placed after the insert
3783 * pos normally; thus we reset es->selection... to 0 and
3786 es->selection_start = es->selection_end = 0;
3787 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3788 * Messages are only to be sent when the USER does something to
3789 * change the contents. So I am removing this EN_CHANGE
3791 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3793 EDIT_EM_ScrollCaret(es);
3795 /* force scroll info update */
3796 EDIT_UpdateScrollInfo(es);
3797 /* The rule seems to return 1 here for success */
3798 /* Power Builder masked edit controls will crash */
3800 /* FIXME: is that in all cases so ? */
3805 /*********************************************************************
3810 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3815 while (LocalUnlock(es->hloc32W)) ;
3816 LocalFree(es->hloc32W);
3819 while (LocalUnlock(es->hloc32A)) ;
3820 LocalFree(es->hloc32A);
3823 HINSTANCE16 hInstance = GetWindowWord( es->hwndSelf, GWL_HINSTANCE );
3824 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3825 LOCAL_Free(hInstance, es->hloc16);
3828 pc = es->first_line_def;
3832 HeapFree(GetProcessHeap(), 0, pc);
3836 SetWindowLongW( es->hwndSelf, 0, 0 );
3837 HeapFree(GetProcessHeap(), 0, es);
3843 /*********************************************************************
3848 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3853 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
3854 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3856 GetClientRect(es->hwndSelf, &rc);
3857 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3858 GetClipBox(dc, &rc);
3860 * FIXME: specs say that we should UnrealizeObject() the brush,
3861 * but the specs of UnrealizeObject() say that we shouldn't
3862 * unrealize a stock object. The default brush that
3863 * DefWndProc() returns is ... a stock object.
3865 FillRect(dc, &rc, brush);
3870 /*********************************************************************
3875 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3877 if(!count) return 0;
3881 LPWSTR textW = (LPWSTR)lParam;
3882 lstrcpynW(textW, es->text, count);
3883 return strlenW(textW);
3887 LPSTR textA = (LPSTR)lParam;
3888 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3889 textA[count - 1] = 0; /* ensure 0 termination */
3890 return strlen(textA);
3894 /*********************************************************************
3899 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3904 if (!(es->style & ES_MULTILINE))
3907 if (!(es->style & ES_AUTOHSCROLL))
3911 fw = es->format_rect.right - es->format_rect.left;
3914 TRACE("SB_LINELEFT\n");
3916 dx = -es->char_width;
3919 TRACE("SB_LINERIGHT\n");
3920 if (es->x_offset < es->text_width)
3921 dx = es->char_width;
3924 TRACE("SB_PAGELEFT\n");
3926 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3929 TRACE("SB_PAGERIGHT\n");
3930 if (es->x_offset < es->text_width)
3931 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3939 TRACE("SB_RIGHT\n");
3940 if (es->x_offset < es->text_width)
3941 dx = es->text_width - es->x_offset;
3944 TRACE("SB_THUMBTRACK %d\n", pos);
3945 es->flags |= EF_HSCROLL_TRACK;
3946 if(es->style & WS_HSCROLL)
3947 dx = pos - es->x_offset;
3952 if(pos < 0 || pos > 100) return 0;
3953 /* Assume default scroll range 0-100 */
3954 fw = es->format_rect.right - es->format_rect.left;
3955 new_x = pos * (es->text_width - fw) / 100;
3956 dx = es->text_width ? (new_x - es->x_offset) : 0;
3959 case SB_THUMBPOSITION:
3960 TRACE("SB_THUMBPOSITION %d\n", pos);
3961 es->flags &= ~EF_HSCROLL_TRACK;
3962 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3963 dx = pos - es->x_offset;
3968 if(pos < 0 || pos > 100) return 0;
3969 /* Assume default scroll range 0-100 */
3970 fw = es->format_rect.right - es->format_rect.left;
3971 new_x = pos * (es->text_width - fw) / 100;
3972 dx = es->text_width ? (new_x - es->x_offset) : 0;
3975 /* force scroll info update */
3976 EDIT_UpdateScrollInfo(es);
3977 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
3981 TRACE("SB_ENDSCROLL\n");
3984 * FIXME : the next two are undocumented !
3985 * Are we doing the right thing ?
3986 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3987 * although it's also a regular control message.
3989 case EM_GETTHUMB: /* this one is used by NT notepad */
3993 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3994 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3997 /* Assume default scroll range 0-100 */
3998 INT fw = es->format_rect.right - es->format_rect.left;
3999 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4001 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4004 case EM_LINESCROLL16:
4005 TRACE("EM_LINESCROLL16\n");
4010 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4016 INT fw = es->format_rect.right - es->format_rect.left;
4017 /* check if we are going to move too far */
4018 if(es->x_offset + dx + fw > es->text_width)
4019 dx = es->text_width - fw - es->x_offset;
4021 EDIT_EM_LineScroll_internal(es, dx, 0);
4027 /*********************************************************************
4032 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4034 HWND hLBox = es->hwndListBox;
4042 hCombo = GetParent(es->hwndSelf);
4046 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4048 if (key == VK_UP || key == VK_DOWN)
4050 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4053 if (msg == WM_KEYDOWN || nEUI)
4054 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4060 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4062 /* make sure ComboLBox pops up */
4063 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4068 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4071 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4073 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4075 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4080 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4086 /*********************************************************************
4090 * Handling of special keys that don't produce a WM_CHAR
4091 * (i.e. non-printable keys) & Backspace & Delete
4094 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4099 if (GetKeyState(VK_MENU) & 0x8000)
4102 shift = GetKeyState(VK_SHIFT) & 0x8000;
4103 control = GetKeyState(VK_CONTROL) & 0x8000;
4108 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4113 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4114 EDIT_MoveUp_ML(es, shift);
4117 EDIT_MoveWordBackward(es, shift);
4119 EDIT_MoveBackward(es, shift);
4122 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4126 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4127 EDIT_MoveDown_ML(es, shift);
4129 EDIT_MoveWordForward(es, shift);
4131 EDIT_MoveForward(es, shift);
4134 EDIT_MoveHome(es, shift);
4137 EDIT_MoveEnd(es, shift);
4140 if (es->style & ES_MULTILINE)
4141 EDIT_MovePageUp_ML(es, shift);
4143 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4146 if (es->style & ES_MULTILINE)
4147 EDIT_MovePageDown_ML(es, shift);
4149 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4152 if (!(es->style & ES_READONLY) && !(shift && control)) {
4153 if (es->selection_start != es->selection_end) {
4160 /* delete character left of caret */
4161 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4162 EDIT_MoveBackward(es, TRUE);
4164 } else if (control) {
4165 /* delete to end of line */
4166 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4167 EDIT_MoveEnd(es, TRUE);
4170 /* delete character right of caret */
4171 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4172 EDIT_MoveForward(es, TRUE);
4180 if (!(es->style & ES_READONLY))
4186 /* If the edit doesn't want the return send a message to the default object */
4187 if(!(es->style & ES_WANTRETURN))
4189 HWND hwndParent = GetParent(es->hwndSelf);
4190 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4191 if (HIWORD(dw) == DC_HASDEFID)
4193 SendMessageW( hwndParent, WM_COMMAND,
4194 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4195 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4204 /*********************************************************************
4209 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4211 es->flags &= ~EF_FOCUSED;
4213 if(!(es->style & ES_NOHIDESEL))
4214 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4215 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4220 /*********************************************************************
4224 * The caret position has been set on the WM_LBUTTONDOWN message
4227 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4230 INT e = es->selection_end;
4235 if (!(es->flags & EF_FOCUSED))
4238 l = EDIT_EM_LineFromChar(es, e);
4239 li = EDIT_EM_LineIndex(es, l);
4240 ll = EDIT_EM_LineLength(es, e);
4241 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4242 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4243 EDIT_EM_SetSel(es, s, e, FALSE);
4244 EDIT_EM_ScrollCaret(es);
4249 /*********************************************************************
4254 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4259 SetFocus(es->hwndSelf);
4260 if (!(es->flags & EF_FOCUSED))
4263 es->bCaptureState = TRUE;
4264 SetCapture(es->hwndSelf);
4265 EDIT_ConfinePoint(es, &x, &y);
4266 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4267 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4268 EDIT_EM_ScrollCaret(es);
4269 es->region_posx = es->region_posy = 0;
4270 SetTimer(es->hwndSelf, 0, 100, NULL);
4275 /*********************************************************************
4280 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4282 if (es->bCaptureState) {
4283 KillTimer(es->hwndSelf, 0);
4284 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4286 es->bCaptureState = FALSE;
4291 /*********************************************************************
4296 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4298 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4303 /*********************************************************************
4308 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4314 if (GetCapture() != es->hwndSelf)
4318 * FIXME: gotta do some scrolling if outside client
4319 * area. Maybe reset the timer ?
4322 EDIT_ConfinePoint(es, &x, &y);
4323 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4324 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4325 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4326 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4327 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4332 /*********************************************************************
4336 * See also EDIT_WM_StyleChanged
4338 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4343 TRACE("Creating %s edit control, style = %08lx\n",
4344 unicode ? "Unicode" : "ANSI", lpcs->style);
4346 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4348 SetWindowLongW( hwnd, 0, (LONG)es );
4351 * Note: since the EDITSTATE has not been fully initialized yet,
4352 * we can't use any API calls that may send
4353 * WM_XXX messages before WM_NCCREATE is completed.
4356 es->is_unicode = unicode;
4357 es->style = lpcs->style;
4359 es->bEnableState = !(es->style & WS_DISABLED);
4361 es->hwndSelf = hwnd;
4362 /* Save parent, which will be notified by EN_* messages */
4363 es->hwndParent = lpcs->hwndParent;
4365 if (es->style & ES_COMBO)
4366 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4368 /* Number overrides lowercase overrides uppercase (at least it
4369 * does in Win95). However I'll bet that ES_NUMBER would be
4370 * invalid under Win 3.1.
4372 if (es->style & ES_NUMBER) {
4373 ; /* do not override the ES_NUMBER */
4374 } else if (es->style & ES_LOWERCASE) {
4375 es->style &= ~ES_UPPERCASE;
4377 if (es->style & ES_MULTILINE) {
4378 es->buffer_limit = BUFLIMIT_MULTI;
4379 if (es->style & WS_VSCROLL)
4380 es->style |= ES_AUTOVSCROLL;
4381 if (es->style & WS_HSCROLL)
4382 es->style |= ES_AUTOHSCROLL;
4383 es->style &= ~ES_PASSWORD;
4384 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4385 /* Confirmed - RIGHT overrides CENTER */
4386 if (es->style & ES_RIGHT)
4387 es->style &= ~ES_CENTER;
4388 es->style &= ~WS_HSCROLL;
4389 es->style &= ~ES_AUTOHSCROLL;
4392 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4393 es->style |= ES_AUTOVSCROLL;
4395 es->buffer_limit = BUFLIMIT_SINGLE;
4396 es->style &= ~ES_CENTER;
4397 es->style &= ~ES_RIGHT;
4398 es->style &= ~WS_HSCROLL;
4399 es->style &= ~WS_VSCROLL;
4400 es->style &= ~ES_AUTOVSCROLL;
4401 es->style &= ~ES_WANTRETURN;
4402 if (es->style & ES_PASSWORD)
4403 es->password_char = '*';
4405 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4406 es->style |= ES_AUTOHSCROLL;
4409 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4410 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4412 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4414 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4416 es->undo_buffer_size = es->buffer_size;
4418 if (es->style & ES_MULTILINE)
4419 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4424 * In Win95 look and feel, the WS_BORDER style is replaced by the
4425 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4426 * control a non client area. Not always. This coordinates in some
4427 * way with the window creation code in dialog.c When making
4428 * modifications please ensure that the code still works for edit
4429 * controls created directly with style 0x50800000, exStyle 0 (
4430 * which should have a single pixel border)
4432 es->style &= ~WS_BORDER;
4437 /*********************************************************************
4442 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam)
4451 BOOL rev = es->bEnableState &&
4452 ((es->flags & EF_FOCUSED) ||
4453 (es->style & ES_NOHIDESEL));
4455 dc = BeginPaint(es->hwndSelf, &ps);
4458 if(es->style & WS_BORDER) {
4459 GetClientRect(es->hwndSelf, &rc);
4460 if(es->style & ES_MULTILINE) {
4461 if(es->style & WS_HSCROLL) rc.bottom++;
4462 if(es->style & WS_VSCROLL) rc.right++;
4464 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4466 IntersectClipRect(dc, es->format_rect.left,
4467 es->format_rect.top,
4468 es->format_rect.right,
4469 es->format_rect.bottom);
4470 if (es->style & ES_MULTILINE) {
4471 GetClientRect(es->hwndSelf, &rc);
4472 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4475 old_font = SelectObject(dc, es->font);
4476 EDIT_NotifyCtlColor(es, dc);
4478 if (!es->bEnableState)
4479 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4480 GetClipBox(dc, &rcRgn);
4481 if (es->style & ES_MULTILINE) {
4482 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4483 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4484 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4485 if (IntersectRect(&rc, &rcRgn, &rcLine))
4486 EDIT_PaintLine(es, dc, i, rev);
4489 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4490 if (IntersectRect(&rc, &rcRgn, &rcLine))
4491 EDIT_PaintLine(es, dc, 0, rev);
4494 SelectObject(dc, old_font);
4497 EndPaint(es->hwndSelf, &ps);
4501 /*********************************************************************
4506 static void EDIT_WM_Paste(EDITSTATE *es)
4511 /* Protect read-only edit control from modification */
4512 if(es->style & ES_READONLY)
4515 OpenClipboard(es->hwndSelf);
4516 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4517 src = (LPWSTR)GlobalLock(hsrc);
4518 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4525 /*********************************************************************
4530 static void EDIT_WM_SetFocus(EDITSTATE *es)
4532 es->flags |= EF_FOCUSED;
4533 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4534 EDIT_SetCaretPos(es, es->selection_end,
4535 es->flags & EF_AFTER_WRAP);
4536 if(!(es->style & ES_NOHIDESEL))
4537 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4538 ShowCaret(es->hwndSelf);
4539 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4543 /*********************************************************************
4547 * With Win95 look the margins are set to default font value unless
4548 * the system font (font == 0) is being set, in which case they are left
4552 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4560 dc = GetDC(es->hwndSelf);
4562 old_font = SelectObject(dc, font);
4563 GetTextMetricsW(dc, &tm);
4564 es->line_height = tm.tmHeight;
4565 es->char_width = tm.tmAveCharWidth;
4567 SelectObject(dc, old_font);
4568 ReleaseDC(es->hwndSelf, dc);
4569 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4570 EC_USEFONTINFO, EC_USEFONTINFO);
4572 /* Force the recalculation of the format rect for each font change */
4573 GetClientRect(es->hwndSelf, &r);
4574 EDIT_SetRectNP(es, &r);
4576 if (es->style & ES_MULTILINE)
4577 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4579 EDIT_CalcLineWidth_SL(es);
4582 EDIT_UpdateText(es, NULL, TRUE);
4583 if (es->flags & EF_FOCUSED) {
4585 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4586 EDIT_SetCaretPos(es, es->selection_end,
4587 es->flags & EF_AFTER_WRAP);
4588 ShowCaret(es->hwndSelf);
4593 /*********************************************************************
4598 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4599 * The modified flag is reset. No notifications are sent.
4601 * For single-line controls, reception of WM_SETTEXT triggers:
4602 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4605 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode)
4610 text = (LPWSTR)lParam;
4613 LPCSTR textA = (LPCSTR)lParam;
4614 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4615 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4616 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4619 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4621 TRACE("%s\n", debugstr_w(text));
4622 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4624 HeapFree(GetProcessHeap(), 0, text);
4626 static const WCHAR empty_stringW[] = {0};
4628 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4631 es->flags &= ~EF_MODIFIED;
4632 EDIT_EM_SetSel(es, 0, 0, FALSE);
4633 /* Send the notification after the selection start and end have been set
4634 * edit control doesn't send notification on WM_SETTEXT
4635 * if it is multiline, or it is part of combobox
4637 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4639 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4640 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4642 EDIT_EM_ScrollCaret(es);
4646 /*********************************************************************
4651 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4653 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4655 TRACE("width = %d, height = %d\n", width, height);
4656 SetRect(&rc, 0, 0, width, height);
4657 EDIT_SetRectNP(es, &rc);
4658 EDIT_UpdateText(es, NULL, TRUE);
4663 /*********************************************************************
4667 * This message is sent by SetWindowLong on having changed either the Style
4668 * or the extended style.
4670 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4672 * See also EDIT_WM_NCCreate
4674 * It appears that the Windows version of the edit control allows the style
4675 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4676 * style variable which will generally be different. In this function we
4677 * update the internal style based on what changed in the externally visible
4680 * Much of this content as based upon the MSDN, especially:
4681 * Platform SDK Documentation -> User Interface Services ->
4682 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4683 * Edit Control Styles
4685 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4687 if (GWL_STYLE == which) {
4688 DWORD style_change_mask;
4690 /* Only a subset of changes can be applied after the control
4693 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4695 if (es->style & ES_MULTILINE)
4696 style_change_mask |= ES_WANTRETURN;
4698 new_style = style->styleNew & style_change_mask;
4700 /* Number overrides lowercase overrides uppercase (at least it
4701 * does in Win95). However I'll bet that ES_NUMBER would be
4702 * invalid under Win 3.1.
4704 if (new_style & ES_NUMBER) {
4705 ; /* do not override the ES_NUMBER */
4706 } else if (new_style & ES_LOWERCASE) {
4707 new_style &= ~ES_UPPERCASE;
4710 es->style = (es->style & ~style_change_mask) | new_style;
4711 } else if (GWL_EXSTYLE == which) {
4712 ; /* FIXME - what is needed here */
4714 WARN ("Invalid style change %d\n",which);
4720 /*********************************************************************
4725 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4727 if ((key == VK_BACK) && (key_data & 0x2000)) {
4728 if (EDIT_EM_CanUndo(es))
4731 } else if (key == VK_UP || key == VK_DOWN) {
4732 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4735 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4739 /*********************************************************************
4744 static void EDIT_WM_Timer(EDITSTATE *es)
4746 if (es->region_posx < 0) {
4747 EDIT_MoveBackward(es, TRUE);
4748 } else if (es->region_posx > 0) {
4749 EDIT_MoveForward(es, TRUE);
4752 * FIXME: gotta do some vertical scrolling here, like
4753 * EDIT_EM_LineScroll(hwnd, 0, 1);
4757 /*********************************************************************
4762 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4766 if (!(es->style & ES_MULTILINE))
4769 if (!(es->style & ES_AUTOVSCROLL))
4778 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4779 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4780 (action == SB_PAGEUP ? "SB_PAGEUP" :
4782 EDIT_EM_Scroll(es, action);
4789 TRACE("SB_BOTTOM\n");
4790 dy = es->line_count - 1 - es->y_offset;
4793 TRACE("SB_THUMBTRACK %d\n", pos);
4794 es->flags |= EF_VSCROLL_TRACK;
4795 if(es->style & WS_VSCROLL)
4796 dy = pos - es->y_offset;
4799 /* Assume default scroll range 0-100 */
4802 if(pos < 0 || pos > 100) return 0;
4803 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4804 new_y = pos * (es->line_count - vlc) / 100;
4805 dy = es->line_count ? (new_y - es->y_offset) : 0;
4806 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4807 es->line_count, es->y_offset, pos, dy);
4810 case SB_THUMBPOSITION:
4811 TRACE("SB_THUMBPOSITION %d\n", pos);
4812 es->flags &= ~EF_VSCROLL_TRACK;
4813 if(es->style & WS_VSCROLL)
4814 dy = pos - es->y_offset;
4817 /* Assume default scroll range 0-100 */
4820 if(pos < 0 || pos > 100) return 0;
4821 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4822 new_y = pos * (es->line_count - vlc) / 100;
4823 dy = es->line_count ? (new_y - es->y_offset) : 0;
4824 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4825 es->line_count, es->y_offset, pos, dy);
4829 /* force scroll info update */
4830 EDIT_UpdateScrollInfo(es);
4831 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4835 TRACE("SB_ENDSCROLL\n");
4838 * FIXME : the next two are undocumented !
4839 * Are we doing the right thing ?
4840 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4841 * although it's also a regular control message.
4843 case EM_GETTHUMB: /* this one is used by NT notepad */
4847 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4848 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4851 /* Assume default scroll range 0-100 */
4852 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4853 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4855 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4858 case EM_LINESCROLL16:
4859 TRACE("EM_LINESCROLL16 %d\n", pos);
4864 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4869 EDIT_EM_LineScroll(es, 0, dy);
4873 /*********************************************************************
4878 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4880 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4881 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4885 /*********************************************************************
4890 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4892 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4893 InvalidateRect(es->hwndSelf, rc, bErase);