Separate Win16 and Win32 implementations in memlockbytes.
[wine.git] / controls / edit.c
blob7ce698b24ecd3c93b9bfa8c655bfdf95828dfd74
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;
748 if( es->hwndListBox )
750 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
752 int vk = (int)((LPMSG)lParam)->wParam;
753 if( vk == VK_RETURN || vk == VK_ESCAPE)
754 if( SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
755 result |= DLGC_WANTMESSAGE;
757 } else
758 /* It seems in all other cases Windows has this set: */
759 result |= DLGC_WANTMESSAGE;
760 break;
762 case WM_IME_CHAR:
763 if (!unicode)
765 WCHAR charW;
766 CHAR strng[2];
768 strng[0] = wParam >> 8;
769 strng[1] = wParam & 0xff;
770 MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
771 EDIT_WM_Char(es, charW);
772 break;
774 /* fall through */
775 case WM_CHAR:
777 WCHAR charW;
779 if(unicode)
780 charW = wParam;
781 else
783 CHAR charA = wParam;
784 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
787 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
789 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
790 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
791 break;
793 EDIT_WM_Char(es, charW);
794 break;
797 case WM_CLEAR:
798 EDIT_WM_Clear(es);
799 break;
801 case WM_COMMAND:
802 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
803 break;
805 case WM_CONTEXTMENU:
806 EDIT_WM_ContextMenu(es, SLOWORD(lParam), SHIWORD(lParam));
807 break;
809 case WM_COPY:
810 EDIT_WM_Copy(es);
811 break;
813 case WM_CREATE:
814 if(unicode)
815 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
816 else
818 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
819 LPWSTR nameW = NULL;
820 if(nameA)
822 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
823 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
824 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
826 result = EDIT_WM_Create(es, nameW);
827 if(nameW)
828 HeapFree(GetProcessHeap(), 0, nameW);
830 break;
832 case WM_CUT:
833 EDIT_WM_Cut(es);
834 break;
836 case WM_ENABLE:
837 es->bEnableState = (BOOL) wParam;
838 EDIT_UpdateText(es, NULL, TRUE);
839 break;
841 case WM_ERASEBKGND:
842 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
843 break;
845 case WM_GETFONT:
846 result = (LRESULT)es->font;
847 break;
849 case WM_GETTEXT:
850 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
851 break;
853 case WM_GETTEXTLENGTH:
854 if (unicode) result = strlenW(es->text);
855 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
856 NULL, 0, NULL, NULL );
857 break;
859 case WM_HSCROLL:
860 result = EDIT_WM_HScroll(es, LOWORD(wParam), SHIWORD(wParam));
861 break;
863 case WM_KEYDOWN:
864 result = EDIT_WM_KeyDown(es, (INT)wParam);
865 break;
867 case WM_KILLFOCUS:
868 result = EDIT_WM_KillFocus(es);
869 break;
871 case WM_LBUTTONDBLCLK:
872 result = EDIT_WM_LButtonDblClk(es);
873 break;
875 case WM_LBUTTONDOWN:
876 result = EDIT_WM_LButtonDown(es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
877 break;
879 case WM_LBUTTONUP:
880 result = EDIT_WM_LButtonUp(es);
881 break;
883 case WM_MBUTTONDOWN:
884 result = EDIT_WM_MButtonDown(es);
885 break;
887 case WM_MOUSEACTIVATE:
889 * FIXME: maybe DefWindowProc() screws up, but it seems that
890 * modeless dialog boxes need this. If we don't do this, the focus
891 * will _not_ be set by DefWindowProc() for edit controls in a
892 * modeless dialog box ???
894 SetFocus(hwnd);
895 result = MA_ACTIVATE;
896 break;
898 case WM_MOUSEMOVE:
899 result = EDIT_WM_MouseMove(es, SLOWORD(lParam), SHIWORD(lParam));
900 break;
902 case WM_PAINT:
903 EDIT_WM_Paint(es, wParam);
904 break;
906 case WM_PASTE:
907 EDIT_WM_Paste(es);
908 break;
910 case WM_SETFOCUS:
911 EDIT_WM_SetFocus(es);
912 break;
914 case WM_SETFONT:
915 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
916 break;
918 case WM_SETREDRAW:
919 /* FIXME: actually set an internal flag and behave accordingly */
920 break;
922 case WM_SETTEXT:
923 EDIT_WM_SetText(es, lParam, unicode);
924 result = TRUE;
925 break;
927 case WM_SIZE:
928 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
929 break;
931 case WM_STYLECHANGED:
932 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
933 break;
935 case WM_STYLECHANGING:
936 result = 0; /* See EDIT_WM_StyleChanged */
937 break;
939 case WM_SYSKEYDOWN:
940 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
941 break;
943 case WM_TIMER:
944 EDIT_WM_Timer(es);
945 break;
947 case WM_VSCROLL:
948 result = EDIT_WM_VScroll(es, LOWORD(wParam), SHIWORD(wParam));
949 break;
951 case WM_MOUSEWHEEL:
953 int gcWheelDelta = 0;
954 UINT pulScrollLines = 3;
955 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
957 if (wParam & (MK_SHIFT | MK_CONTROL)) {
958 result = DefWindowProcW(hwnd, msg, wParam, lParam);
959 break;
961 gcWheelDelta -= SHIWORD(wParam);
962 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
964 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
965 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
966 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
969 break;
970 default:
971 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
972 break;
975 if (es) EDIT_UnlockBuffer(es, FALSE);
977 return result;
980 /*********************************************************************
982 * EditWndProcW (USER32.@)
984 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
986 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
989 /*********************************************************************
991 * EditWndProc (USER32.@)
993 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
995 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
998 /*********************************************************************
1000 * EDIT_BuildLineDefs_ML
1002 * Build linked list of text lines.
1003 * Lines can end with '\0' (last line), a character (if it is wrapped),
1004 * a soft return '\r\r\n' or a hard return '\r\n'
1007 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1009 HDC dc;
1010 HFONT old_font = 0;
1011 LPWSTR current_position, cp;
1012 INT fw;
1013 LINEDEF *current_line;
1014 LINEDEF *previous_line;
1015 LINEDEF *start_line;
1016 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1017 INT line_count = es->line_count;
1018 INT orig_net_length;
1019 RECT rc;
1021 if (istart == iend && delta == 0)
1022 return;
1024 dc = GetDC(es->hwndSelf);
1025 if (es->font)
1026 old_font = SelectObject(dc, es->font);
1028 previous_line = NULL;
1029 current_line = es->first_line_def;
1031 /* Find starting line. istart must lie inside an existing line or
1032 * at the end of buffer */
1033 do {
1034 if (istart < current_line->index + current_line->length ||
1035 current_line->ending == END_0)
1036 break;
1038 previous_line = current_line;
1039 current_line = current_line->next;
1040 line_index++;
1041 } while (current_line);
1043 if (!current_line) /* Error occurred start is not inside previous buffer */
1045 FIXME(" modification occurred outside buffer\n");
1046 ReleaseDC(es->hwndSelf, dc);
1047 return;
1050 /* Remember start of modifications in order to calculate update region */
1051 nstart_line = line_index;
1052 nstart_index = current_line->index;
1054 /* We must start to reformat from the previous line since the modifications
1055 * may have caused the line to wrap upwards. */
1056 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1058 line_index--;
1059 current_line = previous_line;
1061 start_line = current_line;
1063 fw = es->format_rect.right - es->format_rect.left;
1064 current_position = es->text + current_line->index;
1065 do {
1066 if (current_line != start_line)
1068 if (!current_line || current_line->index + delta > current_position - es->text)
1070 /* The buffer has been expanded, create a new line and
1071 insert it into the link list */
1072 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1073 new_line->next = previous_line->next;
1074 previous_line->next = new_line;
1075 current_line = new_line;
1076 es->line_count++;
1078 else if (current_line->index + delta < current_position - es->text)
1080 /* The previous line merged with this line so we delete this extra entry */
1081 previous_line->next = current_line->next;
1082 HeapFree(GetProcessHeap(), 0, current_line);
1083 current_line = previous_line->next;
1084 es->line_count--;
1085 continue;
1087 else /* current_line->index + delta == current_position */
1089 if (current_position - es->text > iend)
1090 break; /* We reached end of line modifications */
1091 /* else recalulate this line */
1095 current_line->index = current_position - es->text;
1096 orig_net_length = current_line->net_length;
1098 /* Find end of line */
1099 cp = current_position;
1100 while (*cp) {
1101 if (*cp == '\n') break;
1102 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1103 break;
1104 cp++;
1107 /* Mark type of line termination */
1108 if (!(*cp)) {
1109 current_line->ending = END_0;
1110 current_line->net_length = strlenW(current_position);
1111 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1112 current_line->ending = END_SOFT;
1113 current_line->net_length = cp - current_position - 1;
1114 } else if (*cp == '\n') {
1115 current_line->ending = END_RICH;
1116 current_line->net_length = cp - current_position;
1117 } else {
1118 current_line->ending = END_HARD;
1119 current_line->net_length = cp - current_position;
1122 /* Calculate line width */
1123 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1124 current_position, current_line->net_length,
1125 es->tabs_count, es->tabs));
1127 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1128 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1129 INT next = 0;
1130 INT prev;
1131 do {
1132 prev = next;
1133 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1134 prev + 1, current_line->net_length, WB_RIGHT);
1135 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1136 current_position, next, es->tabs_count, es->tabs));
1137 } while (current_line->width <= fw);
1138 if (!prev) { /* Didn't find a line break so force a break */
1139 next = 0;
1140 do {
1141 prev = next;
1142 next++;
1143 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1144 current_position, next, es->tabs_count, es->tabs));
1145 } while (current_line->width <= fw);
1146 if (!prev)
1147 prev = 1;
1150 /* If the first line we are calculating, wrapped before istart, we must
1151 * adjust istart in order for this to be reflected in the update region. */
1152 if (current_line->index == nstart_index && istart > current_line->index + prev)
1153 istart = current_line->index + prev;
1154 /* else if we are updating the previous line before the first line we
1155 * are re-calculating and it expanded */
1156 else if (current_line == start_line &&
1157 current_line->index != nstart_index && orig_net_length < prev)
1159 /* Line expanded due to an upwards line wrap so we must partially include
1160 * previous line in update region */
1161 nstart_line = line_index;
1162 nstart_index = current_line->index;
1163 istart = current_line->index + orig_net_length;
1166 current_line->net_length = prev;
1167 current_line->ending = END_WRAP;
1168 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1169 current_line->net_length, es->tabs_count, es->tabs));
1173 /* Adjust length to include line termination */
1174 switch (current_line->ending) {
1175 case END_SOFT:
1176 current_line->length = current_line->net_length + 3;
1177 break;
1178 case END_RICH:
1179 current_line->length = current_line->net_length + 1;
1180 break;
1181 case END_HARD:
1182 current_line->length = current_line->net_length + 2;
1183 break;
1184 case END_WRAP:
1185 case END_0:
1186 current_line->length = current_line->net_length;
1187 break;
1189 es->text_width = max(es->text_width, current_line->width);
1190 current_position += current_line->length;
1191 previous_line = current_line;
1192 current_line = current_line->next;
1193 line_index++;
1194 } while (previous_line->ending != END_0);
1196 /* Finish adjusting line indexes by delta or remove hanging lines */
1197 if (previous_line->ending == END_0)
1199 LINEDEF *pnext = NULL;
1201 previous_line->next = NULL;
1202 while (current_line)
1204 pnext = current_line->next;
1205 HeapFree(GetProcessHeap(), 0, current_line);
1206 current_line = pnext;
1207 es->line_count--;
1210 else
1212 while (current_line)
1214 current_line->index += delta;
1215 current_line = current_line->next;
1219 /* Calculate rest of modification rectangle */
1220 if (hrgn)
1222 HRGN tmphrgn;
1224 * We calculate two rectangles. One for the first line which may have
1225 * an indent with respect to the format rect. The other is a format-width
1226 * rectangle that spans the rest of the lines that changed or moved.
1228 rc.top = es->format_rect.top + nstart_line * es->line_height -
1229 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1230 rc.bottom = rc.top + es->line_height;
1231 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1232 es->text + nstart_index, istart - nstart_index,
1233 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1234 rc.right = es->format_rect.right;
1235 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1237 rc.top = rc.bottom;
1238 rc.left = es->format_rect.left;
1239 rc.right = es->format_rect.right;
1241 * If lines were added or removed we must re-paint the remainder of the
1242 * lines since the remaining lines were either shifted up or down.
1244 if (line_count < es->line_count) /* We added lines */
1245 rc.bottom = es->line_count * es->line_height;
1246 else if (line_count > es->line_count) /* We removed lines */
1247 rc.bottom = line_count * es->line_height;
1248 else
1249 rc.bottom = line_index * es->line_height;
1250 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1251 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1252 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1253 DeleteObject(tmphrgn);
1256 if (es->font)
1257 SelectObject(dc, old_font);
1259 ReleaseDC(es->hwndSelf, dc);
1262 /*********************************************************************
1264 * EDIT_CalcLineWidth_SL
1267 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1269 es->text_width = SLOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1272 /*********************************************************************
1274 * EDIT_CallWordBreakProc
1276 * Call appropriate WordBreakProc (internal or external).
1278 * Note: The "start" argument should always be an index referring
1279 * to es->text. The actual wordbreak proc might be
1280 * 16 bit, so we can't always pass any 32 bit LPSTR.
1281 * Hence we assume that es->text is the buffer that holds
1282 * the string under examination (we can decide this for ourselves).
1285 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1287 INT ret, iWndsLocks;
1289 /* To avoid any deadlocks, all the locks on the window structures
1290 must be suspended before the control is passed to the application */
1291 iWndsLocks = WIN_SuspendWndsLock();
1293 if (es->word_break_proc16) {
1294 HGLOBAL16 hglob16;
1295 SEGPTR segptr;
1296 INT countA;
1297 WORD args[5];
1298 DWORD result;
1300 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1301 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1302 segptr = K32WOWGlobalLock16(hglob16);
1303 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1304 args[4] = SELECTOROF(segptr);
1305 args[3] = OFFSETOF(segptr);
1306 args[2] = index;
1307 args[1] = countA;
1308 args[0] = action;
1309 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1310 ret = LOWORD(result);
1311 GlobalUnlock16(hglob16);
1312 GlobalFree16(hglob16);
1314 else if (es->word_break_proc)
1316 if(es->is_unicode)
1318 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1320 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1321 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1322 ret = wbpW(es->text + start, index, count, action);
1324 else
1326 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1327 INT countA;
1328 CHAR *textA;
1330 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1331 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1332 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1333 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1334 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1335 ret = wbpA(textA, index, countA, action);
1336 HeapFree(GetProcessHeap(), 0, textA);
1339 else
1340 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1342 WIN_RestoreWndsLock(iWndsLocks);
1343 return ret;
1347 /*********************************************************************
1349 * EDIT_CharFromPos
1351 * Beware: This is not the function called on EM_CHARFROMPOS
1352 * The position _can_ be outside the formatting / client
1353 * rectangle
1354 * The return value is only the character index
1357 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1359 INT index;
1360 HDC dc;
1361 HFONT old_font = 0;
1363 if (es->style & ES_MULTILINE) {
1364 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1365 INT line_index = 0;
1366 LINEDEF *line_def = es->first_line_def;
1367 INT low, high;
1368 while ((line > 0) && line_def->next) {
1369 line_index += line_def->length;
1370 line_def = line_def->next;
1371 line--;
1373 x += es->x_offset - es->format_rect.left;
1374 if (x >= line_def->width) {
1375 if (after_wrap)
1376 *after_wrap = (line_def->ending == END_WRAP);
1377 return line_index + line_def->net_length;
1379 if (x <= 0) {
1380 if (after_wrap)
1381 *after_wrap = FALSE;
1382 return line_index;
1384 dc = GetDC(es->hwndSelf);
1385 if (es->font)
1386 old_font = SelectObject(dc, es->font);
1387 low = line_index + 1;
1388 high = line_index + line_def->net_length + 1;
1389 while (low < high - 1)
1391 INT mid = (low + high) / 2;
1392 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1393 else low = mid;
1395 index = low;
1397 if (after_wrap)
1398 *after_wrap = ((index == line_index + line_def->net_length) &&
1399 (line_def->ending == END_WRAP));
1400 } else {
1401 LPWSTR text;
1402 SIZE size;
1403 if (after_wrap)
1404 *after_wrap = FALSE;
1405 x -= es->format_rect.left;
1406 if (!x)
1407 return es->x_offset;
1408 text = EDIT_GetPasswordPointer_SL(es);
1409 dc = GetDC(es->hwndSelf);
1410 if (es->font)
1411 old_font = SelectObject(dc, es->font);
1412 if (x < 0)
1414 INT low = 0;
1415 INT high = es->x_offset;
1416 while (low < high - 1)
1418 INT mid = (low + high) / 2;
1419 GetTextExtentPoint32W( dc, text + mid,
1420 es->x_offset - mid, &size );
1421 if (size.cx > -x) low = mid;
1422 else high = mid;
1424 index = low;
1426 else
1428 INT low = es->x_offset;
1429 INT high = strlenW(es->text) + 1;
1430 while (low < high - 1)
1432 INT mid = (low + high) / 2;
1433 GetTextExtentPoint32W( dc, text + es->x_offset,
1434 mid - es->x_offset, &size );
1435 if (size.cx > x) high = mid;
1436 else low = mid;
1438 index = low;
1440 if (es->style & ES_PASSWORD)
1441 HeapFree(GetProcessHeap(), 0, text);
1443 if (es->font)
1444 SelectObject(dc, old_font);
1445 ReleaseDC(es->hwndSelf, dc);
1446 return index;
1450 /*********************************************************************
1452 * EDIT_ConfinePoint
1454 * adjusts the point to be within the formatting rectangle
1455 * (so CharFromPos returns the nearest _visible_ character)
1458 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1460 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1461 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1465 /*********************************************************************
1467 * EDIT_GetLineRect
1469 * Calculates the bounding rectangle for a line from a starting
1470 * column to an ending column.
1473 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1475 INT line_index = EDIT_EM_LineIndex(es, line);
1477 if (es->style & ES_MULTILINE)
1478 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1479 else
1480 rc->top = es->format_rect.top;
1481 rc->bottom = rc->top + es->line_height;
1482 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1483 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1487 /*********************************************************************
1489 * EDIT_GetPasswordPointer_SL
1491 * note: caller should free the (optionally) allocated buffer
1494 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1496 if (es->style & ES_PASSWORD) {
1497 INT len = strlenW(es->text);
1498 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1499 text[len] = '\0';
1500 while(len) text[--len] = es->password_char;
1501 return text;
1502 } else
1503 return es->text;
1507 /*********************************************************************
1509 * EDIT_LockBuffer
1511 * This acts as a LOCAL_Lock(), but it locks only once. This way
1512 * you can call it whenever you like, without unlocking.
1514 * Initially the edit control allocates a HLOCAL32 buffer
1515 * (32 bit linear memory handler). However, 16 bit application
1516 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1517 * handler). From that moment on we have to keep using this 16 bit memory
1518 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1519 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1520 * conversion.
1523 static void EDIT_LockBuffer(EDITSTATE *es)
1525 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
1526 if (!es->text) {
1527 CHAR *textA = NULL;
1528 UINT countA = 0;
1529 BOOL _16bit = FALSE;
1531 if(es->hloc32W)
1533 if(es->hloc32A)
1535 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1536 textA = LocalLock(es->hloc32A);
1537 countA = strlen(textA) + 1;
1539 else if(es->hloc16)
1541 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1542 textA = LOCAL_Lock(hInstance, es->hloc16);
1543 countA = strlen(textA) + 1;
1544 _16bit = TRUE;
1547 else {
1548 ERR("no buffer ... please report\n");
1549 return;
1552 if(textA)
1554 HLOCAL hloc32W_new;
1555 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1556 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1557 if(countW_new > es->buffer_size + 1)
1559 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1560 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1561 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1562 if(hloc32W_new)
1564 es->hloc32W = hloc32W_new;
1565 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1566 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1568 else
1569 WARN("FAILED! Will synchronize partially\n");
1573 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1574 es->text = LocalLock(es->hloc32W);
1576 if(textA)
1578 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1579 if(_16bit)
1580 LOCAL_Unlock(hInstance, es->hloc16);
1581 else
1582 LocalUnlock(es->hloc32A);
1585 es->lock_count++;
1589 /*********************************************************************
1591 * EDIT_SL_InvalidateText
1593 * Called from EDIT_InvalidateText().
1594 * Does the job for single-line controls only.
1597 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1599 RECT line_rect;
1600 RECT rc;
1602 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1603 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1604 EDIT_UpdateText(es, &rc, TRUE);
1608 /*********************************************************************
1610 * EDIT_ML_InvalidateText
1612 * Called from EDIT_InvalidateText().
1613 * Does the job for multi-line controls only.
1616 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1618 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1619 INT sl = EDIT_EM_LineFromChar(es, start);
1620 INT el = EDIT_EM_LineFromChar(es, end);
1621 INT sc;
1622 INT ec;
1623 RECT rc1;
1624 RECT rcWnd;
1625 RECT rcLine;
1626 RECT rcUpdate;
1627 INT l;
1629 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1630 return;
1632 sc = start - EDIT_EM_LineIndex(es, sl);
1633 ec = end - EDIT_EM_LineIndex(es, el);
1634 if (sl < es->y_offset) {
1635 sl = es->y_offset;
1636 sc = 0;
1638 if (el > es->y_offset + vlc) {
1639 el = es->y_offset + vlc;
1640 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1642 GetClientRect(es->hwndSelf, &rc1);
1643 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1644 if (sl == el) {
1645 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1646 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1647 EDIT_UpdateText(es, &rcUpdate, TRUE);
1648 } else {
1649 EDIT_GetLineRect(es, sl, sc,
1650 EDIT_EM_LineLength(es,
1651 EDIT_EM_LineIndex(es, sl)),
1652 &rcLine);
1653 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1654 EDIT_UpdateText(es, &rcUpdate, TRUE);
1655 for (l = sl + 1 ; l < el ; l++) {
1656 EDIT_GetLineRect(es, l, 0,
1657 EDIT_EM_LineLength(es,
1658 EDIT_EM_LineIndex(es, l)),
1659 &rcLine);
1660 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1661 EDIT_UpdateText(es, &rcUpdate, TRUE);
1663 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1664 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1665 EDIT_UpdateText(es, &rcUpdate, TRUE);
1670 /*********************************************************************
1672 * EDIT_InvalidateText
1674 * Invalidate the text from offset start upto, but not including,
1675 * offset end. Useful for (re)painting the selection.
1676 * Regions outside the linewidth are not invalidated.
1677 * end == -1 means end == TextLength.
1678 * start and end need not be ordered.
1681 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1683 if (end == start)
1684 return;
1686 if (end == -1)
1687 end = strlenW(es->text);
1689 if (end < start) {
1690 INT tmp = start;
1691 start = end;
1692 end = tmp;
1695 if (es->style & ES_MULTILINE)
1696 EDIT_ML_InvalidateText(es, start, end);
1697 else
1698 EDIT_SL_InvalidateText(es, start, end);
1702 /*********************************************************************
1704 * EDIT_MakeFit
1706 * Try to fit size + 1 characters in the buffer.
1707 * Constrain to limits if honor_limit is TRUE.
1709 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size, BOOL honor_limit)
1711 HLOCAL hNew32W;
1713 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
1714 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
1715 return FALSE;
1718 if (size <= es->buffer_size)
1719 return TRUE;
1721 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1723 /* Force edit to unlock it's buffer. es->text now NULL */
1724 EDIT_UnlockBuffer(es, TRUE);
1726 if (es->hloc32W) {
1727 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1728 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1729 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1730 es->hloc32W = hNew32W;
1731 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1735 EDIT_LockBuffer(es);
1737 if (es->buffer_size < size) {
1738 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1739 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1740 return FALSE;
1741 } else {
1742 TRACE("We now have %d+1\n", es->buffer_size);
1743 return TRUE;
1748 /*********************************************************************
1750 * EDIT_MakeUndoFit
1752 * Try to fit size + 1 bytes in the undo buffer.
1755 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1757 UINT alloc_size;
1759 if (size <= es->undo_buffer_size)
1760 return TRUE;
1762 TRACE("trying to ReAlloc to %d+1\n", size);
1764 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1765 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1766 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1767 return TRUE;
1769 else
1771 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1772 return FALSE;
1777 /*********************************************************************
1779 * EDIT_MoveBackward
1782 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1784 INT e = es->selection_end;
1786 if (e) {
1787 e--;
1788 if ((es->style & ES_MULTILINE) && e &&
1789 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1790 e--;
1791 if (e && (es->text[e - 1] == '\r'))
1792 e--;
1795 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1796 EDIT_EM_ScrollCaret(es);
1800 /*********************************************************************
1802 * EDIT_MoveDown_ML
1804 * Only for multi line controls
1805 * Move the caret one line down, on a column with the nearest
1806 * x coordinate on the screen (might be a different column).
1809 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1811 INT s = es->selection_start;
1812 INT e = es->selection_end;
1813 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1814 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1815 INT x = SLOWORD(pos);
1816 INT y = SHIWORD(pos);
1818 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1819 if (!extend)
1820 s = e;
1821 EDIT_EM_SetSel(es, s, e, after_wrap);
1822 EDIT_EM_ScrollCaret(es);
1826 /*********************************************************************
1828 * EDIT_MoveEnd
1831 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1833 BOOL after_wrap = FALSE;
1834 INT e;
1836 /* Pass a high value in x to make sure of receiving the end of the line */
1837 if (es->style & ES_MULTILINE)
1838 e = EDIT_CharFromPos(es, 0x3fffffff,
1839 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1840 else
1841 e = strlenW(es->text);
1842 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1843 EDIT_EM_ScrollCaret(es);
1847 /*********************************************************************
1849 * EDIT_MoveForward
1852 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1854 INT e = es->selection_end;
1856 if (es->text[e]) {
1857 e++;
1858 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1859 if (es->text[e] == '\n')
1860 e++;
1861 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1862 e += 2;
1865 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1866 EDIT_EM_ScrollCaret(es);
1870 /*********************************************************************
1872 * EDIT_MoveHome
1874 * Home key: move to beginning of line.
1877 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1879 INT e;
1881 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1882 if (es->style & ES_MULTILINE)
1883 e = EDIT_CharFromPos(es, -es->x_offset,
1884 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1885 else
1886 e = 0;
1887 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1888 EDIT_EM_ScrollCaret(es);
1892 /*********************************************************************
1894 * EDIT_MovePageDown_ML
1896 * Only for multi line controls
1897 * Move the caret one page down, on a column with the nearest
1898 * x coordinate on the screen (might be a different column).
1901 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1903 INT s = es->selection_start;
1904 INT e = es->selection_end;
1905 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1906 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1907 INT x = SLOWORD(pos);
1908 INT y = SHIWORD(pos);
1910 e = EDIT_CharFromPos(es, x,
1911 y + (es->format_rect.bottom - es->format_rect.top),
1912 &after_wrap);
1913 if (!extend)
1914 s = e;
1915 EDIT_EM_SetSel(es, s, e, after_wrap);
1916 EDIT_EM_ScrollCaret(es);
1920 /*********************************************************************
1922 * EDIT_MovePageUp_ML
1924 * Only for multi line controls
1925 * Move the caret one page up, on a column with the nearest
1926 * x coordinate on the screen (might be a different column).
1929 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1931 INT s = es->selection_start;
1932 INT e = es->selection_end;
1933 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1934 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1935 INT x = SLOWORD(pos);
1936 INT y = SHIWORD(pos);
1938 e = EDIT_CharFromPos(es, x,
1939 y - (es->format_rect.bottom - es->format_rect.top),
1940 &after_wrap);
1941 if (!extend)
1942 s = e;
1943 EDIT_EM_SetSel(es, s, e, after_wrap);
1944 EDIT_EM_ScrollCaret(es);
1948 /*********************************************************************
1950 * EDIT_MoveUp_ML
1952 * Only for multi line controls
1953 * Move the caret one line up, on a column with the nearest
1954 * x coordinate on the screen (might be a different column).
1957 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1959 INT s = es->selection_start;
1960 INT e = es->selection_end;
1961 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1962 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1963 INT x = SLOWORD(pos);
1964 INT y = SHIWORD(pos);
1966 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1967 if (!extend)
1968 s = e;
1969 EDIT_EM_SetSel(es, s, e, after_wrap);
1970 EDIT_EM_ScrollCaret(es);
1974 /*********************************************************************
1976 * EDIT_MoveWordBackward
1979 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1981 INT s = es->selection_start;
1982 INT e = es->selection_end;
1983 INT l;
1984 INT ll;
1985 INT li;
1987 l = EDIT_EM_LineFromChar(es, e);
1988 ll = EDIT_EM_LineLength(es, e);
1989 li = EDIT_EM_LineIndex(es, l);
1990 if (e - li == 0) {
1991 if (l) {
1992 li = EDIT_EM_LineIndex(es, l - 1);
1993 e = li + EDIT_EM_LineLength(es, li);
1995 } else {
1996 e = li + (INT)EDIT_CallWordBreakProc(es,
1997 li, e - li, ll, WB_LEFT);
1999 if (!extend)
2000 s = e;
2001 EDIT_EM_SetSel(es, s, e, FALSE);
2002 EDIT_EM_ScrollCaret(es);
2006 /*********************************************************************
2008 * EDIT_MoveWordForward
2011 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2013 INT s = es->selection_start;
2014 INT e = es->selection_end;
2015 INT l;
2016 INT ll;
2017 INT li;
2019 l = EDIT_EM_LineFromChar(es, e);
2020 ll = EDIT_EM_LineLength(es, e);
2021 li = EDIT_EM_LineIndex(es, l);
2022 if (e - li == ll) {
2023 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2024 e = EDIT_EM_LineIndex(es, l + 1);
2025 } else {
2026 e = li + EDIT_CallWordBreakProc(es,
2027 li, e - li + 1, ll, WB_RIGHT);
2029 if (!extend)
2030 s = e;
2031 EDIT_EM_SetSel(es, s, e, FALSE);
2032 EDIT_EM_ScrollCaret(es);
2036 /*********************************************************************
2038 * EDIT_PaintLine
2041 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2043 INT s = es->selection_start;
2044 INT e = es->selection_end;
2045 INT li;
2046 INT ll;
2047 INT x;
2048 INT y;
2049 LRESULT pos;
2051 if (es->style & ES_MULTILINE) {
2052 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2053 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2054 return;
2055 } else if (line)
2056 return;
2058 TRACE("line=%d\n", line);
2060 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2061 x = SLOWORD(pos);
2062 y = SHIWORD(pos);
2063 li = EDIT_EM_LineIndex(es, line);
2064 ll = EDIT_EM_LineLength(es, li);
2065 s = min(es->selection_start, es->selection_end);
2066 e = max(es->selection_start, es->selection_end);
2067 s = min(li + ll, max(li, s));
2068 e = min(li + ll, max(li, e));
2069 if (rev && (s != e) &&
2070 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2071 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2072 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2073 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2074 } else
2075 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2079 /*********************************************************************
2081 * EDIT_PaintText
2084 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2086 COLORREF BkColor;
2087 COLORREF TextColor;
2088 INT ret;
2089 INT li;
2090 INT BkMode;
2091 SIZE size;
2093 if (!count)
2094 return 0;
2095 BkMode = GetBkMode(dc);
2096 BkColor = GetBkColor(dc);
2097 TextColor = GetTextColor(dc);
2098 if (rev) {
2099 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2100 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2101 SetBkMode( dc, OPAQUE);
2103 li = EDIT_EM_LineIndex(es, line);
2104 if (es->style & ES_MULTILINE) {
2105 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2106 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2107 } else {
2108 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2109 TextOutW(dc, x, y, text + li + col, count);
2110 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2111 ret = size.cx;
2112 if (es->style & ES_PASSWORD)
2113 HeapFree(GetProcessHeap(), 0, text);
2115 if (rev) {
2116 SetBkColor(dc, BkColor);
2117 SetTextColor(dc, TextColor);
2118 SetBkMode( dc, BkMode);
2120 return ret;
2124 /*********************************************************************
2126 * EDIT_SetCaretPos
2129 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2130 BOOL after_wrap)
2132 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2133 SetCaretPos(SLOWORD(res), SHIWORD(res));
2137 /*********************************************************************
2139 * EDIT_SetRectNP
2141 * note: this is not (exactly) the handler called on EM_SETRECTNP
2142 * it is also used to set the rect of a single line control
2145 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2147 CopyRect(&es->format_rect, rc);
2148 if (es->style & WS_BORDER) {
2149 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2150 if(TWEAK_WineLook == WIN31_LOOK)
2151 bw += 2;
2152 es->format_rect.left += bw;
2153 es->format_rect.top += bw;
2154 es->format_rect.right -= bw;
2155 es->format_rect.bottom -= bw;
2157 es->format_rect.left += es->left_margin;
2158 es->format_rect.right -= es->right_margin;
2159 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2160 if (es->style & ES_MULTILINE)
2162 INT fw, vlc, max_x_offset, max_y_offset;
2164 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2165 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2167 /* correct es->x_offset */
2168 fw = es->format_rect.right - es->format_rect.left;
2169 max_x_offset = es->text_width - fw;
2170 if(max_x_offset < 0) max_x_offset = 0;
2171 if(es->x_offset > max_x_offset)
2172 es->x_offset = max_x_offset;
2174 /* correct es->y_offset */
2175 max_y_offset = es->line_count - vlc;
2176 if(max_y_offset < 0) max_y_offset = 0;
2177 if(es->y_offset > max_y_offset)
2178 es->y_offset = max_y_offset;
2180 /* force scroll info update */
2181 EDIT_UpdateScrollInfo(es);
2183 else
2184 /* Windows doesn't care to fix text placement for SL controls */
2185 es->format_rect.bottom = es->format_rect.top + es->line_height;
2187 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2188 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2192 /*********************************************************************
2194 * EDIT_UnlockBuffer
2197 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2199 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2201 /* Edit window might be already destroyed */
2202 if(!IsWindow(es->hwndSelf))
2204 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2205 return;
2208 if (!es->lock_count) {
2209 ERR("lock_count == 0 ... please report\n");
2210 return;
2212 if (!es->text) {
2213 ERR("es->text == 0 ... please report\n");
2214 return;
2217 if (force || (es->lock_count == 1)) {
2218 if (es->hloc32W) {
2219 CHAR *textA = NULL;
2220 BOOL _16bit = FALSE;
2221 UINT countA = 0;
2222 UINT countW = strlenW(es->text) + 1;
2224 if(es->hloc32A)
2226 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2227 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2228 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2229 countA = LocalSize(es->hloc32A);
2230 if(countA_new > countA)
2232 HLOCAL hloc32A_new;
2233 UINT alloc_size = ROUND_TO_GROW(countA_new);
2234 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2235 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2236 if(hloc32A_new)
2238 es->hloc32A = hloc32A_new;
2239 countA = LocalSize(hloc32A_new);
2240 TRACE("Real new size %d bytes\n", countA);
2242 else
2243 WARN("FAILED! Will synchronize partially\n");
2245 textA = LocalLock(es->hloc32A);
2247 else if(es->hloc16)
2249 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2250 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2251 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2252 countA = LOCAL_Size(hInstance, es->hloc16);
2253 if(countA_new > countA)
2255 HLOCAL16 hloc16_new;
2256 UINT alloc_size = ROUND_TO_GROW(countA_new);
2257 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2258 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2259 if(hloc16_new)
2261 es->hloc16 = hloc16_new;
2262 countA = LOCAL_Size(hInstance, hloc16_new);
2263 TRACE("Real new size %d bytes\n", countA);
2265 else
2266 WARN("FAILED! Will synchronize partially\n");
2268 textA = LOCAL_Lock(hInstance, es->hloc16);
2269 _16bit = TRUE;
2272 if(textA)
2274 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2275 if(_16bit)
2276 LOCAL_Unlock(hInstance, es->hloc16);
2277 else
2278 LocalUnlock(es->hloc32A);
2281 LocalUnlock(es->hloc32W);
2282 es->text = NULL;
2284 else {
2285 ERR("no buffer ... please report\n");
2286 return;
2289 es->lock_count--;
2293 /*********************************************************************
2295 * EDIT_UpdateScrollInfo
2298 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2300 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2302 SCROLLINFO si;
2303 si.cbSize = sizeof(SCROLLINFO);
2304 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2305 si.nMin = 0;
2306 si.nMax = es->line_count - 1;
2307 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2308 si.nPos = es->y_offset;
2309 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2310 si.nMin, si.nMax, si.nPage, si.nPos);
2311 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2314 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2316 SCROLLINFO si;
2317 si.cbSize = sizeof(SCROLLINFO);
2318 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2319 si.nMin = 0;
2320 si.nMax = es->text_width - 1;
2321 si.nPage = es->format_rect.right - es->format_rect.left;
2322 si.nPos = es->x_offset;
2323 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2324 si.nMin, si.nMax, si.nPage, si.nPos);
2325 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2329 /*********************************************************************
2331 * EDIT_WordBreakProc
2333 * Find the beginning of words.
2334 * Note: unlike the specs for a WordBreakProc, this function only
2335 * allows to be called without linebreaks between s[0] upto
2336 * s[count - 1]. Remember it is only called
2337 * internally, so we can decide this for ourselves.
2340 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2342 INT ret = 0;
2344 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2346 if(!s) return 0;
2348 switch (action) {
2349 case WB_LEFT:
2350 if (!count)
2351 break;
2352 if (index)
2353 index--;
2354 if (s[index] == ' ') {
2355 while (index && (s[index] == ' '))
2356 index--;
2357 if (index) {
2358 while (index && (s[index] != ' '))
2359 index--;
2360 if (s[index] == ' ')
2361 index++;
2363 } else {
2364 while (index && (s[index] != ' '))
2365 index--;
2366 if (s[index] == ' ')
2367 index++;
2369 ret = index;
2370 break;
2371 case WB_RIGHT:
2372 if (!count)
2373 break;
2374 if (index)
2375 index--;
2376 if (s[index] == ' ')
2377 while ((index < count) && (s[index] == ' ')) index++;
2378 else {
2379 while (s[index] && (s[index] != ' ') && (index < count))
2380 index++;
2381 while ((s[index] == ' ') && (index < count)) index++;
2383 ret = index;
2384 break;
2385 case WB_ISDELIMITER:
2386 ret = (s[index] == ' ');
2387 break;
2388 default:
2389 ERR("unknown action code, please report !\n");
2390 break;
2392 return ret;
2396 /*********************************************************************
2398 * EM_CHARFROMPOS
2400 * returns line number (not index) in high-order word of result.
2401 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2402 * to Richedit, not to the edit control. Original documentation is valid.
2403 * FIXME: do the specs mean to return -1 if outside client area or
2404 * if outside formatting rectangle ???
2407 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2409 POINT pt;
2410 RECT rc;
2411 INT index;
2413 pt.x = x;
2414 pt.y = y;
2415 GetClientRect(es->hwndSelf, &rc);
2416 if (!PtInRect(&rc, pt))
2417 return -1;
2419 index = EDIT_CharFromPos(es, x, y, NULL);
2420 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2424 /*********************************************************************
2426 * EM_FMTLINES
2428 * Enable or disable soft breaks.
2430 * This means: insert or remove the soft linebreak character (\r\r\n).
2431 * Take care to check if the text still fits the buffer after insertion.
2432 * If not, notify with EN_ERRSPACE.
2435 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2437 es->flags &= ~EF_USE_SOFTBRK;
2438 if (add_eol) {
2439 es->flags |= EF_USE_SOFTBRK;
2440 FIXME("soft break enabled, not implemented\n");
2442 return add_eol;
2446 /*********************************************************************
2448 * EM_GETHANDLE
2450 * Hopefully this won't fire back at us.
2451 * We always start with a fixed buffer in the local heap.
2452 * Despite of the documentation says that the local heap is used
2453 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2454 * buffer on the local heap.
2457 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2459 HLOCAL hLocal;
2461 if (!(es->style & ES_MULTILINE))
2462 return 0;
2464 if(es->is_unicode)
2465 hLocal = es->hloc32W;
2466 else
2468 if(!es->hloc32A)
2470 CHAR *textA;
2471 UINT countA, alloc_size;
2472 TRACE("Allocating 32-bit ANSI alias buffer\n");
2473 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2474 alloc_size = ROUND_TO_GROW(countA);
2475 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2477 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2478 return 0;
2480 textA = LocalLock(es->hloc32A);
2481 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2482 LocalUnlock(es->hloc32A);
2484 hLocal = es->hloc32A;
2487 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2488 return hLocal;
2492 /*********************************************************************
2494 * EM_GETHANDLE16
2496 * Hopefully this won't fire back at us.
2497 * We always start with a buffer in 32 bit linear memory.
2498 * However, with this message a 16 bit application requests
2499 * a handle of 16 bit local heap memory, where it expects to find
2500 * the text.
2501 * It's a pitty that from this moment on we have to use this
2502 * local heap, because applications may rely on the handle
2503 * in the future.
2505 * In this function we'll try to switch to local heap.
2507 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2509 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2510 CHAR *textA;
2511 UINT countA, alloc_size;
2513 if (!(es->style & ES_MULTILINE))
2514 return 0;
2516 if (es->hloc16)
2517 return es->hloc16;
2519 if (!LOCAL_HeapSize(hInstance)) {
2520 if (!LocalInit16(hInstance, 0,
2521 GlobalSize16(hInstance))) {
2522 ERR("could not initialize local heap\n");
2523 return 0;
2525 TRACE("local heap initialized\n");
2528 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2529 alloc_size = ROUND_TO_GROW(countA);
2531 TRACE("Allocating 16-bit ANSI alias buffer\n");
2532 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2533 ERR("could not allocate new 16 bit buffer\n");
2534 return 0;
2537 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2538 ERR("could not lock new 16 bit buffer\n");
2539 LOCAL_Free(hInstance, es->hloc16);
2540 es->hloc16 = 0;
2541 return 0;
2544 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2545 LOCAL_Unlock(hInstance, es->hloc16);
2547 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2548 return es->hloc16;
2552 /*********************************************************************
2554 * EM_GETLINE
2557 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2559 LPWSTR src;
2560 INT line_len, dst_len;
2561 INT i;
2563 if (es->style & ES_MULTILINE) {
2564 if (line >= es->line_count)
2565 return 0;
2566 } else
2567 line = 0;
2568 i = EDIT_EM_LineIndex(es, line);
2569 src = es->text + i;
2570 line_len = EDIT_EM_LineLength(es, i);
2571 dst_len = *(WORD *)lParam;
2572 if(unicode)
2574 LPWSTR dst = (LPWSTR)lParam;
2575 if(dst_len <= line_len)
2577 memcpy(dst, src, dst_len * sizeof(WCHAR));
2578 return dst_len;
2580 else /* Append 0 if enough space */
2582 memcpy(dst, src, line_len * sizeof(WCHAR));
2583 dst[line_len] = 0;
2584 return line_len;
2587 else
2589 LPSTR dst = (LPSTR)lParam;
2590 INT ret;
2591 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2592 if(!ret) /* Insufficient buffer size */
2593 return dst_len;
2594 if(ret < dst_len) /* Append 0 if enough space */
2595 dst[ret] = 0;
2596 return ret;
2601 /*********************************************************************
2603 * EM_GETSEL
2606 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2608 UINT s = es->selection_start;
2609 UINT e = es->selection_end;
2611 ORDER_UINT(s, e);
2612 if (start)
2613 *start = s;
2614 if (end)
2615 *end = e;
2616 return MAKELONG(s, e);
2620 /*********************************************************************
2622 * EM_GETTHUMB
2624 * FIXME: is this right ? (or should it be only VSCROLL)
2625 * (and maybe only for edit controls that really have their
2626 * own scrollbars) (and maybe only for multiline controls ?)
2627 * All in all: very poorly documented
2630 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2632 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2633 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2637 /*********************************************************************
2639 * EM_LINEFROMCHAR
2642 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2644 INT line;
2645 LINEDEF *line_def;
2647 if (!(es->style & ES_MULTILINE))
2648 return 0;
2649 if (index > (INT)strlenW(es->text))
2650 return es->line_count - 1;
2651 if (index == -1)
2652 index = min(es->selection_start, es->selection_end);
2654 line = 0;
2655 line_def = es->first_line_def;
2656 index -= line_def->length;
2657 while ((index >= 0) && line_def->next) {
2658 line++;
2659 line_def = line_def->next;
2660 index -= line_def->length;
2662 return line;
2666 /*********************************************************************
2668 * EM_LINEINDEX
2671 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2673 INT line_index;
2674 LINEDEF *line_def;
2676 if (!(es->style & ES_MULTILINE))
2677 return 0;
2678 if (line >= es->line_count)
2679 return -1;
2681 line_index = 0;
2682 line_def = es->first_line_def;
2683 if (line == -1) {
2684 INT index = es->selection_end - line_def->length;
2685 while ((index >= 0) && line_def->next) {
2686 line_index += line_def->length;
2687 line_def = line_def->next;
2688 index -= line_def->length;
2690 } else {
2691 while (line > 0) {
2692 line_index += line_def->length;
2693 line_def = line_def->next;
2694 line--;
2697 return line_index;
2701 /*********************************************************************
2703 * EM_LINELENGTH
2706 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2708 LINEDEF *line_def;
2710 if (!(es->style & ES_MULTILINE))
2711 return strlenW(es->text);
2713 if (index == -1) {
2714 /* get the number of remaining non-selected chars of selected lines */
2715 INT32 l; /* line number */
2716 INT32 li; /* index of first char in line */
2717 INT32 count;
2718 l = EDIT_EM_LineFromChar(es, es->selection_start);
2719 /* # chars before start of selection area */
2720 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2721 l = EDIT_EM_LineFromChar(es, es->selection_end);
2722 /* # chars after end of selection */
2723 li = EDIT_EM_LineIndex(es, l);
2724 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2725 return count;
2727 line_def = es->first_line_def;
2728 index -= line_def->length;
2729 while ((index >= 0) && line_def->next) {
2730 line_def = line_def->next;
2731 index -= line_def->length;
2733 return line_def->net_length;
2737 /*********************************************************************
2739 * EM_LINESCROLL
2741 * NOTE: dx is in average character widths, dy - in lines;
2744 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2746 if (!(es->style & ES_MULTILINE))
2747 return FALSE;
2749 dx *= es->char_width;
2750 return EDIT_EM_LineScroll_internal(es, dx, dy);
2753 /*********************************************************************
2755 * EDIT_EM_LineScroll_internal
2757 * Version of EDIT_EM_LineScroll for internal use.
2758 * It doesn't refuse if ES_MULTILINE is set and assumes that
2759 * dx is in pixels, dy - in lines.
2762 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2764 INT nyoff;
2765 INT x_offset_in_pixels;
2767 if (es->style & ES_MULTILINE)
2769 x_offset_in_pixels = es->x_offset;
2771 else
2773 dy = 0;
2774 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2777 if (-dx > x_offset_in_pixels)
2778 dx = -x_offset_in_pixels;
2779 if (dx > es->text_width - x_offset_in_pixels)
2780 dx = es->text_width - x_offset_in_pixels;
2781 nyoff = max(0, es->y_offset + dy);
2782 if (nyoff >= es->line_count)
2783 nyoff = es->line_count - 1;
2784 dy = (es->y_offset - nyoff) * es->line_height;
2785 if (dx || dy) {
2786 RECT rc1;
2787 RECT rc;
2789 es->y_offset = nyoff;
2790 if(es->style & ES_MULTILINE)
2791 es->x_offset += dx;
2792 else
2793 es->x_offset += dx / es->char_width;
2795 GetClientRect(es->hwndSelf, &rc1);
2796 IntersectRect(&rc, &rc1, &es->format_rect);
2797 ScrollWindowEx(es->hwndSelf, -dx, dy,
2798 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2799 /* force scroll info update */
2800 EDIT_UpdateScrollInfo(es);
2802 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2803 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2804 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2805 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2806 return TRUE;
2810 /*********************************************************************
2812 * EM_POSFROMCHAR
2815 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2817 INT len = strlenW(es->text);
2818 INT l;
2819 INT li;
2820 INT x;
2821 INT y = 0;
2822 HDC dc;
2823 HFONT old_font = 0;
2824 SIZE size;
2826 index = min(index, len);
2827 dc = GetDC(es->hwndSelf);
2828 if (es->font)
2829 old_font = SelectObject(dc, es->font);
2830 if (es->style & ES_MULTILINE) {
2831 l = EDIT_EM_LineFromChar(es, index);
2832 y = (l - es->y_offset) * es->line_height;
2833 li = EDIT_EM_LineIndex(es, l);
2834 if (after_wrap && (li == index) && l) {
2835 INT l2 = l - 1;
2836 LINEDEF *line_def = es->first_line_def;
2837 while (l2) {
2838 line_def = line_def->next;
2839 l2--;
2841 if (line_def->ending == END_WRAP) {
2842 l--;
2843 y -= es->line_height;
2844 li = EDIT_EM_LineIndex(es, l);
2847 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2848 es->tabs_count, es->tabs)) - es->x_offset;
2849 } else {
2850 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2851 if (index < es->x_offset) {
2852 GetTextExtentPoint32W(dc, text + index,
2853 es->x_offset - index, &size);
2854 x = -size.cx;
2855 } else {
2856 GetTextExtentPoint32W(dc, text + es->x_offset,
2857 index - es->x_offset, &size);
2858 x = size.cx;
2860 y = 0;
2861 if (es->style & ES_PASSWORD)
2862 HeapFree(GetProcessHeap(), 0, text);
2864 x += es->format_rect.left;
2865 y += es->format_rect.top;
2866 if (es->font)
2867 SelectObject(dc, old_font);
2868 ReleaseDC(es->hwndSelf, dc);
2869 return MAKELONG((INT16)x, (INT16)y);
2873 /*********************************************************************
2875 * EM_REPLACESEL
2877 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2880 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2882 UINT strl = strlenW(lpsz_replace);
2883 UINT tl = strlenW(es->text);
2884 UINT utl;
2885 UINT s;
2886 UINT e;
2887 UINT i;
2888 LPWSTR p;
2889 HRGN hrgn = 0;
2891 TRACE("%s, can_undo %d, send_update %d\n",
2892 debugstr_w(lpsz_replace), can_undo, send_update);
2894 s = es->selection_start;
2895 e = es->selection_end;
2897 if ((s == e) && !strl)
2898 return;
2900 ORDER_UINT(s, e);
2902 if (!EDIT_MakeFit(es, tl - (e - s) + strl, honor_limit))
2903 return;
2905 if (e != s) {
2906 /* there is something to be deleted */
2907 TRACE("deleting stuff.\n");
2908 if (can_undo) {
2909 utl = strlenW(es->undo_text);
2910 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2911 /* undo-buffer is extended to the right */
2912 EDIT_MakeUndoFit(es, utl + e - s);
2913 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2914 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2915 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2916 /* undo-buffer is extended to the left */
2917 EDIT_MakeUndoFit(es, utl + e - s);
2918 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2919 p[e - s] = p[0];
2920 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2921 p[i] = (es->text + s)[i];
2922 es->undo_position = s;
2923 } else {
2924 /* new undo-buffer */
2925 EDIT_MakeUndoFit(es, e - s);
2926 strncpyW(es->undo_text, es->text + s, e - s + 1);
2927 es->undo_text[e - s] = 0; /* ensure 0 termination */
2928 es->undo_position = s;
2930 /* any deletion makes the old insertion-undo invalid */
2931 es->undo_insert_count = 0;
2932 } else
2933 EDIT_EM_EmptyUndoBuffer(es);
2935 /* now delete */
2936 strcpyW(es->text + s, es->text + e);
2938 if (strl) {
2939 /* there is an insertion */
2940 if (can_undo) {
2941 if ((s == es->undo_position) ||
2942 ((es->undo_insert_count) &&
2943 (s == es->undo_position + es->undo_insert_count)))
2945 * insertion is new and at delete position or
2946 * an extension to either left or right
2948 es->undo_insert_count += strl;
2949 else {
2950 /* new insertion undo */
2951 es->undo_position = s;
2952 es->undo_insert_count = strl;
2953 /* new insertion makes old delete-buffer invalid */
2954 *es->undo_text = '\0';
2956 } else
2957 EDIT_EM_EmptyUndoBuffer(es);
2959 /* now insert */
2960 tl = strlenW(es->text);
2961 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));
2962 for (p = es->text + tl ; p >= es->text + s ; p--)
2963 p[strl] = p[0];
2964 for (i = 0 , p = es->text + s ; i < strl ; i++)
2965 p[i] = lpsz_replace[i];
2966 if(es->style & ES_UPPERCASE)
2967 CharUpperBuffW(p, strl);
2968 else if(es->style & ES_LOWERCASE)
2969 CharLowerBuffW(p, strl);
2970 s += strl;
2972 if (es->style & ES_MULTILINE)
2974 INT s = min(es->selection_start, es->selection_end);
2976 hrgn = CreateRectRgn(0, 0, 0, 0);
2977 EDIT_BuildLineDefs_ML(es, s, s + strl,
2978 strl - abs(es->selection_end - es->selection_start), hrgn);
2980 else
2981 EDIT_CalcLineWidth_SL(es);
2983 EDIT_EM_SetSel(es, s, s, FALSE);
2984 es->flags |= EF_MODIFIED;
2985 if (send_update) es->flags |= EF_UPDATE;
2986 EDIT_EM_ScrollCaret(es);
2988 /* force scroll info update */
2989 EDIT_UpdateScrollInfo(es);
2991 if (hrgn)
2993 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2994 DeleteObject(hrgn);
2996 else
2997 EDIT_UpdateText(es, NULL, TRUE);
2999 if(es->flags & EF_UPDATE)
3001 es->flags &= ~EF_UPDATE;
3002 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3007 /*********************************************************************
3009 * EM_SCROLL
3012 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3014 INT dy;
3016 if (!(es->style & ES_MULTILINE))
3017 return (LRESULT)FALSE;
3019 dy = 0;
3021 switch (action) {
3022 case SB_LINEUP:
3023 if (es->y_offset)
3024 dy = -1;
3025 break;
3026 case SB_LINEDOWN:
3027 if (es->y_offset < es->line_count - 1)
3028 dy = 1;
3029 break;
3030 case SB_PAGEUP:
3031 if (es->y_offset)
3032 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3033 break;
3034 case SB_PAGEDOWN:
3035 if (es->y_offset < es->line_count - 1)
3036 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3037 break;
3038 default:
3039 return (LRESULT)FALSE;
3041 if (dy) {
3042 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3043 /* check if we are going to move too far */
3044 if(es->y_offset + dy > es->line_count - vlc)
3045 dy = es->line_count - vlc - es->y_offset;
3047 /* Notification is done in EDIT_EM_LineScroll */
3048 if(dy)
3049 EDIT_EM_LineScroll(es, 0, dy);
3051 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3055 /*********************************************************************
3057 * EM_SCROLLCARET
3060 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3062 if (es->style & ES_MULTILINE) {
3063 INT l;
3064 INT li;
3065 INT vlc;
3066 INT ww;
3067 INT cw = es->char_width;
3068 INT x;
3069 INT dy = 0;
3070 INT dx = 0;
3072 l = EDIT_EM_LineFromChar(es, es->selection_end);
3073 li = EDIT_EM_LineIndex(es, l);
3074 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3075 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3076 if (l >= es->y_offset + vlc)
3077 dy = l - vlc + 1 - es->y_offset;
3078 if (l < es->y_offset)
3079 dy = l - es->y_offset;
3080 ww = es->format_rect.right - es->format_rect.left;
3081 if (x < es->format_rect.left)
3082 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3083 if (x > es->format_rect.right)
3084 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3085 if (dy || dx)
3087 /* check if we are going to move too far */
3088 if(es->x_offset + dx + ww > es->text_width)
3089 dx = es->text_width - ww - es->x_offset;
3090 if(dx || dy)
3091 EDIT_EM_LineScroll_internal(es, dx, dy);
3093 } else {
3094 INT x;
3095 INT goal;
3096 INT format_width;
3098 if (!(es->style & ES_AUTOHSCROLL))
3099 return;
3101 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3102 format_width = es->format_rect.right - es->format_rect.left;
3103 if (x < es->format_rect.left) {
3104 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3105 do {
3106 es->x_offset--;
3107 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3108 } while ((x < goal) && es->x_offset);
3109 /* FIXME: use ScrollWindow() somehow to improve performance */
3110 EDIT_UpdateText(es, NULL, TRUE);
3111 } else if (x > es->format_rect.right) {
3112 INT x_last;
3113 INT len = strlenW(es->text);
3114 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3115 do {
3116 es->x_offset++;
3117 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3118 x_last = SLOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3119 } while ((x > goal) && (x_last > es->format_rect.right));
3120 /* FIXME: use ScrollWindow() somehow to improve performance */
3121 EDIT_UpdateText(es, NULL, TRUE);
3125 if(es->flags & EF_FOCUSED)
3126 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3130 /*********************************************************************
3132 * EM_SETHANDLE
3134 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3137 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3139 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3141 if (!(es->style & ES_MULTILINE))
3142 return;
3144 if (!hloc) {
3145 WARN("called with NULL handle\n");
3146 return;
3149 EDIT_UnlockBuffer(es, TRUE);
3151 if(es->hloc16)
3153 LOCAL_Free(hInstance, es->hloc16);
3154 es->hloc16 = (HLOCAL16)NULL;
3157 if(es->is_unicode)
3159 if(es->hloc32A)
3161 LocalFree(es->hloc32A);
3162 es->hloc32A = NULL;
3164 es->hloc32W = hloc;
3166 else
3168 INT countW, countA;
3169 HLOCAL hloc32W_new;
3170 WCHAR *textW;
3171 CHAR *textA;
3173 countA = LocalSize(hloc);
3174 textA = LocalLock(hloc);
3175 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3176 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3178 ERR("Could not allocate new unicode buffer\n");
3179 return;
3181 textW = LocalLock(hloc32W_new);
3182 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3183 LocalUnlock(hloc32W_new);
3184 LocalUnlock(hloc);
3186 if(es->hloc32W)
3187 LocalFree(es->hloc32W);
3189 es->hloc32W = hloc32W_new;
3190 es->hloc32A = hloc;
3193 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3195 EDIT_LockBuffer(es);
3197 es->x_offset = es->y_offset = 0;
3198 es->selection_start = es->selection_end = 0;
3199 EDIT_EM_EmptyUndoBuffer(es);
3200 es->flags &= ~EF_MODIFIED;
3201 es->flags &= ~EF_UPDATE;
3202 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3203 EDIT_UpdateText(es, NULL, TRUE);
3204 EDIT_EM_ScrollCaret(es);
3205 /* force scroll info update */
3206 EDIT_UpdateScrollInfo(es);
3210 /*********************************************************************
3212 * EM_SETHANDLE16
3214 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3217 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3219 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3220 INT countW, countA;
3221 HLOCAL hloc32W_new;
3222 WCHAR *textW;
3223 CHAR *textA;
3225 if (!(es->style & ES_MULTILINE))
3226 return;
3228 if (!hloc) {
3229 WARN("called with NULL handle\n");
3230 return;
3233 EDIT_UnlockBuffer(es, TRUE);
3235 if(es->hloc32A)
3237 LocalFree(es->hloc32A);
3238 es->hloc32A = NULL;
3241 countA = LOCAL_Size(hInstance, hloc);
3242 textA = LOCAL_Lock(hInstance, hloc);
3243 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3244 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3246 ERR("Could not allocate new unicode buffer\n");
3247 return;
3249 textW = LocalLock(hloc32W_new);
3250 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3251 LocalUnlock(hloc32W_new);
3252 LOCAL_Unlock(hInstance, hloc);
3254 if(es->hloc32W)
3255 LocalFree(es->hloc32W);
3257 es->hloc32W = hloc32W_new;
3258 es->hloc16 = hloc;
3260 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3262 EDIT_LockBuffer(es);
3264 es->x_offset = es->y_offset = 0;
3265 es->selection_start = es->selection_end = 0;
3266 EDIT_EM_EmptyUndoBuffer(es);
3267 es->flags &= ~EF_MODIFIED;
3268 es->flags &= ~EF_UPDATE;
3269 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3270 EDIT_UpdateText(es, NULL, TRUE);
3271 EDIT_EM_ScrollCaret(es);
3272 /* force scroll info update */
3273 EDIT_UpdateScrollInfo(es);
3277 /*********************************************************************
3279 * EM_SETLIMITTEXT
3281 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3282 * However, the windows version is not complied to yet in all of edit.c
3284 * Additionally as the wrapper for RichEdit controls we need larger buffers
3285 * at present -1 will represent nolimit
3287 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3289 if (limit == 0xFFFFFFFF)
3290 es->buffer_limit = -1;
3291 else if (es->style & ES_MULTILINE) {
3292 if (limit)
3293 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3294 else
3295 es->buffer_limit = BUFLIMIT_MULTI;
3296 } else {
3297 if (limit)
3298 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3299 else
3300 es->buffer_limit = BUFLIMIT_SINGLE;
3305 /*********************************************************************
3307 * EM_SETMARGINS
3309 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3310 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3311 * margin according to the textmetrics of the current font.
3313 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3314 * of the char's width as the margin, but this is not how Windows handles this.
3315 * For all other fonts Windows sets the margins to zero.
3318 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3319 INT left, INT right)
3321 TEXTMETRICW tm;
3322 INT default_left_margin = 0; /* in pixels */
3323 INT default_right_margin = 0; /* in pixels */
3325 /* Set the default margins depending on the font */
3326 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3327 HDC dc = GetDC(es->hwndSelf);
3328 HFONT old_font = SelectObject(dc, es->font);
3329 GetTextMetricsW(dc, &tm);
3330 /* The default margins are only non zero for TrueType or Vector fonts */
3331 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3332 /* This must be calculated more exactly! But how? */
3333 default_left_margin = tm.tmAveCharWidth / 3;
3334 default_right_margin = tm.tmAveCharWidth / 3;
3336 SelectObject(dc, old_font);
3337 ReleaseDC(es->hwndSelf, dc);
3340 if (action & EC_LEFTMARGIN) {
3341 if (left != EC_USEFONTINFO)
3342 es->left_margin = left;
3343 else
3344 es->left_margin = default_left_margin;
3347 if (action & EC_RIGHTMARGIN) {
3348 if (right != EC_USEFONTINFO)
3349 es->right_margin = right;
3350 else
3351 es->right_margin = default_right_margin;
3353 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3357 /*********************************************************************
3359 * EM_SETPASSWORDCHAR
3362 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3364 LONG style;
3366 if (es->style & ES_MULTILINE)
3367 return;
3369 if (es->password_char == c)
3370 return;
3372 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3373 es->password_char = c;
3374 if (c) {
3375 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3376 es->style |= ES_PASSWORD;
3377 } else {
3378 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3379 es->style &= ~ES_PASSWORD;
3381 EDIT_UpdateText(es, NULL, TRUE);
3385 /*********************************************************************
3387 * EDIT_EM_SetSel
3389 * note: unlike the specs say: the order of start and end
3390 * _is_ preserved in Windows. (i.e. start can be > end)
3391 * In other words: this handler is OK
3394 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3396 UINT old_start = es->selection_start;
3397 UINT old_end = es->selection_end;
3398 UINT len = strlenW(es->text);
3400 if (start == (UINT)-1) {
3401 start = es->selection_end;
3402 end = es->selection_end;
3403 } else {
3404 start = min(start, len);
3405 end = min(end, len);
3407 es->selection_start = start;
3408 es->selection_end = end;
3409 if (after_wrap)
3410 es->flags |= EF_AFTER_WRAP;
3411 else
3412 es->flags &= ~EF_AFTER_WRAP;
3413 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3414 ORDER_UINT(start, end);
3415 ORDER_UINT(end, old_end);
3416 ORDER_UINT(start, old_start);
3417 ORDER_UINT(old_start, old_end);
3418 if (end != old_start)
3421 * One can also do
3422 * ORDER_UINT32(end, old_start);
3423 * EDIT_InvalidateText(es, start, end);
3424 * EDIT_InvalidateText(es, old_start, old_end);
3425 * in place of the following if statement.
3427 if (old_start > end )
3429 EDIT_InvalidateText(es, start, end);
3430 EDIT_InvalidateText(es, old_start, old_end);
3432 else
3434 EDIT_InvalidateText(es, start, old_start);
3435 EDIT_InvalidateText(es, end, old_end);
3438 else EDIT_InvalidateText(es, start, old_end);
3442 /*********************************************************************
3444 * EM_SETTABSTOPS
3447 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3449 if (!(es->style & ES_MULTILINE))
3450 return FALSE;
3451 if (es->tabs)
3452 HeapFree(GetProcessHeap(), 0, es->tabs);
3453 es->tabs_count = count;
3454 if (!count)
3455 es->tabs = NULL;
3456 else {
3457 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3458 memcpy(es->tabs, tabs, count * sizeof(INT));
3460 return TRUE;
3464 /*********************************************************************
3466 * EM_SETTABSTOPS16
3469 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3471 if (!(es->style & ES_MULTILINE))
3472 return FALSE;
3473 if (es->tabs)
3474 HeapFree(GetProcessHeap(), 0, es->tabs);
3475 es->tabs_count = count;
3476 if (!count)
3477 es->tabs = NULL;
3478 else {
3479 INT i;
3480 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3481 for (i = 0 ; i < count ; i++)
3482 es->tabs[i] = *tabs++;
3484 return TRUE;
3488 /*********************************************************************
3490 * EM_SETWORDBREAKPROC
3493 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam)
3495 if (es->word_break_proc == (void *)lParam)
3496 return;
3498 es->word_break_proc = (void *)lParam;
3499 es->word_break_proc16 = NULL;
3501 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3502 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3503 EDIT_UpdateText(es, NULL, TRUE);
3508 /*********************************************************************
3510 * EM_SETWORDBREAKPROC16
3513 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3515 if (es->word_break_proc16 == wbp)
3516 return;
3518 es->word_break_proc = NULL;
3519 es->word_break_proc16 = wbp;
3520 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3521 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3522 EDIT_UpdateText(es, NULL, TRUE);
3527 /*********************************************************************
3529 * EM_UNDO / WM_UNDO
3532 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3534 INT ulength;
3535 LPWSTR utext;
3537 /* Protect read-only edit control from modification */
3538 if(es->style & ES_READONLY)
3539 return FALSE;
3541 ulength = strlenW(es->undo_text);
3542 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3544 strcpyW(utext, es->undo_text);
3546 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3547 es->undo_insert_count, debugstr_w(utext));
3549 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3550 EDIT_EM_EmptyUndoBuffer(es);
3551 EDIT_EM_ReplaceSel(es, TRUE, utext, FALSE, TRUE);
3552 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3553 /* send the notification after the selection start and end are set */
3554 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3555 EDIT_EM_ScrollCaret(es);
3556 HeapFree(GetProcessHeap(), 0, utext);
3558 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3559 es->undo_insert_count, debugstr_w(es->undo_text));
3560 return TRUE;
3564 /*********************************************************************
3566 * WM_CHAR
3569 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3571 BOOL control;
3573 /* Protect read-only edit control from modification */
3574 if(es->style & ES_READONLY)
3575 return;
3577 control = GetKeyState(VK_CONTROL) & 0x8000;
3579 switch (c) {
3580 case '\r':
3581 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3582 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3583 break;
3584 case '\n':
3585 if (es->style & ES_MULTILINE) {
3586 if (es->style & ES_READONLY) {
3587 EDIT_MoveHome(es, FALSE);
3588 EDIT_MoveDown_ML(es, FALSE);
3589 } else {
3590 static const WCHAR cr_lfW[] = {'\r','\n',0};
3591 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3594 break;
3595 case '\t':
3596 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3598 static const WCHAR tabW[] = {'\t',0};
3599 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3601 break;
3602 case VK_BACK:
3603 if (!(es->style & ES_READONLY) && !control) {
3604 if (es->selection_start != es->selection_end)
3605 EDIT_WM_Clear(es);
3606 else {
3607 /* delete character left of caret */
3608 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3609 EDIT_MoveBackward(es, TRUE);
3610 EDIT_WM_Clear(es);
3613 break;
3614 case 0x03: /* ^C */
3615 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3616 break;
3617 case 0x16: /* ^V */
3618 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3619 break;
3620 case 0x18: /* ^X */
3621 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3622 break;
3624 default:
3625 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3626 WCHAR str[2];
3627 str[0] = c;
3628 str[1] = '\0';
3629 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3631 break;
3636 /*********************************************************************
3638 * WM_COMMAND
3641 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3643 if (code || control)
3644 return;
3646 switch (id) {
3647 case EM_UNDO:
3648 EDIT_EM_Undo(es);
3649 break;
3650 case WM_CUT:
3651 EDIT_WM_Cut(es);
3652 break;
3653 case WM_COPY:
3654 EDIT_WM_Copy(es);
3655 break;
3656 case WM_PASTE:
3657 EDIT_WM_Paste(es);
3658 break;
3659 case WM_CLEAR:
3660 EDIT_WM_Clear(es);
3661 break;
3662 case EM_SETSEL:
3663 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3664 EDIT_EM_ScrollCaret(es);
3665 break;
3666 default:
3667 ERR("unknown menu item, please report\n");
3668 break;
3673 /*********************************************************************
3675 * WM_CONTEXTMENU
3677 * Note: the resource files resource/sysres_??.rc cannot define a
3678 * single popup menu. Hence we use a (dummy) menubar
3679 * containing the single popup menu as its first item.
3681 * FIXME: the message identifiers have been chosen arbitrarily,
3682 * hence we use MF_BYPOSITION.
3683 * We might as well use the "real" values (anybody knows ?)
3684 * The menu definition is in resources/sysres_??.rc.
3685 * Once these are OK, we better use MF_BYCOMMAND here
3686 * (as we do in EDIT_WM_Command()).
3689 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3691 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3692 HMENU popup = GetSubMenu(menu, 0);
3693 UINT start = es->selection_start;
3694 UINT end = es->selection_end;
3696 ORDER_UINT(start, end);
3698 /* undo */
3699 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3700 /* cut */
3701 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3702 /* copy */
3703 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3704 /* paste */
3705 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3706 /* delete */
3707 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3708 /* select all */
3709 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3711 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3712 DestroyMenu(menu);
3716 /*********************************************************************
3718 * WM_COPY
3721 static void EDIT_WM_Copy(EDITSTATE *es)
3723 INT s = min(es->selection_start, es->selection_end);
3724 INT e = max(es->selection_start, es->selection_end);
3725 HGLOBAL hdst;
3726 LPWSTR dst;
3728 if (e == s) return;
3730 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3731 dst = GlobalLock(hdst);
3732 strncpyW(dst, es->text + s, e - s);
3733 dst[e - s] = 0; /* ensure 0 termination */
3734 TRACE("%s\n", debugstr_w(dst));
3735 GlobalUnlock(hdst);
3736 OpenClipboard(es->hwndSelf);
3737 EmptyClipboard();
3738 SetClipboardData(CF_UNICODETEXT, hdst);
3739 CloseClipboard();
3743 /*********************************************************************
3745 * WM_CREATE
3748 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3750 TRACE("%s\n", debugstr_w(name));
3752 * To initialize some final structure members, we call some helper
3753 * functions. However, since the EDITSTATE is not consistent (i.e.
3754 * not fully initialized), we should be very careful which
3755 * functions can be called, and in what order.
3757 EDIT_WM_SetFont(es, 0, FALSE);
3758 EDIT_EM_EmptyUndoBuffer(es);
3760 if (name && *name) {
3761 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3762 /* if we insert text to the editline, the text scrolls out
3763 * of the window, as the caret is placed after the insert
3764 * pos normally; thus we reset es->selection... to 0 and
3765 * update caret
3767 es->selection_start = es->selection_end = 0;
3768 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3769 * Messages are only to be sent when the USER does something to
3770 * change the contents. So I am removing this EN_CHANGE
3772 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3774 EDIT_EM_ScrollCaret(es);
3776 /* force scroll info update */
3777 EDIT_UpdateScrollInfo(es);
3778 return 0;
3782 /*********************************************************************
3784 * WM_DESTROY
3787 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3789 LINEDEF *pc, *pp;
3791 if (es->hloc32W) {
3792 while (LocalUnlock(es->hloc32W)) ;
3793 LocalFree(es->hloc32W);
3795 if (es->hloc32A) {
3796 while (LocalUnlock(es->hloc32A)) ;
3797 LocalFree(es->hloc32A);
3799 if (es->hloc16) {
3800 HINSTANCE16 hInstance = GetWindowWord( es->hwndSelf, GWL_HINSTANCE );
3801 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3802 LOCAL_Free(hInstance, es->hloc16);
3805 pc = es->first_line_def;
3806 while (pc)
3808 pp = pc->next;
3809 HeapFree(GetProcessHeap(), 0, pc);
3810 pc = pp;
3813 SetWindowLongW( es->hwndSelf, 0, 0 );
3814 HeapFree(GetProcessHeap(), 0, es);
3816 return 0;
3820 /*********************************************************************
3822 * WM_ERASEBKGND
3825 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3827 HBRUSH brush;
3828 RECT rc;
3830 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
3831 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3833 GetClientRect(es->hwndSelf, &rc);
3834 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3835 GetClipBox(dc, &rc);
3837 * FIXME: specs say that we should UnrealizeObject() the brush,
3838 * but the specs of UnrealizeObject() say that we shouldn't
3839 * unrealize a stock object. The default brush that
3840 * DefWndProc() returns is ... a stock object.
3842 FillRect(dc, &rc, brush);
3843 return -1;
3847 /*********************************************************************
3849 * WM_GETTEXT
3852 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3854 if(!count) return 0;
3856 if(unicode)
3858 LPWSTR textW = (LPWSTR)lParam;
3859 lstrcpynW(textW, es->text, count);
3860 return strlenW(textW);
3862 else
3864 LPSTR textA = (LPSTR)lParam;
3865 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3866 textA[count - 1] = 0; /* ensure 0 termination */
3867 return strlen(textA);
3871 /*********************************************************************
3873 * WM_HSCROLL
3876 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3878 INT dx;
3879 INT fw;
3881 if (!(es->style & ES_MULTILINE))
3882 return 0;
3884 if (!(es->style & ES_AUTOHSCROLL))
3885 return 0;
3887 dx = 0;
3888 fw = es->format_rect.right - es->format_rect.left;
3889 switch (action) {
3890 case SB_LINELEFT:
3891 TRACE("SB_LINELEFT\n");
3892 if (es->x_offset)
3893 dx = -es->char_width;
3894 break;
3895 case SB_LINERIGHT:
3896 TRACE("SB_LINERIGHT\n");
3897 if (es->x_offset < es->text_width)
3898 dx = es->char_width;
3899 break;
3900 case SB_PAGELEFT:
3901 TRACE("SB_PAGELEFT\n");
3902 if (es->x_offset)
3903 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3904 break;
3905 case SB_PAGERIGHT:
3906 TRACE("SB_PAGERIGHT\n");
3907 if (es->x_offset < es->text_width)
3908 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3909 break;
3910 case SB_LEFT:
3911 TRACE("SB_LEFT\n");
3912 if (es->x_offset)
3913 dx = -es->x_offset;
3914 break;
3915 case SB_RIGHT:
3916 TRACE("SB_RIGHT\n");
3917 if (es->x_offset < es->text_width)
3918 dx = es->text_width - es->x_offset;
3919 break;
3920 case SB_THUMBTRACK:
3921 TRACE("SB_THUMBTRACK %d\n", pos);
3922 es->flags |= EF_HSCROLL_TRACK;
3923 if(es->style & WS_HSCROLL)
3924 dx = pos - es->x_offset;
3925 else
3927 INT fw, new_x;
3928 /* Sanity check */
3929 if(pos < 0 || pos > 100) return 0;
3930 /* Assume default scroll range 0-100 */
3931 fw = es->format_rect.right - es->format_rect.left;
3932 new_x = pos * (es->text_width - fw) / 100;
3933 dx = es->text_width ? (new_x - es->x_offset) : 0;
3935 break;
3936 case SB_THUMBPOSITION:
3937 TRACE("SB_THUMBPOSITION %d\n", pos);
3938 es->flags &= ~EF_HSCROLL_TRACK;
3939 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3940 dx = pos - es->x_offset;
3941 else
3943 INT fw, new_x;
3944 /* Sanity check */
3945 if(pos < 0 || pos > 100) return 0;
3946 /* Assume default scroll range 0-100 */
3947 fw = es->format_rect.right - es->format_rect.left;
3948 new_x = pos * (es->text_width - fw) / 100;
3949 dx = es->text_width ? (new_x - es->x_offset) : 0;
3951 if (!dx) {
3952 /* force scroll info update */
3953 EDIT_UpdateScrollInfo(es);
3954 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
3956 break;
3957 case SB_ENDSCROLL:
3958 TRACE("SB_ENDSCROLL\n");
3959 break;
3961 * FIXME : the next two are undocumented !
3962 * Are we doing the right thing ?
3963 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3964 * although it's also a regular control message.
3966 case EM_GETTHUMB: /* this one is used by NT notepad */
3967 case EM_GETTHUMB16:
3969 LRESULT ret;
3970 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3971 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3972 else
3974 /* Assume default scroll range 0-100 */
3975 INT fw = es->format_rect.right - es->format_rect.left;
3976 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
3978 TRACE("EM_GETTHUMB: returning %ld\n", ret);
3979 return ret;
3981 case EM_LINESCROLL16:
3982 TRACE("EM_LINESCROLL16\n");
3983 dx = pos;
3984 break;
3986 default:
3987 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
3988 action, action);
3989 return 0;
3991 if (dx)
3993 INT fw = es->format_rect.right - es->format_rect.left;
3994 /* check if we are going to move too far */
3995 if(es->x_offset + dx + fw > es->text_width)
3996 dx = es->text_width - fw - es->x_offset;
3997 if(dx)
3998 EDIT_EM_LineScroll_internal(es, dx, 0);
4000 return 0;
4004 /*********************************************************************
4006 * EDIT_CheckCombo
4009 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4011 HWND hLBox = es->hwndListBox;
4012 HWND hCombo;
4013 BOOL bDropped;
4014 int nEUI;
4016 if (!hLBox)
4017 return FALSE;
4019 hCombo = GetParent(es->hwndSelf);
4020 bDropped = TRUE;
4021 nEUI = 0;
4023 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4025 if (key == VK_UP || key == VK_DOWN)
4027 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4028 nEUI = 1;
4030 if (msg == WM_KEYDOWN || nEUI)
4031 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4034 switch (msg)
4036 case WM_KEYDOWN:
4037 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4039 /* make sure ComboLBox pops up */
4040 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4041 key = VK_F4;
4042 nEUI = 2;
4045 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4046 break;
4048 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4049 if (nEUI)
4050 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4051 else
4052 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4053 break;
4056 if(nEUI == 2)
4057 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4059 return TRUE;
4063 /*********************************************************************
4065 * WM_KEYDOWN
4067 * Handling of special keys that don't produce a WM_CHAR
4068 * (i.e. non-printable keys) & Backspace & Delete
4071 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4073 BOOL shift;
4074 BOOL control;
4076 if (GetKeyState(VK_MENU) & 0x8000)
4077 return 0;
4079 shift = GetKeyState(VK_SHIFT) & 0x8000;
4080 control = GetKeyState(VK_CONTROL) & 0x8000;
4082 switch (key) {
4083 case VK_F4:
4084 case VK_UP:
4085 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4086 break;
4088 /* fall through */
4089 case VK_LEFT:
4090 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4091 EDIT_MoveUp_ML(es, shift);
4092 else
4093 if (control)
4094 EDIT_MoveWordBackward(es, shift);
4095 else
4096 EDIT_MoveBackward(es, shift);
4097 break;
4098 case VK_DOWN:
4099 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4100 break;
4101 /* fall through */
4102 case VK_RIGHT:
4103 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4104 EDIT_MoveDown_ML(es, shift);
4105 else if (control)
4106 EDIT_MoveWordForward(es, shift);
4107 else
4108 EDIT_MoveForward(es, shift);
4109 break;
4110 case VK_HOME:
4111 EDIT_MoveHome(es, shift);
4112 break;
4113 case VK_END:
4114 EDIT_MoveEnd(es, shift);
4115 break;
4116 case VK_PRIOR:
4117 if (es->style & ES_MULTILINE)
4118 EDIT_MovePageUp_ML(es, shift);
4119 else
4120 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4121 break;
4122 case VK_NEXT:
4123 if (es->style & ES_MULTILINE)
4124 EDIT_MovePageDown_ML(es, shift);
4125 else
4126 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4127 break;
4128 case VK_DELETE:
4129 if (!(es->style & ES_READONLY) && !(shift && control)) {
4130 if (es->selection_start != es->selection_end) {
4131 if (shift)
4132 EDIT_WM_Cut(es);
4133 else
4134 EDIT_WM_Clear(es);
4135 } else {
4136 if (shift) {
4137 /* delete character left of caret */
4138 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4139 EDIT_MoveBackward(es, TRUE);
4140 EDIT_WM_Clear(es);
4141 } else if (control) {
4142 /* delete to end of line */
4143 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4144 EDIT_MoveEnd(es, TRUE);
4145 EDIT_WM_Clear(es);
4146 } else {
4147 /* delete character right of caret */
4148 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4149 EDIT_MoveForward(es, TRUE);
4150 EDIT_WM_Clear(es);
4154 break;
4155 case VK_INSERT:
4156 if (shift) {
4157 if (!(es->style & ES_READONLY))
4158 EDIT_WM_Paste(es);
4159 } else if (control)
4160 EDIT_WM_Copy(es);
4161 break;
4162 case VK_RETURN:
4163 /* If the edit doesn't want the return send a message to the default object */
4164 if(!(es->style & ES_WANTRETURN))
4166 HWND hwndParent = GetParent(es->hwndSelf);
4167 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4168 if (HIWORD(dw) == DC_HASDEFID)
4170 SendMessageW( hwndParent, WM_COMMAND,
4171 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4172 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4175 break;
4177 return 0;
4181 /*********************************************************************
4183 * WM_KILLFOCUS
4186 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4188 es->flags &= ~EF_FOCUSED;
4189 DestroyCaret();
4190 if(!(es->style & ES_NOHIDESEL))
4191 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4192 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4193 return 0;
4197 /*********************************************************************
4199 * WM_LBUTTONDBLCLK
4201 * The caret position has been set on the WM_LBUTTONDOWN message
4204 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4206 INT s;
4207 INT e = es->selection_end;
4208 INT l;
4209 INT li;
4210 INT ll;
4212 if (!(es->flags & EF_FOCUSED))
4213 return 0;
4215 l = EDIT_EM_LineFromChar(es, e);
4216 li = EDIT_EM_LineIndex(es, l);
4217 ll = EDIT_EM_LineLength(es, e);
4218 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4219 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4220 EDIT_EM_SetSel(es, s, e, FALSE);
4221 EDIT_EM_ScrollCaret(es);
4222 return 0;
4226 /*********************************************************************
4228 * WM_LBUTTONDOWN
4231 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4233 INT e;
4234 BOOL after_wrap;
4236 if (!(es->flags & EF_FOCUSED))
4237 return 0;
4239 es->bCaptureState = TRUE;
4240 SetCapture(es->hwndSelf);
4241 EDIT_ConfinePoint(es, &x, &y);
4242 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4243 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4244 EDIT_EM_ScrollCaret(es);
4245 es->region_posx = es->region_posy = 0;
4246 SetTimer(es->hwndSelf, 0, 100, NULL);
4247 return 0;
4251 /*********************************************************************
4253 * WM_LBUTTONUP
4256 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4258 if (es->bCaptureState) {
4259 KillTimer(es->hwndSelf, 0);
4260 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4262 es->bCaptureState = FALSE;
4263 return 0;
4267 /*********************************************************************
4269 * WM_MBUTTONDOWN
4272 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4274 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4275 return 0;
4279 /*********************************************************************
4281 * WM_MOUSEMOVE
4284 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4286 INT e;
4287 BOOL after_wrap;
4288 INT prex, prey;
4290 if (GetCapture() != es->hwndSelf)
4291 return 0;
4294 * FIXME: gotta do some scrolling if outside client
4295 * area. Maybe reset the timer ?
4297 prex = x; prey = y;
4298 EDIT_ConfinePoint(es, &x, &y);
4299 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4300 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4301 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4302 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4303 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4304 return 0;
4308 /*********************************************************************
4310 * WM_NCCREATE
4312 * See also EDIT_WM_StyleChanged
4314 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4316 EDITSTATE *es;
4317 UINT alloc_size;
4319 TRACE("Creating %s edit control, style = %08lx\n",
4320 unicode ? "Unicode" : "ANSI", lpcs->style);
4322 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4323 return FALSE;
4324 SetWindowLongW( hwnd, 0, (LONG)es );
4327 * Note: since the EDITSTATE has not been fully initialized yet,
4328 * we can't use any API calls that may send
4329 * WM_XXX messages before WM_NCCREATE is completed.
4332 es->is_unicode = unicode;
4333 es->style = lpcs->style;
4335 es->bEnableState = !(es->style & WS_DISABLED);
4337 es->hwndSelf = hwnd;
4338 /* Save parent, which will be notified by EN_* messages */
4339 es->hwndParent = lpcs->hwndParent;
4341 if (es->style & ES_COMBO)
4342 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4344 /* Number overrides lowercase overrides uppercase (at least it
4345 * does in Win95). However I'll bet that ES_NUMBER would be
4346 * invalid under Win 3.1.
4348 if (es->style & ES_NUMBER) {
4349 ; /* do not override the ES_NUMBER */
4350 } else if (es->style & ES_LOWERCASE) {
4351 es->style &= ~ES_UPPERCASE;
4353 if (es->style & ES_MULTILINE) {
4354 es->buffer_limit = BUFLIMIT_MULTI;
4355 if (es->style & WS_VSCROLL)
4356 es->style |= ES_AUTOVSCROLL;
4357 if (es->style & WS_HSCROLL)
4358 es->style |= ES_AUTOHSCROLL;
4359 es->style &= ~ES_PASSWORD;
4360 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4361 /* Confirmed - RIGHT overrides CENTER */
4362 if (es->style & ES_RIGHT)
4363 es->style &= ~ES_CENTER;
4364 es->style &= ~WS_HSCROLL;
4365 es->style &= ~ES_AUTOHSCROLL;
4368 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4369 es->style |= ES_AUTOVSCROLL;
4370 } else {
4371 es->buffer_limit = BUFLIMIT_SINGLE;
4372 if (WIN31_LOOK == TWEAK_WineLook ||
4373 WIN95_LOOK == TWEAK_WineLook) {
4374 es->style &= ~ES_CENTER;
4375 es->style &= ~ES_RIGHT;
4376 } else {
4377 if (es->style & ES_RIGHT)
4378 es->style &= ~ES_CENTER;
4380 es->style &= ~WS_HSCROLL;
4381 es->style &= ~WS_VSCROLL;
4382 es->style &= ~ES_AUTOVSCROLL;
4383 es->style &= ~ES_WANTRETURN;
4384 if (es->style & ES_PASSWORD)
4385 es->password_char = '*';
4387 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4388 es->style |= ES_AUTOHSCROLL;
4391 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4392 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4393 return FALSE;
4394 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4396 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4397 return FALSE;
4398 es->undo_buffer_size = es->buffer_size;
4400 if (es->style & ES_MULTILINE)
4401 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4402 return FALSE;
4403 es->line_count = 1;
4406 * In Win95 look and feel, the WS_BORDER style is replaced by the
4407 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4408 * control a non client area. Not always. This coordinates in some
4409 * way with the window creation code in dialog.c When making
4410 * modifications please ensure that the code still works for edit
4411 * controls created directly with style 0x50800000, exStyle 0 (
4412 * which should have a single pixel border)
4414 if (TWEAK_WineLook != WIN31_LOOK)
4416 es->style &= ~WS_BORDER;
4418 else
4420 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4421 SetWindowLongW( hwnd, GWL_STYLE,
4422 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_BORDER );
4425 return TRUE;
4428 /*********************************************************************
4430 * WM_PAINT
4433 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam)
4435 PAINTSTRUCT ps;
4436 INT i;
4437 HDC dc;
4438 HFONT old_font = 0;
4439 RECT rc;
4440 RECT rcLine;
4441 RECT rcRgn;
4442 BOOL rev = es->bEnableState &&
4443 ((es->flags & EF_FOCUSED) ||
4444 (es->style & ES_NOHIDESEL));
4445 if (!wParam)
4446 dc = BeginPaint(es->hwndSelf, &ps);
4447 else
4448 dc = (HDC) wParam;
4449 if(es->style & WS_BORDER) {
4450 GetClientRect(es->hwndSelf, &rc);
4451 if(es->style & ES_MULTILINE) {
4452 if(es->style & WS_HSCROLL) rc.bottom++;
4453 if(es->style & WS_VSCROLL) rc.right++;
4455 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4457 IntersectClipRect(dc, es->format_rect.left,
4458 es->format_rect.top,
4459 es->format_rect.right,
4460 es->format_rect.bottom);
4461 if (es->style & ES_MULTILINE) {
4462 GetClientRect(es->hwndSelf, &rc);
4463 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4465 if (es->font)
4466 old_font = SelectObject(dc, es->font);
4467 EDIT_NotifyCtlColor(es, dc);
4469 if (!es->bEnableState)
4470 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4471 GetClipBox(dc, &rcRgn);
4472 if (es->style & ES_MULTILINE) {
4473 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4474 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4475 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4476 if (IntersectRect(&rc, &rcRgn, &rcLine))
4477 EDIT_PaintLine(es, dc, i, rev);
4479 } else {
4480 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4481 if (IntersectRect(&rc, &rcRgn, &rcLine))
4482 EDIT_PaintLine(es, dc, 0, rev);
4484 if (es->font)
4485 SelectObject(dc, old_font);
4487 if (!wParam)
4488 EndPaint(es->hwndSelf, &ps);
4492 /*********************************************************************
4494 * WM_PASTE
4497 static void EDIT_WM_Paste(EDITSTATE *es)
4499 HGLOBAL hsrc;
4500 LPWSTR src;
4502 /* Protect read-only edit control from modification */
4503 if(es->style & ES_READONLY)
4504 return;
4506 OpenClipboard(es->hwndSelf);
4507 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4508 src = (LPWSTR)GlobalLock(hsrc);
4509 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4510 GlobalUnlock(hsrc);
4512 CloseClipboard();
4516 /*********************************************************************
4518 * WM_SETFOCUS
4521 static void EDIT_WM_SetFocus(EDITSTATE *es)
4523 es->flags |= EF_FOCUSED;
4524 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4525 EDIT_SetCaretPos(es, es->selection_end,
4526 es->flags & EF_AFTER_WRAP);
4527 if(!(es->style & ES_NOHIDESEL))
4528 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4529 ShowCaret(es->hwndSelf);
4530 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4534 /*********************************************************************
4536 * WM_SETFONT
4538 * With Win95 look the margins are set to default font value unless
4539 * the system font (font == 0) is being set, in which case they are left
4540 * unchanged.
4543 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4545 TEXTMETRICW tm;
4546 HDC dc;
4547 HFONT old_font = 0;
4548 RECT r;
4550 es->font = font;
4551 dc = GetDC(es->hwndSelf);
4552 if (font)
4553 old_font = SelectObject(dc, font);
4554 GetTextMetricsW(dc, &tm);
4555 es->line_height = tm.tmHeight;
4556 es->char_width = tm.tmAveCharWidth;
4557 if (font)
4558 SelectObject(dc, old_font);
4559 ReleaseDC(es->hwndSelf, dc);
4560 if (font && (TWEAK_WineLook > WIN31_LOOK))
4561 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4562 EC_USEFONTINFO, EC_USEFONTINFO);
4564 /* Force the recalculation of the format rect for each font change */
4565 GetClientRect(es->hwndSelf, &r);
4566 EDIT_SetRectNP(es, &r);
4568 if (es->style & ES_MULTILINE)
4569 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4570 else
4571 EDIT_CalcLineWidth_SL(es);
4573 if (redraw)
4574 EDIT_UpdateText(es, NULL, TRUE);
4575 if (es->flags & EF_FOCUSED) {
4576 DestroyCaret();
4577 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4578 EDIT_SetCaretPos(es, es->selection_end,
4579 es->flags & EF_AFTER_WRAP);
4580 ShowCaret(es->hwndSelf);
4585 /*********************************************************************
4587 * WM_SETTEXT
4589 * NOTES
4590 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4591 * The modified flag is reset. No notifications are sent.
4593 * For single-line controls, reception of WM_SETTEXT triggers:
4594 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4597 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode)
4599 LPWSTR text = NULL;
4601 if(unicode)
4602 text = (LPWSTR)lParam;
4603 else if (lParam)
4605 LPCSTR textA = (LPCSTR)lParam;
4606 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4607 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4608 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4611 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4612 if (text) {
4613 TRACE("%s\n", debugstr_w(text));
4614 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4615 if(!unicode)
4616 HeapFree(GetProcessHeap(), 0, text);
4617 } else {
4618 static const WCHAR empty_stringW[] = {0};
4619 TRACE("<NULL>\n");
4620 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4622 es->x_offset = 0;
4623 es->flags &= ~EF_MODIFIED;
4624 EDIT_EM_SetSel(es, 0, 0, FALSE);
4625 /* Send the notification after the selection start and end have been set
4626 * edit control doesn't send notification on WM_SETTEXT
4627 * if it is multiline, or it is part of combobox
4629 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4631 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4632 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4634 EDIT_EM_ScrollCaret(es);
4638 /*********************************************************************
4640 * WM_SIZE
4643 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4645 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4646 RECT rc;
4647 TRACE("width = %d, height = %d\n", width, height);
4648 SetRect(&rc, 0, 0, width, height);
4649 EDIT_SetRectNP(es, &rc);
4650 EDIT_UpdateText(es, NULL, TRUE);
4655 /*********************************************************************
4657 * WM_STYLECHANGED
4659 * This message is sent by SetWindowLong on having changed either the Style
4660 * or the extended style.
4662 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4664 * See also EDIT_WM_NCCreate
4666 * It appears that the Windows version of the edit control allows the style
4667 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4668 * style variable which will generally be different. In this function we
4669 * update the internal style based on what changed in the externally visible
4670 * style.
4672 * Much of this content as based upon the MSDN, especially:
4673 * Platform SDK Documentation -> User Interface Services ->
4674 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4675 * Edit Control Styles
4677 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4679 if (GWL_STYLE == which) {
4680 DWORD style_change_mask;
4681 DWORD new_style;
4682 /* Only a subset of changes can be applied after the control
4683 * has been created.
4685 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4686 ES_NUMBER;
4687 if (es->style & ES_MULTILINE)
4688 style_change_mask |= ES_WANTRETURN;
4690 new_style = style->styleNew & style_change_mask;
4692 /* Number overrides lowercase overrides uppercase (at least it
4693 * does in Win95). However I'll bet that ES_NUMBER would be
4694 * invalid under Win 3.1.
4696 if (new_style & ES_NUMBER) {
4697 ; /* do not override the ES_NUMBER */
4698 } else if (new_style & ES_LOWERCASE) {
4699 new_style &= ~ES_UPPERCASE;
4702 es->style = (es->style & ~style_change_mask) | new_style;
4703 } else if (GWL_EXSTYLE == which) {
4704 ; /* FIXME - what is needed here */
4705 } else {
4706 WARN ("Invalid style change %d\n",which);
4709 return 0;
4712 /*********************************************************************
4714 * WM_SYSKEYDOWN
4717 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4719 if ((key == VK_BACK) && (key_data & 0x2000)) {
4720 if (EDIT_EM_CanUndo(es))
4721 EDIT_EM_Undo(es);
4722 return 0;
4723 } else if (key == VK_UP || key == VK_DOWN) {
4724 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4725 return 0;
4727 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4731 /*********************************************************************
4733 * WM_TIMER
4736 static void EDIT_WM_Timer(EDITSTATE *es)
4738 if (es->region_posx < 0) {
4739 EDIT_MoveBackward(es, TRUE);
4740 } else if (es->region_posx > 0) {
4741 EDIT_MoveForward(es, TRUE);
4744 * FIXME: gotta do some vertical scrolling here, like
4745 * EDIT_EM_LineScroll(hwnd, 0, 1);
4749 /*********************************************************************
4751 * WM_VSCROLL
4754 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4756 INT dy;
4758 if (!(es->style & ES_MULTILINE))
4759 return 0;
4761 if (!(es->style & ES_AUTOVSCROLL))
4762 return 0;
4764 dy = 0;
4765 switch (action) {
4766 case SB_LINEUP:
4767 case SB_LINEDOWN:
4768 case SB_PAGEUP:
4769 case SB_PAGEDOWN:
4770 TRACE("action %d\n", action);
4771 EDIT_EM_Scroll(es, action);
4772 return 0;
4773 case SB_TOP:
4774 TRACE("SB_TOP\n");
4775 dy = -es->y_offset;
4776 break;
4777 case SB_BOTTOM:
4778 TRACE("SB_BOTTOM\n");
4779 dy = es->line_count - 1 - es->y_offset;
4780 break;
4781 case SB_THUMBTRACK:
4782 TRACE("SB_THUMBTRACK %d\n", pos);
4783 es->flags |= EF_VSCROLL_TRACK;
4784 if(es->style & WS_VSCROLL)
4785 dy = pos - es->y_offset;
4786 else
4788 /* Assume default scroll range 0-100 */
4789 INT vlc, new_y;
4790 /* Sanity check */
4791 if(pos < 0 || pos > 100) return 0;
4792 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4793 new_y = pos * (es->line_count - vlc) / 100;
4794 dy = es->line_count ? (new_y - es->y_offset) : 0;
4795 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4796 es->line_count, es->y_offset, pos, dy);
4798 break;
4799 case SB_THUMBPOSITION:
4800 TRACE("SB_THUMBPOSITION %d\n", pos);
4801 es->flags &= ~EF_VSCROLL_TRACK;
4802 if(es->style & WS_VSCROLL)
4803 dy = pos - es->y_offset;
4804 else
4806 /* Assume default scroll range 0-100 */
4807 INT vlc, new_y;
4808 /* Sanity check */
4809 if(pos < 0 || pos > 100) return 0;
4810 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4811 new_y = pos * (es->line_count - vlc) / 100;
4812 dy = es->line_count ? (new_y - es->y_offset) : 0;
4813 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4814 es->line_count, es->y_offset, pos, dy);
4816 if (!dy)
4818 /* force scroll info update */
4819 EDIT_UpdateScrollInfo(es);
4820 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4822 break;
4823 case SB_ENDSCROLL:
4824 TRACE("SB_ENDSCROLL\n");
4825 break;
4827 * FIXME : the next two are undocumented !
4828 * Are we doing the right thing ?
4829 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4830 * although it's also a regular control message.
4832 case EM_GETTHUMB: /* this one is used by NT notepad */
4833 case EM_GETTHUMB16:
4835 LRESULT ret;
4836 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4837 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4838 else
4840 /* Assume default scroll range 0-100 */
4841 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4842 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4844 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4845 return ret;
4847 case EM_LINESCROLL16:
4848 TRACE("EM_LINESCROLL16 %d\n", pos);
4849 dy = pos;
4850 break;
4852 default:
4853 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4854 action, action);
4855 return 0;
4857 if (dy)
4858 EDIT_EM_LineScroll(es, 0, dy);
4859 return 0;
4862 /*********************************************************************
4864 * EDIT_UpdateText
4867 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4869 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4870 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4874 /*********************************************************************
4876 * EDIT_UpdateText
4879 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4881 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4882 InvalidateRect(es->hwndSelf, rc, bErase);