4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * please read EDIT.TODO (and update it when you change things)
36 #include "wine/winbase16.h"
37 #include "wine/winuser16.h"
38 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(edit);
45 WINE_DECLARE_DEBUG_CHANNEL(combo);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
49 FIXME: BTW, new specs say 65535 (do you dare ???) */
50 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
51 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
52 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
53 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
56 * extra flags for EDITSTATE.flags field
58 #define EF_MODIFIED 0x0001 /* text has been modified */
59 #define EF_FOCUSED 0x0002 /* we have input focus */
60 #define EF_UPDATE 0x0004 /* notify parent of changed state */
61 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
62 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
63 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
64 wrapped line, instead of in front of the next character */
65 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
69 END_0 = 0, /* line ends with terminating '\0' character */
70 END_WRAP, /* line is wrapped */
71 END_HARD, /* line ends with a hard return '\r\n' */
72 END_SOFT, /* line ends with a soft return '\r\r\n' */
73 END_RICH /* line ends with a single '\n' */
76 typedef struct tagLINEDEF {
77 INT length; /* bruto length of a line in bytes */
78 INT net_length; /* netto length of a line in visible characters */
80 INT width; /* width of the line in pixels */
81 INT index; /* line index into the buffer */
82 struct tagLINEDEF *next;
87 BOOL is_unicode; /* how the control was created */
88 LPWSTR text; /* the actual contents of the control */
89 UINT buffer_size; /* the size of the buffer in characters */
90 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
91 HFONT font; /* NULL means standard system font */
92 INT x_offset; /* scroll offset for multi lines this is in pixels
93 for single lines it's in characters */
94 INT line_height; /* height of a screen line in pixels */
95 INT char_width; /* average character width in pixels */
96 DWORD style; /* sane version of wnd->dwStyle */
97 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
98 INT undo_insert_count; /* number of characters inserted in sequence */
99 UINT undo_position; /* character index of the insertion and deletion */
100 LPWSTR undo_text; /* deleted text */
101 UINT undo_buffer_size; /* size of the deleted text buffer */
102 INT selection_start; /* == selection_end if no selection */
103 INT selection_end; /* == current caret position */
104 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
105 INT left_margin; /* in pixels */
106 INT right_margin; /* in pixels */
108 INT text_width; /* width of the widest line in pixels for multi line controls
109 and just line width for single line controls */
110 INT region_posx; /* Position of cursor relative to region: */
111 INT region_posy; /* -1: to left, 0: within, 1: to right */
112 EDITWORDBREAKPROC16 word_break_proc16;
113 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
114 INT line_count; /* number of lines */
115 INT y_offset; /* scroll offset in number of lines */
116 BOOL bCaptureState; /* flag indicating whether mouse was captured */
117 BOOL bEnableState; /* flag keeping the enable state */
118 HWND hwndParent; /* Handle of parent for sending EN_* messages.
119 Even if parent will change, EN_* messages
120 should be sent to the first parent. */
121 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
123 * only for multi line controls
125 INT lock_count; /* amount of re-entries in the EditWndProc */
128 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
129 HLOCAL hloc32W; /* our unicode local memory block */
130 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
132 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
137 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
138 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
140 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
141 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
143 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
144 do {TRACE("notification " str " sent to hwnd=%08x\n", \
145 (UINT)(hwnd));} while(0)
147 /* used for disabled or read-only edit control */
148 #define EDIT_SEND_CTLCOLORSTATIC(hwnd,hdc) \
149 (SendMessageW(GetParent(hwnd), WM_CTLCOLORSTATIC, \
150 (WPARAM)(hdc), (LPARAM)(hwnd)))
151 #define EDIT_SEND_CTLCOLOR(hwnd,hdc) \
152 (SendMessageW(GetParent(hwnd), WM_CTLCOLOREDIT, \
153 (WPARAM)(hdc), (LPARAM)(hwnd)))
154 #define EDIT_NOTIFY_PARENT(hwnd, es, wNotifyCode, str) \
156 { /* Notify parent which has created this edit control */ \
157 DPRINTF_EDIT_NOTIFY((es)->hwndParent, str); \
158 SendMessageW((es)->hwndParent, WM_COMMAND, \
159 MAKEWPARAM(GetWindowLongA((hwnd),GWL_ID), wNotifyCode), \
162 #define DPRINTF_EDIT_MSG16(str) \
164 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
165 hwnd, (UINT)wParam, (UINT)lParam)
166 #define DPRINTF_EDIT_MSG32(str) \
168 "32 bit %c : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
169 unicode ? 'W' : 'A', \
170 hwnd, (UINT)wParam, (UINT)lParam)
172 /*********************************************************************
179 * These functions have trivial implementations
180 * We still like to call them internally
181 * "static inline" makes them more like macro's
183 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
184 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
185 static inline void EDIT_WM_Clear(HWND hwnd, EDITSTATE *es);
186 static inline void EDIT_WM_Cut(HWND hwnd, EDITSTATE *es);
189 * Helper functions only valid for one type of control
191 static void EDIT_BuildLineDefs_ML(HWND hwnd, EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
192 static void EDIT_CalcLineWidth_SL(HWND hwnd, EDITSTATE *es);
193 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
194 static void EDIT_MoveDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
195 static void EDIT_MovePageDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
196 static void EDIT_MovePageUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
197 static void EDIT_MoveUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
199 * Helper functions valid for both single line _and_ multi line controls
201 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
202 static INT EDIT_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
203 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
204 static void EDIT_GetLineRect(HWND hwnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
205 static void EDIT_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end);
206 static void EDIT_LockBuffer(HWND hwnd, EDITSTATE *es);
207 static BOOL EDIT_MakeFit(HWND hwnd, EDITSTATE *es, UINT size);
208 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
209 static void EDIT_MoveBackward(HWND hwnd, EDITSTATE *es, BOOL extend);
210 static void EDIT_MoveEnd(HWND hwnd, EDITSTATE *es, BOOL extend);
211 static void EDIT_MoveForward(HWND hwnd, EDITSTATE *es, BOOL extend);
212 static void EDIT_MoveHome(HWND hwnd, EDITSTATE *es, BOOL extend);
213 static void EDIT_MoveWordBackward(HWND hwnd, EDITSTATE *es, BOOL extend);
214 static void EDIT_MoveWordForward(HWND hwnd, EDITSTATE *es, BOOL extend);
215 static void EDIT_PaintLine(HWND hwnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
216 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
217 static void EDIT_SetCaretPos(HWND hwnd, EDITSTATE *es, INT pos, BOOL after_wrap);
218 static void EDIT_SetRectNP(HWND hwnd, EDITSTATE *es, LPRECT lprc);
219 static void EDIT_UnlockBuffer(HWND hwnd, EDITSTATE *es, BOOL force);
220 static void EDIT_UpdateScrollInfo(HWND hwnd, EDITSTATE *es);
221 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
223 * EM_XXX message handlers
225 static LRESULT EDIT_EM_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y);
226 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
227 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
228 static HLOCAL16 EDIT_EM_GetHandle16(HWND hwnd, EDITSTATE *es);
229 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
230 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
231 static LRESULT EDIT_EM_GetThumb(HWND hwnd, EDITSTATE *es);
232 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
233 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
234 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
235 static BOOL EDIT_EM_LineScroll(HWND hwnd, EDITSTATE *es, INT dx, INT dy);
236 static BOOL EDIT_EM_LineScroll_internal(HWND hwnd, EDITSTATE *es, INT dx, INT dy);
237 static LRESULT EDIT_EM_PosFromChar(HWND hwnd, EDITSTATE *es, INT index, BOOL after_wrap);
238 static void EDIT_EM_ReplaceSel(HWND hwnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update);
239 static LRESULT EDIT_EM_Scroll(HWND hwnd, EDITSTATE *es, INT action);
240 static void EDIT_EM_ScrollCaret(HWND hwnd, EDITSTATE *es);
241 static void EDIT_EM_SetHandle(HWND hwnd, EDITSTATE *es, HLOCAL hloc);
242 static void EDIT_EM_SetHandle16(HWND hwnd, EDITSTATE *es, HLOCAL16 hloc);
243 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
244 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
245 static void EDIT_EM_SetPasswordChar(HWND hwnd, EDITSTATE *es, WCHAR c);
246 static void EDIT_EM_SetSel(HWND hwnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
247 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
248 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
249 static void EDIT_EM_SetWordBreakProc(HWND hwnd, EDITSTATE *es, LPARAM lParam);
250 static void EDIT_EM_SetWordBreakProc16(HWND hwnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
251 static BOOL EDIT_EM_Undo(HWND hwnd, EDITSTATE *es);
253 * WM_XXX message handlers
255 static void EDIT_WM_Char(HWND hwnd, EDITSTATE *es, WCHAR c);
256 static void EDIT_WM_Command(HWND hwnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
257 static void EDIT_WM_ContextMenu(HWND hwnd, EDITSTATE *es, INT x, INT y);
258 static void EDIT_WM_Copy(HWND hwnd, EDITSTATE *es);
259 static LRESULT EDIT_WM_Create(HWND hwnd, EDITSTATE *es, LPCWSTR name);
260 static void EDIT_WM_Destroy(HWND hwnd, EDITSTATE *es);
261 static LRESULT EDIT_WM_EraseBkGnd(HWND hwnd, EDITSTATE *es, HDC dc);
262 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
263 static LRESULT EDIT_WM_HScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos);
264 static LRESULT EDIT_WM_KeyDown(HWND hwnd, EDITSTATE *es, INT key);
265 static LRESULT EDIT_WM_KillFocus(HWND hwnd, EDITSTATE *es);
266 static LRESULT EDIT_WM_LButtonDblClk(HWND hwnd, EDITSTATE *es);
267 static LRESULT EDIT_WM_LButtonDown(HWND hwnd, EDITSTATE *es, DWORD keys, INT x, INT y);
268 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es);
269 static LRESULT EDIT_WM_MButtonDown(HWND hwnd);
270 static LRESULT EDIT_WM_MouseMove(HWND hwnd, EDITSTATE *es, INT x, INT y);
271 static LRESULT EDIT_WM_NCCreate(HWND hwnd, DWORD style, HWND hwndParent, BOOL unicode);
272 static void EDIT_WM_Paint(HWND hwnd, EDITSTATE *es, WPARAM wParam);
273 static void EDIT_WM_Paste(HWND hwnd, EDITSTATE *es);
274 static void EDIT_WM_SetFocus(HWND hwnd, EDITSTATE *es);
275 static void EDIT_WM_SetFont(HWND hwnd, EDITSTATE *es, HFONT font, BOOL redraw);
276 static void EDIT_WM_SetText(HWND hwnd, EDITSTATE *es, LPARAM lParam, BOOL unicode);
277 static void EDIT_WM_Size(HWND hwnd, EDITSTATE *es, UINT action, INT width, INT height);
278 static LRESULT EDIT_WM_StyleChanged (HWND hwnd, EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
279 static LRESULT EDIT_WM_SysKeyDown(HWND hwnd, EDITSTATE *es, INT key, DWORD key_data);
280 static void EDIT_WM_Timer(HWND hwnd, EDITSTATE *es);
281 static LRESULT EDIT_WM_VScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos);
282 static void EDIT_UpdateText(HWND hwnd, EDITSTATE *es, LPRECT rc, BOOL bErase);
283 static void EDIT_UpdateTextRegion(HWND hwnd, EDITSTATE *es, HRGN hrgn, BOOL bErase);
285 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
286 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
288 /*********************************************************************
289 * edit class descriptor
291 const struct builtin_class_descr EDIT_builtin_class =
294 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
295 EditWndProcA, /* procA */
296 EditWndProcW, /* procW */
297 sizeof(EDITSTATE *), /* extra */
298 IDC_IBEAMA, /* cursor */
303 /*********************************************************************
308 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
310 return (es->undo_insert_count || strlenW(es->undo_text));
314 /*********************************************************************
319 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
321 es->undo_insert_count = 0;
322 *es->undo_text = '\0';
326 /*********************************************************************
331 static inline void EDIT_WM_Clear(HWND hwnd, EDITSTATE *es)
333 static const WCHAR empty_stringW[] = {0};
335 /* Protect read-only edit control from modification */
336 if(es->style & ES_READONLY)
339 EDIT_EM_ReplaceSel(hwnd, es, TRUE, empty_stringW, TRUE);
343 /*********************************************************************
348 static inline void EDIT_WM_Cut(HWND hwnd, EDITSTATE *es)
350 EDIT_WM_Copy(hwnd, es);
351 EDIT_WM_Clear(hwnd, es);
355 /**********************************************************************
358 * Returns the window version in case Wine emulates a later version
359 * of windows then the application expects.
361 * In a number of cases when windows runs an application that was
362 * designed for an earlier windows version, windows reverts
363 * to "old" behaviour of that earlier version.
365 * An example is a disabled edit control that needs to be painted.
366 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
367 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
368 * applications with an expected version 0f 4.0 or higher.
371 static DWORD get_app_version(void)
373 static DWORD version;
376 DWORD dwEmulatedVersion;
378 DWORD dwProcVersion = GetProcessVersion(0);
380 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
381 GetVersionExW( &info );
382 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
383 /* FIXME: this may not be 100% correct; see discussion on the
384 * wine developer list in Nov 1999 */
385 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
391 /*********************************************************************
395 * The messages are in the order of the actual integer values
396 * (which can be found in include/windows.h)
397 * Wherever possible the 16 bit versions are converted to
398 * the 32 bit ones, so that we can 'fall through' to the
399 * helper functions. These are mostly 32 bit (with a few
400 * exceptions, clearly indicated by a '16' extension to their
404 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
405 WPARAM wParam, LPARAM lParam, BOOL unicode )
407 EDITSTATE *es = (EDITSTATE *)GetWindowLongA( hwnd, 0 );
412 DPRINTF_EDIT_MSG32("WM_DESTROY");
413 if (es) EDIT_WM_Destroy(hwnd, es);
418 DPRINTF_EDIT_MSG32("WM_NCCREATE");
421 LPCREATESTRUCTW cs = (LPCREATESTRUCTW)lParam;
422 result = EDIT_WM_NCCreate(hwnd, cs->style, cs->hwndParent, TRUE);
426 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
427 result = EDIT_WM_NCCreate(hwnd, cs->style, cs->hwndParent, FALSE);
435 result = DefWindowProcW(hwnd, msg, wParam, lParam);
437 result = DefWindowProcA(hwnd, msg, wParam, lParam);
442 EDIT_LockBuffer(hwnd, es);
445 DPRINTF_EDIT_MSG16("EM_GETSEL");
450 DPRINTF_EDIT_MSG32("EM_GETSEL");
451 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
455 DPRINTF_EDIT_MSG16("EM_SETSEL");
456 if (SLOWORD(lParam) == -1)
457 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
459 EDIT_EM_SetSel(hwnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
461 EDIT_EM_ScrollCaret(hwnd, es);
465 DPRINTF_EDIT_MSG32("EM_SETSEL");
466 EDIT_EM_SetSel(hwnd, es, wParam, lParam, FALSE);
467 EDIT_EM_ScrollCaret(hwnd, es);
472 DPRINTF_EDIT_MSG16("EM_GETRECT");
474 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
477 DPRINTF_EDIT_MSG32("EM_GETRECT");
479 CopyRect((LPRECT)lParam, &es->format_rect);
483 DPRINTF_EDIT_MSG16("EM_SETRECT");
484 if ((es->style & ES_MULTILINE) && lParam) {
486 CONV_RECT16TO32(MapSL(lParam), &rc);
487 EDIT_SetRectNP(hwnd, es, &rc);
488 EDIT_UpdateText(hwnd, es, NULL, TRUE);
492 DPRINTF_EDIT_MSG32("EM_SETRECT");
493 if ((es->style & ES_MULTILINE) && lParam) {
494 EDIT_SetRectNP(hwnd, es, (LPRECT)lParam);
495 EDIT_UpdateText(hwnd, es, NULL, TRUE);
500 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
501 if ((es->style & ES_MULTILINE) && lParam) {
503 CONV_RECT16TO32(MapSL(lParam), &rc);
504 EDIT_SetRectNP(hwnd, es, &rc);
508 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
509 if ((es->style & ES_MULTILINE) && lParam)
510 EDIT_SetRectNP(hwnd, es, (LPRECT)lParam);
514 DPRINTF_EDIT_MSG16("EM_SCROLL");
517 DPRINTF_EDIT_MSG32("EM_SCROLL");
518 result = EDIT_EM_Scroll(hwnd, es, (INT)wParam);
521 case EM_LINESCROLL16:
522 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
523 wParam = (WPARAM)(INT)SHIWORD(lParam);
524 lParam = (LPARAM)(INT)SLOWORD(lParam);
527 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
528 result = (LRESULT)EDIT_EM_LineScroll(hwnd, es, (INT)wParam, (INT)lParam);
531 case EM_SCROLLCARET16:
532 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
535 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
536 EDIT_EM_ScrollCaret(hwnd, es);
541 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
544 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
545 result = ((es->flags & EF_MODIFIED) != 0);
549 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
552 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
554 es->flags |= EF_MODIFIED;
556 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
559 case EM_GETLINECOUNT16:
560 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
562 case EM_GETLINECOUNT:
563 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
564 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
568 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
569 if ((INT16)wParam == -1)
573 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
574 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
578 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
579 EDIT_EM_SetHandle16(hwnd, es, (HLOCAL16)wParam);
582 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
583 EDIT_EM_SetHandle(hwnd, es, (HLOCAL)wParam);
587 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
588 result = (LRESULT)EDIT_EM_GetHandle16(hwnd, es);
591 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
592 result = (LRESULT)EDIT_EM_GetHandle(es);
596 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
599 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
600 result = EDIT_EM_GetThumb(hwnd, es);
603 /* messages 0x00bf and 0x00c0 missing from specs */
606 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
609 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
610 result = DefWindowProcW(hwnd, msg, wParam, lParam);
614 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
617 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
618 result = DefWindowProcW(hwnd, msg, wParam, lParam);
621 case EM_LINELENGTH16:
622 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
625 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
626 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
629 case EM_REPLACESEL16:
630 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
631 lParam = (LPARAM)MapSL(lParam);
632 unicode = FALSE; /* 16-bit message is always ascii */
637 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
640 textW = (LPWSTR)lParam;
643 LPSTR textA = (LPSTR)lParam;
644 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
645 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
646 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
649 EDIT_EM_ReplaceSel(hwnd, es, (BOOL)wParam, textW, TRUE);
653 HeapFree(GetProcessHeap(), 0, textW);
656 /* message 0x00c3 missing from specs */
659 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
662 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
663 result = DefWindowProcW(hwnd, msg, wParam, lParam);
667 DPRINTF_EDIT_MSG16("EM_GETLINE");
668 lParam = (LPARAM)MapSL(lParam);
669 unicode = FALSE; /* 16-bit message is always ascii */
672 DPRINTF_EDIT_MSG32("EM_GETLINE");
673 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
677 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
679 case EM_SETLIMITTEXT:
680 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
681 EDIT_EM_SetLimitText(es, (INT)wParam);
685 DPRINTF_EDIT_MSG16("EM_CANUNDO");
688 DPRINTF_EDIT_MSG32("EM_CANUNDO");
689 result = (LRESULT)EDIT_EM_CanUndo(es);
693 DPRINTF_EDIT_MSG16("EM_UNDO");
698 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
699 result = (LRESULT)EDIT_EM_Undo(hwnd, es);
703 DPRINTF_EDIT_MSG16("EM_FMTLINES");
706 DPRINTF_EDIT_MSG32("EM_FMTLINES");
707 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
710 case EM_LINEFROMCHAR16:
711 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
713 case EM_LINEFROMCHAR:
714 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
715 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
718 /* message 0x00ca missing from specs */
721 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
724 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
725 result = DefWindowProcW(hwnd, msg, wParam, lParam);
728 case EM_SETTABSTOPS16:
729 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
730 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
733 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
734 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
737 case EM_SETPASSWORDCHAR16:
738 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
739 unicode = FALSE; /* 16-bit message is always ascii */
741 case EM_SETPASSWORDCHAR:
744 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
747 charW = (WCHAR)wParam;
751 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
754 EDIT_EM_SetPasswordChar(hwnd, es, charW);
758 case EM_EMPTYUNDOBUFFER16:
759 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
761 case EM_EMPTYUNDOBUFFER:
762 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
763 EDIT_EM_EmptyUndoBuffer(es);
766 case EM_GETFIRSTVISIBLELINE16:
767 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
768 result = es->y_offset;
770 case EM_GETFIRSTVISIBLELINE:
771 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
772 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
775 case EM_SETREADONLY16:
776 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
779 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
781 SetWindowLongA( hwnd, GWL_STYLE,
782 GetWindowLongA( hwnd, GWL_STYLE ) | ES_READONLY );
783 es->style |= ES_READONLY;
785 SetWindowLongA( hwnd, GWL_STYLE,
786 GetWindowLongA( hwnd, GWL_STYLE ) & ~ES_READONLY );
787 es->style &= ~ES_READONLY;
792 case EM_SETWORDBREAKPROC16:
793 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
794 EDIT_EM_SetWordBreakProc16(hwnd, es, (EDITWORDBREAKPROC16)lParam);
796 case EM_SETWORDBREAKPROC:
797 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
798 EDIT_EM_SetWordBreakProc(hwnd, es, lParam);
801 case EM_GETWORDBREAKPROC16:
802 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
803 result = (LRESULT)es->word_break_proc16;
805 case EM_GETWORDBREAKPROC:
806 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
807 result = (LRESULT)es->word_break_proc;
810 case EM_GETPASSWORDCHAR16:
811 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
812 unicode = FALSE; /* 16-bit message is always ascii */
814 case EM_GETPASSWORDCHAR:
816 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
819 result = es->password_char;
822 WCHAR charW = es->password_char;
824 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
830 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
833 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
834 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
838 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
839 result = MAKELONG(es->left_margin, es->right_margin);
842 case EM_GETLIMITTEXT:
843 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
844 result = es->buffer_limit;
848 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
849 result = EDIT_EM_PosFromChar(hwnd, es, (INT)wParam, FALSE);
853 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
854 result = EDIT_EM_CharFromPos(hwnd, es, SLOWORD(lParam), SHIWORD(lParam));
857 /* End of the EM_ messages which were in numerical order; what order
858 * are these in? vaguely alphabetical?
862 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
863 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
865 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
867 int vk = (int)((LPMSG)lParam)->wParam;
869 if (vk == VK_RETURN && (GetWindowLongA( hwnd, GWL_STYLE ) & ES_WANTRETURN))
871 result |= DLGC_WANTMESSAGE;
873 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
875 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
876 result |= DLGC_WANTMESSAGE;
884 DPRINTF_EDIT_MSG32("WM_CHAR");
891 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
894 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
896 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
897 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
900 EDIT_WM_Char(hwnd, es, charW);
905 DPRINTF_EDIT_MSG32("WM_CLEAR");
906 EDIT_WM_Clear(hwnd, es);
910 DPRINTF_EDIT_MSG32("WM_COMMAND");
911 EDIT_WM_Command(hwnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
915 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
916 EDIT_WM_ContextMenu(hwnd, es, SLOWORD(lParam), SHIWORD(lParam));
920 DPRINTF_EDIT_MSG32("WM_COPY");
921 EDIT_WM_Copy(hwnd, es);
925 DPRINTF_EDIT_MSG32("WM_CREATE");
927 result = EDIT_WM_Create(hwnd, es, ((LPCREATESTRUCTW)lParam)->lpszName);
930 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
934 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
935 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
936 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
938 result = EDIT_WM_Create(hwnd, es, nameW);
940 HeapFree(GetProcessHeap(), 0, nameW);
945 DPRINTF_EDIT_MSG32("WM_CUT");
946 EDIT_WM_Cut(hwnd, es);
950 DPRINTF_EDIT_MSG32("WM_ENABLE");
951 es->bEnableState = (BOOL) wParam;
952 EDIT_UpdateText(hwnd, es, NULL, TRUE);
956 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
957 result = EDIT_WM_EraseBkGnd(hwnd, es, (HDC)wParam);
961 DPRINTF_EDIT_MSG32("WM_GETFONT");
962 result = (LRESULT)es->font;
966 DPRINTF_EDIT_MSG32("WM_GETTEXT");
967 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
970 case WM_GETTEXTLENGTH:
971 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
972 if (unicode) result = strlenW(es->text);
973 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
974 NULL, 0, NULL, NULL );
978 DPRINTF_EDIT_MSG32("WM_HSCROLL");
979 result = EDIT_WM_HScroll(hwnd, es, LOWORD(wParam), SHIWORD(wParam));
983 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
984 result = EDIT_WM_KeyDown(hwnd, es, (INT)wParam);
988 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
989 result = EDIT_WM_KillFocus(hwnd, es);
992 case WM_LBUTTONDBLCLK:
993 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
994 result = EDIT_WM_LButtonDblClk(hwnd, es);
998 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
999 result = EDIT_WM_LButtonDown(hwnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
1003 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
1004 result = EDIT_WM_LButtonUp(hwnd, es);
1007 case WM_MBUTTONDOWN:
1008 DPRINTF_EDIT_MSG32("WM_MBUTTONDOWN");
1009 result = EDIT_WM_MButtonDown(hwnd);
1012 case WM_MOUSEACTIVATE:
1014 * FIXME: maybe DefWindowProc() screws up, but it seems that
1015 * modeless dialog boxes need this. If we don't do this, the focus
1016 * will _not_ be set by DefWindowProc() for edit controls in a
1017 * modeless dialog box ???
1019 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
1021 result = MA_ACTIVATE;
1026 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
1028 result = EDIT_WM_MouseMove(hwnd, es, SLOWORD(lParam), SHIWORD(lParam));
1032 DPRINTF_EDIT_MSG32("WM_PAINT");
1033 EDIT_WM_Paint(hwnd, es, wParam);
1037 DPRINTF_EDIT_MSG32("WM_PASTE");
1038 EDIT_WM_Paste(hwnd, es);
1042 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
1043 EDIT_WM_SetFocus(hwnd, es);
1047 DPRINTF_EDIT_MSG32("WM_SETFONT");
1048 EDIT_WM_SetFont(hwnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
1052 /* FIXME: actually set an internal flag and behave accordingly */
1056 DPRINTF_EDIT_MSG32("WM_SETTEXT");
1057 EDIT_WM_SetText(hwnd, es, lParam, unicode);
1062 DPRINTF_EDIT_MSG32("WM_SIZE");
1063 EDIT_WM_Size(hwnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
1066 case WM_STYLECHANGED:
1067 DPRINTF_EDIT_MSG32("WM_STYLECHANGED");
1068 result = EDIT_WM_StyleChanged (hwnd, es, wParam, (const STYLESTRUCT *)lParam);
1071 case WM_STYLECHANGING:
1072 DPRINTF_EDIT_MSG32("WM_STYLECHANGING");
1073 result = 0; /* See EDIT_WM_StyleChanged */
1077 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
1078 result = EDIT_WM_SysKeyDown(hwnd, es, (INT)wParam, (DWORD)lParam);
1082 DPRINTF_EDIT_MSG32("WM_TIMER");
1083 EDIT_WM_Timer(hwnd, es);
1087 DPRINTF_EDIT_MSG32("WM_VSCROLL");
1088 result = EDIT_WM_VScroll(hwnd, es, LOWORD(wParam), SHIWORD(wParam));
1093 int gcWheelDelta = 0;
1094 UINT pulScrollLines = 3;
1095 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1097 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1098 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1101 gcWheelDelta -= SHIWORD(wParam);
1102 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1104 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1105 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1106 result = EDIT_EM_LineScroll(hwnd, es, 0, cLineScroll);
1112 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1114 result = DefWindowProcA(hwnd, msg, wParam, lParam);
1117 EDIT_UnlockBuffer(hwnd, es, FALSE);
1122 /*********************************************************************
1124 * EditWndProcW (USER32.@)
1126 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1128 if (!IsWindow( hWnd )) return 0;
1129 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1132 /*********************************************************************
1134 * EditWndProc (USER32.@)
1136 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1138 if (!IsWindow( hWnd )) return 0;
1139 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1142 /*********************************************************************
1144 * EDIT_BuildLineDefs_ML
1146 * Build linked list of text lines.
1147 * Lines can end with '\0' (last line), a character (if it is wrapped),
1148 * a soft return '\r\r\n' or a hard return '\r\n'
1151 static void EDIT_BuildLineDefs_ML(HWND hwnd, EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1155 LPWSTR current_position, cp;
1157 LINEDEF *current_line;
1158 LINEDEF *previous_line;
1159 LINEDEF *start_line;
1160 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1161 INT line_count = es->line_count;
1162 INT orig_net_length;
1165 if (istart == iend && delta == 0)
1170 old_font = SelectObject(dc, es->font);
1172 previous_line = NULL;
1173 current_line = es->first_line_def;
1175 /* Find starting line. istart must lie inside an existing line or
1176 * at the end of buffer */
1178 if (istart < current_line->index + current_line->length ||
1179 current_line->ending == END_0)
1182 previous_line = current_line;
1183 current_line = current_line->next;
1185 } while (current_line);
1187 if (!current_line) /* Error occurred start is not inside previous buffer */
1189 FIXME(" modification occurred outside buffer\n");
1193 /* Remember start of modifications in order to calculate update region */
1194 nstart_line = line_index;
1195 nstart_index = current_line->index;
1197 /* We must start to reformat from the previous line since the modifications
1198 * may have caused the line to wrap upwards. */
1199 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1202 current_line = previous_line;
1204 start_line = current_line;
1206 fw = es->format_rect.right - es->format_rect.left;
1207 current_position = es->text + current_line->index;
1209 if (current_line != start_line)
1211 if (!current_line || current_line->index + delta > current_position - es->text)
1213 /* The buffer has been expanded, create a new line and
1214 insert it into the link list */
1215 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1216 new_line->next = previous_line->next;
1217 previous_line->next = new_line;
1218 current_line = new_line;
1221 else if (current_line->index + delta < current_position - es->text)
1223 /* The previous line merged with this line so we delete this extra entry */
1224 previous_line->next = current_line->next;
1225 HeapFree(GetProcessHeap(), 0, current_line);
1226 current_line = previous_line->next;
1230 else /* current_line->index + delta == current_position */
1232 if (current_position - es->text > iend)
1233 break; /* We reached end of line modifications */
1234 /* else recalulate this line */
1238 current_line->index = current_position - es->text;
1239 orig_net_length = current_line->net_length;
1241 /* Find end of line */
1242 cp = current_position;
1244 if (*cp == '\n') break;
1245 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1250 /* Mark type of line termination */
1252 current_line->ending = END_0;
1253 current_line->net_length = strlenW(current_position);
1254 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1255 current_line->ending = END_SOFT;
1256 current_line->net_length = cp - current_position - 1;
1257 } else if (*cp == '\n') {
1258 current_line->ending = END_RICH;
1259 current_line->net_length = cp - current_position;
1261 current_line->ending = END_HARD;
1262 current_line->net_length = cp - current_position;
1265 /* Calculate line width */
1266 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1267 current_position, current_line->net_length,
1268 es->tabs_count, es->tabs));
1270 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1271 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1276 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1277 prev + 1, current_line->net_length, WB_RIGHT);
1278 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1279 current_position, next, es->tabs_count, es->tabs));
1280 } while (current_line->width <= fw);
1281 if (!prev) { /* Didn't find a line break so force a break */
1286 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1287 current_position, next, es->tabs_count, es->tabs));
1288 } while (current_line->width <= fw);
1293 /* If the first line we are calculating, wrapped before istart, we must
1294 * adjust istart in order for this to be reflected in the update region. */
1295 if (current_line->index == nstart_index && istart > current_line->index + prev)
1296 istart = current_line->index + prev;
1297 /* else if we are updating the previous line before the first line we
1298 * are re-calculating and it expanded */
1299 else if (current_line == start_line &&
1300 current_line->index != nstart_index && orig_net_length < prev)
1302 /* Line expanded due to an upwards line wrap so we must partially include
1303 * previous line in update region */
1304 nstart_line = line_index;
1305 nstart_index = current_line->index;
1306 istart = current_line->index + orig_net_length;
1309 current_line->net_length = prev;
1310 current_line->ending = END_WRAP;
1311 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1312 current_line->net_length, es->tabs_count, es->tabs));
1316 /* Adjust length to include line termination */
1317 switch (current_line->ending) {
1319 current_line->length = current_line->net_length + 3;
1322 current_line->length = current_line->net_length + 1;
1325 current_line->length = current_line->net_length + 2;
1329 current_line->length = current_line->net_length;
1332 es->text_width = max(es->text_width, current_line->width);
1333 current_position += current_line->length;
1334 previous_line = current_line;
1335 current_line = current_line->next;
1337 } while (previous_line->ending != END_0);
1339 /* Finish adjusting line indexes by delta or remove hanging lines */
1340 if (previous_line->ending == END_0)
1342 LINEDEF *pnext = NULL;
1344 previous_line->next = NULL;
1345 while (current_line)
1347 pnext = current_line->next;
1348 HeapFree(GetProcessHeap(), 0, current_line);
1349 current_line = pnext;
1355 while (current_line)
1357 current_line->index += delta;
1358 current_line = current_line->next;
1362 /* Calculate rest of modification rectangle */
1367 * We calculate two rectangles. One for the first line which may have
1368 * an indent with respect to the format rect. The other is a format-width
1369 * rectangle that spans the rest of the lines that changed or moved.
1371 rc.top = es->format_rect.top + nstart_line * es->line_height -
1372 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1373 rc.bottom = rc.top + es->line_height;
1374 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1375 es->text + nstart_index, istart - nstart_index,
1376 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1377 rc.right = es->format_rect.right;
1378 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1381 rc.left = es->format_rect.left;
1382 rc.right = es->format_rect.right;
1384 * If lines were added or removed we must re-paint the remainder of the
1385 * lines since the remaining lines were either shifted up or down.
1387 if (line_count < es->line_count) /* We added lines */
1388 rc.bottom = es->line_count * es->line_height;
1389 else if (line_count > es->line_count) /* We removed lines */
1390 rc.bottom = line_count * es->line_height;
1392 rc.bottom = line_index * es->line_height;
1393 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1394 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1395 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1396 DeleteObject(tmphrgn);
1400 SelectObject(dc, old_font);
1402 ReleaseDC(hwnd, dc);
1405 /*********************************************************************
1407 * EDIT_CalcLineWidth_SL
1410 static void EDIT_CalcLineWidth_SL(HWND hwnd, EDITSTATE *es)
1412 es->text_width = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, strlenW(es->text), FALSE));
1415 /*********************************************************************
1417 * EDIT_CallWordBreakProc
1419 * Call appropriate WordBreakProc (internal or external).
1421 * Note: The "start" argument should always be an index referring
1422 * to es->text. The actual wordbreak proc might be
1423 * 16 bit, so we can't always pass any 32 bit LPSTR.
1424 * Hence we assume that es->text is the buffer that holds
1425 * the string under examination (we can decide this for ourselves).
1428 /* ### start build ### */
1429 extern WORD CALLBACK EDIT_CallTo16_word_lwww(EDITWORDBREAKPROC16,SEGPTR,WORD,WORD,WORD);
1430 /* ### stop build ### */
1431 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1433 INT ret, iWndsLocks;
1435 /* To avoid any deadlocks, all the locks on the window structures
1436 must be suspended before the control is passed to the application */
1437 iWndsLocks = WIN_SuspendWndsLock();
1439 if (es->word_break_proc16) {
1444 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1445 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1446 segptr = K32WOWGlobalLock16(hglob16);
1447 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1448 ret = (INT)EDIT_CallTo16_word_lwww(es->word_break_proc16,
1449 segptr, index, countA, action);
1450 GlobalUnlock16(hglob16);
1451 GlobalFree16(hglob16);
1453 else if (es->word_break_proc)
1457 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1459 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1460 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1461 ret = wbpW(es->text + start, index, count, action);
1465 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1469 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1470 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1471 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1472 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1473 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1474 ret = wbpA(textA, index, countA, action);
1475 HeapFree(GetProcessHeap(), 0, textA);
1479 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1481 WIN_RestoreWndsLock(iWndsLocks);
1486 /*********************************************************************
1490 * Beware: This is not the function called on EM_CHARFROMPOS
1491 * The position _can_ be outside the formatting / client
1493 * The return value is only the character index
1496 static INT EDIT_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1502 if (es->style & ES_MULTILINE) {
1503 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1505 LINEDEF *line_def = es->first_line_def;
1507 while ((line > 0) && line_def->next) {
1508 line_index += line_def->length;
1509 line_def = line_def->next;
1512 x += es->x_offset - es->format_rect.left;
1513 if (x >= line_def->width) {
1515 *after_wrap = (line_def->ending == END_WRAP);
1516 return line_index + line_def->net_length;
1520 *after_wrap = FALSE;
1525 old_font = SelectObject(dc, es->font);
1526 low = line_index + 1;
1527 high = line_index + line_def->net_length + 1;
1528 while (low < high - 1)
1530 INT mid = (low + high) / 2;
1531 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1537 *after_wrap = ((index == line_index + line_def->net_length) &&
1538 (line_def->ending == END_WRAP));
1543 *after_wrap = FALSE;
1544 x -= es->format_rect.left;
1546 return es->x_offset;
1547 text = EDIT_GetPasswordPointer_SL(es);
1550 old_font = SelectObject(dc, es->font);
1554 INT high = es->x_offset;
1555 while (low < high - 1)
1557 INT mid = (low + high) / 2;
1558 GetTextExtentPoint32W( dc, text + mid,
1559 es->x_offset - mid, &size );
1560 if (size.cx > -x) low = mid;
1567 INT low = es->x_offset;
1568 INT high = strlenW(es->text) + 1;
1569 while (low < high - 1)
1571 INT mid = (low + high) / 2;
1572 GetTextExtentPoint32W( dc, text + es->x_offset,
1573 mid - es->x_offset, &size );
1574 if (size.cx > x) high = mid;
1579 if (es->style & ES_PASSWORD)
1580 HeapFree(GetProcessHeap(), 0, text);
1583 SelectObject(dc, old_font);
1584 ReleaseDC(hwnd, dc);
1589 /*********************************************************************
1593 * adjusts the point to be within the formatting rectangle
1594 * (so CharFromPos returns the nearest _visible_ character)
1597 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1599 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1600 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1604 /*********************************************************************
1608 * Calculates the bounding rectangle for a line from a starting
1609 * column to an ending column.
1612 static void EDIT_GetLineRect(HWND hwnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1614 INT line_index = EDIT_EM_LineIndex(es, line);
1616 if (es->style & ES_MULTILINE)
1617 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1619 rc->top = es->format_rect.top;
1620 rc->bottom = rc->top + es->line_height;
1621 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(hwnd, es, line_index + scol, TRUE));
1622 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(hwnd, es, line_index + ecol, TRUE));
1626 /*********************************************************************
1628 * EDIT_GetPasswordPointer_SL
1630 * note: caller should free the (optionally) allocated buffer
1633 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1635 if (es->style & ES_PASSWORD) {
1636 INT len = strlenW(es->text);
1637 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1639 while(len) text[--len] = es->password_char;
1646 /*********************************************************************
1650 * This acts as a LOCAL_Lock(), but it locks only once. This way
1651 * you can call it whenever you like, without unlocking.
1654 static void EDIT_LockBuffer(HWND hwnd, EDITSTATE *es)
1656 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
1658 ERR("no EDITSTATE ... please report\n");
1664 BOOL _16bit = FALSE;
1670 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1671 textA = LocalLock(es->hloc32A);
1672 countA = strlen(textA) + 1;
1676 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1677 textA = LOCAL_Lock(hInstance, es->hloc16);
1678 countA = strlen(textA) + 1;
1683 ERR("no buffer ... please report\n");
1690 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1691 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1692 if(countW_new > es->buffer_size + 1)
1694 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1695 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1696 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1699 es->hloc32W = hloc32W_new;
1700 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1701 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1704 WARN("FAILED! Will synchronize partially\n");
1708 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1709 es->text = LocalLock(es->hloc32W);
1713 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1715 LOCAL_Unlock(hInstance, es->hloc16);
1717 LocalUnlock(es->hloc32A);
1724 /*********************************************************************
1726 * EDIT_SL_InvalidateText
1728 * Called from EDIT_InvalidateText().
1729 * Does the job for single-line controls only.
1732 static void EDIT_SL_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end)
1737 EDIT_GetLineRect(hwnd, es, 0, start, end, &line_rect);
1738 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1739 EDIT_UpdateText(hwnd, es, &rc, TRUE);
1743 /*********************************************************************
1745 * EDIT_ML_InvalidateText
1747 * Called from EDIT_InvalidateText().
1748 * Does the job for multi-line controls only.
1751 static void EDIT_ML_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end)
1753 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1754 INT sl = EDIT_EM_LineFromChar(es, start);
1755 INT el = EDIT_EM_LineFromChar(es, end);
1764 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1767 sc = start - EDIT_EM_LineIndex(es, sl);
1768 ec = end - EDIT_EM_LineIndex(es, el);
1769 if (sl < es->y_offset) {
1773 if (el > es->y_offset + vlc) {
1774 el = es->y_offset + vlc;
1775 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1777 GetClientRect(hwnd, &rc1);
1778 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1780 EDIT_GetLineRect(hwnd, es, sl, sc, ec, &rcLine);
1781 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1782 EDIT_UpdateText(hwnd, es, &rcUpdate, TRUE);
1784 EDIT_GetLineRect(hwnd, es, sl, sc,
1785 EDIT_EM_LineLength(es,
1786 EDIT_EM_LineIndex(es, sl)),
1788 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1789 EDIT_UpdateText(hwnd, es, &rcUpdate, TRUE);
1790 for (l = sl + 1 ; l < el ; l++) {
1791 EDIT_GetLineRect(hwnd, es, l, 0,
1792 EDIT_EM_LineLength(es,
1793 EDIT_EM_LineIndex(es, l)),
1795 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1796 EDIT_UpdateText(hwnd, es, &rcUpdate, TRUE);
1798 EDIT_GetLineRect(hwnd, es, el, 0, ec, &rcLine);
1799 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1800 EDIT_UpdateText(hwnd, es, &rcUpdate, TRUE);
1805 /*********************************************************************
1807 * EDIT_InvalidateText
1809 * Invalidate the text from offset start upto, but not including,
1810 * offset end. Useful for (re)painting the selection.
1811 * Regions outside the linewidth are not invalidated.
1812 * end == -1 means end == TextLength.
1813 * start and end need not be ordered.
1816 static void EDIT_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end)
1822 end = strlenW(es->text);
1824 ORDER_INT(start, end);
1826 if (es->style & ES_MULTILINE)
1827 EDIT_ML_InvalidateText(hwnd, es, start, end);
1829 EDIT_SL_InvalidateText(hwnd, es, start, end);
1833 /*********************************************************************
1837 * Try to fit size + 1 characters in the buffer. Constrain to limits.
1840 static BOOL EDIT_MakeFit(HWND hwnd, EDITSTATE *es, UINT size)
1844 if (size <= es->buffer_size)
1846 if ((es->buffer_limit > 0) && (size > es->buffer_limit)) {
1847 EDIT_NOTIFY_PARENT(hwnd, es, EN_MAXTEXT, "EN_MAXTEXT");
1850 if ((es->buffer_limit > 0) && (size > es->buffer_limit))
1851 size = es->buffer_limit;
1853 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1855 /* Force edit to unlock it's buffer. es->text now NULL */
1856 EDIT_UnlockBuffer(hwnd, es, TRUE);
1859 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1860 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1861 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32W, hNew32W);
1862 es->hloc32W = hNew32W;
1863 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1867 EDIT_LockBuffer(hwnd, es);
1869 if (es->buffer_size < size) {
1870 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1871 EDIT_NOTIFY_PARENT(hwnd, es, EN_ERRSPACE, "EN_ERRSPACE");
1874 TRACE("We now have %d+1\n", es->buffer_size);
1880 /*********************************************************************
1884 * Try to fit size + 1 bytes in the undo buffer.
1887 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1891 if (size <= es->undo_buffer_size)
1894 TRACE("trying to ReAlloc to %d+1\n", size);
1896 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1897 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1898 es->undo_buffer_size = alloc_size/sizeof(WCHAR);
1903 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1909 /*********************************************************************
1914 static void EDIT_MoveBackward(HWND hwnd, EDITSTATE *es, BOOL extend)
1916 INT e = es->selection_end;
1920 if ((es->style & ES_MULTILINE) && e &&
1921 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1923 if (e && (es->text[e - 1] == '\r'))
1927 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, FALSE);
1928 EDIT_EM_ScrollCaret(hwnd, es);
1932 /*********************************************************************
1936 * Only for multi line controls
1937 * Move the caret one line down, on a column with the nearest
1938 * x coordinate on the screen (might be a different column).
1941 static void EDIT_MoveDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
1943 INT s = es->selection_start;
1944 INT e = es->selection_end;
1945 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1946 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
1947 INT x = SLOWORD(pos);
1948 INT y = SHIWORD(pos);
1950 e = EDIT_CharFromPos(hwnd, es, x, y + es->line_height, &after_wrap);
1953 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
1954 EDIT_EM_ScrollCaret(hwnd, es);
1958 /*********************************************************************
1963 static void EDIT_MoveEnd(HWND hwnd, EDITSTATE *es, BOOL extend)
1965 BOOL after_wrap = FALSE;
1968 /* Pass a high value in x to make sure of receiving the end of the line */
1969 if (es->style & ES_MULTILINE)
1970 e = EDIT_CharFromPos(hwnd, es, 0x3fffffff,
1971 HIWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1973 e = strlenW(es->text);
1974 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, after_wrap);
1975 EDIT_EM_ScrollCaret(hwnd, es);
1979 /*********************************************************************
1984 static void EDIT_MoveForward(HWND hwnd, EDITSTATE *es, BOOL extend)
1986 INT e = es->selection_end;
1990 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1991 if (es->text[e] == '\n')
1993 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1997 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, FALSE);
1998 EDIT_EM_ScrollCaret(hwnd, es);
2002 /*********************************************************************
2006 * Home key: move to beginning of line.
2009 static void EDIT_MoveHome(HWND hwnd, EDITSTATE *es, BOOL extend)
2013 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2014 if (es->style & ES_MULTILINE)
2015 e = EDIT_CharFromPos(hwnd, es, -es->x_offset,
2016 HIWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2019 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, FALSE);
2020 EDIT_EM_ScrollCaret(hwnd, es);
2024 /*********************************************************************
2026 * EDIT_MovePageDown_ML
2028 * Only for multi line controls
2029 * Move the caret one page down, on a column with the nearest
2030 * x coordinate on the screen (might be a different column).
2033 static void EDIT_MovePageDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
2035 INT s = es->selection_start;
2036 INT e = es->selection_end;
2037 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2038 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
2039 INT x = SLOWORD(pos);
2040 INT y = SHIWORD(pos);
2042 e = EDIT_CharFromPos(hwnd, es, x,
2043 y + (es->format_rect.bottom - es->format_rect.top),
2047 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
2048 EDIT_EM_ScrollCaret(hwnd, es);
2052 /*********************************************************************
2054 * EDIT_MovePageUp_ML
2056 * Only for multi line controls
2057 * Move the caret one page up, on a column with the nearest
2058 * x coordinate on the screen (might be a different column).
2061 static void EDIT_MovePageUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
2063 INT s = es->selection_start;
2064 INT e = es->selection_end;
2065 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2066 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
2067 INT x = SLOWORD(pos);
2068 INT y = SHIWORD(pos);
2070 e = EDIT_CharFromPos(hwnd, es, x,
2071 y - (es->format_rect.bottom - es->format_rect.top),
2075 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
2076 EDIT_EM_ScrollCaret(hwnd, es);
2080 /*********************************************************************
2084 * Only for multi line controls
2085 * Move the caret one line up, on a column with the nearest
2086 * x coordinate on the screen (might be a different column).
2089 static void EDIT_MoveUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
2091 INT s = es->selection_start;
2092 INT e = es->selection_end;
2093 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2094 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
2095 INT x = SLOWORD(pos);
2096 INT y = SHIWORD(pos);
2098 e = EDIT_CharFromPos(hwnd, es, x, y - es->line_height, &after_wrap);
2101 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
2102 EDIT_EM_ScrollCaret(hwnd, es);
2106 /*********************************************************************
2108 * EDIT_MoveWordBackward
2111 static void EDIT_MoveWordBackward(HWND hwnd, EDITSTATE *es, BOOL extend)
2113 INT s = es->selection_start;
2114 INT e = es->selection_end;
2119 l = EDIT_EM_LineFromChar(es, e);
2120 ll = EDIT_EM_LineLength(es, e);
2121 li = EDIT_EM_LineIndex(es, l);
2124 li = EDIT_EM_LineIndex(es, l - 1);
2125 e = li + EDIT_EM_LineLength(es, li);
2128 e = li + (INT)EDIT_CallWordBreakProc(es,
2129 li, e - li, ll, WB_LEFT);
2133 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
2134 EDIT_EM_ScrollCaret(hwnd, es);
2138 /*********************************************************************
2140 * EDIT_MoveWordForward
2143 static void EDIT_MoveWordForward(HWND hwnd, EDITSTATE *es, BOOL extend)
2145 INT s = es->selection_start;
2146 INT e = es->selection_end;
2151 l = EDIT_EM_LineFromChar(es, e);
2152 ll = EDIT_EM_LineLength(es, e);
2153 li = EDIT_EM_LineIndex(es, l);
2155 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2156 e = EDIT_EM_LineIndex(es, l + 1);
2158 e = li + EDIT_CallWordBreakProc(es,
2159 li, e - li + 1, ll, WB_RIGHT);
2163 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
2164 EDIT_EM_ScrollCaret(hwnd, es);
2168 /*********************************************************************
2173 static void EDIT_PaintLine(HWND hwnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
2175 INT s = es->selection_start;
2176 INT e = es->selection_end;
2183 if (es->style & ES_MULTILINE) {
2184 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2185 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2190 TRACE("line=%d\n", line);
2192 pos = EDIT_EM_PosFromChar(hwnd, es, EDIT_EM_LineIndex(es, line), FALSE);
2195 li = EDIT_EM_LineIndex(es, line);
2196 ll = EDIT_EM_LineLength(es, li);
2197 s = es->selection_start;
2198 e = es->selection_end;
2200 s = min(li + ll, max(li, s));
2201 e = min(li + ll, max(li, e));
2202 if (rev && (s != e) &&
2203 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2204 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2205 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2206 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2208 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2212 /*********************************************************************
2217 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2228 BkMode = GetBkMode(dc);
2229 BkColor = GetBkColor(dc);
2230 TextColor = GetTextColor(dc);
2232 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2233 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2234 SetBkMode( dc, OPAQUE);
2236 li = EDIT_EM_LineIndex(es, line);
2237 if (es->style & ES_MULTILINE) {
2238 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2239 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2241 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2242 TextOutW(dc, x, y, text + li + col, count);
2243 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2245 if (es->style & ES_PASSWORD)
2246 HeapFree(GetProcessHeap(), 0, text);
2249 SetBkColor(dc, BkColor);
2250 SetTextColor(dc, TextColor);
2251 SetBkMode( dc, BkMode);
2257 /*********************************************************************
2262 static void EDIT_SetCaretPos(HWND hwnd, EDITSTATE *es, INT pos,
2265 LRESULT res = EDIT_EM_PosFromChar(hwnd, es, pos, after_wrap);
2266 SetCaretPos(SLOWORD(res), SHIWORD(res));
2270 /*********************************************************************
2274 * note: this is not (exactly) the handler called on EM_SETRECTNP
2275 * it is also used to set the rect of a single line control
2278 static void EDIT_SetRectNP(HWND hwnd, EDITSTATE *es, LPRECT rc)
2280 CopyRect(&es->format_rect, rc);
2281 if (es->style & WS_BORDER) {
2282 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2283 if(TWEAK_WineLook == WIN31_LOOK)
2285 es->format_rect.left += bw;
2286 es->format_rect.top += bw;
2287 es->format_rect.right -= bw;
2288 es->format_rect.bottom -= bw;
2290 es->format_rect.left += es->left_margin;
2291 es->format_rect.right -= es->right_margin;
2292 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2293 if (es->style & ES_MULTILINE)
2295 INT fw, vlc, max_x_offset, max_y_offset;
2297 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2298 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2300 /* correct es->x_offset */
2301 fw = es->format_rect.right - es->format_rect.left;
2302 max_x_offset = es->text_width - fw;
2303 if(max_x_offset < 0) max_x_offset = 0;
2304 if(es->x_offset > max_x_offset)
2305 es->x_offset = max_x_offset;
2307 /* correct es->y_offset */
2308 max_y_offset = es->line_count - vlc;
2309 if(max_y_offset < 0) max_y_offset = 0;
2310 if(es->y_offset > max_y_offset)
2311 es->y_offset = max_y_offset;
2313 /* force scroll info update */
2314 EDIT_UpdateScrollInfo(hwnd, es);
2317 /* Windows doesn't care to fix text placement for SL controls */
2318 es->format_rect.bottom = es->format_rect.top + es->line_height;
2320 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2321 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
2325 /*********************************************************************
2330 static void EDIT_UnlockBuffer(HWND hwnd, EDITSTATE *es, BOOL force)
2332 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
2334 /* Edit window might be already destroyed */
2337 WARN("edit hwnd %04x already destroyed\n", hwnd);
2342 ERR("no EDITSTATE ... please report\n");
2345 if (!es->lock_count) {
2346 ERR("lock_count == 0 ... please report\n");
2350 ERR("es->text == 0 ... please report\n");
2354 if (force || (es->lock_count == 1)) {
2357 BOOL _16bit = FALSE;
2359 UINT countW = strlenW(es->text) + 1;
2363 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2364 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2365 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2366 countA = LocalSize(es->hloc32A);
2367 if(countA_new > countA)
2370 UINT alloc_size = ROUND_TO_GROW(countA_new);
2371 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2372 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2375 es->hloc32A = hloc32A_new;
2376 countA = LocalSize(hloc32A_new);
2377 TRACE("Real new size %d bytes\n", countA);
2380 WARN("FAILED! Will synchronize partially\n");
2382 textA = LocalLock(es->hloc32A);
2386 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2387 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2388 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2389 countA = LOCAL_Size(hInstance, es->hloc16);
2390 if(countA_new > countA)
2392 HLOCAL16 hloc16_new;
2393 UINT alloc_size = ROUND_TO_GROW(countA_new);
2394 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2395 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2398 es->hloc16 = hloc16_new;
2399 countA = LOCAL_Size(hInstance, hloc16_new);
2400 TRACE("Real new size %d bytes\n", countA);
2403 WARN("FAILED! Will synchronize partially\n");
2405 textA = LOCAL_Lock(hInstance, es->hloc16);
2411 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2413 LOCAL_Unlock(hInstance, es->hloc16);
2415 LocalUnlock(es->hloc32A);
2418 LocalUnlock(es->hloc32W);
2422 ERR("no buffer ... please report\n");
2430 /*********************************************************************
2432 * EDIT_UpdateScrollInfo
2435 static void EDIT_UpdateScrollInfo(HWND hwnd, EDITSTATE *es)
2437 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2440 si.cbSize = sizeof(SCROLLINFO);
2441 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2443 si.nMax = es->line_count - 1;
2444 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2445 si.nPos = es->y_offset;
2446 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2447 si.nMin, si.nMax, si.nPage, si.nPos);
2448 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2451 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2454 si.cbSize = sizeof(SCROLLINFO);
2455 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2457 si.nMax = es->text_width - 1;
2458 si.nPage = es->format_rect.right - es->format_rect.left;
2459 si.nPos = es->x_offset;
2460 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2461 si.nMin, si.nMax, si.nPage, si.nPos);
2462 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2466 /*********************************************************************
2468 * EDIT_WordBreakProc
2470 * Find the beginning of words.
2471 * Note: unlike the specs for a WordBreakProc, this function only
2472 * allows to be called without linebreaks between s[0] upto
2473 * s[count - 1]. Remember it is only called
2474 * internally, so we can decide this for ourselves.
2477 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2481 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2491 if (s[index] == ' ') {
2492 while (index && (s[index] == ' '))
2495 while (index && (s[index] != ' '))
2497 if (s[index] == ' ')
2501 while (index && (s[index] != ' '))
2503 if (s[index] == ' ')
2513 if (s[index] == ' ')
2514 while ((index < count) && (s[index] == ' ')) index++;
2516 while (s[index] && (s[index] != ' ') && (index < count))
2518 while ((s[index] == ' ') && (index < count)) index++;
2522 case WB_ISDELIMITER:
2523 ret = (s[index] == ' ');
2526 ERR("unknown action code, please report !\n");
2533 /*********************************************************************
2537 * returns line number (not index) in high-order word of result.
2538 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2539 * to Richedit, not to the edit control. Original documentation is valid.
2540 * FIXME: do the specs mean to return -1 if outside client area or
2541 * if outside formatting rectangle ???
2544 static LRESULT EDIT_EM_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y)
2552 GetClientRect(hwnd, &rc);
2553 if (!PtInRect(&rc, pt))
2556 index = EDIT_CharFromPos(hwnd, es, x, y, NULL);
2557 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2561 /*********************************************************************
2565 * Enable or disable soft breaks.
2567 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2569 es->flags &= ~EF_USE_SOFTBRK;
2571 es->flags |= EF_USE_SOFTBRK;
2572 FIXME("soft break enabled, not implemented\n");
2578 /*********************************************************************
2582 * Hopefully this won't fire back at us.
2583 * We always start with a fixed buffer in the local heap.
2584 * Despite of the documentation says that the local heap is used
2585 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2586 * buffer on the local heap.
2589 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2593 if (!(es->style & ES_MULTILINE))
2597 hLocal = es->hloc32W;
2603 UINT countA, alloc_size;
2604 TRACE("Allocating 32-bit ANSI alias buffer\n");
2605 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2606 alloc_size = ROUND_TO_GROW(countA);
2607 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2609 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2612 textA = LocalLock(es->hloc32A);
2613 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2614 LocalUnlock(es->hloc32A);
2616 hLocal = es->hloc32A;
2619 TRACE("Returning %04X, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2624 /*********************************************************************
2628 * Hopefully this won't fire back at us.
2629 * We always start with a buffer in 32 bit linear memory.
2630 * However, with this message a 16 bit application requests
2631 * a handle of 16 bit local heap memory, where it expects to find
2633 * It's a pitty that from this moment on we have to use this
2634 * local heap, because applications may rely on the handle
2637 * In this function we'll try to switch to local heap.
2639 static HLOCAL16 EDIT_EM_GetHandle16(HWND hwnd, EDITSTATE *es)
2641 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
2643 UINT countA, alloc_size;
2645 if (!(es->style & ES_MULTILINE))
2651 if (!LOCAL_HeapSize(hInstance)) {
2652 if (!LocalInit16(hInstance, 0,
2653 GlobalSize16(hInstance))) {
2654 ERR("could not initialize local heap\n");
2657 TRACE("local heap initialized\n");
2660 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2661 alloc_size = ROUND_TO_GROW(countA);
2663 TRACE("Allocating 16-bit ANSI alias buffer\n");
2664 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2665 ERR("could not allocate new 16 bit buffer\n");
2669 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2670 ERR("could not lock new 16 bit buffer\n");
2671 LOCAL_Free(hInstance, es->hloc16);
2676 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2677 LOCAL_Unlock(hInstance, es->hloc16);
2679 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2684 /*********************************************************************
2689 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2692 INT line_len, dst_len;
2695 if (es->style & ES_MULTILINE) {
2696 if (line >= es->line_count)
2700 i = EDIT_EM_LineIndex(es, line);
2702 line_len = EDIT_EM_LineLength(es, i);
2703 dst_len = *(WORD *)lParam;
2706 LPWSTR dst = (LPWSTR)lParam;
2707 if(dst_len <= line_len)
2709 memcpy(dst, src, dst_len * sizeof(WCHAR));
2712 else /* Append 0 if enough space */
2714 memcpy(dst, src, line_len * sizeof(WCHAR));
2721 LPSTR dst = (LPSTR)lParam;
2723 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2724 if(!ret) /* Insufficient buffer size */
2726 if(ret < dst_len) /* Append 0 if enough space */
2733 /*********************************************************************
2738 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2740 UINT s = es->selection_start;
2741 UINT e = es->selection_end;
2748 return MAKELONG(s, e);
2752 /*********************************************************************
2756 * FIXME: is this right ? (or should it be only VSCROLL)
2757 * (and maybe only for edit controls that really have their
2758 * own scrollbars) (and maybe only for multiline controls ?)
2759 * All in all: very poorly documented
2762 static LRESULT EDIT_EM_GetThumb(HWND hwnd, EDITSTATE *es)
2764 return MAKELONG(EDIT_WM_VScroll(hwnd, es, EM_GETTHUMB16, 0),
2765 EDIT_WM_HScroll(hwnd, es, EM_GETTHUMB16, 0));
2769 /*********************************************************************
2774 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2779 if (!(es->style & ES_MULTILINE))
2781 if (index > (INT)strlenW(es->text))
2782 return es->line_count - 1;
2784 index = min(es->selection_start, es->selection_end);
2787 line_def = es->first_line_def;
2788 index -= line_def->length;
2789 while ((index >= 0) && line_def->next) {
2791 line_def = line_def->next;
2792 index -= line_def->length;
2798 /*********************************************************************
2803 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2808 if (!(es->style & ES_MULTILINE))
2810 if (line >= es->line_count)
2814 line_def = es->first_line_def;
2816 INT index = es->selection_end - line_def->length;
2817 while ((index >= 0) && line_def->next) {
2818 line_index += line_def->length;
2819 line_def = line_def->next;
2820 index -= line_def->length;
2824 line_index += line_def->length;
2825 line_def = line_def->next;
2833 /*********************************************************************
2838 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2842 if (!(es->style & ES_MULTILINE))
2843 return strlenW(es->text);
2846 /* get the number of remaining non-selected chars of selected lines */
2847 INT32 l; /* line number */
2848 INT32 li; /* index of first char in line */
2850 l = EDIT_EM_LineFromChar(es, es->selection_start);
2851 /* # chars before start of selection area */
2852 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2853 l = EDIT_EM_LineFromChar(es, es->selection_end);
2854 /* # chars after end of selection */
2855 li = EDIT_EM_LineIndex(es, l);
2856 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2859 line_def = es->first_line_def;
2860 index -= line_def->length;
2861 while ((index >= 0) && line_def->next) {
2862 line_def = line_def->next;
2863 index -= line_def->length;
2865 return line_def->net_length;
2869 /*********************************************************************
2873 * NOTE: dx is in average character widths, dy - in lines;
2876 static BOOL EDIT_EM_LineScroll(HWND hwnd, EDITSTATE *es, INT dx, INT dy)
2878 if (!(es->style & ES_MULTILINE))
2881 dx *= es->char_width;
2882 return EDIT_EM_LineScroll_internal(hwnd, es, dx, dy);
2885 /*********************************************************************
2887 * EDIT_EM_LineScroll_internal
2889 * Version of EDIT_EM_LineScroll for internal use.
2890 * It doesn't refuse if ES_MULTILINE is set and assumes that
2891 * dx is in pixels, dy - in lines.
2894 static BOOL EDIT_EM_LineScroll_internal(HWND hwnd, EDITSTATE *es, INT dx, INT dy)
2897 INT x_offset_in_pixels;
2899 if (es->style & ES_MULTILINE)
2901 x_offset_in_pixels = es->x_offset;
2906 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->x_offset, FALSE));
2909 if (-dx > x_offset_in_pixels)
2910 dx = -x_offset_in_pixels;
2911 if (dx > es->text_width - x_offset_in_pixels)
2912 dx = es->text_width - x_offset_in_pixels;
2913 nyoff = max(0, es->y_offset + dy);
2914 if (nyoff >= es->line_count)
2915 nyoff = es->line_count - 1;
2916 dy = (es->y_offset - nyoff) * es->line_height;
2921 es->y_offset = nyoff;
2922 if(es->style & ES_MULTILINE)
2925 es->x_offset += dx / es->char_width;
2927 GetClientRect(hwnd, &rc1);
2928 IntersectRect(&rc, &rc1, &es->format_rect);
2929 ScrollWindowEx(hwnd, -dx, dy,
2930 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2931 /* force scroll info update */
2932 EDIT_UpdateScrollInfo(hwnd, es);
2934 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2935 EDIT_NOTIFY_PARENT(hwnd, es, EN_HSCROLL, "EN_HSCROLL");
2936 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2937 EDIT_NOTIFY_PARENT(hwnd, es, EN_VSCROLL, "EN_VSCROLL");
2942 /*********************************************************************
2947 static LRESULT EDIT_EM_PosFromChar(HWND hwnd, EDITSTATE *es, INT index, BOOL after_wrap)
2949 INT len = strlenW(es->text);
2958 index = min(index, len);
2961 old_font = SelectObject(dc, es->font);
2962 if (es->style & ES_MULTILINE) {
2963 l = EDIT_EM_LineFromChar(es, index);
2964 y = (l - es->y_offset) * es->line_height;
2965 li = EDIT_EM_LineIndex(es, l);
2966 if (after_wrap && (li == index) && l) {
2968 LINEDEF *line_def = es->first_line_def;
2970 line_def = line_def->next;
2973 if (line_def->ending == END_WRAP) {
2975 y -= es->line_height;
2976 li = EDIT_EM_LineIndex(es, l);
2979 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2980 es->tabs_count, es->tabs)) - es->x_offset;
2982 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2983 if (index < es->x_offset) {
2984 GetTextExtentPoint32W(dc, text + index,
2985 es->x_offset - index, &size);
2988 GetTextExtentPoint32W(dc, text + es->x_offset,
2989 index - es->x_offset, &size);
2993 if (es->style & ES_PASSWORD)
2994 HeapFree(GetProcessHeap(), 0, text);
2996 x += es->format_rect.left;
2997 y += es->format_rect.top;
2999 SelectObject(dc, old_font);
3000 ReleaseDC(hwnd, dc);
3001 return MAKELONG((INT16)x, (INT16)y);
3005 /*********************************************************************
3009 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3012 static void EDIT_EM_ReplaceSel(HWND hwnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
3014 UINT strl = strlenW(lpsz_replace);
3015 UINT tl = strlenW(es->text);
3023 TRACE("%s, can_undo %d, send_update %d\n",
3024 debugstr_w(lpsz_replace), can_undo, send_update);
3026 s = es->selection_start;
3027 e = es->selection_end;
3029 if ((s == e) && !strl)
3034 if (!EDIT_MakeFit(hwnd, es, tl - (e - s) + strl))
3038 /* there is something to be deleted */
3039 TRACE("deleting stuff.\n");
3041 utl = strlenW(es->undo_text);
3042 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3043 /* undo-buffer is extended to the right */
3044 EDIT_MakeUndoFit(es, utl + e - s);
3045 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
3046 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3047 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3048 /* undo-buffer is extended to the left */
3049 EDIT_MakeUndoFit(es, utl + e - s);
3050 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3052 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3053 p[i] = (es->text + s)[i];
3054 es->undo_position = s;
3056 /* new undo-buffer */
3057 EDIT_MakeUndoFit(es, e - s);
3058 strncpyW(es->undo_text, es->text + s, e - s + 1);
3059 es->undo_text[e - s] = 0; /* ensure 0 termination */
3060 es->undo_position = s;
3062 /* any deletion makes the old insertion-undo invalid */
3063 es->undo_insert_count = 0;
3065 EDIT_EM_EmptyUndoBuffer(es);
3068 strcpyW(es->text + s, es->text + e);
3071 /* there is an insertion */
3073 if ((s == es->undo_position) ||
3074 ((es->undo_insert_count) &&
3075 (s == es->undo_position + es->undo_insert_count)))
3077 * insertion is new and at delete position or
3078 * an extension to either left or right
3080 es->undo_insert_count += strl;
3082 /* new insertion undo */
3083 es->undo_position = s;
3084 es->undo_insert_count = strl;
3085 /* new insertion makes old delete-buffer invalid */
3086 *es->undo_text = '\0';
3089 EDIT_EM_EmptyUndoBuffer(es);
3092 tl = strlenW(es->text);
3093 TRACE("inserting stuff (tl %d, strl %d, selstart %d ('%s'), text '%s')\n", tl, strl, s, debugstr_w(es->text + s), debugstr_w(es->text));
3094 for (p = es->text + tl ; p >= es->text + s ; p--)
3096 for (i = 0 , p = es->text + s ; i < strl ; i++)
3097 p[i] = lpsz_replace[i];
3098 if(es->style & ES_UPPERCASE)
3099 CharUpperBuffW(p, strl);
3100 else if(es->style & ES_LOWERCASE)
3101 CharLowerBuffW(p, strl);
3104 if (es->style & ES_MULTILINE)
3106 INT s = min(es->selection_start, es->selection_end);
3108 hrgn = CreateRectRgn(0, 0, 0, 0);
3109 EDIT_BuildLineDefs_ML(hwnd, es, s, s + strl,
3110 strl - abs(es->selection_end - es->selection_start), hrgn);
3113 EDIT_CalcLineWidth_SL(hwnd, es);
3115 EDIT_EM_SetSel(hwnd, es, s, s, FALSE);
3116 es->flags |= EF_MODIFIED;
3117 if (send_update) es->flags |= EF_UPDATE;
3118 EDIT_EM_ScrollCaret(hwnd, es);
3120 /* force scroll info update */
3121 EDIT_UpdateScrollInfo(hwnd, es);
3125 EDIT_UpdateTextRegion(hwnd, es, hrgn, TRUE);
3129 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3131 if(es->flags & EF_UPDATE)
3133 es->flags &= ~EF_UPDATE;
3134 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3139 /*********************************************************************
3144 static LRESULT EDIT_EM_Scroll(HWND hwnd, EDITSTATE *es, INT action)
3148 if (!(es->style & ES_MULTILINE))
3149 return (LRESULT)FALSE;
3159 if (es->y_offset < es->line_count - 1)
3164 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3167 if (es->y_offset < es->line_count - 1)
3168 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3171 return (LRESULT)FALSE;
3174 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3175 /* check if we are going to move too far */
3176 if(es->y_offset + dy > es->line_count - vlc)
3177 dy = es->line_count - vlc - es->y_offset;
3179 /* Notification is done in EDIT_EM_LineScroll */
3181 EDIT_EM_LineScroll(hwnd, es, 0, dy);
3183 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3187 /*********************************************************************
3192 static void EDIT_EM_ScrollCaret(HWND hwnd, EDITSTATE *es)
3194 if (es->style & ES_MULTILINE) {
3199 INT cw = es->char_width;
3204 l = EDIT_EM_LineFromChar(es, es->selection_end);
3205 li = EDIT_EM_LineIndex(es, l);
3206 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
3207 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3208 if (l >= es->y_offset + vlc)
3209 dy = l - vlc + 1 - es->y_offset;
3210 if (l < es->y_offset)
3211 dy = l - es->y_offset;
3212 ww = es->format_rect.right - es->format_rect.left;
3213 if (x < es->format_rect.left)
3214 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3215 if (x > es->format_rect.right)
3216 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3219 /* check if we are going to move too far */
3220 if(es->x_offset + dx + ww > es->text_width)
3221 dx = es->text_width - ww - es->x_offset;
3223 EDIT_EM_LineScroll_internal(hwnd, es, dx, dy);
3230 if (!(es->style & ES_AUTOHSCROLL))
3233 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3234 format_width = es->format_rect.right - es->format_rect.left;
3235 if (x < es->format_rect.left) {
3236 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3239 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3240 } while ((x < goal) && es->x_offset);
3241 /* FIXME: use ScrollWindow() somehow to improve performance */
3242 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3243 } else if (x > es->format_rect.right) {
3245 INT len = strlenW(es->text);
3246 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3249 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3250 x_last = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, len, FALSE));
3251 } while ((x > goal) && (x_last > es->format_rect.right));
3252 /* FIXME: use ScrollWindow() somehow to improve performance */
3253 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3257 if(es->flags & EF_FOCUSED)
3258 EDIT_SetCaretPos(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
3262 /*********************************************************************
3266 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3269 static void EDIT_EM_SetHandle(HWND hwnd, EDITSTATE *es, HLOCAL hloc)
3271 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3273 if (!(es->style & ES_MULTILINE))
3277 WARN("called with NULL handle\n");
3281 EDIT_UnlockBuffer(hwnd, es, TRUE);
3285 LOCAL_Free(hInstance, es->hloc16);
3286 es->hloc16 = (HLOCAL16)NULL;
3293 LocalFree(es->hloc32A);
3294 es->hloc32A = (HLOCAL)NULL;
3305 countA = LocalSize(hloc);
3306 textA = LocalLock(hloc);
3307 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3308 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3310 ERR("Could not allocate new unicode buffer\n");
3313 textW = LocalLock(hloc32W_new);
3314 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3315 LocalUnlock(hloc32W_new);
3319 LocalFree(es->hloc32W);
3321 es->hloc32W = hloc32W_new;
3325 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3327 EDIT_LockBuffer(hwnd, es);
3329 es->x_offset = es->y_offset = 0;
3330 es->selection_start = es->selection_end = 0;
3331 EDIT_EM_EmptyUndoBuffer(es);
3332 es->flags &= ~EF_MODIFIED;
3333 es->flags &= ~EF_UPDATE;
3334 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3335 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3336 EDIT_EM_ScrollCaret(hwnd, es);
3337 /* force scroll info update */
3338 EDIT_UpdateScrollInfo(hwnd, es);
3342 /*********************************************************************
3346 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3349 static void EDIT_EM_SetHandle16(HWND hwnd, EDITSTATE *es, HLOCAL16 hloc)
3351 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3357 if (!(es->style & ES_MULTILINE))
3361 WARN("called with NULL handle\n");
3365 EDIT_UnlockBuffer(hwnd, es, TRUE);
3369 LocalFree(es->hloc32A);
3370 es->hloc32A = (HLOCAL)NULL;
3373 countA = LOCAL_Size(hInstance, hloc);
3374 textA = LOCAL_Lock(hInstance, hloc);
3375 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3376 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3378 ERR("Could not allocate new unicode buffer\n");
3381 textW = LocalLock(hloc32W_new);
3382 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3383 LocalUnlock(hloc32W_new);
3384 LOCAL_Unlock(hInstance, hloc);
3387 LocalFree(es->hloc32W);
3389 es->hloc32W = hloc32W_new;
3392 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3394 EDIT_LockBuffer(hwnd, es);
3396 es->x_offset = es->y_offset = 0;
3397 es->selection_start = es->selection_end = 0;
3398 EDIT_EM_EmptyUndoBuffer(es);
3399 es->flags &= ~EF_MODIFIED;
3400 es->flags &= ~EF_UPDATE;
3401 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3402 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3403 EDIT_EM_ScrollCaret(hwnd, es);
3404 /* force scroll info update */
3405 EDIT_UpdateScrollInfo(hwnd, es);
3409 /*********************************************************************
3413 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3414 * However, the windows version is not complied to yet in all of edit.c
3416 * Additionally as the wrapper for RichEdit controls we need larger buffers
3417 * at present -1 will represent nolimit
3419 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3421 if (limit == 0xFFFFFFFF)
3422 es->buffer_limit = -1;
3423 else if (es->style & ES_MULTILINE) {
3425 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3427 es->buffer_limit = BUFLIMIT_MULTI;
3430 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3432 es->buffer_limit = BUFLIMIT_SINGLE;
3437 /*********************************************************************
3441 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3442 * action wParam despite what the docs say. EC_USEFONTINFO means one third
3443 * of the char's width, according to the new docs.
3446 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3447 INT left, INT right)
3449 if (action & EC_LEFTMARGIN) {
3450 if (left != EC_USEFONTINFO)
3451 es->left_margin = left;
3453 es->left_margin = es->char_width / 3;
3456 if (action & EC_RIGHTMARGIN) {
3457 if (right != EC_USEFONTINFO)
3458 es->right_margin = right;
3460 es->right_margin = es->char_width / 3;
3462 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3466 /*********************************************************************
3468 * EM_SETPASSWORDCHAR
3471 static void EDIT_EM_SetPasswordChar(HWND hwnd, EDITSTATE *es, WCHAR c)
3475 if (es->style & ES_MULTILINE)
3478 if (es->password_char == c)
3481 style = GetWindowLongA( hwnd, GWL_STYLE );
3482 es->password_char = c;
3484 SetWindowLongA( hwnd, GWL_STYLE, style | ES_PASSWORD );
3485 es->style |= ES_PASSWORD;
3487 SetWindowLongA( hwnd, GWL_STYLE, style & ~ES_PASSWORD );
3488 es->style &= ~ES_PASSWORD;
3490 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3494 /*********************************************************************
3498 * note: unlike the specs say: the order of start and end
3499 * _is_ preserved in Windows. (i.e. start can be > end)
3500 * In other words: this handler is OK
3503 static void EDIT_EM_SetSel(HWND hwnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3505 UINT old_start = es->selection_start;
3506 UINT old_end = es->selection_end;
3507 UINT len = strlenW(es->text);
3509 if (start == (UINT)-1) {
3510 start = es->selection_end;
3511 end = es->selection_end;
3513 start = min(start, len);
3514 end = min(end, len);
3516 es->selection_start = start;
3517 es->selection_end = end;
3519 es->flags |= EF_AFTER_WRAP;
3521 es->flags &= ~EF_AFTER_WRAP;
3522 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3523 ORDER_UINT(start, end);
3524 ORDER_UINT(end, old_end);
3525 ORDER_UINT(start, old_start);
3526 ORDER_UINT(old_start, old_end);
3527 if (end != old_start)
3531 * ORDER_UINT32(end, old_start);
3532 * EDIT_InvalidateText(hwnd, es, start, end);
3533 * EDIT_InvalidateText(hwnd, es, old_start, old_end);
3534 * in place of the following if statement.
3536 if (old_start > end )
3538 EDIT_InvalidateText(hwnd, es, start, end);
3539 EDIT_InvalidateText(hwnd, es, old_start, old_end);
3543 EDIT_InvalidateText(hwnd, es, start, old_start);
3544 EDIT_InvalidateText(hwnd, es, end, old_end);
3547 else EDIT_InvalidateText(hwnd, es, start, old_end);
3551 /*********************************************************************
3556 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3558 if (!(es->style & ES_MULTILINE))
3561 HeapFree(GetProcessHeap(), 0, es->tabs);
3562 es->tabs_count = count;
3566 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3567 memcpy(es->tabs, tabs, count * sizeof(INT));
3573 /*********************************************************************
3578 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3580 if (!(es->style & ES_MULTILINE))
3583 HeapFree(GetProcessHeap(), 0, es->tabs);
3584 es->tabs_count = count;
3589 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3590 for (i = 0 ; i < count ; i++)
3591 es->tabs[i] = *tabs++;
3597 /*********************************************************************
3599 * EM_SETWORDBREAKPROC
3602 static void EDIT_EM_SetWordBreakProc(HWND hwnd, EDITSTATE *es, LPARAM lParam)
3604 if (es->word_break_proc == (void *)lParam)
3607 es->word_break_proc = (void *)lParam;
3608 es->word_break_proc16 = NULL;
3610 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3611 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3612 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3617 /*********************************************************************
3619 * EM_SETWORDBREAKPROC16
3622 static void EDIT_EM_SetWordBreakProc16(HWND hwnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3624 if (es->word_break_proc16 == wbp)
3627 es->word_break_proc = NULL;
3628 es->word_break_proc16 = wbp;
3629 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3630 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3631 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3636 /*********************************************************************
3641 static BOOL EDIT_EM_Undo(HWND hwnd, EDITSTATE *es)
3646 /* Protect read-only edit control from modification */
3647 if(es->style & ES_READONLY)
3650 ulength = strlenW(es->undo_text);
3651 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3653 strcpyW(utext, es->undo_text);
3655 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3656 es->undo_insert_count, debugstr_w(utext));
3658 EDIT_EM_SetSel(hwnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3659 EDIT_EM_EmptyUndoBuffer(es);
3660 EDIT_EM_ReplaceSel(hwnd, es, TRUE, utext, FALSE);
3661 EDIT_EM_SetSel(hwnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3662 /* send the notification after the selection start and end are set */
3663 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3664 EDIT_EM_ScrollCaret(hwnd, es);
3665 HeapFree(GetProcessHeap(), 0, utext);
3667 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3668 es->undo_insert_count, debugstr_w(es->undo_text));
3673 /*********************************************************************
3678 static void EDIT_WM_Char(HWND hwnd, EDITSTATE *es, WCHAR c)
3682 /* Protect read-only edit control from modification */
3683 if(es->style & ES_READONLY)
3686 control = GetKeyState(VK_CONTROL) & 0x8000;
3690 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3691 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3694 if (es->style & ES_MULTILINE) {
3695 if (es->style & ES_READONLY) {
3696 EDIT_MoveHome(hwnd, es, FALSE);
3697 EDIT_MoveDown_ML(hwnd, es, FALSE);
3699 static const WCHAR cr_lfW[] = {'\r','\n',0};
3700 EDIT_EM_ReplaceSel(hwnd, es, TRUE, cr_lfW, TRUE);
3705 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3707 static const WCHAR tabW[] = {'\t',0};
3708 EDIT_EM_ReplaceSel(hwnd, es, TRUE, tabW, TRUE);
3712 if (!(es->style & ES_READONLY) && !control) {
3713 if (es->selection_start != es->selection_end)
3714 EDIT_WM_Clear(hwnd, es);
3716 /* delete character left of caret */
3717 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
3718 EDIT_MoveBackward(hwnd, es, TRUE);
3719 EDIT_WM_Clear(hwnd, es);
3724 SendMessageW(hwnd, WM_COPY, 0, 0);
3727 SendMessageW(hwnd, WM_PASTE, 0, 0);
3730 SendMessageW(hwnd, WM_CUT, 0, 0);
3734 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3738 EDIT_EM_ReplaceSel(hwnd, es, TRUE, str, TRUE);
3745 /*********************************************************************
3750 static void EDIT_WM_Command(HWND hwnd, EDITSTATE *es, INT code, INT id, HWND control)
3752 if (code || control)
3757 EDIT_EM_Undo(hwnd, es);
3760 EDIT_WM_Cut(hwnd, es);
3763 EDIT_WM_Copy(hwnd, es);
3766 EDIT_WM_Paste(hwnd, es);
3769 EDIT_WM_Clear(hwnd, es);
3772 EDIT_EM_SetSel(hwnd, es, 0, (UINT)-1, FALSE);
3773 EDIT_EM_ScrollCaret(hwnd, es);
3776 ERR("unknown menu item, please report\n");
3782 /*********************************************************************
3786 * Note: the resource files resource/sysres_??.rc cannot define a
3787 * single popup menu. Hence we use a (dummy) menubar
3788 * containing the single popup menu as its first item.
3790 * FIXME: the message identifiers have been chosen arbitrarily,
3791 * hence we use MF_BYPOSITION.
3792 * We might as well use the "real" values (anybody knows ?)
3793 * The menu definition is in resources/sysres_??.rc.
3794 * Once these are OK, we better use MF_BYCOMMAND here
3795 * (as we do in EDIT_WM_Command()).
3798 static void EDIT_WM_ContextMenu(HWND hwnd, EDITSTATE *es, INT x, INT y)
3800 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3801 HMENU popup = GetSubMenu(menu, 0);
3802 UINT start = es->selection_start;
3803 UINT end = es->selection_end;
3805 ORDER_UINT(start, end);
3808 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3810 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3812 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3814 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3816 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3818 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3820 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, hwnd, NULL);
3825 /*********************************************************************
3830 static void EDIT_WM_Copy(HWND hwnd, EDITSTATE *es)
3832 INT s = es->selection_start;
3833 INT e = es->selection_end;
3840 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3841 dst = GlobalLock(hdst);
3842 strncpyW(dst, es->text + s, e - s);
3843 dst[e - s] = 0; /* ensure 0 termination */
3844 TRACE("%s\n", debugstr_w(dst));
3846 OpenClipboard(hwnd);
3848 SetClipboardData(CF_UNICODETEXT, hdst);
3853 /*********************************************************************
3858 static LRESULT EDIT_WM_Create(HWND hwnd, EDITSTATE *es, LPCWSTR name)
3860 TRACE("%s\n", debugstr_w(name));
3862 * To initialize some final structure members, we call some helper
3863 * functions. However, since the EDITSTATE is not consistent (i.e.
3864 * not fully initialized), we should be very careful which
3865 * functions can be called, and in what order.
3867 EDIT_WM_SetFont(hwnd, es, 0, FALSE);
3868 EDIT_EM_EmptyUndoBuffer(es);
3870 if (name && *name) {
3871 EDIT_EM_ReplaceSel(hwnd, es, FALSE, name, FALSE);
3872 /* if we insert text to the editline, the text scrolls out
3873 * of the window, as the caret is placed after the insert
3874 * pos normally; thus we reset es->selection... to 0 and
3877 es->selection_start = es->selection_end = 0;
3878 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3879 * Messages are only to be sent when the USER does something to
3880 * change the contents. So I am removing this EN_CHANGE
3882 * EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3884 EDIT_EM_ScrollCaret(hwnd, es);
3886 /* force scroll info update */
3887 EDIT_UpdateScrollInfo(hwnd, es);
3892 /*********************************************************************
3897 static void EDIT_WM_Destroy(HWND hwnd, EDITSTATE *es)
3899 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3903 while (LocalUnlock(es->hloc32W)) ;
3904 LocalFree(es->hloc32W);
3907 while (LocalUnlock(es->hloc32A)) ;
3908 LocalFree(es->hloc32A);
3911 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3912 LOCAL_Free(hInstance, es->hloc16);
3915 pc = es->first_line_def;
3919 HeapFree(GetProcessHeap(), 0, pc);
3923 SetWindowLongA( hwnd, 0, 0 );
3924 HeapFree(GetProcessHeap(), 0, es);
3928 /*********************************************************************
3933 static LRESULT EDIT_WM_EraseBkGnd(HWND hwnd, EDITSTATE *es, HDC dc)
3938 if ( get_app_version() >= 0x40000 &&(
3939 !es->bEnableState || (es->style & ES_READONLY)))
3940 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(hwnd, dc);
3942 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(hwnd, dc);
3945 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3947 GetClientRect(hwnd, &rc);
3948 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3949 GetClipBox(dc, &rc);
3951 * FIXME: specs say that we should UnrealizeObject() the brush,
3952 * but the specs of UnrealizeObject() say that we shouldn't
3953 * unrealize a stock object. The default brush that
3954 * DefWndProc() returns is ... a stock object.
3956 FillRect(dc, &rc, brush);
3961 /*********************************************************************
3966 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3968 if(!count) return 0;
3972 LPWSTR textW = (LPWSTR)lParam;
3973 lstrcpynW(textW, es->text, count);
3974 return strlenW(textW);
3978 LPSTR textA = (LPSTR)lParam;
3979 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3980 textA[count - 1] = 0; /* ensure 0 termination */
3981 return strlen(textA);
3985 /*********************************************************************
3990 static LRESULT EDIT_WM_HScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos)
3995 if (!(es->style & ES_MULTILINE))
3998 if (!(es->style & ES_AUTOHSCROLL))
4002 fw = es->format_rect.right - es->format_rect.left;
4005 TRACE("SB_LINELEFT\n");
4007 dx = -es->char_width;
4010 TRACE("SB_LINERIGHT\n");
4011 if (es->x_offset < es->text_width)
4012 dx = es->char_width;
4015 TRACE("SB_PAGELEFT\n");
4017 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4020 TRACE("SB_PAGERIGHT\n");
4021 if (es->x_offset < es->text_width)
4022 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4030 TRACE("SB_RIGHT\n");
4031 if (es->x_offset < es->text_width)
4032 dx = es->text_width - es->x_offset;
4035 TRACE("SB_THUMBTRACK %d\n", pos);
4036 es->flags |= EF_HSCROLL_TRACK;
4037 if(es->style & WS_HSCROLL)
4038 dx = pos - es->x_offset;
4043 if(pos < 0 || pos > 100) return 0;
4044 /* Assume default scroll range 0-100 */
4045 fw = es->format_rect.right - es->format_rect.left;
4046 new_x = pos * (es->text_width - fw) / 100;
4047 dx = es->text_width ? (new_x - es->x_offset) : 0;
4050 case SB_THUMBPOSITION:
4051 TRACE("SB_THUMBPOSITION %d\n", pos);
4052 es->flags &= ~EF_HSCROLL_TRACK;
4053 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_HSCROLL)
4054 dx = pos - es->x_offset;
4059 if(pos < 0 || pos > 100) return 0;
4060 /* Assume default scroll range 0-100 */
4061 fw = es->format_rect.right - es->format_rect.left;
4062 new_x = pos * (es->text_width - fw) / 100;
4063 dx = es->text_width ? (new_x - es->x_offset) : 0;
4066 /* force scroll info update */
4067 EDIT_UpdateScrollInfo(hwnd, es);
4068 EDIT_NOTIFY_PARENT(hwnd, es, EN_HSCROLL, "EN_HSCROLL");
4072 TRACE("SB_ENDSCROLL\n");
4075 * FIXME : the next two are undocumented !
4076 * Are we doing the right thing ?
4077 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4078 * although it's also a regular control message.
4080 case EM_GETTHUMB: /* this one is used by NT notepad */
4084 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_HSCROLL)
4085 ret = GetScrollPos(hwnd, SB_HORZ);
4088 /* Assume default scroll range 0-100 */
4089 INT fw = es->format_rect.right - es->format_rect.left;
4090 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4092 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4095 case EM_LINESCROLL16:
4096 TRACE("EM_LINESCROLL16\n");
4101 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4107 INT fw = es->format_rect.right - es->format_rect.left;
4108 /* check if we are going to move too far */
4109 if(es->x_offset + dx + fw > es->text_width)
4110 dx = es->text_width - fw - es->x_offset;
4112 EDIT_EM_LineScroll_internal(hwnd, es, dx, 0);
4118 /*********************************************************************
4123 static BOOL EDIT_CheckCombo(HWND hwnd, EDITSTATE *es, UINT msg, INT key)
4125 HWND hLBox = es->hwndListBox;
4133 hCombo = GetParent(hwnd);
4137 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
4138 hwnd, (UINT16)msg, (UINT16)key);
4140 if (key == VK_UP || key == VK_DOWN)
4142 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4145 if (msg == WM_KEYDOWN || nEUI)
4146 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4152 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4154 /* make sure ComboLBox pops up */
4155 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4160 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4163 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4165 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4167 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4172 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4178 /*********************************************************************
4182 * Handling of special keys that don't produce a WM_CHAR
4183 * (i.e. non-printable keys) & Backspace & Delete
4186 static LRESULT EDIT_WM_KeyDown(HWND hwnd, EDITSTATE *es, INT key)
4191 if (GetKeyState(VK_MENU) & 0x8000)
4194 shift = GetKeyState(VK_SHIFT) & 0x8000;
4195 control = GetKeyState(VK_CONTROL) & 0x8000;
4200 if (EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key) || key == VK_F4)
4205 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4206 EDIT_MoveUp_ML(hwnd, es, shift);
4209 EDIT_MoveWordBackward(hwnd, es, shift);
4211 EDIT_MoveBackward(hwnd, es, shift);
4214 if (EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key))
4218 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4219 EDIT_MoveDown_ML(hwnd, es, shift);
4221 EDIT_MoveWordForward(hwnd, es, shift);
4223 EDIT_MoveForward(hwnd, es, shift);
4226 EDIT_MoveHome(hwnd, es, shift);
4229 EDIT_MoveEnd(hwnd, es, shift);
4232 if (es->style & ES_MULTILINE)
4233 EDIT_MovePageUp_ML(hwnd, es, shift);
4235 EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key);
4238 if (es->style & ES_MULTILINE)
4239 EDIT_MovePageDown_ML(hwnd, es, shift);
4241 EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key);
4244 if (!(es->style & ES_READONLY) && !(shift && control)) {
4245 if (es->selection_start != es->selection_end) {
4247 EDIT_WM_Cut(hwnd, es);
4249 EDIT_WM_Clear(hwnd, es);
4252 /* delete character left of caret */
4253 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4254 EDIT_MoveBackward(hwnd, es, TRUE);
4255 EDIT_WM_Clear(hwnd, es);
4256 } else if (control) {
4257 /* delete to end of line */
4258 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4259 EDIT_MoveEnd(hwnd, es, TRUE);
4260 EDIT_WM_Clear(hwnd, es);
4262 /* delete character right of caret */
4263 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4264 EDIT_MoveForward(hwnd, es, TRUE);
4265 EDIT_WM_Clear(hwnd, es);
4272 if (!(es->style & ES_READONLY))
4273 EDIT_WM_Paste(hwnd, es);
4275 EDIT_WM_Copy(hwnd, es);
4278 /* If the edit doesn't want the return send a message to the default object */
4279 if(!(es->style & ES_WANTRETURN))
4281 HWND hwndParent = GetParent(hwnd);
4282 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4283 if (HIWORD(dw) == DC_HASDEFID)
4285 SendMessageW( hwndParent, WM_COMMAND,
4286 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4287 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4296 /*********************************************************************
4301 static LRESULT EDIT_WM_KillFocus(HWND hwnd, EDITSTATE *es)
4303 es->flags &= ~EF_FOCUSED;
4305 if(!(es->style & ES_NOHIDESEL))
4306 EDIT_InvalidateText(hwnd, es, es->selection_start, es->selection_end);
4307 EDIT_NOTIFY_PARENT(hwnd, es, EN_KILLFOCUS, "EN_KILLFOCUS");
4312 /*********************************************************************
4316 * The caret position has been set on the WM_LBUTTONDOWN message
4319 static LRESULT EDIT_WM_LButtonDblClk(HWND hwnd, EDITSTATE *es)
4322 INT e = es->selection_end;
4327 if (!(es->flags & EF_FOCUSED))
4330 l = EDIT_EM_LineFromChar(es, e);
4331 li = EDIT_EM_LineIndex(es, l);
4332 ll = EDIT_EM_LineLength(es, e);
4333 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4334 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4335 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
4336 EDIT_EM_ScrollCaret(hwnd, es);
4341 /*********************************************************************
4346 static LRESULT EDIT_WM_LButtonDown(HWND hwnd, EDITSTATE *es, DWORD keys, INT x, INT y)
4351 if (!(es->flags & EF_FOCUSED))
4354 es->bCaptureState = TRUE;
4356 EDIT_ConfinePoint(es, &x, &y);
4357 e = EDIT_CharFromPos(hwnd, es, x, y, &after_wrap);
4358 EDIT_EM_SetSel(hwnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4359 EDIT_EM_ScrollCaret(hwnd, es);
4360 es->region_posx = es->region_posy = 0;
4361 SetTimer(hwnd, 0, 100, NULL);
4366 /*********************************************************************
4371 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es)
4373 if (es->bCaptureState && GetCapture() == hwndSelf) {
4374 KillTimer(hwndSelf, 0);
4377 es->bCaptureState = FALSE;
4382 /*********************************************************************
4387 static LRESULT EDIT_WM_MButtonDown(HWND hwnd)
4389 SendMessageW(hwnd,WM_PASTE,0,0);
4394 /*********************************************************************
4399 static LRESULT EDIT_WM_MouseMove(HWND hwnd, EDITSTATE *es, INT x, INT y)
4405 if (GetCapture() != hwnd)
4409 * FIXME: gotta do some scrolling if outside client
4410 * area. Maybe reset the timer ?
4413 EDIT_ConfinePoint(es, &x, &y);
4414 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4415 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4416 e = EDIT_CharFromPos(hwnd, es, x, y, &after_wrap);
4417 EDIT_EM_SetSel(hwnd, es, es->selection_start, e, after_wrap);
4418 EDIT_SetCaretPos(hwnd,es,es->selection_end,es->flags & EF_AFTER_WRAP);
4423 /*********************************************************************
4427 * See also EDIT_WM_StyleChanged
4429 static LRESULT EDIT_WM_NCCreate(HWND hwnd, DWORD style, HWND hwndParent, BOOL unicode)
4434 TRACE("Creating %s edit control, style = %08lx\n",
4435 unicode ? "Unicode" : "ANSI", style);
4437 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4439 SetWindowLongA( hwnd, 0, (LONG)es );
4442 * Note: since the EDITSTATE has not been fully initialized yet,
4443 * we can't use any API calls that may send
4444 * WM_XXX messages before WM_NCCREATE is completed.
4447 es->is_unicode = unicode;
4450 es->bEnableState = !(style & WS_DISABLED);
4452 /* Save parent, which will be notified by EN_* messages */
4453 es->hwndParent = hwndParent;
4455 if (es->style & ES_COMBO)
4456 es->hwndListBox = GetDlgItem(hwndParent, ID_CB_LISTBOX);
4458 /* Number overrides lowercase overrides uppercase (at least it
4459 * does in Win95). However I'll bet that ES_NUMBER would be
4460 * invalid under Win 3.1.
4462 if (es->style & ES_NUMBER) {
4463 ; /* do not override the ES_NUMBER */
4464 } else if (es->style & ES_LOWERCASE) {
4465 es->style &= ~ES_UPPERCASE;
4467 if (es->style & ES_MULTILINE) {
4468 es->buffer_limit = BUFLIMIT_MULTI;
4469 if (es->style & WS_VSCROLL)
4470 es->style |= ES_AUTOVSCROLL;
4471 if (es->style & WS_HSCROLL)
4472 es->style |= ES_AUTOHSCROLL;
4473 es->style &= ~ES_PASSWORD;
4474 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4475 /* Confirmed - RIGHT overrides CENTER */
4476 if (es->style & ES_RIGHT)
4477 es->style &= ~ES_CENTER;
4478 es->style &= ~WS_HSCROLL;
4479 es->style &= ~ES_AUTOHSCROLL;
4482 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4483 es->style |= ES_AUTOVSCROLL;
4485 es->buffer_limit = BUFLIMIT_SINGLE;
4486 if (WIN31_LOOK == TWEAK_WineLook ||
4487 WIN95_LOOK == TWEAK_WineLook) {
4488 es->style &= ~ES_CENTER;
4489 es->style &= ~ES_RIGHT;
4491 if (es->style & ES_RIGHT)
4492 es->style &= ~ES_CENTER;
4494 es->style &= ~WS_HSCROLL;
4495 es->style &= ~WS_VSCROLL;
4496 es->style &= ~ES_AUTOVSCROLL;
4497 es->style &= ~ES_WANTRETURN;
4498 if (es->style & ES_PASSWORD)
4499 es->password_char = '*';
4501 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4502 es->style |= ES_AUTOHSCROLL;
4505 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4506 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4508 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4510 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4512 es->undo_buffer_size = es->buffer_size;
4514 if (es->style & ES_MULTILINE)
4515 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4520 * In Win95 look and feel, the WS_BORDER style is replaced by the
4521 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4522 * control a non client area. Not always. This coordinates in some
4523 * way with the window creation code in dialog.c When making
4524 * modifications please ensure that the code still works for edit
4525 * controls created directly with style 0x50800000, exStyle 0 (
4526 * which should have a single pixel border)
4528 if (TWEAK_WineLook != WIN31_LOOK)
4530 es->style &= ~WS_BORDER;
4534 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4535 SetWindowLongA( hwnd, GWL_STYLE,
4536 GetWindowLongA( hwnd, GWL_STYLE ) & ~WS_BORDER );
4542 /*********************************************************************
4547 static void EDIT_WM_Paint(HWND hwnd, EDITSTATE *es, WPARAM wParam)
4556 BOOL rev = es->bEnableState &&
4557 ((es->flags & EF_FOCUSED) ||
4558 (es->style & ES_NOHIDESEL));
4560 dc = BeginPaint(hwnd, &ps);
4563 if(es->style & WS_BORDER) {
4564 GetClientRect(hwnd, &rc);
4565 if(es->style & ES_MULTILINE) {
4566 if(es->style & WS_HSCROLL) rc.bottom++;
4567 if(es->style & WS_VSCROLL) rc.right++;
4569 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4571 IntersectClipRect(dc, es->format_rect.left,
4572 es->format_rect.top,
4573 es->format_rect.right,
4574 es->format_rect.bottom);
4575 if (es->style & ES_MULTILINE) {
4576 GetClientRect(hwnd, &rc);
4577 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4580 old_font = SelectObject(dc, es->font);
4581 if ( get_app_version() >= 0x40000 &&(
4582 !es->bEnableState || (es->style & ES_READONLY)))
4583 EDIT_SEND_CTLCOLORSTATIC(hwnd, dc);
4585 EDIT_SEND_CTLCOLOR(hwnd, dc);
4587 if (!es->bEnableState)
4588 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4589 GetClipBox(dc, &rcRgn);
4590 if (es->style & ES_MULTILINE) {
4591 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4592 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4593 EDIT_GetLineRect(hwnd, es, i, 0, -1, &rcLine);
4594 if (IntersectRect(&rc, &rcRgn, &rcLine))
4595 EDIT_PaintLine(hwnd, es, dc, i, rev);
4598 EDIT_GetLineRect(hwnd, es, 0, 0, -1, &rcLine);
4599 if (IntersectRect(&rc, &rcRgn, &rcLine))
4600 EDIT_PaintLine(hwnd, es, dc, 0, rev);
4603 SelectObject(dc, old_font);
4606 EndPaint(hwnd, &ps);
4610 /*********************************************************************
4615 static void EDIT_WM_Paste(HWND hwnd, EDITSTATE *es)
4620 /* Protect read-only edit control from modification */
4621 if(es->style & ES_READONLY)
4624 OpenClipboard(hwnd);
4625 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4626 src = (LPWSTR)GlobalLock(hsrc);
4627 EDIT_EM_ReplaceSel(hwnd, es, TRUE, src, TRUE);
4634 /*********************************************************************
4639 static void EDIT_WM_SetFocus(HWND hwnd, EDITSTATE *es)
4641 es->flags |= EF_FOCUSED;
4642 CreateCaret(hwnd, 0, 2, es->line_height);
4643 EDIT_SetCaretPos(hwnd, es, es->selection_end,
4644 es->flags & EF_AFTER_WRAP);
4645 if(!(es->style & ES_NOHIDESEL))
4646 EDIT_InvalidateText(hwnd, es, es->selection_start, es->selection_end);
4648 EDIT_NOTIFY_PARENT(hwnd, es, EN_SETFOCUS, "EN_SETFOCUS");
4652 /*********************************************************************
4656 * With Win95 look the margins are set to default font value unless
4657 * the system font (font == 0) is being set, in which case they are left
4661 static void EDIT_WM_SetFont(HWND hwnd, EDITSTATE *es, HFONT font, BOOL redraw)
4671 old_font = SelectObject(dc, font);
4672 GetTextMetricsW(dc, &tm);
4673 es->line_height = tm.tmHeight;
4674 es->char_width = tm.tmAveCharWidth;
4676 SelectObject(dc, old_font);
4677 ReleaseDC(hwnd, dc);
4678 if (font && (TWEAK_WineLook > WIN31_LOOK))
4679 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4680 EC_USEFONTINFO, EC_USEFONTINFO);
4682 /* Force the recalculation of the format rect for each font change */
4683 GetClientRect(hwnd, &r);
4684 EDIT_SetRectNP(hwnd, es, &r);
4686 if (es->style & ES_MULTILINE)
4687 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
4689 EDIT_CalcLineWidth_SL(hwnd, es);
4692 EDIT_UpdateText(hwnd, es, NULL, TRUE);
4693 if (es->flags & EF_FOCUSED) {
4695 CreateCaret(hwnd, 0, 2, es->line_height);
4696 EDIT_SetCaretPos(hwnd, es, es->selection_end,
4697 es->flags & EF_AFTER_WRAP);
4703 /*********************************************************************
4708 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4709 * The modified flag is reset. No notifications are sent.
4711 * For single-line controls, reception of WM_SETTEXT triggers:
4712 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4715 static void EDIT_WM_SetText(HWND hwnd, EDITSTATE *es, LPARAM lParam, BOOL unicode)
4720 text = (LPWSTR)lParam;
4723 LPCSTR textA = (LPCSTR)lParam;
4724 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4725 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4726 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4729 EDIT_EM_SetSel(hwnd, es, 0, (UINT)-1, FALSE);
4731 TRACE("%s\n", debugstr_w(text));
4732 EDIT_EM_ReplaceSel(hwnd, es, FALSE, text, FALSE);
4734 HeapFree(GetProcessHeap(), 0, text);
4736 static const WCHAR empty_stringW[] = {0};
4738 EDIT_EM_ReplaceSel(hwnd, es, FALSE, empty_stringW, FALSE);
4741 es->flags &= ~EF_MODIFIED;
4742 EDIT_EM_SetSel(hwnd, es, 0, 0, FALSE);
4743 /* Send the notification after the selection start and end have been set
4744 * edit control doesn't send notification on WM_SETTEXT
4745 * if it is multiline, or it is part of combobox
4747 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4749 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
4750 EDIT_NOTIFY_PARENT(hwnd, es, EN_UPDATE, "EN_UPDATE");
4752 EDIT_EM_ScrollCaret(hwnd, es);
4756 /*********************************************************************
4761 static void EDIT_WM_Size(HWND hwnd, EDITSTATE *es, UINT action, INT width, INT height)
4763 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4765 TRACE("width = %d, height = %d\n", width, height);
4766 SetRect(&rc, 0, 0, width, height);
4767 EDIT_SetRectNP(hwnd, es, &rc);
4768 EDIT_UpdateText(hwnd, es, NULL, TRUE);
4773 /*********************************************************************
4777 * This message is sent by SetWindowLong on having changed either the Style
4778 * or the extended style.
4780 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4782 * See also EDIT_WM_NCCreate
4784 * It appears that the Windows version of the edit control allows the style
4785 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4786 * style variable which will generally be different. In this function we
4787 * update the internal style based on what changed in the externally visible
4790 * Much of this content as based upon the MSDN, especially:
4791 * Platform SDK Documentation -> User Interface Services ->
4792 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4793 * Edit Control Styles
4795 static LRESULT EDIT_WM_StyleChanged (HWND hwnd,
4798 const STYLESTRUCT *style)
4800 if (GWL_STYLE == which) {
4801 DWORD style_change_mask;
4803 /* Only a subset of changes can be applied after the control
4806 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4808 if (es->style & ES_MULTILINE)
4809 style_change_mask |= ES_WANTRETURN;
4811 new_style = style->styleNew & style_change_mask;
4813 /* Number overrides lowercase overrides uppercase (at least it
4814 * does in Win95). However I'll bet that ES_NUMBER would be
4815 * invalid under Win 3.1.
4817 if (new_style & ES_NUMBER) {
4818 ; /* do not override the ES_NUMBER */
4819 } else if (new_style & ES_LOWERCASE) {
4820 new_style &= ~ES_UPPERCASE;
4823 es->style = (es->style & ~style_change_mask) | new_style;
4824 } else if (GWL_EXSTYLE == which) {
4825 ; /* FIXME - what is needed here */
4827 WARN ("Invalid style change %d\n",which);
4833 /*********************************************************************
4838 static LRESULT EDIT_WM_SysKeyDown(HWND hwnd, EDITSTATE *es, INT key, DWORD key_data)
4840 if ((key == VK_BACK) && (key_data & 0x2000)) {
4841 if (EDIT_EM_CanUndo(es))
4842 EDIT_EM_Undo(hwnd, es);
4844 } else if (key == VK_UP || key == VK_DOWN) {
4845 if (EDIT_CheckCombo(hwnd, es, WM_SYSKEYDOWN, key))
4848 return DefWindowProcW(hwnd, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4852 /*********************************************************************
4857 static void EDIT_WM_Timer(HWND hwnd, EDITSTATE *es)
4859 if (es->region_posx < 0) {
4860 EDIT_MoveBackward(hwnd, es, TRUE);
4861 } else if (es->region_posx > 0) {
4862 EDIT_MoveForward(hwnd, es, TRUE);
4865 * FIXME: gotta do some vertical scrolling here, like
4866 * EDIT_EM_LineScroll(hwnd, 0, 1);
4870 /*********************************************************************
4875 static LRESULT EDIT_WM_VScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos)
4879 if (!(es->style & ES_MULTILINE))
4882 if (!(es->style & ES_AUTOVSCROLL))
4891 TRACE("action %d\n", action);
4892 EDIT_EM_Scroll(hwnd, es, action);
4899 TRACE("SB_BOTTOM\n");
4900 dy = es->line_count - 1 - es->y_offset;
4903 TRACE("SB_THUMBTRACK %d\n", pos);
4904 es->flags |= EF_VSCROLL_TRACK;
4905 if(es->style & WS_VSCROLL)
4906 dy = pos - es->y_offset;
4909 /* Assume default scroll range 0-100 */
4912 if(pos < 0 || pos > 100) return 0;
4913 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4914 new_y = pos * (es->line_count - vlc) / 100;
4915 dy = es->line_count ? (new_y - es->y_offset) : 0;
4916 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4917 es->line_count, es->y_offset, pos, dy);
4920 case SB_THUMBPOSITION:
4921 TRACE("SB_THUMBPOSITION %d\n", pos);
4922 es->flags &= ~EF_VSCROLL_TRACK;
4923 if(es->style & WS_VSCROLL)
4924 dy = pos - es->y_offset;
4927 /* Assume default scroll range 0-100 */
4930 if(pos < 0 || pos > 100) return 0;
4931 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4932 new_y = pos * (es->line_count - vlc) / 100;
4933 dy = es->line_count ? (new_y - es->y_offset) : 0;
4934 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4935 es->line_count, es->y_offset, pos, dy);
4939 /* force scroll info update */
4940 EDIT_UpdateScrollInfo(hwnd, es);
4941 EDIT_NOTIFY_PARENT(hwnd, es, EN_VSCROLL, "EN_VSCROLL");
4945 TRACE("SB_ENDSCROLL\n");
4948 * FIXME : the next two are undocumented !
4949 * Are we doing the right thing ?
4950 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4951 * although it's also a regular control message.
4953 case EM_GETTHUMB: /* this one is used by NT notepad */
4957 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_VSCROLL)
4958 ret = GetScrollPos(hwnd, SB_VERT);
4961 /* Assume default scroll range 0-100 */
4962 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4963 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4965 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4968 case EM_LINESCROLL16:
4969 TRACE("EM_LINESCROLL16 %d\n", pos);
4974 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4979 EDIT_EM_LineScroll(hwnd, es, 0, dy);
4983 /*********************************************************************
4988 static void EDIT_UpdateTextRegion(HWND hwnd, EDITSTATE *es, HRGN hrgn, BOOL bErase)
4990 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(hwnd, es, EN_UPDATE, "EN_UPDATE");
4991 InvalidateRgn(hwnd, hrgn, bErase);
4995 /*********************************************************************
5000 static void EDIT_UpdateText(HWND hwnd, EDITSTATE *es, LPRECT rc, BOOL bErase)
5002 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(hwnd, es, EN_UPDATE, "EN_UPDATE");
5003 InvalidateRect(hwnd, rc, bErase);