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 INT 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 region_posx; /* Position of cursor relative to region: */
94 INT region_posy; /* -1: to left, 0: within, 1: to right */
95 EDITWORDBREAKPROC16 word_break_proc16;
96 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
97 INT line_count; /* number of lines */
98 INT y_offset; /* scroll offset in number of lines */
99 BOOL bCaptureState; /* flag indicating whether mouse was captured */
100 BOOL bEnableState; /* flag keeping the enable state */
101 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
103 * only for multi line controls
105 INT lock_count; /* amount of re-entries in the EditWndProc */
108 INT text_width; /* width of the widest line in pixels */
109 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
110 HLOCAL hloc32W; /* our unicode local memory block */
111 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
113 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
118 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
119 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
121 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
122 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
124 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
125 do {TRACE("notification " str " sent to hwnd=%08x\n", \
126 (UINT)(hwnd));} while(0)
128 /* used for disabled or read-only edit control */
129 #define EDIT_SEND_CTLCOLORSTATIC(wnd,hdc) \
130 (SendMessageW((wnd)->parent->hwndSelf, WM_CTLCOLORSTATIC, \
131 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
132 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
133 (SendMessageW((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
134 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
135 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
136 do {DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str); \
137 SendMessageW((wnd)->parent->hwndSelf, WM_COMMAND, \
138 MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
139 (LPARAM)(wnd)->hwndSelf);} while(0)
140 #define DPRINTF_EDIT_MSG16(str) \
142 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
143 (UINT)wnd->hwndSelf, (UINT)wParam, (UINT)lParam)
144 #define DPRINTF_EDIT_MSG32(str) \
146 "32 bit %c : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
147 unicode ? 'W' : 'A', \
148 (UINT)wnd->hwndSelf, (UINT)wParam, (UINT)lParam)
150 /*********************************************************************
157 * These functions have trivial implementations
158 * We still like to call them internally
159 * "static inline" makes them more like macro's
161 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
162 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
163 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
164 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
167 * Helper functions only valid for one type of control
169 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
170 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
171 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
172 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
173 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
174 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
176 * Helper functions valid for both single line _and_ multi line controls
178 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action);
179 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
180 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
181 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
182 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end);
183 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
184 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, UINT size);
185 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, INT size);
186 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend);
187 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend);
188 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend);
189 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend);
190 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend);
191 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend);
192 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
193 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
194 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos, BOOL after_wrap);
195 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT lprc);
196 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force);
197 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
199 * EM_XXX message handlers
201 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y);
202 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
203 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es);
204 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
205 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR lpch);
206 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end);
207 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
208 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
209 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
210 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
211 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy);
212 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap);
213 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update);
214 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action);
215 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
216 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc);
217 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
218 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
219 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
220 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, WCHAR c);
221 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
222 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
223 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
224 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, LPARAM lParam);
225 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
226 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
228 * WM_XXX message handlers
230 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, WCHAR c);
231 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
232 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, INT x, INT y);
233 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
234 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCWSTR name);
235 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
236 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc);
237 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
238 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos);
239 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key);
240 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es);
241 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es);
242 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
243 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es);
244 static LRESULT EDIT_WM_MButtonDown(WND *wnd);
245 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, INT x, INT y);
246 static LRESULT EDIT_WM_NCCreate(WND *wnd, DWORD style, HWND hwndParent, BOOL unicode);
247 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam);
248 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
249 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es);
250 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw);
251 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPARAM lParam, BOOL unicode);
252 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height);
253 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
254 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es);
255 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos);
256 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase);
258 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
259 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
261 /*********************************************************************
262 * edit class descriptor
264 const struct builtin_class_descr EDIT_builtin_class =
267 CS_GLOBALCLASS | CS_DBLCLKS /*| CS_PARENTDC*/, /* style */
268 EditWndProcA, /* procA */
269 EditWndProcW, /* procW */
270 sizeof(EDITSTATE *), /* extra */
271 IDC_IBEAMA, /* cursor */
276 /*********************************************************************
281 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
283 return (es->undo_insert_count || strlenW(es->undo_text));
287 /*********************************************************************
292 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
294 es->undo_insert_count = 0;
295 *es->undo_text = '\0';
299 /*********************************************************************
304 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
306 static const WCHAR empty_stringW[] = {0};
308 EDIT_EM_ReplaceSel(wnd, es, TRUE, empty_stringW, TRUE);
310 if (es->flags & EF_UPDATE) {
311 es->flags &= ~EF_UPDATE;
312 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
317 /*********************************************************************
322 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
324 EDIT_WM_Copy(wnd, es);
325 EDIT_WM_Clear(wnd, es);
329 /**********************************************************************
332 * Returns the window version in case Wine emulates a later version
333 * of windows then the application expects.
335 * In a number of cases when windows runs an application that was
336 * designed for an earlier windows version, windows reverts
337 * to "old" behaviour of that earlier version.
339 * An example is a disabled edit control that needs to be painted.
340 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
341 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
342 * applications with an expected version 0f 4.0 or higher.
345 static DWORD get_app_version(void)
347 static DWORD version;
350 DWORD dwEmulatedVersion;
352 DWORD dwProcVersion = GetProcessVersion(0);
354 GetVersionExW( &info );
355 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
356 /* fixme: this may not be 100% correct; see discussion on the
357 * wine developer list in Nov 1999 */
358 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
364 /*********************************************************************
368 * The messages are in the order of the actual integer values
369 * (which can be found in include/windows.h)
370 * Whereever possible the 16 bit versions are converted to
371 * the 32 bit ones, so that we can 'fall through' to the
372 * helper functions. These are mostly 32 bit (with a few
373 * exceptions, clearly indicated by a '16' extension to their
377 static LRESULT WINAPI EditWndProc_locked( WND *wnd, UINT msg,
378 WPARAM wParam, LPARAM lParam, BOOL unicode )
380 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
385 DPRINTF_EDIT_MSG32("WM_DESTROY");
386 EDIT_WM_Destroy(wnd, es);
391 DPRINTF_EDIT_MSG32("WM_NCCREATE");
394 LPCREATESTRUCTW cs = (LPCREATESTRUCTW)lParam;
395 result = EDIT_WM_NCCreate(wnd, cs->style, cs->hwndParent, TRUE);
399 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
400 result = EDIT_WM_NCCreate(wnd, cs->style, cs->hwndParent, FALSE);
408 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
410 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
415 EDIT_LockBuffer(wnd, es);
418 DPRINTF_EDIT_MSG16("EM_GETSEL");
423 DPRINTF_EDIT_MSG32("EM_GETSEL");
424 result = EDIT_EM_GetSel(es, (LPUINT)wParam, (LPUINT)lParam);
428 DPRINTF_EDIT_MSG16("EM_SETSEL");
429 if (SLOWORD(lParam) == -1)
430 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
432 EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
434 EDIT_EM_ScrollCaret(wnd, es);
438 DPRINTF_EDIT_MSG32("EM_SETSEL");
439 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
440 EDIT_EM_ScrollCaret(wnd, es);
445 DPRINTF_EDIT_MSG16("EM_GETRECT");
447 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
450 DPRINTF_EDIT_MSG32("EM_GETRECT");
452 CopyRect((LPRECT)lParam, &es->format_rect);
456 DPRINTF_EDIT_MSG16("EM_SETRECT");
457 if ((es->style & ES_MULTILINE) && lParam) {
459 CONV_RECT16TO32(MapSL(lParam), &rc);
460 EDIT_SetRectNP(wnd, es, &rc);
461 EDIT_UpdateText(wnd, NULL, TRUE);
465 DPRINTF_EDIT_MSG32("EM_SETRECT");
466 if ((es->style & ES_MULTILINE) && lParam) {
467 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
468 EDIT_UpdateText(wnd, NULL, TRUE);
473 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
474 if ((es->style & ES_MULTILINE) && lParam) {
476 CONV_RECT16TO32(MapSL(lParam), &rc);
477 EDIT_SetRectNP(wnd, es, &rc);
481 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
482 if ((es->style & ES_MULTILINE) && lParam)
483 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
487 DPRINTF_EDIT_MSG16("EM_SCROLL");
490 DPRINTF_EDIT_MSG32("EM_SCROLL");
491 result = EDIT_EM_Scroll(wnd, es, (INT)wParam);
494 case EM_LINESCROLL16:
495 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
496 wParam = (WPARAM)(INT)SHIWORD(lParam);
497 lParam = (LPARAM)(INT)SLOWORD(lParam);
500 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
501 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT)wParam, (INT)lParam);
504 case EM_SCROLLCARET16:
505 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
508 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
509 EDIT_EM_ScrollCaret(wnd, es);
514 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
517 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
518 result = ((es->flags & EF_MODIFIED) != 0);
522 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
525 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
527 es->flags |= EF_MODIFIED;
529 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
532 case EM_GETLINECOUNT16:
533 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
535 case EM_GETLINECOUNT:
536 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
537 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
541 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
542 if ((INT16)wParam == -1)
546 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
547 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
551 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
552 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
555 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
556 EDIT_EM_SetHandle(wnd, es, (HLOCAL)wParam);
560 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
561 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
564 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
565 result = (LRESULT)EDIT_EM_GetHandle(wnd, es);
569 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
572 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
573 result = EDIT_EM_GetThumb(wnd, es);
576 /* messages 0x00bf and 0x00c0 missing from specs */
579 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
582 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
584 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
586 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
590 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
593 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
595 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
597 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
600 case EM_LINELENGTH16:
601 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
604 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
605 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
608 case EM_REPLACESEL16:
609 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
610 lParam = (LPARAM)MapSL(lParam);
615 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
618 textW = (LPWSTR)lParam;
621 LPSTR textA = (LPSTR)lParam;
622 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
623 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
624 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
627 EDIT_EM_ReplaceSel(wnd, es, (BOOL)wParam, textW, TRUE);
628 if (es->flags & EF_UPDATE) {
629 es->flags &= ~EF_UPDATE;
630 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
635 HeapFree(GetProcessHeap(), 0, textW);
638 /* message 0x00c3 missing from specs */
641 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
644 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
646 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
648 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
652 DPRINTF_EDIT_MSG16("EM_GETLINE");
653 lParam = (LPARAM)MapSL(lParam);
656 DPRINTF_EDIT_MSG32("EM_GETLINE");
657 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam);
661 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
663 case EM_SETLIMITTEXT:
664 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
665 EDIT_EM_SetLimitText(es, (INT)wParam);
669 DPRINTF_EDIT_MSG16("EM_CANUNDO");
672 DPRINTF_EDIT_MSG32("EM_CANUNDO");
673 result = (LRESULT)EDIT_EM_CanUndo(es);
677 DPRINTF_EDIT_MSG16("EM_UNDO");
682 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
683 result = (LRESULT)EDIT_EM_Undo(wnd, es);
687 DPRINTF_EDIT_MSG16("EM_FMTLINES");
690 DPRINTF_EDIT_MSG32("EM_FMTLINES");
691 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
694 case EM_LINEFROMCHAR16:
695 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
697 case EM_LINEFROMCHAR:
698 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
699 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
702 /* message 0x00ca missing from specs */
705 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
708 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
710 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
712 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
715 case EM_SETTABSTOPS16:
716 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
717 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
720 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
721 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
724 case EM_SETPASSWORDCHAR16:
725 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
727 case EM_SETPASSWORDCHAR:
730 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
733 charW = (WCHAR)wParam;
737 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
740 EDIT_EM_SetPasswordChar(wnd, es, charW);
744 case EM_EMPTYUNDOBUFFER16:
745 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
747 case EM_EMPTYUNDOBUFFER:
748 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
749 EDIT_EM_EmptyUndoBuffer(es);
752 case EM_GETFIRSTVISIBLELINE16:
753 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
754 result = es->y_offset;
756 case EM_GETFIRSTVISIBLELINE:
757 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
758 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
761 case EM_SETREADONLY16:
762 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
765 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
767 wnd->dwStyle |= ES_READONLY;
768 es->style |= ES_READONLY;
770 wnd->dwStyle &= ~ES_READONLY;
771 es->style &= ~ES_READONLY;
776 case EM_SETWORDBREAKPROC16:
777 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
778 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
780 case EM_SETWORDBREAKPROC:
781 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
782 EDIT_EM_SetWordBreakProc(wnd, es, lParam);
785 case EM_GETWORDBREAKPROC16:
786 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
787 result = (LRESULT)es->word_break_proc16;
789 case EM_GETWORDBREAKPROC:
790 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
791 result = (LRESULT)es->word_break_proc;
794 case EM_GETPASSWORDCHAR16:
795 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
797 case EM_GETPASSWORDCHAR:
799 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
802 result = es->password_char;
805 WCHAR charW = es->password_char;
807 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
813 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
816 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
817 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
821 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
822 result = MAKELONG(es->left_margin, es->right_margin);
825 case EM_GETLIMITTEXT:
826 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
827 result = es->buffer_limit;
831 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
832 result = EDIT_EM_PosFromChar(wnd, es, (INT)wParam, FALSE);
836 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
837 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
841 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
842 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
844 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
846 int vk = (int)((LPMSG)lParam)->wParam;
848 if ((wnd->dwStyle & ES_WANTRETURN) && vk == VK_RETURN)
850 result |= DLGC_WANTMESSAGE;
852 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
854 if (SendMessageW(wnd->parent->hwndSelf, CB_GETDROPPEDSTATE, 0, 0))
855 result |= DLGC_WANTMESSAGE;
863 DPRINTF_EDIT_MSG32("WM_CHAR");
870 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
873 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
875 if (SendMessageW(wnd->parent->hwndSelf, CB_GETDROPPEDSTATE, 0, 0))
876 SendMessageW(wnd->parent->hwndSelf, WM_KEYDOWN, charW, 0);
879 EDIT_WM_Char(wnd, es, charW);
884 DPRINTF_EDIT_MSG32("WM_CLEAR");
885 EDIT_WM_Clear(wnd, es);
889 DPRINTF_EDIT_MSG32("WM_COMMAND");
890 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
894 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
895 EDIT_WM_ContextMenu(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
899 DPRINTF_EDIT_MSG32("WM_COPY");
900 EDIT_WM_Copy(wnd, es);
904 DPRINTF_EDIT_MSG32("WM_CREATE");
906 result = EDIT_WM_Create(wnd, es, ((LPCREATESTRUCTW)lParam)->lpszName);
909 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
913 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
914 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
915 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
917 result = EDIT_WM_Create(wnd, es, nameW);
919 HeapFree(GetProcessHeap(), 0, nameW);
924 DPRINTF_EDIT_MSG32("WM_CUT");
925 EDIT_WM_Cut(wnd, es);
929 DPRINTF_EDIT_MSG32("WM_ENABLE");
930 es->bEnableState = (BOOL) wParam;
931 EDIT_UpdateText(wnd, NULL, TRUE);
935 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
936 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC)wParam);
940 DPRINTF_EDIT_MSG32("WM_GETFONT");
941 result = (LRESULT)es->font;
945 DPRINTF_EDIT_MSG32("WM_GETTEXT");
946 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
949 case WM_GETTEXTLENGTH:
950 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
951 result = strlenW(es->text);
955 DPRINTF_EDIT_MSG32("WM_HSCROLL");
956 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam));
960 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
961 result = EDIT_WM_KeyDown(wnd, es, (INT)wParam);
965 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
966 result = EDIT_WM_KillFocus(wnd, es);
969 case WM_LBUTTONDBLCLK:
970 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
971 result = EDIT_WM_LButtonDblClk(wnd, es);
975 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
976 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
980 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
981 result = EDIT_WM_LButtonUp(wnd->hwndSelf, es);
985 DPRINTF_EDIT_MSG32("WM_MBUTTONDOWN");
986 result = EDIT_WM_MButtonDown(wnd);
989 case WM_MOUSEACTIVATE:
991 * FIXME: maybe DefWindowProc() screws up, but it seems that
992 * modeless dialog boxes need this. If we don't do this, the focus
993 * will _not_ be set by DefWindowProc() for edit controls in a
994 * modeless dialog box ???
996 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
997 SetFocus(wnd->hwndSelf);
998 result = MA_ACTIVATE;
1003 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
1005 result = EDIT_WM_MouseMove(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
1009 DPRINTF_EDIT_MSG32("WM_PAINT");
1010 EDIT_WM_Paint(wnd, es, wParam);
1014 DPRINTF_EDIT_MSG32("WM_PASTE");
1015 EDIT_WM_Paste(wnd, es);
1019 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
1020 EDIT_WM_SetFocus(wnd, es);
1024 DPRINTF_EDIT_MSG32("WM_SETFONT");
1025 EDIT_WM_SetFont(wnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
1029 DPRINTF_EDIT_MSG32("WM_SETTEXT");
1030 EDIT_WM_SetText(wnd, es, lParam, unicode);
1035 DPRINTF_EDIT_MSG32("WM_SIZE");
1036 EDIT_WM_Size(wnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
1040 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
1041 result = EDIT_WM_SysKeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
1045 DPRINTF_EDIT_MSG32("WM_TIMER");
1046 EDIT_WM_Timer(wnd, es);
1050 DPRINTF_EDIT_MSG32("WM_VSCROLL");
1051 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam));
1056 int gcWheelDelta = 0;
1057 UINT pulScrollLines = 3;
1058 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1060 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1062 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
1064 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
1067 gcWheelDelta -= SHIWORD(wParam);
1068 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1070 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1071 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1072 result = EDIT_EM_LineScroll(wnd, es, 0, cLineScroll);
1078 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
1080 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
1083 EDIT_UnlockBuffer(wnd, es, FALSE);
1088 /*********************************************************************
1090 * EditWndProcW (USER32.@)
1092 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1095 WND *wndPtr = WIN_FindWndPtr(hWnd);
1097 res = EditWndProc_locked(wndPtr, uMsg, wParam, lParam, TRUE);
1099 WIN_ReleaseWndPtr(wndPtr);
1103 /*********************************************************************
1105 * EditWndProcA (USER32.@)
1107 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1110 WND *wndPtr = WIN_FindWndPtr(hWnd);
1112 res = EditWndProc_locked(wndPtr, uMsg, wParam, lParam, FALSE);
1114 WIN_ReleaseWndPtr(wndPtr);
1118 /*********************************************************************
1120 * EDIT_BuildLineDefs_ML
1122 * Build linked list of text lines.
1123 * Lines can end with '\0' (last line), a character (if it is wrapped),
1124 * a soft return '\r\r\n' or a hard return '\r\n'
1127 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
1133 LINEDEF *current_def;
1134 LINEDEF **previous_next;
1136 current_def = es->first_line_def;
1138 LINEDEF *next_def = current_def->next;
1139 HeapFree(GetProcessHeap(), 0, current_def);
1140 current_def = next_def;
1141 } while (current_def);
1145 dc = GetDC(wnd->hwndSelf);
1147 old_font = SelectObject(dc, es->font);
1149 fw = es->format_rect.right - es->format_rect.left;
1151 previous_next = &es->first_line_def;
1153 current_def = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1154 current_def->next = NULL;
1157 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1162 current_def->ending = END_0;
1163 current_def->net_length = strlenW(start);
1164 } else if ((cp > start) && (*(cp - 1) == '\r')) {
1165 current_def->ending = END_SOFT;
1166 current_def->net_length = cp - start - 1;
1168 current_def->ending = END_HARD;
1169 current_def->net_length = cp - start;
1171 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1172 start, current_def->net_length,
1173 es->tabs_count, es->tabs));
1174 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1175 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
1180 next = EDIT_CallWordBreakProc(wnd, es, start - es->text,
1181 prev + 1, current_def->net_length, WB_RIGHT);
1182 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1183 start, next, es->tabs_count, es->tabs));
1184 } while (current_def->width <= fw);
1190 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1191 start, next, es->tabs_count, es->tabs));
1192 } while (current_def->width <= fw);
1196 current_def->net_length = prev;
1197 current_def->ending = END_WRAP;
1198 current_def->width = (INT)LOWORD(GetTabbedTextExtentW(dc, start,
1199 current_def->net_length, es->tabs_count, es->tabs));
1201 switch (current_def->ending) {
1203 current_def->length = current_def->net_length + 3;
1206 current_def->length = current_def->net_length + 2;
1210 current_def->length = current_def->net_length;
1213 es->text_width = max(es->text_width, current_def->width);
1214 start += current_def->length;
1215 *previous_next = current_def;
1216 previous_next = ¤t_def->next;
1218 } while (current_def->ending != END_0);
1220 SelectObject(dc, old_font);
1221 ReleaseDC(wnd->hwndSelf, dc);
1225 /*********************************************************************
1227 * EDIT_CallWordBreakProc
1229 * Call appropriate WordBreakProc (internal or external).
1231 * Note: The "start" argument should always be an index refering
1232 * to es->text. The actual wordbreak proc might be
1233 * 16 bit, so we can't always pass any 32 bit LPSTR.
1234 * Hence we assume that es->text is the buffer that holds
1235 * the string under examination (we can decide this for ourselves).
1238 /* ### start build ### */
1239 extern WORD CALLBACK EDIT_CallTo16_word_lwww(EDITWORDBREAKPROC16,SEGPTR,WORD,WORD,WORD);
1240 /* ### stop build ### */
1241 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action)
1243 INT ret, iWndsLocks;
1245 /* To avoid any deadlocks, all the locks on the windows structures
1246 must be suspended before the control is passed to the application */
1247 iWndsLocks = WIN_SuspendWndsLock();
1249 if (es->word_break_proc16) {
1254 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1255 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1256 segptr = WIN16_GlobalLock16(hglob16);
1257 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1258 ret = (INT)EDIT_CallTo16_word_lwww(es->word_break_proc16,
1259 segptr, index, countA, action);
1260 GlobalUnlock16(hglob16);
1261 GlobalFree16(hglob16);
1263 else if (es->word_break_proc)
1267 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1269 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1270 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1271 ret = wbpW(es->text + start, index, count, action);
1275 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1279 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1280 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1281 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1282 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1283 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1284 ret = wbpA(textA, index, countA, action);
1285 HeapFree(GetProcessHeap(), 0, textA);
1289 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1291 WIN_RestoreWndsLock(iWndsLocks);
1296 /*********************************************************************
1300 * Beware: This is not the function called on EM_CHARFROMPOS
1301 * The position _can_ be outside the formatting / client
1303 * The return value is only the character index
1306 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1312 if (es->style & ES_MULTILINE) {
1313 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1315 LINEDEF *line_def = es->first_line_def;
1317 while ((line > 0) && line_def->next) {
1318 line_index += line_def->length;
1319 line_def = line_def->next;
1322 x += es->x_offset - es->format_rect.left;
1323 if (x >= line_def->width) {
1325 *after_wrap = (line_def->ending == END_WRAP);
1326 return line_index + line_def->net_length;
1330 *after_wrap = FALSE;
1333 dc = GetDC(wnd->hwndSelf);
1335 old_font = SelectObject(dc, es->font);
1336 low = line_index + 1;
1337 high = line_index + line_def->net_length + 1;
1338 while (low < high - 1)
1340 INT mid = (low + high) / 2;
1341 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1347 *after_wrap = ((index == line_index + line_def->net_length) &&
1348 (line_def->ending == END_WRAP));
1353 *after_wrap = FALSE;
1354 x -= es->format_rect.left;
1356 return es->x_offset;
1357 text = EDIT_GetPasswordPointer_SL(es);
1358 dc = GetDC(wnd->hwndSelf);
1360 old_font = SelectObject(dc, es->font);
1364 INT high = es->x_offset;
1365 while (low < high - 1)
1367 INT mid = (low + high) / 2;
1368 GetTextExtentPoint32W( dc, text + mid,
1369 es->x_offset - mid, &size );
1370 if (size.cx > -x) low = mid;
1377 INT low = es->x_offset;
1378 INT high = strlenW(es->text) + 1;
1379 while (low < high - 1)
1381 INT mid = (low + high) / 2;
1382 GetTextExtentPoint32W( dc, text + es->x_offset,
1383 mid - es->x_offset, &size );
1384 if (size.cx > x) high = mid;
1389 if (es->style & ES_PASSWORD)
1390 HeapFree(GetProcessHeap(), 0, text);
1393 SelectObject(dc, old_font);
1394 ReleaseDC(wnd->hwndSelf, dc);
1399 /*********************************************************************
1403 * adjusts the point to be within the formatting rectangle
1404 * (so CharFromPos returns the nearest _visible_ character)
1407 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1409 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1410 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1414 /*********************************************************************
1418 * Calculates the bounding rectangle for a line from a starting
1419 * column to an ending column.
1422 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1424 INT line_index = EDIT_EM_LineIndex(es, line);
1426 if (es->style & ES_MULTILINE)
1427 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1429 rc->top = es->format_rect.top;
1430 rc->bottom = rc->top + es->line_height;
1431 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1432 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1436 /*********************************************************************
1438 * EDIT_GetPasswordPointer_SL
1440 * note: caller should free the (optionally) allocated buffer
1443 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1445 if (es->style & ES_PASSWORD) {
1446 INT len = strlenW(es->text);
1447 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1449 while(--len) text[len] = es->password_char;
1456 /*********************************************************************
1460 * This acts as a LOCAL_Lock(), but it locks only once. This way
1461 * you can call it whenever you like, without unlocking.
1464 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1467 ERR("no EDITSTATE ... please report\n");
1473 BOOL _16bit = FALSE;
1477 TRACE("Locking 32-bit UNICODE buffer\n");
1478 es->text = LocalLock(es->hloc32W);
1482 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1483 countA = LocalSize(es->hloc32A);
1484 textA = LocalLock(es->hloc32A);
1488 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1489 countA = LOCAL_Size(wnd->hInstance, es->hloc16);
1490 textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
1495 ERR("no buffer ... please report\n");
1501 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size);
1503 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1505 LocalUnlock(es->hloc32A);
1512 /*********************************************************************
1514 * EDIT_SL_InvalidateText
1516 * Called from EDIT_InvalidateText().
1517 * Does the job for single-line controls only.
1520 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1525 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1526 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1527 EDIT_UpdateText(wnd, &rc, FALSE);
1531 /*********************************************************************
1533 * EDIT_ML_InvalidateText
1535 * Called from EDIT_InvalidateText().
1536 * Does the job for multi-line controls only.
1539 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1541 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1542 INT sl = EDIT_EM_LineFromChar(es, start);
1543 INT el = EDIT_EM_LineFromChar(es, end);
1552 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1555 sc = start - EDIT_EM_LineIndex(es, sl);
1556 ec = end - EDIT_EM_LineIndex(es, el);
1557 if (sl < es->y_offset) {
1561 if (el > es->y_offset + vlc) {
1562 el = es->y_offset + vlc;
1563 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1565 GetClientRect(wnd->hwndSelf, &rc1);
1566 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1568 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1569 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1570 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1572 EDIT_GetLineRect(wnd, es, sl, sc,
1573 EDIT_EM_LineLength(es,
1574 EDIT_EM_LineIndex(es, sl)),
1576 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1577 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1578 for (l = sl + 1 ; l < el ; l++) {
1579 EDIT_GetLineRect(wnd, es, l, 0,
1580 EDIT_EM_LineLength(es,
1581 EDIT_EM_LineIndex(es, l)),
1583 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1584 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1586 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1587 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1588 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1593 /*********************************************************************
1595 * EDIT_InvalidateText
1597 * Invalidate the text from offset start upto, but not including,
1598 * offset end. Useful for (re)painting the selection.
1599 * Regions outside the linewidth are not invalidated.
1600 * end == -1 means end == TextLength.
1601 * start and end need not be ordered.
1604 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1610 end = strlenW(es->text);
1612 ORDER_INT(start, end);
1614 if (es->style & ES_MULTILINE)
1615 EDIT_ML_InvalidateText(wnd, es, start, end);
1617 EDIT_SL_InvalidateText(wnd, es, start, end);
1621 /*********************************************************************
1625 * Try to fit size + 1 characters in the buffer. Constrain to limits.
1628 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, UINT size)
1632 if (size <= es->buffer_size)
1634 if (size > es->buffer_limit) {
1635 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1638 if (size > es->buffer_limit)
1639 size = es->buffer_limit;
1641 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1643 /* Force edit to unlock it's buffer. es->text now NULL */
1644 EDIT_UnlockBuffer(wnd, es, TRUE);
1647 UINT alloc_size = ((size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
1648 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1649 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32W, hNew32W);
1650 es->hloc32W = hNew32W;
1651 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1655 EDIT_LockBuffer(wnd, es);
1657 if (es->buffer_size < size) {
1658 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1659 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1662 TRACE("We now have %d+1\n", es->buffer_size);
1668 /*********************************************************************
1672 * Try to fit size + 1 bytes in the undo buffer.
1675 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, INT size)
1679 if (size <= es->undo_buffer_size)
1682 TRACE("trying to ReAlloc to %d+1\n", size);
1684 alloc_size = ((size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
1685 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1686 es->undo_buffer_size = alloc_size/sizeof(WCHAR);
1691 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1697 /*********************************************************************
1702 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1704 INT e = es->selection_end;
1708 if ((es->style & ES_MULTILINE) && e &&
1709 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1711 if (e && (es->text[e - 1] == '\r'))
1715 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1716 EDIT_EM_ScrollCaret(wnd, es);
1720 /*********************************************************************
1724 * Only for multi line controls
1725 * Move the caret one line down, on a column with the nearest
1726 * x coordinate on the screen (might be a different column).
1729 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1731 INT s = es->selection_start;
1732 INT e = es->selection_end;
1733 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1734 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1735 INT x = SLOWORD(pos);
1736 INT y = SHIWORD(pos);
1738 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1741 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1742 EDIT_EM_ScrollCaret(wnd, es);
1746 /*********************************************************************
1751 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1753 BOOL after_wrap = FALSE;
1756 /* Pass a high value in x to make sure of receiving the end of the line */
1757 if (es->style & ES_MULTILINE)
1758 e = EDIT_CharFromPos(wnd, es, 0x3fffffff,
1759 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1761 e = strlenW(es->text);
1762 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1763 EDIT_EM_ScrollCaret(wnd, es);
1767 /*********************************************************************
1772 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1774 INT e = es->selection_end;
1778 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1779 if (es->text[e] == '\n')
1781 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1785 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1786 EDIT_EM_ScrollCaret(wnd, es);
1790 /*********************************************************************
1794 * Home key: move to beginning of line.
1797 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
1801 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1802 if (es->style & ES_MULTILINE)
1803 e = EDIT_CharFromPos(wnd, es, -es->x_offset,
1804 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1807 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1808 EDIT_EM_ScrollCaret(wnd, es);
1812 /*********************************************************************
1814 * EDIT_MovePageDown_ML
1816 * Only for multi line controls
1817 * Move the caret one page down, on a column with the nearest
1818 * x coordinate on the screen (might be a different column).
1821 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1823 INT s = es->selection_start;
1824 INT e = es->selection_end;
1825 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1826 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1827 INT x = SLOWORD(pos);
1828 INT y = SHIWORD(pos);
1830 e = EDIT_CharFromPos(wnd, es, x,
1831 y + (es->format_rect.bottom - es->format_rect.top),
1835 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1836 EDIT_EM_ScrollCaret(wnd, es);
1840 /*********************************************************************
1842 * EDIT_MovePageUp_ML
1844 * Only for multi line controls
1845 * Move the caret one page up, on a column with the nearest
1846 * x coordinate on the screen (might be a different column).
1849 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1851 INT s = es->selection_start;
1852 INT e = es->selection_end;
1853 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1854 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1855 INT x = SLOWORD(pos);
1856 INT y = SHIWORD(pos);
1858 e = EDIT_CharFromPos(wnd, es, x,
1859 y - (es->format_rect.bottom - es->format_rect.top),
1863 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1864 EDIT_EM_ScrollCaret(wnd, es);
1868 /*********************************************************************
1872 * Only for multi line controls
1873 * Move the caret one line up, on a column with the nearest
1874 * x coordinate on the screen (might be a different column).
1877 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1879 INT s = es->selection_start;
1880 INT e = es->selection_end;
1881 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1882 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1883 INT x = SLOWORD(pos);
1884 INT y = SHIWORD(pos);
1886 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1889 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1890 EDIT_EM_ScrollCaret(wnd, es);
1894 /*********************************************************************
1896 * EDIT_MoveWordBackward
1899 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1901 INT s = es->selection_start;
1902 INT e = es->selection_end;
1907 l = EDIT_EM_LineFromChar(es, e);
1908 ll = EDIT_EM_LineLength(es, e);
1909 li = EDIT_EM_LineIndex(es, l);
1912 li = EDIT_EM_LineIndex(es, l - 1);
1913 e = li + EDIT_EM_LineLength(es, li);
1916 e = li + (INT)EDIT_CallWordBreakProc(wnd, es,
1917 li, e - li, ll, WB_LEFT);
1921 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1922 EDIT_EM_ScrollCaret(wnd, es);
1926 /*********************************************************************
1928 * EDIT_MoveWordForward
1931 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
1933 INT s = es->selection_start;
1934 INT e = es->selection_end;
1939 l = EDIT_EM_LineFromChar(es, e);
1940 ll = EDIT_EM_LineLength(es, e);
1941 li = EDIT_EM_LineIndex(es, l);
1943 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1944 e = EDIT_EM_LineIndex(es, l + 1);
1946 e = li + EDIT_CallWordBreakProc(wnd, es,
1947 li, e - li + 1, ll, WB_RIGHT);
1951 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1952 EDIT_EM_ScrollCaret(wnd, es);
1956 /*********************************************************************
1961 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
1963 INT s = es->selection_start;
1964 INT e = es->selection_end;
1971 if (es->style & ES_MULTILINE) {
1972 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1973 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1978 TRACE("line=%d\n", line);
1980 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(es, line), FALSE);
1983 li = EDIT_EM_LineIndex(es, line);
1984 ll = EDIT_EM_LineLength(es, li);
1985 s = es->selection_start;
1986 e = es->selection_end;
1988 s = min(li + ll, max(li, s));
1989 e = min(li + ll, max(li, e));
1990 if (rev && (s != e) &&
1991 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
1992 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
1993 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
1994 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
1996 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2000 /*********************************************************************
2005 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2015 BkColor = GetBkColor(dc);
2016 TextColor = GetTextColor(dc);
2018 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2019 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2021 li = EDIT_EM_LineIndex(es, line);
2022 if (es->style & ES_MULTILINE) {
2023 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2024 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2026 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2027 TextOutW(dc, x, y, text + li + col, count);
2028 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2030 if (es->style & ES_PASSWORD)
2031 HeapFree(GetProcessHeap(), 0, text);
2034 SetBkColor(dc, BkColor);
2035 SetTextColor(dc, TextColor);
2041 /*********************************************************************
2046 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
2049 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
2050 INT x = SLOWORD(res);
2051 INT y = SHIWORD(res);
2053 if(x < es->format_rect.left)
2054 x = es->format_rect.left;
2055 if(x > es->format_rect.right - 2)
2056 x = es->format_rect.right - 2;
2057 if(y > es->format_rect.bottom)
2058 y = es->format_rect.bottom;
2059 if(y < es->format_rect.top)
2060 y = es->format_rect.top;
2066 /*********************************************************************
2070 * note: this is not (exactly) the handler called on EM_SETRECTNP
2071 * it is also used to set the rect of a single line control
2074 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
2076 CopyRect(&es->format_rect, rc);
2077 if (es->style & WS_BORDER) {
2078 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2079 if(TWEAK_WineLook == WIN31_LOOK)
2081 es->format_rect.left += bw;
2082 es->format_rect.top += bw;
2083 es->format_rect.right -= bw;
2084 es->format_rect.bottom -= bw;
2086 es->format_rect.left += es->left_margin;
2087 es->format_rect.right -= es->right_margin;
2088 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2089 if (es->style & ES_MULTILINE)
2090 es->format_rect.bottom = es->format_rect.top +
2091 max(1, (es->format_rect.bottom - es->format_rect.top) / es->line_height) * es->line_height;
2093 es->format_rect.bottom = es->format_rect.top + es->line_height;
2094 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2095 EDIT_BuildLineDefs_ML(wnd, es);
2099 /*********************************************************************
2104 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
2107 ERR("no EDITSTATE ... please report\n");
2110 if (!es->lock_count) {
2111 ERR("lock_count == 0 ... please report\n");
2115 ERR("es->text == 0 ... please report\n");
2119 if (force || (es->lock_count == 1)) {
2122 BOOL _16bit = FALSE;
2127 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2128 countA = LocalSize(es->hloc32A);
2129 textA = LocalLock(es->hloc32A);
2133 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2134 countA = LOCAL_Size(wnd->hInstance, es->hloc16);
2135 textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
2141 WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, countA, NULL, NULL);
2143 LOCAL_Unlock(wnd->hInstance, es->hloc16);
2145 LocalUnlock(es->hloc32A);
2148 LocalUnlock(es->hloc32W);
2152 ERR("no buffer ... please report\n");
2160 /*********************************************************************
2162 * EDIT_WordBreakProc
2164 * Find the beginning of words.
2165 * Note: unlike the specs for a WordBreakProc, this function only
2166 * allows to be called without linebreaks between s[0] upto
2167 * s[count - 1]. Remember it is only called
2168 * internally, so we can decide this for ourselves.
2171 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2175 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2185 if (s[index] == ' ') {
2186 while (index && (s[index] == ' '))
2189 while (index && (s[index] != ' '))
2191 if (s[index] == ' ')
2195 while (index && (s[index] != ' '))
2197 if (s[index] == ' ')
2207 if (s[index] == ' ')
2208 while ((index < count) && (s[index] == ' ')) index++;
2210 while (s[index] && (s[index] != ' ') && (index < count))
2212 while ((s[index] == ' ') && (index < count)) index++;
2216 case WB_ISDELIMITER:
2217 ret = (s[index] == ' ');
2220 ERR("unknown action code, please report !\n");
2227 /*********************************************************************
2231 * returns line number (not index) in high-order word of result.
2232 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2233 * to Richedit, not to the edit control. Original documentation is valid.
2234 * FIXME: do the specs mean to return -1 if outside client area or
2235 * if outside formatting rectangle ???
2238 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y)
2246 GetClientRect(wnd->hwndSelf, &rc);
2247 if (!PtInRect(&rc, pt))
2250 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
2251 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2255 /*********************************************************************
2259 * Enable or disable soft breaks.
2261 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2263 es->flags &= ~EF_USE_SOFTBRK;
2265 es->flags |= EF_USE_SOFTBRK;
2266 FIXME("soft break enabled, not implemented\n");
2272 /*********************************************************************
2276 * Hopefully this won't fire back at us.
2277 * We always start with a fixed buffer in the local heap.
2278 * Despite of the documenation says that the local heap is used
2279 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2280 * buffer on the local heap.
2283 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es)
2287 if (!(es->style & ES_MULTILINE))
2291 hLocal = es->hloc32W;
2298 TRACE("Allocating 32-bit ANSI alias buffer\n");
2299 countA = WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, NULL, 0, NULL, NULL);
2300 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countA)))
2302 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", countA);
2305 textA = LocalLock(es->hloc32A);
2306 WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, countA, NULL, NULL);
2307 LocalUnlock(es->hloc32A);
2309 hLocal = es->hloc32A;
2312 TRACE("Returning %04X, LocalSize() = %d\n", hLocal, LocalSize(hLocal));
2317 /*********************************************************************
2321 * Hopefully this won't fire back at us.
2322 * We always start with a buffer in 32 bit linear memory.
2323 * However, with this message a 16 bit application requests
2324 * a handle of 16 bit local heap memory, where it expects to find
2326 * It's a pitty that from this moment on we have to use this
2327 * local heap, because applications may rely on the handle
2330 * In this function we'll try to switch to local heap.
2332 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2337 if (!(es->style & ES_MULTILINE))
2343 if (!LOCAL_HeapSize(wnd->hInstance)) {
2344 if (!LocalInit16(wnd->hInstance, 0,
2345 GlobalSize16(wnd->hInstance))) {
2346 ERR("could not initialize local heap\n");
2349 TRACE("local heap initialized\n");
2352 countA = WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, NULL, 0, NULL, NULL);
2354 TRACE("Allocating 16-bit ANSI alias buffer\n");
2355 if (!(es->hloc16 = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, countA))) {
2356 ERR("could not allocate new 16 bit buffer\n");
2360 if (!(textA = (LPSTR)LOCAL_Lock(wnd->hInstance, es->hloc16))) {
2361 ERR("could not lock new 16 bit buffer\n");
2362 LOCAL_Free(wnd->hInstance, es->hloc16);
2367 WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, countA, NULL, NULL);
2368 LOCAL_Unlock(wnd->hInstance, es->hloc16);
2370 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(wnd->hInstance, es->hloc16));
2375 /*********************************************************************
2380 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR lpch)
2386 if (es->style & ES_MULTILINE) {
2387 if (line >= es->line_count)
2391 i = EDIT_EM_LineIndex(es, line);
2393 len = min(*(WORD *)lpch, EDIT_EM_LineLength(es, i));
2394 for (i = 0 ; i < len ; i++) {
2399 return (LRESULT)len;
2403 /*********************************************************************
2408 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end)
2410 UINT s = es->selection_start;
2411 UINT e = es->selection_end;
2418 return MAKELONG(s, e);
2422 /*********************************************************************
2426 * FIXME: is this right ? (or should it be only VSCROLL)
2427 * (and maybe only for edit controls that really have their
2428 * own scrollbars) (and maybe only for multiline controls ?)
2429 * All in all: very poorly documented
2431 * FIXME: now it's also broken, because of the new WM_HSCROLL /
2432 * WM_VSCROLL handlers
2435 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2437 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0),
2438 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0));
2442 /*********************************************************************
2447 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2452 if (!(es->style & ES_MULTILINE))
2454 if (index > (INT)strlenW(es->text))
2455 return es->line_count - 1;
2457 index = min(es->selection_start, es->selection_end);
2460 line_def = es->first_line_def;
2461 index -= line_def->length;
2462 while ((index >= 0) && line_def->next) {
2464 line_def = line_def->next;
2465 index -= line_def->length;
2471 /*********************************************************************
2476 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2481 if (!(es->style & ES_MULTILINE))
2483 if (line >= es->line_count)
2487 line_def = es->first_line_def;
2489 INT index = es->selection_end - line_def->length;
2490 while ((index >= 0) && line_def->next) {
2491 line_index += line_def->length;
2492 line_def = line_def->next;
2493 index -= line_def->length;
2497 line_index += line_def->length;
2498 line_def = line_def->next;
2506 /*********************************************************************
2511 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2515 if (!(es->style & ES_MULTILINE))
2516 return strlenW(es->text);
2519 /* get the number of remaining non-selected chars of selected lines */
2522 li = EDIT_EM_LineFromChar(es, es->selection_start);
2523 /* # chars before start of selection area */
2524 count = es->selection_start - EDIT_EM_LineIndex(es, li);
2525 li = EDIT_EM_LineFromChar(es, es->selection_end);
2526 /* # chars after end of selection */
2527 count += EDIT_EM_LineIndex(es, li) +
2528 EDIT_EM_LineLength(es, li) - es->selection_end;
2531 line_def = es->first_line_def;
2532 index -= line_def->length;
2533 while ((index >= 0) && line_def->next) {
2534 line_def = line_def->next;
2535 index -= line_def->length;
2537 return line_def->net_length;
2541 /*********************************************************************
2545 * FIXME: dx is in average character widths
2546 * However, we assume it is in pixels when we use this
2547 * function internally
2550 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2554 if (!(es->style & ES_MULTILINE))
2557 if (-dx > es->x_offset)
2559 if (dx > es->text_width - es->x_offset)
2560 dx = es->text_width - es->x_offset;
2561 nyoff = max(0, es->y_offset + dy);
2562 if (nyoff >= es->line_count)
2563 nyoff = es->line_count - 1;
2564 dy = (es->y_offset - nyoff) * es->line_height;
2568 GetClientRect(wnd->hwndSelf, &rc1);
2569 IntersectRect(&rc, &rc1, &es->format_rect);
2570 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2571 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2572 es->y_offset = nyoff;
2575 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2576 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2577 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2578 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2583 /*********************************************************************
2588 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap)
2590 INT len = strlenW(es->text);
2599 index = min(index, len);
2600 dc = GetDC(wnd->hwndSelf);
2602 old_font = SelectObject(dc, es->font);
2603 if (es->style & ES_MULTILINE) {
2604 l = EDIT_EM_LineFromChar(es, index);
2605 y = (l - es->y_offset) * es->line_height;
2606 li = EDIT_EM_LineIndex(es, l);
2607 if (after_wrap && (li == index) && l) {
2609 LINEDEF *line_def = es->first_line_def;
2611 line_def = line_def->next;
2614 if (line_def->ending == END_WRAP) {
2616 y -= es->line_height;
2617 li = EDIT_EM_LineIndex(es, l);
2620 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2621 es->tabs_count, es->tabs)) - es->x_offset;
2623 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2624 if (index < es->x_offset) {
2625 GetTextExtentPoint32W(dc, text + index,
2626 es->x_offset - index, &size);
2629 GetTextExtentPoint32W(dc, text + es->x_offset,
2630 index - es->x_offset, &size);
2634 if (es->style & ES_PASSWORD)
2635 HeapFree(GetProcessHeap(), 0, text);
2637 x += es->format_rect.left;
2638 y += es->format_rect.top;
2640 SelectObject(dc, old_font);
2641 ReleaseDC(wnd->hwndSelf, dc);
2642 return MAKELONG((INT16)x, (INT16)y);
2646 /*********************************************************************
2650 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2653 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
2655 UINT strl = strlenW(lpsz_replace);
2656 UINT tl = strlenW(es->text);
2663 TRACE("%s, can_undo %d, send_update %d\n",
2664 debugstr_w(lpsz_replace), can_undo, send_update);
2666 s = es->selection_start;
2667 e = es->selection_end;
2669 if ((s == e) && !strl)
2674 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2678 /* there is something to be deleted */
2680 utl = strlenW(es->undo_text);
2681 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2682 /* undo-buffer is extended to the right */
2683 EDIT_MakeUndoFit(es, utl + e - s);
2684 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2685 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2686 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2687 /* undo-buffer is extended to the left */
2688 EDIT_MakeUndoFit(es, utl + e - s);
2689 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2691 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2692 p[i] = (es->text + s)[i];
2693 es->undo_position = s;
2695 /* new undo-buffer */
2696 EDIT_MakeUndoFit(es, e - s);
2697 strncpyW(es->undo_text, es->text + s, e - s + 1);
2698 es->undo_text[e - s] = 0; /* ensure 0 termination */
2699 es->undo_position = s;
2701 /* any deletion makes the old insertion-undo invalid */
2702 es->undo_insert_count = 0;
2704 EDIT_EM_EmptyUndoBuffer(es);
2707 strcpyW(es->text + s, es->text + e);
2710 /* there is an insertion */
2712 if ((s == es->undo_position) ||
2713 ((es->undo_insert_count) &&
2714 (s == es->undo_position + es->undo_insert_count)))
2716 * insertion is new and at delete position or
2717 * an extension to either left or right
2719 es->undo_insert_count += strl;
2721 /* new insertion undo */
2722 es->undo_position = s;
2723 es->undo_insert_count = strl;
2724 /* new insertion makes old delete-buffer invalid */
2725 *es->undo_text = '\0';
2728 EDIT_EM_EmptyUndoBuffer(es);
2731 tl = strlenW(es->text);
2732 for (p = es->text + tl ; p >= es->text + s ; p--)
2734 for (i = 0 , p = es->text + s ; i < strl ; i++)
2735 p[i] = lpsz_replace[i];
2736 if(es->style & ES_UPPERCASE)
2737 CharUpperBuffW(p, strl);
2738 else if(es->style & ES_LOWERCASE)
2739 CharLowerBuffW(p, strl);
2742 /* FIXME: really inefficient */
2743 if (es->style & ES_MULTILINE)
2744 EDIT_BuildLineDefs_ML(wnd, es);
2746 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2747 es->flags |= EF_MODIFIED;
2748 if (send_update) es->flags |= EF_UPDATE;
2749 EDIT_EM_ScrollCaret(wnd, es);
2751 /* FIXME: really inefficient */
2752 EDIT_UpdateText(wnd, NULL, TRUE);
2756 /*********************************************************************
2761 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
2765 if (!(es->style & ES_MULTILINE))
2766 return (LRESULT)FALSE;
2776 if (es->y_offset < es->line_count - 1)
2781 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2784 if (es->y_offset < es->line_count - 1)
2785 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2788 return (LRESULT)FALSE;
2791 EDIT_EM_LineScroll(wnd, es, 0, dy);
2792 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2794 return MAKELONG((INT16)dy, (BOOL16)TRUE);
2798 /*********************************************************************
2803 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
2805 if (es->style & ES_MULTILINE) {
2810 INT cw = es->char_width;
2815 l = EDIT_EM_LineFromChar(es, es->selection_end);
2816 li = EDIT_EM_LineIndex(es, l);
2817 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
2818 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2819 if (l >= es->y_offset + vlc)
2820 dy = l - vlc + 1 - es->y_offset;
2821 if (l < es->y_offset)
2822 dy = l - es->y_offset;
2823 ww = es->format_rect.right - es->format_rect.left;
2824 if (x < es->format_rect.left)
2825 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
2826 if (x > es->format_rect.right)
2827 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
2829 EDIT_EM_LineScroll(wnd, es, dx, dy);
2835 if (!(es->style & ES_AUTOHSCROLL))
2838 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2839 format_width = es->format_rect.right - es->format_rect.left;
2840 if (x < es->format_rect.left) {
2841 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
2844 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2845 } while ((x < goal) && es->x_offset);
2846 /* FIXME: use ScrollWindow() somehow to improve performance */
2847 EDIT_UpdateText(wnd, NULL, TRUE);
2848 } else if (x > es->format_rect.right) {
2850 INT len = strlenW(es->text);
2851 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
2854 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2855 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
2856 } while ((x > goal) && (x_last > es->format_rect.right));
2857 /* FIXME: use ScrollWindow() somehow to improve performance */
2858 EDIT_UpdateText(wnd, NULL, TRUE);
2864 /*********************************************************************
2868 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2871 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc)
2873 if (!(es->style & ES_MULTILINE))
2877 WARN("called with NULL handle\n");
2881 EDIT_UnlockBuffer(wnd, es, TRUE);
2885 LOCAL_Free(wnd->hInstance, es->hloc16);
2886 es->hloc16 = (HLOCAL16)NULL;
2893 LocalFree(es->hloc32A);
2894 es->hloc32A = (HLOCAL)NULL;
2905 countA = LocalSize(hloc);
2906 textA = LocalLock(hloc);
2907 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2908 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2910 ERR("Could not allocate new unicode buffer\n");
2913 textW = LocalLock(hloc32W_new);
2914 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2915 LocalUnlock(hloc32W_new);
2919 LocalFree(es->hloc32W);
2921 es->hloc32W = hloc32W_new;
2925 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2927 EDIT_LockBuffer(wnd, es);
2929 es->x_offset = es->y_offset = 0;
2930 es->selection_start = es->selection_end = 0;
2931 EDIT_EM_EmptyUndoBuffer(es);
2932 es->flags &= ~EF_MODIFIED;
2933 es->flags &= ~EF_UPDATE;
2934 EDIT_BuildLineDefs_ML(wnd, es);
2935 EDIT_UpdateText(wnd, NULL, TRUE);
2936 EDIT_EM_ScrollCaret(wnd, es);
2940 /*********************************************************************
2944 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2947 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
2954 if (!(es->style & ES_MULTILINE))
2958 WARN("called with NULL handle\n");
2962 EDIT_UnlockBuffer(wnd, es, TRUE);
2966 LocalFree(es->hloc32A);
2967 es->hloc32A = (HLOCAL)NULL;
2970 countA = LOCAL_Size(wnd->hInstance, hloc);
2971 textA = LOCAL_Lock(wnd->hInstance, hloc);
2972 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2973 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2975 ERR("Could not allocate new unicode buffer\n");
2978 textW = LocalLock(hloc32W_new);
2979 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2980 LocalUnlock(hloc32W_new);
2981 LOCAL_Unlock(wnd->hInstance, hloc);
2984 LocalFree(es->hloc32W);
2986 es->hloc32W = hloc32W_new;
2989 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2991 EDIT_LockBuffer(wnd, es);
2993 es->x_offset = es->y_offset = 0;
2994 es->selection_start = es->selection_end = 0;
2995 EDIT_EM_EmptyUndoBuffer(es);
2996 es->flags &= ~EF_MODIFIED;
2997 es->flags &= ~EF_UPDATE;
2998 EDIT_BuildLineDefs_ML(wnd, es);
2999 EDIT_UpdateText(wnd, NULL, TRUE);
3000 EDIT_EM_ScrollCaret(wnd, es);
3004 /*********************************************************************
3008 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3009 * However, the windows version is not complied to yet in all of edit.c
3012 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3014 if (es->style & ES_MULTILINE) {
3016 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3018 es->buffer_limit = BUFLIMIT_MULTI;
3021 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3023 es->buffer_limit = BUFLIMIT_SINGLE;
3028 /*********************************************************************
3032 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3033 * action wParam despite what the docs say. EC_USEFONTINFO means one third
3034 * of the char's width, according to the new docs.
3037 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3038 INT left, INT right)
3040 if (action & EC_LEFTMARGIN) {
3041 if (left != EC_USEFONTINFO)
3042 es->left_margin = left;
3044 es->left_margin = es->char_width / 3;
3047 if (action & EC_RIGHTMARGIN) {
3048 if (right != EC_USEFONTINFO)
3049 es->right_margin = right;
3051 es->right_margin = es->char_width / 3;
3053 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3057 /*********************************************************************
3059 * EM_SETPASSWORDCHAR
3062 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, WCHAR c)
3064 if (es->style & ES_MULTILINE)
3067 if (es->password_char == c)
3070 es->password_char = c;
3072 wnd->dwStyle |= ES_PASSWORD;
3073 es->style |= ES_PASSWORD;
3075 wnd->dwStyle &= ~ES_PASSWORD;
3076 es->style &= ~ES_PASSWORD;
3078 EDIT_UpdateText(wnd, NULL, TRUE);
3082 /*********************************************************************
3086 * note: unlike the specs say: the order of start and end
3087 * _is_ preserved in Windows. (i.e. start can be > end)
3088 * In other words: this handler is OK
3091 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3093 UINT old_start = es->selection_start;
3094 UINT old_end = es->selection_end;
3095 UINT len = strlenW(es->text);
3097 if (start == (UINT)-1) {
3098 start = es->selection_end;
3099 end = es->selection_end;
3101 start = min(start, len);
3102 end = min(end, len);
3104 es->selection_start = start;
3105 es->selection_end = end;
3107 es->flags |= EF_AFTER_WRAP;
3109 es->flags &= ~EF_AFTER_WRAP;
3110 if (es->flags & EF_FOCUSED)
3111 EDIT_SetCaretPos(wnd, es, end, after_wrap);
3112 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3113 ORDER_UINT(start, end);
3114 ORDER_UINT(end, old_end);
3115 ORDER_UINT(start, old_start);
3116 ORDER_UINT(old_start, old_end);
3117 if (end != old_start)
3121 * ORDER_UINT32(end, old_start);
3122 * EDIT_InvalidateText(wnd, es, start, end);
3123 * EDIT_InvalidateText(wnd, es, old_start, old_end);
3124 * in place of the following if statement.
3126 if (old_start > end )
3128 EDIT_InvalidateText(wnd, es, start, end);
3129 EDIT_InvalidateText(wnd, es, old_start, old_end);
3133 EDIT_InvalidateText(wnd, es, start, old_start);
3134 EDIT_InvalidateText(wnd, es, end, old_end);
3137 else EDIT_InvalidateText(wnd, es, start, old_end);
3141 /*********************************************************************
3146 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3148 if (!(es->style & ES_MULTILINE))
3151 HeapFree(GetProcessHeap(), 0, es->tabs);
3152 es->tabs_count = count;
3156 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3157 memcpy(es->tabs, tabs, count * sizeof(INT));
3163 /*********************************************************************
3168 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3170 if (!(es->style & ES_MULTILINE))
3173 HeapFree(GetProcessHeap(), 0, es->tabs);
3174 es->tabs_count = count;
3179 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3180 for (i = 0 ; i < count ; i++)
3181 es->tabs[i] = *tabs++;
3187 /*********************************************************************
3189 * EM_SETWORDBREAKPROC
3192 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, LPARAM lParam)
3194 if (es->word_break_proc == (void *)lParam)
3197 es->word_break_proc = (void *)lParam;
3198 es->word_break_proc16 = NULL;
3200 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3201 EDIT_BuildLineDefs_ML(wnd, es);
3202 EDIT_UpdateText(wnd, NULL, TRUE);
3207 /*********************************************************************
3209 * EM_SETWORDBREAKPROC16
3212 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3214 if (es->word_break_proc16 == wbp)
3217 es->word_break_proc = NULL;
3218 es->word_break_proc16 = wbp;
3219 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3220 EDIT_BuildLineDefs_ML(wnd, es);
3221 EDIT_UpdateText(wnd, NULL, TRUE);
3226 /*********************************************************************
3231 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
3233 INT ulength = strlenW(es->undo_text);
3234 LPWSTR utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3236 strcpyW(utext, es->undo_text);
3238 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3239 es->undo_insert_count, debugstr_w(utext));
3241 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3242 EDIT_EM_EmptyUndoBuffer(es);
3243 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext, TRUE);
3244 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3245 HeapFree(GetProcessHeap(), 0, utext);
3247 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3248 es->undo_insert_count, debugstr_w(es->undo_text));
3250 if (es->flags & EF_UPDATE) {
3251 es->flags &= ~EF_UPDATE;
3252 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3259 /*********************************************************************
3264 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, WCHAR c)
3266 BOOL control = GetKeyState(VK_CONTROL) & 0x8000;
3269 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3270 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3273 if (es->style & ES_MULTILINE) {
3274 if (es->style & ES_READONLY) {
3275 EDIT_MoveHome(wnd, es, FALSE);
3276 EDIT_MoveDown_ML(wnd, es, FALSE);
3278 static const WCHAR cr_lfW[] = {'\r','\n',0};
3279 EDIT_EM_ReplaceSel(wnd, es, TRUE, cr_lfW, TRUE);
3280 if (es->flags & EF_UPDATE) {
3281 es->flags &= ~EF_UPDATE;
3282 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3288 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3290 static const WCHAR tabW[] = {'\t',0};
3291 EDIT_EM_ReplaceSel(wnd, es, TRUE, tabW, TRUE);
3292 if (es->flags & EF_UPDATE) {
3293 es->flags &= ~EF_UPDATE;
3294 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3299 if (!(es->style & ES_READONLY) && !control) {
3300 if (es->selection_start != es->selection_end)
3301 EDIT_WM_Clear(wnd, es);
3303 /* delete character left of caret */
3304 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3305 EDIT_MoveBackward(wnd, es, TRUE);
3306 EDIT_WM_Clear(wnd, es);
3311 SendMessageW(wnd->hwndSelf, WM_COPY, 0, 0);
3314 SendMessageW(wnd->hwndSelf, WM_PASTE, 0, 0);
3317 SendMessageW(wnd->hwndSelf, WM_CUT, 0, 0);
3321 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3325 EDIT_EM_ReplaceSel(wnd, es, TRUE, str, TRUE);
3326 if (es->flags & EF_UPDATE) {
3327 es->flags &= ~EF_UPDATE;
3328 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3336 /*********************************************************************
3341 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND control)
3343 if (code || control)
3348 EDIT_EM_Undo(wnd, es);
3351 EDIT_WM_Cut(wnd, es);
3354 EDIT_WM_Copy(wnd, es);
3357 EDIT_WM_Paste(wnd, es);
3360 EDIT_WM_Clear(wnd, es);
3363 EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
3364 EDIT_EM_ScrollCaret(wnd, es);
3367 ERR("unknown menu item, please report\n");
3373 /*********************************************************************
3377 * Note: the resource files resource/sysres_??.rc cannot define a
3378 * single popup menu. Hence we use a (dummy) menubar
3379 * containing the single popup menu as its first item.
3381 * FIXME: the message identifiers have been chosen arbitrarily,
3382 * hence we use MF_BYPOSITION.
3383 * We might as well use the "real" values (anybody knows ?)
3384 * The menu definition is in resources/sysres_??.rc.
3385 * Once these are OK, we better use MF_BYCOMMAND here
3386 * (as we do in EDIT_WM_Command()).
3389 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, INT x, INT y)
3391 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3392 HMENU popup = GetSubMenu(menu, 0);
3393 UINT start = es->selection_start;
3394 UINT end = es->selection_end;
3396 ORDER_UINT(start, end);
3399 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) ? MF_ENABLED : MF_GRAYED));
3401 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3403 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3405 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) ? MF_ENABLED : MF_GRAYED));
3407 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
3409 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3411 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
3416 /*********************************************************************
3421 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
3423 INT s = es->selection_start;
3424 INT e = es->selection_end;
3431 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3432 dst = GlobalLock(hdst);
3433 strncpyW(dst, es->text + s, e - s);
3434 dst[e - s] = 0; /* ensure 0 termination */
3435 TRACE("%s\n", debugstr_w(dst));
3437 OpenClipboard(wnd->hwndSelf);
3439 SetClipboardData(CF_UNICODETEXT, hdst);
3444 /*********************************************************************
3449 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCWSTR name)
3451 TRACE("%s\n", debugstr_w(name));
3453 * To initialize some final structure members, we call some helper
3454 * functions. However, since the EDITSTATE is not consistent (i.e.
3455 * not fully initialized), we should be very careful which
3456 * functions can be called, and in what order.
3458 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3459 EDIT_EM_EmptyUndoBuffer(es);
3461 if (name && *name) {
3462 EDIT_EM_ReplaceSel(wnd, es, FALSE, name, TRUE);
3463 /* if we insert text to the editline, the text scrolls out
3464 * of the window, as the caret is placed after the insert
3465 * pos normally; thus we reset es->selection... to 0 and
3468 es->selection_start = es->selection_end = 0;
3469 EDIT_EM_ScrollCaret(wnd, es);
3470 if (es->flags & EF_UPDATE) {
3471 es->flags &= ~EF_UPDATE;
3472 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3479 /*********************************************************************
3484 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3487 while (LocalUnlock(es->hloc32W)) ;
3488 LocalFree(es->hloc32W);
3491 while (LocalUnlock(es->hloc32A)) ;
3492 LocalFree(es->hloc32A);
3495 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3496 LOCAL_Free(wnd->hInstance, es->hloc16);
3498 HeapFree(GetProcessHeap(), 0, es);
3499 *(EDITSTATE **)wnd->wExtra = NULL;
3503 /*********************************************************************
3508 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3513 if ( get_app_version() >= 0x40000 &&(
3514 !es->bEnableState || (es->style & ES_READONLY)))
3515 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3517 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc);
3520 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3522 GetClientRect(wnd->hwndSelf, &rc);
3523 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3524 GetClipBox(dc, &rc);
3526 * FIXME: specs say that we should UnrealizeObject() the brush,
3527 * but the specs of UnrealizeObject() say that we shouldn't
3528 * unrealize a stock object. The default brush that
3529 * DefWndProc() returns is ... a stock object.
3531 FillRect(dc, &rc, brush);
3536 /*********************************************************************
3541 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3543 if(!count) return 0;
3547 LPWSTR textW = (LPWSTR)lParam;
3548 strncpyW(textW, es->text, count);
3549 textW[count - 1] = 0; /* ensure 0 termination */
3550 return strlenW(textW);
3554 LPSTR textA = (LPSTR)lParam;
3555 INT ret = WideCharToMultiByte(CP_ACP, 0, es->text, es->buffer_size, textA, count, NULL, NULL);
3556 textA[count - 1] = 0; /* ensure 0 termination */
3562 /*********************************************************************
3566 * 16 bit notepad needs this. Actually it is not _our_ hack,
3567 * it is notepad's. Notepad is sending us scrollbar messages with
3568 * undocumented parameters without us even having a scrollbar ... !?!?
3571 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos)
3574 INT fw = es->format_rect.right - es->format_rect.left;
3577 if (!(es->flags & EF_HSCROLL_HACK)) {
3578 ERR("hacked WM_HSCROLL handler invoked\n");
3579 ERR(" if you are _not_ running 16 bit notepad, please report\n");
3580 ERR(" (this message is only displayed once per edit control)\n");
3581 es->flags |= EF_HSCROLL_HACK;
3587 dx = -es->char_width;
3590 if (es->x_offset < es->text_width)
3591 dx = es->char_width;
3595 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3598 if (es->x_offset < es->text_width)
3599 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3606 if (es->x_offset < es->text_width)
3607 dx = es->text_width - es->x_offset;
3610 es->flags |= EF_HSCROLL_TRACK;
3611 dx = pos * es->text_width / 100 - es->x_offset;
3613 case SB_THUMBPOSITION:
3614 es->flags &= ~EF_HSCROLL_TRACK;
3615 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3616 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3622 * FIXME : the next two are undocumented !
3623 * Are we doing the right thing ?
3624 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3625 * although it's also a regular control message.
3628 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3630 case EM_LINESCROLL16:
3635 ERR("undocumented (hacked) WM_HSCROLL parameter, please report\n");
3639 EDIT_EM_LineScroll(wnd, es, dx, 0);
3644 /*********************************************************************
3649 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
3654 if (!(es->style & ES_MULTILINE))
3657 if (!(es->style & ES_AUTOHSCROLL))
3660 if (!(es->style & WS_HSCROLL))
3661 return EDIT_HScroll_Hack(wnd, es, action, pos);
3664 fw = es->format_rect.right - es->format_rect.left;
3668 dx = -es->char_width;
3671 if (es->x_offset < es->text_width)
3672 dx = es->char_width;
3676 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3679 if (es->x_offset < es->text_width)
3680 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3687 if (es->x_offset < es->text_width)
3688 dx = es->text_width - es->x_offset;
3691 es->flags |= EF_HSCROLL_TRACK;
3692 dx = pos - es->x_offset;
3694 case SB_THUMBPOSITION:
3695 es->flags &= ~EF_HSCROLL_TRACK;
3696 if (!(dx = pos - es->x_offset)) {
3697 SetScrollPos(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3698 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3705 ERR("undocumented WM_HSCROLL parameter, please report\n");
3709 EDIT_EM_LineScroll(wnd, es, dx, 0);
3714 /*********************************************************************
3719 static BOOL EDIT_CheckCombo(WND *wnd, EDITSTATE *es, UINT msg, INT key)
3721 HWND hLBox = es->hwndListBox;
3729 hCombo = wnd->parent->hwndSelf;
3733 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
3734 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3736 if (key == VK_UP || key == VK_DOWN)
3738 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3741 if (msg == WM_KEYDOWN || nEUI)
3742 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3748 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3750 /* make sure ComboLBox pops up */
3751 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3756 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3759 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3761 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3763 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
3768 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3774 /*********************************************************************
3778 * Handling of special keys that don't produce a WM_CHAR
3779 * (i.e. non-printable keys) & Backspace & Delete
3782 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key)
3787 if (GetKeyState(VK_MENU) & 0x8000)
3790 shift = GetKeyState(VK_SHIFT) & 0x8000;
3791 control = GetKeyState(VK_CONTROL) & 0x8000;
3796 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key) || key == VK_F4)
3801 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3802 EDIT_MoveUp_ML(wnd, es, shift);
3805 EDIT_MoveWordBackward(wnd, es, shift);
3807 EDIT_MoveBackward(wnd, es, shift);
3810 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key))
3814 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3815 EDIT_MoveDown_ML(wnd, es, shift);
3817 EDIT_MoveWordForward(wnd, es, shift);
3819 EDIT_MoveForward(wnd, es, shift);
3822 EDIT_MoveHome(wnd, es, shift);
3825 EDIT_MoveEnd(wnd, es, shift);
3828 if (es->style & ES_MULTILINE)
3829 EDIT_MovePageUp_ML(wnd, es, shift);
3831 EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
3834 if (es->style & ES_MULTILINE)
3835 EDIT_MovePageDown_ML(wnd, es, shift);
3837 EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
3840 if (!(es->style & ES_READONLY) && !(shift && control)) {
3841 if (es->selection_start != es->selection_end) {
3843 EDIT_WM_Cut(wnd, es);
3845 EDIT_WM_Clear(wnd, es);
3848 /* delete character left of caret */
3849 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3850 EDIT_MoveBackward(wnd, es, TRUE);
3851 EDIT_WM_Clear(wnd, es);
3852 } else if (control) {
3853 /* delete to end of line */
3854 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3855 EDIT_MoveEnd(wnd, es, TRUE);
3856 EDIT_WM_Clear(wnd, es);
3858 /* delete character right of caret */
3859 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3860 EDIT_MoveForward(wnd, es, TRUE);
3861 EDIT_WM_Clear(wnd, es);
3868 if (!(es->style & ES_READONLY))
3869 EDIT_WM_Paste(wnd, es);
3871 EDIT_WM_Copy(wnd, es);
3874 /* If the edit doesn't want the return send a message to the default object */
3875 if(!(es->style & ES_WANTRETURN))
3877 HWND hwndParent = GetParent(wnd->hwndSelf);
3878 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
3879 if (HIWORD(dw) == DC_HASDEFID)
3881 SendMessageW( hwndParent, WM_COMMAND,
3882 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
3883 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
3892 /*********************************************************************
3897 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es)
3899 es->flags &= ~EF_FOCUSED;
3901 if(!(es->style & ES_NOHIDESEL))
3902 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3903 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
3908 /*********************************************************************
3912 * The caret position has been set on the WM_LBUTTONDOWN message
3915 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es)
3918 INT e = es->selection_end;
3923 if (!(es->flags & EF_FOCUSED))
3926 l = EDIT_EM_LineFromChar(es, e);
3927 li = EDIT_EM_LineIndex(es, l);
3928 ll = EDIT_EM_LineLength(es, e);
3929 s = li + EDIT_CallWordBreakProc (wnd, es, li, e - li, ll, WB_LEFT);
3930 e = li + EDIT_CallWordBreakProc(wnd, es, li, e - li, ll, WB_RIGHT);
3931 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
3932 EDIT_EM_ScrollCaret(wnd, es);
3937 /*********************************************************************
3942 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3947 if (!(es->flags & EF_FOCUSED))
3950 es->bCaptureState = TRUE;
3951 SetCapture(wnd->hwndSelf);
3952 EDIT_ConfinePoint(es, &x, &y);
3953 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3954 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3955 EDIT_EM_ScrollCaret(wnd, es);
3956 es->region_posx = es->region_posy = 0;
3957 SetTimer(wnd->hwndSelf, 0, 100, NULL);
3962 /*********************************************************************
3967 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es)
3969 if (es->bCaptureState && GetCapture() == hwndSelf) {
3970 KillTimer(hwndSelf, 0);
3973 es->bCaptureState = FALSE;
3978 /*********************************************************************
3983 static LRESULT EDIT_WM_MButtonDown(WND *wnd)
3985 SendMessageW(wnd->hwndSelf,WM_PASTE,0,0);
3990 /*********************************************************************
3995 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, INT x, INT y)
4001 if (GetCapture() != wnd->hwndSelf)
4005 * FIXME: gotta do some scrolling if outside client
4006 * area. Maybe reset the timer ?
4009 EDIT_ConfinePoint(es, &x, &y);
4010 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4011 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4012 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
4013 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
4018 /*********************************************************************
4023 static LRESULT EDIT_WM_NCCreate(WND *wnd, DWORD style, HWND hwndParent, BOOL unicode)
4028 TRACE("Creating %s edit control\n", unicode ? "Unicode" : "ANSI");
4030 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4032 *(EDITSTATE **)wnd->wExtra = es;
4035 * Note: since the EDITSTATE has not been fully initialized yet,
4036 * we can't use any API calls that may send
4037 * WM_XXX messages before WM_NCCREATE is completed.
4040 es->is_unicode = unicode;
4043 es->bEnableState = !(style & WS_DISABLED);
4046 * In Win95 look and feel, the WS_BORDER style is replaced by the
4047 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4048 * control a non client area.
4050 if (TWEAK_WineLook != WIN31_LOOK)
4052 if (es->style & WS_BORDER)
4054 es->style &= ~WS_BORDER;
4055 wnd->dwStyle &= ~WS_BORDER;
4056 wnd->dwExStyle |= WS_EX_CLIENTEDGE;
4061 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4062 wnd->dwStyle &= ~WS_BORDER;
4065 if (es->style & ES_COMBO)
4066 es->hwndListBox = GetDlgItem(hwndParent, ID_CB_LISTBOX);
4068 if (es->style & ES_MULTILINE) {
4069 es->buffer_limit = BUFLIMIT_MULTI;
4070 if (es->style & WS_VSCROLL)
4071 es->style |= ES_AUTOVSCROLL;
4072 if (es->style & WS_HSCROLL)
4073 es->style |= ES_AUTOHSCROLL;
4074 es->style &= ~ES_PASSWORD;
4075 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4076 if (es->style & ES_RIGHT)
4077 es->style &= ~ES_CENTER;
4078 es->style &= ~WS_HSCROLL;
4079 es->style &= ~ES_AUTOHSCROLL;
4082 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4083 es->style |= ES_AUTOVSCROLL;
4085 es->buffer_limit = BUFLIMIT_SINGLE;
4086 es->style &= ~ES_CENTER;
4087 es->style &= ~ES_RIGHT;
4088 es->style &= ~WS_HSCROLL;
4089 es->style &= ~WS_VSCROLL;
4090 es->style &= ~ES_AUTOVSCROLL;
4091 es->style &= ~ES_WANTRETURN;
4092 if (es->style & ES_UPPERCASE) {
4093 es->style &= ~ES_LOWERCASE;
4094 es->style &= ~ES_NUMBER;
4095 } else if (es->style & ES_LOWERCASE)
4096 es->style &= ~ES_NUMBER;
4097 if (es->style & ES_PASSWORD)
4098 es->password_char = '*';
4100 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4101 es->style |= ES_AUTOHSCROLL;
4104 alloc_size = ((es->buffer_size + 1) * sizeof(WCHAR) + GROWLENGTH - 1) & ~(GROWLENGTH - 1);
4105 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4107 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4109 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4111 es->undo_buffer_size = es->buffer_size;
4113 if (es->style & ES_MULTILINE)
4114 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4121 /*********************************************************************
4126 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam)
4135 BOOL rev = es->bEnableState &&
4136 ((es->flags & EF_FOCUSED) ||
4137 (es->style & ES_NOHIDESEL));
4139 dc = BeginPaint(wnd->hwndSelf, &ps);
4142 if(es->style & WS_BORDER) {
4143 GetClientRect(wnd->hwndSelf, &rc);
4144 if(es->style & ES_MULTILINE) {
4145 if(es->style & WS_HSCROLL) rc.bottom++;
4146 if(es->style & WS_VSCROLL) rc.right++;
4148 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4150 IntersectClipRect(dc, es->format_rect.left,
4151 es->format_rect.top,
4152 es->format_rect.right,
4153 es->format_rect.bottom);
4154 if (es->style & ES_MULTILINE) {
4155 GetClientRect(wnd->hwndSelf, &rc);
4156 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4159 old_font = SelectObject(dc, es->font);
4160 if ( get_app_version() >= 0x40000 &&(
4161 !es->bEnableState || (es->style & ES_READONLY)))
4162 EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
4164 EDIT_SEND_CTLCOLOR(wnd, dc);
4166 if (!es->bEnableState)
4167 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4168 GetClipBox(dc, &rcRgn);
4169 if (es->style & ES_MULTILINE) {
4170 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4171 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4172 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
4173 if (IntersectRect(&rc, &rcRgn, &rcLine))
4174 EDIT_PaintLine(wnd, es, dc, i, rev);
4177 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
4178 if (IntersectRect(&rc, &rcRgn, &rcLine))
4179 EDIT_PaintLine(wnd, es, dc, 0, rev);
4182 SelectObject(dc, old_font);
4183 if (es->flags & EF_FOCUSED)
4184 EDIT_SetCaretPos(wnd, es, es->selection_end,
4185 es->flags & EF_AFTER_WRAP);
4187 EndPaint(wnd->hwndSelf, &ps);
4188 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK)) {
4189 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4191 si.cbSize = sizeof(SCROLLINFO);
4192 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
4194 si.nMax = es->line_count + vlc - 2;
4196 si.nPos = es->y_offset;
4197 SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
4199 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK)) {
4201 INT fw = es->format_rect.right - es->format_rect.left;
4202 si.cbSize = sizeof(SCROLLINFO);
4203 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
4205 si.nMax = es->text_width + fw - 1;
4207 si.nPos = es->x_offset;
4208 SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
4213 /*********************************************************************
4218 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
4223 OpenClipboard(wnd->hwndSelf);
4224 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4225 src = (LPWSTR)GlobalLock(hsrc);
4226 EDIT_EM_ReplaceSel(wnd, es, TRUE, src, TRUE);
4229 if (es->flags & EF_UPDATE) {
4230 es->flags &= ~EF_UPDATE;
4231 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
4238 /*********************************************************************
4243 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es)
4245 es->flags |= EF_FOCUSED;
4246 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4247 EDIT_SetCaretPos(wnd, es, es->selection_end,
4248 es->flags & EF_AFTER_WRAP);
4249 if(!(es->style & ES_NOHIDESEL))
4250 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
4251 ShowCaret(wnd->hwndSelf);
4252 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
4256 /*********************************************************************
4260 * With Win95 look the margins are set to default font value unless
4261 * the system font (font == 0) is being set, in which case they are left
4265 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
4273 dc = GetDC(wnd->hwndSelf);
4275 old_font = SelectObject(dc, font);
4276 GetTextMetricsW(dc, &tm);
4277 es->line_height = tm.tmHeight;
4278 es->char_width = tm.tmAveCharWidth;
4280 SelectObject(dc, old_font);
4281 ReleaseDC(wnd->hwndSelf, dc);
4282 if (font && (TWEAK_WineLook > WIN31_LOOK))
4283 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4284 EC_USEFONTINFO, EC_USEFONTINFO);
4286 /* Force the recalculation of the format rect for each font change */
4287 GetClientRect(wnd->hwndSelf, &r);
4288 EDIT_SetRectNP(wnd, es, &r);
4290 if (es->style & ES_MULTILINE)
4291 EDIT_BuildLineDefs_ML(wnd, es);
4294 EDIT_UpdateText(wnd, NULL, TRUE);
4295 if (es->flags & EF_FOCUSED) {
4297 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4298 EDIT_SetCaretPos(wnd, es, es->selection_end,
4299 es->flags & EF_AFTER_WRAP);
4300 ShowCaret(wnd->hwndSelf);
4305 /*********************************************************************
4310 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4311 * The modified flag is reset. No notifications are sent.
4313 * For single-line controls, reception of WM_SETTEXT triggers:
4314 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4317 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPARAM lParam, BOOL unicode)
4322 text = (LPWSTR)lParam;
4325 LPCSTR textA = (LPCSTR)lParam;
4326 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4327 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4328 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4331 EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
4333 TRACE("%s\n", debugstr_w(text));
4334 EDIT_EM_ReplaceSel(wnd, es, FALSE, text, !(es->style & ES_MULTILINE));
4336 HeapFree(GetProcessHeap(), 0, text);
4338 static const WCHAR empty_stringW[] = {0};
4340 EDIT_EM_ReplaceSel(wnd, es, FALSE, empty_stringW, !(es->style & ES_MULTILINE));
4343 es->flags &= ~EF_MODIFIED;
4344 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
4345 EDIT_EM_ScrollCaret(wnd, es);
4347 if (es->flags & EF_UPDATE) {
4348 es->flags &= ~EF_UPDATE;
4349 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
4354 /*********************************************************************
4359 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
4361 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4363 TRACE("width = %d, height = %d\n", width, height);
4364 SetRect(&rc, 0, 0, width, height);
4365 EDIT_SetRectNP(wnd, es, &rc);
4366 EDIT_UpdateText(wnd, NULL, TRUE);
4371 /*********************************************************************
4376 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
4378 if ((key == VK_BACK) && (key_data & 0x2000)) {
4379 if (EDIT_EM_CanUndo(es))
4380 EDIT_EM_Undo(wnd, es);
4382 } else if (key == VK_UP || key == VK_DOWN) {
4383 if (EDIT_CheckCombo(wnd, es, WM_SYSKEYDOWN, key))
4386 return DefWindowProcW(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4390 /*********************************************************************
4395 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es)
4397 if (es->region_posx < 0) {
4398 EDIT_MoveBackward(wnd, es, TRUE);
4399 } else if (es->region_posx > 0) {
4400 EDIT_MoveForward(wnd, es, TRUE);
4403 * FIXME: gotta do some vertical scrolling here, like
4404 * EDIT_EM_LineScroll(wnd, 0, 1);
4409 /*********************************************************************
4413 * 16 bit notepad needs this. Actually it is not _our_ hack,
4414 * it is notepad's. Notepad is sending us scrollbar messages with
4415 * undocumented parameters without us even having a scrollbar ... !?!?
4418 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos)
4423 if (!(es->flags & EF_VSCROLL_HACK)) {
4424 ERR("hacked WM_VSCROLL handler invoked\n");
4425 ERR(" if you are _not_ running 16 bit notepad, please report\n");
4426 ERR(" (this message is only displayed once per edit control)\n");
4427 es->flags |= EF_VSCROLL_HACK;
4435 EDIT_EM_Scroll(wnd, es, action);
4441 dy = es->line_count - 1 - es->y_offset;
4444 es->flags |= EF_VSCROLL_TRACK;
4445 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
4447 case SB_THUMBPOSITION:
4448 es->flags &= ~EF_VSCROLL_TRACK;
4449 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
4450 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4456 * FIXME : the next two are undocumented !
4457 * Are we doing the right thing ?
4458 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4459 * although it's also a regular control message.
4462 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
4464 case EM_LINESCROLL16:
4469 ERR("undocumented (hacked) WM_VSCROLL parameter, please report\n");
4473 EDIT_EM_LineScroll(wnd, es, 0, dy);
4478 /*********************************************************************
4483 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
4487 if (!(es->style & ES_MULTILINE))
4490 if (!(es->style & ES_AUTOVSCROLL))
4493 if (!(es->style & WS_VSCROLL))
4494 return EDIT_VScroll_Hack(wnd, es, action, pos);
4502 EDIT_EM_Scroll(wnd, es, action);
4509 dy = es->line_count - 1 - es->y_offset;
4512 es->flags |= EF_VSCROLL_TRACK;
4513 dy = pos - es->y_offset;
4515 case SB_THUMBPOSITION:
4516 es->flags &= ~EF_VSCROLL_TRACK;
4517 if (!(dy = pos - es->y_offset)) {
4518 SetScrollPos(wnd->hwndSelf, SB_VERT, pos, TRUE);
4519 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4526 ERR("undocumented WM_VSCROLL action %d, please report\n",
4531 EDIT_EM_LineScroll(wnd, es, 0, dy);
4536 /*********************************************************************
4541 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase)
4543 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
4545 /* EF_UPDATE will be turned off in paint */
4546 if (es->flags & EF_UPDATE)
4547 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
4549 InvalidateRect(wnd->hwndSelf, rc, bErase);