4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
11 * please read EDIT.TODO (and update it when you change things)
22 #include "wine/winbase16.h"
23 #include "wine/winuser16.h"
24 #include "wine/unicode.h"
27 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(edit);
31 DECLARE_DEBUG_CHANNEL(combo);
32 DECLARE_DEBUG_CHANNEL(relay);
34 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
35 FIXME: BTW, new specs say 65535 (do you dare ???) */
36 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
37 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
38 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
41 * extra flags for EDITSTATE.flags field
43 #define EF_MODIFIED 0x0001 /* text has been modified */
44 #define EF_FOCUSED 0x0002 /* we have input focus */
45 #define EF_UPDATE 0x0004 /* notify parent of changed state on next WM_PAINT */
46 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
47 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
48 #define EF_VSCROLL_HACK 0x0020 /* we already have informed the user of the hacked handler */
49 #define EF_HSCROLL_HACK 0x0040 /* we already have informed the user of the hacked handler */
50 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
51 wrapped line, instead of in front of the next character */
52 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
56 END_0 = 0, /* line ends with terminating '\0' character */
57 END_WRAP, /* line is wrapped */
58 END_HARD, /* line ends with a hard return '\r\n' */
59 END_SOFT /* line ends with a soft return '\r\r\n' */
62 typedef struct tagLINEDEF {
63 INT length; /* bruto length of a line in bytes */
64 INT net_length; /* netto length of a line in visible characters */
66 INT width; /* width of the line in pixels */
67 struct tagLINEDEF *next;
72 BOOL is_unicode; /* how the control was created */
73 LPWSTR text; /* the actual contents of the control */
74 UINT buffer_size; /* the size of the buffer in characters */
75 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
76 HFONT font; /* NULL means standard system font */
77 INT x_offset; /* scroll offset for multi lines this is in pixels
78 for single lines it's in characters */
79 INT line_height; /* height of a screen line in pixels */
80 INT char_width; /* average character width in pixels */
81 DWORD style; /* sane version of wnd->dwStyle */
82 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
83 INT undo_insert_count; /* number of characters inserted in sequence */
84 UINT undo_position; /* character index of the insertion and deletion */
85 LPWSTR undo_text; /* deleted text */
86 UINT undo_buffer_size; /* size of the deleted text buffer */
87 INT selection_start; /* == selection_end if no selection */
88 INT selection_end; /* == current caret position */
89 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
90 INT left_margin; /* in pixels */
91 INT right_margin; /* in pixels */
93 INT text_width; /* width of the widest line in pixels for multi line controls
94 and just line width for single line controls */
95 INT region_posx; /* Position of cursor relative to region: */
96 INT region_posy; /* -1: to left, 0: within, 1: to right */
97 EDITWORDBREAKPROC16 word_break_proc16;
98 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
99 INT line_count; /* number of lines */
100 INT y_offset; /* scroll offset in number of lines */
101 BOOL bCaptureState; /* flag indicating whether mouse was captured */
102 BOOL bEnableState; /* flag keeping the enable state */
103 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
105 * only for multi line controls
107 INT lock_count; /* amount of re-entries in the EditWndProc */
110 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
111 HLOCAL hloc32W; /* our unicode local memory block */
112 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
114 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
119 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
120 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
122 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
123 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
125 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
126 do {TRACE("notification " str " sent to hwnd=%08x\n", \
127 (UINT)(hwnd));} while(0)
129 /* used for disabled or read-only edit control */
130 #define EDIT_SEND_CTLCOLORSTATIC(wnd,hdc) \
131 (SendMessageW((wnd)->parent->hwndSelf, WM_CTLCOLORSTATIC, \
132 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
133 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
134 (SendMessageW((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
135 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
136 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
137 do {DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str); \
138 SendMessageW((wnd)->parent->hwndSelf, WM_COMMAND, \
139 MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
140 (LPARAM)(wnd)->hwndSelf);} while(0)
141 #define DPRINTF_EDIT_MSG16(str) \
143 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
144 (UINT)wnd->hwndSelf, (UINT)wParam, (UINT)lParam)
145 #define DPRINTF_EDIT_MSG32(str) \
147 "32 bit %c : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
148 unicode ? 'W' : 'A', \
149 (UINT)wnd->hwndSelf, (UINT)wParam, (UINT)lParam)
151 /*********************************************************************
158 * These functions have trivial implementations
159 * We still like to call them internally
160 * "static inline" makes them more like macro's
162 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
163 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
164 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
165 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
168 * Helper functions only valid for one type of control
170 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
171 static void EDIT_CalcLineWidth_SL(WND *wnd, EDITSTATE *es);
172 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
173 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
174 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
175 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
176 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
178 * Helper functions valid for both single line _and_ multi line controls
180 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
181 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
182 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
183 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
184 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end);
185 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
186 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, UINT size);
187 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
188 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend);
189 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend);
190 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend);
191 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend);
192 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend);
193 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend);
194 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
195 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
196 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos, BOOL after_wrap);
197 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT lprc);
198 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force);
199 static void EDIT_UpdateScrollInfo(WND *wnd, EDITSTATE *es);
200 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
202 * EM_XXX message handlers
204 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y);
205 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
206 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
207 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
208 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR lpch);
209 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end);
210 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
211 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
212 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
213 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
214 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy);
215 static BOOL EDIT_EM_LineScroll_internal(WND *wnd, EDITSTATE *es, INT dx, INT dy);
216 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap);
217 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update);
218 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action);
219 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
220 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc);
221 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
222 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
223 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
224 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, WCHAR c);
225 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
226 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
227 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
228 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, LPARAM lParam);
229 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
230 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
232 * WM_XXX message handlers
234 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, WCHAR c);
235 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
236 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, INT x, INT y);
237 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
238 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCWSTR name);
239 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
240 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc);
241 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
242 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos);
243 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key);
244 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es);
245 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es);
246 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
247 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es);
248 static LRESULT EDIT_WM_MButtonDown(WND *wnd);
249 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, INT x, INT y);
250 static LRESULT EDIT_WM_NCCreate(WND *wnd, DWORD style, HWND hwndParent, BOOL unicode);
251 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam);
252 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
253 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es);
254 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw);
255 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPARAM lParam, BOOL unicode);
256 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height);
257 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
258 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es);
259 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos);
260 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase);
262 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
263 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
265 /*********************************************************************
266 * edit class descriptor
268 const struct builtin_class_descr EDIT_builtin_class =
271 CS_GLOBALCLASS | CS_DBLCLKS /*| CS_PARENTDC*/, /* style */
272 EditWndProcA, /* procA */
273 EditWndProcW, /* procW */
274 sizeof(EDITSTATE *), /* extra */
275 IDC_IBEAMA, /* cursor */
280 /*********************************************************************
285 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
287 return (es->undo_insert_count || strlenW(es->undo_text));
291 /*********************************************************************
296 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
298 es->undo_insert_count = 0;
299 *es->undo_text = '\0';
303 /*********************************************************************
308 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
310 static const WCHAR empty_stringW[] = {0};
312 EDIT_EM_ReplaceSel(wnd, es, TRUE, empty_stringW, TRUE);
314 if (es->flags & EF_UPDATE) {
315 es->flags &= ~EF_UPDATE;
316 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
321 /*********************************************************************
326 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
328 EDIT_WM_Copy(wnd, es);
329 EDIT_WM_Clear(wnd, es);
333 /**********************************************************************
336 * Returns the window version in case Wine emulates a later version
337 * of windows then the application expects.
339 * In a number of cases when windows runs an application that was
340 * designed for an earlier windows version, windows reverts
341 * to "old" behaviour of that earlier version.
343 * An example is a disabled edit control that needs to be painted.
344 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
345 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
346 * applications with an expected version 0f 4.0 or higher.
349 static DWORD get_app_version(void)
351 static DWORD version;
354 DWORD dwEmulatedVersion;
356 DWORD dwProcVersion = GetProcessVersion(0);
358 GetVersionExW( &info );
359 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
360 /* fixme: this may not be 100% correct; see discussion on the
361 * wine developer list in Nov 1999 */
362 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
368 /*********************************************************************
372 * The messages are in the order of the actual integer values
373 * (which can be found in include/windows.h)
374 * Whereever possible the 16 bit versions are converted to
375 * the 32 bit ones, so that we can 'fall through' to the
376 * helper functions. These are mostly 32 bit (with a few
377 * exceptions, clearly indicated by a '16' extension to their
381 static LRESULT WINAPI EditWndProc_locked( WND *wnd, UINT msg,
382 WPARAM wParam, LPARAM lParam, BOOL unicode )
384 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
389 DPRINTF_EDIT_MSG32("WM_DESTROY");
390 EDIT_WM_Destroy(wnd, es);
395 DPRINTF_EDIT_MSG32("WM_NCCREATE");
398 LPCREATESTRUCTW cs = (LPCREATESTRUCTW)lParam;
399 result = EDIT_WM_NCCreate(wnd, cs->style, cs->hwndParent, TRUE);
403 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
404 result = EDIT_WM_NCCreate(wnd, cs->style, cs->hwndParent, FALSE);
412 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
414 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
419 EDIT_LockBuffer(wnd, es);
422 DPRINTF_EDIT_MSG16("EM_GETSEL");
427 DPRINTF_EDIT_MSG32("EM_GETSEL");
428 result = EDIT_EM_GetSel(es, (LPUINT)wParam, (LPUINT)lParam);
432 DPRINTF_EDIT_MSG16("EM_SETSEL");
433 if (SLOWORD(lParam) == -1)
434 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
436 EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
438 EDIT_EM_ScrollCaret(wnd, es);
442 DPRINTF_EDIT_MSG32("EM_SETSEL");
443 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
444 EDIT_EM_ScrollCaret(wnd, es);
449 DPRINTF_EDIT_MSG16("EM_GETRECT");
451 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
454 DPRINTF_EDIT_MSG32("EM_GETRECT");
456 CopyRect((LPRECT)lParam, &es->format_rect);
460 DPRINTF_EDIT_MSG16("EM_SETRECT");
461 if ((es->style & ES_MULTILINE) && lParam) {
463 CONV_RECT16TO32(MapSL(lParam), &rc);
464 EDIT_SetRectNP(wnd, es, &rc);
465 EDIT_UpdateText(wnd, NULL, TRUE);
469 DPRINTF_EDIT_MSG32("EM_SETRECT");
470 if ((es->style & ES_MULTILINE) && lParam) {
471 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
472 EDIT_UpdateText(wnd, NULL, TRUE);
477 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
478 if ((es->style & ES_MULTILINE) && lParam) {
480 CONV_RECT16TO32(MapSL(lParam), &rc);
481 EDIT_SetRectNP(wnd, es, &rc);
485 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
486 if ((es->style & ES_MULTILINE) && lParam)
487 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
491 DPRINTF_EDIT_MSG16("EM_SCROLL");
494 DPRINTF_EDIT_MSG32("EM_SCROLL");
495 result = EDIT_EM_Scroll(wnd, es, (INT)wParam);
498 case EM_LINESCROLL16:
499 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
500 wParam = (WPARAM)(INT)SHIWORD(lParam);
501 lParam = (LPARAM)(INT)SLOWORD(lParam);
504 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
505 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT)wParam, (INT)lParam);
508 case EM_SCROLLCARET16:
509 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
512 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
513 EDIT_EM_ScrollCaret(wnd, es);
518 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
521 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
522 result = ((es->flags & EF_MODIFIED) != 0);
526 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
529 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
531 es->flags |= EF_MODIFIED;
533 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
536 case EM_GETLINECOUNT16:
537 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
539 case EM_GETLINECOUNT:
540 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
541 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
545 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
546 if ((INT16)wParam == -1)
550 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
551 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
555 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
556 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
559 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
560 EDIT_EM_SetHandle(wnd, es, (HLOCAL)wParam);
564 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
565 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
568 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
569 result = (LRESULT)EDIT_EM_GetHandle(es);
573 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
576 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
577 result = EDIT_EM_GetThumb(wnd, es);
580 /* messages 0x00bf and 0x00c0 missing from specs */
583 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
586 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
588 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
590 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
594 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
597 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
599 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
601 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
604 case EM_LINELENGTH16:
605 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
608 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
609 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
612 case EM_REPLACESEL16:
613 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
614 lParam = (LPARAM)MapSL(lParam);
619 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
622 textW = (LPWSTR)lParam;
625 LPSTR textA = (LPSTR)lParam;
626 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
627 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
628 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
631 EDIT_EM_ReplaceSel(wnd, es, (BOOL)wParam, textW, TRUE);
632 if (es->flags & EF_UPDATE) {
633 es->flags &= ~EF_UPDATE;
634 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
639 HeapFree(GetProcessHeap(), 0, textW);
642 /* message 0x00c3 missing from specs */
645 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
648 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
650 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
652 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
656 DPRINTF_EDIT_MSG16("EM_GETLINE");
657 lParam = (LPARAM)MapSL(lParam);
660 DPRINTF_EDIT_MSG32("EM_GETLINE");
661 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam);
665 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
667 case EM_SETLIMITTEXT:
668 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
669 EDIT_EM_SetLimitText(es, (INT)wParam);
673 DPRINTF_EDIT_MSG16("EM_CANUNDO");
676 DPRINTF_EDIT_MSG32("EM_CANUNDO");
677 result = (LRESULT)EDIT_EM_CanUndo(es);
681 DPRINTF_EDIT_MSG16("EM_UNDO");
686 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
687 result = (LRESULT)EDIT_EM_Undo(wnd, es);
691 DPRINTF_EDIT_MSG16("EM_FMTLINES");
694 DPRINTF_EDIT_MSG32("EM_FMTLINES");
695 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
698 case EM_LINEFROMCHAR16:
699 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
701 case EM_LINEFROMCHAR:
702 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
703 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
706 /* message 0x00ca missing from specs */
709 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
712 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
714 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
716 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
719 case EM_SETTABSTOPS16:
720 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
721 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
724 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
725 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
728 case EM_SETPASSWORDCHAR16:
729 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
731 case EM_SETPASSWORDCHAR:
734 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
737 charW = (WCHAR)wParam;
741 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
744 EDIT_EM_SetPasswordChar(wnd, es, charW);
748 case EM_EMPTYUNDOBUFFER16:
749 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
751 case EM_EMPTYUNDOBUFFER:
752 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
753 EDIT_EM_EmptyUndoBuffer(es);
756 case EM_GETFIRSTVISIBLELINE16:
757 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
758 result = es->y_offset;
760 case EM_GETFIRSTVISIBLELINE:
761 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
762 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
765 case EM_SETREADONLY16:
766 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
769 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
771 wnd->dwStyle |= ES_READONLY;
772 es->style |= ES_READONLY;
774 wnd->dwStyle &= ~ES_READONLY;
775 es->style &= ~ES_READONLY;
780 case EM_SETWORDBREAKPROC16:
781 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
782 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
784 case EM_SETWORDBREAKPROC:
785 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
786 EDIT_EM_SetWordBreakProc(wnd, es, lParam);
789 case EM_GETWORDBREAKPROC16:
790 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
791 result = (LRESULT)es->word_break_proc16;
793 case EM_GETWORDBREAKPROC:
794 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
795 result = (LRESULT)es->word_break_proc;
798 case EM_GETPASSWORDCHAR16:
799 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
801 case EM_GETPASSWORDCHAR:
803 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
806 result = es->password_char;
809 WCHAR charW = es->password_char;
811 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
817 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
820 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
821 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
825 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
826 result = MAKELONG(es->left_margin, es->right_margin);
829 case EM_GETLIMITTEXT:
830 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
831 result = es->buffer_limit;
835 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
836 result = EDIT_EM_PosFromChar(wnd, es, (INT)wParam, FALSE);
840 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
841 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
845 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
846 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
848 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
850 int vk = (int)((LPMSG)lParam)->wParam;
852 if ((wnd->dwStyle & ES_WANTRETURN) && vk == VK_RETURN)
854 result |= DLGC_WANTMESSAGE;
856 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
858 if (SendMessageW(wnd->parent->hwndSelf, CB_GETDROPPEDSTATE, 0, 0))
859 result |= DLGC_WANTMESSAGE;
867 DPRINTF_EDIT_MSG32("WM_CHAR");
874 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
877 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
879 if (SendMessageW(wnd->parent->hwndSelf, CB_GETDROPPEDSTATE, 0, 0))
880 SendMessageW(wnd->parent->hwndSelf, WM_KEYDOWN, charW, 0);
883 EDIT_WM_Char(wnd, es, charW);
888 DPRINTF_EDIT_MSG32("WM_CLEAR");
889 EDIT_WM_Clear(wnd, es);
893 DPRINTF_EDIT_MSG32("WM_COMMAND");
894 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
898 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
899 EDIT_WM_ContextMenu(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
903 DPRINTF_EDIT_MSG32("WM_COPY");
904 EDIT_WM_Copy(wnd, es);
908 DPRINTF_EDIT_MSG32("WM_CREATE");
910 result = EDIT_WM_Create(wnd, es, ((LPCREATESTRUCTW)lParam)->lpszName);
913 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
917 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
918 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
919 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
921 result = EDIT_WM_Create(wnd, es, nameW);
923 HeapFree(GetProcessHeap(), 0, nameW);
928 DPRINTF_EDIT_MSG32("WM_CUT");
929 EDIT_WM_Cut(wnd, es);
933 DPRINTF_EDIT_MSG32("WM_ENABLE");
934 es->bEnableState = (BOOL) wParam;
935 EDIT_UpdateText(wnd, NULL, TRUE);
939 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
940 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC)wParam);
944 DPRINTF_EDIT_MSG32("WM_GETFONT");
945 result = (LRESULT)es->font;
949 DPRINTF_EDIT_MSG32("WM_GETTEXT");
950 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
953 case WM_GETTEXTLENGTH:
954 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
955 result = strlenW(es->text);
959 DPRINTF_EDIT_MSG32("WM_HSCROLL");
960 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam));
964 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
965 result = EDIT_WM_KeyDown(wnd, es, (INT)wParam);
969 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
970 result = EDIT_WM_KillFocus(wnd, es);
973 case WM_LBUTTONDBLCLK:
974 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
975 result = EDIT_WM_LButtonDblClk(wnd, es);
979 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
980 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
984 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
985 result = EDIT_WM_LButtonUp(wnd->hwndSelf, es);
989 DPRINTF_EDIT_MSG32("WM_MBUTTONDOWN");
990 result = EDIT_WM_MButtonDown(wnd);
993 case WM_MOUSEACTIVATE:
995 * FIXME: maybe DefWindowProc() screws up, but it seems that
996 * modeless dialog boxes need this. If we don't do this, the focus
997 * will _not_ be set by DefWindowProc() for edit controls in a
998 * modeless dialog box ???
1000 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
1001 SetFocus(wnd->hwndSelf);
1002 result = MA_ACTIVATE;
1007 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
1009 result = EDIT_WM_MouseMove(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
1013 DPRINTF_EDIT_MSG32("WM_PAINT");
1014 EDIT_WM_Paint(wnd, es, wParam);
1018 DPRINTF_EDIT_MSG32("WM_PASTE");
1019 EDIT_WM_Paste(wnd, es);
1023 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
1024 EDIT_WM_SetFocus(wnd, es);
1028 DPRINTF_EDIT_MSG32("WM_SETFONT");
1029 EDIT_WM_SetFont(wnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
1033 DPRINTF_EDIT_MSG32("WM_SETTEXT");
1034 EDIT_WM_SetText(wnd, es, lParam, unicode);
1039 DPRINTF_EDIT_MSG32("WM_SIZE");
1040 EDIT_WM_Size(wnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
1044 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
1045 result = EDIT_WM_SysKeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
1049 DPRINTF_EDIT_MSG32("WM_TIMER");
1050 EDIT_WM_Timer(wnd, es);
1054 DPRINTF_EDIT_MSG32("WM_VSCROLL");
1055 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam));
1060 int gcWheelDelta = 0;
1061 UINT pulScrollLines = 3;
1062 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1064 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1066 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
1068 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
1071 gcWheelDelta -= SHIWORD(wParam);
1072 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1074 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1075 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1076 result = EDIT_EM_LineScroll(wnd, es, 0, cLineScroll);
1082 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
1084 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
1087 EDIT_UnlockBuffer(wnd, es, FALSE);
1092 /*********************************************************************
1094 * EditWndProcW (USER32.@)
1096 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1099 WND *wndPtr = WIN_FindWndPtr(hWnd);
1101 res = EditWndProc_locked(wndPtr, uMsg, wParam, lParam, TRUE);
1103 WIN_ReleaseWndPtr(wndPtr);
1107 /*********************************************************************
1109 * EditWndProcA (USER32.@)
1111 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1114 WND *wndPtr = WIN_FindWndPtr(hWnd);
1116 res = EditWndProc_locked(wndPtr, uMsg, wParam, lParam, FALSE);
1118 WIN_ReleaseWndPtr(wndPtr);
1122 /*********************************************************************
1124 * EDIT_BuildLineDefs_ML
1126 * Build linked list of text lines.
1127 * Lines can end with '\0' (last line), a character (if it is wrapped),
1128 * a soft return '\r\r\n' or a hard return '\r\n'
1131 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
1137 LINEDEF *current_def;
1138 LINEDEF **previous_next;
1140 current_def = es->first_line_def;
1142 LINEDEF *next_def = current_def->next;
1143 HeapFree(GetProcessHeap(), 0, current_def);
1144 current_def = next_def;
1145 } while (current_def);
1149 dc = GetDC(wnd->hwndSelf);
1151 old_font = SelectObject(dc, es->font);
1153 fw = es->format_rect.right - es->format_rect.left;
1155 previous_next = &es->first_line_def;
1157 current_def = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1158 current_def->next = NULL;
1161 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1166 current_def->ending = END_0;
1167 current_def->net_length = strlenW(start);
1168 } else if ((cp > start) && (*(cp - 1) == '\r')) {
1169 current_def->ending = END_SOFT;
1170 current_def->net_length = cp - start - 1;
1172 current_def->ending = END_HARD;
1173 current_def->net_length = cp - start;
1175 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1176 start, current_def->net_length,
1177 es->tabs_count, es->tabs));
1178 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1179 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
1184 next = EDIT_CallWordBreakProc(es, start - es->text,
1185 prev + 1, current_def->net_length, WB_RIGHT);
1186 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1187 start, next, es->tabs_count, es->tabs));
1188 } while (current_def->width <= fw);
1194 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1195 start, next, es->tabs_count, es->tabs));
1196 } while (current_def->width <= fw);
1200 current_def->net_length = prev;
1201 current_def->ending = END_WRAP;
1202 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc, start,
1203 current_def->net_length, es->tabs_count, es->tabs));
1205 switch (current_def->ending) {
1207 current_def->length = current_def->net_length + 3;
1210 current_def->length = current_def->net_length + 2;
1214 current_def->length = current_def->net_length;
1217 es->text_width = max(es->text_width, current_def->width);
1218 start += current_def->length;
1219 *previous_next = current_def;
1220 previous_next = ¤t_def->next;
1222 } while (current_def->ending != END_0);
1224 SelectObject(dc, old_font);
1225 ReleaseDC(wnd->hwndSelf, dc);
1228 /*********************************************************************
1230 * EDIT_CalcLineWidth_SL
1233 static void EDIT_CalcLineWidth_SL(WND *wnd, EDITSTATE *es)
1235 es->text_width = SLOWORD(EDIT_EM_PosFromChar(wnd, es, strlenW(es->text), FALSE));
1238 /*********************************************************************
1240 * EDIT_CallWordBreakProc
1242 * Call appropriate WordBreakProc (internal or external).
1244 * Note: The "start" argument should always be an index refering
1245 * to es->text. The actual wordbreak proc might be
1246 * 16 bit, so we can't always pass any 32 bit LPSTR.
1247 * Hence we assume that es->text is the buffer that holds
1248 * the string under examination (we can decide this for ourselves).
1251 /* ### start build ### */
1252 extern WORD CALLBACK EDIT_CallTo16_word_lwww(EDITWORDBREAKPROC16,SEGPTR,WORD,WORD,WORD);
1253 /* ### stop build ### */
1254 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1256 INT ret, iWndsLocks;
1258 /* To avoid any deadlocks, all the locks on the windows structures
1259 must be suspended before the control is passed to the application */
1260 iWndsLocks = WIN_SuspendWndsLock();
1262 if (es->word_break_proc16) {
1267 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1268 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1269 segptr = K32WOWGlobalLock16(hglob16);
1270 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1271 ret = (INT)EDIT_CallTo16_word_lwww(es->word_break_proc16,
1272 segptr, index, countA, action);
1273 GlobalUnlock16(hglob16);
1274 GlobalFree16(hglob16);
1276 else if (es->word_break_proc)
1280 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1282 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1283 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1284 ret = wbpW(es->text + start, index, count, action);
1288 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1292 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1293 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1294 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1295 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1296 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1297 ret = wbpA(textA, index, countA, action);
1298 HeapFree(GetProcessHeap(), 0, textA);
1302 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1304 WIN_RestoreWndsLock(iWndsLocks);
1309 /*********************************************************************
1313 * Beware: This is not the function called on EM_CHARFROMPOS
1314 * The position _can_ be outside the formatting / client
1316 * The return value is only the character index
1319 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1325 if (es->style & ES_MULTILINE) {
1326 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1328 LINEDEF *line_def = es->first_line_def;
1330 while ((line > 0) && line_def->next) {
1331 line_index += line_def->length;
1332 line_def = line_def->next;
1335 x += es->x_offset - es->format_rect.left;
1336 if (x >= line_def->width) {
1338 *after_wrap = (line_def->ending == END_WRAP);
1339 return line_index + line_def->net_length;
1343 *after_wrap = FALSE;
1346 dc = GetDC(wnd->hwndSelf);
1348 old_font = SelectObject(dc, es->font);
1349 low = line_index + 1;
1350 high = line_index + line_def->net_length + 1;
1351 while (low < high - 1)
1353 INT mid = (low + high) / 2;
1354 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1360 *after_wrap = ((index == line_index + line_def->net_length) &&
1361 (line_def->ending == END_WRAP));
1366 *after_wrap = FALSE;
1367 x -= es->format_rect.left;
1369 return es->x_offset;
1370 text = EDIT_GetPasswordPointer_SL(es);
1371 dc = GetDC(wnd->hwndSelf);
1373 old_font = SelectObject(dc, es->font);
1377 INT high = es->x_offset;
1378 while (low < high - 1)
1380 INT mid = (low + high) / 2;
1381 GetTextExtentPoint32W( dc, text + mid,
1382 es->x_offset - mid, &size );
1383 if (size.cx > -x) low = mid;
1390 INT low = es->x_offset;
1391 INT high = strlenW(es->text) + 1;
1392 while (low < high - 1)
1394 INT mid = (low + high) / 2;
1395 GetTextExtentPoint32W( dc, text + es->x_offset,
1396 mid - es->x_offset, &size );
1397 if (size.cx > x) high = mid;
1402 if (es->style & ES_PASSWORD)
1403 HeapFree(GetProcessHeap(), 0, text);
1406 SelectObject(dc, old_font);
1407 ReleaseDC(wnd->hwndSelf, dc);
1412 /*********************************************************************
1416 * adjusts the point to be within the formatting rectangle
1417 * (so CharFromPos returns the nearest _visible_ character)
1420 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1422 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1423 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1427 /*********************************************************************
1431 * Calculates the bounding rectangle for a line from a starting
1432 * column to an ending column.
1435 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1437 INT line_index = EDIT_EM_LineIndex(es, line);
1439 if (es->style & ES_MULTILINE)
1440 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1442 rc->top = es->format_rect.top;
1443 rc->bottom = rc->top + es->line_height;
1444 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1445 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1449 /*********************************************************************
1451 * EDIT_GetPasswordPointer_SL
1453 * note: caller should free the (optionally) allocated buffer
1456 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1458 if (es->style & ES_PASSWORD) {
1459 INT len = strlenW(es->text);
1460 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1462 while(len) text[--len] = es->password_char;
1469 /*********************************************************************
1473 * This acts as a LOCAL_Lock(), but it locks only once. This way
1474 * you can call it whenever you like, without unlocking.
1477 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1480 ERR("no EDITSTATE ... please report\n");
1486 BOOL _16bit = FALSE;
1490 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1491 es->text = LocalLock(es->hloc32W);
1495 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1496 countA = LocalSize(es->hloc32A);
1497 textA = LocalLock(es->hloc32A);
1501 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1502 countA = LOCAL_Size(wnd->hInstance, es->hloc16);
1503 textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
1508 ERR("no buffer ... please report\n");
1514 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size);
1516 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1518 LocalUnlock(es->hloc32A);
1525 /*********************************************************************
1527 * EDIT_SL_InvalidateText
1529 * Called from EDIT_InvalidateText().
1530 * Does the job for single-line controls only.
1533 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1538 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1539 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1540 EDIT_UpdateText(wnd, &rc, FALSE);
1544 /*********************************************************************
1546 * EDIT_ML_InvalidateText
1548 * Called from EDIT_InvalidateText().
1549 * Does the job for multi-line controls only.
1552 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1554 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1555 INT sl = EDIT_EM_LineFromChar(es, start);
1556 INT el = EDIT_EM_LineFromChar(es, end);
1565 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1568 sc = start - EDIT_EM_LineIndex(es, sl);
1569 ec = end - EDIT_EM_LineIndex(es, el);
1570 if (sl < es->y_offset) {
1574 if (el > es->y_offset + vlc) {
1575 el = es->y_offset + vlc;
1576 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1578 GetClientRect(wnd->hwndSelf, &rc1);
1579 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1581 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1582 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1583 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1585 EDIT_GetLineRect(wnd, es, sl, sc,
1586 EDIT_EM_LineLength(es,
1587 EDIT_EM_LineIndex(es, sl)),
1589 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1590 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1591 for (l = sl + 1 ; l < el ; l++) {
1592 EDIT_GetLineRect(wnd, es, l, 0,
1593 EDIT_EM_LineLength(es,
1594 EDIT_EM_LineIndex(es, l)),
1596 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1597 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1599 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1600 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1601 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1606 /*********************************************************************
1608 * EDIT_InvalidateText
1610 * Invalidate the text from offset start upto, but not including,
1611 * offset end. Useful for (re)painting the selection.
1612 * Regions outside the linewidth are not invalidated.
1613 * end == -1 means end == TextLength.
1614 * start and end need not be ordered.
1617 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1623 end = strlenW(es->text);
1625 ORDER_INT(start, end);
1627 if (es->style & ES_MULTILINE)
1628 EDIT_ML_InvalidateText(wnd, es, start, end);
1630 EDIT_SL_InvalidateText(wnd, es, start, end);
1634 /*********************************************************************
1638 * Try to fit size + 1 characters in the buffer. Constrain to limits.
1641 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, UINT size)
1645 if (size <= es->buffer_size)
1647 if (size > es->buffer_limit) {
1648 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1651 if (size > es->buffer_limit)
1652 size = es->buffer_limit;
1654 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1656 /* Force edit to unlock it's buffer. es->text now NULL */
1657 EDIT_UnlockBuffer(wnd, es, TRUE);
1660 UINT alloc_size = ((size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
1661 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1662 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32W, hNew32W);
1663 es->hloc32W = hNew32W;
1664 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1668 EDIT_LockBuffer(wnd, es);
1670 if (es->buffer_size < size) {
1671 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1672 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1675 TRACE("We now have %d+1\n", es->buffer_size);
1681 /*********************************************************************
1685 * Try to fit size + 1 bytes in the undo buffer.
1688 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1692 if (size <= es->undo_buffer_size)
1695 TRACE("trying to ReAlloc to %d+1\n", size);
1697 alloc_size = ((size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
1698 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1699 es->undo_buffer_size = alloc_size/sizeof(WCHAR);
1704 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1710 /*********************************************************************
1715 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1717 INT e = es->selection_end;
1721 if ((es->style & ES_MULTILINE) && e &&
1722 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1724 if (e && (es->text[e - 1] == '\r'))
1728 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1729 EDIT_EM_ScrollCaret(wnd, es);
1733 /*********************************************************************
1737 * Only for multi line controls
1738 * Move the caret one line down, on a column with the nearest
1739 * x coordinate on the screen (might be a different column).
1742 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1744 INT s = es->selection_start;
1745 INT e = es->selection_end;
1746 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1747 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1748 INT x = SLOWORD(pos);
1749 INT y = SHIWORD(pos);
1751 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1754 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1755 EDIT_EM_ScrollCaret(wnd, es);
1759 /*********************************************************************
1764 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1766 BOOL after_wrap = FALSE;
1769 /* Pass a high value in x to make sure of receiving the end of the line */
1770 if (es->style & ES_MULTILINE)
1771 e = EDIT_CharFromPos(wnd, es, 0x3fffffff,
1772 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1774 e = strlenW(es->text);
1775 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1776 EDIT_EM_ScrollCaret(wnd, es);
1780 /*********************************************************************
1785 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1787 INT e = es->selection_end;
1791 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1792 if (es->text[e] == '\n')
1794 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1798 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1799 EDIT_EM_ScrollCaret(wnd, es);
1803 /*********************************************************************
1807 * Home key: move to beginning of line.
1810 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
1814 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1815 if (es->style & ES_MULTILINE)
1816 e = EDIT_CharFromPos(wnd, es, -es->x_offset,
1817 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1820 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1821 EDIT_EM_ScrollCaret(wnd, es);
1825 /*********************************************************************
1827 * EDIT_MovePageDown_ML
1829 * Only for multi line controls
1830 * Move the caret one page down, on a column with the nearest
1831 * x coordinate on the screen (might be a different column).
1834 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1836 INT s = es->selection_start;
1837 INT e = es->selection_end;
1838 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1839 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1840 INT x = SLOWORD(pos);
1841 INT y = SHIWORD(pos);
1843 e = EDIT_CharFromPos(wnd, es, x,
1844 y + (es->format_rect.bottom - es->format_rect.top),
1848 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1849 EDIT_EM_ScrollCaret(wnd, es);
1853 /*********************************************************************
1855 * EDIT_MovePageUp_ML
1857 * Only for multi line controls
1858 * Move the caret one page up, on a column with the nearest
1859 * x coordinate on the screen (might be a different column).
1862 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1864 INT s = es->selection_start;
1865 INT e = es->selection_end;
1866 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1867 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1868 INT x = SLOWORD(pos);
1869 INT y = SHIWORD(pos);
1871 e = EDIT_CharFromPos(wnd, es, x,
1872 y - (es->format_rect.bottom - es->format_rect.top),
1876 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1877 EDIT_EM_ScrollCaret(wnd, es);
1881 /*********************************************************************
1885 * Only for multi line controls
1886 * Move the caret one line up, on a column with the nearest
1887 * x coordinate on the screen (might be a different column).
1890 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1892 INT s = es->selection_start;
1893 INT e = es->selection_end;
1894 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1895 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1896 INT x = SLOWORD(pos);
1897 INT y = SHIWORD(pos);
1899 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1902 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1903 EDIT_EM_ScrollCaret(wnd, es);
1907 /*********************************************************************
1909 * EDIT_MoveWordBackward
1912 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1914 INT s = es->selection_start;
1915 INT e = es->selection_end;
1920 l = EDIT_EM_LineFromChar(es, e);
1921 ll = EDIT_EM_LineLength(es, e);
1922 li = EDIT_EM_LineIndex(es, l);
1925 li = EDIT_EM_LineIndex(es, l - 1);
1926 e = li + EDIT_EM_LineLength(es, li);
1929 e = li + (INT)EDIT_CallWordBreakProc(es,
1930 li, e - li, ll, WB_LEFT);
1934 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1935 EDIT_EM_ScrollCaret(wnd, es);
1939 /*********************************************************************
1941 * EDIT_MoveWordForward
1944 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
1946 INT s = es->selection_start;
1947 INT e = es->selection_end;
1952 l = EDIT_EM_LineFromChar(es, e);
1953 ll = EDIT_EM_LineLength(es, e);
1954 li = EDIT_EM_LineIndex(es, l);
1956 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1957 e = EDIT_EM_LineIndex(es, l + 1);
1959 e = li + EDIT_CallWordBreakProc(es,
1960 li, e - li + 1, ll, WB_RIGHT);
1964 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1965 EDIT_EM_ScrollCaret(wnd, es);
1969 /*********************************************************************
1974 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
1976 INT s = es->selection_start;
1977 INT e = es->selection_end;
1984 if (es->style & ES_MULTILINE) {
1985 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1986 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1991 TRACE("line=%d\n", line);
1993 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(es, line), FALSE);
1996 li = EDIT_EM_LineIndex(es, line);
1997 ll = EDIT_EM_LineLength(es, li);
1998 s = es->selection_start;
1999 e = es->selection_end;
2001 s = min(li + ll, max(li, s));
2002 e = min(li + ll, max(li, e));
2003 if (rev && (s != e) &&
2004 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2005 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2006 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2007 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2009 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2013 /*********************************************************************
2018 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2028 BkColor = GetBkColor(dc);
2029 TextColor = GetTextColor(dc);
2031 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2032 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2034 li = EDIT_EM_LineIndex(es, line);
2035 if (es->style & ES_MULTILINE) {
2036 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2037 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2039 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2040 TextOutW(dc, x, y, text + li + col, count);
2041 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2043 if (es->style & ES_PASSWORD)
2044 HeapFree(GetProcessHeap(), 0, text);
2047 SetBkColor(dc, BkColor);
2048 SetTextColor(dc, TextColor);
2054 /*********************************************************************
2059 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
2062 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
2063 SetCaretPos(SLOWORD(res), SHIWORD(res));
2067 /*********************************************************************
2071 * note: this is not (exactly) the handler called on EM_SETRECTNP
2072 * it is also used to set the rect of a single line control
2075 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
2077 CopyRect(&es->format_rect, rc);
2078 if (es->style & WS_BORDER) {
2079 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2080 if(TWEAK_WineLook == WIN31_LOOK)
2082 es->format_rect.left += bw;
2083 es->format_rect.top += bw;
2084 es->format_rect.right -= bw;
2085 es->format_rect.bottom -= bw;
2087 es->format_rect.left += es->left_margin;
2088 es->format_rect.right -= es->right_margin;
2089 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2090 if (es->style & ES_MULTILINE)
2092 INT fw, vlc, max_x_offset, max_y_offset;
2094 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2095 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2097 /* correct es->x_offset */
2098 fw = es->format_rect.right - es->format_rect.left;
2099 max_x_offset = es->text_width - fw;
2100 if(max_x_offset < 0) max_x_offset = 0;
2101 if(es->x_offset > max_x_offset)
2102 es->x_offset = max_x_offset;
2104 /* correct es->y_offset */
2105 max_y_offset = es->line_count - vlc;
2106 if(max_y_offset < 0) max_y_offset = 0;
2107 if(es->y_offset > max_y_offset)
2108 es->y_offset = max_y_offset;
2111 /* Windows doesn't care to fix text placement for SL controls */
2112 es->format_rect.bottom = es->format_rect.top + es->line_height;
2114 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2115 EDIT_BuildLineDefs_ML(wnd, es);
2119 /*********************************************************************
2124 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
2127 ERR("no EDITSTATE ... please report\n");
2130 if (!es->lock_count) {
2131 ERR("lock_count == 0 ... please report\n");
2135 ERR("es->text == 0 ... please report\n");
2139 if (force || (es->lock_count == 1)) {
2142 BOOL _16bit = FALSE;
2147 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2148 countA = LocalSize(es->hloc32A);
2149 textA = LocalLock(es->hloc32A);
2153 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2154 countA = LOCAL_Size(wnd->hInstance, es->hloc16);
2155 textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
2161 WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, countA, NULL, NULL);
2163 LOCAL_Unlock(wnd->hInstance, es->hloc16);
2165 LocalUnlock(es->hloc32A);
2168 LocalUnlock(es->hloc32W);
2172 ERR("no buffer ... please report\n");
2180 /*********************************************************************
2182 * EDIT_UpdateScrollInfo
2185 static void EDIT_UpdateScrollInfo(WND *wnd, EDITSTATE *es)
2187 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2190 si.cbSize = sizeof(SCROLLINFO);
2191 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2193 si.nMax = es->line_count - 1;
2194 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2195 si.nPos = es->y_offset;
2196 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2197 si.nMin, si.nMax, si.nPage, si.nPos);
2198 SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
2201 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2204 si.cbSize = sizeof(SCROLLINFO);
2205 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2207 si.nMax = es->text_width - 1;
2208 si.nPage = es->format_rect.right - es->format_rect.left;
2209 si.nPos = es->x_offset;
2210 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2211 si.nMin, si.nMax, si.nPage, si.nPos);
2212 SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
2216 /*********************************************************************
2218 * EDIT_WordBreakProc
2220 * Find the beginning of words.
2221 * Note: unlike the specs for a WordBreakProc, this function only
2222 * allows to be called without linebreaks between s[0] upto
2223 * s[count - 1]. Remember it is only called
2224 * internally, so we can decide this for ourselves.
2227 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2231 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2241 if (s[index] == ' ') {
2242 while (index && (s[index] == ' '))
2245 while (index && (s[index] != ' '))
2247 if (s[index] == ' ')
2251 while (index && (s[index] != ' '))
2253 if (s[index] == ' ')
2263 if (s[index] == ' ')
2264 while ((index < count) && (s[index] == ' ')) index++;
2266 while (s[index] && (s[index] != ' ') && (index < count))
2268 while ((s[index] == ' ') && (index < count)) index++;
2272 case WB_ISDELIMITER:
2273 ret = (s[index] == ' ');
2276 ERR("unknown action code, please report !\n");
2283 /*********************************************************************
2287 * returns line number (not index) in high-order word of result.
2288 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2289 * to Richedit, not to the edit control. Original documentation is valid.
2290 * FIXME: do the specs mean to return -1 if outside client area or
2291 * if outside formatting rectangle ???
2294 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y)
2302 GetClientRect(wnd->hwndSelf, &rc);
2303 if (!PtInRect(&rc, pt))
2306 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
2307 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2311 /*********************************************************************
2315 * Enable or disable soft breaks.
2317 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2319 es->flags &= ~EF_USE_SOFTBRK;
2321 es->flags |= EF_USE_SOFTBRK;
2322 FIXME("soft break enabled, not implemented\n");
2328 /*********************************************************************
2332 * Hopefully this won't fire back at us.
2333 * We always start with a fixed buffer in the local heap.
2334 * Despite of the documentation says that the local heap is used
2335 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2336 * buffer on the local heap.
2339 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2343 if (!(es->style & ES_MULTILINE))
2347 hLocal = es->hloc32W;
2354 TRACE("Allocating 32-bit ANSI alias buffer\n");
2355 countA = WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, NULL, 0, NULL, NULL);
2356 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countA)))
2358 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", countA);
2361 textA = LocalLock(es->hloc32A);
2362 WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, countA, NULL, NULL);
2363 LocalUnlock(es->hloc32A);
2365 hLocal = es->hloc32A;
2368 TRACE("Returning %04X, LocalSize() = %d\n", hLocal, LocalSize(hLocal));
2373 /*********************************************************************
2377 * Hopefully this won't fire back at us.
2378 * We always start with a buffer in 32 bit linear memory.
2379 * However, with this message a 16 bit application requests
2380 * a handle of 16 bit local heap memory, where it expects to find
2382 * It's a pitty that from this moment on we have to use this
2383 * local heap, because applications may rely on the handle
2386 * In this function we'll try to switch to local heap.
2388 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2393 if (!(es->style & ES_MULTILINE))
2399 if (!LOCAL_HeapSize(wnd->hInstance)) {
2400 if (!LocalInit16(wnd->hInstance, 0,
2401 GlobalSize16(wnd->hInstance))) {
2402 ERR("could not initialize local heap\n");
2405 TRACE("local heap initialized\n");
2408 countA = WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, NULL, 0, NULL, NULL);
2410 TRACE("Allocating 16-bit ANSI alias buffer\n");
2411 if (!(es->hloc16 = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, countA))) {
2412 ERR("could not allocate new 16 bit buffer\n");
2416 if (!(textA = (LPSTR)LOCAL_Lock(wnd->hInstance, es->hloc16))) {
2417 ERR("could not lock new 16 bit buffer\n");
2418 LOCAL_Free(wnd->hInstance, es->hloc16);
2423 WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, countA, NULL, NULL);
2424 LOCAL_Unlock(wnd->hInstance, es->hloc16);
2426 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(wnd->hInstance, es->hloc16));
2431 /*********************************************************************
2436 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR lpch)
2442 if (es->style & ES_MULTILINE) {
2443 if (line >= es->line_count)
2447 i = EDIT_EM_LineIndex(es, line);
2449 len = min(*(WORD *)lpch, EDIT_EM_LineLength(es, i));
2450 for (i = 0 ; i < len ; i++) {
2455 return (LRESULT)len;
2459 /*********************************************************************
2464 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end)
2466 UINT s = es->selection_start;
2467 UINT e = es->selection_end;
2474 return MAKELONG(s, e);
2478 /*********************************************************************
2482 * FIXME: is this right ? (or should it be only VSCROLL)
2483 * (and maybe only for edit controls that really have their
2484 * own scrollbars) (and maybe only for multiline controls ?)
2485 * All in all: very poorly documented
2487 * FIXME: now it's also broken, because of the new WM_HSCROLL /
2488 * WM_VSCROLL handlers
2491 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2493 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0),
2494 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0));
2498 /*********************************************************************
2503 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2508 if (!(es->style & ES_MULTILINE))
2510 if (index > (INT)strlenW(es->text))
2511 return es->line_count - 1;
2513 index = min(es->selection_start, es->selection_end);
2516 line_def = es->first_line_def;
2517 index -= line_def->length;
2518 while ((index >= 0) && line_def->next) {
2520 line_def = line_def->next;
2521 index -= line_def->length;
2527 /*********************************************************************
2532 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2537 if (!(es->style & ES_MULTILINE))
2539 if (line >= es->line_count)
2543 line_def = es->first_line_def;
2545 INT index = es->selection_end - line_def->length;
2546 while ((index >= 0) && line_def->next) {
2547 line_index += line_def->length;
2548 line_def = line_def->next;
2549 index -= line_def->length;
2553 line_index += line_def->length;
2554 line_def = line_def->next;
2562 /*********************************************************************
2567 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2571 if (!(es->style & ES_MULTILINE))
2572 return strlenW(es->text);
2575 /* get the number of remaining non-selected chars of selected lines */
2578 li = EDIT_EM_LineFromChar(es, es->selection_start);
2579 /* # chars before start of selection area */
2580 count = es->selection_start - EDIT_EM_LineIndex(es, li);
2581 li = EDIT_EM_LineFromChar(es, es->selection_end);
2582 /* # chars after end of selection */
2583 count += EDIT_EM_LineIndex(es, li) +
2584 EDIT_EM_LineLength(es, li) - es->selection_end;
2587 line_def = es->first_line_def;
2588 index -= line_def->length;
2589 while ((index >= 0) && line_def->next) {
2590 line_def = line_def->next;
2591 index -= line_def->length;
2593 return line_def->net_length;
2597 /*********************************************************************
2601 * NOTE: dx is in average character widths, dy - in lines;
2604 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2606 if (!(es->style & ES_MULTILINE))
2609 dx *= es->char_width;
2610 return EDIT_EM_LineScroll_internal(wnd, es, dx, dy);
2613 /*********************************************************************
2615 * EDIT_EM_LineScroll_internal
2617 * Version of EDIT_EM_LineScroll for internal use.
2618 * It doesn't refuse if ES_MULTILINE is set and assumes that
2619 * dx is in pixels, dy - in lines.
2622 static BOOL EDIT_EM_LineScroll_internal(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2625 INT x_offset_in_pixels;
2627 if (es->style & ES_MULTILINE)
2629 x_offset_in_pixels = es->x_offset;
2634 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->x_offset, FALSE));
2637 if (-dx > x_offset_in_pixels)
2638 dx = -x_offset_in_pixels;
2639 if (dx > es->text_width - x_offset_in_pixels)
2640 dx = es->text_width - x_offset_in_pixels;
2641 nyoff = max(0, es->y_offset + dy);
2642 if (nyoff >= es->line_count)
2643 nyoff = es->line_count - 1;
2644 dy = (es->y_offset - nyoff) * es->line_height;
2649 es->y_offset = nyoff;
2650 if(es->style & ES_MULTILINE)
2653 es->x_offset += dx / es->char_width;
2655 GetClientRect(wnd->hwndSelf, &rc1);
2656 IntersectRect(&rc, &rc1, &es->format_rect);
2657 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2658 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2660 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2661 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2662 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2663 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2668 /*********************************************************************
2673 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap)
2675 INT len = strlenW(es->text);
2684 index = min(index, len);
2685 dc = GetDC(wnd->hwndSelf);
2687 old_font = SelectObject(dc, es->font);
2688 if (es->style & ES_MULTILINE) {
2689 l = EDIT_EM_LineFromChar(es, index);
2690 y = (l - es->y_offset) * es->line_height;
2691 li = EDIT_EM_LineIndex(es, l);
2692 if (after_wrap && (li == index) && l) {
2694 LINEDEF *line_def = es->first_line_def;
2696 line_def = line_def->next;
2699 if (line_def->ending == END_WRAP) {
2701 y -= es->line_height;
2702 li = EDIT_EM_LineIndex(es, l);
2705 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2706 es->tabs_count, es->tabs)) - es->x_offset;
2708 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2709 if (index < es->x_offset) {
2710 GetTextExtentPoint32W(dc, text + index,
2711 es->x_offset - index, &size);
2714 GetTextExtentPoint32W(dc, text + es->x_offset,
2715 index - es->x_offset, &size);
2719 if (es->style & ES_PASSWORD)
2720 HeapFree(GetProcessHeap(), 0, text);
2722 x += es->format_rect.left;
2723 y += es->format_rect.top;
2725 SelectObject(dc, old_font);
2726 ReleaseDC(wnd->hwndSelf, dc);
2727 return MAKELONG((INT16)x, (INT16)y);
2731 /*********************************************************************
2735 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2738 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
2740 UINT strl = strlenW(lpsz_replace);
2741 UINT tl = strlenW(es->text);
2748 TRACE("%s, can_undo %d, send_update %d\n",
2749 debugstr_w(lpsz_replace), can_undo, send_update);
2751 s = es->selection_start;
2752 e = es->selection_end;
2754 if ((s == e) && !strl)
2759 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2763 /* there is something to be deleted */
2765 utl = strlenW(es->undo_text);
2766 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2767 /* undo-buffer is extended to the right */
2768 EDIT_MakeUndoFit(es, utl + e - s);
2769 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2770 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2771 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2772 /* undo-buffer is extended to the left */
2773 EDIT_MakeUndoFit(es, utl + e - s);
2774 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2776 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2777 p[i] = (es->text + s)[i];
2778 es->undo_position = s;
2780 /* new undo-buffer */
2781 EDIT_MakeUndoFit(es, e - s);
2782 strncpyW(es->undo_text, es->text + s, e - s + 1);
2783 es->undo_text[e - s] = 0; /* ensure 0 termination */
2784 es->undo_position = s;
2786 /* any deletion makes the old insertion-undo invalid */
2787 es->undo_insert_count = 0;
2789 EDIT_EM_EmptyUndoBuffer(es);
2792 strcpyW(es->text + s, es->text + e);
2795 /* there is an insertion */
2797 if ((s == es->undo_position) ||
2798 ((es->undo_insert_count) &&
2799 (s == es->undo_position + es->undo_insert_count)))
2801 * insertion is new and at delete position or
2802 * an extension to either left or right
2804 es->undo_insert_count += strl;
2806 /* new insertion undo */
2807 es->undo_position = s;
2808 es->undo_insert_count = strl;
2809 /* new insertion makes old delete-buffer invalid */
2810 *es->undo_text = '\0';
2813 EDIT_EM_EmptyUndoBuffer(es);
2816 tl = strlenW(es->text);
2817 for (p = es->text + tl ; p >= es->text + s ; p--)
2819 for (i = 0 , p = es->text + s ; i < strl ; i++)
2820 p[i] = lpsz_replace[i];
2821 if(es->style & ES_UPPERCASE)
2822 CharUpperBuffW(p, strl);
2823 else if(es->style & ES_LOWERCASE)
2824 CharLowerBuffW(p, strl);
2827 /* FIXME: really inefficient */
2828 if (es->style & ES_MULTILINE)
2829 EDIT_BuildLineDefs_ML(wnd, es);
2831 EDIT_CalcLineWidth_SL(wnd, es);
2833 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2834 es->flags |= EF_MODIFIED;
2835 if (send_update) es->flags |= EF_UPDATE;
2836 EDIT_EM_ScrollCaret(wnd, es);
2838 /* FIXME: really inefficient */
2839 EDIT_UpdateText(wnd, NULL, TRUE);
2843 /*********************************************************************
2848 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
2852 if (!(es->style & ES_MULTILINE))
2853 return (LRESULT)FALSE;
2863 if (es->y_offset < es->line_count - 1)
2868 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2871 if (es->y_offset < es->line_count - 1)
2872 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2875 return (LRESULT)FALSE;
2878 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2879 /* check if we are going to move too far */
2880 if(es->y_offset + dy > es->line_count - vlc)
2881 dy = es->line_count - vlc - es->y_offset;
2883 /* Notification is done in EDIT_EM_LineScroll */
2885 EDIT_EM_LineScroll(wnd, es, 0, dy);
2887 return MAKELONG((INT16)dy, (BOOL16)TRUE);
2891 /*********************************************************************
2896 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
2898 if (es->style & ES_MULTILINE) {
2903 INT cw = es->char_width;
2908 l = EDIT_EM_LineFromChar(es, es->selection_end);
2909 li = EDIT_EM_LineIndex(es, l);
2910 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
2911 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2912 if (l >= es->y_offset + vlc)
2913 dy = l - vlc + 1 - es->y_offset;
2914 if (l < es->y_offset)
2915 dy = l - es->y_offset;
2916 ww = es->format_rect.right - es->format_rect.left;
2917 if (x < es->format_rect.left)
2918 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
2919 if (x > es->format_rect.right)
2920 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
2923 /* check if we are going to move too far */
2924 if(es->x_offset + dx + ww > es->text_width)
2925 dx = es->text_width - ww - es->x_offset;
2927 EDIT_EM_LineScroll_internal(wnd, es, dx, dy);
2934 if (!(es->style & ES_AUTOHSCROLL))
2937 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2938 format_width = es->format_rect.right - es->format_rect.left;
2939 if (x < es->format_rect.left) {
2940 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
2943 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2944 } while ((x < goal) && es->x_offset);
2945 /* FIXME: use ScrollWindow() somehow to improve performance */
2946 EDIT_UpdateText(wnd, NULL, TRUE);
2947 } else if (x > es->format_rect.right) {
2949 INT len = strlenW(es->text);
2950 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
2953 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2954 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
2955 } while ((x > goal) && (x_last > es->format_rect.right));
2956 /* FIXME: use ScrollWindow() somehow to improve performance */
2957 EDIT_UpdateText(wnd, NULL, TRUE);
2961 if(es->flags & EF_FOCUSED)
2962 EDIT_SetCaretPos(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
2966 /*********************************************************************
2970 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2973 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc)
2975 if (!(es->style & ES_MULTILINE))
2979 WARN("called with NULL handle\n");
2983 EDIT_UnlockBuffer(wnd, es, TRUE);
2987 LOCAL_Free(wnd->hInstance, es->hloc16);
2988 es->hloc16 = (HLOCAL16)NULL;
2995 LocalFree(es->hloc32A);
2996 es->hloc32A = (HLOCAL)NULL;
3007 countA = LocalSize(hloc);
3008 textA = LocalLock(hloc);
3009 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3010 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3012 ERR("Could not allocate new unicode buffer\n");
3015 textW = LocalLock(hloc32W_new);
3016 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3017 LocalUnlock(hloc32W_new);
3021 LocalFree(es->hloc32W);
3023 es->hloc32W = hloc32W_new;
3027 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3029 EDIT_LockBuffer(wnd, es);
3031 es->x_offset = es->y_offset = 0;
3032 es->selection_start = es->selection_end = 0;
3033 EDIT_EM_EmptyUndoBuffer(es);
3034 es->flags &= ~EF_MODIFIED;
3035 es->flags &= ~EF_UPDATE;
3036 EDIT_BuildLineDefs_ML(wnd, es);
3037 EDIT_UpdateText(wnd, NULL, TRUE);
3038 EDIT_EM_ScrollCaret(wnd, es);
3042 /*********************************************************************
3046 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3049 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
3056 if (!(es->style & ES_MULTILINE))
3060 WARN("called with NULL handle\n");
3064 EDIT_UnlockBuffer(wnd, es, TRUE);
3068 LocalFree(es->hloc32A);
3069 es->hloc32A = (HLOCAL)NULL;
3072 countA = LOCAL_Size(wnd->hInstance, hloc);
3073 textA = LOCAL_Lock(wnd->hInstance, hloc);
3074 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3075 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3077 ERR("Could not allocate new unicode buffer\n");
3080 textW = LocalLock(hloc32W_new);
3081 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3082 LocalUnlock(hloc32W_new);
3083 LOCAL_Unlock(wnd->hInstance, hloc);
3086 LocalFree(es->hloc32W);
3088 es->hloc32W = hloc32W_new;
3091 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3093 EDIT_LockBuffer(wnd, es);
3095 es->x_offset = es->y_offset = 0;
3096 es->selection_start = es->selection_end = 0;
3097 EDIT_EM_EmptyUndoBuffer(es);
3098 es->flags &= ~EF_MODIFIED;
3099 es->flags &= ~EF_UPDATE;
3100 EDIT_BuildLineDefs_ML(wnd, es);
3101 EDIT_UpdateText(wnd, NULL, TRUE);
3102 EDIT_EM_ScrollCaret(wnd, es);
3106 /*********************************************************************
3110 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3111 * However, the windows version is not complied to yet in all of edit.c
3114 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3116 if (es->style & ES_MULTILINE) {
3118 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3120 es->buffer_limit = BUFLIMIT_MULTI;
3123 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3125 es->buffer_limit = BUFLIMIT_SINGLE;
3130 /*********************************************************************
3134 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3135 * action wParam despite what the docs say. EC_USEFONTINFO means one third
3136 * of the char's width, according to the new docs.
3139 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3140 INT left, INT right)
3142 if (action & EC_LEFTMARGIN) {
3143 if (left != EC_USEFONTINFO)
3144 es->left_margin = left;
3146 es->left_margin = es->char_width / 3;
3149 if (action & EC_RIGHTMARGIN) {
3150 if (right != EC_USEFONTINFO)
3151 es->right_margin = right;
3153 es->right_margin = es->char_width / 3;
3155 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3159 /*********************************************************************
3161 * EM_SETPASSWORDCHAR
3164 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, WCHAR c)
3166 if (es->style & ES_MULTILINE)
3169 if (es->password_char == c)
3172 es->password_char = c;
3174 wnd->dwStyle |= ES_PASSWORD;
3175 es->style |= ES_PASSWORD;
3177 wnd->dwStyle &= ~ES_PASSWORD;
3178 es->style &= ~ES_PASSWORD;
3180 EDIT_UpdateText(wnd, NULL, TRUE);
3184 /*********************************************************************
3188 * note: unlike the specs say: the order of start and end
3189 * _is_ preserved in Windows. (i.e. start can be > end)
3190 * In other words: this handler is OK
3193 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3195 UINT old_start = es->selection_start;
3196 UINT old_end = es->selection_end;
3197 UINT len = strlenW(es->text);
3199 if (start == (UINT)-1) {
3200 start = es->selection_end;
3201 end = es->selection_end;
3203 start = min(start, len);
3204 end = min(end, len);
3206 es->selection_start = start;
3207 es->selection_end = end;
3209 es->flags |= EF_AFTER_WRAP;
3211 es->flags &= ~EF_AFTER_WRAP;
3212 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3213 ORDER_UINT(start, end);
3214 ORDER_UINT(end, old_end);
3215 ORDER_UINT(start, old_start);
3216 ORDER_UINT(old_start, old_end);
3217 if (end != old_start)
3221 * ORDER_UINT32(end, old_start);
3222 * EDIT_InvalidateText(wnd, es, start, end);
3223 * EDIT_InvalidateText(wnd, es, old_start, old_end);
3224 * in place of the following if statement.
3226 if (old_start > end )
3228 EDIT_InvalidateText(wnd, es, start, end);
3229 EDIT_InvalidateText(wnd, es, old_start, old_end);
3233 EDIT_InvalidateText(wnd, es, start, old_start);
3234 EDIT_InvalidateText(wnd, es, end, old_end);
3237 else EDIT_InvalidateText(wnd, es, start, old_end);
3241 /*********************************************************************
3246 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3248 if (!(es->style & ES_MULTILINE))
3251 HeapFree(GetProcessHeap(), 0, es->tabs);
3252 es->tabs_count = count;
3256 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3257 memcpy(es->tabs, tabs, count * sizeof(INT));
3263 /*********************************************************************
3268 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3270 if (!(es->style & ES_MULTILINE))
3273 HeapFree(GetProcessHeap(), 0, es->tabs);
3274 es->tabs_count = count;
3279 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3280 for (i = 0 ; i < count ; i++)
3281 es->tabs[i] = *tabs++;
3287 /*********************************************************************
3289 * EM_SETWORDBREAKPROC
3292 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, LPARAM lParam)
3294 if (es->word_break_proc == (void *)lParam)
3297 es->word_break_proc = (void *)lParam;
3298 es->word_break_proc16 = NULL;
3300 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3301 EDIT_BuildLineDefs_ML(wnd, es);
3302 EDIT_UpdateText(wnd, NULL, TRUE);
3307 /*********************************************************************
3309 * EM_SETWORDBREAKPROC16
3312 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3314 if (es->word_break_proc16 == wbp)
3317 es->word_break_proc = NULL;
3318 es->word_break_proc16 = wbp;
3319 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3320 EDIT_BuildLineDefs_ML(wnd, es);
3321 EDIT_UpdateText(wnd, NULL, TRUE);
3326 /*********************************************************************
3331 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
3333 INT ulength = strlenW(es->undo_text);
3334 LPWSTR utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3336 strcpyW(utext, es->undo_text);
3338 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3339 es->undo_insert_count, debugstr_w(utext));
3341 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3342 EDIT_EM_EmptyUndoBuffer(es);
3343 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext, TRUE);
3344 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3345 EDIT_EM_ScrollCaret(wnd, es);
3346 HeapFree(GetProcessHeap(), 0, utext);
3348 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3349 es->undo_insert_count, debugstr_w(es->undo_text));
3351 if (es->flags & EF_UPDATE) {
3352 es->flags &= ~EF_UPDATE;
3353 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3360 /*********************************************************************
3365 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, WCHAR c)
3367 BOOL control = GetKeyState(VK_CONTROL) & 0x8000;
3370 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3371 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3374 if (es->style & ES_MULTILINE) {
3375 if (es->style & ES_READONLY) {
3376 EDIT_MoveHome(wnd, es, FALSE);
3377 EDIT_MoveDown_ML(wnd, es, FALSE);
3379 static const WCHAR cr_lfW[] = {'\r','\n',0};
3380 EDIT_EM_ReplaceSel(wnd, es, TRUE, cr_lfW, TRUE);
3381 if (es->flags & EF_UPDATE) {
3382 es->flags &= ~EF_UPDATE;
3383 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3389 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3391 static const WCHAR tabW[] = {'\t',0};
3392 EDIT_EM_ReplaceSel(wnd, es, TRUE, tabW, TRUE);
3393 if (es->flags & EF_UPDATE) {
3394 es->flags &= ~EF_UPDATE;
3395 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3400 if (!(es->style & ES_READONLY) && !control) {
3401 if (es->selection_start != es->selection_end)
3402 EDIT_WM_Clear(wnd, es);
3404 /* delete character left of caret */
3405 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3406 EDIT_MoveBackward(wnd, es, TRUE);
3407 EDIT_WM_Clear(wnd, es);
3412 SendMessageW(wnd->hwndSelf, WM_COPY, 0, 0);
3415 SendMessageW(wnd->hwndSelf, WM_PASTE, 0, 0);
3418 SendMessageW(wnd->hwndSelf, WM_CUT, 0, 0);
3422 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3426 EDIT_EM_ReplaceSel(wnd, es, TRUE, str, TRUE);
3427 if (es->flags & EF_UPDATE) {
3428 es->flags &= ~EF_UPDATE;
3429 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3437 /*********************************************************************
3442 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND control)
3444 if (code || control)
3449 EDIT_EM_Undo(wnd, es);
3452 EDIT_WM_Cut(wnd, es);
3455 EDIT_WM_Copy(wnd, es);
3458 EDIT_WM_Paste(wnd, es);
3461 EDIT_WM_Clear(wnd, es);
3464 EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
3465 EDIT_EM_ScrollCaret(wnd, es);
3468 ERR("unknown menu item, please report\n");
3474 /*********************************************************************
3478 * Note: the resource files resource/sysres_??.rc cannot define a
3479 * single popup menu. Hence we use a (dummy) menubar
3480 * containing the single popup menu as its first item.
3482 * FIXME: the message identifiers have been chosen arbitrarily,
3483 * hence we use MF_BYPOSITION.
3484 * We might as well use the "real" values (anybody knows ?)
3485 * The menu definition is in resources/sysres_??.rc.
3486 * Once these are OK, we better use MF_BYCOMMAND here
3487 * (as we do in EDIT_WM_Command()).
3490 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, INT x, INT y)
3492 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3493 HMENU popup = GetSubMenu(menu, 0);
3494 UINT start = es->selection_start;
3495 UINT end = es->selection_end;
3497 ORDER_UINT(start, end);
3500 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) ? MF_ENABLED : MF_GRAYED));
3502 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3504 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3506 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) ? MF_ENABLED : MF_GRAYED));
3508 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
3510 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3512 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
3517 /*********************************************************************
3522 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
3524 INT s = es->selection_start;
3525 INT e = es->selection_end;
3532 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3533 dst = GlobalLock(hdst);
3534 strncpyW(dst, es->text + s, e - s);
3535 dst[e - s] = 0; /* ensure 0 termination */
3536 TRACE("%s\n", debugstr_w(dst));
3538 OpenClipboard(wnd->hwndSelf);
3540 SetClipboardData(CF_UNICODETEXT, hdst);
3545 /*********************************************************************
3550 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCWSTR name)
3552 TRACE("%s\n", debugstr_w(name));
3554 * To initialize some final structure members, we call some helper
3555 * functions. However, since the EDITSTATE is not consistent (i.e.
3556 * not fully initialized), we should be very careful which
3557 * functions can be called, and in what order.
3559 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3560 EDIT_EM_EmptyUndoBuffer(es);
3562 if (name && *name) {
3563 EDIT_EM_ReplaceSel(wnd, es, FALSE, name, TRUE);
3564 /* if we insert text to the editline, the text scrolls out
3565 * of the window, as the caret is placed after the insert
3566 * pos normally; thus we reset es->selection... to 0 and
3569 es->selection_start = es->selection_end = 0;
3570 EDIT_EM_ScrollCaret(wnd, es);
3571 if (es->flags & EF_UPDATE) {
3572 es->flags &= ~EF_UPDATE;
3573 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3580 /*********************************************************************
3585 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3588 while (LocalUnlock(es->hloc32W)) ;
3589 LocalFree(es->hloc32W);
3592 while (LocalUnlock(es->hloc32A)) ;
3593 LocalFree(es->hloc32A);
3596 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3597 LOCAL_Free(wnd->hInstance, es->hloc16);
3599 HeapFree(GetProcessHeap(), 0, es);
3600 *(EDITSTATE **)wnd->wExtra = NULL;
3604 /*********************************************************************
3609 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3614 if ( get_app_version() >= 0x40000 &&(
3615 !es->bEnableState || (es->style & ES_READONLY)))
3616 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3618 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc);
3621 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3623 GetClientRect(wnd->hwndSelf, &rc);
3624 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3625 GetClipBox(dc, &rc);
3627 * FIXME: specs say that we should UnrealizeObject() the brush,
3628 * but the specs of UnrealizeObject() say that we shouldn't
3629 * unrealize a stock object. The default brush that
3630 * DefWndProc() returns is ... a stock object.
3632 FillRect(dc, &rc, brush);
3637 /*********************************************************************
3642 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3644 if(!count) return 0;
3648 LPWSTR textW = (LPWSTR)lParam;
3649 strncpyW(textW, es->text, count);
3650 textW[count - 1] = 0; /* ensure 0 termination */
3651 return strlenW(textW);
3655 LPSTR textA = (LPSTR)lParam;
3656 INT ret = WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, count, NULL, NULL);
3657 textA[count - 1] = 0; /* ensure 0 termination */
3663 /*********************************************************************
3667 * 16 bit notepad needs this. Actually it is not _our_ hack,
3668 * it is notepad's. Notepad is sending us scrollbar messages with
3669 * undocumented parameters without us even having a scrollbar ... !?!?
3672 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos)
3675 INT fw = es->format_rect.right - es->format_rect.left;
3678 if (!(es->flags & EF_HSCROLL_HACK)) {
3679 ERR("hacked WM_HSCROLL handler invoked\n");
3680 ERR(" if you are _not_ running 16 bit notepad, please report\n");
3681 ERR(" (this message is only displayed once per edit control)\n");
3682 es->flags |= EF_HSCROLL_HACK;
3688 dx = -es->char_width;
3691 if (es->x_offset < es->text_width)
3692 dx = es->char_width;
3696 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3699 if (es->x_offset < es->text_width)
3700 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3707 if (es->x_offset < es->text_width)
3708 dx = es->text_width - es->x_offset;
3711 es->flags |= EF_HSCROLL_TRACK;
3712 dx = pos * es->text_width / 100 - es->x_offset;
3714 case SB_THUMBPOSITION:
3715 es->flags &= ~EF_HSCROLL_TRACK;
3716 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3717 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3723 * FIXME : the next two are undocumented !
3724 * Are we doing the right thing ?
3725 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3726 * although it's also a regular control message.
3729 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3731 case EM_LINESCROLL16:
3736 ERR("undocumented (hacked) WM_HSCROLL parameter, please report\n");
3741 INT fw = es->format_rect.right - es->format_rect.left;
3742 /* check if we are going to move too far */
3743 if(es->x_offset + dx + fw > es->text_width)
3744 dx = es->text_width - fw - es->x_offset;
3746 EDIT_EM_LineScroll_internal(wnd, es, dx, 0);
3752 /*********************************************************************
3757 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
3762 if (!(es->style & ES_MULTILINE))
3765 if (!(es->style & ES_AUTOHSCROLL))
3768 if (!(es->style & WS_HSCROLL))
3769 return EDIT_HScroll_Hack(wnd, es, action, pos);
3772 fw = es->format_rect.right - es->format_rect.left;
3776 dx = -es->char_width;
3779 if (es->x_offset < es->text_width)
3780 dx = es->char_width;
3784 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3787 if (es->x_offset < es->text_width)
3788 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3795 if (es->x_offset < es->text_width)
3796 dx = es->text_width - es->x_offset;
3799 es->flags |= EF_HSCROLL_TRACK;
3800 dx = pos - es->x_offset;
3802 case SB_THUMBPOSITION:
3803 es->flags &= ~EF_HSCROLL_TRACK;
3804 if (!(dx = pos - es->x_offset)) {
3805 SetScrollPos(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3806 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3813 ERR("undocumented WM_HSCROLL parameter, please report\n");
3818 INT fw = es->format_rect.right - es->format_rect.left;
3819 /* check if we are going to move too far */
3820 if(es->x_offset + dx + fw > es->text_width)
3821 dx = es->text_width - fw - es->x_offset;
3823 EDIT_EM_LineScroll_internal(wnd, es, dx, 0);
3829 /*********************************************************************
3834 static BOOL EDIT_CheckCombo(WND *wnd, EDITSTATE *es, UINT msg, INT key)
3836 HWND hLBox = es->hwndListBox;
3844 hCombo = wnd->parent->hwndSelf;
3848 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
3849 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3851 if (key == VK_UP || key == VK_DOWN)
3853 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3856 if (msg == WM_KEYDOWN || nEUI)
3857 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3863 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3865 /* make sure ComboLBox pops up */
3866 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3871 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3874 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3876 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3878 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
3883 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3889 /*********************************************************************
3893 * Handling of special keys that don't produce a WM_CHAR
3894 * (i.e. non-printable keys) & Backspace & Delete
3897 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key)
3902 if (GetKeyState(VK_MENU) & 0x8000)
3905 shift = GetKeyState(VK_SHIFT) & 0x8000;
3906 control = GetKeyState(VK_CONTROL) & 0x8000;
3911 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key) || key == VK_F4)
3916 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3917 EDIT_MoveUp_ML(wnd, es, shift);
3920 EDIT_MoveWordBackward(wnd, es, shift);
3922 EDIT_MoveBackward(wnd, es, shift);
3925 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key))
3929 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3930 EDIT_MoveDown_ML(wnd, es, shift);
3932 EDIT_MoveWordForward(wnd, es, shift);
3934 EDIT_MoveForward(wnd, es, shift);
3937 EDIT_MoveHome(wnd, es, shift);
3940 EDIT_MoveEnd(wnd, es, shift);
3943 if (es->style & ES_MULTILINE)
3944 EDIT_MovePageUp_ML(wnd, es, shift);
3946 EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
3949 if (es->style & ES_MULTILINE)
3950 EDIT_MovePageDown_ML(wnd, es, shift);
3952 EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
3955 if (!(es->style & ES_READONLY) && !(shift && control)) {
3956 if (es->selection_start != es->selection_end) {
3958 EDIT_WM_Cut(wnd, es);
3960 EDIT_WM_Clear(wnd, es);
3963 /* delete character left of caret */
3964 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3965 EDIT_MoveBackward(wnd, es, TRUE);
3966 EDIT_WM_Clear(wnd, es);
3967 } else if (control) {
3968 /* delete to end of line */
3969 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3970 EDIT_MoveEnd(wnd, es, TRUE);
3971 EDIT_WM_Clear(wnd, es);
3973 /* delete character right of caret */
3974 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3975 EDIT_MoveForward(wnd, es, TRUE);
3976 EDIT_WM_Clear(wnd, es);
3983 if (!(es->style & ES_READONLY))
3984 EDIT_WM_Paste(wnd, es);
3986 EDIT_WM_Copy(wnd, es);
3989 /* If the edit doesn't want the return send a message to the default object */
3990 if(!(es->style & ES_WANTRETURN))
3992 HWND hwndParent = GetParent(wnd->hwndSelf);
3993 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
3994 if (HIWORD(dw) == DC_HASDEFID)
3996 SendMessageW( hwndParent, WM_COMMAND,
3997 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
3998 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4007 /*********************************************************************
4012 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es)
4014 es->flags &= ~EF_FOCUSED;
4016 if(!(es->style & ES_NOHIDESEL))
4017 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
4018 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
4023 /*********************************************************************
4027 * The caret position has been set on the WM_LBUTTONDOWN message
4030 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es)
4033 INT e = es->selection_end;
4038 if (!(es->flags & EF_FOCUSED))
4041 l = EDIT_EM_LineFromChar(es, e);
4042 li = EDIT_EM_LineIndex(es, l);
4043 ll = EDIT_EM_LineLength(es, e);
4044 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4045 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4046 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
4047 EDIT_EM_ScrollCaret(wnd, es);
4052 /*********************************************************************
4057 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
4062 if (!(es->flags & EF_FOCUSED))
4065 es->bCaptureState = TRUE;
4066 SetCapture(wnd->hwndSelf);
4067 EDIT_ConfinePoint(es, &x, &y);
4068 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
4069 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4070 EDIT_EM_ScrollCaret(wnd, es);
4071 es->region_posx = es->region_posy = 0;
4072 SetTimer(wnd->hwndSelf, 0, 100, NULL);
4077 /*********************************************************************
4082 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es)
4084 if (es->bCaptureState && GetCapture() == hwndSelf) {
4085 KillTimer(hwndSelf, 0);
4088 es->bCaptureState = FALSE;
4093 /*********************************************************************
4098 static LRESULT EDIT_WM_MButtonDown(WND *wnd)
4100 SendMessageW(wnd->hwndSelf,WM_PASTE,0,0);
4105 /*********************************************************************
4110 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, INT x, INT y)
4116 if (GetCapture() != wnd->hwndSelf)
4120 * FIXME: gotta do some scrolling if outside client
4121 * area. Maybe reset the timer ?
4124 EDIT_ConfinePoint(es, &x, &y);
4125 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4126 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4127 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
4128 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
4133 /*********************************************************************
4138 static LRESULT EDIT_WM_NCCreate(WND *wnd, DWORD style, HWND hwndParent, BOOL unicode)
4143 TRACE("Creating %s edit control\n", unicode ? "Unicode" : "ANSI");
4145 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4147 *(EDITSTATE **)wnd->wExtra = es;
4150 * Note: since the EDITSTATE has not been fully initialized yet,
4151 * we can't use any API calls that may send
4152 * WM_XXX messages before WM_NCCREATE is completed.
4155 es->is_unicode = unicode;
4158 es->bEnableState = !(style & WS_DISABLED);
4161 * In Win95 look and feel, the WS_BORDER style is replaced by the
4162 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4163 * control a non client area.
4165 if (TWEAK_WineLook != WIN31_LOOK)
4167 if (es->style & WS_BORDER)
4169 es->style &= ~WS_BORDER;
4170 wnd->dwStyle &= ~WS_BORDER;
4171 wnd->dwExStyle |= WS_EX_CLIENTEDGE;
4176 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4177 wnd->dwStyle &= ~WS_BORDER;
4180 if (es->style & ES_COMBO)
4181 es->hwndListBox = GetDlgItem(hwndParent, ID_CB_LISTBOX);
4183 if (es->style & ES_MULTILINE) {
4184 es->buffer_limit = BUFLIMIT_MULTI;
4185 if (es->style & WS_VSCROLL)
4186 es->style |= ES_AUTOVSCROLL;
4187 if (es->style & WS_HSCROLL)
4188 es->style |= ES_AUTOHSCROLL;
4189 es->style &= ~ES_PASSWORD;
4190 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4191 if (es->style & ES_RIGHT)
4192 es->style &= ~ES_CENTER;
4193 es->style &= ~WS_HSCROLL;
4194 es->style &= ~ES_AUTOHSCROLL;
4197 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4198 es->style |= ES_AUTOVSCROLL;
4200 es->buffer_limit = BUFLIMIT_SINGLE;
4201 es->style &= ~ES_CENTER;
4202 es->style &= ~ES_RIGHT;
4203 es->style &= ~WS_HSCROLL;
4204 es->style &= ~WS_VSCROLL;
4205 es->style &= ~ES_AUTOVSCROLL;
4206 es->style &= ~ES_WANTRETURN;
4207 if (es->style & ES_UPPERCASE) {
4208 es->style &= ~ES_LOWERCASE;
4209 es->style &= ~ES_NUMBER;
4210 } else if (es->style & ES_LOWERCASE)
4211 es->style &= ~ES_NUMBER;
4212 if (es->style & ES_PASSWORD)
4213 es->password_char = '*';
4215 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4216 es->style |= ES_AUTOHSCROLL;
4219 alloc_size = ((es->buffer_size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
4220 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4222 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4224 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4226 es->undo_buffer_size = es->buffer_size;
4228 if (es->style & ES_MULTILINE)
4229 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4236 /*********************************************************************
4241 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam)
4250 BOOL rev = es->bEnableState &&
4251 ((es->flags & EF_FOCUSED) ||
4252 (es->style & ES_NOHIDESEL));
4254 dc = BeginPaint(wnd->hwndSelf, &ps);
4257 if(es->style & WS_BORDER) {
4258 GetClientRect(wnd->hwndSelf, &rc);
4259 if(es->style & ES_MULTILINE) {
4260 if(es->style & WS_HSCROLL) rc.bottom++;
4261 if(es->style & WS_VSCROLL) rc.right++;
4263 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4265 IntersectClipRect(dc, es->format_rect.left,
4266 es->format_rect.top,
4267 es->format_rect.right,
4268 es->format_rect.bottom);
4269 if (es->style & ES_MULTILINE) {
4270 GetClientRect(wnd->hwndSelf, &rc);
4271 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4274 old_font = SelectObject(dc, es->font);
4275 if ( get_app_version() >= 0x40000 &&(
4276 !es->bEnableState || (es->style & ES_READONLY)))
4277 EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
4279 EDIT_SEND_CTLCOLOR(wnd, dc);
4281 if (!es->bEnableState)
4282 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4283 GetClipBox(dc, &rcRgn);
4284 if (es->style & ES_MULTILINE) {
4285 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4286 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4287 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
4288 if (IntersectRect(&rc, &rcRgn, &rcLine))
4289 EDIT_PaintLine(wnd, es, dc, i, rev);
4292 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
4293 if (IntersectRect(&rc, &rcRgn, &rcLine))
4294 EDIT_PaintLine(wnd, es, dc, 0, rev);
4297 SelectObject(dc, old_font);
4300 EndPaint(wnd->hwndSelf, &ps);
4302 EDIT_UpdateScrollInfo(wnd, es);
4306 /*********************************************************************
4311 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
4316 OpenClipboard(wnd->hwndSelf);
4317 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4318 src = (LPWSTR)GlobalLock(hsrc);
4319 EDIT_EM_ReplaceSel(wnd, es, TRUE, src, TRUE);
4322 if (es->flags & EF_UPDATE) {
4323 es->flags &= ~EF_UPDATE;
4324 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
4331 /*********************************************************************
4336 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es)
4338 es->flags |= EF_FOCUSED;
4339 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4340 EDIT_SetCaretPos(wnd, es, es->selection_end,
4341 es->flags & EF_AFTER_WRAP);
4342 if(!(es->style & ES_NOHIDESEL))
4343 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
4344 ShowCaret(wnd->hwndSelf);
4345 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
4349 /*********************************************************************
4353 * With Win95 look the margins are set to default font value unless
4354 * the system font (font == 0) is being set, in which case they are left
4358 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
4366 dc = GetDC(wnd->hwndSelf);
4368 old_font = SelectObject(dc, font);
4369 GetTextMetricsW(dc, &tm);
4370 es->line_height = tm.tmHeight;
4371 es->char_width = tm.tmAveCharWidth;
4373 SelectObject(dc, old_font);
4374 ReleaseDC(wnd->hwndSelf, dc);
4375 if (font && (TWEAK_WineLook > WIN31_LOOK))
4376 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4377 EC_USEFONTINFO, EC_USEFONTINFO);
4379 /* Force the recalculation of the format rect for each font change */
4380 GetClientRect(wnd->hwndSelf, &r);
4381 EDIT_SetRectNP(wnd, es, &r);
4383 if (es->style & ES_MULTILINE)
4384 EDIT_BuildLineDefs_ML(wnd, es);
4386 EDIT_CalcLineWidth_SL(wnd, es);
4389 EDIT_UpdateText(wnd, NULL, TRUE);
4390 if (es->flags & EF_FOCUSED) {
4392 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4393 EDIT_SetCaretPos(wnd, es, es->selection_end,
4394 es->flags & EF_AFTER_WRAP);
4395 ShowCaret(wnd->hwndSelf);
4400 /*********************************************************************
4405 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4406 * The modified flag is reset. No notifications are sent.
4408 * For single-line controls, reception of WM_SETTEXT triggers:
4409 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4412 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPARAM lParam, BOOL unicode)
4417 text = (LPWSTR)lParam;
4420 LPCSTR textA = (LPCSTR)lParam;
4421 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4422 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4423 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4426 EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
4428 TRACE("%s\n", debugstr_w(text));
4429 EDIT_EM_ReplaceSel(wnd, es, FALSE, text, !(es->style & ES_MULTILINE));
4431 HeapFree(GetProcessHeap(), 0, text);
4433 static const WCHAR empty_stringW[] = {0};
4435 EDIT_EM_ReplaceSel(wnd, es, FALSE, empty_stringW, !(es->style & ES_MULTILINE));
4438 es->flags &= ~EF_MODIFIED;
4439 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
4440 EDIT_EM_ScrollCaret(wnd, es);
4442 if (es->flags & EF_UPDATE) {
4443 es->flags &= ~EF_UPDATE;
4444 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
4449 /*********************************************************************
4454 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
4456 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4458 TRACE("width = %d, height = %d\n", width, height);
4459 SetRect(&rc, 0, 0, width, height);
4460 EDIT_SetRectNP(wnd, es, &rc);
4461 EDIT_UpdateText(wnd, NULL, TRUE);
4466 /*********************************************************************
4471 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
4473 if ((key == VK_BACK) && (key_data & 0x2000)) {
4474 if (EDIT_EM_CanUndo(es))
4475 EDIT_EM_Undo(wnd, es);
4477 } else if (key == VK_UP || key == VK_DOWN) {
4478 if (EDIT_CheckCombo(wnd, es, WM_SYSKEYDOWN, key))
4481 return DefWindowProcW(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4485 /*********************************************************************
4490 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es)
4492 if (es->region_posx < 0) {
4493 EDIT_MoveBackward(wnd, es, TRUE);
4494 } else if (es->region_posx > 0) {
4495 EDIT_MoveForward(wnd, es, TRUE);
4498 * FIXME: gotta do some vertical scrolling here, like
4499 * EDIT_EM_LineScroll(wnd, 0, 1);
4504 /*********************************************************************
4508 * 16 bit notepad needs this. Actually it is not _our_ hack,
4509 * it is notepad's. Notepad is sending us scrollbar messages with
4510 * undocumented parameters without us even having a scrollbar ... !?!?
4513 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos)
4518 if (!(es->flags & EF_VSCROLL_HACK)) {
4519 ERR("hacked WM_VSCROLL handler invoked\n");
4520 ERR(" if you are _not_ running 16 bit notepad, please report\n");
4521 ERR(" (this message is only displayed once per edit control)\n");
4522 es->flags |= EF_VSCROLL_HACK;
4530 EDIT_EM_Scroll(wnd, es, action);
4536 dy = es->line_count - 1 - es->y_offset;
4539 es->flags |= EF_VSCROLL_TRACK;
4540 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
4542 case SB_THUMBPOSITION:
4543 es->flags &= ~EF_VSCROLL_TRACK;
4544 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
4545 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4551 * FIXME : the next two are undocumented !
4552 * Are we doing the right thing ?
4553 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4554 * although it's also a regular control message.
4557 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
4559 case EM_LINESCROLL16:
4564 ERR("undocumented (hacked) WM_VSCROLL parameter, please report\n");
4568 EDIT_EM_LineScroll(wnd, es, 0, dy);
4573 /*********************************************************************
4578 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
4582 if (!(es->style & ES_MULTILINE))
4585 if (!(es->style & ES_AUTOVSCROLL))
4588 if (!(es->style & WS_VSCROLL))
4589 return EDIT_VScroll_Hack(wnd, es, action, pos);
4597 EDIT_EM_Scroll(wnd, es, action);
4604 dy = es->line_count - 1 - es->y_offset;
4607 es->flags |= EF_VSCROLL_TRACK;
4608 dy = pos - es->y_offset;
4610 case SB_THUMBPOSITION:
4611 es->flags &= ~EF_VSCROLL_TRACK;
4612 if (!(dy = pos - es->y_offset)) {
4613 SetScrollPos(wnd->hwndSelf, SB_VERT, pos, TRUE);
4614 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4621 ERR("undocumented WM_VSCROLL action %d, please report\n",
4626 EDIT_EM_LineScroll(wnd, es, 0, dy);
4631 /*********************************************************************
4636 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase)
4638 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
4640 /* EF_UPDATE will be turned off in paint */
4641 if (es->flags & EF_UPDATE)
4642 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
4644 InvalidateRect(wnd->hwndSelf, rc, bErase);