Add support for Darwin's ptrace.
[wine/testsucceed.git] / controls / edit.c
blobfee3ce6c84cab8c2612dc54fcf4607ad95f58517
1 /*
2 * Edit control
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
23 * TODO:
24 * - ES_CENTER
25 * - ES_RIGHT
26 * - ES_NUMBER (new since win95)
27 * - ES_OEMCONVERT
28 * -!ES_AUTOVSCROLL (every multi line control *is* auto vscroll)
29 * -!ES_AUTOHSCROLL (every single line control *is* auto hscroll)
31 * When there is no autoscrolling, the control should first check whether
32 * the new text would fit. If not, an EN_MAXTEXT should be sent.
33 * However, currently this would require the actual change to be made,
34 * then call EDIT_BuildLineDefs() and then find out that the new text doesn't
35 * fit. After all this, things should be put back in the state before the
36 * changes. Note that for multi line controls !ES_AUTOHSCROLL works : wordwrap.
40 #include "config.h"
42 #include <string.h>
43 #include <stdlib.h>
45 #include "winbase.h"
46 #include "winnt.h"
47 #include "wownt32.h"
48 #include "win.h"
49 #include "wine/winbase16.h"
50 #include "wine/winuser16.h"
51 #include "wine/unicode.h"
52 #include "controls.h"
53 #include "local.h"
54 #include "user.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(edit);
58 WINE_DECLARE_DEBUG_CHANNEL(combo);
59 WINE_DECLARE_DEBUG_CHANNEL(relay);
61 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
62 FIXME: BTW, new specs say 65535 (do you dare ???) */
63 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
64 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
65 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
66 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
69 * extra flags for EDITSTATE.flags field
71 #define EF_MODIFIED 0x0001 /* text has been modified */
72 #define EF_FOCUSED 0x0002 /* we have input focus */
73 #define EF_UPDATE 0x0004 /* notify parent of changed state */
74 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
75 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
76 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
77 wrapped line, instead of in front of the next character */
78 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
80 typedef enum
82 END_0 = 0, /* line ends with terminating '\0' character */
83 END_WRAP, /* line is wrapped */
84 END_HARD, /* line ends with a hard return '\r\n' */
85 END_SOFT, /* line ends with a soft return '\r\r\n' */
86 END_RICH /* line ends with a single '\n' */
87 } LINE_END;
89 typedef struct tagLINEDEF {
90 INT length; /* bruto length of a line in bytes */
91 INT net_length; /* netto length of a line in visible characters */
92 LINE_END ending;
93 INT width; /* width of the line in pixels */
94 INT index; /* line index into the buffer */
95 struct tagLINEDEF *next;
96 } LINEDEF;
98 typedef struct
100 BOOL is_unicode; /* how the control was created */
101 LPWSTR text; /* the actual contents of the control */
102 UINT buffer_size; /* the size of the buffer in characters */
103 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
104 HFONT font; /* NULL means standard system font */
105 INT x_offset; /* scroll offset for multi lines this is in pixels
106 for single lines it's in characters */
107 INT line_height; /* height of a screen line in pixels */
108 INT char_width; /* average character width in pixels */
109 DWORD style; /* sane version of wnd->dwStyle */
110 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
111 INT undo_insert_count; /* number of characters inserted in sequence */
112 UINT undo_position; /* character index of the insertion and deletion */
113 LPWSTR undo_text; /* deleted text */
114 UINT undo_buffer_size; /* size of the deleted text buffer */
115 INT selection_start; /* == selection_end if no selection */
116 INT selection_end; /* == current caret position */
117 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
118 INT left_margin; /* in pixels */
119 INT right_margin; /* in pixels */
120 RECT format_rect;
121 INT text_width; /* width of the widest line in pixels for multi line controls
122 and just line width for single line controls */
123 INT region_posx; /* Position of cursor relative to region: */
124 INT region_posy; /* -1: to left, 0: within, 1: to right */
125 EDITWORDBREAKPROC16 word_break_proc16;
126 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
127 INT line_count; /* number of lines */
128 INT y_offset; /* scroll offset in number of lines */
129 BOOL bCaptureState; /* flag indicating whether mouse was captured */
130 BOOL bEnableState; /* flag keeping the enable state */
131 HWND hwndSelf; /* the our window handle */
132 HWND hwndParent; /* Handle of parent for sending EN_* messages.
133 Even if parent will change, EN_* messages
134 should be sent to the first parent. */
135 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
137 * only for multi line controls
139 INT lock_count; /* amount of re-entries in the EditWndProc */
140 INT tabs_count;
141 LPINT tabs;
142 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
143 HLOCAL hloc32W; /* our unicode local memory block */
144 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
145 or EM_SETHANDLE16 */
146 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
147 or EM_SETHANDLE */
148 } EDITSTATE;
151 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
152 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
154 /* used for disabled or read-only edit control */
155 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
156 do \
157 { /* Notify parent which has created this edit control */ \
158 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
159 SendMessageW(es->hwndParent, WM_COMMAND, \
160 MAKEWPARAM(GetWindowLongW((es->hwndSelf),GWL_ID), wNotifyCode), \
161 (LPARAM)(es->hwndSelf)); \
162 } while(0)
164 /*********************************************************************
166 * Declarations
171 * These functions have trivial implementations
172 * We still like to call them internally
173 * "static inline" makes them more like macro's
175 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
176 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
177 static inline void EDIT_WM_Clear(EDITSTATE *es);
178 static inline void EDIT_WM_Cut(EDITSTATE *es);
181 * Helper functions only valid for one type of control
183 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
184 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
185 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
186 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
187 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
188 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
189 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
191 * Helper functions valid for both single line _and_ multi line controls
193 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
194 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
195 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
196 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
197 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
198 static void EDIT_LockBuffer(EDITSTATE *es);
199 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size, BOOL honor_limit);
200 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
201 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
202 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
203 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
204 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
205 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
206 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
207 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
208 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
209 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
210 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
211 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
212 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
213 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
215 * EM_XXX message handlers
217 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
218 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
219 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
220 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
221 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
222 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
223 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
224 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
225 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
226 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
227 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
228 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
229 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
230 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
231 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
232 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
233 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
234 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
235 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
236 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
237 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
238 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
239 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
240 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
241 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam);
242 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
243 static BOOL EDIT_EM_Undo(EDITSTATE *es);
245 * WM_XXX message handlers
247 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
248 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
249 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
250 static void EDIT_WM_Copy(EDITSTATE *es);
251 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
252 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
253 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
254 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
255 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
256 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
257 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
258 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
259 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
260 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
261 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
262 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
263 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
264 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam);
265 static void EDIT_WM_Paste(EDITSTATE *es);
266 static void EDIT_WM_SetFocus(EDITSTATE *es);
267 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
268 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode);
269 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
270 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
271 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
272 static void EDIT_WM_Timer(EDITSTATE *es);
273 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
274 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
275 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
277 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
278 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
280 /*********************************************************************
281 * edit class descriptor
283 const struct builtin_class_descr EDIT_builtin_class =
285 "Edit", /* name */
286 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
287 EditWndProcA, /* procA */
288 EditWndProcW, /* procW */
289 sizeof(EDITSTATE *), /* extra */
290 IDC_IBEAMA, /* cursor */
291 0 /* brush */
295 /*********************************************************************
297 * EM_CANUNDO
300 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
302 return (es->undo_insert_count || strlenW(es->undo_text));
306 /*********************************************************************
308 * EM_EMPTYUNDOBUFFER
311 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
313 es->undo_insert_count = 0;
314 *es->undo_text = '\0';
318 /*********************************************************************
320 * WM_CLEAR
323 static inline void EDIT_WM_Clear(EDITSTATE *es)
325 static const WCHAR empty_stringW[] = {0};
327 /* Protect read-only edit control from modification */
328 if(es->style & ES_READONLY)
329 return;
331 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
335 /*********************************************************************
337 * WM_CUT
340 static inline void EDIT_WM_Cut(EDITSTATE *es)
342 EDIT_WM_Copy(es);
343 EDIT_WM_Clear(es);
347 /**********************************************************************
348 * get_app_version
350 * Returns the window version in case Wine emulates a later version
351 * of windows then the application expects.
353 * In a number of cases when windows runs an application that was
354 * designed for an earlier windows version, windows reverts
355 * to "old" behaviour of that earlier version.
357 * An example is a disabled edit control that needs to be painted.
358 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
359 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
360 * applications with an expected version 0f 4.0 or higher.
363 static DWORD get_app_version(void)
365 static DWORD version;
366 if (!version)
368 DWORD dwEmulatedVersion;
369 OSVERSIONINFOW info;
370 DWORD dwProcVersion = GetProcessVersion(0);
372 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
373 GetVersionExW( &info );
374 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
375 /* FIXME: this may not be 100% correct; see discussion on the
376 * wine developer list in Nov 1999 */
377 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
379 return version;
383 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
385 UINT msg;
387 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
388 msg = WM_CTLCOLORSTATIC;
389 else
390 msg = WM_CTLCOLOREDIT;
392 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
393 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
396 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
398 if(unicode)
399 return DefWindowProcW(hwnd, msg, wParam, lParam);
400 else
401 return DefWindowProcA(hwnd, msg, wParam, lParam);
404 /*********************************************************************
406 * EditWndProc_common
408 * The messages are in the order of the actual integer values
409 * (which can be found in include/windows.h)
410 * Wherever possible the 16 bit versions are converted to
411 * the 32 bit ones, so that we can 'fall through' to the
412 * helper functions. These are mostly 32 bit (with a few
413 * exceptions, clearly indicated by a '16' extension to their
414 * names).
417 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
418 WPARAM wParam, LPARAM lParam, BOOL unicode )
420 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
421 LRESULT result = 0;
423 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, msg, wParam, lParam);
425 if (!es && msg != WM_NCCREATE)
426 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
427 else if (msg == WM_NCCREATE)
428 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
429 else if (msg == WM_DESTROY)
430 return EDIT_WM_Destroy(es);
433 if (es) EDIT_LockBuffer(es);
435 switch (msg) {
436 case EM_GETSEL16:
437 wParam = 0;
438 lParam = 0;
439 /* fall through */
440 case EM_GETSEL:
441 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
442 break;
444 case EM_SETSEL16:
445 if (SLOWORD(lParam) == -1)
446 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
447 else
448 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
449 if (!wParam)
450 EDIT_EM_ScrollCaret(es);
451 result = 1;
452 break;
453 case EM_SETSEL:
454 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
455 EDIT_EM_ScrollCaret(es);
456 result = 1;
457 break;
459 case EM_GETRECT16:
460 if (lParam)
461 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
462 break;
463 case EM_GETRECT:
464 if (lParam)
465 CopyRect((LPRECT)lParam, &es->format_rect);
466 break;
468 case EM_SETRECT16:
469 if ((es->style & ES_MULTILINE) && lParam) {
470 RECT rc;
471 CONV_RECT16TO32(MapSL(lParam), &rc);
472 EDIT_SetRectNP(es, &rc);
473 EDIT_UpdateText(es, NULL, TRUE);
475 break;
476 case EM_SETRECT:
477 if ((es->style & ES_MULTILINE) && lParam) {
478 EDIT_SetRectNP(es, (LPRECT)lParam);
479 EDIT_UpdateText(es, NULL, TRUE);
481 break;
483 case EM_SETRECTNP16:
484 if ((es->style & ES_MULTILINE) && lParam) {
485 RECT rc;
486 CONV_RECT16TO32(MapSL(lParam), &rc);
487 EDIT_SetRectNP(es, &rc);
489 break;
490 case EM_SETRECTNP:
491 if ((es->style & ES_MULTILINE) && lParam)
492 EDIT_SetRectNP(es, (LPRECT)lParam);
493 break;
495 case EM_SCROLL16:
496 case EM_SCROLL:
497 result = EDIT_EM_Scroll(es, (INT)wParam);
498 break;
500 case EM_LINESCROLL16:
501 wParam = (WPARAM)(INT)SHIWORD(lParam);
502 lParam = (LPARAM)(INT)SLOWORD(lParam);
503 /* fall through */
504 case EM_LINESCROLL:
505 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
506 break;
508 case EM_SCROLLCARET16:
509 case EM_SCROLLCARET:
510 EDIT_EM_ScrollCaret(es);
511 result = 1;
512 break;
514 case EM_GETMODIFY16:
515 case EM_GETMODIFY:
516 result = ((es->flags & EF_MODIFIED) != 0);
517 break;
519 case EM_SETMODIFY16:
520 case EM_SETMODIFY:
521 if (wParam)
522 es->flags |= EF_MODIFIED;
523 else
524 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
525 break;
527 case EM_GETLINECOUNT16:
528 case EM_GETLINECOUNT:
529 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
530 break;
532 case EM_LINEINDEX16:
533 if ((INT16)wParam == -1)
534 wParam = (WPARAM)-1;
535 /* fall through */
536 case EM_LINEINDEX:
537 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
538 break;
540 case EM_SETHANDLE16:
541 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
542 break;
543 case EM_SETHANDLE:
544 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
545 break;
547 case EM_GETHANDLE16:
548 result = (LRESULT)EDIT_EM_GetHandle16(es);
549 break;
550 case EM_GETHANDLE:
551 result = (LRESULT)EDIT_EM_GetHandle(es);
552 break;
554 case EM_GETTHUMB16:
555 case EM_GETTHUMB:
556 result = EDIT_EM_GetThumb(es);
557 break;
559 /* these messages missing from specs */
560 case WM_USER+15:
561 case 0x00bf:
562 case WM_USER+16:
563 case 0x00c0:
564 case WM_USER+19:
565 case 0x00c3:
566 case WM_USER+26:
567 case 0x00ca:
568 FIXME("undocumented message 0x%x, please report\n", msg);
569 result = DefWindowProcW(hwnd, msg, wParam, lParam);
570 break;
572 case EM_LINELENGTH16:
573 case EM_LINELENGTH:
574 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
575 break;
577 case EM_REPLACESEL16:
578 lParam = (LPARAM)MapSL(lParam);
579 unicode = FALSE; /* 16-bit message is always ascii */
580 /* fall through */
581 case EM_REPLACESEL:
583 LPWSTR textW;
585 if(unicode)
586 textW = (LPWSTR)lParam;
587 else
589 LPSTR textA = (LPSTR)lParam;
590 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
591 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
592 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
595 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
596 result = 1;
598 if(!unicode)
599 HeapFree(GetProcessHeap(), 0, textW);
600 break;
603 case EM_GETLINE16:
604 lParam = (LPARAM)MapSL(lParam);
605 unicode = FALSE; /* 16-bit message is always ascii */
606 /* fall through */
607 case EM_GETLINE:
608 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
609 break;
611 case EM_LIMITTEXT16:
612 case EM_SETLIMITTEXT:
613 EDIT_EM_SetLimitText(es, (INT)wParam);
614 break;
616 case EM_CANUNDO16:
617 case EM_CANUNDO:
618 result = (LRESULT)EDIT_EM_CanUndo(es);
619 break;
621 case EM_UNDO16:
622 case EM_UNDO:
623 case WM_UNDO:
624 result = (LRESULT)EDIT_EM_Undo(es);
625 break;
627 case EM_FMTLINES16:
628 case EM_FMTLINES:
629 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
630 break;
632 case EM_LINEFROMCHAR16:
633 case EM_LINEFROMCHAR:
634 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
635 break;
637 case EM_SETTABSTOPS16:
638 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
639 break;
640 case EM_SETTABSTOPS:
641 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
642 break;
644 case EM_SETPASSWORDCHAR16:
645 unicode = FALSE; /* 16-bit message is always ascii */
646 /* fall through */
647 case EM_SETPASSWORDCHAR:
649 WCHAR charW = 0;
651 if(unicode)
652 charW = (WCHAR)wParam;
653 else
655 CHAR charA = wParam;
656 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
659 EDIT_EM_SetPasswordChar(es, charW);
660 break;
663 case EM_EMPTYUNDOBUFFER16:
664 case EM_EMPTYUNDOBUFFER:
665 EDIT_EM_EmptyUndoBuffer(es);
666 break;
668 case EM_GETFIRSTVISIBLELINE16:
669 result = es->y_offset;
670 break;
671 case EM_GETFIRSTVISIBLELINE:
672 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
673 break;
675 case EM_SETREADONLY16:
676 case EM_SETREADONLY:
677 if (wParam) {
678 SetWindowLongW( hwnd, GWL_STYLE,
679 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
680 es->style |= ES_READONLY;
681 } else {
682 SetWindowLongW( hwnd, GWL_STYLE,
683 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
684 es->style &= ~ES_READONLY;
686 result = 1;
687 break;
689 case EM_SETWORDBREAKPROC16:
690 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
691 break;
692 case EM_SETWORDBREAKPROC:
693 EDIT_EM_SetWordBreakProc(es, lParam);
694 break;
696 case EM_GETWORDBREAKPROC16:
697 result = (LRESULT)es->word_break_proc16;
698 break;
699 case EM_GETWORDBREAKPROC:
700 result = (LRESULT)es->word_break_proc;
701 break;
703 case EM_GETPASSWORDCHAR16:
704 unicode = FALSE; /* 16-bit message is always ascii */
705 /* fall through */
706 case EM_GETPASSWORDCHAR:
708 if(unicode)
709 result = es->password_char;
710 else
712 WCHAR charW = es->password_char;
713 CHAR charA = 0;
714 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
715 result = charA;
717 break;
720 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
722 case EM_SETMARGINS:
723 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
724 break;
726 case EM_GETMARGINS:
727 result = MAKELONG(es->left_margin, es->right_margin);
728 break;
730 case EM_GETLIMITTEXT:
731 result = es->buffer_limit;
732 break;
734 case EM_POSFROMCHAR:
735 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
736 break;
738 case EM_CHARFROMPOS:
739 result = EDIT_EM_CharFromPos(es, SLOWORD(lParam), SHIWORD(lParam));
740 break;
742 /* End of the EM_ messages which were in numerical order; what order
743 * are these in? vaguely alphabetical?
746 case WM_GETDLGCODE:
747 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
749 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
751 int vk = (int)((LPMSG)lParam)->wParam;
753 if (vk == VK_RETURN && (GetWindowLongW( hwnd, GWL_STYLE ) & ES_WANTRETURN))
755 result |= DLGC_WANTMESSAGE;
757 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
759 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
760 result |= DLGC_WANTMESSAGE;
763 break;
765 case WM_CHAR:
767 WCHAR charW;
769 if(unicode)
770 charW = wParam;
771 else
773 CHAR charA = wParam;
774 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
777 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
779 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
780 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
781 break;
783 EDIT_WM_Char(es, charW);
784 break;
787 case WM_CLEAR:
788 EDIT_WM_Clear(es);
789 break;
791 case WM_COMMAND:
792 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
793 break;
795 case WM_CONTEXTMENU:
796 EDIT_WM_ContextMenu(es, SLOWORD(lParam), SHIWORD(lParam));
797 break;
799 case WM_COPY:
800 EDIT_WM_Copy(es);
801 break;
803 case WM_CREATE:
804 if(unicode)
805 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
806 else
808 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
809 LPWSTR nameW = NULL;
810 if(nameA)
812 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
813 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
814 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
816 result = EDIT_WM_Create(es, nameW);
817 if(nameW)
818 HeapFree(GetProcessHeap(), 0, nameW);
820 break;
822 case WM_CUT:
823 EDIT_WM_Cut(es);
824 break;
826 case WM_ENABLE:
827 es->bEnableState = (BOOL) wParam;
828 EDIT_UpdateText(es, NULL, TRUE);
829 break;
831 case WM_ERASEBKGND:
832 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
833 break;
835 case WM_GETFONT:
836 result = (LRESULT)es->font;
837 break;
839 case WM_GETTEXT:
840 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
841 break;
843 case WM_GETTEXTLENGTH:
844 if (unicode) result = strlenW(es->text);
845 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
846 NULL, 0, NULL, NULL );
847 break;
849 case WM_HSCROLL:
850 result = EDIT_WM_HScroll(es, LOWORD(wParam), SHIWORD(wParam));
851 break;
853 case WM_KEYDOWN:
854 result = EDIT_WM_KeyDown(es, (INT)wParam);
855 break;
857 case WM_KILLFOCUS:
858 result = EDIT_WM_KillFocus(es);
859 break;
861 case WM_LBUTTONDBLCLK:
862 result = EDIT_WM_LButtonDblClk(es);
863 break;
865 case WM_LBUTTONDOWN:
866 result = EDIT_WM_LButtonDown(es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
867 break;
869 case WM_LBUTTONUP:
870 result = EDIT_WM_LButtonUp(es);
871 break;
873 case WM_MBUTTONDOWN:
874 result = EDIT_WM_MButtonDown(es);
875 break;
877 case WM_MOUSEACTIVATE:
879 * FIXME: maybe DefWindowProc() screws up, but it seems that
880 * modeless dialog boxes need this. If we don't do this, the focus
881 * will _not_ be set by DefWindowProc() for edit controls in a
882 * modeless dialog box ???
884 SetFocus(hwnd);
885 result = MA_ACTIVATE;
886 break;
888 case WM_MOUSEMOVE:
889 result = EDIT_WM_MouseMove(es, SLOWORD(lParam), SHIWORD(lParam));
890 break;
892 case WM_PAINT:
893 EDIT_WM_Paint(es, wParam);
894 break;
896 case WM_PASTE:
897 EDIT_WM_Paste(es);
898 break;
900 case WM_SETFOCUS:
901 EDIT_WM_SetFocus(es);
902 break;
904 case WM_SETFONT:
905 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
906 break;
908 case WM_SETREDRAW:
909 /* FIXME: actually set an internal flag and behave accordingly */
910 break;
912 case WM_SETTEXT:
913 EDIT_WM_SetText(es, lParam, unicode);
914 result = TRUE;
915 break;
917 case WM_SIZE:
918 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
919 break;
921 case WM_STYLECHANGED:
922 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
923 break;
925 case WM_STYLECHANGING:
926 result = 0; /* See EDIT_WM_StyleChanged */
927 break;
929 case WM_SYSKEYDOWN:
930 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
931 break;
933 case WM_TIMER:
934 EDIT_WM_Timer(es);
935 break;
937 case WM_VSCROLL:
938 result = EDIT_WM_VScroll(es, LOWORD(wParam), SHIWORD(wParam));
939 break;
941 case WM_MOUSEWHEEL:
943 int gcWheelDelta = 0;
944 UINT pulScrollLines = 3;
945 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
947 if (wParam & (MK_SHIFT | MK_CONTROL)) {
948 result = DefWindowProcW(hwnd, msg, wParam, lParam);
949 break;
951 gcWheelDelta -= SHIWORD(wParam);
952 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
954 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
955 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
956 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
959 break;
960 default:
961 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
962 break;
965 if (es) EDIT_UnlockBuffer(es, FALSE);
967 return result;
970 /*********************************************************************
972 * EditWndProcW (USER32.@)
974 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
976 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
979 /*********************************************************************
981 * EditWndProc (USER32.@)
983 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
985 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
988 /*********************************************************************
990 * EDIT_BuildLineDefs_ML
992 * Build linked list of text lines.
993 * Lines can end with '\0' (last line), a character (if it is wrapped),
994 * a soft return '\r\r\n' or a hard return '\r\n'
997 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
999 HDC dc;
1000 HFONT old_font = 0;
1001 LPWSTR current_position, cp;
1002 INT fw;
1003 LINEDEF *current_line;
1004 LINEDEF *previous_line;
1005 LINEDEF *start_line;
1006 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1007 INT line_count = es->line_count;
1008 INT orig_net_length;
1009 RECT rc;
1011 if (istart == iend && delta == 0)
1012 return;
1014 dc = GetDC(es->hwndSelf);
1015 if (es->font)
1016 old_font = SelectObject(dc, es->font);
1018 previous_line = NULL;
1019 current_line = es->first_line_def;
1021 /* Find starting line. istart must lie inside an existing line or
1022 * at the end of buffer */
1023 do {
1024 if (istart < current_line->index + current_line->length ||
1025 current_line->ending == END_0)
1026 break;
1028 previous_line = current_line;
1029 current_line = current_line->next;
1030 line_index++;
1031 } while (current_line);
1033 if (!current_line) /* Error occurred start is not inside previous buffer */
1035 FIXME(" modification occurred outside buffer\n");
1036 ReleaseDC(es->hwndSelf, dc);
1037 return;
1040 /* Remember start of modifications in order to calculate update region */
1041 nstart_line = line_index;
1042 nstart_index = current_line->index;
1044 /* We must start to reformat from the previous line since the modifications
1045 * may have caused the line to wrap upwards. */
1046 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1048 line_index--;
1049 current_line = previous_line;
1051 start_line = current_line;
1053 fw = es->format_rect.right - es->format_rect.left;
1054 current_position = es->text + current_line->index;
1055 do {
1056 if (current_line != start_line)
1058 if (!current_line || current_line->index + delta > current_position - es->text)
1060 /* The buffer has been expanded, create a new line and
1061 insert it into the link list */
1062 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1063 new_line->next = previous_line->next;
1064 previous_line->next = new_line;
1065 current_line = new_line;
1066 es->line_count++;
1068 else if (current_line->index + delta < current_position - es->text)
1070 /* The previous line merged with this line so we delete this extra entry */
1071 previous_line->next = current_line->next;
1072 HeapFree(GetProcessHeap(), 0, current_line);
1073 current_line = previous_line->next;
1074 es->line_count--;
1075 continue;
1077 else /* current_line->index + delta == current_position */
1079 if (current_position - es->text > iend)
1080 break; /* We reached end of line modifications */
1081 /* else recalulate this line */
1085 current_line->index = current_position - es->text;
1086 orig_net_length = current_line->net_length;
1088 /* Find end of line */
1089 cp = current_position;
1090 while (*cp) {
1091 if (*cp == '\n') break;
1092 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1093 break;
1094 cp++;
1097 /* Mark type of line termination */
1098 if (!(*cp)) {
1099 current_line->ending = END_0;
1100 current_line->net_length = strlenW(current_position);
1101 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1102 current_line->ending = END_SOFT;
1103 current_line->net_length = cp - current_position - 1;
1104 } else if (*cp == '\n') {
1105 current_line->ending = END_RICH;
1106 current_line->net_length = cp - current_position;
1107 } else {
1108 current_line->ending = END_HARD;
1109 current_line->net_length = cp - current_position;
1112 /* Calculate line width */
1113 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1114 current_position, current_line->net_length,
1115 es->tabs_count, es->tabs));
1117 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1118 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1119 INT next = 0;
1120 INT prev;
1121 do {
1122 prev = next;
1123 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1124 prev + 1, current_line->net_length, WB_RIGHT);
1125 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1126 current_position, next, es->tabs_count, es->tabs));
1127 } while (current_line->width <= fw);
1128 if (!prev) { /* Didn't find a line break so force a break */
1129 next = 0;
1130 do {
1131 prev = next;
1132 next++;
1133 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1134 current_position, next, es->tabs_count, es->tabs));
1135 } while (current_line->width <= fw);
1136 if (!prev)
1137 prev = 1;
1140 /* If the first line we are calculating, wrapped before istart, we must
1141 * adjust istart in order for this to be reflected in the update region. */
1142 if (current_line->index == nstart_index && istart > current_line->index + prev)
1143 istart = current_line->index + prev;
1144 /* else if we are updating the previous line before the first line we
1145 * are re-calculating and it expanded */
1146 else if (current_line == start_line &&
1147 current_line->index != nstart_index && orig_net_length < prev)
1149 /* Line expanded due to an upwards line wrap so we must partially include
1150 * previous line in update region */
1151 nstart_line = line_index;
1152 nstart_index = current_line->index;
1153 istart = current_line->index + orig_net_length;
1156 current_line->net_length = prev;
1157 current_line->ending = END_WRAP;
1158 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1159 current_line->net_length, es->tabs_count, es->tabs));
1163 /* Adjust length to include line termination */
1164 switch (current_line->ending) {
1165 case END_SOFT:
1166 current_line->length = current_line->net_length + 3;
1167 break;
1168 case END_RICH:
1169 current_line->length = current_line->net_length + 1;
1170 break;
1171 case END_HARD:
1172 current_line->length = current_line->net_length + 2;
1173 break;
1174 case END_WRAP:
1175 case END_0:
1176 current_line->length = current_line->net_length;
1177 break;
1179 es->text_width = max(es->text_width, current_line->width);
1180 current_position += current_line->length;
1181 previous_line = current_line;
1182 current_line = current_line->next;
1183 line_index++;
1184 } while (previous_line->ending != END_0);
1186 /* Finish adjusting line indexes by delta or remove hanging lines */
1187 if (previous_line->ending == END_0)
1189 LINEDEF *pnext = NULL;
1191 previous_line->next = NULL;
1192 while (current_line)
1194 pnext = current_line->next;
1195 HeapFree(GetProcessHeap(), 0, current_line);
1196 current_line = pnext;
1197 es->line_count--;
1200 else
1202 while (current_line)
1204 current_line->index += delta;
1205 current_line = current_line->next;
1209 /* Calculate rest of modification rectangle */
1210 if (hrgn)
1212 HRGN tmphrgn;
1214 * We calculate two rectangles. One for the first line which may have
1215 * an indent with respect to the format rect. The other is a format-width
1216 * rectangle that spans the rest of the lines that changed or moved.
1218 rc.top = es->format_rect.top + nstart_line * es->line_height -
1219 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1220 rc.bottom = rc.top + es->line_height;
1221 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1222 es->text + nstart_index, istart - nstart_index,
1223 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1224 rc.right = es->format_rect.right;
1225 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1227 rc.top = rc.bottom;
1228 rc.left = es->format_rect.left;
1229 rc.right = es->format_rect.right;
1231 * If lines were added or removed we must re-paint the remainder of the
1232 * lines since the remaining lines were either shifted up or down.
1234 if (line_count < es->line_count) /* We added lines */
1235 rc.bottom = es->line_count * es->line_height;
1236 else if (line_count > es->line_count) /* We removed lines */
1237 rc.bottom = line_count * es->line_height;
1238 else
1239 rc.bottom = line_index * es->line_height;
1240 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1241 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1242 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1243 DeleteObject(tmphrgn);
1246 if (es->font)
1247 SelectObject(dc, old_font);
1249 ReleaseDC(es->hwndSelf, dc);
1252 /*********************************************************************
1254 * EDIT_CalcLineWidth_SL
1257 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1259 es->text_width = SLOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1262 /*********************************************************************
1264 * EDIT_CallWordBreakProc
1266 * Call appropriate WordBreakProc (internal or external).
1268 * Note: The "start" argument should always be an index referring
1269 * to es->text. The actual wordbreak proc might be
1270 * 16 bit, so we can't always pass any 32 bit LPSTR.
1271 * Hence we assume that es->text is the buffer that holds
1272 * the string under examination (we can decide this for ourselves).
1275 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1277 INT ret, iWndsLocks;
1279 /* To avoid any deadlocks, all the locks on the window structures
1280 must be suspended before the control is passed to the application */
1281 iWndsLocks = WIN_SuspendWndsLock();
1283 if (es->word_break_proc16) {
1284 HGLOBAL16 hglob16;
1285 SEGPTR segptr;
1286 INT countA;
1287 WORD args[5];
1288 DWORD result;
1290 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1291 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1292 segptr = K32WOWGlobalLock16(hglob16);
1293 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1294 args[4] = SELECTOROF(segptr);
1295 args[3] = OFFSETOF(segptr);
1296 args[2] = index;
1297 args[1] = countA;
1298 args[0] = action;
1299 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1300 ret = LOWORD(result);
1301 GlobalUnlock16(hglob16);
1302 GlobalFree16(hglob16);
1304 else if (es->word_break_proc)
1306 if(es->is_unicode)
1308 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1310 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1311 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1312 ret = wbpW(es->text + start, index, count, action);
1314 else
1316 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1317 INT countA;
1318 CHAR *textA;
1320 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1321 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1322 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1323 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1324 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1325 ret = wbpA(textA, index, countA, action);
1326 HeapFree(GetProcessHeap(), 0, textA);
1329 else
1330 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1332 WIN_RestoreWndsLock(iWndsLocks);
1333 return ret;
1337 /*********************************************************************
1339 * EDIT_CharFromPos
1341 * Beware: This is not the function called on EM_CHARFROMPOS
1342 * The position _can_ be outside the formatting / client
1343 * rectangle
1344 * The return value is only the character index
1347 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1349 INT index;
1350 HDC dc;
1351 HFONT old_font = 0;
1353 if (es->style & ES_MULTILINE) {
1354 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1355 INT line_index = 0;
1356 LINEDEF *line_def = es->first_line_def;
1357 INT low, high;
1358 while ((line > 0) && line_def->next) {
1359 line_index += line_def->length;
1360 line_def = line_def->next;
1361 line--;
1363 x += es->x_offset - es->format_rect.left;
1364 if (x >= line_def->width) {
1365 if (after_wrap)
1366 *after_wrap = (line_def->ending == END_WRAP);
1367 return line_index + line_def->net_length;
1369 if (x <= 0) {
1370 if (after_wrap)
1371 *after_wrap = FALSE;
1372 return line_index;
1374 dc = GetDC(es->hwndSelf);
1375 if (es->font)
1376 old_font = SelectObject(dc, es->font);
1377 low = line_index + 1;
1378 high = line_index + line_def->net_length + 1;
1379 while (low < high - 1)
1381 INT mid = (low + high) / 2;
1382 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1383 else low = mid;
1385 index = low;
1387 if (after_wrap)
1388 *after_wrap = ((index == line_index + line_def->net_length) &&
1389 (line_def->ending == END_WRAP));
1390 } else {
1391 LPWSTR text;
1392 SIZE size;
1393 if (after_wrap)
1394 *after_wrap = FALSE;
1395 x -= es->format_rect.left;
1396 if (!x)
1397 return es->x_offset;
1398 text = EDIT_GetPasswordPointer_SL(es);
1399 dc = GetDC(es->hwndSelf);
1400 if (es->font)
1401 old_font = SelectObject(dc, es->font);
1402 if (x < 0)
1404 INT low = 0;
1405 INT high = es->x_offset;
1406 while (low < high - 1)
1408 INT mid = (low + high) / 2;
1409 GetTextExtentPoint32W( dc, text + mid,
1410 es->x_offset - mid, &size );
1411 if (size.cx > -x) low = mid;
1412 else high = mid;
1414 index = low;
1416 else
1418 INT low = es->x_offset;
1419 INT high = strlenW(es->text) + 1;
1420 while (low < high - 1)
1422 INT mid = (low + high) / 2;
1423 GetTextExtentPoint32W( dc, text + es->x_offset,
1424 mid - es->x_offset, &size );
1425 if (size.cx > x) high = mid;
1426 else low = mid;
1428 index = low;
1430 if (es->style & ES_PASSWORD)
1431 HeapFree(GetProcessHeap(), 0, text);
1433 if (es->font)
1434 SelectObject(dc, old_font);
1435 ReleaseDC(es->hwndSelf, dc);
1436 return index;
1440 /*********************************************************************
1442 * EDIT_ConfinePoint
1444 * adjusts the point to be within the formatting rectangle
1445 * (so CharFromPos returns the nearest _visible_ character)
1448 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1450 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1451 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1455 /*********************************************************************
1457 * EDIT_GetLineRect
1459 * Calculates the bounding rectangle for a line from a starting
1460 * column to an ending column.
1463 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1465 INT line_index = EDIT_EM_LineIndex(es, line);
1467 if (es->style & ES_MULTILINE)
1468 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1469 else
1470 rc->top = es->format_rect.top;
1471 rc->bottom = rc->top + es->line_height;
1472 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1473 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1477 /*********************************************************************
1479 * EDIT_GetPasswordPointer_SL
1481 * note: caller should free the (optionally) allocated buffer
1484 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1486 if (es->style & ES_PASSWORD) {
1487 INT len = strlenW(es->text);
1488 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1489 text[len] = '\0';
1490 while(len) text[--len] = es->password_char;
1491 return text;
1492 } else
1493 return es->text;
1497 /*********************************************************************
1499 * EDIT_LockBuffer
1501 * This acts as a LOCAL_Lock(), but it locks only once. This way
1502 * you can call it whenever you like, without unlocking.
1504 * Initially the edit control allocates a HLOCAL32 buffer
1505 * (32 bit linear memory handler). However, 16 bit application
1506 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1507 * handler). From that moment on we have to keep using this 16 bit memory
1508 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1509 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1510 * conversion.
1513 static void EDIT_LockBuffer(EDITSTATE *es)
1515 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
1516 if (!es->text) {
1517 CHAR *textA = NULL;
1518 UINT countA = 0;
1519 BOOL _16bit = FALSE;
1521 if(es->hloc32W)
1523 if(es->hloc32A)
1525 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1526 textA = LocalLock(es->hloc32A);
1527 countA = strlen(textA) + 1;
1529 else if(es->hloc16)
1531 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1532 textA = LOCAL_Lock(hInstance, es->hloc16);
1533 countA = strlen(textA) + 1;
1534 _16bit = TRUE;
1537 else {
1538 ERR("no buffer ... please report\n");
1539 return;
1542 if(textA)
1544 HLOCAL hloc32W_new;
1545 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1546 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1547 if(countW_new > es->buffer_size + 1)
1549 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1550 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1551 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1552 if(hloc32W_new)
1554 es->hloc32W = hloc32W_new;
1555 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1556 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1558 else
1559 WARN("FAILED! Will synchronize partially\n");
1563 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1564 es->text = LocalLock(es->hloc32W);
1566 if(textA)
1568 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1569 if(_16bit)
1570 LOCAL_Unlock(hInstance, es->hloc16);
1571 else
1572 LocalUnlock(es->hloc32A);
1575 es->lock_count++;
1579 /*********************************************************************
1581 * EDIT_SL_InvalidateText
1583 * Called from EDIT_InvalidateText().
1584 * Does the job for single-line controls only.
1587 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1589 RECT line_rect;
1590 RECT rc;
1592 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1593 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1594 EDIT_UpdateText(es, &rc, TRUE);
1598 /*********************************************************************
1600 * EDIT_ML_InvalidateText
1602 * Called from EDIT_InvalidateText().
1603 * Does the job for multi-line controls only.
1606 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1608 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1609 INT sl = EDIT_EM_LineFromChar(es, start);
1610 INT el = EDIT_EM_LineFromChar(es, end);
1611 INT sc;
1612 INT ec;
1613 RECT rc1;
1614 RECT rcWnd;
1615 RECT rcLine;
1616 RECT rcUpdate;
1617 INT l;
1619 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1620 return;
1622 sc = start - EDIT_EM_LineIndex(es, sl);
1623 ec = end - EDIT_EM_LineIndex(es, el);
1624 if (sl < es->y_offset) {
1625 sl = es->y_offset;
1626 sc = 0;
1628 if (el > es->y_offset + vlc) {
1629 el = es->y_offset + vlc;
1630 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1632 GetClientRect(es->hwndSelf, &rc1);
1633 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1634 if (sl == el) {
1635 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1636 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1637 EDIT_UpdateText(es, &rcUpdate, TRUE);
1638 } else {
1639 EDIT_GetLineRect(es, sl, sc,
1640 EDIT_EM_LineLength(es,
1641 EDIT_EM_LineIndex(es, sl)),
1642 &rcLine);
1643 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1644 EDIT_UpdateText(es, &rcUpdate, TRUE);
1645 for (l = sl + 1 ; l < el ; l++) {
1646 EDIT_GetLineRect(es, l, 0,
1647 EDIT_EM_LineLength(es,
1648 EDIT_EM_LineIndex(es, l)),
1649 &rcLine);
1650 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1651 EDIT_UpdateText(es, &rcUpdate, TRUE);
1653 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1654 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1655 EDIT_UpdateText(es, &rcUpdate, TRUE);
1660 /*********************************************************************
1662 * EDIT_InvalidateText
1664 * Invalidate the text from offset start upto, but not including,
1665 * offset end. Useful for (re)painting the selection.
1666 * Regions outside the linewidth are not invalidated.
1667 * end == -1 means end == TextLength.
1668 * start and end need not be ordered.
1671 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1673 if (end == start)
1674 return;
1676 if (end == -1)
1677 end = strlenW(es->text);
1679 if (end < start) {
1680 INT tmp = start;
1681 start = end;
1682 end = tmp;
1685 if (es->style & ES_MULTILINE)
1686 EDIT_ML_InvalidateText(es, start, end);
1687 else
1688 EDIT_SL_InvalidateText(es, start, end);
1692 /*********************************************************************
1694 * EDIT_MakeFit
1696 * Try to fit size + 1 characters in the buffer.
1697 * Constrain to limits if honor_limit is TRUE.
1699 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size, BOOL honor_limit)
1701 HLOCAL hNew32W;
1703 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
1704 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
1705 return FALSE;
1708 if (size <= es->buffer_size)
1709 return TRUE;
1711 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1713 /* Force edit to unlock it's buffer. es->text now NULL */
1714 EDIT_UnlockBuffer(es, TRUE);
1716 if (es->hloc32W) {
1717 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1718 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1719 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1720 es->hloc32W = hNew32W;
1721 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1725 EDIT_LockBuffer(es);
1727 if (es->buffer_size < size) {
1728 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1729 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1730 return FALSE;
1731 } else {
1732 TRACE("We now have %d+1\n", es->buffer_size);
1733 return TRUE;
1738 /*********************************************************************
1740 * EDIT_MakeUndoFit
1742 * Try to fit size + 1 bytes in the undo buffer.
1745 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1747 UINT alloc_size;
1749 if (size <= es->undo_buffer_size)
1750 return TRUE;
1752 TRACE("trying to ReAlloc to %d+1\n", size);
1754 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1755 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1756 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1757 return TRUE;
1759 else
1761 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1762 return FALSE;
1767 /*********************************************************************
1769 * EDIT_MoveBackward
1772 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1774 INT e = es->selection_end;
1776 if (e) {
1777 e--;
1778 if ((es->style & ES_MULTILINE) && e &&
1779 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1780 e--;
1781 if (e && (es->text[e - 1] == '\r'))
1782 e--;
1785 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1786 EDIT_EM_ScrollCaret(es);
1790 /*********************************************************************
1792 * EDIT_MoveDown_ML
1794 * Only for multi line controls
1795 * Move the caret one line down, on a column with the nearest
1796 * x coordinate on the screen (might be a different column).
1799 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1801 INT s = es->selection_start;
1802 INT e = es->selection_end;
1803 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1804 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1805 INT x = SLOWORD(pos);
1806 INT y = SHIWORD(pos);
1808 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1809 if (!extend)
1810 s = e;
1811 EDIT_EM_SetSel(es, s, e, after_wrap);
1812 EDIT_EM_ScrollCaret(es);
1816 /*********************************************************************
1818 * EDIT_MoveEnd
1821 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1823 BOOL after_wrap = FALSE;
1824 INT e;
1826 /* Pass a high value in x to make sure of receiving the end of the line */
1827 if (es->style & ES_MULTILINE)
1828 e = EDIT_CharFromPos(es, 0x3fffffff,
1829 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1830 else
1831 e = strlenW(es->text);
1832 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1833 EDIT_EM_ScrollCaret(es);
1837 /*********************************************************************
1839 * EDIT_MoveForward
1842 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1844 INT e = es->selection_end;
1846 if (es->text[e]) {
1847 e++;
1848 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1849 if (es->text[e] == '\n')
1850 e++;
1851 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1852 e += 2;
1855 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1856 EDIT_EM_ScrollCaret(es);
1860 /*********************************************************************
1862 * EDIT_MoveHome
1864 * Home key: move to beginning of line.
1867 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1869 INT e;
1871 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1872 if (es->style & ES_MULTILINE)
1873 e = EDIT_CharFromPos(es, -es->x_offset,
1874 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1875 else
1876 e = 0;
1877 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1878 EDIT_EM_ScrollCaret(es);
1882 /*********************************************************************
1884 * EDIT_MovePageDown_ML
1886 * Only for multi line controls
1887 * Move the caret one page down, on a column with the nearest
1888 * x coordinate on the screen (might be a different column).
1891 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1893 INT s = es->selection_start;
1894 INT e = es->selection_end;
1895 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1896 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1897 INT x = SLOWORD(pos);
1898 INT y = SHIWORD(pos);
1900 e = EDIT_CharFromPos(es, x,
1901 y + (es->format_rect.bottom - es->format_rect.top),
1902 &after_wrap);
1903 if (!extend)
1904 s = e;
1905 EDIT_EM_SetSel(es, s, e, after_wrap);
1906 EDIT_EM_ScrollCaret(es);
1910 /*********************************************************************
1912 * EDIT_MovePageUp_ML
1914 * Only for multi line controls
1915 * Move the caret one page up, on a column with the nearest
1916 * x coordinate on the screen (might be a different column).
1919 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1921 INT s = es->selection_start;
1922 INT e = es->selection_end;
1923 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1924 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1925 INT x = SLOWORD(pos);
1926 INT y = SHIWORD(pos);
1928 e = EDIT_CharFromPos(es, x,
1929 y - (es->format_rect.bottom - es->format_rect.top),
1930 &after_wrap);
1931 if (!extend)
1932 s = e;
1933 EDIT_EM_SetSel(es, s, e, after_wrap);
1934 EDIT_EM_ScrollCaret(es);
1938 /*********************************************************************
1940 * EDIT_MoveUp_ML
1942 * Only for multi line controls
1943 * Move the caret one line up, on a column with the nearest
1944 * x coordinate on the screen (might be a different column).
1947 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1949 INT s = es->selection_start;
1950 INT e = es->selection_end;
1951 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1952 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1953 INT x = SLOWORD(pos);
1954 INT y = SHIWORD(pos);
1956 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1957 if (!extend)
1958 s = e;
1959 EDIT_EM_SetSel(es, s, e, after_wrap);
1960 EDIT_EM_ScrollCaret(es);
1964 /*********************************************************************
1966 * EDIT_MoveWordBackward
1969 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1971 INT s = es->selection_start;
1972 INT e = es->selection_end;
1973 INT l;
1974 INT ll;
1975 INT li;
1977 l = EDIT_EM_LineFromChar(es, e);
1978 ll = EDIT_EM_LineLength(es, e);
1979 li = EDIT_EM_LineIndex(es, l);
1980 if (e - li == 0) {
1981 if (l) {
1982 li = EDIT_EM_LineIndex(es, l - 1);
1983 e = li + EDIT_EM_LineLength(es, li);
1985 } else {
1986 e = li + (INT)EDIT_CallWordBreakProc(es,
1987 li, e - li, ll, WB_LEFT);
1989 if (!extend)
1990 s = e;
1991 EDIT_EM_SetSel(es, s, e, FALSE);
1992 EDIT_EM_ScrollCaret(es);
1996 /*********************************************************************
1998 * EDIT_MoveWordForward
2001 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2003 INT s = es->selection_start;
2004 INT e = es->selection_end;
2005 INT l;
2006 INT ll;
2007 INT li;
2009 l = EDIT_EM_LineFromChar(es, e);
2010 ll = EDIT_EM_LineLength(es, e);
2011 li = EDIT_EM_LineIndex(es, l);
2012 if (e - li == ll) {
2013 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2014 e = EDIT_EM_LineIndex(es, l + 1);
2015 } else {
2016 e = li + EDIT_CallWordBreakProc(es,
2017 li, e - li + 1, ll, WB_RIGHT);
2019 if (!extend)
2020 s = e;
2021 EDIT_EM_SetSel(es, s, e, FALSE);
2022 EDIT_EM_ScrollCaret(es);
2026 /*********************************************************************
2028 * EDIT_PaintLine
2031 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2033 INT s = es->selection_start;
2034 INT e = es->selection_end;
2035 INT li;
2036 INT ll;
2037 INT x;
2038 INT y;
2039 LRESULT pos;
2041 if (es->style & ES_MULTILINE) {
2042 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2043 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2044 return;
2045 } else if (line)
2046 return;
2048 TRACE("line=%d\n", line);
2050 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2051 x = SLOWORD(pos);
2052 y = SHIWORD(pos);
2053 li = EDIT_EM_LineIndex(es, line);
2054 ll = EDIT_EM_LineLength(es, li);
2055 s = min(es->selection_start, es->selection_end);
2056 e = max(es->selection_start, es->selection_end);
2057 s = min(li + ll, max(li, s));
2058 e = min(li + ll, max(li, e));
2059 if (rev && (s != e) &&
2060 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2061 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2062 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2063 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2064 } else
2065 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2069 /*********************************************************************
2071 * EDIT_PaintText
2074 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2076 COLORREF BkColor;
2077 COLORREF TextColor;
2078 INT ret;
2079 INT li;
2080 INT BkMode;
2081 SIZE size;
2083 if (!count)
2084 return 0;
2085 BkMode = GetBkMode(dc);
2086 BkColor = GetBkColor(dc);
2087 TextColor = GetTextColor(dc);
2088 if (rev) {
2089 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2090 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2091 SetBkMode( dc, OPAQUE);
2093 li = EDIT_EM_LineIndex(es, line);
2094 if (es->style & ES_MULTILINE) {
2095 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2096 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2097 } else {
2098 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2099 TextOutW(dc, x, y, text + li + col, count);
2100 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2101 ret = size.cx;
2102 if (es->style & ES_PASSWORD)
2103 HeapFree(GetProcessHeap(), 0, text);
2105 if (rev) {
2106 SetBkColor(dc, BkColor);
2107 SetTextColor(dc, TextColor);
2108 SetBkMode( dc, BkMode);
2110 return ret;
2114 /*********************************************************************
2116 * EDIT_SetCaretPos
2119 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2120 BOOL after_wrap)
2122 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2123 SetCaretPos(SLOWORD(res), SHIWORD(res));
2127 /*********************************************************************
2129 * EDIT_SetRectNP
2131 * note: this is not (exactly) the handler called on EM_SETRECTNP
2132 * it is also used to set the rect of a single line control
2135 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2137 CopyRect(&es->format_rect, rc);
2138 if (es->style & WS_BORDER) {
2139 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2140 if(TWEAK_WineLook == WIN31_LOOK)
2141 bw += 2;
2142 es->format_rect.left += bw;
2143 es->format_rect.top += bw;
2144 es->format_rect.right -= bw;
2145 es->format_rect.bottom -= bw;
2147 es->format_rect.left += es->left_margin;
2148 es->format_rect.right -= es->right_margin;
2149 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2150 if (es->style & ES_MULTILINE)
2152 INT fw, vlc, max_x_offset, max_y_offset;
2154 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2155 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2157 /* correct es->x_offset */
2158 fw = es->format_rect.right - es->format_rect.left;
2159 max_x_offset = es->text_width - fw;
2160 if(max_x_offset < 0) max_x_offset = 0;
2161 if(es->x_offset > max_x_offset)
2162 es->x_offset = max_x_offset;
2164 /* correct es->y_offset */
2165 max_y_offset = es->line_count - vlc;
2166 if(max_y_offset < 0) max_y_offset = 0;
2167 if(es->y_offset > max_y_offset)
2168 es->y_offset = max_y_offset;
2170 /* force scroll info update */
2171 EDIT_UpdateScrollInfo(es);
2173 else
2174 /* Windows doesn't care to fix text placement for SL controls */
2175 es->format_rect.bottom = es->format_rect.top + es->line_height;
2177 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2178 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2182 /*********************************************************************
2184 * EDIT_UnlockBuffer
2187 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2189 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2191 /* Edit window might be already destroyed */
2192 if(!IsWindow(es->hwndSelf))
2194 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2195 return;
2198 if (!es->lock_count) {
2199 ERR("lock_count == 0 ... please report\n");
2200 return;
2202 if (!es->text) {
2203 ERR("es->text == 0 ... please report\n");
2204 return;
2207 if (force || (es->lock_count == 1)) {
2208 if (es->hloc32W) {
2209 CHAR *textA = NULL;
2210 BOOL _16bit = FALSE;
2211 UINT countA = 0;
2212 UINT countW = strlenW(es->text) + 1;
2214 if(es->hloc32A)
2216 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2217 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2218 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2219 countA = LocalSize(es->hloc32A);
2220 if(countA_new > countA)
2222 HLOCAL hloc32A_new;
2223 UINT alloc_size = ROUND_TO_GROW(countA_new);
2224 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2225 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2226 if(hloc32A_new)
2228 es->hloc32A = hloc32A_new;
2229 countA = LocalSize(hloc32A_new);
2230 TRACE("Real new size %d bytes\n", countA);
2232 else
2233 WARN("FAILED! Will synchronize partially\n");
2235 textA = LocalLock(es->hloc32A);
2237 else if(es->hloc16)
2239 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2240 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2241 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2242 countA = LOCAL_Size(hInstance, es->hloc16);
2243 if(countA_new > countA)
2245 HLOCAL16 hloc16_new;
2246 UINT alloc_size = ROUND_TO_GROW(countA_new);
2247 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2248 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2249 if(hloc16_new)
2251 es->hloc16 = hloc16_new;
2252 countA = LOCAL_Size(hInstance, hloc16_new);
2253 TRACE("Real new size %d bytes\n", countA);
2255 else
2256 WARN("FAILED! Will synchronize partially\n");
2258 textA = LOCAL_Lock(hInstance, es->hloc16);
2259 _16bit = TRUE;
2262 if(textA)
2264 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2265 if(_16bit)
2266 LOCAL_Unlock(hInstance, es->hloc16);
2267 else
2268 LocalUnlock(es->hloc32A);
2271 LocalUnlock(es->hloc32W);
2272 es->text = NULL;
2274 else {
2275 ERR("no buffer ... please report\n");
2276 return;
2279 es->lock_count--;
2283 /*********************************************************************
2285 * EDIT_UpdateScrollInfo
2288 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2290 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2292 SCROLLINFO si;
2293 si.cbSize = sizeof(SCROLLINFO);
2294 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2295 si.nMin = 0;
2296 si.nMax = es->line_count - 1;
2297 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2298 si.nPos = es->y_offset;
2299 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2300 si.nMin, si.nMax, si.nPage, si.nPos);
2301 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2304 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2306 SCROLLINFO si;
2307 si.cbSize = sizeof(SCROLLINFO);
2308 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2309 si.nMin = 0;
2310 si.nMax = es->text_width - 1;
2311 si.nPage = es->format_rect.right - es->format_rect.left;
2312 si.nPos = es->x_offset;
2313 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2314 si.nMin, si.nMax, si.nPage, si.nPos);
2315 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2319 /*********************************************************************
2321 * EDIT_WordBreakProc
2323 * Find the beginning of words.
2324 * Note: unlike the specs for a WordBreakProc, this function only
2325 * allows to be called without linebreaks between s[0] upto
2326 * s[count - 1]. Remember it is only called
2327 * internally, so we can decide this for ourselves.
2330 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2332 INT ret = 0;
2334 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2336 if(!s) return 0;
2338 switch (action) {
2339 case WB_LEFT:
2340 if (!count)
2341 break;
2342 if (index)
2343 index--;
2344 if (s[index] == ' ') {
2345 while (index && (s[index] == ' '))
2346 index--;
2347 if (index) {
2348 while (index && (s[index] != ' '))
2349 index--;
2350 if (s[index] == ' ')
2351 index++;
2353 } else {
2354 while (index && (s[index] != ' '))
2355 index--;
2356 if (s[index] == ' ')
2357 index++;
2359 ret = index;
2360 break;
2361 case WB_RIGHT:
2362 if (!count)
2363 break;
2364 if (index)
2365 index--;
2366 if (s[index] == ' ')
2367 while ((index < count) && (s[index] == ' ')) index++;
2368 else {
2369 while (s[index] && (s[index] != ' ') && (index < count))
2370 index++;
2371 while ((s[index] == ' ') && (index < count)) index++;
2373 ret = index;
2374 break;
2375 case WB_ISDELIMITER:
2376 ret = (s[index] == ' ');
2377 break;
2378 default:
2379 ERR("unknown action code, please report !\n");
2380 break;
2382 return ret;
2386 /*********************************************************************
2388 * EM_CHARFROMPOS
2390 * returns line number (not index) in high-order word of result.
2391 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2392 * to Richedit, not to the edit control. Original documentation is valid.
2393 * FIXME: do the specs mean to return -1 if outside client area or
2394 * if outside formatting rectangle ???
2397 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2399 POINT pt;
2400 RECT rc;
2401 INT index;
2403 pt.x = x;
2404 pt.y = y;
2405 GetClientRect(es->hwndSelf, &rc);
2406 if (!PtInRect(&rc, pt))
2407 return -1;
2409 index = EDIT_CharFromPos(es, x, y, NULL);
2410 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2414 /*********************************************************************
2416 * EM_FMTLINES
2418 * Enable or disable soft breaks.
2420 * This means: insert or remove the soft linebreak character (\r\r\n).
2421 * Take care to check if the text still fits the buffer after insertion.
2422 * If not, notify with EN_ERRSPACE.
2425 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2427 es->flags &= ~EF_USE_SOFTBRK;
2428 if (add_eol) {
2429 es->flags |= EF_USE_SOFTBRK;
2430 FIXME("soft break enabled, not implemented\n");
2432 return add_eol;
2436 /*********************************************************************
2438 * EM_GETHANDLE
2440 * Hopefully this won't fire back at us.
2441 * We always start with a fixed buffer in the local heap.
2442 * Despite of the documentation says that the local heap is used
2443 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2444 * buffer on the local heap.
2447 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2449 HLOCAL hLocal;
2451 if (!(es->style & ES_MULTILINE))
2452 return 0;
2454 if(es->is_unicode)
2455 hLocal = es->hloc32W;
2456 else
2458 if(!es->hloc32A)
2460 CHAR *textA;
2461 UINT countA, alloc_size;
2462 TRACE("Allocating 32-bit ANSI alias buffer\n");
2463 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2464 alloc_size = ROUND_TO_GROW(countA);
2465 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2467 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2468 return 0;
2470 textA = LocalLock(es->hloc32A);
2471 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2472 LocalUnlock(es->hloc32A);
2474 hLocal = es->hloc32A;
2477 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2478 return hLocal;
2482 /*********************************************************************
2484 * EM_GETHANDLE16
2486 * Hopefully this won't fire back at us.
2487 * We always start with a buffer in 32 bit linear memory.
2488 * However, with this message a 16 bit application requests
2489 * a handle of 16 bit local heap memory, where it expects to find
2490 * the text.
2491 * It's a pitty that from this moment on we have to use this
2492 * local heap, because applications may rely on the handle
2493 * in the future.
2495 * In this function we'll try to switch to local heap.
2497 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2499 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2500 CHAR *textA;
2501 UINT countA, alloc_size;
2503 if (!(es->style & ES_MULTILINE))
2504 return 0;
2506 if (es->hloc16)
2507 return es->hloc16;
2509 if (!LOCAL_HeapSize(hInstance)) {
2510 if (!LocalInit16(hInstance, 0,
2511 GlobalSize16(hInstance))) {
2512 ERR("could not initialize local heap\n");
2513 return 0;
2515 TRACE("local heap initialized\n");
2518 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2519 alloc_size = ROUND_TO_GROW(countA);
2521 TRACE("Allocating 16-bit ANSI alias buffer\n");
2522 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2523 ERR("could not allocate new 16 bit buffer\n");
2524 return 0;
2527 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2528 ERR("could not lock new 16 bit buffer\n");
2529 LOCAL_Free(hInstance, es->hloc16);
2530 es->hloc16 = 0;
2531 return 0;
2534 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2535 LOCAL_Unlock(hInstance, es->hloc16);
2537 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2538 return es->hloc16;
2542 /*********************************************************************
2544 * EM_GETLINE
2547 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2549 LPWSTR src;
2550 INT line_len, dst_len;
2551 INT i;
2553 if (es->style & ES_MULTILINE) {
2554 if (line >= es->line_count)
2555 return 0;
2556 } else
2557 line = 0;
2558 i = EDIT_EM_LineIndex(es, line);
2559 src = es->text + i;
2560 line_len = EDIT_EM_LineLength(es, i);
2561 dst_len = *(WORD *)lParam;
2562 if(unicode)
2564 LPWSTR dst = (LPWSTR)lParam;
2565 if(dst_len <= line_len)
2567 memcpy(dst, src, dst_len * sizeof(WCHAR));
2568 return dst_len;
2570 else /* Append 0 if enough space */
2572 memcpy(dst, src, line_len * sizeof(WCHAR));
2573 dst[line_len] = 0;
2574 return line_len;
2577 else
2579 LPSTR dst = (LPSTR)lParam;
2580 INT ret;
2581 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2582 if(!ret) /* Insufficient buffer size */
2583 return dst_len;
2584 if(ret < dst_len) /* Append 0 if enough space */
2585 dst[ret] = 0;
2586 return ret;
2591 /*********************************************************************
2593 * EM_GETSEL
2596 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2598 UINT s = es->selection_start;
2599 UINT e = es->selection_end;
2601 ORDER_UINT(s, e);
2602 if (start)
2603 *start = s;
2604 if (end)
2605 *end = e;
2606 return MAKELONG(s, e);
2610 /*********************************************************************
2612 * EM_GETTHUMB
2614 * FIXME: is this right ? (or should it be only VSCROLL)
2615 * (and maybe only for edit controls that really have their
2616 * own scrollbars) (and maybe only for multiline controls ?)
2617 * All in all: very poorly documented
2620 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2622 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2623 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2627 /*********************************************************************
2629 * EM_LINEFROMCHAR
2632 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2634 INT line;
2635 LINEDEF *line_def;
2637 if (!(es->style & ES_MULTILINE))
2638 return 0;
2639 if (index > (INT)strlenW(es->text))
2640 return es->line_count - 1;
2641 if (index == -1)
2642 index = min(es->selection_start, es->selection_end);
2644 line = 0;
2645 line_def = es->first_line_def;
2646 index -= line_def->length;
2647 while ((index >= 0) && line_def->next) {
2648 line++;
2649 line_def = line_def->next;
2650 index -= line_def->length;
2652 return line;
2656 /*********************************************************************
2658 * EM_LINEINDEX
2661 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2663 INT line_index;
2664 LINEDEF *line_def;
2666 if (!(es->style & ES_MULTILINE))
2667 return 0;
2668 if (line >= es->line_count)
2669 return -1;
2671 line_index = 0;
2672 line_def = es->first_line_def;
2673 if (line == -1) {
2674 INT index = es->selection_end - line_def->length;
2675 while ((index >= 0) && line_def->next) {
2676 line_index += line_def->length;
2677 line_def = line_def->next;
2678 index -= line_def->length;
2680 } else {
2681 while (line > 0) {
2682 line_index += line_def->length;
2683 line_def = line_def->next;
2684 line--;
2687 return line_index;
2691 /*********************************************************************
2693 * EM_LINELENGTH
2696 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2698 LINEDEF *line_def;
2700 if (!(es->style & ES_MULTILINE))
2701 return strlenW(es->text);
2703 if (index == -1) {
2704 /* get the number of remaining non-selected chars of selected lines */
2705 INT32 l; /* line number */
2706 INT32 li; /* index of first char in line */
2707 INT32 count;
2708 l = EDIT_EM_LineFromChar(es, es->selection_start);
2709 /* # chars before start of selection area */
2710 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2711 l = EDIT_EM_LineFromChar(es, es->selection_end);
2712 /* # chars after end of selection */
2713 li = EDIT_EM_LineIndex(es, l);
2714 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2715 return count;
2717 line_def = es->first_line_def;
2718 index -= line_def->length;
2719 while ((index >= 0) && line_def->next) {
2720 line_def = line_def->next;
2721 index -= line_def->length;
2723 return line_def->net_length;
2727 /*********************************************************************
2729 * EM_LINESCROLL
2731 * NOTE: dx is in average character widths, dy - in lines;
2734 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2736 if (!(es->style & ES_MULTILINE))
2737 return FALSE;
2739 dx *= es->char_width;
2740 return EDIT_EM_LineScroll_internal(es, dx, dy);
2743 /*********************************************************************
2745 * EDIT_EM_LineScroll_internal
2747 * Version of EDIT_EM_LineScroll for internal use.
2748 * It doesn't refuse if ES_MULTILINE is set and assumes that
2749 * dx is in pixels, dy - in lines.
2752 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2754 INT nyoff;
2755 INT x_offset_in_pixels;
2757 if (es->style & ES_MULTILINE)
2759 x_offset_in_pixels = es->x_offset;
2761 else
2763 dy = 0;
2764 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2767 if (-dx > x_offset_in_pixels)
2768 dx = -x_offset_in_pixels;
2769 if (dx > es->text_width - x_offset_in_pixels)
2770 dx = es->text_width - x_offset_in_pixels;
2771 nyoff = max(0, es->y_offset + dy);
2772 if (nyoff >= es->line_count)
2773 nyoff = es->line_count - 1;
2774 dy = (es->y_offset - nyoff) * es->line_height;
2775 if (dx || dy) {
2776 RECT rc1;
2777 RECT rc;
2779 es->y_offset = nyoff;
2780 if(es->style & ES_MULTILINE)
2781 es->x_offset += dx;
2782 else
2783 es->x_offset += dx / es->char_width;
2785 GetClientRect(es->hwndSelf, &rc1);
2786 IntersectRect(&rc, &rc1, &es->format_rect);
2787 ScrollWindowEx(es->hwndSelf, -dx, dy,
2788 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2789 /* force scroll info update */
2790 EDIT_UpdateScrollInfo(es);
2792 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2793 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2794 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2795 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2796 return TRUE;
2800 /*********************************************************************
2802 * EM_POSFROMCHAR
2805 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2807 INT len = strlenW(es->text);
2808 INT l;
2809 INT li;
2810 INT x;
2811 INT y = 0;
2812 HDC dc;
2813 HFONT old_font = 0;
2814 SIZE size;
2816 index = min(index, len);
2817 dc = GetDC(es->hwndSelf);
2818 if (es->font)
2819 old_font = SelectObject(dc, es->font);
2820 if (es->style & ES_MULTILINE) {
2821 l = EDIT_EM_LineFromChar(es, index);
2822 y = (l - es->y_offset) * es->line_height;
2823 li = EDIT_EM_LineIndex(es, l);
2824 if (after_wrap && (li == index) && l) {
2825 INT l2 = l - 1;
2826 LINEDEF *line_def = es->first_line_def;
2827 while (l2) {
2828 line_def = line_def->next;
2829 l2--;
2831 if (line_def->ending == END_WRAP) {
2832 l--;
2833 y -= es->line_height;
2834 li = EDIT_EM_LineIndex(es, l);
2837 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2838 es->tabs_count, es->tabs)) - es->x_offset;
2839 } else {
2840 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2841 if (index < es->x_offset) {
2842 GetTextExtentPoint32W(dc, text + index,
2843 es->x_offset - index, &size);
2844 x = -size.cx;
2845 } else {
2846 GetTextExtentPoint32W(dc, text + es->x_offset,
2847 index - es->x_offset, &size);
2848 x = size.cx;
2850 y = 0;
2851 if (es->style & ES_PASSWORD)
2852 HeapFree(GetProcessHeap(), 0, text);
2854 x += es->format_rect.left;
2855 y += es->format_rect.top;
2856 if (es->font)
2857 SelectObject(dc, old_font);
2858 ReleaseDC(es->hwndSelf, dc);
2859 return MAKELONG((INT16)x, (INT16)y);
2863 /*********************************************************************
2865 * EM_REPLACESEL
2867 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2870 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2872 UINT strl = strlenW(lpsz_replace);
2873 UINT tl = strlenW(es->text);
2874 UINT utl;
2875 UINT s;
2876 UINT e;
2877 UINT i;
2878 LPWSTR p;
2879 HRGN hrgn = 0;
2881 TRACE("%s, can_undo %d, send_update %d\n",
2882 debugstr_w(lpsz_replace), can_undo, send_update);
2884 s = es->selection_start;
2885 e = es->selection_end;
2887 if ((s == e) && !strl)
2888 return;
2890 ORDER_UINT(s, e);
2892 if (!EDIT_MakeFit(es, tl - (e - s) + strl, honor_limit))
2893 return;
2895 if (e != s) {
2896 /* there is something to be deleted */
2897 TRACE("deleting stuff.\n");
2898 if (can_undo) {
2899 utl = strlenW(es->undo_text);
2900 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2901 /* undo-buffer is extended to the right */
2902 EDIT_MakeUndoFit(es, utl + e - s);
2903 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2904 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2905 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2906 /* undo-buffer is extended to the left */
2907 EDIT_MakeUndoFit(es, utl + e - s);
2908 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2909 p[e - s] = p[0];
2910 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2911 p[i] = (es->text + s)[i];
2912 es->undo_position = s;
2913 } else {
2914 /* new undo-buffer */
2915 EDIT_MakeUndoFit(es, e - s);
2916 strncpyW(es->undo_text, es->text + s, e - s + 1);
2917 es->undo_text[e - s] = 0; /* ensure 0 termination */
2918 es->undo_position = s;
2920 /* any deletion makes the old insertion-undo invalid */
2921 es->undo_insert_count = 0;
2922 } else
2923 EDIT_EM_EmptyUndoBuffer(es);
2925 /* now delete */
2926 strcpyW(es->text + s, es->text + e);
2928 if (strl) {
2929 /* there is an insertion */
2930 if (can_undo) {
2931 if ((s == es->undo_position) ||
2932 ((es->undo_insert_count) &&
2933 (s == es->undo_position + es->undo_insert_count)))
2935 * insertion is new and at delete position or
2936 * an extension to either left or right
2938 es->undo_insert_count += strl;
2939 else {
2940 /* new insertion undo */
2941 es->undo_position = s;
2942 es->undo_insert_count = strl;
2943 /* new insertion makes old delete-buffer invalid */
2944 *es->undo_text = '\0';
2946 } else
2947 EDIT_EM_EmptyUndoBuffer(es);
2949 /* now insert */
2950 tl = strlenW(es->text);
2951 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));
2952 for (p = es->text + tl ; p >= es->text + s ; p--)
2953 p[strl] = p[0];
2954 for (i = 0 , p = es->text + s ; i < strl ; i++)
2955 p[i] = lpsz_replace[i];
2956 if(es->style & ES_UPPERCASE)
2957 CharUpperBuffW(p, strl);
2958 else if(es->style & ES_LOWERCASE)
2959 CharLowerBuffW(p, strl);
2960 s += strl;
2962 if (es->style & ES_MULTILINE)
2964 INT s = min(es->selection_start, es->selection_end);
2966 hrgn = CreateRectRgn(0, 0, 0, 0);
2967 EDIT_BuildLineDefs_ML(es, s, s + strl,
2968 strl - abs(es->selection_end - es->selection_start), hrgn);
2970 else
2971 EDIT_CalcLineWidth_SL(es);
2973 EDIT_EM_SetSel(es, s, s, FALSE);
2974 es->flags |= EF_MODIFIED;
2975 if (send_update) es->flags |= EF_UPDATE;
2976 EDIT_EM_ScrollCaret(es);
2978 /* force scroll info update */
2979 EDIT_UpdateScrollInfo(es);
2981 if (hrgn)
2983 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2984 DeleteObject(hrgn);
2986 else
2987 EDIT_UpdateText(es, NULL, TRUE);
2989 if(es->flags & EF_UPDATE)
2991 es->flags &= ~EF_UPDATE;
2992 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
2997 /*********************************************************************
2999 * EM_SCROLL
3002 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3004 INT dy;
3006 if (!(es->style & ES_MULTILINE))
3007 return (LRESULT)FALSE;
3009 dy = 0;
3011 switch (action) {
3012 case SB_LINEUP:
3013 if (es->y_offset)
3014 dy = -1;
3015 break;
3016 case SB_LINEDOWN:
3017 if (es->y_offset < es->line_count - 1)
3018 dy = 1;
3019 break;
3020 case SB_PAGEUP:
3021 if (es->y_offset)
3022 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3023 break;
3024 case SB_PAGEDOWN:
3025 if (es->y_offset < es->line_count - 1)
3026 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3027 break;
3028 default:
3029 return (LRESULT)FALSE;
3031 if (dy) {
3032 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3033 /* check if we are going to move too far */
3034 if(es->y_offset + dy > es->line_count - vlc)
3035 dy = es->line_count - vlc - es->y_offset;
3037 /* Notification is done in EDIT_EM_LineScroll */
3038 if(dy)
3039 EDIT_EM_LineScroll(es, 0, dy);
3041 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3045 /*********************************************************************
3047 * EM_SCROLLCARET
3050 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3052 if (es->style & ES_MULTILINE) {
3053 INT l;
3054 INT li;
3055 INT vlc;
3056 INT ww;
3057 INT cw = es->char_width;
3058 INT x;
3059 INT dy = 0;
3060 INT dx = 0;
3062 l = EDIT_EM_LineFromChar(es, es->selection_end);
3063 li = EDIT_EM_LineIndex(es, l);
3064 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3065 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3066 if (l >= es->y_offset + vlc)
3067 dy = l - vlc + 1 - es->y_offset;
3068 if (l < es->y_offset)
3069 dy = l - es->y_offset;
3070 ww = es->format_rect.right - es->format_rect.left;
3071 if (x < es->format_rect.left)
3072 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3073 if (x > es->format_rect.right)
3074 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3075 if (dy || dx)
3077 /* check if we are going to move too far */
3078 if(es->x_offset + dx + ww > es->text_width)
3079 dx = es->text_width - ww - es->x_offset;
3080 if(dx || dy)
3081 EDIT_EM_LineScroll_internal(es, dx, dy);
3083 } else {
3084 INT x;
3085 INT goal;
3086 INT format_width;
3088 if (!(es->style & ES_AUTOHSCROLL))
3089 return;
3091 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3092 format_width = es->format_rect.right - es->format_rect.left;
3093 if (x < es->format_rect.left) {
3094 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3095 do {
3096 es->x_offset--;
3097 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3098 } while ((x < goal) && es->x_offset);
3099 /* FIXME: use ScrollWindow() somehow to improve performance */
3100 EDIT_UpdateText(es, NULL, TRUE);
3101 } else if (x > es->format_rect.right) {
3102 INT x_last;
3103 INT len = strlenW(es->text);
3104 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3105 do {
3106 es->x_offset++;
3107 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3108 x_last = SLOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3109 } while ((x > goal) && (x_last > es->format_rect.right));
3110 /* FIXME: use ScrollWindow() somehow to improve performance */
3111 EDIT_UpdateText(es, NULL, TRUE);
3115 if(es->flags & EF_FOCUSED)
3116 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3120 /*********************************************************************
3122 * EM_SETHANDLE
3124 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3127 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3129 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3131 if (!(es->style & ES_MULTILINE))
3132 return;
3134 if (!hloc) {
3135 WARN("called with NULL handle\n");
3136 return;
3139 EDIT_UnlockBuffer(es, TRUE);
3141 if(es->hloc16)
3143 LOCAL_Free(hInstance, es->hloc16);
3144 es->hloc16 = (HLOCAL16)NULL;
3147 if(es->is_unicode)
3149 if(es->hloc32A)
3151 LocalFree(es->hloc32A);
3152 es->hloc32A = NULL;
3154 es->hloc32W = hloc;
3156 else
3158 INT countW, countA;
3159 HLOCAL hloc32W_new;
3160 WCHAR *textW;
3161 CHAR *textA;
3163 countA = LocalSize(hloc);
3164 textA = LocalLock(hloc);
3165 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3166 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3168 ERR("Could not allocate new unicode buffer\n");
3169 return;
3171 textW = LocalLock(hloc32W_new);
3172 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3173 LocalUnlock(hloc32W_new);
3174 LocalUnlock(hloc);
3176 if(es->hloc32W)
3177 LocalFree(es->hloc32W);
3179 es->hloc32W = hloc32W_new;
3180 es->hloc32A = hloc;
3183 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3185 EDIT_LockBuffer(es);
3187 es->x_offset = es->y_offset = 0;
3188 es->selection_start = es->selection_end = 0;
3189 EDIT_EM_EmptyUndoBuffer(es);
3190 es->flags &= ~EF_MODIFIED;
3191 es->flags &= ~EF_UPDATE;
3192 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3193 EDIT_UpdateText(es, NULL, TRUE);
3194 EDIT_EM_ScrollCaret(es);
3195 /* force scroll info update */
3196 EDIT_UpdateScrollInfo(es);
3200 /*********************************************************************
3202 * EM_SETHANDLE16
3204 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3207 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3209 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3210 INT countW, countA;
3211 HLOCAL hloc32W_new;
3212 WCHAR *textW;
3213 CHAR *textA;
3215 if (!(es->style & ES_MULTILINE))
3216 return;
3218 if (!hloc) {
3219 WARN("called with NULL handle\n");
3220 return;
3223 EDIT_UnlockBuffer(es, TRUE);
3225 if(es->hloc32A)
3227 LocalFree(es->hloc32A);
3228 es->hloc32A = NULL;
3231 countA = LOCAL_Size(hInstance, hloc);
3232 textA = LOCAL_Lock(hInstance, hloc);
3233 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3234 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3236 ERR("Could not allocate new unicode buffer\n");
3237 return;
3239 textW = LocalLock(hloc32W_new);
3240 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3241 LocalUnlock(hloc32W_new);
3242 LOCAL_Unlock(hInstance, hloc);
3244 if(es->hloc32W)
3245 LocalFree(es->hloc32W);
3247 es->hloc32W = hloc32W_new;
3248 es->hloc16 = hloc;
3250 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3252 EDIT_LockBuffer(es);
3254 es->x_offset = es->y_offset = 0;
3255 es->selection_start = es->selection_end = 0;
3256 EDIT_EM_EmptyUndoBuffer(es);
3257 es->flags &= ~EF_MODIFIED;
3258 es->flags &= ~EF_UPDATE;
3259 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3260 EDIT_UpdateText(es, NULL, TRUE);
3261 EDIT_EM_ScrollCaret(es);
3262 /* force scroll info update */
3263 EDIT_UpdateScrollInfo(es);
3267 /*********************************************************************
3269 * EM_SETLIMITTEXT
3271 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3272 * However, the windows version is not complied to yet in all of edit.c
3274 * Additionally as the wrapper for RichEdit controls we need larger buffers
3275 * at present -1 will represent nolimit
3277 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3279 if (limit == 0xFFFFFFFF)
3280 es->buffer_limit = -1;
3281 else if (es->style & ES_MULTILINE) {
3282 if (limit)
3283 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3284 else
3285 es->buffer_limit = BUFLIMIT_MULTI;
3286 } else {
3287 if (limit)
3288 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3289 else
3290 es->buffer_limit = BUFLIMIT_SINGLE;
3295 /*********************************************************************
3297 * EM_SETMARGINS
3299 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3300 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3301 * margin according to the textmetrics of the current font.
3303 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3304 * of the char's width as the margin, but this is not how Windows handles this.
3305 * For all other fonts Windows sets the margins to zero.
3308 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3309 INT left, INT right)
3311 TEXTMETRICW tm;
3312 INT default_left_margin = 0; /* in pixels */
3313 INT default_right_margin = 0; /* in pixels */
3315 /* Set the default margins depending on the font */
3316 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3317 HDC dc = GetDC(es->hwndSelf);
3318 HFONT old_font = SelectObject(dc, es->font);
3319 GetTextMetricsW(dc, &tm);
3320 /* The default margins are only non zero for TrueType or Vector fonts */
3321 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3322 /* This must be calculated more exactly! But how? */
3323 default_left_margin = tm.tmAveCharWidth / 3;
3324 default_right_margin = tm.tmAveCharWidth / 3;
3326 SelectObject(dc, old_font);
3327 ReleaseDC(es->hwndSelf, dc);
3330 if (action & EC_LEFTMARGIN) {
3331 if (left != EC_USEFONTINFO)
3332 es->left_margin = left;
3333 else
3334 es->left_margin = default_left_margin;
3337 if (action & EC_RIGHTMARGIN) {
3338 if (right != EC_USEFONTINFO)
3339 es->right_margin = right;
3340 else
3341 es->right_margin = default_right_margin;
3343 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3347 /*********************************************************************
3349 * EM_SETPASSWORDCHAR
3352 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3354 LONG style;
3356 if (es->style & ES_MULTILINE)
3357 return;
3359 if (es->password_char == c)
3360 return;
3362 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3363 es->password_char = c;
3364 if (c) {
3365 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3366 es->style |= ES_PASSWORD;
3367 } else {
3368 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3369 es->style &= ~ES_PASSWORD;
3371 EDIT_UpdateText(es, NULL, TRUE);
3375 /*********************************************************************
3377 * EDIT_EM_SetSel
3379 * note: unlike the specs say: the order of start and end
3380 * _is_ preserved in Windows. (i.e. start can be > end)
3381 * In other words: this handler is OK
3384 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3386 UINT old_start = es->selection_start;
3387 UINT old_end = es->selection_end;
3388 UINT len = strlenW(es->text);
3390 if (start == (UINT)-1) {
3391 start = es->selection_end;
3392 end = es->selection_end;
3393 } else {
3394 start = min(start, len);
3395 end = min(end, len);
3397 es->selection_start = start;
3398 es->selection_end = end;
3399 if (after_wrap)
3400 es->flags |= EF_AFTER_WRAP;
3401 else
3402 es->flags &= ~EF_AFTER_WRAP;
3403 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3404 ORDER_UINT(start, end);
3405 ORDER_UINT(end, old_end);
3406 ORDER_UINT(start, old_start);
3407 ORDER_UINT(old_start, old_end);
3408 if (end != old_start)
3411 * One can also do
3412 * ORDER_UINT32(end, old_start);
3413 * EDIT_InvalidateText(es, start, end);
3414 * EDIT_InvalidateText(es, old_start, old_end);
3415 * in place of the following if statement.
3417 if (old_start > end )
3419 EDIT_InvalidateText(es, start, end);
3420 EDIT_InvalidateText(es, old_start, old_end);
3422 else
3424 EDIT_InvalidateText(es, start, old_start);
3425 EDIT_InvalidateText(es, end, old_end);
3428 else EDIT_InvalidateText(es, start, old_end);
3432 /*********************************************************************
3434 * EM_SETTABSTOPS
3437 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3439 if (!(es->style & ES_MULTILINE))
3440 return FALSE;
3441 if (es->tabs)
3442 HeapFree(GetProcessHeap(), 0, es->tabs);
3443 es->tabs_count = count;
3444 if (!count)
3445 es->tabs = NULL;
3446 else {
3447 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3448 memcpy(es->tabs, tabs, count * sizeof(INT));
3450 return TRUE;
3454 /*********************************************************************
3456 * EM_SETTABSTOPS16
3459 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3461 if (!(es->style & ES_MULTILINE))
3462 return FALSE;
3463 if (es->tabs)
3464 HeapFree(GetProcessHeap(), 0, es->tabs);
3465 es->tabs_count = count;
3466 if (!count)
3467 es->tabs = NULL;
3468 else {
3469 INT i;
3470 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3471 for (i = 0 ; i < count ; i++)
3472 es->tabs[i] = *tabs++;
3474 return TRUE;
3478 /*********************************************************************
3480 * EM_SETWORDBREAKPROC
3483 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam)
3485 if (es->word_break_proc == (void *)lParam)
3486 return;
3488 es->word_break_proc = (void *)lParam;
3489 es->word_break_proc16 = NULL;
3491 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3492 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3493 EDIT_UpdateText(es, NULL, TRUE);
3498 /*********************************************************************
3500 * EM_SETWORDBREAKPROC16
3503 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3505 if (es->word_break_proc16 == wbp)
3506 return;
3508 es->word_break_proc = NULL;
3509 es->word_break_proc16 = wbp;
3510 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3511 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3512 EDIT_UpdateText(es, NULL, TRUE);
3517 /*********************************************************************
3519 * EM_UNDO / WM_UNDO
3522 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3524 INT ulength;
3525 LPWSTR utext;
3527 /* Protect read-only edit control from modification */
3528 if(es->style & ES_READONLY)
3529 return FALSE;
3531 ulength = strlenW(es->undo_text);
3532 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3534 strcpyW(utext, es->undo_text);
3536 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3537 es->undo_insert_count, debugstr_w(utext));
3539 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3540 EDIT_EM_EmptyUndoBuffer(es);
3541 EDIT_EM_ReplaceSel(es, TRUE, utext, FALSE, TRUE);
3542 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3543 /* send the notification after the selection start and end are set */
3544 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3545 EDIT_EM_ScrollCaret(es);
3546 HeapFree(GetProcessHeap(), 0, utext);
3548 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3549 es->undo_insert_count, debugstr_w(es->undo_text));
3550 return TRUE;
3554 /*********************************************************************
3556 * WM_CHAR
3559 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3561 BOOL control;
3563 /* Protect read-only edit control from modification */
3564 if(es->style & ES_READONLY)
3565 return;
3567 control = GetKeyState(VK_CONTROL) & 0x8000;
3569 switch (c) {
3570 case '\r':
3571 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3572 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3573 break;
3574 case '\n':
3575 if (es->style & ES_MULTILINE) {
3576 if (es->style & ES_READONLY) {
3577 EDIT_MoveHome(es, FALSE);
3578 EDIT_MoveDown_ML(es, FALSE);
3579 } else {
3580 static const WCHAR cr_lfW[] = {'\r','\n',0};
3581 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3584 break;
3585 case '\t':
3586 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3588 static const WCHAR tabW[] = {'\t',0};
3589 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3591 break;
3592 case VK_BACK:
3593 if (!(es->style & ES_READONLY) && !control) {
3594 if (es->selection_start != es->selection_end)
3595 EDIT_WM_Clear(es);
3596 else {
3597 /* delete character left of caret */
3598 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3599 EDIT_MoveBackward(es, TRUE);
3600 EDIT_WM_Clear(es);
3603 break;
3604 case 0x03: /* ^C */
3605 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3606 break;
3607 case 0x16: /* ^V */
3608 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3609 break;
3610 case 0x18: /* ^X */
3611 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3612 break;
3614 default:
3615 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3616 WCHAR str[2];
3617 str[0] = c;
3618 str[1] = '\0';
3619 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3621 break;
3626 /*********************************************************************
3628 * WM_COMMAND
3631 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3633 if (code || control)
3634 return;
3636 switch (id) {
3637 case EM_UNDO:
3638 EDIT_EM_Undo(es);
3639 break;
3640 case WM_CUT:
3641 EDIT_WM_Cut(es);
3642 break;
3643 case WM_COPY:
3644 EDIT_WM_Copy(es);
3645 break;
3646 case WM_PASTE:
3647 EDIT_WM_Paste(es);
3648 break;
3649 case WM_CLEAR:
3650 EDIT_WM_Clear(es);
3651 break;
3652 case EM_SETSEL:
3653 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3654 EDIT_EM_ScrollCaret(es);
3655 break;
3656 default:
3657 ERR("unknown menu item, please report\n");
3658 break;
3663 /*********************************************************************
3665 * WM_CONTEXTMENU
3667 * Note: the resource files resource/sysres_??.rc cannot define a
3668 * single popup menu. Hence we use a (dummy) menubar
3669 * containing the single popup menu as its first item.
3671 * FIXME: the message identifiers have been chosen arbitrarily,
3672 * hence we use MF_BYPOSITION.
3673 * We might as well use the "real" values (anybody knows ?)
3674 * The menu definition is in resources/sysres_??.rc.
3675 * Once these are OK, we better use MF_BYCOMMAND here
3676 * (as we do in EDIT_WM_Command()).
3679 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3681 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3682 HMENU popup = GetSubMenu(menu, 0);
3683 UINT start = es->selection_start;
3684 UINT end = es->selection_end;
3686 ORDER_UINT(start, end);
3688 /* undo */
3689 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3690 /* cut */
3691 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3692 /* copy */
3693 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3694 /* paste */
3695 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3696 /* delete */
3697 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3698 /* select all */
3699 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3701 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3702 DestroyMenu(menu);
3706 /*********************************************************************
3708 * WM_COPY
3711 static void EDIT_WM_Copy(EDITSTATE *es)
3713 INT s = min(es->selection_start, es->selection_end);
3714 INT e = max(es->selection_start, es->selection_end);
3715 HGLOBAL hdst;
3716 LPWSTR dst;
3718 if (e == s) return;
3720 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3721 dst = GlobalLock(hdst);
3722 strncpyW(dst, es->text + s, e - s);
3723 dst[e - s] = 0; /* ensure 0 termination */
3724 TRACE("%s\n", debugstr_w(dst));
3725 GlobalUnlock(hdst);
3726 OpenClipboard(es->hwndSelf);
3727 EmptyClipboard();
3728 SetClipboardData(CF_UNICODETEXT, hdst);
3729 CloseClipboard();
3733 /*********************************************************************
3735 * WM_CREATE
3738 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3740 TRACE("%s\n", debugstr_w(name));
3742 * To initialize some final structure members, we call some helper
3743 * functions. However, since the EDITSTATE is not consistent (i.e.
3744 * not fully initialized), we should be very careful which
3745 * functions can be called, and in what order.
3747 EDIT_WM_SetFont(es, 0, FALSE);
3748 EDIT_EM_EmptyUndoBuffer(es);
3750 if (name && *name) {
3751 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3752 /* if we insert text to the editline, the text scrolls out
3753 * of the window, as the caret is placed after the insert
3754 * pos normally; thus we reset es->selection... to 0 and
3755 * update caret
3757 es->selection_start = es->selection_end = 0;
3758 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3759 * Messages are only to be sent when the USER does something to
3760 * change the contents. So I am removing this EN_CHANGE
3762 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3764 EDIT_EM_ScrollCaret(es);
3766 /* force scroll info update */
3767 EDIT_UpdateScrollInfo(es);
3768 return 0;
3772 /*********************************************************************
3774 * WM_DESTROY
3777 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3779 LINEDEF *pc, *pp;
3781 if (es->hloc32W) {
3782 while (LocalUnlock(es->hloc32W)) ;
3783 LocalFree(es->hloc32W);
3785 if (es->hloc32A) {
3786 while (LocalUnlock(es->hloc32A)) ;
3787 LocalFree(es->hloc32A);
3789 if (es->hloc16) {
3790 HINSTANCE16 hInstance = GetWindowWord( es->hwndSelf, GWL_HINSTANCE );
3791 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3792 LOCAL_Free(hInstance, es->hloc16);
3795 pc = es->first_line_def;
3796 while (pc)
3798 pp = pc->next;
3799 HeapFree(GetProcessHeap(), 0, pc);
3800 pc = pp;
3803 SetWindowLongW( es->hwndSelf, 0, 0 );
3804 HeapFree(GetProcessHeap(), 0, es);
3806 return 0;
3810 /*********************************************************************
3812 * WM_ERASEBKGND
3815 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3817 HBRUSH brush;
3818 RECT rc;
3820 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
3821 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3823 GetClientRect(es->hwndSelf, &rc);
3824 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3825 GetClipBox(dc, &rc);
3827 * FIXME: specs say that we should UnrealizeObject() the brush,
3828 * but the specs of UnrealizeObject() say that we shouldn't
3829 * unrealize a stock object. The default brush that
3830 * DefWndProc() returns is ... a stock object.
3832 FillRect(dc, &rc, brush);
3833 return -1;
3837 /*********************************************************************
3839 * WM_GETTEXT
3842 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3844 if(!count) return 0;
3846 if(unicode)
3848 LPWSTR textW = (LPWSTR)lParam;
3849 lstrcpynW(textW, es->text, count);
3850 return strlenW(textW);
3852 else
3854 LPSTR textA = (LPSTR)lParam;
3855 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3856 textA[count - 1] = 0; /* ensure 0 termination */
3857 return strlen(textA);
3861 /*********************************************************************
3863 * WM_HSCROLL
3866 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3868 INT dx;
3869 INT fw;
3871 if (!(es->style & ES_MULTILINE))
3872 return 0;
3874 if (!(es->style & ES_AUTOHSCROLL))
3875 return 0;
3877 dx = 0;
3878 fw = es->format_rect.right - es->format_rect.left;
3879 switch (action) {
3880 case SB_LINELEFT:
3881 TRACE("SB_LINELEFT\n");
3882 if (es->x_offset)
3883 dx = -es->char_width;
3884 break;
3885 case SB_LINERIGHT:
3886 TRACE("SB_LINERIGHT\n");
3887 if (es->x_offset < es->text_width)
3888 dx = es->char_width;
3889 break;
3890 case SB_PAGELEFT:
3891 TRACE("SB_PAGELEFT\n");
3892 if (es->x_offset)
3893 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3894 break;
3895 case SB_PAGERIGHT:
3896 TRACE("SB_PAGERIGHT\n");
3897 if (es->x_offset < es->text_width)
3898 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3899 break;
3900 case SB_LEFT:
3901 TRACE("SB_LEFT\n");
3902 if (es->x_offset)
3903 dx = -es->x_offset;
3904 break;
3905 case SB_RIGHT:
3906 TRACE("SB_RIGHT\n");
3907 if (es->x_offset < es->text_width)
3908 dx = es->text_width - es->x_offset;
3909 break;
3910 case SB_THUMBTRACK:
3911 TRACE("SB_THUMBTRACK %d\n", pos);
3912 es->flags |= EF_HSCROLL_TRACK;
3913 if(es->style & WS_HSCROLL)
3914 dx = pos - es->x_offset;
3915 else
3917 INT fw, new_x;
3918 /* Sanity check */
3919 if(pos < 0 || pos > 100) return 0;
3920 /* Assume default scroll range 0-100 */
3921 fw = es->format_rect.right - es->format_rect.left;
3922 new_x = pos * (es->text_width - fw) / 100;
3923 dx = es->text_width ? (new_x - es->x_offset) : 0;
3925 break;
3926 case SB_THUMBPOSITION:
3927 TRACE("SB_THUMBPOSITION %d\n", pos);
3928 es->flags &= ~EF_HSCROLL_TRACK;
3929 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3930 dx = pos - es->x_offset;
3931 else
3933 INT fw, new_x;
3934 /* Sanity check */
3935 if(pos < 0 || pos > 100) return 0;
3936 /* Assume default scroll range 0-100 */
3937 fw = es->format_rect.right - es->format_rect.left;
3938 new_x = pos * (es->text_width - fw) / 100;
3939 dx = es->text_width ? (new_x - es->x_offset) : 0;
3941 if (!dx) {
3942 /* force scroll info update */
3943 EDIT_UpdateScrollInfo(es);
3944 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
3946 break;
3947 case SB_ENDSCROLL:
3948 TRACE("SB_ENDSCROLL\n");
3949 break;
3951 * FIXME : the next two are undocumented !
3952 * Are we doing the right thing ?
3953 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3954 * although it's also a regular control message.
3956 case EM_GETTHUMB: /* this one is used by NT notepad */
3957 case EM_GETTHUMB16:
3959 LRESULT ret;
3960 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3961 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3962 else
3964 /* Assume default scroll range 0-100 */
3965 INT fw = es->format_rect.right - es->format_rect.left;
3966 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
3968 TRACE("EM_GETTHUMB: returning %ld\n", ret);
3969 return ret;
3971 case EM_LINESCROLL16:
3972 TRACE("EM_LINESCROLL16\n");
3973 dx = pos;
3974 break;
3976 default:
3977 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
3978 action, action);
3979 return 0;
3981 if (dx)
3983 INT fw = es->format_rect.right - es->format_rect.left;
3984 /* check if we are going to move too far */
3985 if(es->x_offset + dx + fw > es->text_width)
3986 dx = es->text_width - fw - es->x_offset;
3987 if(dx)
3988 EDIT_EM_LineScroll_internal(es, dx, 0);
3990 return 0;
3994 /*********************************************************************
3996 * EDIT_CheckCombo
3999 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4001 HWND hLBox = es->hwndListBox;
4002 HWND hCombo;
4003 BOOL bDropped;
4004 int nEUI;
4006 if (!hLBox)
4007 return FALSE;
4009 hCombo = GetParent(es->hwndSelf);
4010 bDropped = TRUE;
4011 nEUI = 0;
4013 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4015 if (key == VK_UP || key == VK_DOWN)
4017 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4018 nEUI = 1;
4020 if (msg == WM_KEYDOWN || nEUI)
4021 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4024 switch (msg)
4026 case WM_KEYDOWN:
4027 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4029 /* make sure ComboLBox pops up */
4030 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4031 key = VK_F4;
4032 nEUI = 2;
4035 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4036 break;
4038 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4039 if (nEUI)
4040 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4041 else
4042 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4043 break;
4046 if(nEUI == 2)
4047 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4049 return TRUE;
4053 /*********************************************************************
4055 * WM_KEYDOWN
4057 * Handling of special keys that don't produce a WM_CHAR
4058 * (i.e. non-printable keys) & Backspace & Delete
4061 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4063 BOOL shift;
4064 BOOL control;
4066 if (GetKeyState(VK_MENU) & 0x8000)
4067 return 0;
4069 shift = GetKeyState(VK_SHIFT) & 0x8000;
4070 control = GetKeyState(VK_CONTROL) & 0x8000;
4072 switch (key) {
4073 case VK_F4:
4074 case VK_UP:
4075 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4076 break;
4078 /* fall through */
4079 case VK_LEFT:
4080 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4081 EDIT_MoveUp_ML(es, shift);
4082 else
4083 if (control)
4084 EDIT_MoveWordBackward(es, shift);
4085 else
4086 EDIT_MoveBackward(es, shift);
4087 break;
4088 case VK_DOWN:
4089 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4090 break;
4091 /* fall through */
4092 case VK_RIGHT:
4093 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4094 EDIT_MoveDown_ML(es, shift);
4095 else if (control)
4096 EDIT_MoveWordForward(es, shift);
4097 else
4098 EDIT_MoveForward(es, shift);
4099 break;
4100 case VK_HOME:
4101 EDIT_MoveHome(es, shift);
4102 break;
4103 case VK_END:
4104 EDIT_MoveEnd(es, shift);
4105 break;
4106 case VK_PRIOR:
4107 if (es->style & ES_MULTILINE)
4108 EDIT_MovePageUp_ML(es, shift);
4109 else
4110 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4111 break;
4112 case VK_NEXT:
4113 if (es->style & ES_MULTILINE)
4114 EDIT_MovePageDown_ML(es, shift);
4115 else
4116 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4117 break;
4118 case VK_DELETE:
4119 if (!(es->style & ES_READONLY) && !(shift && control)) {
4120 if (es->selection_start != es->selection_end) {
4121 if (shift)
4122 EDIT_WM_Cut(es);
4123 else
4124 EDIT_WM_Clear(es);
4125 } else {
4126 if (shift) {
4127 /* delete character left of caret */
4128 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4129 EDIT_MoveBackward(es, TRUE);
4130 EDIT_WM_Clear(es);
4131 } else if (control) {
4132 /* delete to end of line */
4133 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4134 EDIT_MoveEnd(es, TRUE);
4135 EDIT_WM_Clear(es);
4136 } else {
4137 /* delete character right of caret */
4138 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4139 EDIT_MoveForward(es, TRUE);
4140 EDIT_WM_Clear(es);
4144 break;
4145 case VK_INSERT:
4146 if (shift) {
4147 if (!(es->style & ES_READONLY))
4148 EDIT_WM_Paste(es);
4149 } else if (control)
4150 EDIT_WM_Copy(es);
4151 break;
4152 case VK_RETURN:
4153 /* If the edit doesn't want the return send a message to the default object */
4154 if(!(es->style & ES_WANTRETURN))
4156 HWND hwndParent = GetParent(es->hwndSelf);
4157 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4158 if (HIWORD(dw) == DC_HASDEFID)
4160 SendMessageW( hwndParent, WM_COMMAND,
4161 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4162 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4165 break;
4167 return 0;
4171 /*********************************************************************
4173 * WM_KILLFOCUS
4176 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4178 es->flags &= ~EF_FOCUSED;
4179 DestroyCaret();
4180 if(!(es->style & ES_NOHIDESEL))
4181 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4182 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4183 return 0;
4187 /*********************************************************************
4189 * WM_LBUTTONDBLCLK
4191 * The caret position has been set on the WM_LBUTTONDOWN message
4194 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4196 INT s;
4197 INT e = es->selection_end;
4198 INT l;
4199 INT li;
4200 INT ll;
4202 if (!(es->flags & EF_FOCUSED))
4203 return 0;
4205 l = EDIT_EM_LineFromChar(es, e);
4206 li = EDIT_EM_LineIndex(es, l);
4207 ll = EDIT_EM_LineLength(es, e);
4208 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4209 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4210 EDIT_EM_SetSel(es, s, e, FALSE);
4211 EDIT_EM_ScrollCaret(es);
4212 return 0;
4216 /*********************************************************************
4218 * WM_LBUTTONDOWN
4221 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4223 INT e;
4224 BOOL after_wrap;
4226 if (!(es->flags & EF_FOCUSED))
4227 return 0;
4229 es->bCaptureState = TRUE;
4230 SetCapture(es->hwndSelf);
4231 EDIT_ConfinePoint(es, &x, &y);
4232 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4233 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4234 EDIT_EM_ScrollCaret(es);
4235 es->region_posx = es->region_posy = 0;
4236 SetTimer(es->hwndSelf, 0, 100, NULL);
4237 return 0;
4241 /*********************************************************************
4243 * WM_LBUTTONUP
4246 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4248 if (es->bCaptureState) {
4249 KillTimer(es->hwndSelf, 0);
4250 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4252 es->bCaptureState = FALSE;
4253 return 0;
4257 /*********************************************************************
4259 * WM_MBUTTONDOWN
4262 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4264 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4265 return 0;
4269 /*********************************************************************
4271 * WM_MOUSEMOVE
4274 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4276 INT e;
4277 BOOL after_wrap;
4278 INT prex, prey;
4280 if (GetCapture() != es->hwndSelf)
4281 return 0;
4284 * FIXME: gotta do some scrolling if outside client
4285 * area. Maybe reset the timer ?
4287 prex = x; prey = y;
4288 EDIT_ConfinePoint(es, &x, &y);
4289 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4290 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4291 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4292 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4293 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4294 return 0;
4298 /*********************************************************************
4300 * WM_NCCREATE
4302 * See also EDIT_WM_StyleChanged
4304 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4306 EDITSTATE *es;
4307 UINT alloc_size;
4309 TRACE("Creating %s edit control, style = %08lx\n",
4310 unicode ? "Unicode" : "ANSI", lpcs->style);
4312 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4313 return FALSE;
4314 SetWindowLongW( hwnd, 0, (LONG)es );
4317 * Note: since the EDITSTATE has not been fully initialized yet,
4318 * we can't use any API calls that may send
4319 * WM_XXX messages before WM_NCCREATE is completed.
4322 es->is_unicode = unicode;
4323 es->style = lpcs->style;
4325 es->bEnableState = !(es->style & WS_DISABLED);
4327 es->hwndSelf = hwnd;
4328 /* Save parent, which will be notified by EN_* messages */
4329 es->hwndParent = lpcs->hwndParent;
4331 if (es->style & ES_COMBO)
4332 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4334 /* Number overrides lowercase overrides uppercase (at least it
4335 * does in Win95). However I'll bet that ES_NUMBER would be
4336 * invalid under Win 3.1.
4338 if (es->style & ES_NUMBER) {
4339 ; /* do not override the ES_NUMBER */
4340 } else if (es->style & ES_LOWERCASE) {
4341 es->style &= ~ES_UPPERCASE;
4343 if (es->style & ES_MULTILINE) {
4344 es->buffer_limit = BUFLIMIT_MULTI;
4345 if (es->style & WS_VSCROLL)
4346 es->style |= ES_AUTOVSCROLL;
4347 if (es->style & WS_HSCROLL)
4348 es->style |= ES_AUTOHSCROLL;
4349 es->style &= ~ES_PASSWORD;
4350 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4351 /* Confirmed - RIGHT overrides CENTER */
4352 if (es->style & ES_RIGHT)
4353 es->style &= ~ES_CENTER;
4354 es->style &= ~WS_HSCROLL;
4355 es->style &= ~ES_AUTOHSCROLL;
4358 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4359 es->style |= ES_AUTOVSCROLL;
4360 } else {
4361 es->buffer_limit = BUFLIMIT_SINGLE;
4362 if (WIN31_LOOK == TWEAK_WineLook ||
4363 WIN95_LOOK == TWEAK_WineLook) {
4364 es->style &= ~ES_CENTER;
4365 es->style &= ~ES_RIGHT;
4366 } else {
4367 if (es->style & ES_RIGHT)
4368 es->style &= ~ES_CENTER;
4370 es->style &= ~WS_HSCROLL;
4371 es->style &= ~WS_VSCROLL;
4372 es->style &= ~ES_AUTOVSCROLL;
4373 es->style &= ~ES_WANTRETURN;
4374 if (es->style & ES_PASSWORD)
4375 es->password_char = '*';
4377 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4378 es->style |= ES_AUTOHSCROLL;
4381 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4382 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4383 return FALSE;
4384 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4386 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4387 return FALSE;
4388 es->undo_buffer_size = es->buffer_size;
4390 if (es->style & ES_MULTILINE)
4391 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4392 return FALSE;
4393 es->line_count = 1;
4396 * In Win95 look and feel, the WS_BORDER style is replaced by the
4397 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4398 * control a non client area. Not always. This coordinates in some
4399 * way with the window creation code in dialog.c When making
4400 * modifications please ensure that the code still works for edit
4401 * controls created directly with style 0x50800000, exStyle 0 (
4402 * which should have a single pixel border)
4404 if (TWEAK_WineLook != WIN31_LOOK)
4406 es->style &= ~WS_BORDER;
4408 else
4410 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4411 SetWindowLongW( hwnd, GWL_STYLE,
4412 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_BORDER );
4415 return TRUE;
4418 /*********************************************************************
4420 * WM_PAINT
4423 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam)
4425 PAINTSTRUCT ps;
4426 INT i;
4427 HDC dc;
4428 HFONT old_font = 0;
4429 RECT rc;
4430 RECT rcLine;
4431 RECT rcRgn;
4432 BOOL rev = es->bEnableState &&
4433 ((es->flags & EF_FOCUSED) ||
4434 (es->style & ES_NOHIDESEL));
4435 if (!wParam)
4436 dc = BeginPaint(es->hwndSelf, &ps);
4437 else
4438 dc = (HDC) wParam;
4439 if(es->style & WS_BORDER) {
4440 GetClientRect(es->hwndSelf, &rc);
4441 if(es->style & ES_MULTILINE) {
4442 if(es->style & WS_HSCROLL) rc.bottom++;
4443 if(es->style & WS_VSCROLL) rc.right++;
4445 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4447 IntersectClipRect(dc, es->format_rect.left,
4448 es->format_rect.top,
4449 es->format_rect.right,
4450 es->format_rect.bottom);
4451 if (es->style & ES_MULTILINE) {
4452 GetClientRect(es->hwndSelf, &rc);
4453 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4455 if (es->font)
4456 old_font = SelectObject(dc, es->font);
4457 EDIT_NotifyCtlColor(es, dc);
4459 if (!es->bEnableState)
4460 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4461 GetClipBox(dc, &rcRgn);
4462 if (es->style & ES_MULTILINE) {
4463 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4464 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4465 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4466 if (IntersectRect(&rc, &rcRgn, &rcLine))
4467 EDIT_PaintLine(es, dc, i, rev);
4469 } else {
4470 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4471 if (IntersectRect(&rc, &rcRgn, &rcLine))
4472 EDIT_PaintLine(es, dc, 0, rev);
4474 if (es->font)
4475 SelectObject(dc, old_font);
4477 if (!wParam)
4478 EndPaint(es->hwndSelf, &ps);
4482 /*********************************************************************
4484 * WM_PASTE
4487 static void EDIT_WM_Paste(EDITSTATE *es)
4489 HGLOBAL hsrc;
4490 LPWSTR src;
4492 /* Protect read-only edit control from modification */
4493 if(es->style & ES_READONLY)
4494 return;
4496 OpenClipboard(es->hwndSelf);
4497 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4498 src = (LPWSTR)GlobalLock(hsrc);
4499 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4500 GlobalUnlock(hsrc);
4502 CloseClipboard();
4506 /*********************************************************************
4508 * WM_SETFOCUS
4511 static void EDIT_WM_SetFocus(EDITSTATE *es)
4513 es->flags |= EF_FOCUSED;
4514 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4515 EDIT_SetCaretPos(es, es->selection_end,
4516 es->flags & EF_AFTER_WRAP);
4517 if(!(es->style & ES_NOHIDESEL))
4518 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4519 ShowCaret(es->hwndSelf);
4520 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4524 /*********************************************************************
4526 * WM_SETFONT
4528 * With Win95 look the margins are set to default font value unless
4529 * the system font (font == 0) is being set, in which case they are left
4530 * unchanged.
4533 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4535 TEXTMETRICW tm;
4536 HDC dc;
4537 HFONT old_font = 0;
4538 RECT r;
4540 es->font = font;
4541 dc = GetDC(es->hwndSelf);
4542 if (font)
4543 old_font = SelectObject(dc, font);
4544 GetTextMetricsW(dc, &tm);
4545 es->line_height = tm.tmHeight;
4546 es->char_width = tm.tmAveCharWidth;
4547 if (font)
4548 SelectObject(dc, old_font);
4549 ReleaseDC(es->hwndSelf, dc);
4550 if (font && (TWEAK_WineLook > WIN31_LOOK))
4551 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4552 EC_USEFONTINFO, EC_USEFONTINFO);
4554 /* Force the recalculation of the format rect for each font change */
4555 GetClientRect(es->hwndSelf, &r);
4556 EDIT_SetRectNP(es, &r);
4558 if (es->style & ES_MULTILINE)
4559 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4560 else
4561 EDIT_CalcLineWidth_SL(es);
4563 if (redraw)
4564 EDIT_UpdateText(es, NULL, TRUE);
4565 if (es->flags & EF_FOCUSED) {
4566 DestroyCaret();
4567 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4568 EDIT_SetCaretPos(es, es->selection_end,
4569 es->flags & EF_AFTER_WRAP);
4570 ShowCaret(es->hwndSelf);
4575 /*********************************************************************
4577 * WM_SETTEXT
4579 * NOTES
4580 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4581 * The modified flag is reset. No notifications are sent.
4583 * For single-line controls, reception of WM_SETTEXT triggers:
4584 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4587 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode)
4589 LPWSTR text = NULL;
4591 if(unicode)
4592 text = (LPWSTR)lParam;
4593 else if (lParam)
4595 LPCSTR textA = (LPCSTR)lParam;
4596 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4597 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4598 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4601 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4602 if (text) {
4603 TRACE("%s\n", debugstr_w(text));
4604 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4605 if(!unicode)
4606 HeapFree(GetProcessHeap(), 0, text);
4607 } else {
4608 static const WCHAR empty_stringW[] = {0};
4609 TRACE("<NULL>\n");
4610 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4612 es->x_offset = 0;
4613 es->flags &= ~EF_MODIFIED;
4614 EDIT_EM_SetSel(es, 0, 0, FALSE);
4615 /* Send the notification after the selection start and end have been set
4616 * edit control doesn't send notification on WM_SETTEXT
4617 * if it is multiline, or it is part of combobox
4619 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4621 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4622 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4624 EDIT_EM_ScrollCaret(es);
4628 /*********************************************************************
4630 * WM_SIZE
4633 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4635 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4636 RECT rc;
4637 TRACE("width = %d, height = %d\n", width, height);
4638 SetRect(&rc, 0, 0, width, height);
4639 EDIT_SetRectNP(es, &rc);
4640 EDIT_UpdateText(es, NULL, TRUE);
4645 /*********************************************************************
4647 * WM_STYLECHANGED
4649 * This message is sent by SetWindowLong on having changed either the Style
4650 * or the extended style.
4652 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4654 * See also EDIT_WM_NCCreate
4656 * It appears that the Windows version of the edit control allows the style
4657 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4658 * style variable which will generally be different. In this function we
4659 * update the internal style based on what changed in the externally visible
4660 * style.
4662 * Much of this content as based upon the MSDN, especially:
4663 * Platform SDK Documentation -> User Interface Services ->
4664 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4665 * Edit Control Styles
4667 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4669 if (GWL_STYLE == which) {
4670 DWORD style_change_mask;
4671 DWORD new_style;
4672 /* Only a subset of changes can be applied after the control
4673 * has been created.
4675 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4676 ES_NUMBER;
4677 if (es->style & ES_MULTILINE)
4678 style_change_mask |= ES_WANTRETURN;
4680 new_style = style->styleNew & style_change_mask;
4682 /* Number overrides lowercase overrides uppercase (at least it
4683 * does in Win95). However I'll bet that ES_NUMBER would be
4684 * invalid under Win 3.1.
4686 if (new_style & ES_NUMBER) {
4687 ; /* do not override the ES_NUMBER */
4688 } else if (new_style & ES_LOWERCASE) {
4689 new_style &= ~ES_UPPERCASE;
4692 es->style = (es->style & ~style_change_mask) | new_style;
4693 } else if (GWL_EXSTYLE == which) {
4694 ; /* FIXME - what is needed here */
4695 } else {
4696 WARN ("Invalid style change %d\n",which);
4699 return 0;
4702 /*********************************************************************
4704 * WM_SYSKEYDOWN
4707 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4709 if ((key == VK_BACK) && (key_data & 0x2000)) {
4710 if (EDIT_EM_CanUndo(es))
4711 EDIT_EM_Undo(es);
4712 return 0;
4713 } else if (key == VK_UP || key == VK_DOWN) {
4714 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4715 return 0;
4717 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4721 /*********************************************************************
4723 * WM_TIMER
4726 static void EDIT_WM_Timer(EDITSTATE *es)
4728 if (es->region_posx < 0) {
4729 EDIT_MoveBackward(es, TRUE);
4730 } else if (es->region_posx > 0) {
4731 EDIT_MoveForward(es, TRUE);
4734 * FIXME: gotta do some vertical scrolling here, like
4735 * EDIT_EM_LineScroll(hwnd, 0, 1);
4739 /*********************************************************************
4741 * WM_VSCROLL
4744 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4746 INT dy;
4748 if (!(es->style & ES_MULTILINE))
4749 return 0;
4751 if (!(es->style & ES_AUTOVSCROLL))
4752 return 0;
4754 dy = 0;
4755 switch (action) {
4756 case SB_LINEUP:
4757 case SB_LINEDOWN:
4758 case SB_PAGEUP:
4759 case SB_PAGEDOWN:
4760 TRACE("action %d\n", action);
4761 EDIT_EM_Scroll(es, action);
4762 return 0;
4763 case SB_TOP:
4764 TRACE("SB_TOP\n");
4765 dy = -es->y_offset;
4766 break;
4767 case SB_BOTTOM:
4768 TRACE("SB_BOTTOM\n");
4769 dy = es->line_count - 1 - es->y_offset;
4770 break;
4771 case SB_THUMBTRACK:
4772 TRACE("SB_THUMBTRACK %d\n", pos);
4773 es->flags |= EF_VSCROLL_TRACK;
4774 if(es->style & WS_VSCROLL)
4775 dy = pos - es->y_offset;
4776 else
4778 /* Assume default scroll range 0-100 */
4779 INT vlc, new_y;
4780 /* Sanity check */
4781 if(pos < 0 || pos > 100) return 0;
4782 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4783 new_y = pos * (es->line_count - vlc) / 100;
4784 dy = es->line_count ? (new_y - es->y_offset) : 0;
4785 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4786 es->line_count, es->y_offset, pos, dy);
4788 break;
4789 case SB_THUMBPOSITION:
4790 TRACE("SB_THUMBPOSITION %d\n", pos);
4791 es->flags &= ~EF_VSCROLL_TRACK;
4792 if(es->style & WS_VSCROLL)
4793 dy = pos - es->y_offset;
4794 else
4796 /* Assume default scroll range 0-100 */
4797 INT vlc, new_y;
4798 /* Sanity check */
4799 if(pos < 0 || pos > 100) return 0;
4800 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4801 new_y = pos * (es->line_count - vlc) / 100;
4802 dy = es->line_count ? (new_y - es->y_offset) : 0;
4803 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4804 es->line_count, es->y_offset, pos, dy);
4806 if (!dy)
4808 /* force scroll info update */
4809 EDIT_UpdateScrollInfo(es);
4810 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4812 break;
4813 case SB_ENDSCROLL:
4814 TRACE("SB_ENDSCROLL\n");
4815 break;
4817 * FIXME : the next two are undocumented !
4818 * Are we doing the right thing ?
4819 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4820 * although it's also a regular control message.
4822 case EM_GETTHUMB: /* this one is used by NT notepad */
4823 case EM_GETTHUMB16:
4825 LRESULT ret;
4826 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4827 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4828 else
4830 /* Assume default scroll range 0-100 */
4831 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4832 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4834 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4835 return ret;
4837 case EM_LINESCROLL16:
4838 TRACE("EM_LINESCROLL16 %d\n", pos);
4839 dy = pos;
4840 break;
4842 default:
4843 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4844 action, action);
4845 return 0;
4847 if (dy)
4848 EDIT_EM_LineScroll(es, 0, dy);
4849 return 0;
4852 /*********************************************************************
4854 * EDIT_UpdateText
4857 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4859 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4860 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4864 /*********************************************************************
4866 * EDIT_UpdateText
4869 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4871 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4872 InvalidateRect(es->hwndSelf, rc, bErase);