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.
48 #include "wine/winbase16.h"
49 #include "wine/winuser16.h"
50 #include "wine/unicode.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(edit);
57 WINE_DECLARE_DEBUG_CHANNEL(combo);
58 WINE_DECLARE_DEBUG_CHANNEL(relay);
60 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
61 FIXME: BTW, new specs say 65535 (do you dare ???) */
62 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
63 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
64 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
65 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
68 * extra flags for EDITSTATE.flags field
70 #define EF_MODIFIED 0x0001 /* text has been modified */
71 #define EF_FOCUSED 0x0002 /* we have input focus */
72 #define EF_UPDATE 0x0004 /* notify parent of changed state */
73 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
74 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
75 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
76 wrapped line, instead of in front of the next character */
77 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
81 END_0 = 0, /* line ends with terminating '\0' character */
82 END_WRAP, /* line is wrapped */
83 END_HARD, /* line ends with a hard return '\r\n' */
84 END_SOFT, /* line ends with a soft return '\r\r\n' */
85 END_RICH /* line ends with a single '\n' */
88 typedef struct tagLINEDEF {
89 INT length; /* bruto length of a line in bytes */
90 INT net_length; /* netto length of a line in visible characters */
92 INT width; /* width of the line in pixels */
93 INT index; /* line index into the buffer */
94 struct tagLINEDEF *next;
99 BOOL is_unicode; /* how the control was created */
100 LPWSTR text; /* the actual contents of the control */
101 UINT buffer_size; /* the size of the buffer in characters */
102 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
103 HFONT font; /* NULL means standard system font */
104 INT x_offset; /* scroll offset for multi lines this is in pixels
105 for single lines it's in characters */
106 INT line_height; /* height of a screen line in pixels */
107 INT char_width; /* average character width in pixels */
108 DWORD style; /* sane version of wnd->dwStyle */
109 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
110 INT undo_insert_count; /* number of characters inserted in sequence */
111 UINT undo_position; /* character index of the insertion and deletion */
112 LPWSTR undo_text; /* deleted text */
113 UINT undo_buffer_size; /* size of the deleted text buffer */
114 INT selection_start; /* == selection_end if no selection */
115 INT selection_end; /* == current caret position */
116 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
117 INT left_margin; /* in pixels */
118 INT right_margin; /* in pixels */
120 INT text_width; /* width of the widest line in pixels for multi line controls
121 and just line width for single line controls */
122 INT region_posx; /* Position of cursor relative to region: */
123 INT region_posy; /* -1: to left, 0: within, 1: to right */
124 EDITWORDBREAKPROC16 word_break_proc16;
125 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
126 INT line_count; /* number of lines */
127 INT y_offset; /* scroll offset in number of lines */
128 BOOL bCaptureState; /* flag indicating whether mouse was captured */
129 BOOL bEnableState; /* flag keeping the enable state */
130 HWND hwndSelf; /* the our window handle */
131 HWND hwndParent; /* Handle of parent for sending EN_* messages.
132 Even if parent will change, EN_* messages
133 should be sent to the first parent. */
134 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
136 * only for multi line controls
138 INT lock_count; /* amount of re-entries in the EditWndProc */
141 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
142 HLOCAL hloc32W; /* our unicode local memory block */
143 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
145 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
150 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
151 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
153 /* used for disabled or read-only edit control */
154 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
156 { /* Notify parent which has created this edit control */ \
157 TRACE("notification " str " sent to hwnd=%08x\n", es->hwndParent); \
158 SendMessageW(es->hwndParent, WM_COMMAND, \
159 MAKEWPARAM(GetWindowLongW((es->hwndSelf),GWL_ID), wNotifyCode), \
160 (LPARAM)(es->hwndSelf)); \
163 /*********************************************************************
170 * These functions have trivial implementations
171 * We still like to call them internally
172 * "static inline" makes them more like macro's
174 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
175 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
176 static inline void EDIT_WM_Clear(EDITSTATE *es);
177 static inline void EDIT_WM_Cut(EDITSTATE *es);
180 * Helper functions only valid for one type of control
182 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
183 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
184 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
185 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
186 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
187 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
188 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
190 * Helper functions valid for both single line _and_ multi line controls
192 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
193 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
194 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
195 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
196 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
197 static void EDIT_LockBuffer(EDITSTATE *es);
198 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
199 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
200 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
201 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
202 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
203 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
204 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
205 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
206 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
207 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
208 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
209 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
210 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
211 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
212 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
214 * EM_XXX message handlers
216 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
217 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
218 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
219 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
220 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
221 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
222 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
223 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
224 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
225 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
226 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
227 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
228 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
229 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update);
230 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
231 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
232 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
233 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
234 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
235 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
236 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
237 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
238 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
239 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
240 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam);
241 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
242 static BOOL EDIT_EM_Undo(EDITSTATE *es);
244 * WM_XXX message handlers
246 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
247 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
248 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
249 static void EDIT_WM_Copy(EDITSTATE *es);
250 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
251 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
252 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
253 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
254 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
255 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
256 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
257 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
258 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
259 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
260 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
261 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
262 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
263 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam);
264 static void EDIT_WM_Paste(EDITSTATE *es);
265 static void EDIT_WM_SetFocus(EDITSTATE *es);
266 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
267 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode);
268 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
269 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
270 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
271 static void EDIT_WM_Timer(EDITSTATE *es);
272 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
273 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
274 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
276 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
277 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
279 /*********************************************************************
280 * edit class descriptor
282 const struct builtin_class_descr EDIT_builtin_class =
285 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
286 EditWndProcA, /* procA */
287 EditWndProcW, /* procW */
288 sizeof(EDITSTATE *), /* extra */
289 IDC_IBEAMA, /* cursor */
294 /*********************************************************************
299 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
301 return (es->undo_insert_count || strlenW(es->undo_text));
305 /*********************************************************************
310 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
312 es->undo_insert_count = 0;
313 *es->undo_text = '\0';
317 /*********************************************************************
322 static inline void EDIT_WM_Clear(EDITSTATE *es)
324 static const WCHAR empty_stringW[] = {0};
326 /* Protect read-only edit control from modification */
327 if(es->style & ES_READONLY)
330 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE);
334 /*********************************************************************
339 static inline void EDIT_WM_Cut(EDITSTATE *es)
346 /**********************************************************************
349 * Returns the window version in case Wine emulates a later version
350 * of windows then the application expects.
352 * In a number of cases when windows runs an application that was
353 * designed for an earlier windows version, windows reverts
354 * to "old" behaviour of that earlier version.
356 * An example is a disabled edit control that needs to be painted.
357 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
358 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
359 * applications with an expected version 0f 4.0 or higher.
362 static DWORD get_app_version(void)
364 static DWORD version;
367 DWORD dwEmulatedVersion;
369 DWORD dwProcVersion = GetProcessVersion(0);
371 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
372 GetVersionExW( &info );
373 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
374 /* FIXME: this may not be 100% correct; see discussion on the
375 * wine developer list in Nov 1999 */
376 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
382 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
386 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
387 msg = WM_CTLCOLORSTATIC;
389 msg = WM_CTLCOLOREDIT;
391 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
392 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
395 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
398 return DefWindowProcW(hwnd, msg, wParam, lParam);
400 return DefWindowProcA(hwnd, msg, wParam, lParam);
403 /*********************************************************************
407 * The messages are in the order of the actual integer values
408 * (which can be found in include/windows.h)
409 * Wherever possible the 16 bit versions are converted to
410 * the 32 bit ones, so that we can 'fall through' to the
411 * helper functions. These are mostly 32 bit (with a few
412 * exceptions, clearly indicated by a '16' extension to their
416 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
417 WPARAM wParam, LPARAM lParam, BOOL unicode )
419 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
422 TRACE("hwnd=%x msg=%x wparam=%x lparam=%lx\n", hwnd, msg, wParam, lParam);
424 if (!es && msg != WM_NCCREATE)
425 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
426 else if (msg == WM_NCCREATE)
427 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
428 else if (msg == WM_DESTROY)
429 return EDIT_WM_Destroy(es);
432 if (es) EDIT_LockBuffer(es);
440 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
444 if (SLOWORD(lParam) == -1)
445 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
447 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
449 EDIT_EM_ScrollCaret(es);
453 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
454 EDIT_EM_ScrollCaret(es);
460 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
464 CopyRect((LPRECT)lParam, &es->format_rect);
468 if ((es->style & ES_MULTILINE) && lParam) {
470 CONV_RECT16TO32(MapSL(lParam), &rc);
471 EDIT_SetRectNP(es, &rc);
472 EDIT_UpdateText(es, NULL, TRUE);
476 if ((es->style & ES_MULTILINE) && lParam) {
477 EDIT_SetRectNP(es, (LPRECT)lParam);
478 EDIT_UpdateText(es, NULL, TRUE);
483 if ((es->style & ES_MULTILINE) && lParam) {
485 CONV_RECT16TO32(MapSL(lParam), &rc);
486 EDIT_SetRectNP(es, &rc);
490 if ((es->style & ES_MULTILINE) && lParam)
491 EDIT_SetRectNP(es, (LPRECT)lParam);
496 result = EDIT_EM_Scroll(es, (INT)wParam);
499 case EM_LINESCROLL16:
500 wParam = (WPARAM)(INT)SHIWORD(lParam);
501 lParam = (LPARAM)(INT)SLOWORD(lParam);
504 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
507 case EM_SCROLLCARET16:
509 EDIT_EM_ScrollCaret(es);
515 result = ((es->flags & EF_MODIFIED) != 0);
521 es->flags |= EF_MODIFIED;
523 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
526 case EM_GETLINECOUNT16:
527 case EM_GETLINECOUNT:
528 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
532 if ((INT16)wParam == -1)
536 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
540 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
543 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
547 result = (LRESULT)EDIT_EM_GetHandle16(es);
550 result = (LRESULT)EDIT_EM_GetHandle(es);
555 result = EDIT_EM_GetThumb(es);
558 /* these messages missing from specs */
567 FIXME("undocumented message 0x%x, please report\n", msg);
568 result = DefWindowProcW(hwnd, msg, wParam, lParam);
571 case EM_LINELENGTH16:
573 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
576 case EM_REPLACESEL16:
577 lParam = (LPARAM)MapSL(lParam);
578 unicode = FALSE; /* 16-bit message is always ascii */
585 textW = (LPWSTR)lParam;
588 LPSTR textA = (LPSTR)lParam;
589 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
590 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
591 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
594 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE);
598 HeapFree(GetProcessHeap(), 0, textW);
603 lParam = (LPARAM)MapSL(lParam);
604 unicode = FALSE; /* 16-bit message is always ascii */
607 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
611 case EM_SETLIMITTEXT:
612 EDIT_EM_SetLimitText(es, (INT)wParam);
617 result = (LRESULT)EDIT_EM_CanUndo(es);
623 result = (LRESULT)EDIT_EM_Undo(es);
628 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
631 case EM_LINEFROMCHAR16:
632 case EM_LINEFROMCHAR:
633 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
636 case EM_SETTABSTOPS16:
637 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
640 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
643 case EM_SETPASSWORDCHAR16:
644 unicode = FALSE; /* 16-bit message is always ascii */
646 case EM_SETPASSWORDCHAR:
651 charW = (WCHAR)wParam;
655 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
658 EDIT_EM_SetPasswordChar(es, charW);
662 case EM_EMPTYUNDOBUFFER16:
663 case EM_EMPTYUNDOBUFFER:
664 EDIT_EM_EmptyUndoBuffer(es);
667 case EM_GETFIRSTVISIBLELINE16:
668 result = es->y_offset;
670 case EM_GETFIRSTVISIBLELINE:
671 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
674 case EM_SETREADONLY16:
677 SetWindowLongW( hwnd, GWL_STYLE,
678 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
679 es->style |= ES_READONLY;
681 SetWindowLongW( hwnd, GWL_STYLE,
682 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
683 es->style &= ~ES_READONLY;
688 case EM_SETWORDBREAKPROC16:
689 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
691 case EM_SETWORDBREAKPROC:
692 EDIT_EM_SetWordBreakProc(es, lParam);
695 case EM_GETWORDBREAKPROC16:
696 result = (LRESULT)es->word_break_proc16;
698 case EM_GETWORDBREAKPROC:
699 result = (LRESULT)es->word_break_proc;
702 case EM_GETPASSWORDCHAR16:
703 unicode = FALSE; /* 16-bit message is always ascii */
705 case EM_GETPASSWORDCHAR:
708 result = es->password_char;
711 WCHAR charW = es->password_char;
713 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
719 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
722 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
726 result = MAKELONG(es->left_margin, es->right_margin);
729 case EM_GETLIMITTEXT:
730 result = es->buffer_limit;
734 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
738 result = EDIT_EM_CharFromPos(es, SLOWORD(lParam), SHIWORD(lParam));
741 /* End of the EM_ messages which were in numerical order; what order
742 * are these in? vaguely alphabetical?
746 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
748 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
750 int vk = (int)((LPMSG)lParam)->wParam;
752 if (vk == VK_RETURN && (GetWindowLongW( hwnd, GWL_STYLE ) & ES_WANTRETURN))
754 result |= DLGC_WANTMESSAGE;
756 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
758 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
759 result |= DLGC_WANTMESSAGE;
773 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
776 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
778 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
779 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
782 EDIT_WM_Char(es, charW);
791 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
795 EDIT_WM_ContextMenu(es, SLOWORD(lParam), SHIWORD(lParam));
804 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
807 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
811 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
812 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
813 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
815 result = EDIT_WM_Create(es, nameW);
817 HeapFree(GetProcessHeap(), 0, nameW);
826 es->bEnableState = (BOOL) wParam;
827 EDIT_UpdateText(es, NULL, TRUE);
831 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
835 result = (LRESULT)es->font;
839 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
842 case WM_GETTEXTLENGTH:
843 if (unicode) result = strlenW(es->text);
844 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
845 NULL, 0, NULL, NULL );
849 result = EDIT_WM_HScroll(es, LOWORD(wParam), SHIWORD(wParam));
853 result = EDIT_WM_KeyDown(es, (INT)wParam);
857 result = EDIT_WM_KillFocus(es);
860 case WM_LBUTTONDBLCLK:
861 result = EDIT_WM_LButtonDblClk(es);
865 result = EDIT_WM_LButtonDown(es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
869 result = EDIT_WM_LButtonUp(es);
873 result = EDIT_WM_MButtonDown(es);
876 case WM_MOUSEACTIVATE:
878 * FIXME: maybe DefWindowProc() screws up, but it seems that
879 * modeless dialog boxes need this. If we don't do this, the focus
880 * will _not_ be set by DefWindowProc() for edit controls in a
881 * modeless dialog box ???
884 result = MA_ACTIVATE;
888 result = EDIT_WM_MouseMove(es, SLOWORD(lParam), SHIWORD(lParam));
892 EDIT_WM_Paint(es, wParam);
900 EDIT_WM_SetFocus(es);
904 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
908 /* FIXME: actually set an internal flag and behave accordingly */
912 EDIT_WM_SetText(es, lParam, unicode);
917 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
920 case WM_STYLECHANGED:
921 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
924 case WM_STYLECHANGING:
925 result = 0; /* See EDIT_WM_StyleChanged */
929 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
937 result = EDIT_WM_VScroll(es, LOWORD(wParam), SHIWORD(wParam));
942 int gcWheelDelta = 0;
943 UINT pulScrollLines = 3;
944 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
946 if (wParam & (MK_SHIFT | MK_CONTROL)) {
947 result = DefWindowProcW(hwnd, msg, wParam, lParam);
950 gcWheelDelta -= SHIWORD(wParam);
951 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
953 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
954 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
955 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
960 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
964 if (es) EDIT_UnlockBuffer(es, FALSE);
969 /*********************************************************************
971 * EditWndProcW (USER32.@)
973 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
975 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
978 /*********************************************************************
980 * EditWndProc (USER32.@)
982 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
984 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
987 /*********************************************************************
989 * EDIT_BuildLineDefs_ML
991 * Build linked list of text lines.
992 * Lines can end with '\0' (last line), a character (if it is wrapped),
993 * a soft return '\r\r\n' or a hard return '\r\n'
996 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1000 LPWSTR current_position, cp;
1002 LINEDEF *current_line;
1003 LINEDEF *previous_line;
1004 LINEDEF *start_line;
1005 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1006 INT line_count = es->line_count;
1007 INT orig_net_length;
1010 if (istart == iend && delta == 0)
1013 dc = GetDC(es->hwndSelf);
1015 old_font = SelectObject(dc, es->font);
1017 previous_line = NULL;
1018 current_line = es->first_line_def;
1020 /* Find starting line. istart must lie inside an existing line or
1021 * at the end of buffer */
1023 if (istart < current_line->index + current_line->length ||
1024 current_line->ending == END_0)
1027 previous_line = current_line;
1028 current_line = current_line->next;
1030 } while (current_line);
1032 if (!current_line) /* Error occurred start is not inside previous buffer */
1034 FIXME(" modification occurred outside buffer\n");
1038 /* Remember start of modifications in order to calculate update region */
1039 nstart_line = line_index;
1040 nstart_index = current_line->index;
1042 /* We must start to reformat from the previous line since the modifications
1043 * may have caused the line to wrap upwards. */
1044 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1047 current_line = previous_line;
1049 start_line = current_line;
1051 fw = es->format_rect.right - es->format_rect.left;
1052 current_position = es->text + current_line->index;
1054 if (current_line != start_line)
1056 if (!current_line || current_line->index + delta > current_position - es->text)
1058 /* The buffer has been expanded, create a new line and
1059 insert it into the link list */
1060 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1061 new_line->next = previous_line->next;
1062 previous_line->next = new_line;
1063 current_line = new_line;
1066 else if (current_line->index + delta < current_position - es->text)
1068 /* The previous line merged with this line so we delete this extra entry */
1069 previous_line->next = current_line->next;
1070 HeapFree(GetProcessHeap(), 0, current_line);
1071 current_line = previous_line->next;
1075 else /* current_line->index + delta == current_position */
1077 if (current_position - es->text > iend)
1078 break; /* We reached end of line modifications */
1079 /* else recalulate this line */
1083 current_line->index = current_position - es->text;
1084 orig_net_length = current_line->net_length;
1086 /* Find end of line */
1087 cp = current_position;
1089 if (*cp == '\n') break;
1090 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1095 /* Mark type of line termination */
1097 current_line->ending = END_0;
1098 current_line->net_length = strlenW(current_position);
1099 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1100 current_line->ending = END_SOFT;
1101 current_line->net_length = cp - current_position - 1;
1102 } else if (*cp == '\n') {
1103 current_line->ending = END_RICH;
1104 current_line->net_length = cp - current_position;
1106 current_line->ending = END_HARD;
1107 current_line->net_length = cp - current_position;
1110 /* Calculate line width */
1111 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1112 current_position, current_line->net_length,
1113 es->tabs_count, es->tabs));
1115 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1116 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1121 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1122 prev + 1, current_line->net_length, WB_RIGHT);
1123 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1124 current_position, next, es->tabs_count, es->tabs));
1125 } while (current_line->width <= fw);
1126 if (!prev) { /* Didn't find a line break so force a break */
1131 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1132 current_position, next, es->tabs_count, es->tabs));
1133 } while (current_line->width <= fw);
1138 /* If the first line we are calculating, wrapped before istart, we must
1139 * adjust istart in order for this to be reflected in the update region. */
1140 if (current_line->index == nstart_index && istart > current_line->index + prev)
1141 istart = current_line->index + prev;
1142 /* else if we are updating the previous line before the first line we
1143 * are re-calculating and it expanded */
1144 else if (current_line == start_line &&
1145 current_line->index != nstart_index && orig_net_length < prev)
1147 /* Line expanded due to an upwards line wrap so we must partially include
1148 * previous line in update region */
1149 nstart_line = line_index;
1150 nstart_index = current_line->index;
1151 istart = current_line->index + orig_net_length;
1154 current_line->net_length = prev;
1155 current_line->ending = END_WRAP;
1156 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1157 current_line->net_length, es->tabs_count, es->tabs));
1161 /* Adjust length to include line termination */
1162 switch (current_line->ending) {
1164 current_line->length = current_line->net_length + 3;
1167 current_line->length = current_line->net_length + 1;
1170 current_line->length = current_line->net_length + 2;
1174 current_line->length = current_line->net_length;
1177 es->text_width = max(es->text_width, current_line->width);
1178 current_position += current_line->length;
1179 previous_line = current_line;
1180 current_line = current_line->next;
1182 } while (previous_line->ending != END_0);
1184 /* Finish adjusting line indexes by delta or remove hanging lines */
1185 if (previous_line->ending == END_0)
1187 LINEDEF *pnext = NULL;
1189 previous_line->next = NULL;
1190 while (current_line)
1192 pnext = current_line->next;
1193 HeapFree(GetProcessHeap(), 0, current_line);
1194 current_line = pnext;
1200 while (current_line)
1202 current_line->index += delta;
1203 current_line = current_line->next;
1207 /* Calculate rest of modification rectangle */
1212 * We calculate two rectangles. One for the first line which may have
1213 * an indent with respect to the format rect. The other is a format-width
1214 * rectangle that spans the rest of the lines that changed or moved.
1216 rc.top = es->format_rect.top + nstart_line * es->line_height -
1217 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1218 rc.bottom = rc.top + es->line_height;
1219 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1220 es->text + nstart_index, istart - nstart_index,
1221 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1222 rc.right = es->format_rect.right;
1223 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1226 rc.left = es->format_rect.left;
1227 rc.right = es->format_rect.right;
1229 * If lines were added or removed we must re-paint the remainder of the
1230 * lines since the remaining lines were either shifted up or down.
1232 if (line_count < es->line_count) /* We added lines */
1233 rc.bottom = es->line_count * es->line_height;
1234 else if (line_count > es->line_count) /* We removed lines */
1235 rc.bottom = line_count * es->line_height;
1237 rc.bottom = line_index * es->line_height;
1238 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1239 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1240 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1241 DeleteObject(tmphrgn);
1245 SelectObject(dc, old_font);
1247 ReleaseDC(es->hwndSelf, dc);
1250 /*********************************************************************
1252 * EDIT_CalcLineWidth_SL
1255 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1257 es->text_width = SLOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1260 /*********************************************************************
1262 * EDIT_CallWordBreakProc
1264 * Call appropriate WordBreakProc (internal or external).
1266 * Note: The "start" argument should always be an index referring
1267 * to es->text. The actual wordbreak proc might be
1268 * 16 bit, so we can't always pass any 32 bit LPSTR.
1269 * Hence we assume that es->text is the buffer that holds
1270 * the string under examination (we can decide this for ourselves).
1273 /* ### start build ### */
1274 extern WORD CALLBACK EDIT_CallTo16_word_lwww(EDITWORDBREAKPROC16,SEGPTR,WORD,WORD,WORD);
1275 /* ### stop build ### */
1276 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1278 INT ret, iWndsLocks;
1280 /* To avoid any deadlocks, all the locks on the window structures
1281 must be suspended before the control is passed to the application */
1282 iWndsLocks = WIN_SuspendWndsLock();
1284 if (es->word_break_proc16) {
1289 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1290 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1291 segptr = K32WOWGlobalLock16(hglob16);
1292 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1293 ret = (INT)EDIT_CallTo16_word_lwww(es->word_break_proc16,
1294 segptr, index, countA, action);
1295 GlobalUnlock16(hglob16);
1296 GlobalFree16(hglob16);
1298 else if (es->word_break_proc)
1302 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1304 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1305 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1306 ret = wbpW(es->text + start, index, count, action);
1310 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1314 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1315 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1316 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1317 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1318 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1319 ret = wbpA(textA, index, countA, action);
1320 HeapFree(GetProcessHeap(), 0, textA);
1324 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1326 WIN_RestoreWndsLock(iWndsLocks);
1331 /*********************************************************************
1335 * Beware: This is not the function called on EM_CHARFROMPOS
1336 * The position _can_ be outside the formatting / client
1338 * The return value is only the character index
1341 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1347 if (es->style & ES_MULTILINE) {
1348 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1350 LINEDEF *line_def = es->first_line_def;
1352 while ((line > 0) && line_def->next) {
1353 line_index += line_def->length;
1354 line_def = line_def->next;
1357 x += es->x_offset - es->format_rect.left;
1358 if (x >= line_def->width) {
1360 *after_wrap = (line_def->ending == END_WRAP);
1361 return line_index + line_def->net_length;
1365 *after_wrap = FALSE;
1368 dc = GetDC(es->hwndSelf);
1370 old_font = SelectObject(dc, es->font);
1371 low = line_index + 1;
1372 high = line_index + line_def->net_length + 1;
1373 while (low < high - 1)
1375 INT mid = (low + high) / 2;
1376 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1382 *after_wrap = ((index == line_index + line_def->net_length) &&
1383 (line_def->ending == END_WRAP));
1388 *after_wrap = FALSE;
1389 x -= es->format_rect.left;
1391 return es->x_offset;
1392 text = EDIT_GetPasswordPointer_SL(es);
1393 dc = GetDC(es->hwndSelf);
1395 old_font = SelectObject(dc, es->font);
1399 INT high = es->x_offset;
1400 while (low < high - 1)
1402 INT mid = (low + high) / 2;
1403 GetTextExtentPoint32W( dc, text + mid,
1404 es->x_offset - mid, &size );
1405 if (size.cx > -x) low = mid;
1412 INT low = es->x_offset;
1413 INT high = strlenW(es->text) + 1;
1414 while (low < high - 1)
1416 INT mid = (low + high) / 2;
1417 GetTextExtentPoint32W( dc, text + es->x_offset,
1418 mid - es->x_offset, &size );
1419 if (size.cx > x) high = mid;
1424 if (es->style & ES_PASSWORD)
1425 HeapFree(GetProcessHeap(), 0, text);
1428 SelectObject(dc, old_font);
1429 ReleaseDC(es->hwndSelf, dc);
1434 /*********************************************************************
1438 * adjusts the point to be within the formatting rectangle
1439 * (so CharFromPos returns the nearest _visible_ character)
1442 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1444 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1445 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1449 /*********************************************************************
1453 * Calculates the bounding rectangle for a line from a starting
1454 * column to an ending column.
1457 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1459 INT line_index = EDIT_EM_LineIndex(es, line);
1461 if (es->style & ES_MULTILINE)
1462 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1464 rc->top = es->format_rect.top;
1465 rc->bottom = rc->top + es->line_height;
1466 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1467 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1471 /*********************************************************************
1473 * EDIT_GetPasswordPointer_SL
1475 * note: caller should free the (optionally) allocated buffer
1478 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1480 if (es->style & ES_PASSWORD) {
1481 INT len = strlenW(es->text);
1482 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1484 while(len) text[--len] = es->password_char;
1491 /*********************************************************************
1495 * This acts as a LOCAL_Lock(), but it locks only once. This way
1496 * you can call it whenever you like, without unlocking.
1498 * Initially the edit control allocates a HLOCAL32 buffer
1499 * (32 bit linear memory handler). However, 16 bit application
1500 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1501 * handler). From that moment on we have to keep using this 16 bit memory
1502 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1503 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1507 static void EDIT_LockBuffer(EDITSTATE *es)
1509 HINSTANCE hInstance = (HINSTANCE)GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
1513 BOOL _16bit = FALSE;
1519 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1520 textA = LocalLock(es->hloc32A);
1521 countA = strlen(textA) + 1;
1525 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1526 textA = LOCAL_Lock(hInstance, es->hloc16);
1527 countA = strlen(textA) + 1;
1532 ERR("no buffer ... please report\n");
1539 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1540 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1541 if(countW_new > es->buffer_size + 1)
1543 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1544 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1545 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1548 es->hloc32W = hloc32W_new;
1549 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1550 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1553 WARN("FAILED! Will synchronize partially\n");
1557 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1558 es->text = LocalLock(es->hloc32W);
1562 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1564 LOCAL_Unlock(hInstance, es->hloc16);
1566 LocalUnlock(es->hloc32A);
1573 /*********************************************************************
1575 * EDIT_SL_InvalidateText
1577 * Called from EDIT_InvalidateText().
1578 * Does the job for single-line controls only.
1581 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1586 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1587 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1588 EDIT_UpdateText(es, &rc, TRUE);
1592 /*********************************************************************
1594 * EDIT_ML_InvalidateText
1596 * Called from EDIT_InvalidateText().
1597 * Does the job for multi-line controls only.
1600 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1602 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1603 INT sl = EDIT_EM_LineFromChar(es, start);
1604 INT el = EDIT_EM_LineFromChar(es, end);
1613 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1616 sc = start - EDIT_EM_LineIndex(es, sl);
1617 ec = end - EDIT_EM_LineIndex(es, el);
1618 if (sl < es->y_offset) {
1622 if (el > es->y_offset + vlc) {
1623 el = es->y_offset + vlc;
1624 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1626 GetClientRect(es->hwndSelf, &rc1);
1627 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1629 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1630 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1631 EDIT_UpdateText(es, &rcUpdate, TRUE);
1633 EDIT_GetLineRect(es, sl, sc,
1634 EDIT_EM_LineLength(es,
1635 EDIT_EM_LineIndex(es, sl)),
1637 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1638 EDIT_UpdateText(es, &rcUpdate, TRUE);
1639 for (l = sl + 1 ; l < el ; l++) {
1640 EDIT_GetLineRect(es, l, 0,
1641 EDIT_EM_LineLength(es,
1642 EDIT_EM_LineIndex(es, l)),
1644 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1645 EDIT_UpdateText(es, &rcUpdate, TRUE);
1647 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1648 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1649 EDIT_UpdateText(es, &rcUpdate, TRUE);
1654 /*********************************************************************
1656 * EDIT_InvalidateText
1658 * Invalidate the text from offset start upto, but not including,
1659 * offset end. Useful for (re)painting the selection.
1660 * Regions outside the linewidth are not invalidated.
1661 * end == -1 means end == TextLength.
1662 * start and end need not be ordered.
1665 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1671 end = strlenW(es->text);
1679 if (es->style & ES_MULTILINE)
1680 EDIT_ML_InvalidateText(es, start, end);
1682 EDIT_SL_InvalidateText(es, start, end);
1686 /*********************************************************************
1690 * Try to fit size + 1 characters in the buffer. Constrain to limits.
1693 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1697 if (size <= es->buffer_size)
1699 if ((es->buffer_limit > 0) && (size > es->buffer_limit)) {
1700 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
1703 if ((es->buffer_limit > 0) && (size > es->buffer_limit))
1704 size = es->buffer_limit;
1706 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1708 /* Force edit to unlock it's buffer. es->text now NULL */
1709 EDIT_UnlockBuffer(es, TRUE);
1712 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1713 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1714 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32W, hNew32W);
1715 es->hloc32W = hNew32W;
1716 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1720 EDIT_LockBuffer(es);
1722 if (es->buffer_size < size) {
1723 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1724 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1727 TRACE("We now have %d+1\n", es->buffer_size);
1733 /*********************************************************************
1737 * Try to fit size + 1 bytes in the undo buffer.
1740 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1744 if (size <= es->undo_buffer_size)
1747 TRACE("trying to ReAlloc to %d+1\n", size);
1749 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1750 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1751 es->undo_buffer_size = alloc_size/sizeof(WCHAR);
1756 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1762 /*********************************************************************
1767 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1769 INT e = es->selection_end;
1773 if ((es->style & ES_MULTILINE) && e &&
1774 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1776 if (e && (es->text[e - 1] == '\r'))
1780 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1781 EDIT_EM_ScrollCaret(es);
1785 /*********************************************************************
1789 * Only for multi line controls
1790 * Move the caret one line down, on a column with the nearest
1791 * x coordinate on the screen (might be a different column).
1794 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1796 INT s = es->selection_start;
1797 INT e = es->selection_end;
1798 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1799 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1800 INT x = SLOWORD(pos);
1801 INT y = SHIWORD(pos);
1803 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1806 EDIT_EM_SetSel(es, s, e, after_wrap);
1807 EDIT_EM_ScrollCaret(es);
1811 /*********************************************************************
1816 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1818 BOOL after_wrap = FALSE;
1821 /* Pass a high value in x to make sure of receiving the end of the line */
1822 if (es->style & ES_MULTILINE)
1823 e = EDIT_CharFromPos(es, 0x3fffffff,
1824 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1826 e = strlenW(es->text);
1827 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1828 EDIT_EM_ScrollCaret(es);
1832 /*********************************************************************
1837 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1839 INT e = es->selection_end;
1843 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1844 if (es->text[e] == '\n')
1846 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1850 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1851 EDIT_EM_ScrollCaret(es);
1855 /*********************************************************************
1859 * Home key: move to beginning of line.
1862 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1866 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1867 if (es->style & ES_MULTILINE)
1868 e = EDIT_CharFromPos(es, -es->x_offset,
1869 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1872 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1873 EDIT_EM_ScrollCaret(es);
1877 /*********************************************************************
1879 * EDIT_MovePageDown_ML
1881 * Only for multi line controls
1882 * Move the caret one page down, on a column with the nearest
1883 * x coordinate on the screen (might be a different column).
1886 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1888 INT s = es->selection_start;
1889 INT e = es->selection_end;
1890 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1891 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1892 INT x = SLOWORD(pos);
1893 INT y = SHIWORD(pos);
1895 e = EDIT_CharFromPos(es, x,
1896 y + (es->format_rect.bottom - es->format_rect.top),
1900 EDIT_EM_SetSel(es, s, e, after_wrap);
1901 EDIT_EM_ScrollCaret(es);
1905 /*********************************************************************
1907 * EDIT_MovePageUp_ML
1909 * Only for multi line controls
1910 * Move the caret one page up, on a column with the nearest
1911 * x coordinate on the screen (might be a different column).
1914 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1916 INT s = es->selection_start;
1917 INT e = es->selection_end;
1918 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1919 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1920 INT x = SLOWORD(pos);
1921 INT y = SHIWORD(pos);
1923 e = EDIT_CharFromPos(es, x,
1924 y - (es->format_rect.bottom - es->format_rect.top),
1928 EDIT_EM_SetSel(es, s, e, after_wrap);
1929 EDIT_EM_ScrollCaret(es);
1933 /*********************************************************************
1937 * Only for multi line controls
1938 * Move the caret one line up, on a column with the nearest
1939 * x coordinate on the screen (might be a different column).
1942 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1944 INT s = es->selection_start;
1945 INT e = es->selection_end;
1946 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1947 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1948 INT x = SLOWORD(pos);
1949 INT y = SHIWORD(pos);
1951 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1954 EDIT_EM_SetSel(es, s, e, after_wrap);
1955 EDIT_EM_ScrollCaret(es);
1959 /*********************************************************************
1961 * EDIT_MoveWordBackward
1964 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1966 INT s = es->selection_start;
1967 INT e = es->selection_end;
1972 l = EDIT_EM_LineFromChar(es, e);
1973 ll = EDIT_EM_LineLength(es, e);
1974 li = EDIT_EM_LineIndex(es, l);
1977 li = EDIT_EM_LineIndex(es, l - 1);
1978 e = li + EDIT_EM_LineLength(es, li);
1981 e = li + (INT)EDIT_CallWordBreakProc(es,
1982 li, e - li, ll, WB_LEFT);
1986 EDIT_EM_SetSel(es, s, e, FALSE);
1987 EDIT_EM_ScrollCaret(es);
1991 /*********************************************************************
1993 * EDIT_MoveWordForward
1996 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
1998 INT s = es->selection_start;
1999 INT e = es->selection_end;
2004 l = EDIT_EM_LineFromChar(es, e);
2005 ll = EDIT_EM_LineLength(es, e);
2006 li = EDIT_EM_LineIndex(es, l);
2008 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2009 e = EDIT_EM_LineIndex(es, l + 1);
2011 e = li + EDIT_CallWordBreakProc(es,
2012 li, e - li + 1, ll, WB_RIGHT);
2016 EDIT_EM_SetSel(es, s, e, FALSE);
2017 EDIT_EM_ScrollCaret(es);
2021 /*********************************************************************
2026 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2028 INT s = es->selection_start;
2029 INT e = es->selection_end;
2036 if (es->style & ES_MULTILINE) {
2037 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2038 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2043 TRACE("line=%d\n", line);
2045 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2048 li = EDIT_EM_LineIndex(es, line);
2049 ll = EDIT_EM_LineLength(es, li);
2050 s = min(es->selection_start, es->selection_end);
2051 e = max(es->selection_start, es->selection_end);
2052 s = min(li + ll, max(li, s));
2053 e = min(li + ll, max(li, e));
2054 if (rev && (s != e) &&
2055 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2056 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2057 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2058 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2060 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2064 /*********************************************************************
2069 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2080 BkMode = GetBkMode(dc);
2081 BkColor = GetBkColor(dc);
2082 TextColor = GetTextColor(dc);
2084 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2085 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2086 SetBkMode( dc, OPAQUE);
2088 li = EDIT_EM_LineIndex(es, line);
2089 if (es->style & ES_MULTILINE) {
2090 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2091 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2093 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2094 TextOutW(dc, x, y, text + li + col, count);
2095 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2097 if (es->style & ES_PASSWORD)
2098 HeapFree(GetProcessHeap(), 0, text);
2101 SetBkColor(dc, BkColor);
2102 SetTextColor(dc, TextColor);
2103 SetBkMode( dc, BkMode);
2109 /*********************************************************************
2114 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2117 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2118 SetCaretPos(SLOWORD(res), SHIWORD(res));
2122 /*********************************************************************
2126 * note: this is not (exactly) the handler called on EM_SETRECTNP
2127 * it is also used to set the rect of a single line control
2130 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2132 CopyRect(&es->format_rect, rc);
2133 if (es->style & WS_BORDER) {
2134 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2135 if(TWEAK_WineLook == WIN31_LOOK)
2137 es->format_rect.left += bw;
2138 es->format_rect.top += bw;
2139 es->format_rect.right -= bw;
2140 es->format_rect.bottom -= bw;
2142 es->format_rect.left += es->left_margin;
2143 es->format_rect.right -= es->right_margin;
2144 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2145 if (es->style & ES_MULTILINE)
2147 INT fw, vlc, max_x_offset, max_y_offset;
2149 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2150 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2152 /* correct es->x_offset */
2153 fw = es->format_rect.right - es->format_rect.left;
2154 max_x_offset = es->text_width - fw;
2155 if(max_x_offset < 0) max_x_offset = 0;
2156 if(es->x_offset > max_x_offset)
2157 es->x_offset = max_x_offset;
2159 /* correct es->y_offset */
2160 max_y_offset = es->line_count - vlc;
2161 if(max_y_offset < 0) max_y_offset = 0;
2162 if(es->y_offset > max_y_offset)
2163 es->y_offset = max_y_offset;
2165 /* force scroll info update */
2166 EDIT_UpdateScrollInfo(es);
2169 /* Windows doesn't care to fix text placement for SL controls */
2170 es->format_rect.bottom = es->format_rect.top + es->line_height;
2172 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2173 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, (HRGN)0);
2177 /*********************************************************************
2182 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2184 HINSTANCE hInstance = (HINSTANCE)GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2186 /* Edit window might be already destroyed */
2187 if(!IsWindow(es->hwndSelf))
2189 WARN("edit hwnd %04x already destroyed\n", es->hwndSelf);
2193 if (!es->lock_count) {
2194 ERR("lock_count == 0 ... please report\n");
2198 ERR("es->text == 0 ... please report\n");
2202 if (force || (es->lock_count == 1)) {
2205 BOOL _16bit = FALSE;
2207 UINT countW = strlenW(es->text) + 1;
2211 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2212 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2213 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2214 countA = LocalSize(es->hloc32A);
2215 if(countA_new > countA)
2218 UINT alloc_size = ROUND_TO_GROW(countA_new);
2219 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2220 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2223 es->hloc32A = hloc32A_new;
2224 countA = LocalSize(hloc32A_new);
2225 TRACE("Real new size %d bytes\n", countA);
2228 WARN("FAILED! Will synchronize partially\n");
2230 textA = LocalLock(es->hloc32A);
2234 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2235 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2236 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2237 countA = LOCAL_Size(hInstance, es->hloc16);
2238 if(countA_new > countA)
2240 HLOCAL16 hloc16_new;
2241 UINT alloc_size = ROUND_TO_GROW(countA_new);
2242 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2243 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2246 es->hloc16 = hloc16_new;
2247 countA = LOCAL_Size(hInstance, hloc16_new);
2248 TRACE("Real new size %d bytes\n", countA);
2251 WARN("FAILED! Will synchronize partially\n");
2253 textA = LOCAL_Lock(hInstance, es->hloc16);
2259 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2261 LOCAL_Unlock(hInstance, es->hloc16);
2263 LocalUnlock(es->hloc32A);
2266 LocalUnlock(es->hloc32W);
2270 ERR("no buffer ... please report\n");
2278 /*********************************************************************
2280 * EDIT_UpdateScrollInfo
2283 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2285 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2288 si.cbSize = sizeof(SCROLLINFO);
2289 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2291 si.nMax = es->line_count - 1;
2292 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2293 si.nPos = es->y_offset;
2294 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2295 si.nMin, si.nMax, si.nPage, si.nPos);
2296 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2299 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2302 si.cbSize = sizeof(SCROLLINFO);
2303 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2305 si.nMax = es->text_width - 1;
2306 si.nPage = es->format_rect.right - es->format_rect.left;
2307 si.nPos = es->x_offset;
2308 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2309 si.nMin, si.nMax, si.nPage, si.nPos);
2310 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2314 /*********************************************************************
2316 * EDIT_WordBreakProc
2318 * Find the beginning of words.
2319 * Note: unlike the specs for a WordBreakProc, this function only
2320 * allows to be called without linebreaks between s[0] upto
2321 * s[count - 1]. Remember it is only called
2322 * internally, so we can decide this for ourselves.
2325 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2329 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2339 if (s[index] == ' ') {
2340 while (index && (s[index] == ' '))
2343 while (index && (s[index] != ' '))
2345 if (s[index] == ' ')
2349 while (index && (s[index] != ' '))
2351 if (s[index] == ' ')
2361 if (s[index] == ' ')
2362 while ((index < count) && (s[index] == ' ')) index++;
2364 while (s[index] && (s[index] != ' ') && (index < count))
2366 while ((s[index] == ' ') && (index < count)) index++;
2370 case WB_ISDELIMITER:
2371 ret = (s[index] == ' ');
2374 ERR("unknown action code, please report !\n");
2381 /*********************************************************************
2385 * returns line number (not index) in high-order word of result.
2386 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2387 * to Richedit, not to the edit control. Original documentation is valid.
2388 * FIXME: do the specs mean to return -1 if outside client area or
2389 * if outside formatting rectangle ???
2392 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2400 GetClientRect(es->hwndSelf, &rc);
2401 if (!PtInRect(&rc, pt))
2404 index = EDIT_CharFromPos(es, x, y, NULL);
2405 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2409 /*********************************************************************
2413 * Enable or disable soft breaks.
2415 * This means: insert or remove the soft linebreak character (\r\r\n).
2416 * Take care to check if the text still fits the buffer after insertion.
2417 * If not, notify with EN_ERRSPACE.
2420 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2422 es->flags &= ~EF_USE_SOFTBRK;
2424 es->flags |= EF_USE_SOFTBRK;
2425 FIXME("soft break enabled, not implemented\n");
2431 /*********************************************************************
2435 * Hopefully this won't fire back at us.
2436 * We always start with a fixed buffer in the local heap.
2437 * Despite of the documentation says that the local heap is used
2438 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2439 * buffer on the local heap.
2442 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2446 if (!(es->style & ES_MULTILINE))
2450 hLocal = es->hloc32W;
2456 UINT countA, alloc_size;
2457 TRACE("Allocating 32-bit ANSI alias buffer\n");
2458 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2459 alloc_size = ROUND_TO_GROW(countA);
2460 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2462 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2465 textA = LocalLock(es->hloc32A);
2466 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2467 LocalUnlock(es->hloc32A);
2469 hLocal = es->hloc32A;
2472 TRACE("Returning %04X, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2477 /*********************************************************************
2481 * Hopefully this won't fire back at us.
2482 * We always start with a buffer in 32 bit linear memory.
2483 * However, with this message a 16 bit application requests
2484 * a handle of 16 bit local heap memory, where it expects to find
2486 * It's a pitty that from this moment on we have to use this
2487 * local heap, because applications may rely on the handle
2490 * In this function we'll try to switch to local heap.
2492 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2494 HINSTANCE hInstance = (HINSTANCE)GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2496 UINT countA, alloc_size;
2498 if (!(es->style & ES_MULTILINE))
2504 if (!LOCAL_HeapSize(hInstance)) {
2505 if (!LocalInit16(hInstance, 0,
2506 GlobalSize16(hInstance))) {
2507 ERR("could not initialize local heap\n");
2510 TRACE("local heap initialized\n");
2513 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2514 alloc_size = ROUND_TO_GROW(countA);
2516 TRACE("Allocating 16-bit ANSI alias buffer\n");
2517 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2518 ERR("could not allocate new 16 bit buffer\n");
2522 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2523 ERR("could not lock new 16 bit buffer\n");
2524 LOCAL_Free(hInstance, es->hloc16);
2529 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2530 LOCAL_Unlock(hInstance, es->hloc16);
2532 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2537 /*********************************************************************
2542 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2545 INT line_len, dst_len;
2548 if (es->style & ES_MULTILINE) {
2549 if (line >= es->line_count)
2553 i = EDIT_EM_LineIndex(es, line);
2555 line_len = EDIT_EM_LineLength(es, i);
2556 dst_len = *(WORD *)lParam;
2559 LPWSTR dst = (LPWSTR)lParam;
2560 if(dst_len <= line_len)
2562 memcpy(dst, src, dst_len * sizeof(WCHAR));
2565 else /* Append 0 if enough space */
2567 memcpy(dst, src, line_len * sizeof(WCHAR));
2574 LPSTR dst = (LPSTR)lParam;
2576 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2577 if(!ret) /* Insufficient buffer size */
2579 if(ret < dst_len) /* Append 0 if enough space */
2586 /*********************************************************************
2591 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2593 UINT s = es->selection_start;
2594 UINT e = es->selection_end;
2601 return MAKELONG(s, e);
2605 /*********************************************************************
2609 * FIXME: is this right ? (or should it be only VSCROLL)
2610 * (and maybe only for edit controls that really have their
2611 * own scrollbars) (and maybe only for multiline controls ?)
2612 * All in all: very poorly documented
2615 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2617 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2618 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2622 /*********************************************************************
2627 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2632 if (!(es->style & ES_MULTILINE))
2634 if (index > (INT)strlenW(es->text))
2635 return es->line_count - 1;
2637 index = min(es->selection_start, es->selection_end);
2640 line_def = es->first_line_def;
2641 index -= line_def->length;
2642 while ((index >= 0) && line_def->next) {
2644 line_def = line_def->next;
2645 index -= line_def->length;
2651 /*********************************************************************
2656 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2661 if (!(es->style & ES_MULTILINE))
2663 if (line >= es->line_count)
2667 line_def = es->first_line_def;
2669 INT index = es->selection_end - line_def->length;
2670 while ((index >= 0) && line_def->next) {
2671 line_index += line_def->length;
2672 line_def = line_def->next;
2673 index -= line_def->length;
2677 line_index += line_def->length;
2678 line_def = line_def->next;
2686 /*********************************************************************
2691 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2695 if (!(es->style & ES_MULTILINE))
2696 return strlenW(es->text);
2699 /* get the number of remaining non-selected chars of selected lines */
2700 INT32 l; /* line number */
2701 INT32 li; /* index of first char in line */
2703 l = EDIT_EM_LineFromChar(es, es->selection_start);
2704 /* # chars before start of selection area */
2705 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2706 l = EDIT_EM_LineFromChar(es, es->selection_end);
2707 /* # chars after end of selection */
2708 li = EDIT_EM_LineIndex(es, l);
2709 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2712 line_def = es->first_line_def;
2713 index -= line_def->length;
2714 while ((index >= 0) && line_def->next) {
2715 line_def = line_def->next;
2716 index -= line_def->length;
2718 return line_def->net_length;
2722 /*********************************************************************
2726 * NOTE: dx is in average character widths, dy - in lines;
2729 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2731 if (!(es->style & ES_MULTILINE))
2734 dx *= es->char_width;
2735 return EDIT_EM_LineScroll_internal(es, dx, dy);
2738 /*********************************************************************
2740 * EDIT_EM_LineScroll_internal
2742 * Version of EDIT_EM_LineScroll for internal use.
2743 * It doesn't refuse if ES_MULTILINE is set and assumes that
2744 * dx is in pixels, dy - in lines.
2747 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2750 INT x_offset_in_pixels;
2752 if (es->style & ES_MULTILINE)
2754 x_offset_in_pixels = es->x_offset;
2759 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2762 if (-dx > x_offset_in_pixels)
2763 dx = -x_offset_in_pixels;
2764 if (dx > es->text_width - x_offset_in_pixels)
2765 dx = es->text_width - x_offset_in_pixels;
2766 nyoff = max(0, es->y_offset + dy);
2767 if (nyoff >= es->line_count)
2768 nyoff = es->line_count - 1;
2769 dy = (es->y_offset - nyoff) * es->line_height;
2774 es->y_offset = nyoff;
2775 if(es->style & ES_MULTILINE)
2778 es->x_offset += dx / es->char_width;
2780 GetClientRect(es->hwndSelf, &rc1);
2781 IntersectRect(&rc, &rc1, &es->format_rect);
2782 ScrollWindowEx(es->hwndSelf, -dx, dy,
2783 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2784 /* force scroll info update */
2785 EDIT_UpdateScrollInfo(es);
2787 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2788 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2789 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2790 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2795 /*********************************************************************
2800 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2802 INT len = strlenW(es->text);
2811 index = min(index, len);
2812 dc = GetDC(es->hwndSelf);
2814 old_font = SelectObject(dc, es->font);
2815 if (es->style & ES_MULTILINE) {
2816 l = EDIT_EM_LineFromChar(es, index);
2817 y = (l - es->y_offset) * es->line_height;
2818 li = EDIT_EM_LineIndex(es, l);
2819 if (after_wrap && (li == index) && l) {
2821 LINEDEF *line_def = es->first_line_def;
2823 line_def = line_def->next;
2826 if (line_def->ending == END_WRAP) {
2828 y -= es->line_height;
2829 li = EDIT_EM_LineIndex(es, l);
2832 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2833 es->tabs_count, es->tabs)) - es->x_offset;
2835 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2836 if (index < es->x_offset) {
2837 GetTextExtentPoint32W(dc, text + index,
2838 es->x_offset - index, &size);
2841 GetTextExtentPoint32W(dc, text + es->x_offset,
2842 index - es->x_offset, &size);
2846 if (es->style & ES_PASSWORD)
2847 HeapFree(GetProcessHeap(), 0, text);
2849 x += es->format_rect.left;
2850 y += es->format_rect.top;
2852 SelectObject(dc, old_font);
2853 ReleaseDC(es->hwndSelf, dc);
2854 return MAKELONG((INT16)x, (INT16)y);
2858 /*********************************************************************
2862 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2865 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
2867 UINT strl = strlenW(lpsz_replace);
2868 UINT tl = strlenW(es->text);
2876 TRACE("%s, can_undo %d, send_update %d\n",
2877 debugstr_w(lpsz_replace), can_undo, send_update);
2879 s = es->selection_start;
2880 e = es->selection_end;
2882 if ((s == e) && !strl)
2887 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2891 /* there is something to be deleted */
2892 TRACE("deleting stuff.\n");
2894 utl = strlenW(es->undo_text);
2895 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2896 /* undo-buffer is extended to the right */
2897 EDIT_MakeUndoFit(es, utl + e - s);
2898 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2899 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2900 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2901 /* undo-buffer is extended to the left */
2902 EDIT_MakeUndoFit(es, utl + e - s);
2903 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2905 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2906 p[i] = (es->text + s)[i];
2907 es->undo_position = s;
2909 /* new undo-buffer */
2910 EDIT_MakeUndoFit(es, e - s);
2911 strncpyW(es->undo_text, es->text + s, e - s + 1);
2912 es->undo_text[e - s] = 0; /* ensure 0 termination */
2913 es->undo_position = s;
2915 /* any deletion makes the old insertion-undo invalid */
2916 es->undo_insert_count = 0;
2918 EDIT_EM_EmptyUndoBuffer(es);
2921 strcpyW(es->text + s, es->text + e);
2924 /* there is an insertion */
2926 if ((s == es->undo_position) ||
2927 ((es->undo_insert_count) &&
2928 (s == es->undo_position + es->undo_insert_count)))
2930 * insertion is new and at delete position or
2931 * an extension to either left or right
2933 es->undo_insert_count += strl;
2935 /* new insertion undo */
2936 es->undo_position = s;
2937 es->undo_insert_count = strl;
2938 /* new insertion makes old delete-buffer invalid */
2939 *es->undo_text = '\0';
2942 EDIT_EM_EmptyUndoBuffer(es);
2945 tl = strlenW(es->text);
2946 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));
2947 for (p = es->text + tl ; p >= es->text + s ; p--)
2949 for (i = 0 , p = es->text + s ; i < strl ; i++)
2950 p[i] = lpsz_replace[i];
2951 if(es->style & ES_UPPERCASE)
2952 CharUpperBuffW(p, strl);
2953 else if(es->style & ES_LOWERCASE)
2954 CharLowerBuffW(p, strl);
2957 if (es->style & ES_MULTILINE)
2959 INT s = min(es->selection_start, es->selection_end);
2961 hrgn = CreateRectRgn(0, 0, 0, 0);
2962 EDIT_BuildLineDefs_ML(es, s, s + strl,
2963 strl - abs(es->selection_end - es->selection_start), hrgn);
2966 EDIT_CalcLineWidth_SL(es);
2968 EDIT_EM_SetSel(es, s, s, FALSE);
2969 es->flags |= EF_MODIFIED;
2970 if (send_update) es->flags |= EF_UPDATE;
2971 EDIT_EM_ScrollCaret(es);
2973 /* force scroll info update */
2974 EDIT_UpdateScrollInfo(es);
2978 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2982 EDIT_UpdateText(es, NULL, TRUE);
2984 if(es->flags & EF_UPDATE)
2986 es->flags &= ~EF_UPDATE;
2987 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
2992 /*********************************************************************
2997 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3001 if (!(es->style & ES_MULTILINE))
3002 return (LRESULT)FALSE;
3012 if (es->y_offset < es->line_count - 1)
3017 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3020 if (es->y_offset < es->line_count - 1)
3021 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3024 return (LRESULT)FALSE;
3027 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3028 /* check if we are going to move too far */
3029 if(es->y_offset + dy > es->line_count - vlc)
3030 dy = es->line_count - vlc - es->y_offset;
3032 /* Notification is done in EDIT_EM_LineScroll */
3034 EDIT_EM_LineScroll(es, 0, dy);
3036 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3040 /*********************************************************************
3045 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3047 if (es->style & ES_MULTILINE) {
3052 INT cw = es->char_width;
3057 l = EDIT_EM_LineFromChar(es, es->selection_end);
3058 li = EDIT_EM_LineIndex(es, l);
3059 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3060 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3061 if (l >= es->y_offset + vlc)
3062 dy = l - vlc + 1 - es->y_offset;
3063 if (l < es->y_offset)
3064 dy = l - es->y_offset;
3065 ww = es->format_rect.right - es->format_rect.left;
3066 if (x < es->format_rect.left)
3067 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3068 if (x > es->format_rect.right)
3069 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3072 /* check if we are going to move too far */
3073 if(es->x_offset + dx + ww > es->text_width)
3074 dx = es->text_width - ww - es->x_offset;
3076 EDIT_EM_LineScroll_internal(es, dx, dy);
3083 if (!(es->style & ES_AUTOHSCROLL))
3086 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3087 format_width = es->format_rect.right - es->format_rect.left;
3088 if (x < es->format_rect.left) {
3089 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3092 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3093 } while ((x < goal) && es->x_offset);
3094 /* FIXME: use ScrollWindow() somehow to improve performance */
3095 EDIT_UpdateText(es, NULL, TRUE);
3096 } else if (x > es->format_rect.right) {
3098 INT len = strlenW(es->text);
3099 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3102 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3103 x_last = SLOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3104 } while ((x > goal) && (x_last > es->format_rect.right));
3105 /* FIXME: use ScrollWindow() somehow to improve performance */
3106 EDIT_UpdateText(es, NULL, TRUE);
3110 if(es->flags & EF_FOCUSED)
3111 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3115 /*********************************************************************
3119 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3122 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3124 HINSTANCE hInstance = (HINSTANCE)GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3126 if (!(es->style & ES_MULTILINE))
3130 WARN("called with NULL handle\n");
3134 EDIT_UnlockBuffer(es, TRUE);
3138 LOCAL_Free(hInstance, es->hloc16);
3139 es->hloc16 = (HLOCAL16)NULL;
3146 LocalFree(es->hloc32A);
3147 es->hloc32A = (HLOCAL)NULL;
3158 countA = LocalSize(hloc);
3159 textA = LocalLock(hloc);
3160 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3161 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3163 ERR("Could not allocate new unicode buffer\n");
3166 textW = LocalLock(hloc32W_new);
3167 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3168 LocalUnlock(hloc32W_new);
3172 LocalFree(es->hloc32W);
3174 es->hloc32W = hloc32W_new;
3178 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3180 EDIT_LockBuffer(es);
3182 es->x_offset = es->y_offset = 0;
3183 es->selection_start = es->selection_end = 0;
3184 EDIT_EM_EmptyUndoBuffer(es);
3185 es->flags &= ~EF_MODIFIED;
3186 es->flags &= ~EF_UPDATE;
3187 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, (HRGN)0);
3188 EDIT_UpdateText(es, NULL, TRUE);
3189 EDIT_EM_ScrollCaret(es);
3190 /* force scroll info update */
3191 EDIT_UpdateScrollInfo(es);
3195 /*********************************************************************
3199 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3202 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3204 HINSTANCE hInstance = (HINSTANCE)GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3210 if (!(es->style & ES_MULTILINE))
3214 WARN("called with NULL handle\n");
3218 EDIT_UnlockBuffer(es, TRUE);
3222 LocalFree(es->hloc32A);
3223 es->hloc32A = (HLOCAL)NULL;
3226 countA = LOCAL_Size(hInstance, hloc);
3227 textA = LOCAL_Lock(hInstance, hloc);
3228 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3229 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3231 ERR("Could not allocate new unicode buffer\n");
3234 textW = LocalLock(hloc32W_new);
3235 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3236 LocalUnlock(hloc32W_new);
3237 LOCAL_Unlock(hInstance, hloc);
3240 LocalFree(es->hloc32W);
3242 es->hloc32W = hloc32W_new;
3245 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3247 EDIT_LockBuffer(es);
3249 es->x_offset = es->y_offset = 0;
3250 es->selection_start = es->selection_end = 0;
3251 EDIT_EM_EmptyUndoBuffer(es);
3252 es->flags &= ~EF_MODIFIED;
3253 es->flags &= ~EF_UPDATE;
3254 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, (HRGN)0);
3255 EDIT_UpdateText(es, NULL, TRUE);
3256 EDIT_EM_ScrollCaret(es);
3257 /* force scroll info update */
3258 EDIT_UpdateScrollInfo(es);
3262 /*********************************************************************
3266 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3267 * However, the windows version is not complied to yet in all of edit.c
3269 * Additionally as the wrapper for RichEdit controls we need larger buffers
3270 * at present -1 will represent nolimit
3272 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3274 if (limit == 0xFFFFFFFF)
3275 es->buffer_limit = -1;
3276 else if (es->style & ES_MULTILINE) {
3278 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3280 es->buffer_limit = BUFLIMIT_MULTI;
3283 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3285 es->buffer_limit = BUFLIMIT_SINGLE;
3290 /*********************************************************************
3294 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3295 * action wParam despite what the docs say. EC_USEFONTINFO means one third
3296 * of the char's width, according to the new docs.
3299 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3300 INT left, INT right)
3302 if (action & EC_LEFTMARGIN) {
3303 if (left != EC_USEFONTINFO)
3304 es->left_margin = left;
3306 es->left_margin = es->char_width / 3;
3309 if (action & EC_RIGHTMARGIN) {
3310 if (right != EC_USEFONTINFO)
3311 es->right_margin = right;
3313 es->right_margin = es->char_width / 3;
3315 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3319 /*********************************************************************
3321 * EM_SETPASSWORDCHAR
3324 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3328 if (es->style & ES_MULTILINE)
3331 if (es->password_char == c)
3334 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3335 es->password_char = c;
3337 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3338 es->style |= ES_PASSWORD;
3340 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3341 es->style &= ~ES_PASSWORD;
3343 EDIT_UpdateText(es, NULL, TRUE);
3347 /*********************************************************************
3351 * note: unlike the specs say: the order of start and end
3352 * _is_ preserved in Windows. (i.e. start can be > end)
3353 * In other words: this handler is OK
3356 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3358 UINT old_start = es->selection_start;
3359 UINT old_end = es->selection_end;
3360 UINT len = strlenW(es->text);
3362 if (start == (UINT)-1) {
3363 start = es->selection_end;
3364 end = es->selection_end;
3366 start = min(start, len);
3367 end = min(end, len);
3369 es->selection_start = start;
3370 es->selection_end = end;
3372 es->flags |= EF_AFTER_WRAP;
3374 es->flags &= ~EF_AFTER_WRAP;
3375 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3376 ORDER_UINT(start, end);
3377 ORDER_UINT(end, old_end);
3378 ORDER_UINT(start, old_start);
3379 ORDER_UINT(old_start, old_end);
3380 if (end != old_start)
3384 * ORDER_UINT32(end, old_start);
3385 * EDIT_InvalidateText(es, start, end);
3386 * EDIT_InvalidateText(es, old_start, old_end);
3387 * in place of the following if statement.
3389 if (old_start > end )
3391 EDIT_InvalidateText(es, start, end);
3392 EDIT_InvalidateText(es, old_start, old_end);
3396 EDIT_InvalidateText(es, start, old_start);
3397 EDIT_InvalidateText(es, end, old_end);
3400 else EDIT_InvalidateText(es, start, old_end);
3404 /*********************************************************************
3409 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3411 if (!(es->style & ES_MULTILINE))
3414 HeapFree(GetProcessHeap(), 0, es->tabs);
3415 es->tabs_count = count;
3419 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3420 memcpy(es->tabs, tabs, count * sizeof(INT));
3426 /*********************************************************************
3431 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3433 if (!(es->style & ES_MULTILINE))
3436 HeapFree(GetProcessHeap(), 0, es->tabs);
3437 es->tabs_count = count;
3442 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3443 for (i = 0 ; i < count ; i++)
3444 es->tabs[i] = *tabs++;
3450 /*********************************************************************
3452 * EM_SETWORDBREAKPROC
3455 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam)
3457 if (es->word_break_proc == (void *)lParam)
3460 es->word_break_proc = (void *)lParam;
3461 es->word_break_proc16 = NULL;
3463 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3464 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, (HRGN)0);
3465 EDIT_UpdateText(es, NULL, TRUE);
3470 /*********************************************************************
3472 * EM_SETWORDBREAKPROC16
3475 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3477 if (es->word_break_proc16 == wbp)
3480 es->word_break_proc = NULL;
3481 es->word_break_proc16 = wbp;
3482 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3483 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, (HRGN)0);
3484 EDIT_UpdateText(es, NULL, TRUE);
3489 /*********************************************************************
3494 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3499 /* Protect read-only edit control from modification */
3500 if(es->style & ES_READONLY)
3503 ulength = strlenW(es->undo_text);
3504 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3506 strcpyW(utext, es->undo_text);
3508 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3509 es->undo_insert_count, debugstr_w(utext));
3511 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3512 EDIT_EM_EmptyUndoBuffer(es);
3513 EDIT_EM_ReplaceSel(es, TRUE, utext, FALSE);
3514 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3515 /* send the notification after the selection start and end are set */
3516 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3517 EDIT_EM_ScrollCaret(es);
3518 HeapFree(GetProcessHeap(), 0, utext);
3520 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3521 es->undo_insert_count, debugstr_w(es->undo_text));
3526 /*********************************************************************
3531 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3535 /* Protect read-only edit control from modification */
3536 if(es->style & ES_READONLY)
3539 control = GetKeyState(VK_CONTROL) & 0x8000;
3543 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3544 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3547 if (es->style & ES_MULTILINE) {
3548 if (es->style & ES_READONLY) {
3549 EDIT_MoveHome(es, FALSE);
3550 EDIT_MoveDown_ML(es, FALSE);
3552 static const WCHAR cr_lfW[] = {'\r','\n',0};
3553 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE);
3558 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3560 static const WCHAR tabW[] = {'\t',0};
3561 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE);
3565 if (!(es->style & ES_READONLY) && !control) {
3566 if (es->selection_start != es->selection_end)
3569 /* delete character left of caret */
3570 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3571 EDIT_MoveBackward(es, TRUE);
3577 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3580 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3583 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3587 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3591 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE);
3598 /*********************************************************************
3603 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3605 if (code || control)
3625 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3626 EDIT_EM_ScrollCaret(es);
3629 ERR("unknown menu item, please report\n");
3635 /*********************************************************************
3639 * Note: the resource files resource/sysres_??.rc cannot define a
3640 * single popup menu. Hence we use a (dummy) menubar
3641 * containing the single popup menu as its first item.
3643 * FIXME: the message identifiers have been chosen arbitrarily,
3644 * hence we use MF_BYPOSITION.
3645 * We might as well use the "real" values (anybody knows ?)
3646 * The menu definition is in resources/sysres_??.rc.
3647 * Once these are OK, we better use MF_BYCOMMAND here
3648 * (as we do in EDIT_WM_Command()).
3651 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3653 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3654 HMENU popup = GetSubMenu(menu, 0);
3655 UINT start = es->selection_start;
3656 UINT end = es->selection_end;
3658 ORDER_UINT(start, end);
3661 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3663 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3665 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3667 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3669 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3671 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3673 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3678 /*********************************************************************
3683 static void EDIT_WM_Copy(EDITSTATE *es)
3685 INT s = min(es->selection_start, es->selection_end);
3686 INT e = max(es->selection_start, es->selection_end);
3692 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3693 dst = GlobalLock(hdst);
3694 strncpyW(dst, es->text + s, e - s);
3695 dst[e - s] = 0; /* ensure 0 termination */
3696 TRACE("%s\n", debugstr_w(dst));
3698 OpenClipboard(es->hwndSelf);
3700 SetClipboardData(CF_UNICODETEXT, hdst);
3705 /*********************************************************************
3710 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3712 TRACE("%s\n", debugstr_w(name));
3714 * To initialize some final structure members, we call some helper
3715 * functions. However, since the EDITSTATE is not consistent (i.e.
3716 * not fully initialized), we should be very careful which
3717 * functions can be called, and in what order.
3719 EDIT_WM_SetFont(es, 0, FALSE);
3720 EDIT_EM_EmptyUndoBuffer(es);
3722 if (name && *name) {
3723 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE);
3724 /* if we insert text to the editline, the text scrolls out
3725 * of the window, as the caret is placed after the insert
3726 * pos normally; thus we reset es->selection... to 0 and
3729 es->selection_start = es->selection_end = 0;
3730 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3731 * Messages are only to be sent when the USER does something to
3732 * change the contents. So I am removing this EN_CHANGE
3734 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3736 EDIT_EM_ScrollCaret(es);
3738 /* force scroll info update */
3739 EDIT_UpdateScrollInfo(es);
3744 /*********************************************************************
3749 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3751 HINSTANCE hInstance = (HINSTANCE)GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3755 while (LocalUnlock(es->hloc32W)) ;
3756 LocalFree(es->hloc32W);
3759 while (LocalUnlock(es->hloc32A)) ;
3760 LocalFree(es->hloc32A);
3763 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3764 LOCAL_Free(hInstance, es->hloc16);
3767 pc = es->first_line_def;
3771 HeapFree(GetProcessHeap(), 0, pc);
3775 SetWindowLongW( es->hwndSelf, 0, 0 );
3776 HeapFree(GetProcessHeap(), 0, es);
3782 /*********************************************************************
3787 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3792 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
3793 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3795 GetClientRect(es->hwndSelf, &rc);
3796 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3797 GetClipBox(dc, &rc);
3799 * FIXME: specs say that we should UnrealizeObject() the brush,
3800 * but the specs of UnrealizeObject() say that we shouldn't
3801 * unrealize a stock object. The default brush that
3802 * DefWndProc() returns is ... a stock object.
3804 FillRect(dc, &rc, brush);
3809 /*********************************************************************
3814 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3816 if(!count) return 0;
3820 LPWSTR textW = (LPWSTR)lParam;
3821 lstrcpynW(textW, es->text, count);
3822 return strlenW(textW);
3826 LPSTR textA = (LPSTR)lParam;
3827 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3828 textA[count - 1] = 0; /* ensure 0 termination */
3829 return strlen(textA);
3833 /*********************************************************************
3838 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3843 if (!(es->style & ES_MULTILINE))
3846 if (!(es->style & ES_AUTOHSCROLL))
3850 fw = es->format_rect.right - es->format_rect.left;
3853 TRACE("SB_LINELEFT\n");
3855 dx = -es->char_width;
3858 TRACE("SB_LINERIGHT\n");
3859 if (es->x_offset < es->text_width)
3860 dx = es->char_width;
3863 TRACE("SB_PAGELEFT\n");
3865 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3868 TRACE("SB_PAGERIGHT\n");
3869 if (es->x_offset < es->text_width)
3870 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3878 TRACE("SB_RIGHT\n");
3879 if (es->x_offset < es->text_width)
3880 dx = es->text_width - es->x_offset;
3883 TRACE("SB_THUMBTRACK %d\n", pos);
3884 es->flags |= EF_HSCROLL_TRACK;
3885 if(es->style & WS_HSCROLL)
3886 dx = pos - es->x_offset;
3891 if(pos < 0 || pos > 100) return 0;
3892 /* Assume default scroll range 0-100 */
3893 fw = es->format_rect.right - es->format_rect.left;
3894 new_x = pos * (es->text_width - fw) / 100;
3895 dx = es->text_width ? (new_x - es->x_offset) : 0;
3898 case SB_THUMBPOSITION:
3899 TRACE("SB_THUMBPOSITION %d\n", pos);
3900 es->flags &= ~EF_HSCROLL_TRACK;
3901 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3902 dx = pos - es->x_offset;
3907 if(pos < 0 || pos > 100) return 0;
3908 /* Assume default scroll range 0-100 */
3909 fw = es->format_rect.right - es->format_rect.left;
3910 new_x = pos * (es->text_width - fw) / 100;
3911 dx = es->text_width ? (new_x - es->x_offset) : 0;
3914 /* force scroll info update */
3915 EDIT_UpdateScrollInfo(es);
3916 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
3920 TRACE("SB_ENDSCROLL\n");
3923 * FIXME : the next two are undocumented !
3924 * Are we doing the right thing ?
3925 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3926 * although it's also a regular control message.
3928 case EM_GETTHUMB: /* this one is used by NT notepad */
3932 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3933 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3936 /* Assume default scroll range 0-100 */
3937 INT fw = es->format_rect.right - es->format_rect.left;
3938 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
3940 TRACE("EM_GETTHUMB: returning %ld\n", ret);
3943 case EM_LINESCROLL16:
3944 TRACE("EM_LINESCROLL16\n");
3949 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
3955 INT fw = es->format_rect.right - es->format_rect.left;
3956 /* check if we are going to move too far */
3957 if(es->x_offset + dx + fw > es->text_width)
3958 dx = es->text_width - fw - es->x_offset;
3960 EDIT_EM_LineScroll_internal(es, dx, 0);
3966 /*********************************************************************
3971 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3973 HWND hLBox = es->hwndListBox;
3981 hCombo = GetParent(es->hwndSelf);
3985 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
3986 es->hwndSelf, (UINT16)msg, (UINT16)key);
3988 if (key == VK_UP || key == VK_DOWN)
3990 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3993 if (msg == WM_KEYDOWN || nEUI)
3994 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4000 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4002 /* make sure ComboLBox pops up */
4003 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4008 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4011 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4013 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4015 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4020 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4026 /*********************************************************************
4030 * Handling of special keys that don't produce a WM_CHAR
4031 * (i.e. non-printable keys) & Backspace & Delete
4034 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4039 if (GetKeyState(VK_MENU) & 0x8000)
4042 shift = GetKeyState(VK_SHIFT) & 0x8000;
4043 control = GetKeyState(VK_CONTROL) & 0x8000;
4048 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4053 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4054 EDIT_MoveUp_ML(es, shift);
4057 EDIT_MoveWordBackward(es, shift);
4059 EDIT_MoveBackward(es, shift);
4062 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4066 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4067 EDIT_MoveDown_ML(es, shift);
4069 EDIT_MoveWordForward(es, shift);
4071 EDIT_MoveForward(es, shift);
4074 EDIT_MoveHome(es, shift);
4077 EDIT_MoveEnd(es, shift);
4080 if (es->style & ES_MULTILINE)
4081 EDIT_MovePageUp_ML(es, shift);
4083 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4086 if (es->style & ES_MULTILINE)
4087 EDIT_MovePageDown_ML(es, shift);
4089 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4092 if (!(es->style & ES_READONLY) && !(shift && control)) {
4093 if (es->selection_start != es->selection_end) {
4100 /* delete character left of caret */
4101 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4102 EDIT_MoveBackward(es, TRUE);
4104 } else if (control) {
4105 /* delete to end of line */
4106 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4107 EDIT_MoveEnd(es, TRUE);
4110 /* delete character right of caret */
4111 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4112 EDIT_MoveForward(es, TRUE);
4120 if (!(es->style & ES_READONLY))
4126 /* If the edit doesn't want the return send a message to the default object */
4127 if(!(es->style & ES_WANTRETURN))
4129 HWND hwndParent = GetParent(es->hwndSelf);
4130 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4131 if (HIWORD(dw) == DC_HASDEFID)
4133 SendMessageW( hwndParent, WM_COMMAND,
4134 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4135 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4144 /*********************************************************************
4149 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4151 es->flags &= ~EF_FOCUSED;
4153 if(!(es->style & ES_NOHIDESEL))
4154 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4155 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4160 /*********************************************************************
4164 * The caret position has been set on the WM_LBUTTONDOWN message
4167 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4170 INT e = es->selection_end;
4175 if (!(es->flags & EF_FOCUSED))
4178 l = EDIT_EM_LineFromChar(es, e);
4179 li = EDIT_EM_LineIndex(es, l);
4180 ll = EDIT_EM_LineLength(es, e);
4181 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4182 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4183 EDIT_EM_SetSel(es, s, e, FALSE);
4184 EDIT_EM_ScrollCaret(es);
4189 /*********************************************************************
4194 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4199 if (!(es->flags & EF_FOCUSED))
4202 es->bCaptureState = TRUE;
4203 SetCapture(es->hwndSelf);
4204 EDIT_ConfinePoint(es, &x, &y);
4205 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4206 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4207 EDIT_EM_ScrollCaret(es);
4208 es->region_posx = es->region_posy = 0;
4209 SetTimer(es->hwndSelf, 0, 100, NULL);
4214 /*********************************************************************
4219 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4221 if (es->bCaptureState && GetCapture() == es->hwndSelf) {
4222 KillTimer(es->hwndSelf, 0);
4225 es->bCaptureState = FALSE;
4230 /*********************************************************************
4235 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4237 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4242 /*********************************************************************
4247 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4253 if (GetCapture() != es->hwndSelf)
4257 * FIXME: gotta do some scrolling if outside client
4258 * area. Maybe reset the timer ?
4261 EDIT_ConfinePoint(es, &x, &y);
4262 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4263 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4264 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4265 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4266 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4271 /*********************************************************************
4275 * See also EDIT_WM_StyleChanged
4277 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4282 TRACE("Creating %s edit control, style = %08lx\n",
4283 unicode ? "Unicode" : "ANSI", lpcs->style);
4285 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4287 SetWindowLongW( hwnd, 0, (LONG)es );
4290 * Note: since the EDITSTATE has not been fully initialized yet,
4291 * we can't use any API calls that may send
4292 * WM_XXX messages before WM_NCCREATE is completed.
4295 es->is_unicode = unicode;
4296 es->style = lpcs->style;
4298 es->bEnableState = !(es->style & WS_DISABLED);
4300 es->hwndSelf = hwnd;
4301 /* Save parent, which will be notified by EN_* messages */
4302 es->hwndParent = lpcs->hwndParent;
4304 if (es->style & ES_COMBO)
4305 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4307 /* Number overrides lowercase overrides uppercase (at least it
4308 * does in Win95). However I'll bet that ES_NUMBER would be
4309 * invalid under Win 3.1.
4311 if (es->style & ES_NUMBER) {
4312 ; /* do not override the ES_NUMBER */
4313 } else if (es->style & ES_LOWERCASE) {
4314 es->style &= ~ES_UPPERCASE;
4316 if (es->style & ES_MULTILINE) {
4317 es->buffer_limit = BUFLIMIT_MULTI;
4318 if (es->style & WS_VSCROLL)
4319 es->style |= ES_AUTOVSCROLL;
4320 if (es->style & WS_HSCROLL)
4321 es->style |= ES_AUTOHSCROLL;
4322 es->style &= ~ES_PASSWORD;
4323 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4324 /* Confirmed - RIGHT overrides CENTER */
4325 if (es->style & ES_RIGHT)
4326 es->style &= ~ES_CENTER;
4327 es->style &= ~WS_HSCROLL;
4328 es->style &= ~ES_AUTOHSCROLL;
4331 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4332 es->style |= ES_AUTOVSCROLL;
4334 es->buffer_limit = BUFLIMIT_SINGLE;
4335 if (WIN31_LOOK == TWEAK_WineLook ||
4336 WIN95_LOOK == TWEAK_WineLook) {
4337 es->style &= ~ES_CENTER;
4338 es->style &= ~ES_RIGHT;
4340 if (es->style & ES_RIGHT)
4341 es->style &= ~ES_CENTER;
4343 es->style &= ~WS_HSCROLL;
4344 es->style &= ~WS_VSCROLL;
4345 es->style &= ~ES_AUTOVSCROLL;
4346 es->style &= ~ES_WANTRETURN;
4347 if (es->style & ES_PASSWORD)
4348 es->password_char = '*';
4350 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4351 es->style |= ES_AUTOHSCROLL;
4354 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4355 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4357 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4359 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4361 es->undo_buffer_size = es->buffer_size;
4363 if (es->style & ES_MULTILINE)
4364 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4369 * In Win95 look and feel, the WS_BORDER style is replaced by the
4370 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4371 * control a non client area. Not always. This coordinates in some
4372 * way with the window creation code in dialog.c When making
4373 * modifications please ensure that the code still works for edit
4374 * controls created directly with style 0x50800000, exStyle 0 (
4375 * which should have a single pixel border)
4377 if (TWEAK_WineLook != WIN31_LOOK)
4379 es->style &= ~WS_BORDER;
4383 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4384 SetWindowLongW( hwnd, GWL_STYLE,
4385 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_BORDER );
4391 /*********************************************************************
4396 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam)
4405 BOOL rev = es->bEnableState &&
4406 ((es->flags & EF_FOCUSED) ||
4407 (es->style & ES_NOHIDESEL));
4409 dc = BeginPaint(es->hwndSelf, &ps);
4412 if(es->style & WS_BORDER) {
4413 GetClientRect(es->hwndSelf, &rc);
4414 if(es->style & ES_MULTILINE) {
4415 if(es->style & WS_HSCROLL) rc.bottom++;
4416 if(es->style & WS_VSCROLL) rc.right++;
4418 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4420 IntersectClipRect(dc, es->format_rect.left,
4421 es->format_rect.top,
4422 es->format_rect.right,
4423 es->format_rect.bottom);
4424 if (es->style & ES_MULTILINE) {
4425 GetClientRect(es->hwndSelf, &rc);
4426 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4429 old_font = SelectObject(dc, es->font);
4430 EDIT_NotifyCtlColor(es, dc);
4432 if (!es->bEnableState)
4433 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4434 GetClipBox(dc, &rcRgn);
4435 if (es->style & ES_MULTILINE) {
4436 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4437 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4438 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4439 if (IntersectRect(&rc, &rcRgn, &rcLine))
4440 EDIT_PaintLine(es, dc, i, rev);
4443 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4444 if (IntersectRect(&rc, &rcRgn, &rcLine))
4445 EDIT_PaintLine(es, dc, 0, rev);
4448 SelectObject(dc, old_font);
4451 EndPaint(es->hwndSelf, &ps);
4455 /*********************************************************************
4460 static void EDIT_WM_Paste(EDITSTATE *es)
4465 /* Protect read-only edit control from modification */
4466 if(es->style & ES_READONLY)
4469 OpenClipboard(es->hwndSelf);
4470 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4471 src = (LPWSTR)GlobalLock(hsrc);
4472 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE);
4479 /*********************************************************************
4484 static void EDIT_WM_SetFocus(EDITSTATE *es)
4486 es->flags |= EF_FOCUSED;
4487 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4488 EDIT_SetCaretPos(es, es->selection_end,
4489 es->flags & EF_AFTER_WRAP);
4490 if(!(es->style & ES_NOHIDESEL))
4491 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4492 ShowCaret(es->hwndSelf);
4493 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4497 /*********************************************************************
4501 * With Win95 look the margins are set to default font value unless
4502 * the system font (font == 0) is being set, in which case they are left
4506 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4514 dc = GetDC(es->hwndSelf);
4516 old_font = SelectObject(dc, font);
4517 GetTextMetricsW(dc, &tm);
4518 es->line_height = tm.tmHeight;
4519 es->char_width = tm.tmAveCharWidth;
4521 SelectObject(dc, old_font);
4522 ReleaseDC(es->hwndSelf, dc);
4523 if (font && (TWEAK_WineLook > WIN31_LOOK))
4524 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4525 EC_USEFONTINFO, EC_USEFONTINFO);
4527 /* Force the recalculation of the format rect for each font change */
4528 GetClientRect(es->hwndSelf, &r);
4529 EDIT_SetRectNP(es, &r);
4531 if (es->style & ES_MULTILINE)
4532 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, (HRGN)0);
4534 EDIT_CalcLineWidth_SL(es);
4537 EDIT_UpdateText(es, NULL, TRUE);
4538 if (es->flags & EF_FOCUSED) {
4540 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4541 EDIT_SetCaretPos(es, es->selection_end,
4542 es->flags & EF_AFTER_WRAP);
4543 ShowCaret(es->hwndSelf);
4548 /*********************************************************************
4553 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4554 * The modified flag is reset. No notifications are sent.
4556 * For single-line controls, reception of WM_SETTEXT triggers:
4557 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4560 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode)
4565 text = (LPWSTR)lParam;
4568 LPCSTR textA = (LPCSTR)lParam;
4569 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4570 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4571 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4574 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4576 TRACE("%s\n", debugstr_w(text));
4577 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE);
4579 HeapFree(GetProcessHeap(), 0, text);
4581 static const WCHAR empty_stringW[] = {0};
4583 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE);
4586 es->flags &= ~EF_MODIFIED;
4587 EDIT_EM_SetSel(es, 0, 0, FALSE);
4588 /* Send the notification after the selection start and end have been set
4589 * edit control doesn't send notification on WM_SETTEXT
4590 * if it is multiline, or it is part of combobox
4592 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4594 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4595 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4597 EDIT_EM_ScrollCaret(es);
4601 /*********************************************************************
4606 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4608 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4610 TRACE("width = %d, height = %d\n", width, height);
4611 SetRect(&rc, 0, 0, width, height);
4612 EDIT_SetRectNP(es, &rc);
4613 EDIT_UpdateText(es, NULL, TRUE);
4618 /*********************************************************************
4622 * This message is sent by SetWindowLong on having changed either the Style
4623 * or the extended style.
4625 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4627 * See also EDIT_WM_NCCreate
4629 * It appears that the Windows version of the edit control allows the style
4630 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4631 * style variable which will generally be different. In this function we
4632 * update the internal style based on what changed in the externally visible
4635 * Much of this content as based upon the MSDN, especially:
4636 * Platform SDK Documentation -> User Interface Services ->
4637 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4638 * Edit Control Styles
4640 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4642 if (GWL_STYLE == which) {
4643 DWORD style_change_mask;
4645 /* Only a subset of changes can be applied after the control
4648 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4650 if (es->style & ES_MULTILINE)
4651 style_change_mask |= ES_WANTRETURN;
4653 new_style = style->styleNew & style_change_mask;
4655 /* Number overrides lowercase overrides uppercase (at least it
4656 * does in Win95). However I'll bet that ES_NUMBER would be
4657 * invalid under Win 3.1.
4659 if (new_style & ES_NUMBER) {
4660 ; /* do not override the ES_NUMBER */
4661 } else if (new_style & ES_LOWERCASE) {
4662 new_style &= ~ES_UPPERCASE;
4665 es->style = (es->style & ~style_change_mask) | new_style;
4666 } else if (GWL_EXSTYLE == which) {
4667 ; /* FIXME - what is needed here */
4669 WARN ("Invalid style change %d\n",which);
4675 /*********************************************************************
4680 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4682 if ((key == VK_BACK) && (key_data & 0x2000)) {
4683 if (EDIT_EM_CanUndo(es))
4686 } else if (key == VK_UP || key == VK_DOWN) {
4687 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4690 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4694 /*********************************************************************
4699 static void EDIT_WM_Timer(EDITSTATE *es)
4701 if (es->region_posx < 0) {
4702 EDIT_MoveBackward(es, TRUE);
4703 } else if (es->region_posx > 0) {
4704 EDIT_MoveForward(es, TRUE);
4707 * FIXME: gotta do some vertical scrolling here, like
4708 * EDIT_EM_LineScroll(hwnd, 0, 1);
4712 /*********************************************************************
4717 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4721 if (!(es->style & ES_MULTILINE))
4724 if (!(es->style & ES_AUTOVSCROLL))
4733 TRACE("action %d\n", action);
4734 EDIT_EM_Scroll(es, action);
4741 TRACE("SB_BOTTOM\n");
4742 dy = es->line_count - 1 - es->y_offset;
4745 TRACE("SB_THUMBTRACK %d\n", pos);
4746 es->flags |= EF_VSCROLL_TRACK;
4747 if(es->style & WS_VSCROLL)
4748 dy = pos - es->y_offset;
4751 /* Assume default scroll range 0-100 */
4754 if(pos < 0 || pos > 100) return 0;
4755 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4756 new_y = pos * (es->line_count - vlc) / 100;
4757 dy = es->line_count ? (new_y - es->y_offset) : 0;
4758 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4759 es->line_count, es->y_offset, pos, dy);
4762 case SB_THUMBPOSITION:
4763 TRACE("SB_THUMBPOSITION %d\n", pos);
4764 es->flags &= ~EF_VSCROLL_TRACK;
4765 if(es->style & WS_VSCROLL)
4766 dy = pos - es->y_offset;
4769 /* Assume default scroll range 0-100 */
4772 if(pos < 0 || pos > 100) return 0;
4773 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4774 new_y = pos * (es->line_count - vlc) / 100;
4775 dy = es->line_count ? (new_y - es->y_offset) : 0;
4776 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4777 es->line_count, es->y_offset, pos, dy);
4781 /* force scroll info update */
4782 EDIT_UpdateScrollInfo(es);
4783 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4787 TRACE("SB_ENDSCROLL\n");
4790 * FIXME : the next two are undocumented !
4791 * Are we doing the right thing ?
4792 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4793 * although it's also a regular control message.
4795 case EM_GETTHUMB: /* this one is used by NT notepad */
4799 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4800 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4803 /* Assume default scroll range 0-100 */
4804 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4805 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4807 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4810 case EM_LINESCROLL16:
4811 TRACE("EM_LINESCROLL16 %d\n", pos);
4816 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4821 EDIT_EM_LineScroll(es, 0, dy);
4825 /*********************************************************************
4830 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4832 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4833 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4837 /*********************************************************************
4842 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4844 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4845 InvalidateRect(es->hwndSelf, rc, bErase);