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;
1492 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1493 textA = LocalLock(es->hloc32A);
1494 countA = strlen(textA) + 1;
1498 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1499 textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
1500 countA = strlen(textA) + 1;
1505 ERR("no buffer ... please report\n");
1512 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1513 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1514 if(countW_new > es->buffer_size + 1)
1516 UINT alloc_size = (countW_new * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
1517 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1518 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1521 es->hloc32W = hloc32W_new;
1522 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1523 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1526 WARN("FAILED! Will synchronize partially\n");
1530 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1531 es->text = LocalLock(es->hloc32W);
1535 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1537 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1539 LocalUnlock(es->hloc32A);
1546 /*********************************************************************
1548 * EDIT_SL_InvalidateText
1550 * Called from EDIT_InvalidateText().
1551 * Does the job for single-line controls only.
1554 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1559 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1560 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1561 EDIT_UpdateText(wnd, &rc, FALSE);
1565 /*********************************************************************
1567 * EDIT_ML_InvalidateText
1569 * Called from EDIT_InvalidateText().
1570 * Does the job for multi-line controls only.
1573 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1575 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1576 INT sl = EDIT_EM_LineFromChar(es, start);
1577 INT el = EDIT_EM_LineFromChar(es, end);
1586 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1589 sc = start - EDIT_EM_LineIndex(es, sl);
1590 ec = end - EDIT_EM_LineIndex(es, el);
1591 if (sl < es->y_offset) {
1595 if (el > es->y_offset + vlc) {
1596 el = es->y_offset + vlc;
1597 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1599 GetClientRect(wnd->hwndSelf, &rc1);
1600 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1602 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1603 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1604 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1606 EDIT_GetLineRect(wnd, es, sl, sc,
1607 EDIT_EM_LineLength(es,
1608 EDIT_EM_LineIndex(es, sl)),
1610 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1611 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1612 for (l = sl + 1 ; l < el ; l++) {
1613 EDIT_GetLineRect(wnd, es, l, 0,
1614 EDIT_EM_LineLength(es,
1615 EDIT_EM_LineIndex(es, l)),
1617 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1618 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1620 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1621 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1622 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1627 /*********************************************************************
1629 * EDIT_InvalidateText
1631 * Invalidate the text from offset start upto, but not including,
1632 * offset end. Useful for (re)painting the selection.
1633 * Regions outside the linewidth are not invalidated.
1634 * end == -1 means end == TextLength.
1635 * start and end need not be ordered.
1638 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1644 end = strlenW(es->text);
1646 ORDER_INT(start, end);
1648 if (es->style & ES_MULTILINE)
1649 EDIT_ML_InvalidateText(wnd, es, start, end);
1651 EDIT_SL_InvalidateText(wnd, es, start, end);
1655 /*********************************************************************
1659 * Try to fit size + 1 characters in the buffer. Constrain to limits.
1662 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, UINT size)
1666 if (size <= es->buffer_size)
1668 if (size > es->buffer_limit) {
1669 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1672 if (size > es->buffer_limit)
1673 size = es->buffer_limit;
1675 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1677 /* Force edit to unlock it's buffer. es->text now NULL */
1678 EDIT_UnlockBuffer(wnd, es, TRUE);
1681 UINT alloc_size = ((size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
1682 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1683 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32W, hNew32W);
1684 es->hloc32W = hNew32W;
1685 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1689 EDIT_LockBuffer(wnd, es);
1691 if (es->buffer_size < size) {
1692 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1693 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1696 TRACE("We now have %d+1\n", es->buffer_size);
1702 /*********************************************************************
1706 * Try to fit size + 1 bytes in the undo buffer.
1709 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1713 if (size <= es->undo_buffer_size)
1716 TRACE("trying to ReAlloc to %d+1\n", size);
1718 alloc_size = ((size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
1719 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1720 es->undo_buffer_size = alloc_size/sizeof(WCHAR);
1725 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1731 /*********************************************************************
1736 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1738 INT e = es->selection_end;
1742 if ((es->style & ES_MULTILINE) && e &&
1743 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1745 if (e && (es->text[e - 1] == '\r'))
1749 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1750 EDIT_EM_ScrollCaret(wnd, es);
1754 /*********************************************************************
1758 * Only for multi line controls
1759 * Move the caret one line down, on a column with the nearest
1760 * x coordinate on the screen (might be a different column).
1763 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1765 INT s = es->selection_start;
1766 INT e = es->selection_end;
1767 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1768 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1769 INT x = SLOWORD(pos);
1770 INT y = SHIWORD(pos);
1772 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1775 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1776 EDIT_EM_ScrollCaret(wnd, es);
1780 /*********************************************************************
1785 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1787 BOOL after_wrap = FALSE;
1790 /* Pass a high value in x to make sure of receiving the end of the line */
1791 if (es->style & ES_MULTILINE)
1792 e = EDIT_CharFromPos(wnd, es, 0x3fffffff,
1793 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1795 e = strlenW(es->text);
1796 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1797 EDIT_EM_ScrollCaret(wnd, es);
1801 /*********************************************************************
1806 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1808 INT e = es->selection_end;
1812 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1813 if (es->text[e] == '\n')
1815 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1819 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1820 EDIT_EM_ScrollCaret(wnd, es);
1824 /*********************************************************************
1828 * Home key: move to beginning of line.
1831 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
1835 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1836 if (es->style & ES_MULTILINE)
1837 e = EDIT_CharFromPos(wnd, es, -es->x_offset,
1838 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1841 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1842 EDIT_EM_ScrollCaret(wnd, es);
1846 /*********************************************************************
1848 * EDIT_MovePageDown_ML
1850 * Only for multi line controls
1851 * Move the caret one page down, on a column with the nearest
1852 * x coordinate on the screen (might be a different column).
1855 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1857 INT s = es->selection_start;
1858 INT e = es->selection_end;
1859 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1860 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1861 INT x = SLOWORD(pos);
1862 INT y = SHIWORD(pos);
1864 e = EDIT_CharFromPos(wnd, es, x,
1865 y + (es->format_rect.bottom - es->format_rect.top),
1869 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1870 EDIT_EM_ScrollCaret(wnd, es);
1874 /*********************************************************************
1876 * EDIT_MovePageUp_ML
1878 * Only for multi line controls
1879 * Move the caret one page up, on a column with the nearest
1880 * x coordinate on the screen (might be a different column).
1883 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1885 INT s = es->selection_start;
1886 INT e = es->selection_end;
1887 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1888 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1889 INT x = SLOWORD(pos);
1890 INT y = SHIWORD(pos);
1892 e = EDIT_CharFromPos(wnd, es, x,
1893 y - (es->format_rect.bottom - es->format_rect.top),
1897 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1898 EDIT_EM_ScrollCaret(wnd, es);
1902 /*********************************************************************
1906 * Only for multi line controls
1907 * Move the caret one line up, on a column with the nearest
1908 * x coordinate on the screen (might be a different column).
1911 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1913 INT s = es->selection_start;
1914 INT e = es->selection_end;
1915 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1916 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1917 INT x = SLOWORD(pos);
1918 INT y = SHIWORD(pos);
1920 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1923 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1924 EDIT_EM_ScrollCaret(wnd, es);
1928 /*********************************************************************
1930 * EDIT_MoveWordBackward
1933 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1935 INT s = es->selection_start;
1936 INT e = es->selection_end;
1941 l = EDIT_EM_LineFromChar(es, e);
1942 ll = EDIT_EM_LineLength(es, e);
1943 li = EDIT_EM_LineIndex(es, l);
1946 li = EDIT_EM_LineIndex(es, l - 1);
1947 e = li + EDIT_EM_LineLength(es, li);
1950 e = li + (INT)EDIT_CallWordBreakProc(es,
1951 li, e - li, ll, WB_LEFT);
1955 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1956 EDIT_EM_ScrollCaret(wnd, es);
1960 /*********************************************************************
1962 * EDIT_MoveWordForward
1965 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
1967 INT s = es->selection_start;
1968 INT e = es->selection_end;
1973 l = EDIT_EM_LineFromChar(es, e);
1974 ll = EDIT_EM_LineLength(es, e);
1975 li = EDIT_EM_LineIndex(es, l);
1977 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1978 e = EDIT_EM_LineIndex(es, l + 1);
1980 e = li + EDIT_CallWordBreakProc(es,
1981 li, e - li + 1, ll, WB_RIGHT);
1985 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1986 EDIT_EM_ScrollCaret(wnd, es);
1990 /*********************************************************************
1995 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
1997 INT s = es->selection_start;
1998 INT e = es->selection_end;
2005 if (es->style & ES_MULTILINE) {
2006 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2007 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2012 TRACE("line=%d\n", line);
2014 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(es, line), FALSE);
2017 li = EDIT_EM_LineIndex(es, line);
2018 ll = EDIT_EM_LineLength(es, li);
2019 s = es->selection_start;
2020 e = es->selection_end;
2022 s = min(li + ll, max(li, s));
2023 e = min(li + ll, max(li, e));
2024 if (rev && (s != e) &&
2025 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2026 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2027 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2028 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2030 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2034 /*********************************************************************
2039 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2049 BkColor = GetBkColor(dc);
2050 TextColor = GetTextColor(dc);
2052 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2053 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2055 li = EDIT_EM_LineIndex(es, line);
2056 if (es->style & ES_MULTILINE) {
2057 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2058 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2060 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2061 TextOutW(dc, x, y, text + li + col, count);
2062 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2064 if (es->style & ES_PASSWORD)
2065 HeapFree(GetProcessHeap(), 0, text);
2068 SetBkColor(dc, BkColor);
2069 SetTextColor(dc, TextColor);
2075 /*********************************************************************
2080 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
2083 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
2084 SetCaretPos(SLOWORD(res), SHIWORD(res));
2088 /*********************************************************************
2092 * note: this is not (exactly) the handler called on EM_SETRECTNP
2093 * it is also used to set the rect of a single line control
2096 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
2098 CopyRect(&es->format_rect, rc);
2099 if (es->style & WS_BORDER) {
2100 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2101 if(TWEAK_WineLook == WIN31_LOOK)
2103 es->format_rect.left += bw;
2104 es->format_rect.top += bw;
2105 es->format_rect.right -= bw;
2106 es->format_rect.bottom -= bw;
2108 es->format_rect.left += es->left_margin;
2109 es->format_rect.right -= es->right_margin;
2110 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2111 if (es->style & ES_MULTILINE)
2113 INT fw, vlc, max_x_offset, max_y_offset;
2115 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2116 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2118 /* correct es->x_offset */
2119 fw = es->format_rect.right - es->format_rect.left;
2120 max_x_offset = es->text_width - fw;
2121 if(max_x_offset < 0) max_x_offset = 0;
2122 if(es->x_offset > max_x_offset)
2123 es->x_offset = max_x_offset;
2125 /* correct es->y_offset */
2126 max_y_offset = es->line_count - vlc;
2127 if(max_y_offset < 0) max_y_offset = 0;
2128 if(es->y_offset > max_y_offset)
2129 es->y_offset = max_y_offset;
2132 /* Windows doesn't care to fix text placement for SL controls */
2133 es->format_rect.bottom = es->format_rect.top + es->line_height;
2135 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2136 EDIT_BuildLineDefs_ML(wnd, es);
2140 /*********************************************************************
2145 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
2148 ERR("no EDITSTATE ... please report\n");
2151 if (!es->lock_count) {
2152 ERR("lock_count == 0 ... please report\n");
2156 ERR("es->text == 0 ... please report\n");
2160 if (force || (es->lock_count == 1)) {
2163 BOOL _16bit = FALSE;
2165 UINT countW = strlenW(es->text) + 1;
2169 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2170 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2171 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2172 countA = LocalSize(es->hloc32A);
2173 if(countA_new > countA)
2176 UINT alloc_size = (countA_new + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
2177 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2178 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2181 es->hloc32A = hloc32A_new;
2182 countA = LocalSize(hloc32A_new);
2183 TRACE("Real new size %d bytes\n", countA);
2186 WARN("FAILED! Will synchronize partially\n");
2188 textA = LocalLock(es->hloc32A);
2192 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2193 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2194 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2195 countA = LOCAL_Size(wnd->hInstance, es->hloc16);
2196 if(countA_new > countA)
2198 HLOCAL16 hloc16_new;
2199 UINT alloc_size = (countA_new + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
2200 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2201 hloc16_new = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2204 es->hloc16 = hloc16_new;
2205 countA = LOCAL_Size(wnd->hInstance, hloc16_new);
2206 TRACE("Real new size %d bytes\n", countA);
2209 WARN("FAILED! Will synchronize partially\n");
2211 textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
2217 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2219 LOCAL_Unlock(wnd->hInstance, es->hloc16);
2221 LocalUnlock(es->hloc32A);
2224 LocalUnlock(es->hloc32W);
2228 ERR("no buffer ... please report\n");
2236 /*********************************************************************
2238 * EDIT_UpdateScrollInfo
2241 static void EDIT_UpdateScrollInfo(WND *wnd, EDITSTATE *es)
2243 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2246 si.cbSize = sizeof(SCROLLINFO);
2247 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2249 si.nMax = es->line_count - 1;
2250 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2251 si.nPos = es->y_offset;
2252 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2253 si.nMin, si.nMax, si.nPage, si.nPos);
2254 SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
2257 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2260 si.cbSize = sizeof(SCROLLINFO);
2261 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2263 si.nMax = es->text_width - 1;
2264 si.nPage = es->format_rect.right - es->format_rect.left;
2265 si.nPos = es->x_offset;
2266 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2267 si.nMin, si.nMax, si.nPage, si.nPos);
2268 SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
2272 /*********************************************************************
2274 * EDIT_WordBreakProc
2276 * Find the beginning of words.
2277 * Note: unlike the specs for a WordBreakProc, this function only
2278 * allows to be called without linebreaks between s[0] upto
2279 * s[count - 1]. Remember it is only called
2280 * internally, so we can decide this for ourselves.
2283 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2287 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2297 if (s[index] == ' ') {
2298 while (index && (s[index] == ' '))
2301 while (index && (s[index] != ' '))
2303 if (s[index] == ' ')
2307 while (index && (s[index] != ' '))
2309 if (s[index] == ' ')
2319 if (s[index] == ' ')
2320 while ((index < count) && (s[index] == ' ')) index++;
2322 while (s[index] && (s[index] != ' ') && (index < count))
2324 while ((s[index] == ' ') && (index < count)) index++;
2328 case WB_ISDELIMITER:
2329 ret = (s[index] == ' ');
2332 ERR("unknown action code, please report !\n");
2339 /*********************************************************************
2343 * returns line number (not index) in high-order word of result.
2344 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2345 * to Richedit, not to the edit control. Original documentation is valid.
2346 * FIXME: do the specs mean to return -1 if outside client area or
2347 * if outside formatting rectangle ???
2350 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y)
2358 GetClientRect(wnd->hwndSelf, &rc);
2359 if (!PtInRect(&rc, pt))
2362 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
2363 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2367 /*********************************************************************
2371 * Enable or disable soft breaks.
2373 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2375 es->flags &= ~EF_USE_SOFTBRK;
2377 es->flags |= EF_USE_SOFTBRK;
2378 FIXME("soft break enabled, not implemented\n");
2384 /*********************************************************************
2388 * Hopefully this won't fire back at us.
2389 * We always start with a fixed buffer in the local heap.
2390 * Despite of the documentation says that the local heap is used
2391 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2392 * buffer on the local heap.
2395 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2399 if (!(es->style & ES_MULTILINE))
2403 hLocal = es->hloc32W;
2410 TRACE("Allocating 32-bit ANSI alias buffer\n");
2411 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2412 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countA)))
2414 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", countA);
2417 textA = LocalLock(es->hloc32A);
2418 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2419 LocalUnlock(es->hloc32A);
2421 hLocal = es->hloc32A;
2424 TRACE("Returning %04X, LocalSize() = %d\n", hLocal, LocalSize(hLocal));
2429 /*********************************************************************
2433 * Hopefully this won't fire back at us.
2434 * We always start with a buffer in 32 bit linear memory.
2435 * However, with this message a 16 bit application requests
2436 * a handle of 16 bit local heap memory, where it expects to find
2438 * It's a pitty that from this moment on we have to use this
2439 * local heap, because applications may rely on the handle
2442 * In this function we'll try to switch to local heap.
2444 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2449 if (!(es->style & ES_MULTILINE))
2455 if (!LOCAL_HeapSize(wnd->hInstance)) {
2456 if (!LocalInit16(wnd->hInstance, 0,
2457 GlobalSize16(wnd->hInstance))) {
2458 ERR("could not initialize local heap\n");
2461 TRACE("local heap initialized\n");
2464 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2466 TRACE("Allocating 16-bit ANSI alias buffer\n");
2467 if (!(es->hloc16 = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, countA))) {
2468 ERR("could not allocate new 16 bit buffer\n");
2472 if (!(textA = (LPSTR)LOCAL_Lock(wnd->hInstance, es->hloc16))) {
2473 ERR("could not lock new 16 bit buffer\n");
2474 LOCAL_Free(wnd->hInstance, es->hloc16);
2479 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2480 LOCAL_Unlock(wnd->hInstance, es->hloc16);
2482 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(wnd->hInstance, es->hloc16));
2487 /*********************************************************************
2492 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR lpch)
2498 if (es->style & ES_MULTILINE) {
2499 if (line >= es->line_count)
2503 i = EDIT_EM_LineIndex(es, line);
2505 len = min(*(WORD *)lpch, EDIT_EM_LineLength(es, i));
2506 for (i = 0 ; i < len ; i++) {
2511 return (LRESULT)len;
2515 /*********************************************************************
2520 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end)
2522 UINT s = es->selection_start;
2523 UINT e = es->selection_end;
2530 return MAKELONG(s, e);
2534 /*********************************************************************
2538 * FIXME: is this right ? (or should it be only VSCROLL)
2539 * (and maybe only for edit controls that really have their
2540 * own scrollbars) (and maybe only for multiline controls ?)
2541 * All in all: very poorly documented
2543 * FIXME: now it's also broken, because of the new WM_HSCROLL /
2544 * WM_VSCROLL handlers
2547 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2549 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0),
2550 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0));
2554 /*********************************************************************
2559 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2564 if (!(es->style & ES_MULTILINE))
2566 if (index > (INT)strlenW(es->text))
2567 return es->line_count - 1;
2569 index = min(es->selection_start, es->selection_end);
2572 line_def = es->first_line_def;
2573 index -= line_def->length;
2574 while ((index >= 0) && line_def->next) {
2576 line_def = line_def->next;
2577 index -= line_def->length;
2583 /*********************************************************************
2588 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2593 if (!(es->style & ES_MULTILINE))
2595 if (line >= es->line_count)
2599 line_def = es->first_line_def;
2601 INT index = es->selection_end - line_def->length;
2602 while ((index >= 0) && line_def->next) {
2603 line_index += line_def->length;
2604 line_def = line_def->next;
2605 index -= line_def->length;
2609 line_index += line_def->length;
2610 line_def = line_def->next;
2618 /*********************************************************************
2623 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2627 if (!(es->style & ES_MULTILINE))
2628 return strlenW(es->text);
2631 /* get the number of remaining non-selected chars of selected lines */
2634 li = EDIT_EM_LineFromChar(es, es->selection_start);
2635 /* # chars before start of selection area */
2636 count = es->selection_start - EDIT_EM_LineIndex(es, li);
2637 li = EDIT_EM_LineFromChar(es, es->selection_end);
2638 /* # chars after end of selection */
2639 count += EDIT_EM_LineIndex(es, li) +
2640 EDIT_EM_LineLength(es, li) - es->selection_end;
2643 line_def = es->first_line_def;
2644 index -= line_def->length;
2645 while ((index >= 0) && line_def->next) {
2646 line_def = line_def->next;
2647 index -= line_def->length;
2649 return line_def->net_length;
2653 /*********************************************************************
2657 * NOTE: dx is in average character widths, dy - in lines;
2660 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2662 if (!(es->style & ES_MULTILINE))
2665 dx *= es->char_width;
2666 return EDIT_EM_LineScroll_internal(wnd, es, dx, dy);
2669 /*********************************************************************
2671 * EDIT_EM_LineScroll_internal
2673 * Version of EDIT_EM_LineScroll for internal use.
2674 * It doesn't refuse if ES_MULTILINE is set and assumes that
2675 * dx is in pixels, dy - in lines.
2678 static BOOL EDIT_EM_LineScroll_internal(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2681 INT x_offset_in_pixels;
2683 if (es->style & ES_MULTILINE)
2685 x_offset_in_pixels = es->x_offset;
2690 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->x_offset, FALSE));
2693 if (-dx > x_offset_in_pixels)
2694 dx = -x_offset_in_pixels;
2695 if (dx > es->text_width - x_offset_in_pixels)
2696 dx = es->text_width - x_offset_in_pixels;
2697 nyoff = max(0, es->y_offset + dy);
2698 if (nyoff >= es->line_count)
2699 nyoff = es->line_count - 1;
2700 dy = (es->y_offset - nyoff) * es->line_height;
2705 es->y_offset = nyoff;
2706 if(es->style & ES_MULTILINE)
2709 es->x_offset += dx / es->char_width;
2711 GetClientRect(wnd->hwndSelf, &rc1);
2712 IntersectRect(&rc, &rc1, &es->format_rect);
2713 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2714 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2716 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2717 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2718 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2719 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2724 /*********************************************************************
2729 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap)
2731 INT len = strlenW(es->text);
2740 index = min(index, len);
2741 dc = GetDC(wnd->hwndSelf);
2743 old_font = SelectObject(dc, es->font);
2744 if (es->style & ES_MULTILINE) {
2745 l = EDIT_EM_LineFromChar(es, index);
2746 y = (l - es->y_offset) * es->line_height;
2747 li = EDIT_EM_LineIndex(es, l);
2748 if (after_wrap && (li == index) && l) {
2750 LINEDEF *line_def = es->first_line_def;
2752 line_def = line_def->next;
2755 if (line_def->ending == END_WRAP) {
2757 y -= es->line_height;
2758 li = EDIT_EM_LineIndex(es, l);
2761 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2762 es->tabs_count, es->tabs)) - es->x_offset;
2764 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2765 if (index < es->x_offset) {
2766 GetTextExtentPoint32W(dc, text + index,
2767 es->x_offset - index, &size);
2770 GetTextExtentPoint32W(dc, text + es->x_offset,
2771 index - es->x_offset, &size);
2775 if (es->style & ES_PASSWORD)
2776 HeapFree(GetProcessHeap(), 0, text);
2778 x += es->format_rect.left;
2779 y += es->format_rect.top;
2781 SelectObject(dc, old_font);
2782 ReleaseDC(wnd->hwndSelf, dc);
2783 return MAKELONG((INT16)x, (INT16)y);
2787 /*********************************************************************
2791 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2794 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
2796 UINT strl = strlenW(lpsz_replace);
2797 UINT tl = strlenW(es->text);
2804 TRACE("%s, can_undo %d, send_update %d\n",
2805 debugstr_w(lpsz_replace), can_undo, send_update);
2807 s = es->selection_start;
2808 e = es->selection_end;
2810 if ((s == e) && !strl)
2815 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2819 /* there is something to be deleted */
2821 utl = strlenW(es->undo_text);
2822 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2823 /* undo-buffer is extended to the right */
2824 EDIT_MakeUndoFit(es, utl + e - s);
2825 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2826 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2827 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2828 /* undo-buffer is extended to the left */
2829 EDIT_MakeUndoFit(es, utl + e - s);
2830 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2832 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2833 p[i] = (es->text + s)[i];
2834 es->undo_position = s;
2836 /* new undo-buffer */
2837 EDIT_MakeUndoFit(es, e - s);
2838 strncpyW(es->undo_text, es->text + s, e - s + 1);
2839 es->undo_text[e - s] = 0; /* ensure 0 termination */
2840 es->undo_position = s;
2842 /* any deletion makes the old insertion-undo invalid */
2843 es->undo_insert_count = 0;
2845 EDIT_EM_EmptyUndoBuffer(es);
2848 strcpyW(es->text + s, es->text + e);
2851 /* there is an insertion */
2853 if ((s == es->undo_position) ||
2854 ((es->undo_insert_count) &&
2855 (s == es->undo_position + es->undo_insert_count)))
2857 * insertion is new and at delete position or
2858 * an extension to either left or right
2860 es->undo_insert_count += strl;
2862 /* new insertion undo */
2863 es->undo_position = s;
2864 es->undo_insert_count = strl;
2865 /* new insertion makes old delete-buffer invalid */
2866 *es->undo_text = '\0';
2869 EDIT_EM_EmptyUndoBuffer(es);
2872 tl = strlenW(es->text);
2873 for (p = es->text + tl ; p >= es->text + s ; p--)
2875 for (i = 0 , p = es->text + s ; i < strl ; i++)
2876 p[i] = lpsz_replace[i];
2877 if(es->style & ES_UPPERCASE)
2878 CharUpperBuffW(p, strl);
2879 else if(es->style & ES_LOWERCASE)
2880 CharLowerBuffW(p, strl);
2883 /* FIXME: really inefficient */
2884 if (es->style & ES_MULTILINE)
2885 EDIT_BuildLineDefs_ML(wnd, es);
2887 EDIT_CalcLineWidth_SL(wnd, es);
2889 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2890 es->flags |= EF_MODIFIED;
2891 if (send_update) es->flags |= EF_UPDATE;
2892 EDIT_EM_ScrollCaret(wnd, es);
2894 /* FIXME: really inefficient */
2895 EDIT_UpdateText(wnd, NULL, TRUE);
2899 /*********************************************************************
2904 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
2908 if (!(es->style & ES_MULTILINE))
2909 return (LRESULT)FALSE;
2919 if (es->y_offset < es->line_count - 1)
2924 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2927 if (es->y_offset < es->line_count - 1)
2928 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2931 return (LRESULT)FALSE;
2934 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2935 /* check if we are going to move too far */
2936 if(es->y_offset + dy > es->line_count - vlc)
2937 dy = es->line_count - vlc - es->y_offset;
2939 /* Notification is done in EDIT_EM_LineScroll */
2941 EDIT_EM_LineScroll(wnd, es, 0, dy);
2943 return MAKELONG((INT16)dy, (BOOL16)TRUE);
2947 /*********************************************************************
2952 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
2954 if (es->style & ES_MULTILINE) {
2959 INT cw = es->char_width;
2964 l = EDIT_EM_LineFromChar(es, es->selection_end);
2965 li = EDIT_EM_LineIndex(es, l);
2966 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
2967 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2968 if (l >= es->y_offset + vlc)
2969 dy = l - vlc + 1 - es->y_offset;
2970 if (l < es->y_offset)
2971 dy = l - es->y_offset;
2972 ww = es->format_rect.right - es->format_rect.left;
2973 if (x < es->format_rect.left)
2974 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
2975 if (x > es->format_rect.right)
2976 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
2979 /* check if we are going to move too far */
2980 if(es->x_offset + dx + ww > es->text_width)
2981 dx = es->text_width - ww - es->x_offset;
2983 EDIT_EM_LineScroll_internal(wnd, es, dx, dy);
2990 if (!(es->style & ES_AUTOHSCROLL))
2993 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2994 format_width = es->format_rect.right - es->format_rect.left;
2995 if (x < es->format_rect.left) {
2996 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
2999 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
3000 } while ((x < goal) && es->x_offset);
3001 /* FIXME: use ScrollWindow() somehow to improve performance */
3002 EDIT_UpdateText(wnd, NULL, TRUE);
3003 } else if (x > es->format_rect.right) {
3005 INT len = strlenW(es->text);
3006 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3009 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
3010 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
3011 } while ((x > goal) && (x_last > es->format_rect.right));
3012 /* FIXME: use ScrollWindow() somehow to improve performance */
3013 EDIT_UpdateText(wnd, NULL, TRUE);
3017 if(es->flags & EF_FOCUSED)
3018 EDIT_SetCaretPos(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
3022 /*********************************************************************
3026 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3029 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc)
3031 if (!(es->style & ES_MULTILINE))
3035 WARN("called with NULL handle\n");
3039 EDIT_UnlockBuffer(wnd, es, TRUE);
3043 LOCAL_Free(wnd->hInstance, es->hloc16);
3044 es->hloc16 = (HLOCAL16)NULL;
3051 LocalFree(es->hloc32A);
3052 es->hloc32A = (HLOCAL)NULL;
3063 countA = LocalSize(hloc);
3064 textA = LocalLock(hloc);
3065 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3066 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3068 ERR("Could not allocate new unicode buffer\n");
3071 textW = LocalLock(hloc32W_new);
3072 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3073 LocalUnlock(hloc32W_new);
3077 LocalFree(es->hloc32W);
3079 es->hloc32W = hloc32W_new;
3083 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3085 EDIT_LockBuffer(wnd, es);
3087 es->x_offset = es->y_offset = 0;
3088 es->selection_start = es->selection_end = 0;
3089 EDIT_EM_EmptyUndoBuffer(es);
3090 es->flags &= ~EF_MODIFIED;
3091 es->flags &= ~EF_UPDATE;
3092 EDIT_BuildLineDefs_ML(wnd, es);
3093 EDIT_UpdateText(wnd, NULL, TRUE);
3094 EDIT_EM_ScrollCaret(wnd, es);
3098 /*********************************************************************
3102 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3105 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
3112 if (!(es->style & ES_MULTILINE))
3116 WARN("called with NULL handle\n");
3120 EDIT_UnlockBuffer(wnd, es, TRUE);
3124 LocalFree(es->hloc32A);
3125 es->hloc32A = (HLOCAL)NULL;
3128 countA = LOCAL_Size(wnd->hInstance, hloc);
3129 textA = LOCAL_Lock(wnd->hInstance, hloc);
3130 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3131 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3133 ERR("Could not allocate new unicode buffer\n");
3136 textW = LocalLock(hloc32W_new);
3137 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3138 LocalUnlock(hloc32W_new);
3139 LOCAL_Unlock(wnd->hInstance, hloc);
3142 LocalFree(es->hloc32W);
3144 es->hloc32W = hloc32W_new;
3147 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3149 EDIT_LockBuffer(wnd, es);
3151 es->x_offset = es->y_offset = 0;
3152 es->selection_start = es->selection_end = 0;
3153 EDIT_EM_EmptyUndoBuffer(es);
3154 es->flags &= ~EF_MODIFIED;
3155 es->flags &= ~EF_UPDATE;
3156 EDIT_BuildLineDefs_ML(wnd, es);
3157 EDIT_UpdateText(wnd, NULL, TRUE);
3158 EDIT_EM_ScrollCaret(wnd, es);
3162 /*********************************************************************
3166 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3167 * However, the windows version is not complied to yet in all of edit.c
3170 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3172 if (es->style & ES_MULTILINE) {
3174 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3176 es->buffer_limit = BUFLIMIT_MULTI;
3179 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3181 es->buffer_limit = BUFLIMIT_SINGLE;
3186 /*********************************************************************
3190 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3191 * action wParam despite what the docs say. EC_USEFONTINFO means one third
3192 * of the char's width, according to the new docs.
3195 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3196 INT left, INT right)
3198 if (action & EC_LEFTMARGIN) {
3199 if (left != EC_USEFONTINFO)
3200 es->left_margin = left;
3202 es->left_margin = es->char_width / 3;
3205 if (action & EC_RIGHTMARGIN) {
3206 if (right != EC_USEFONTINFO)
3207 es->right_margin = right;
3209 es->right_margin = es->char_width / 3;
3211 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3215 /*********************************************************************
3217 * EM_SETPASSWORDCHAR
3220 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, WCHAR c)
3222 if (es->style & ES_MULTILINE)
3225 if (es->password_char == c)
3228 es->password_char = c;
3230 wnd->dwStyle |= ES_PASSWORD;
3231 es->style |= ES_PASSWORD;
3233 wnd->dwStyle &= ~ES_PASSWORD;
3234 es->style &= ~ES_PASSWORD;
3236 EDIT_UpdateText(wnd, NULL, TRUE);
3240 /*********************************************************************
3244 * note: unlike the specs say: the order of start and end
3245 * _is_ preserved in Windows. (i.e. start can be > end)
3246 * In other words: this handler is OK
3249 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3251 UINT old_start = es->selection_start;
3252 UINT old_end = es->selection_end;
3253 UINT len = strlenW(es->text);
3255 if (start == (UINT)-1) {
3256 start = es->selection_end;
3257 end = es->selection_end;
3259 start = min(start, len);
3260 end = min(end, len);
3262 es->selection_start = start;
3263 es->selection_end = end;
3265 es->flags |= EF_AFTER_WRAP;
3267 es->flags &= ~EF_AFTER_WRAP;
3268 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3269 ORDER_UINT(start, end);
3270 ORDER_UINT(end, old_end);
3271 ORDER_UINT(start, old_start);
3272 ORDER_UINT(old_start, old_end);
3273 if (end != old_start)
3277 * ORDER_UINT32(end, old_start);
3278 * EDIT_InvalidateText(wnd, es, start, end);
3279 * EDIT_InvalidateText(wnd, es, old_start, old_end);
3280 * in place of the following if statement.
3282 if (old_start > end )
3284 EDIT_InvalidateText(wnd, es, start, end);
3285 EDIT_InvalidateText(wnd, es, old_start, old_end);
3289 EDIT_InvalidateText(wnd, es, start, old_start);
3290 EDIT_InvalidateText(wnd, es, end, old_end);
3293 else EDIT_InvalidateText(wnd, es, start, old_end);
3297 /*********************************************************************
3302 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3304 if (!(es->style & ES_MULTILINE))
3307 HeapFree(GetProcessHeap(), 0, es->tabs);
3308 es->tabs_count = count;
3312 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3313 memcpy(es->tabs, tabs, count * sizeof(INT));
3319 /*********************************************************************
3324 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3326 if (!(es->style & ES_MULTILINE))
3329 HeapFree(GetProcessHeap(), 0, es->tabs);
3330 es->tabs_count = count;
3335 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3336 for (i = 0 ; i < count ; i++)
3337 es->tabs[i] = *tabs++;
3343 /*********************************************************************
3345 * EM_SETWORDBREAKPROC
3348 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, LPARAM lParam)
3350 if (es->word_break_proc == (void *)lParam)
3353 es->word_break_proc = (void *)lParam;
3354 es->word_break_proc16 = NULL;
3356 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3357 EDIT_BuildLineDefs_ML(wnd, es);
3358 EDIT_UpdateText(wnd, NULL, TRUE);
3363 /*********************************************************************
3365 * EM_SETWORDBREAKPROC16
3368 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3370 if (es->word_break_proc16 == wbp)
3373 es->word_break_proc = NULL;
3374 es->word_break_proc16 = wbp;
3375 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3376 EDIT_BuildLineDefs_ML(wnd, es);
3377 EDIT_UpdateText(wnd, NULL, TRUE);
3382 /*********************************************************************
3387 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
3389 INT ulength = strlenW(es->undo_text);
3390 LPWSTR utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3392 strcpyW(utext, es->undo_text);
3394 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3395 es->undo_insert_count, debugstr_w(utext));
3397 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3398 EDIT_EM_EmptyUndoBuffer(es);
3399 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext, TRUE);
3400 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3401 EDIT_EM_ScrollCaret(wnd, es);
3402 HeapFree(GetProcessHeap(), 0, utext);
3404 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3405 es->undo_insert_count, debugstr_w(es->undo_text));
3407 if (es->flags & EF_UPDATE) {
3408 es->flags &= ~EF_UPDATE;
3409 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3416 /*********************************************************************
3421 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, WCHAR c)
3423 BOOL control = GetKeyState(VK_CONTROL) & 0x8000;
3426 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3427 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3430 if (es->style & ES_MULTILINE) {
3431 if (es->style & ES_READONLY) {
3432 EDIT_MoveHome(wnd, es, FALSE);
3433 EDIT_MoveDown_ML(wnd, es, FALSE);
3435 static const WCHAR cr_lfW[] = {'\r','\n',0};
3436 EDIT_EM_ReplaceSel(wnd, es, TRUE, cr_lfW, TRUE);
3437 if (es->flags & EF_UPDATE) {
3438 es->flags &= ~EF_UPDATE;
3439 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3445 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3447 static const WCHAR tabW[] = {'\t',0};
3448 EDIT_EM_ReplaceSel(wnd, es, TRUE, tabW, TRUE);
3449 if (es->flags & EF_UPDATE) {
3450 es->flags &= ~EF_UPDATE;
3451 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3456 if (!(es->style & ES_READONLY) && !control) {
3457 if (es->selection_start != es->selection_end)
3458 EDIT_WM_Clear(wnd, es);
3460 /* delete character left of caret */
3461 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3462 EDIT_MoveBackward(wnd, es, TRUE);
3463 EDIT_WM_Clear(wnd, es);
3468 SendMessageW(wnd->hwndSelf, WM_COPY, 0, 0);
3471 SendMessageW(wnd->hwndSelf, WM_PASTE, 0, 0);
3474 SendMessageW(wnd->hwndSelf, WM_CUT, 0, 0);
3478 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3482 EDIT_EM_ReplaceSel(wnd, es, TRUE, str, TRUE);
3483 if (es->flags & EF_UPDATE) {
3484 es->flags &= ~EF_UPDATE;
3485 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3493 /*********************************************************************
3498 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND control)
3500 if (code || control)
3505 EDIT_EM_Undo(wnd, es);
3508 EDIT_WM_Cut(wnd, es);
3511 EDIT_WM_Copy(wnd, es);
3514 EDIT_WM_Paste(wnd, es);
3517 EDIT_WM_Clear(wnd, es);
3520 EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
3521 EDIT_EM_ScrollCaret(wnd, es);
3524 ERR("unknown menu item, please report\n");
3530 /*********************************************************************
3534 * Note: the resource files resource/sysres_??.rc cannot define a
3535 * single popup menu. Hence we use a (dummy) menubar
3536 * containing the single popup menu as its first item.
3538 * FIXME: the message identifiers have been chosen arbitrarily,
3539 * hence we use MF_BYPOSITION.
3540 * We might as well use the "real" values (anybody knows ?)
3541 * The menu definition is in resources/sysres_??.rc.
3542 * Once these are OK, we better use MF_BYCOMMAND here
3543 * (as we do in EDIT_WM_Command()).
3546 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, INT x, INT y)
3548 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3549 HMENU popup = GetSubMenu(menu, 0);
3550 UINT start = es->selection_start;
3551 UINT end = es->selection_end;
3553 ORDER_UINT(start, end);
3556 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) ? MF_ENABLED : MF_GRAYED));
3558 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3560 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3562 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) ? MF_ENABLED : MF_GRAYED));
3564 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
3566 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3568 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
3573 /*********************************************************************
3578 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
3580 INT s = es->selection_start;
3581 INT e = es->selection_end;
3588 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3589 dst = GlobalLock(hdst);
3590 strncpyW(dst, es->text + s, e - s);
3591 dst[e - s] = 0; /* ensure 0 termination */
3592 TRACE("%s\n", debugstr_w(dst));
3594 OpenClipboard(wnd->hwndSelf);
3596 SetClipboardData(CF_UNICODETEXT, hdst);
3601 /*********************************************************************
3606 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCWSTR name)
3608 TRACE("%s\n", debugstr_w(name));
3610 * To initialize some final structure members, we call some helper
3611 * functions. However, since the EDITSTATE is not consistent (i.e.
3612 * not fully initialized), we should be very careful which
3613 * functions can be called, and in what order.
3615 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3616 EDIT_EM_EmptyUndoBuffer(es);
3618 if (name && *name) {
3619 EDIT_EM_ReplaceSel(wnd, es, FALSE, name, TRUE);
3620 /* if we insert text to the editline, the text scrolls out
3621 * of the window, as the caret is placed after the insert
3622 * pos normally; thus we reset es->selection... to 0 and
3625 es->selection_start = es->selection_end = 0;
3626 EDIT_EM_ScrollCaret(wnd, es);
3627 if (es->flags & EF_UPDATE) {
3628 es->flags &= ~EF_UPDATE;
3629 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3636 /*********************************************************************
3641 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3644 while (LocalUnlock(es->hloc32W)) ;
3645 LocalFree(es->hloc32W);
3648 while (LocalUnlock(es->hloc32A)) ;
3649 LocalFree(es->hloc32A);
3652 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3653 LOCAL_Free(wnd->hInstance, es->hloc16);
3655 HeapFree(GetProcessHeap(), 0, es);
3656 *(EDITSTATE **)wnd->wExtra = NULL;
3660 /*********************************************************************
3665 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3670 if ( get_app_version() >= 0x40000 &&(
3671 !es->bEnableState || (es->style & ES_READONLY)))
3672 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3674 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc);
3677 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3679 GetClientRect(wnd->hwndSelf, &rc);
3680 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3681 GetClipBox(dc, &rc);
3683 * FIXME: specs say that we should UnrealizeObject() the brush,
3684 * but the specs of UnrealizeObject() say that we shouldn't
3685 * unrealize a stock object. The default brush that
3686 * DefWndProc() returns is ... a stock object.
3688 FillRect(dc, &rc, brush);
3693 /*********************************************************************
3698 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3700 if(!count) return 0;
3704 LPWSTR textW = (LPWSTR)lParam;
3705 strncpyW(textW, es->text, count);
3706 textW[count - 1] = 0; /* ensure 0 termination */
3707 return strlenW(textW);
3711 LPSTR textA = (LPSTR)lParam;
3712 INT ret = WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL);
3713 textA[count - 1] = 0; /* ensure 0 termination */
3719 /*********************************************************************
3723 * 16 bit notepad needs this. Actually it is not _our_ hack,
3724 * it is notepad's. Notepad is sending us scrollbar messages with
3725 * undocumented parameters without us even having a scrollbar ... !?!?
3728 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos)
3731 INT fw = es->format_rect.right - es->format_rect.left;
3734 if (!(es->flags & EF_HSCROLL_HACK)) {
3735 ERR("hacked WM_HSCROLL handler invoked\n");
3736 ERR(" if you are _not_ running 16 bit notepad, please report\n");
3737 ERR(" (this message is only displayed once per edit control)\n");
3738 es->flags |= EF_HSCROLL_HACK;
3744 dx = -es->char_width;
3747 if (es->x_offset < es->text_width)
3748 dx = es->char_width;
3752 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3755 if (es->x_offset < es->text_width)
3756 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3763 if (es->x_offset < es->text_width)
3764 dx = es->text_width - es->x_offset;
3767 es->flags |= EF_HSCROLL_TRACK;
3768 dx = pos * es->text_width / 100 - es->x_offset;
3770 case SB_THUMBPOSITION:
3771 es->flags &= ~EF_HSCROLL_TRACK;
3772 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3773 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3779 * FIXME : the next two are undocumented !
3780 * Are we doing the right thing ?
3781 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3782 * although it's also a regular control message.
3785 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3787 case EM_LINESCROLL16:
3792 ERR("undocumented (hacked) WM_HSCROLL parameter, please report\n");
3797 INT fw = es->format_rect.right - es->format_rect.left;
3798 /* check if we are going to move too far */
3799 if(es->x_offset + dx + fw > es->text_width)
3800 dx = es->text_width - fw - es->x_offset;
3802 EDIT_EM_LineScroll_internal(wnd, es, dx, 0);
3808 /*********************************************************************
3813 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
3818 if (!(es->style & ES_MULTILINE))
3821 if (!(es->style & ES_AUTOHSCROLL))
3824 if (!(es->style & WS_HSCROLL))
3825 return EDIT_HScroll_Hack(wnd, es, action, pos);
3828 fw = es->format_rect.right - es->format_rect.left;
3832 dx = -es->char_width;
3835 if (es->x_offset < es->text_width)
3836 dx = es->char_width;
3840 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3843 if (es->x_offset < es->text_width)
3844 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3851 if (es->x_offset < es->text_width)
3852 dx = es->text_width - es->x_offset;
3855 es->flags |= EF_HSCROLL_TRACK;
3856 dx = pos - es->x_offset;
3858 case SB_THUMBPOSITION:
3859 es->flags &= ~EF_HSCROLL_TRACK;
3860 if (!(dx = pos - es->x_offset)) {
3861 SetScrollPos(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3862 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3869 ERR("undocumented WM_HSCROLL parameter, please report\n");
3874 INT fw = es->format_rect.right - es->format_rect.left;
3875 /* check if we are going to move too far */
3876 if(es->x_offset + dx + fw > es->text_width)
3877 dx = es->text_width - fw - es->x_offset;
3879 EDIT_EM_LineScroll_internal(wnd, es, dx, 0);
3885 /*********************************************************************
3890 static BOOL EDIT_CheckCombo(WND *wnd, EDITSTATE *es, UINT msg, INT key)
3892 HWND hLBox = es->hwndListBox;
3900 hCombo = wnd->parent->hwndSelf;
3904 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
3905 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3907 if (key == VK_UP || key == VK_DOWN)
3909 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3912 if (msg == WM_KEYDOWN || nEUI)
3913 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3919 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3921 /* make sure ComboLBox pops up */
3922 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3927 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3930 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3932 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3934 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
3939 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3945 /*********************************************************************
3949 * Handling of special keys that don't produce a WM_CHAR
3950 * (i.e. non-printable keys) & Backspace & Delete
3953 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key)
3958 if (GetKeyState(VK_MENU) & 0x8000)
3961 shift = GetKeyState(VK_SHIFT) & 0x8000;
3962 control = GetKeyState(VK_CONTROL) & 0x8000;
3967 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key) || key == VK_F4)
3972 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3973 EDIT_MoveUp_ML(wnd, es, shift);
3976 EDIT_MoveWordBackward(wnd, es, shift);
3978 EDIT_MoveBackward(wnd, es, shift);
3981 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key))
3985 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3986 EDIT_MoveDown_ML(wnd, es, shift);
3988 EDIT_MoveWordForward(wnd, es, shift);
3990 EDIT_MoveForward(wnd, es, shift);
3993 EDIT_MoveHome(wnd, es, shift);
3996 EDIT_MoveEnd(wnd, es, shift);
3999 if (es->style & ES_MULTILINE)
4000 EDIT_MovePageUp_ML(wnd, es, shift);
4002 EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
4005 if (es->style & ES_MULTILINE)
4006 EDIT_MovePageDown_ML(wnd, es, shift);
4008 EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
4011 if (!(es->style & ES_READONLY) && !(shift && control)) {
4012 if (es->selection_start != es->selection_end) {
4014 EDIT_WM_Cut(wnd, es);
4016 EDIT_WM_Clear(wnd, es);
4019 /* delete character left of caret */
4020 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
4021 EDIT_MoveBackward(wnd, es, TRUE);
4022 EDIT_WM_Clear(wnd, es);
4023 } else if (control) {
4024 /* delete to end of line */
4025 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
4026 EDIT_MoveEnd(wnd, es, TRUE);
4027 EDIT_WM_Clear(wnd, es);
4029 /* delete character right of caret */
4030 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
4031 EDIT_MoveForward(wnd, es, TRUE);
4032 EDIT_WM_Clear(wnd, es);
4039 if (!(es->style & ES_READONLY))
4040 EDIT_WM_Paste(wnd, es);
4042 EDIT_WM_Copy(wnd, es);
4045 /* If the edit doesn't want the return send a message to the default object */
4046 if(!(es->style & ES_WANTRETURN))
4048 HWND hwndParent = GetParent(wnd->hwndSelf);
4049 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4050 if (HIWORD(dw) == DC_HASDEFID)
4052 SendMessageW( hwndParent, WM_COMMAND,
4053 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4054 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4063 /*********************************************************************
4068 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es)
4070 es->flags &= ~EF_FOCUSED;
4072 if(!(es->style & ES_NOHIDESEL))
4073 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
4074 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
4079 /*********************************************************************
4083 * The caret position has been set on the WM_LBUTTONDOWN message
4086 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es)
4089 INT e = es->selection_end;
4094 if (!(es->flags & EF_FOCUSED))
4097 l = EDIT_EM_LineFromChar(es, e);
4098 li = EDIT_EM_LineIndex(es, l);
4099 ll = EDIT_EM_LineLength(es, e);
4100 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4101 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4102 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
4103 EDIT_EM_ScrollCaret(wnd, es);
4108 /*********************************************************************
4113 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
4118 if (!(es->flags & EF_FOCUSED))
4121 es->bCaptureState = TRUE;
4122 SetCapture(wnd->hwndSelf);
4123 EDIT_ConfinePoint(es, &x, &y);
4124 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
4125 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4126 EDIT_EM_ScrollCaret(wnd, es);
4127 es->region_posx = es->region_posy = 0;
4128 SetTimer(wnd->hwndSelf, 0, 100, NULL);
4133 /*********************************************************************
4138 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es)
4140 if (es->bCaptureState && GetCapture() == hwndSelf) {
4141 KillTimer(hwndSelf, 0);
4144 es->bCaptureState = FALSE;
4149 /*********************************************************************
4154 static LRESULT EDIT_WM_MButtonDown(WND *wnd)
4156 SendMessageW(wnd->hwndSelf,WM_PASTE,0,0);
4161 /*********************************************************************
4166 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, INT x, INT y)
4172 if (GetCapture() != wnd->hwndSelf)
4176 * FIXME: gotta do some scrolling if outside client
4177 * area. Maybe reset the timer ?
4180 EDIT_ConfinePoint(es, &x, &y);
4181 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4182 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4183 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
4184 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
4189 /*********************************************************************
4194 static LRESULT EDIT_WM_NCCreate(WND *wnd, DWORD style, HWND hwndParent, BOOL unicode)
4199 TRACE("Creating %s edit control\n", unicode ? "Unicode" : "ANSI");
4201 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4203 *(EDITSTATE **)wnd->wExtra = es;
4206 * Note: since the EDITSTATE has not been fully initialized yet,
4207 * we can't use any API calls that may send
4208 * WM_XXX messages before WM_NCCREATE is completed.
4211 es->is_unicode = unicode;
4214 es->bEnableState = !(style & WS_DISABLED);
4217 * In Win95 look and feel, the WS_BORDER style is replaced by the
4218 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4219 * control a non client area.
4221 if (TWEAK_WineLook != WIN31_LOOK)
4223 if (es->style & WS_BORDER)
4225 es->style &= ~WS_BORDER;
4226 wnd->dwStyle &= ~WS_BORDER;
4227 wnd->dwExStyle |= WS_EX_CLIENTEDGE;
4232 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4233 wnd->dwStyle &= ~WS_BORDER;
4236 if (es->style & ES_COMBO)
4237 es->hwndListBox = GetDlgItem(hwndParent, ID_CB_LISTBOX);
4239 if (es->style & ES_MULTILINE) {
4240 es->buffer_limit = BUFLIMIT_MULTI;
4241 if (es->style & WS_VSCROLL)
4242 es->style |= ES_AUTOVSCROLL;
4243 if (es->style & WS_HSCROLL)
4244 es->style |= ES_AUTOHSCROLL;
4245 es->style &= ~ES_PASSWORD;
4246 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4247 if (es->style & ES_RIGHT)
4248 es->style &= ~ES_CENTER;
4249 es->style &= ~WS_HSCROLL;
4250 es->style &= ~ES_AUTOHSCROLL;
4253 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4254 es->style |= ES_AUTOVSCROLL;
4256 es->buffer_limit = BUFLIMIT_SINGLE;
4257 es->style &= ~ES_CENTER;
4258 es->style &= ~ES_RIGHT;
4259 es->style &= ~WS_HSCROLL;
4260 es->style &= ~WS_VSCROLL;
4261 es->style &= ~ES_AUTOVSCROLL;
4262 es->style &= ~ES_WANTRETURN;
4263 if (es->style & ES_UPPERCASE) {
4264 es->style &= ~ES_LOWERCASE;
4265 es->style &= ~ES_NUMBER;
4266 } else if (es->style & ES_LOWERCASE)
4267 es->style &= ~ES_NUMBER;
4268 if (es->style & ES_PASSWORD)
4269 es->password_char = '*';
4271 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4272 es->style |= ES_AUTOHSCROLL;
4275 alloc_size = ((es->buffer_size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
4276 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4278 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4280 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4282 es->undo_buffer_size = es->buffer_size;
4284 if (es->style & ES_MULTILINE)
4285 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4292 /*********************************************************************
4297 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam)
4306 BOOL rev = es->bEnableState &&
4307 ((es->flags & EF_FOCUSED) ||
4308 (es->style & ES_NOHIDESEL));
4310 dc = BeginPaint(wnd->hwndSelf, &ps);
4313 if(es->style & WS_BORDER) {
4314 GetClientRect(wnd->hwndSelf, &rc);
4315 if(es->style & ES_MULTILINE) {
4316 if(es->style & WS_HSCROLL) rc.bottom++;
4317 if(es->style & WS_VSCROLL) rc.right++;
4319 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4321 IntersectClipRect(dc, es->format_rect.left,
4322 es->format_rect.top,
4323 es->format_rect.right,
4324 es->format_rect.bottom);
4325 if (es->style & ES_MULTILINE) {
4326 GetClientRect(wnd->hwndSelf, &rc);
4327 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4330 old_font = SelectObject(dc, es->font);
4331 if ( get_app_version() >= 0x40000 &&(
4332 !es->bEnableState || (es->style & ES_READONLY)))
4333 EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
4335 EDIT_SEND_CTLCOLOR(wnd, dc);
4337 if (!es->bEnableState)
4338 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4339 GetClipBox(dc, &rcRgn);
4340 if (es->style & ES_MULTILINE) {
4341 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4342 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4343 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
4344 if (IntersectRect(&rc, &rcRgn, &rcLine))
4345 EDIT_PaintLine(wnd, es, dc, i, rev);
4348 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
4349 if (IntersectRect(&rc, &rcRgn, &rcLine))
4350 EDIT_PaintLine(wnd, es, dc, 0, rev);
4353 SelectObject(dc, old_font);
4356 EndPaint(wnd->hwndSelf, &ps);
4358 EDIT_UpdateScrollInfo(wnd, es);
4362 /*********************************************************************
4367 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
4372 OpenClipboard(wnd->hwndSelf);
4373 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4374 src = (LPWSTR)GlobalLock(hsrc);
4375 EDIT_EM_ReplaceSel(wnd, es, TRUE, src, TRUE);
4378 if (es->flags & EF_UPDATE) {
4379 es->flags &= ~EF_UPDATE;
4380 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
4387 /*********************************************************************
4392 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es)
4394 es->flags |= EF_FOCUSED;
4395 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4396 EDIT_SetCaretPos(wnd, es, es->selection_end,
4397 es->flags & EF_AFTER_WRAP);
4398 if(!(es->style & ES_NOHIDESEL))
4399 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
4400 ShowCaret(wnd->hwndSelf);
4401 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
4405 /*********************************************************************
4409 * With Win95 look the margins are set to default font value unless
4410 * the system font (font == 0) is being set, in which case they are left
4414 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
4422 dc = GetDC(wnd->hwndSelf);
4424 old_font = SelectObject(dc, font);
4425 GetTextMetricsW(dc, &tm);
4426 es->line_height = tm.tmHeight;
4427 es->char_width = tm.tmAveCharWidth;
4429 SelectObject(dc, old_font);
4430 ReleaseDC(wnd->hwndSelf, dc);
4431 if (font && (TWEAK_WineLook > WIN31_LOOK))
4432 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4433 EC_USEFONTINFO, EC_USEFONTINFO);
4435 /* Force the recalculation of the format rect for each font change */
4436 GetClientRect(wnd->hwndSelf, &r);
4437 EDIT_SetRectNP(wnd, es, &r);
4439 if (es->style & ES_MULTILINE)
4440 EDIT_BuildLineDefs_ML(wnd, es);
4442 EDIT_CalcLineWidth_SL(wnd, es);
4445 EDIT_UpdateText(wnd, NULL, TRUE);
4446 if (es->flags & EF_FOCUSED) {
4448 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4449 EDIT_SetCaretPos(wnd, es, es->selection_end,
4450 es->flags & EF_AFTER_WRAP);
4451 ShowCaret(wnd->hwndSelf);
4456 /*********************************************************************
4461 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4462 * The modified flag is reset. No notifications are sent.
4464 * For single-line controls, reception of WM_SETTEXT triggers:
4465 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4468 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPARAM lParam, BOOL unicode)
4473 text = (LPWSTR)lParam;
4476 LPCSTR textA = (LPCSTR)lParam;
4477 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4478 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4479 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4482 EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
4484 TRACE("%s\n", debugstr_w(text));
4485 EDIT_EM_ReplaceSel(wnd, es, FALSE, text, !(es->style & ES_MULTILINE));
4487 HeapFree(GetProcessHeap(), 0, text);
4489 static const WCHAR empty_stringW[] = {0};
4491 EDIT_EM_ReplaceSel(wnd, es, FALSE, empty_stringW, !(es->style & ES_MULTILINE));
4494 es->flags &= ~EF_MODIFIED;
4495 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
4496 EDIT_EM_ScrollCaret(wnd, es);
4498 if (es->flags & EF_UPDATE) {
4499 es->flags &= ~EF_UPDATE;
4500 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
4505 /*********************************************************************
4510 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
4512 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4514 TRACE("width = %d, height = %d\n", width, height);
4515 SetRect(&rc, 0, 0, width, height);
4516 EDIT_SetRectNP(wnd, es, &rc);
4517 EDIT_UpdateText(wnd, NULL, TRUE);
4522 /*********************************************************************
4527 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
4529 if ((key == VK_BACK) && (key_data & 0x2000)) {
4530 if (EDIT_EM_CanUndo(es))
4531 EDIT_EM_Undo(wnd, es);
4533 } else if (key == VK_UP || key == VK_DOWN) {
4534 if (EDIT_CheckCombo(wnd, es, WM_SYSKEYDOWN, key))
4537 return DefWindowProcW(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4541 /*********************************************************************
4546 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es)
4548 if (es->region_posx < 0) {
4549 EDIT_MoveBackward(wnd, es, TRUE);
4550 } else if (es->region_posx > 0) {
4551 EDIT_MoveForward(wnd, es, TRUE);
4554 * FIXME: gotta do some vertical scrolling here, like
4555 * EDIT_EM_LineScroll(wnd, 0, 1);
4560 /*********************************************************************
4564 * 16 bit notepad needs this. Actually it is not _our_ hack,
4565 * it is notepad's. Notepad is sending us scrollbar messages with
4566 * undocumented parameters without us even having a scrollbar ... !?!?
4569 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos)
4574 if (!(es->flags & EF_VSCROLL_HACK)) {
4575 ERR("hacked WM_VSCROLL handler invoked\n");
4576 ERR(" if you are _not_ running 16 bit notepad, please report\n");
4577 ERR(" (this message is only displayed once per edit control)\n");
4578 es->flags |= EF_VSCROLL_HACK;
4586 EDIT_EM_Scroll(wnd, es, action);
4592 dy = es->line_count - 1 - es->y_offset;
4595 es->flags |= EF_VSCROLL_TRACK;
4596 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
4598 case SB_THUMBPOSITION:
4599 es->flags &= ~EF_VSCROLL_TRACK;
4600 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
4601 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4607 * FIXME : the next two are undocumented !
4608 * Are we doing the right thing ?
4609 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4610 * although it's also a regular control message.
4613 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
4615 case EM_LINESCROLL16:
4620 ERR("undocumented (hacked) WM_VSCROLL parameter, please report\n");
4624 EDIT_EM_LineScroll(wnd, es, 0, dy);
4629 /*********************************************************************
4634 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
4638 if (!(es->style & ES_MULTILINE))
4641 if (!(es->style & ES_AUTOVSCROLL))
4644 if (!(es->style & WS_VSCROLL))
4645 return EDIT_VScroll_Hack(wnd, es, action, pos);
4653 EDIT_EM_Scroll(wnd, es, action);
4660 dy = es->line_count - 1 - es->y_offset;
4663 es->flags |= EF_VSCROLL_TRACK;
4664 dy = pos - es->y_offset;
4666 case SB_THUMBPOSITION:
4667 es->flags &= ~EF_VSCROLL_TRACK;
4668 if (!(dy = pos - es->y_offset)) {
4669 SetScrollPos(wnd->hwndSelf, SB_VERT, pos, TRUE);
4670 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4677 ERR("undocumented WM_VSCROLL action %d, please report\n",
4682 EDIT_EM_LineScroll(wnd, es, 0, dy);
4687 /*********************************************************************
4692 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase)
4694 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
4696 /* EF_UPDATE will be turned off in paint */
4697 if (es->flags & EF_UPDATE)
4698 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
4700 InvalidateRect(wnd->hwndSelf, rc, bErase);