Fix compile error.
[wine/multimedia.git] / controls / edit.c
blob14e362947952e302b7ce6b703d60ecd90be4b257
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * TODO:
24 * - ES_CENTER
25 * - ES_RIGHT
26 * - ES_NUMBER (new since win95)
27 * - ES_OEMCONVERT
28 * -!ES_AUTOVSCROLL (every multi line control *is* auto vscroll)
29 * -!ES_AUTOHSCROLL (every single line control *is* auto hscroll)
31 * When there is no autoscrolling, the control should first check whether
32 * the new text would fit. If not, an EN_MAXTEXT should be sent.
33 * However, currently this would require the actual change to be made,
34 * then call EDIT_BuildLineDefs() and then find out that the new text doesn't
35 * fit. After all this, things should be put back in the state before the
36 * changes. Note that for multi line controls !ES_AUTOHSCROLL works : wordwrap.
40 #include "config.h"
42 #include <string.h>
43 #include <stdlib.h>
45 #include "winbase.h"
46 #include "winnt.h"
47 #include "wownt32.h"
48 #include "win.h"
49 #include "wine/winbase16.h"
50 #include "wine/winuser16.h"
51 #include "wine/unicode.h"
52 #include "controls.h"
53 #include "local.h"
54 #include "user.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(edit);
58 WINE_DECLARE_DEBUG_CHANNEL(combo);
59 WINE_DECLARE_DEBUG_CHANNEL(relay);
61 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
62 FIXME: BTW, new specs say 65535 (do you dare ???) */
63 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
64 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
65 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
66 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
69 * extra flags for EDITSTATE.flags field
71 #define EF_MODIFIED 0x0001 /* text has been modified */
72 #define EF_FOCUSED 0x0002 /* we have input focus */
73 #define EF_UPDATE 0x0004 /* notify parent of changed state */
74 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
75 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
76 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
77 wrapped line, instead of in front of the next character */
78 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
80 typedef enum
82 END_0 = 0, /* line ends with terminating '\0' character */
83 END_WRAP, /* line is wrapped */
84 END_HARD, /* line ends with a hard return '\r\n' */
85 END_SOFT, /* line ends with a soft return '\r\r\n' */
86 END_RICH /* line ends with a single '\n' */
87 } LINE_END;
89 typedef struct tagLINEDEF {
90 INT length; /* bruto length of a line in bytes */
91 INT net_length; /* netto length of a line in visible characters */
92 LINE_END ending;
93 INT width; /* width of the line in pixels */
94 INT index; /* line index into the buffer */
95 struct tagLINEDEF *next;
96 } LINEDEF;
98 typedef struct
100 BOOL is_unicode; /* how the control was created */
101 LPWSTR text; /* the actual contents of the control */
102 UINT buffer_size; /* the size of the buffer in characters */
103 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
104 HFONT font; /* NULL means standard system font */
105 INT x_offset; /* scroll offset for multi lines this is in pixels
106 for single lines it's in characters */
107 INT line_height; /* height of a screen line in pixels */
108 INT char_width; /* average character width in pixels */
109 DWORD style; /* sane version of wnd->dwStyle */
110 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
111 INT undo_insert_count; /* number of characters inserted in sequence */
112 UINT undo_position; /* character index of the insertion and deletion */
113 LPWSTR undo_text; /* deleted text */
114 UINT undo_buffer_size; /* size of the deleted text buffer */
115 INT selection_start; /* == selection_end if no selection */
116 INT selection_end; /* == current caret position */
117 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
118 INT left_margin; /* in pixels */
119 INT right_margin; /* in pixels */
120 RECT format_rect;
121 INT text_width; /* width of the widest line in pixels for multi line controls
122 and just line width for single line controls */
123 INT region_posx; /* Position of cursor relative to region: */
124 INT region_posy; /* -1: to left, 0: within, 1: to right */
125 EDITWORDBREAKPROC16 word_break_proc16;
126 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
127 INT line_count; /* number of lines */
128 INT y_offset; /* scroll offset in number of lines */
129 BOOL bCaptureState; /* flag indicating whether mouse was captured */
130 BOOL bEnableState; /* flag keeping the enable state */
131 HWND hwndSelf; /* the our window handle */
132 HWND hwndParent; /* Handle of parent for sending EN_* messages.
133 Even if parent will change, EN_* messages
134 should be sent to the first parent. */
135 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
137 * only for multi line controls
139 INT lock_count; /* amount of re-entries in the EditWndProc */
140 INT tabs_count;
141 LPINT tabs;
142 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
143 HLOCAL hloc32W; /* our unicode local memory block */
144 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
145 or EM_SETHANDLE16 */
146 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
147 or EM_SETHANDLE */
148 } EDITSTATE;
151 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
152 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
154 /* used for disabled or read-only edit control */
155 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
156 do \
157 { /* Notify parent which has created this edit control */ \
158 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
159 SendMessageW(es->hwndParent, WM_COMMAND, \
160 MAKEWPARAM(GetWindowLongW((es->hwndSelf),GWL_ID), wNotifyCode), \
161 (LPARAM)(es->hwndSelf)); \
162 } while(0)
164 /*********************************************************************
166 * Declarations
171 * These functions have trivial implementations
172 * We still like to call them internally
173 * "static inline" makes them more like macro's
175 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
176 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
177 static inline void EDIT_WM_Clear(EDITSTATE *es);
178 static inline void EDIT_WM_Cut(EDITSTATE *es);
181 * Helper functions only valid for one type of control
183 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
184 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
185 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
186 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
187 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
188 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
189 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
191 * Helper functions valid for both single line _and_ multi line controls
193 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
194 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
195 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
196 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
197 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
198 static void EDIT_LockBuffer(EDITSTATE *es);
199 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size, BOOL honor_limit);
200 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
201 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
202 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
203 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
204 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
205 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
206 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
207 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
208 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
209 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
210 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
211 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
212 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
213 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
215 * EM_XXX message handlers
217 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
218 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
219 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
220 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
221 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
222 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
223 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
224 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
225 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
226 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
227 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
228 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
229 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
230 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
231 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
232 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
233 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
234 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
235 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
236 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
237 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
238 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
239 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
240 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
241 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam);
242 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
243 static BOOL EDIT_EM_Undo(EDITSTATE *es);
245 * WM_XXX message handlers
247 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
248 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
249 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
250 static void EDIT_WM_Copy(EDITSTATE *es);
251 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
252 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
253 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
254 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
255 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
256 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
257 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
258 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
259 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
260 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
261 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
262 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
263 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
264 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam);
265 static void EDIT_WM_Paste(EDITSTATE *es);
266 static void EDIT_WM_SetFocus(EDITSTATE *es);
267 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
268 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode);
269 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
270 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
271 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
272 static void EDIT_WM_Timer(EDITSTATE *es);
273 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
274 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
275 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
277 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
278 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
280 /*********************************************************************
281 * edit class descriptor
283 const struct builtin_class_descr EDIT_builtin_class =
285 "Edit", /* name */
286 CS_GLOBALCLASS | CS_DBLCLKS | CS_PARENTDC, /* style */
287 EditWndProcA, /* procA */
288 EditWndProcW, /* procW */
289 sizeof(EDITSTATE *), /* extra */
290 IDC_IBEAMA, /* cursor */
291 0 /* brush */
295 /*********************************************************************
297 * EM_CANUNDO
300 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
302 return (es->undo_insert_count || strlenW(es->undo_text));
306 /*********************************************************************
308 * EM_EMPTYUNDOBUFFER
311 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
313 es->undo_insert_count = 0;
314 *es->undo_text = '\0';
318 /*********************************************************************
320 * WM_CLEAR
323 static inline void EDIT_WM_Clear(EDITSTATE *es)
325 static const WCHAR empty_stringW[] = {0};
327 /* Protect read-only edit control from modification */
328 if(es->style & ES_READONLY)
329 return;
331 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
335 /*********************************************************************
337 * WM_CUT
340 static inline void EDIT_WM_Cut(EDITSTATE *es)
342 EDIT_WM_Copy(es);
343 EDIT_WM_Clear(es);
347 /**********************************************************************
348 * get_app_version
350 * Returns the window version in case Wine emulates a later version
351 * of windows then the application expects.
353 * In a number of cases when windows runs an application that was
354 * designed for an earlier windows version, windows reverts
355 * to "old" behaviour of that earlier version.
357 * An example is a disabled edit control that needs to be painted.
358 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
359 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
360 * applications with an expected version 0f 4.0 or higher.
363 static DWORD get_app_version(void)
365 static DWORD version;
366 if (!version)
368 DWORD dwEmulatedVersion;
369 OSVERSIONINFOW info;
370 DWORD dwProcVersion = GetProcessVersion(0);
372 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
373 GetVersionExW( &info );
374 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
375 /* FIXME: this may not be 100% correct; see discussion on the
376 * wine developer list in Nov 1999 */
377 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
379 return version;
383 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
385 UINT msg;
387 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
388 msg = WM_CTLCOLORSTATIC;
389 else
390 msg = WM_CTLCOLOREDIT;
392 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
393 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
396 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
398 if(unicode)
399 return DefWindowProcW(hwnd, msg, wParam, lParam);
400 else
401 return DefWindowProcA(hwnd, msg, wParam, lParam);
404 /*********************************************************************
406 * EditWndProc_common
408 * The messages are in the order of the actual integer values
409 * (which can be found in include/windows.h)
410 * Wherever possible the 16 bit versions are converted to
411 * the 32 bit ones, so that we can 'fall through' to the
412 * helper functions. These are mostly 32 bit (with a few
413 * exceptions, clearly indicated by a '16' extension to their
414 * names).
417 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
418 WPARAM wParam, LPARAM lParam, BOOL unicode )
420 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
421 LRESULT result = 0;
423 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, msg, wParam, lParam);
425 if (!es && msg != WM_NCCREATE)
426 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
427 else if (msg == WM_NCCREATE)
428 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
429 else if (msg == WM_DESTROY)
430 return EDIT_WM_Destroy(es);
433 if (es) EDIT_LockBuffer(es);
435 switch (msg) {
436 case EM_GETSEL16:
437 wParam = 0;
438 lParam = 0;
439 /* fall through */
440 case EM_GETSEL:
441 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
442 break;
444 case EM_SETSEL16:
445 if (SLOWORD(lParam) == -1)
446 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
447 else
448 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
449 if (!wParam)
450 EDIT_EM_ScrollCaret(es);
451 result = 1;
452 break;
453 case EM_SETSEL:
454 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
455 EDIT_EM_ScrollCaret(es);
456 result = 1;
457 break;
459 case EM_GETRECT16:
460 if (lParam)
461 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
462 break;
463 case EM_GETRECT:
464 if (lParam)
465 CopyRect((LPRECT)lParam, &es->format_rect);
466 break;
468 case EM_SETRECT16:
469 if ((es->style & ES_MULTILINE) && lParam) {
470 RECT rc;
471 CONV_RECT16TO32(MapSL(lParam), &rc);
472 EDIT_SetRectNP(es, &rc);
473 EDIT_UpdateText(es, NULL, TRUE);
475 break;
476 case EM_SETRECT:
477 if ((es->style & ES_MULTILINE) && lParam) {
478 EDIT_SetRectNP(es, (LPRECT)lParam);
479 EDIT_UpdateText(es, NULL, TRUE);
481 break;
483 case EM_SETRECTNP16:
484 if ((es->style & ES_MULTILINE) && lParam) {
485 RECT rc;
486 CONV_RECT16TO32(MapSL(lParam), &rc);
487 EDIT_SetRectNP(es, &rc);
489 break;
490 case EM_SETRECTNP:
491 if ((es->style & ES_MULTILINE) && lParam)
492 EDIT_SetRectNP(es, (LPRECT)lParam);
493 break;
495 case EM_SCROLL16:
496 case EM_SCROLL:
497 result = EDIT_EM_Scroll(es, (INT)wParam);
498 break;
500 case EM_LINESCROLL16:
501 wParam = (WPARAM)(INT)SHIWORD(lParam);
502 lParam = (LPARAM)(INT)SLOWORD(lParam);
503 /* fall through */
504 case EM_LINESCROLL:
505 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
506 break;
508 case EM_SCROLLCARET16:
509 case EM_SCROLLCARET:
510 EDIT_EM_ScrollCaret(es);
511 result = 1;
512 break;
514 case EM_GETMODIFY16:
515 case EM_GETMODIFY:
516 result = ((es->flags & EF_MODIFIED) != 0);
517 break;
519 case EM_SETMODIFY16:
520 case EM_SETMODIFY:
521 if (wParam)
522 es->flags |= EF_MODIFIED;
523 else
524 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
525 break;
527 case EM_GETLINECOUNT16:
528 case EM_GETLINECOUNT:
529 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
530 break;
532 case EM_LINEINDEX16:
533 if ((INT16)wParam == -1)
534 wParam = (WPARAM)-1;
535 /* fall through */
536 case EM_LINEINDEX:
537 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
538 break;
540 case EM_SETHANDLE16:
541 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
542 break;
543 case EM_SETHANDLE:
544 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
545 break;
547 case EM_GETHANDLE16:
548 result = (LRESULT)EDIT_EM_GetHandle16(es);
549 break;
550 case EM_GETHANDLE:
551 result = (LRESULT)EDIT_EM_GetHandle(es);
552 break;
554 case EM_GETTHUMB16:
555 case EM_GETTHUMB:
556 result = EDIT_EM_GetThumb(es);
557 break;
559 /* these messages missing from specs */
560 case WM_USER+15:
561 case 0x00bf:
562 case WM_USER+16:
563 case 0x00c0:
564 case WM_USER+19:
565 case 0x00c3:
566 case WM_USER+26:
567 case 0x00ca:
568 FIXME("undocumented message 0x%x, please report\n", msg);
569 result = DefWindowProcW(hwnd, msg, wParam, lParam);
570 break;
572 case EM_LINELENGTH16:
573 case EM_LINELENGTH:
574 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
575 break;
577 case EM_REPLACESEL16:
578 lParam = (LPARAM)MapSL(lParam);
579 unicode = FALSE; /* 16-bit message is always ascii */
580 /* fall through */
581 case EM_REPLACESEL:
583 LPWSTR textW;
585 if(unicode)
586 textW = (LPWSTR)lParam;
587 else
589 LPSTR textA = (LPSTR)lParam;
590 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
591 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
592 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
595 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
596 result = 1;
598 if(!unicode)
599 HeapFree(GetProcessHeap(), 0, textW);
600 break;
603 case EM_GETLINE16:
604 lParam = (LPARAM)MapSL(lParam);
605 unicode = FALSE; /* 16-bit message is always ascii */
606 /* fall through */
607 case EM_GETLINE:
608 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
609 break;
611 case EM_LIMITTEXT16:
612 case EM_SETLIMITTEXT:
613 EDIT_EM_SetLimitText(es, (INT)wParam);
614 break;
616 case EM_CANUNDO16:
617 case EM_CANUNDO:
618 result = (LRESULT)EDIT_EM_CanUndo(es);
619 break;
621 case EM_UNDO16:
622 case EM_UNDO:
623 case WM_UNDO:
624 result = (LRESULT)EDIT_EM_Undo(es);
625 break;
627 case EM_FMTLINES16:
628 case EM_FMTLINES:
629 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
630 break;
632 case EM_LINEFROMCHAR16:
633 case EM_LINEFROMCHAR:
634 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
635 break;
637 case EM_SETTABSTOPS16:
638 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
639 break;
640 case EM_SETTABSTOPS:
641 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
642 break;
644 case EM_SETPASSWORDCHAR16:
645 unicode = FALSE; /* 16-bit message is always ascii */
646 /* fall through */
647 case EM_SETPASSWORDCHAR:
649 WCHAR charW = 0;
651 if(unicode)
652 charW = (WCHAR)wParam;
653 else
655 CHAR charA = wParam;
656 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
659 EDIT_EM_SetPasswordChar(es, charW);
660 break;
663 case EM_EMPTYUNDOBUFFER16:
664 case EM_EMPTYUNDOBUFFER:
665 EDIT_EM_EmptyUndoBuffer(es);
666 break;
668 case EM_GETFIRSTVISIBLELINE16:
669 result = es->y_offset;
670 break;
671 case EM_GETFIRSTVISIBLELINE:
672 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
673 break;
675 case EM_SETREADONLY16:
676 case EM_SETREADONLY:
677 if (wParam) {
678 SetWindowLongW( hwnd, GWL_STYLE,
679 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
680 es->style |= ES_READONLY;
681 } else {
682 SetWindowLongW( hwnd, GWL_STYLE,
683 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
684 es->style &= ~ES_READONLY;
686 result = 1;
687 break;
689 case EM_SETWORDBREAKPROC16:
690 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
691 break;
692 case EM_SETWORDBREAKPROC:
693 EDIT_EM_SetWordBreakProc(es, lParam);
694 break;
696 case EM_GETWORDBREAKPROC16:
697 result = (LRESULT)es->word_break_proc16;
698 break;
699 case EM_GETWORDBREAKPROC:
700 result = (LRESULT)es->word_break_proc;
701 break;
703 case EM_GETPASSWORDCHAR16:
704 unicode = FALSE; /* 16-bit message is always ascii */
705 /* fall through */
706 case EM_GETPASSWORDCHAR:
708 if(unicode)
709 result = es->password_char;
710 else
712 WCHAR charW = es->password_char;
713 CHAR charA = 0;
714 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
715 result = charA;
717 break;
720 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
722 case EM_SETMARGINS:
723 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
724 break;
726 case EM_GETMARGINS:
727 result = MAKELONG(es->left_margin, es->right_margin);
728 break;
730 case EM_GETLIMITTEXT:
731 result = es->buffer_limit;
732 break;
734 case EM_POSFROMCHAR:
735 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
736 break;
738 case EM_CHARFROMPOS:
739 result = EDIT_EM_CharFromPos(es, SLOWORD(lParam), SHIWORD(lParam));
740 break;
742 /* End of the EM_ messages which were in numerical order; what order
743 * are these in? vaguely alphabetical?
746 case WM_GETDLGCODE:
747 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
749 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
751 int vk = (int)((LPMSG)lParam)->wParam;
753 if (vk == VK_RETURN && (GetWindowLongW( hwnd, GWL_STYLE ) & ES_WANTRETURN))
755 result |= DLGC_WANTMESSAGE;
757 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
759 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
760 result |= DLGC_WANTMESSAGE;
763 break;
765 case WM_IME_CHAR:
766 if (!unicode)
768 WCHAR charW;
769 CHAR strng[2];
771 strng[0] = wParam >> 8;
772 strng[1] = wParam & 0xff;
773 MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
774 EDIT_WM_Char(es, charW);
775 break;
777 /* fall through */
778 case WM_CHAR:
780 WCHAR charW;
782 if(unicode)
783 charW = wParam;
784 else
786 CHAR charA = wParam;
787 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
790 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
792 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
793 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
794 break;
796 EDIT_WM_Char(es, charW);
797 break;
800 case WM_CLEAR:
801 EDIT_WM_Clear(es);
802 break;
804 case WM_COMMAND:
805 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
806 break;
808 case WM_CONTEXTMENU:
809 EDIT_WM_ContextMenu(es, SLOWORD(lParam), SHIWORD(lParam));
810 break;
812 case WM_COPY:
813 EDIT_WM_Copy(es);
814 break;
816 case WM_CREATE:
817 if(unicode)
818 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
819 else
821 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
822 LPWSTR nameW = NULL;
823 if(nameA)
825 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
826 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
827 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
829 result = EDIT_WM_Create(es, nameW);
830 if(nameW)
831 HeapFree(GetProcessHeap(), 0, nameW);
833 break;
835 case WM_CUT:
836 EDIT_WM_Cut(es);
837 break;
839 case WM_ENABLE:
840 es->bEnableState = (BOOL) wParam;
841 EDIT_UpdateText(es, NULL, TRUE);
842 break;
844 case WM_ERASEBKGND:
845 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
846 break;
848 case WM_GETFONT:
849 result = (LRESULT)es->font;
850 break;
852 case WM_GETTEXT:
853 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
854 break;
856 case WM_GETTEXTLENGTH:
857 if (unicode) result = strlenW(es->text);
858 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
859 NULL, 0, NULL, NULL );
860 break;
862 case WM_HSCROLL:
863 result = EDIT_WM_HScroll(es, LOWORD(wParam), SHIWORD(wParam));
864 break;
866 case WM_KEYDOWN:
867 result = EDIT_WM_KeyDown(es, (INT)wParam);
868 break;
870 case WM_KILLFOCUS:
871 result = EDIT_WM_KillFocus(es);
872 break;
874 case WM_LBUTTONDBLCLK:
875 result = EDIT_WM_LButtonDblClk(es);
876 break;
878 case WM_LBUTTONDOWN:
879 result = EDIT_WM_LButtonDown(es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
880 break;
882 case WM_LBUTTONUP:
883 result = EDIT_WM_LButtonUp(es);
884 break;
886 case WM_MBUTTONDOWN:
887 result = EDIT_WM_MButtonDown(es);
888 break;
890 case WM_MOUSEACTIVATE:
892 * FIXME: maybe DefWindowProc() screws up, but it seems that
893 * modeless dialog boxes need this. If we don't do this, the focus
894 * will _not_ be set by DefWindowProc() for edit controls in a
895 * modeless dialog box ???
897 SetFocus(hwnd);
898 result = MA_ACTIVATE;
899 break;
901 case WM_MOUSEMOVE:
902 result = EDIT_WM_MouseMove(es, SLOWORD(lParam), SHIWORD(lParam));
903 break;
905 case WM_PAINT:
906 EDIT_WM_Paint(es, wParam);
907 break;
909 case WM_PASTE:
910 EDIT_WM_Paste(es);
911 break;
913 case WM_SETFOCUS:
914 EDIT_WM_SetFocus(es);
915 break;
917 case WM_SETFONT:
918 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
919 break;
921 case WM_SETREDRAW:
922 /* FIXME: actually set an internal flag and behave accordingly */
923 break;
925 case WM_SETTEXT:
926 EDIT_WM_SetText(es, lParam, unicode);
927 result = TRUE;
928 break;
930 case WM_SIZE:
931 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
932 break;
934 case WM_STYLECHANGED:
935 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
936 break;
938 case WM_STYLECHANGING:
939 result = 0; /* See EDIT_WM_StyleChanged */
940 break;
942 case WM_SYSKEYDOWN:
943 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
944 break;
946 case WM_TIMER:
947 EDIT_WM_Timer(es);
948 break;
950 case WM_VSCROLL:
951 result = EDIT_WM_VScroll(es, LOWORD(wParam), SHIWORD(wParam));
952 break;
954 case WM_MOUSEWHEEL:
956 int gcWheelDelta = 0;
957 UINT pulScrollLines = 3;
958 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
960 if (wParam & (MK_SHIFT | MK_CONTROL)) {
961 result = DefWindowProcW(hwnd, msg, wParam, lParam);
962 break;
964 gcWheelDelta -= SHIWORD(wParam);
965 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
967 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
968 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
969 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
972 break;
973 default:
974 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
975 break;
978 if (es) EDIT_UnlockBuffer(es, FALSE);
980 return result;
983 /*********************************************************************
985 * EditWndProcW (USER32.@)
987 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
989 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
992 /*********************************************************************
994 * EditWndProc (USER32.@)
996 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
998 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1001 /*********************************************************************
1003 * EDIT_BuildLineDefs_ML
1005 * Build linked list of text lines.
1006 * Lines can end with '\0' (last line), a character (if it is wrapped),
1007 * a soft return '\r\r\n' or a hard return '\r\n'
1010 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1012 HDC dc;
1013 HFONT old_font = 0;
1014 LPWSTR current_position, cp;
1015 INT fw;
1016 LINEDEF *current_line;
1017 LINEDEF *previous_line;
1018 LINEDEF *start_line;
1019 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1020 INT line_count = es->line_count;
1021 INT orig_net_length;
1022 RECT rc;
1024 if (istart == iend && delta == 0)
1025 return;
1027 dc = GetDC(es->hwndSelf);
1028 if (es->font)
1029 old_font = SelectObject(dc, es->font);
1031 previous_line = NULL;
1032 current_line = es->first_line_def;
1034 /* Find starting line. istart must lie inside an existing line or
1035 * at the end of buffer */
1036 do {
1037 if (istart < current_line->index + current_line->length ||
1038 current_line->ending == END_0)
1039 break;
1041 previous_line = current_line;
1042 current_line = current_line->next;
1043 line_index++;
1044 } while (current_line);
1046 if (!current_line) /* Error occurred start is not inside previous buffer */
1048 FIXME(" modification occurred outside buffer\n");
1049 ReleaseDC(es->hwndSelf, dc);
1050 return;
1053 /* Remember start of modifications in order to calculate update region */
1054 nstart_line = line_index;
1055 nstart_index = current_line->index;
1057 /* We must start to reformat from the previous line since the modifications
1058 * may have caused the line to wrap upwards. */
1059 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1061 line_index--;
1062 current_line = previous_line;
1064 start_line = current_line;
1066 fw = es->format_rect.right - es->format_rect.left;
1067 current_position = es->text + current_line->index;
1068 do {
1069 if (current_line != start_line)
1071 if (!current_line || current_line->index + delta > current_position - es->text)
1073 /* The buffer has been expanded, create a new line and
1074 insert it into the link list */
1075 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1076 new_line->next = previous_line->next;
1077 previous_line->next = new_line;
1078 current_line = new_line;
1079 es->line_count++;
1081 else if (current_line->index + delta < current_position - es->text)
1083 /* The previous line merged with this line so we delete this extra entry */
1084 previous_line->next = current_line->next;
1085 HeapFree(GetProcessHeap(), 0, current_line);
1086 current_line = previous_line->next;
1087 es->line_count--;
1088 continue;
1090 else /* current_line->index + delta == current_position */
1092 if (current_position - es->text > iend)
1093 break; /* We reached end of line modifications */
1094 /* else recalulate this line */
1098 current_line->index = current_position - es->text;
1099 orig_net_length = current_line->net_length;
1101 /* Find end of line */
1102 cp = current_position;
1103 while (*cp) {
1104 if (*cp == '\n') break;
1105 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1106 break;
1107 cp++;
1110 /* Mark type of line termination */
1111 if (!(*cp)) {
1112 current_line->ending = END_0;
1113 current_line->net_length = strlenW(current_position);
1114 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1115 current_line->ending = END_SOFT;
1116 current_line->net_length = cp - current_position - 1;
1117 } else if (*cp == '\n') {
1118 current_line->ending = END_RICH;
1119 current_line->net_length = cp - current_position;
1120 } else {
1121 current_line->ending = END_HARD;
1122 current_line->net_length = cp - current_position;
1125 /* Calculate line width */
1126 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1127 current_position, current_line->net_length,
1128 es->tabs_count, es->tabs));
1130 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1131 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1132 INT next = 0;
1133 INT prev;
1134 do {
1135 prev = next;
1136 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1137 prev + 1, current_line->net_length, WB_RIGHT);
1138 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1139 current_position, next, es->tabs_count, es->tabs));
1140 } while (current_line->width <= fw);
1141 if (!prev) { /* Didn't find a line break so force a break */
1142 next = 0;
1143 do {
1144 prev = next;
1145 next++;
1146 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1147 current_position, next, es->tabs_count, es->tabs));
1148 } while (current_line->width <= fw);
1149 if (!prev)
1150 prev = 1;
1153 /* If the first line we are calculating, wrapped before istart, we must
1154 * adjust istart in order for this to be reflected in the update region. */
1155 if (current_line->index == nstart_index && istart > current_line->index + prev)
1156 istart = current_line->index + prev;
1157 /* else if we are updating the previous line before the first line we
1158 * are re-calculating and it expanded */
1159 else if (current_line == start_line &&
1160 current_line->index != nstart_index && orig_net_length < prev)
1162 /* Line expanded due to an upwards line wrap so we must partially include
1163 * previous line in update region */
1164 nstart_line = line_index;
1165 nstart_index = current_line->index;
1166 istart = current_line->index + orig_net_length;
1169 current_line->net_length = prev;
1170 current_line->ending = END_WRAP;
1171 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1172 current_line->net_length, es->tabs_count, es->tabs));
1176 /* Adjust length to include line termination */
1177 switch (current_line->ending) {
1178 case END_SOFT:
1179 current_line->length = current_line->net_length + 3;
1180 break;
1181 case END_RICH:
1182 current_line->length = current_line->net_length + 1;
1183 break;
1184 case END_HARD:
1185 current_line->length = current_line->net_length + 2;
1186 break;
1187 case END_WRAP:
1188 case END_0:
1189 current_line->length = current_line->net_length;
1190 break;
1192 es->text_width = max(es->text_width, current_line->width);
1193 current_position += current_line->length;
1194 previous_line = current_line;
1195 current_line = current_line->next;
1196 line_index++;
1197 } while (previous_line->ending != END_0);
1199 /* Finish adjusting line indexes by delta or remove hanging lines */
1200 if (previous_line->ending == END_0)
1202 LINEDEF *pnext = NULL;
1204 previous_line->next = NULL;
1205 while (current_line)
1207 pnext = current_line->next;
1208 HeapFree(GetProcessHeap(), 0, current_line);
1209 current_line = pnext;
1210 es->line_count--;
1213 else
1215 while (current_line)
1217 current_line->index += delta;
1218 current_line = current_line->next;
1222 /* Calculate rest of modification rectangle */
1223 if (hrgn)
1225 HRGN tmphrgn;
1227 * We calculate two rectangles. One for the first line which may have
1228 * an indent with respect to the format rect. The other is a format-width
1229 * rectangle that spans the rest of the lines that changed or moved.
1231 rc.top = es->format_rect.top + nstart_line * es->line_height -
1232 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1233 rc.bottom = rc.top + es->line_height;
1234 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1235 es->text + nstart_index, istart - nstart_index,
1236 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1237 rc.right = es->format_rect.right;
1238 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1240 rc.top = rc.bottom;
1241 rc.left = es->format_rect.left;
1242 rc.right = es->format_rect.right;
1244 * If lines were added or removed we must re-paint the remainder of the
1245 * lines since the remaining lines were either shifted up or down.
1247 if (line_count < es->line_count) /* We added lines */
1248 rc.bottom = es->line_count * es->line_height;
1249 else if (line_count > es->line_count) /* We removed lines */
1250 rc.bottom = line_count * es->line_height;
1251 else
1252 rc.bottom = line_index * es->line_height;
1253 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1254 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1255 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1256 DeleteObject(tmphrgn);
1259 if (es->font)
1260 SelectObject(dc, old_font);
1262 ReleaseDC(es->hwndSelf, dc);
1265 /*********************************************************************
1267 * EDIT_CalcLineWidth_SL
1270 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1272 es->text_width = SLOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1275 /*********************************************************************
1277 * EDIT_CallWordBreakProc
1279 * Call appropriate WordBreakProc (internal or external).
1281 * Note: The "start" argument should always be an index referring
1282 * to es->text. The actual wordbreak proc might be
1283 * 16 bit, so we can't always pass any 32 bit LPSTR.
1284 * Hence we assume that es->text is the buffer that holds
1285 * the string under examination (we can decide this for ourselves).
1288 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1290 INT ret, iWndsLocks;
1292 /* To avoid any deadlocks, all the locks on the window structures
1293 must be suspended before the control is passed to the application */
1294 iWndsLocks = WIN_SuspendWndsLock();
1296 if (es->word_break_proc16) {
1297 HGLOBAL16 hglob16;
1298 SEGPTR segptr;
1299 INT countA;
1300 WORD args[5];
1301 DWORD result;
1303 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1304 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1305 segptr = K32WOWGlobalLock16(hglob16);
1306 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1307 args[4] = SELECTOROF(segptr);
1308 args[3] = OFFSETOF(segptr);
1309 args[2] = index;
1310 args[1] = countA;
1311 args[0] = action;
1312 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1313 ret = LOWORD(result);
1314 GlobalUnlock16(hglob16);
1315 GlobalFree16(hglob16);
1317 else if (es->word_break_proc)
1319 if(es->is_unicode)
1321 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1323 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1324 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1325 ret = wbpW(es->text + start, index, count, action);
1327 else
1329 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1330 INT countA;
1331 CHAR *textA;
1333 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1334 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1335 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1336 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1337 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1338 ret = wbpA(textA, index, countA, action);
1339 HeapFree(GetProcessHeap(), 0, textA);
1342 else
1343 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1345 WIN_RestoreWndsLock(iWndsLocks);
1346 return ret;
1350 /*********************************************************************
1352 * EDIT_CharFromPos
1354 * Beware: This is not the function called on EM_CHARFROMPOS
1355 * The position _can_ be outside the formatting / client
1356 * rectangle
1357 * The return value is only the character index
1360 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1362 INT index;
1363 HDC dc;
1364 HFONT old_font = 0;
1366 if (es->style & ES_MULTILINE) {
1367 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1368 INT line_index = 0;
1369 LINEDEF *line_def = es->first_line_def;
1370 INT low, high;
1371 while ((line > 0) && line_def->next) {
1372 line_index += line_def->length;
1373 line_def = line_def->next;
1374 line--;
1376 x += es->x_offset - es->format_rect.left;
1377 if (x >= line_def->width) {
1378 if (after_wrap)
1379 *after_wrap = (line_def->ending == END_WRAP);
1380 return line_index + line_def->net_length;
1382 if (x <= 0) {
1383 if (after_wrap)
1384 *after_wrap = FALSE;
1385 return line_index;
1387 dc = GetDC(es->hwndSelf);
1388 if (es->font)
1389 old_font = SelectObject(dc, es->font);
1390 low = line_index + 1;
1391 high = line_index + line_def->net_length + 1;
1392 while (low < high - 1)
1394 INT mid = (low + high) / 2;
1395 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1396 else low = mid;
1398 index = low;
1400 if (after_wrap)
1401 *after_wrap = ((index == line_index + line_def->net_length) &&
1402 (line_def->ending == END_WRAP));
1403 } else {
1404 LPWSTR text;
1405 SIZE size;
1406 if (after_wrap)
1407 *after_wrap = FALSE;
1408 x -= es->format_rect.left;
1409 if (!x)
1410 return es->x_offset;
1411 text = EDIT_GetPasswordPointer_SL(es);
1412 dc = GetDC(es->hwndSelf);
1413 if (es->font)
1414 old_font = SelectObject(dc, es->font);
1415 if (x < 0)
1417 INT low = 0;
1418 INT high = es->x_offset;
1419 while (low < high - 1)
1421 INT mid = (low + high) / 2;
1422 GetTextExtentPoint32W( dc, text + mid,
1423 es->x_offset - mid, &size );
1424 if (size.cx > -x) low = mid;
1425 else high = mid;
1427 index = low;
1429 else
1431 INT low = es->x_offset;
1432 INT high = strlenW(es->text) + 1;
1433 while (low < high - 1)
1435 INT mid = (low + high) / 2;
1436 GetTextExtentPoint32W( dc, text + es->x_offset,
1437 mid - es->x_offset, &size );
1438 if (size.cx > x) high = mid;
1439 else low = mid;
1441 index = low;
1443 if (es->style & ES_PASSWORD)
1444 HeapFree(GetProcessHeap(), 0, text);
1446 if (es->font)
1447 SelectObject(dc, old_font);
1448 ReleaseDC(es->hwndSelf, dc);
1449 return index;
1453 /*********************************************************************
1455 * EDIT_ConfinePoint
1457 * adjusts the point to be within the formatting rectangle
1458 * (so CharFromPos returns the nearest _visible_ character)
1461 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1463 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1464 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1468 /*********************************************************************
1470 * EDIT_GetLineRect
1472 * Calculates the bounding rectangle for a line from a starting
1473 * column to an ending column.
1476 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1478 INT line_index = EDIT_EM_LineIndex(es, line);
1480 if (es->style & ES_MULTILINE)
1481 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1482 else
1483 rc->top = es->format_rect.top;
1484 rc->bottom = rc->top + es->line_height;
1485 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1486 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1490 /*********************************************************************
1492 * EDIT_GetPasswordPointer_SL
1494 * note: caller should free the (optionally) allocated buffer
1497 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1499 if (es->style & ES_PASSWORD) {
1500 INT len = strlenW(es->text);
1501 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1502 text[len] = '\0';
1503 while(len) text[--len] = es->password_char;
1504 return text;
1505 } else
1506 return es->text;
1510 /*********************************************************************
1512 * EDIT_LockBuffer
1514 * This acts as a LOCAL_Lock(), but it locks only once. This way
1515 * you can call it whenever you like, without unlocking.
1517 * Initially the edit control allocates a HLOCAL32 buffer
1518 * (32 bit linear memory handler). However, 16 bit application
1519 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1520 * handler). From that moment on we have to keep using this 16 bit memory
1521 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1522 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1523 * conversion.
1526 static void EDIT_LockBuffer(EDITSTATE *es)
1528 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
1529 if (!es->text) {
1530 CHAR *textA = NULL;
1531 UINT countA = 0;
1532 BOOL _16bit = FALSE;
1534 if(es->hloc32W)
1536 if(es->hloc32A)
1538 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1539 textA = LocalLock(es->hloc32A);
1540 countA = strlen(textA) + 1;
1542 else if(es->hloc16)
1544 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1545 textA = LOCAL_Lock(hInstance, es->hloc16);
1546 countA = strlen(textA) + 1;
1547 _16bit = TRUE;
1550 else {
1551 ERR("no buffer ... please report\n");
1552 return;
1555 if(textA)
1557 HLOCAL hloc32W_new;
1558 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1559 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1560 if(countW_new > es->buffer_size + 1)
1562 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1563 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1564 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1565 if(hloc32W_new)
1567 es->hloc32W = hloc32W_new;
1568 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1569 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1571 else
1572 WARN("FAILED! Will synchronize partially\n");
1576 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1577 es->text = LocalLock(es->hloc32W);
1579 if(textA)
1581 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1582 if(_16bit)
1583 LOCAL_Unlock(hInstance, es->hloc16);
1584 else
1585 LocalUnlock(es->hloc32A);
1588 es->lock_count++;
1592 /*********************************************************************
1594 * EDIT_SL_InvalidateText
1596 * Called from EDIT_InvalidateText().
1597 * Does the job for single-line controls only.
1600 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1602 RECT line_rect;
1603 RECT rc;
1605 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1606 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1607 EDIT_UpdateText(es, &rc, TRUE);
1611 /*********************************************************************
1613 * EDIT_ML_InvalidateText
1615 * Called from EDIT_InvalidateText().
1616 * Does the job for multi-line controls only.
1619 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1621 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1622 INT sl = EDIT_EM_LineFromChar(es, start);
1623 INT el = EDIT_EM_LineFromChar(es, end);
1624 INT sc;
1625 INT ec;
1626 RECT rc1;
1627 RECT rcWnd;
1628 RECT rcLine;
1629 RECT rcUpdate;
1630 INT l;
1632 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1633 return;
1635 sc = start - EDIT_EM_LineIndex(es, sl);
1636 ec = end - EDIT_EM_LineIndex(es, el);
1637 if (sl < es->y_offset) {
1638 sl = es->y_offset;
1639 sc = 0;
1641 if (el > es->y_offset + vlc) {
1642 el = es->y_offset + vlc;
1643 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1645 GetClientRect(es->hwndSelf, &rc1);
1646 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1647 if (sl == el) {
1648 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1649 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1650 EDIT_UpdateText(es, &rcUpdate, TRUE);
1651 } else {
1652 EDIT_GetLineRect(es, sl, sc,
1653 EDIT_EM_LineLength(es,
1654 EDIT_EM_LineIndex(es, sl)),
1655 &rcLine);
1656 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1657 EDIT_UpdateText(es, &rcUpdate, TRUE);
1658 for (l = sl + 1 ; l < el ; l++) {
1659 EDIT_GetLineRect(es, l, 0,
1660 EDIT_EM_LineLength(es,
1661 EDIT_EM_LineIndex(es, l)),
1662 &rcLine);
1663 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1664 EDIT_UpdateText(es, &rcUpdate, TRUE);
1666 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1667 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1668 EDIT_UpdateText(es, &rcUpdate, TRUE);
1673 /*********************************************************************
1675 * EDIT_InvalidateText
1677 * Invalidate the text from offset start upto, but not including,
1678 * offset end. Useful for (re)painting the selection.
1679 * Regions outside the linewidth are not invalidated.
1680 * end == -1 means end == TextLength.
1681 * start and end need not be ordered.
1684 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1686 if (end == start)
1687 return;
1689 if (end == -1)
1690 end = strlenW(es->text);
1692 if (end < start) {
1693 INT tmp = start;
1694 start = end;
1695 end = tmp;
1698 if (es->style & ES_MULTILINE)
1699 EDIT_ML_InvalidateText(es, start, end);
1700 else
1701 EDIT_SL_InvalidateText(es, start, end);
1705 /*********************************************************************
1707 * EDIT_MakeFit
1709 * Try to fit size + 1 characters in the buffer.
1710 * Constrain to limits if honor_limit is TRUE.
1712 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size, BOOL honor_limit)
1714 HLOCAL hNew32W;
1716 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
1717 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
1718 return FALSE;
1721 if (size <= es->buffer_size)
1722 return TRUE;
1724 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1726 /* Force edit to unlock it's buffer. es->text now NULL */
1727 EDIT_UnlockBuffer(es, TRUE);
1729 if (es->hloc32W) {
1730 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1731 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1732 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1733 es->hloc32W = hNew32W;
1734 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1738 EDIT_LockBuffer(es);
1740 if (es->buffer_size < size) {
1741 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1742 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1743 return FALSE;
1744 } else {
1745 TRACE("We now have %d+1\n", es->buffer_size);
1746 return TRUE;
1751 /*********************************************************************
1753 * EDIT_MakeUndoFit
1755 * Try to fit size + 1 bytes in the undo buffer.
1758 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1760 UINT alloc_size;
1762 if (size <= es->undo_buffer_size)
1763 return TRUE;
1765 TRACE("trying to ReAlloc to %d+1\n", size);
1767 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1768 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1769 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1770 return TRUE;
1772 else
1774 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1775 return FALSE;
1780 /*********************************************************************
1782 * EDIT_MoveBackward
1785 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1787 INT e = es->selection_end;
1789 if (e) {
1790 e--;
1791 if ((es->style & ES_MULTILINE) && e &&
1792 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1793 e--;
1794 if (e && (es->text[e - 1] == '\r'))
1795 e--;
1798 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1799 EDIT_EM_ScrollCaret(es);
1803 /*********************************************************************
1805 * EDIT_MoveDown_ML
1807 * Only for multi line controls
1808 * Move the caret one line down, on a column with the nearest
1809 * x coordinate on the screen (might be a different column).
1812 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1814 INT s = es->selection_start;
1815 INT e = es->selection_end;
1816 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1817 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1818 INT x = SLOWORD(pos);
1819 INT y = SHIWORD(pos);
1821 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1822 if (!extend)
1823 s = e;
1824 EDIT_EM_SetSel(es, s, e, after_wrap);
1825 EDIT_EM_ScrollCaret(es);
1829 /*********************************************************************
1831 * EDIT_MoveEnd
1834 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1836 BOOL after_wrap = FALSE;
1837 INT e;
1839 /* Pass a high value in x to make sure of receiving the end of the line */
1840 if (es->style & ES_MULTILINE)
1841 e = EDIT_CharFromPos(es, 0x3fffffff,
1842 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1843 else
1844 e = strlenW(es->text);
1845 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1846 EDIT_EM_ScrollCaret(es);
1850 /*********************************************************************
1852 * EDIT_MoveForward
1855 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1857 INT e = es->selection_end;
1859 if (es->text[e]) {
1860 e++;
1861 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1862 if (es->text[e] == '\n')
1863 e++;
1864 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1865 e += 2;
1868 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1869 EDIT_EM_ScrollCaret(es);
1873 /*********************************************************************
1875 * EDIT_MoveHome
1877 * Home key: move to beginning of line.
1880 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1882 INT e;
1884 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1885 if (es->style & ES_MULTILINE)
1886 e = EDIT_CharFromPos(es, -es->x_offset,
1887 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1888 else
1889 e = 0;
1890 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1891 EDIT_EM_ScrollCaret(es);
1895 /*********************************************************************
1897 * EDIT_MovePageDown_ML
1899 * Only for multi line controls
1900 * Move the caret one page down, on a column with the nearest
1901 * x coordinate on the screen (might be a different column).
1904 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1906 INT s = es->selection_start;
1907 INT e = es->selection_end;
1908 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1909 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1910 INT x = SLOWORD(pos);
1911 INT y = SHIWORD(pos);
1913 e = EDIT_CharFromPos(es, x,
1914 y + (es->format_rect.bottom - es->format_rect.top),
1915 &after_wrap);
1916 if (!extend)
1917 s = e;
1918 EDIT_EM_SetSel(es, s, e, after_wrap);
1919 EDIT_EM_ScrollCaret(es);
1923 /*********************************************************************
1925 * EDIT_MovePageUp_ML
1927 * Only for multi line controls
1928 * Move the caret one page up, on a column with the nearest
1929 * x coordinate on the screen (might be a different column).
1932 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1934 INT s = es->selection_start;
1935 INT e = es->selection_end;
1936 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1937 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1938 INT x = SLOWORD(pos);
1939 INT y = SHIWORD(pos);
1941 e = EDIT_CharFromPos(es, x,
1942 y - (es->format_rect.bottom - es->format_rect.top),
1943 &after_wrap);
1944 if (!extend)
1945 s = e;
1946 EDIT_EM_SetSel(es, s, e, after_wrap);
1947 EDIT_EM_ScrollCaret(es);
1951 /*********************************************************************
1953 * EDIT_MoveUp_ML
1955 * Only for multi line controls
1956 * Move the caret one line up, on a column with the nearest
1957 * x coordinate on the screen (might be a different column).
1960 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1962 INT s = es->selection_start;
1963 INT e = es->selection_end;
1964 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1965 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1966 INT x = SLOWORD(pos);
1967 INT y = SHIWORD(pos);
1969 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1970 if (!extend)
1971 s = e;
1972 EDIT_EM_SetSel(es, s, e, after_wrap);
1973 EDIT_EM_ScrollCaret(es);
1977 /*********************************************************************
1979 * EDIT_MoveWordBackward
1982 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1984 INT s = es->selection_start;
1985 INT e = es->selection_end;
1986 INT l;
1987 INT ll;
1988 INT li;
1990 l = EDIT_EM_LineFromChar(es, e);
1991 ll = EDIT_EM_LineLength(es, e);
1992 li = EDIT_EM_LineIndex(es, l);
1993 if (e - li == 0) {
1994 if (l) {
1995 li = EDIT_EM_LineIndex(es, l - 1);
1996 e = li + EDIT_EM_LineLength(es, li);
1998 } else {
1999 e = li + (INT)EDIT_CallWordBreakProc(es,
2000 li, e - li, ll, WB_LEFT);
2002 if (!extend)
2003 s = e;
2004 EDIT_EM_SetSel(es, s, e, FALSE);
2005 EDIT_EM_ScrollCaret(es);
2009 /*********************************************************************
2011 * EDIT_MoveWordForward
2014 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2016 INT s = es->selection_start;
2017 INT e = es->selection_end;
2018 INT l;
2019 INT ll;
2020 INT li;
2022 l = EDIT_EM_LineFromChar(es, e);
2023 ll = EDIT_EM_LineLength(es, e);
2024 li = EDIT_EM_LineIndex(es, l);
2025 if (e - li == ll) {
2026 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2027 e = EDIT_EM_LineIndex(es, l + 1);
2028 } else {
2029 e = li + EDIT_CallWordBreakProc(es,
2030 li, e - li + 1, ll, WB_RIGHT);
2032 if (!extend)
2033 s = e;
2034 EDIT_EM_SetSel(es, s, e, FALSE);
2035 EDIT_EM_ScrollCaret(es);
2039 /*********************************************************************
2041 * EDIT_PaintLine
2044 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2046 INT s = es->selection_start;
2047 INT e = es->selection_end;
2048 INT li;
2049 INT ll;
2050 INT x;
2051 INT y;
2052 LRESULT pos;
2054 if (es->style & ES_MULTILINE) {
2055 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2056 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2057 return;
2058 } else if (line)
2059 return;
2061 TRACE("line=%d\n", line);
2063 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2064 x = SLOWORD(pos);
2065 y = SHIWORD(pos);
2066 li = EDIT_EM_LineIndex(es, line);
2067 ll = EDIT_EM_LineLength(es, li);
2068 s = min(es->selection_start, es->selection_end);
2069 e = max(es->selection_start, es->selection_end);
2070 s = min(li + ll, max(li, s));
2071 e = min(li + ll, max(li, e));
2072 if (rev && (s != e) &&
2073 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2074 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2075 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2076 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2077 } else
2078 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2082 /*********************************************************************
2084 * EDIT_PaintText
2087 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2089 COLORREF BkColor;
2090 COLORREF TextColor;
2091 INT ret;
2092 INT li;
2093 INT BkMode;
2094 SIZE size;
2096 if (!count)
2097 return 0;
2098 BkMode = GetBkMode(dc);
2099 BkColor = GetBkColor(dc);
2100 TextColor = GetTextColor(dc);
2101 if (rev) {
2102 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2103 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2104 SetBkMode( dc, OPAQUE);
2106 li = EDIT_EM_LineIndex(es, line);
2107 if (es->style & ES_MULTILINE) {
2108 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2109 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2110 } else {
2111 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2112 TextOutW(dc, x, y, text + li + col, count);
2113 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2114 ret = size.cx;
2115 if (es->style & ES_PASSWORD)
2116 HeapFree(GetProcessHeap(), 0, text);
2118 if (rev) {
2119 SetBkColor(dc, BkColor);
2120 SetTextColor(dc, TextColor);
2121 SetBkMode( dc, BkMode);
2123 return ret;
2127 /*********************************************************************
2129 * EDIT_SetCaretPos
2132 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2133 BOOL after_wrap)
2135 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2136 SetCaretPos(SLOWORD(res), SHIWORD(res));
2140 /*********************************************************************
2142 * EDIT_SetRectNP
2144 * note: this is not (exactly) the handler called on EM_SETRECTNP
2145 * it is also used to set the rect of a single line control
2148 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2150 CopyRect(&es->format_rect, rc);
2151 if (es->style & WS_BORDER) {
2152 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2153 if(TWEAK_WineLook == WIN31_LOOK)
2154 bw += 2;
2155 es->format_rect.left += bw;
2156 es->format_rect.top += bw;
2157 es->format_rect.right -= bw;
2158 es->format_rect.bottom -= bw;
2160 es->format_rect.left += es->left_margin;
2161 es->format_rect.right -= es->right_margin;
2162 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2163 if (es->style & ES_MULTILINE)
2165 INT fw, vlc, max_x_offset, max_y_offset;
2167 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2168 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2170 /* correct es->x_offset */
2171 fw = es->format_rect.right - es->format_rect.left;
2172 max_x_offset = es->text_width - fw;
2173 if(max_x_offset < 0) max_x_offset = 0;
2174 if(es->x_offset > max_x_offset)
2175 es->x_offset = max_x_offset;
2177 /* correct es->y_offset */
2178 max_y_offset = es->line_count - vlc;
2179 if(max_y_offset < 0) max_y_offset = 0;
2180 if(es->y_offset > max_y_offset)
2181 es->y_offset = max_y_offset;
2183 /* force scroll info update */
2184 EDIT_UpdateScrollInfo(es);
2186 else
2187 /* Windows doesn't care to fix text placement for SL controls */
2188 es->format_rect.bottom = es->format_rect.top + es->line_height;
2190 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2191 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2195 /*********************************************************************
2197 * EDIT_UnlockBuffer
2200 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2202 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2204 /* Edit window might be already destroyed */
2205 if(!IsWindow(es->hwndSelf))
2207 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2208 return;
2211 if (!es->lock_count) {
2212 ERR("lock_count == 0 ... please report\n");
2213 return;
2215 if (!es->text) {
2216 ERR("es->text == 0 ... please report\n");
2217 return;
2220 if (force || (es->lock_count == 1)) {
2221 if (es->hloc32W) {
2222 CHAR *textA = NULL;
2223 BOOL _16bit = FALSE;
2224 UINT countA = 0;
2225 UINT countW = strlenW(es->text) + 1;
2227 if(es->hloc32A)
2229 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2230 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2231 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2232 countA = LocalSize(es->hloc32A);
2233 if(countA_new > countA)
2235 HLOCAL hloc32A_new;
2236 UINT alloc_size = ROUND_TO_GROW(countA_new);
2237 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2238 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2239 if(hloc32A_new)
2241 es->hloc32A = hloc32A_new;
2242 countA = LocalSize(hloc32A_new);
2243 TRACE("Real new size %d bytes\n", countA);
2245 else
2246 WARN("FAILED! Will synchronize partially\n");
2248 textA = LocalLock(es->hloc32A);
2250 else if(es->hloc16)
2252 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2253 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2254 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2255 countA = LOCAL_Size(hInstance, es->hloc16);
2256 if(countA_new > countA)
2258 HLOCAL16 hloc16_new;
2259 UINT alloc_size = ROUND_TO_GROW(countA_new);
2260 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2261 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2262 if(hloc16_new)
2264 es->hloc16 = hloc16_new;
2265 countA = LOCAL_Size(hInstance, hloc16_new);
2266 TRACE("Real new size %d bytes\n", countA);
2268 else
2269 WARN("FAILED! Will synchronize partially\n");
2271 textA = LOCAL_Lock(hInstance, es->hloc16);
2272 _16bit = TRUE;
2275 if(textA)
2277 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2278 if(_16bit)
2279 LOCAL_Unlock(hInstance, es->hloc16);
2280 else
2281 LocalUnlock(es->hloc32A);
2284 LocalUnlock(es->hloc32W);
2285 es->text = NULL;
2287 else {
2288 ERR("no buffer ... please report\n");
2289 return;
2292 es->lock_count--;
2296 /*********************************************************************
2298 * EDIT_UpdateScrollInfo
2301 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2303 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2305 SCROLLINFO si;
2306 si.cbSize = sizeof(SCROLLINFO);
2307 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2308 si.nMin = 0;
2309 si.nMax = es->line_count - 1;
2310 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2311 si.nPos = es->y_offset;
2312 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2313 si.nMin, si.nMax, si.nPage, si.nPos);
2314 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2317 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2319 SCROLLINFO si;
2320 si.cbSize = sizeof(SCROLLINFO);
2321 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2322 si.nMin = 0;
2323 si.nMax = es->text_width - 1;
2324 si.nPage = es->format_rect.right - es->format_rect.left;
2325 si.nPos = es->x_offset;
2326 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2327 si.nMin, si.nMax, si.nPage, si.nPos);
2328 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2332 /*********************************************************************
2334 * EDIT_WordBreakProc
2336 * Find the beginning of words.
2337 * Note: unlike the specs for a WordBreakProc, this function only
2338 * allows to be called without linebreaks between s[0] upto
2339 * s[count - 1]. Remember it is only called
2340 * internally, so we can decide this for ourselves.
2343 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2345 INT ret = 0;
2347 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2349 if(!s) return 0;
2351 switch (action) {
2352 case WB_LEFT:
2353 if (!count)
2354 break;
2355 if (index)
2356 index--;
2357 if (s[index] == ' ') {
2358 while (index && (s[index] == ' '))
2359 index--;
2360 if (index) {
2361 while (index && (s[index] != ' '))
2362 index--;
2363 if (s[index] == ' ')
2364 index++;
2366 } else {
2367 while (index && (s[index] != ' '))
2368 index--;
2369 if (s[index] == ' ')
2370 index++;
2372 ret = index;
2373 break;
2374 case WB_RIGHT:
2375 if (!count)
2376 break;
2377 if (index)
2378 index--;
2379 if (s[index] == ' ')
2380 while ((index < count) && (s[index] == ' ')) index++;
2381 else {
2382 while (s[index] && (s[index] != ' ') && (index < count))
2383 index++;
2384 while ((s[index] == ' ') && (index < count)) index++;
2386 ret = index;
2387 break;
2388 case WB_ISDELIMITER:
2389 ret = (s[index] == ' ');
2390 break;
2391 default:
2392 ERR("unknown action code, please report !\n");
2393 break;
2395 return ret;
2399 /*********************************************************************
2401 * EM_CHARFROMPOS
2403 * returns line number (not index) in high-order word of result.
2404 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2405 * to Richedit, not to the edit control. Original documentation is valid.
2406 * FIXME: do the specs mean to return -1 if outside client area or
2407 * if outside formatting rectangle ???
2410 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2412 POINT pt;
2413 RECT rc;
2414 INT index;
2416 pt.x = x;
2417 pt.y = y;
2418 GetClientRect(es->hwndSelf, &rc);
2419 if (!PtInRect(&rc, pt))
2420 return -1;
2422 index = EDIT_CharFromPos(es, x, y, NULL);
2423 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2427 /*********************************************************************
2429 * EM_FMTLINES
2431 * Enable or disable soft breaks.
2433 * This means: insert or remove the soft linebreak character (\r\r\n).
2434 * Take care to check if the text still fits the buffer after insertion.
2435 * If not, notify with EN_ERRSPACE.
2438 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2440 es->flags &= ~EF_USE_SOFTBRK;
2441 if (add_eol) {
2442 es->flags |= EF_USE_SOFTBRK;
2443 FIXME("soft break enabled, not implemented\n");
2445 return add_eol;
2449 /*********************************************************************
2451 * EM_GETHANDLE
2453 * Hopefully this won't fire back at us.
2454 * We always start with a fixed buffer in the local heap.
2455 * Despite of the documentation says that the local heap is used
2456 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2457 * buffer on the local heap.
2460 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2462 HLOCAL hLocal;
2464 if (!(es->style & ES_MULTILINE))
2465 return 0;
2467 if(es->is_unicode)
2468 hLocal = es->hloc32W;
2469 else
2471 if(!es->hloc32A)
2473 CHAR *textA;
2474 UINT countA, alloc_size;
2475 TRACE("Allocating 32-bit ANSI alias buffer\n");
2476 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2477 alloc_size = ROUND_TO_GROW(countA);
2478 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2480 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2481 return 0;
2483 textA = LocalLock(es->hloc32A);
2484 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2485 LocalUnlock(es->hloc32A);
2487 hLocal = es->hloc32A;
2490 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2491 return hLocal;
2495 /*********************************************************************
2497 * EM_GETHANDLE16
2499 * Hopefully this won't fire back at us.
2500 * We always start with a buffer in 32 bit linear memory.
2501 * However, with this message a 16 bit application requests
2502 * a handle of 16 bit local heap memory, where it expects to find
2503 * the text.
2504 * It's a pitty that from this moment on we have to use this
2505 * local heap, because applications may rely on the handle
2506 * in the future.
2508 * In this function we'll try to switch to local heap.
2510 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2512 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2513 CHAR *textA;
2514 UINT countA, alloc_size;
2516 if (!(es->style & ES_MULTILINE))
2517 return 0;
2519 if (es->hloc16)
2520 return es->hloc16;
2522 if (!LOCAL_HeapSize(hInstance)) {
2523 if (!LocalInit16(hInstance, 0,
2524 GlobalSize16(hInstance))) {
2525 ERR("could not initialize local heap\n");
2526 return 0;
2528 TRACE("local heap initialized\n");
2531 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2532 alloc_size = ROUND_TO_GROW(countA);
2534 TRACE("Allocating 16-bit ANSI alias buffer\n");
2535 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2536 ERR("could not allocate new 16 bit buffer\n");
2537 return 0;
2540 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2541 ERR("could not lock new 16 bit buffer\n");
2542 LOCAL_Free(hInstance, es->hloc16);
2543 es->hloc16 = 0;
2544 return 0;
2547 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2548 LOCAL_Unlock(hInstance, es->hloc16);
2550 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2551 return es->hloc16;
2555 /*********************************************************************
2557 * EM_GETLINE
2560 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2562 LPWSTR src;
2563 INT line_len, dst_len;
2564 INT i;
2566 if (es->style & ES_MULTILINE) {
2567 if (line >= es->line_count)
2568 return 0;
2569 } else
2570 line = 0;
2571 i = EDIT_EM_LineIndex(es, line);
2572 src = es->text + i;
2573 line_len = EDIT_EM_LineLength(es, i);
2574 dst_len = *(WORD *)lParam;
2575 if(unicode)
2577 LPWSTR dst = (LPWSTR)lParam;
2578 if(dst_len <= line_len)
2580 memcpy(dst, src, dst_len * sizeof(WCHAR));
2581 return dst_len;
2583 else /* Append 0 if enough space */
2585 memcpy(dst, src, line_len * sizeof(WCHAR));
2586 dst[line_len] = 0;
2587 return line_len;
2590 else
2592 LPSTR dst = (LPSTR)lParam;
2593 INT ret;
2594 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2595 if(!ret) /* Insufficient buffer size */
2596 return dst_len;
2597 if(ret < dst_len) /* Append 0 if enough space */
2598 dst[ret] = 0;
2599 return ret;
2604 /*********************************************************************
2606 * EM_GETSEL
2609 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2611 UINT s = es->selection_start;
2612 UINT e = es->selection_end;
2614 ORDER_UINT(s, e);
2615 if (start)
2616 *start = s;
2617 if (end)
2618 *end = e;
2619 return MAKELONG(s, e);
2623 /*********************************************************************
2625 * EM_GETTHUMB
2627 * FIXME: is this right ? (or should it be only VSCROLL)
2628 * (and maybe only for edit controls that really have their
2629 * own scrollbars) (and maybe only for multiline controls ?)
2630 * All in all: very poorly documented
2633 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2635 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2636 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2640 /*********************************************************************
2642 * EM_LINEFROMCHAR
2645 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2647 INT line;
2648 LINEDEF *line_def;
2650 if (!(es->style & ES_MULTILINE))
2651 return 0;
2652 if (index > (INT)strlenW(es->text))
2653 return es->line_count - 1;
2654 if (index == -1)
2655 index = min(es->selection_start, es->selection_end);
2657 line = 0;
2658 line_def = es->first_line_def;
2659 index -= line_def->length;
2660 while ((index >= 0) && line_def->next) {
2661 line++;
2662 line_def = line_def->next;
2663 index -= line_def->length;
2665 return line;
2669 /*********************************************************************
2671 * EM_LINEINDEX
2674 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2676 INT line_index;
2677 LINEDEF *line_def;
2679 if (!(es->style & ES_MULTILINE))
2680 return 0;
2681 if (line >= es->line_count)
2682 return -1;
2684 line_index = 0;
2685 line_def = es->first_line_def;
2686 if (line == -1) {
2687 INT index = es->selection_end - line_def->length;
2688 while ((index >= 0) && line_def->next) {
2689 line_index += line_def->length;
2690 line_def = line_def->next;
2691 index -= line_def->length;
2693 } else {
2694 while (line > 0) {
2695 line_index += line_def->length;
2696 line_def = line_def->next;
2697 line--;
2700 return line_index;
2704 /*********************************************************************
2706 * EM_LINELENGTH
2709 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2711 LINEDEF *line_def;
2713 if (!(es->style & ES_MULTILINE))
2714 return strlenW(es->text);
2716 if (index == -1) {
2717 /* get the number of remaining non-selected chars of selected lines */
2718 INT32 l; /* line number */
2719 INT32 li; /* index of first char in line */
2720 INT32 count;
2721 l = EDIT_EM_LineFromChar(es, es->selection_start);
2722 /* # chars before start of selection area */
2723 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2724 l = EDIT_EM_LineFromChar(es, es->selection_end);
2725 /* # chars after end of selection */
2726 li = EDIT_EM_LineIndex(es, l);
2727 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2728 return count;
2730 line_def = es->first_line_def;
2731 index -= line_def->length;
2732 while ((index >= 0) && line_def->next) {
2733 line_def = line_def->next;
2734 index -= line_def->length;
2736 return line_def->net_length;
2740 /*********************************************************************
2742 * EM_LINESCROLL
2744 * NOTE: dx is in average character widths, dy - in lines;
2747 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2749 if (!(es->style & ES_MULTILINE))
2750 return FALSE;
2752 dx *= es->char_width;
2753 return EDIT_EM_LineScroll_internal(es, dx, dy);
2756 /*********************************************************************
2758 * EDIT_EM_LineScroll_internal
2760 * Version of EDIT_EM_LineScroll for internal use.
2761 * It doesn't refuse if ES_MULTILINE is set and assumes that
2762 * dx is in pixels, dy - in lines.
2765 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2767 INT nyoff;
2768 INT x_offset_in_pixels;
2770 if (es->style & ES_MULTILINE)
2772 x_offset_in_pixels = es->x_offset;
2774 else
2776 dy = 0;
2777 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2780 if (-dx > x_offset_in_pixels)
2781 dx = -x_offset_in_pixels;
2782 if (dx > es->text_width - x_offset_in_pixels)
2783 dx = es->text_width - x_offset_in_pixels;
2784 nyoff = max(0, es->y_offset + dy);
2785 if (nyoff >= es->line_count)
2786 nyoff = es->line_count - 1;
2787 dy = (es->y_offset - nyoff) * es->line_height;
2788 if (dx || dy) {
2789 RECT rc1;
2790 RECT rc;
2792 es->y_offset = nyoff;
2793 if(es->style & ES_MULTILINE)
2794 es->x_offset += dx;
2795 else
2796 es->x_offset += dx / es->char_width;
2798 GetClientRect(es->hwndSelf, &rc1);
2799 IntersectRect(&rc, &rc1, &es->format_rect);
2800 ScrollWindowEx(es->hwndSelf, -dx, dy,
2801 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2802 /* force scroll info update */
2803 EDIT_UpdateScrollInfo(es);
2805 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2806 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2807 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2808 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2809 return TRUE;
2813 /*********************************************************************
2815 * EM_POSFROMCHAR
2818 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2820 INT len = strlenW(es->text);
2821 INT l;
2822 INT li;
2823 INT x;
2824 INT y = 0;
2825 HDC dc;
2826 HFONT old_font = 0;
2827 SIZE size;
2829 index = min(index, len);
2830 dc = GetDC(es->hwndSelf);
2831 if (es->font)
2832 old_font = SelectObject(dc, es->font);
2833 if (es->style & ES_MULTILINE) {
2834 l = EDIT_EM_LineFromChar(es, index);
2835 y = (l - es->y_offset) * es->line_height;
2836 li = EDIT_EM_LineIndex(es, l);
2837 if (after_wrap && (li == index) && l) {
2838 INT l2 = l - 1;
2839 LINEDEF *line_def = es->first_line_def;
2840 while (l2) {
2841 line_def = line_def->next;
2842 l2--;
2844 if (line_def->ending == END_WRAP) {
2845 l--;
2846 y -= es->line_height;
2847 li = EDIT_EM_LineIndex(es, l);
2850 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2851 es->tabs_count, es->tabs)) - es->x_offset;
2852 } else {
2853 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2854 if (index < es->x_offset) {
2855 GetTextExtentPoint32W(dc, text + index,
2856 es->x_offset - index, &size);
2857 x = -size.cx;
2858 } else {
2859 GetTextExtentPoint32W(dc, text + es->x_offset,
2860 index - es->x_offset, &size);
2861 x = size.cx;
2863 y = 0;
2864 if (es->style & ES_PASSWORD)
2865 HeapFree(GetProcessHeap(), 0, text);
2867 x += es->format_rect.left;
2868 y += es->format_rect.top;
2869 if (es->font)
2870 SelectObject(dc, old_font);
2871 ReleaseDC(es->hwndSelf, dc);
2872 return MAKELONG((INT16)x, (INT16)y);
2876 /*********************************************************************
2878 * EM_REPLACESEL
2880 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2883 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2885 UINT strl = strlenW(lpsz_replace);
2886 UINT tl = strlenW(es->text);
2887 UINT utl;
2888 UINT s;
2889 UINT e;
2890 UINT i;
2891 LPWSTR p;
2892 HRGN hrgn = 0;
2894 TRACE("%s, can_undo %d, send_update %d\n",
2895 debugstr_w(lpsz_replace), can_undo, send_update);
2897 s = es->selection_start;
2898 e = es->selection_end;
2900 if ((s == e) && !strl)
2901 return;
2903 ORDER_UINT(s, e);
2905 if (!EDIT_MakeFit(es, tl - (e - s) + strl, honor_limit))
2906 return;
2908 if (e != s) {
2909 /* there is something to be deleted */
2910 TRACE("deleting stuff.\n");
2911 if (can_undo) {
2912 utl = strlenW(es->undo_text);
2913 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2914 /* undo-buffer is extended to the right */
2915 EDIT_MakeUndoFit(es, utl + e - s);
2916 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2917 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2918 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2919 /* undo-buffer is extended to the left */
2920 EDIT_MakeUndoFit(es, utl + e - s);
2921 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2922 p[e - s] = p[0];
2923 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2924 p[i] = (es->text + s)[i];
2925 es->undo_position = s;
2926 } else {
2927 /* new undo-buffer */
2928 EDIT_MakeUndoFit(es, e - s);
2929 strncpyW(es->undo_text, es->text + s, e - s + 1);
2930 es->undo_text[e - s] = 0; /* ensure 0 termination */
2931 es->undo_position = s;
2933 /* any deletion makes the old insertion-undo invalid */
2934 es->undo_insert_count = 0;
2935 } else
2936 EDIT_EM_EmptyUndoBuffer(es);
2938 /* now delete */
2939 strcpyW(es->text + s, es->text + e);
2941 if (strl) {
2942 /* there is an insertion */
2943 if (can_undo) {
2944 if ((s == es->undo_position) ||
2945 ((es->undo_insert_count) &&
2946 (s == es->undo_position + es->undo_insert_count)))
2948 * insertion is new and at delete position or
2949 * an extension to either left or right
2951 es->undo_insert_count += strl;
2952 else {
2953 /* new insertion undo */
2954 es->undo_position = s;
2955 es->undo_insert_count = strl;
2956 /* new insertion makes old delete-buffer invalid */
2957 *es->undo_text = '\0';
2959 } else
2960 EDIT_EM_EmptyUndoBuffer(es);
2962 /* now insert */
2963 tl = strlenW(es->text);
2964 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));
2965 for (p = es->text + tl ; p >= es->text + s ; p--)
2966 p[strl] = p[0];
2967 for (i = 0 , p = es->text + s ; i < strl ; i++)
2968 p[i] = lpsz_replace[i];
2969 if(es->style & ES_UPPERCASE)
2970 CharUpperBuffW(p, strl);
2971 else if(es->style & ES_LOWERCASE)
2972 CharLowerBuffW(p, strl);
2973 s += strl;
2975 if (es->style & ES_MULTILINE)
2977 INT s = min(es->selection_start, es->selection_end);
2979 hrgn = CreateRectRgn(0, 0, 0, 0);
2980 EDIT_BuildLineDefs_ML(es, s, s + strl,
2981 strl - abs(es->selection_end - es->selection_start), hrgn);
2983 else
2984 EDIT_CalcLineWidth_SL(es);
2986 EDIT_EM_SetSel(es, s, s, FALSE);
2987 es->flags |= EF_MODIFIED;
2988 if (send_update) es->flags |= EF_UPDATE;
2989 EDIT_EM_ScrollCaret(es);
2991 /* force scroll info update */
2992 EDIT_UpdateScrollInfo(es);
2994 if (hrgn)
2996 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2997 DeleteObject(hrgn);
2999 else
3000 EDIT_UpdateText(es, NULL, TRUE);
3002 if(es->flags & EF_UPDATE)
3004 es->flags &= ~EF_UPDATE;
3005 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3010 /*********************************************************************
3012 * EM_SCROLL
3015 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3017 INT dy;
3019 if (!(es->style & ES_MULTILINE))
3020 return (LRESULT)FALSE;
3022 dy = 0;
3024 switch (action) {
3025 case SB_LINEUP:
3026 if (es->y_offset)
3027 dy = -1;
3028 break;
3029 case SB_LINEDOWN:
3030 if (es->y_offset < es->line_count - 1)
3031 dy = 1;
3032 break;
3033 case SB_PAGEUP:
3034 if (es->y_offset)
3035 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3036 break;
3037 case SB_PAGEDOWN:
3038 if (es->y_offset < es->line_count - 1)
3039 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3040 break;
3041 default:
3042 return (LRESULT)FALSE;
3044 if (dy) {
3045 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3046 /* check if we are going to move too far */
3047 if(es->y_offset + dy > es->line_count - vlc)
3048 dy = es->line_count - vlc - es->y_offset;
3050 /* Notification is done in EDIT_EM_LineScroll */
3051 if(dy)
3052 EDIT_EM_LineScroll(es, 0, dy);
3054 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3058 /*********************************************************************
3060 * EM_SCROLLCARET
3063 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3065 if (es->style & ES_MULTILINE) {
3066 INT l;
3067 INT li;
3068 INT vlc;
3069 INT ww;
3070 INT cw = es->char_width;
3071 INT x;
3072 INT dy = 0;
3073 INT dx = 0;
3075 l = EDIT_EM_LineFromChar(es, es->selection_end);
3076 li = EDIT_EM_LineIndex(es, l);
3077 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3078 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3079 if (l >= es->y_offset + vlc)
3080 dy = l - vlc + 1 - es->y_offset;
3081 if (l < es->y_offset)
3082 dy = l - es->y_offset;
3083 ww = es->format_rect.right - es->format_rect.left;
3084 if (x < es->format_rect.left)
3085 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3086 if (x > es->format_rect.right)
3087 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3088 if (dy || dx)
3090 /* check if we are going to move too far */
3091 if(es->x_offset + dx + ww > es->text_width)
3092 dx = es->text_width - ww - es->x_offset;
3093 if(dx || dy)
3094 EDIT_EM_LineScroll_internal(es, dx, dy);
3096 } else {
3097 INT x;
3098 INT goal;
3099 INT format_width;
3101 if (!(es->style & ES_AUTOHSCROLL))
3102 return;
3104 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3105 format_width = es->format_rect.right - es->format_rect.left;
3106 if (x < es->format_rect.left) {
3107 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3108 do {
3109 es->x_offset--;
3110 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3111 } while ((x < goal) && es->x_offset);
3112 /* FIXME: use ScrollWindow() somehow to improve performance */
3113 EDIT_UpdateText(es, NULL, TRUE);
3114 } else if (x > es->format_rect.right) {
3115 INT x_last;
3116 INT len = strlenW(es->text);
3117 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3118 do {
3119 es->x_offset++;
3120 x = SLOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3121 x_last = SLOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3122 } while ((x > goal) && (x_last > es->format_rect.right));
3123 /* FIXME: use ScrollWindow() somehow to improve performance */
3124 EDIT_UpdateText(es, NULL, TRUE);
3128 if(es->flags & EF_FOCUSED)
3129 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3133 /*********************************************************************
3135 * EM_SETHANDLE
3137 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3140 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3142 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3144 if (!(es->style & ES_MULTILINE))
3145 return;
3147 if (!hloc) {
3148 WARN("called with NULL handle\n");
3149 return;
3152 EDIT_UnlockBuffer(es, TRUE);
3154 if(es->hloc16)
3156 LOCAL_Free(hInstance, es->hloc16);
3157 es->hloc16 = (HLOCAL16)NULL;
3160 if(es->is_unicode)
3162 if(es->hloc32A)
3164 LocalFree(es->hloc32A);
3165 es->hloc32A = NULL;
3167 es->hloc32W = hloc;
3169 else
3171 INT countW, countA;
3172 HLOCAL hloc32W_new;
3173 WCHAR *textW;
3174 CHAR *textA;
3176 countA = LocalSize(hloc);
3177 textA = LocalLock(hloc);
3178 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3179 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3181 ERR("Could not allocate new unicode buffer\n");
3182 return;
3184 textW = LocalLock(hloc32W_new);
3185 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3186 LocalUnlock(hloc32W_new);
3187 LocalUnlock(hloc);
3189 if(es->hloc32W)
3190 LocalFree(es->hloc32W);
3192 es->hloc32W = hloc32W_new;
3193 es->hloc32A = hloc;
3196 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3198 EDIT_LockBuffer(es);
3200 es->x_offset = es->y_offset = 0;
3201 es->selection_start = es->selection_end = 0;
3202 EDIT_EM_EmptyUndoBuffer(es);
3203 es->flags &= ~EF_MODIFIED;
3204 es->flags &= ~EF_UPDATE;
3205 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3206 EDIT_UpdateText(es, NULL, TRUE);
3207 EDIT_EM_ScrollCaret(es);
3208 /* force scroll info update */
3209 EDIT_UpdateScrollInfo(es);
3213 /*********************************************************************
3215 * EM_SETHANDLE16
3217 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3220 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3222 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3223 INT countW, countA;
3224 HLOCAL hloc32W_new;
3225 WCHAR *textW;
3226 CHAR *textA;
3228 if (!(es->style & ES_MULTILINE))
3229 return;
3231 if (!hloc) {
3232 WARN("called with NULL handle\n");
3233 return;
3236 EDIT_UnlockBuffer(es, TRUE);
3238 if(es->hloc32A)
3240 LocalFree(es->hloc32A);
3241 es->hloc32A = NULL;
3244 countA = LOCAL_Size(hInstance, hloc);
3245 textA = LOCAL_Lock(hInstance, hloc);
3246 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3247 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3249 ERR("Could not allocate new unicode buffer\n");
3250 return;
3252 textW = LocalLock(hloc32W_new);
3253 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3254 LocalUnlock(hloc32W_new);
3255 LOCAL_Unlock(hInstance, hloc);
3257 if(es->hloc32W)
3258 LocalFree(es->hloc32W);
3260 es->hloc32W = hloc32W_new;
3261 es->hloc16 = hloc;
3263 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3265 EDIT_LockBuffer(es);
3267 es->x_offset = es->y_offset = 0;
3268 es->selection_start = es->selection_end = 0;
3269 EDIT_EM_EmptyUndoBuffer(es);
3270 es->flags &= ~EF_MODIFIED;
3271 es->flags &= ~EF_UPDATE;
3272 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3273 EDIT_UpdateText(es, NULL, TRUE);
3274 EDIT_EM_ScrollCaret(es);
3275 /* force scroll info update */
3276 EDIT_UpdateScrollInfo(es);
3280 /*********************************************************************
3282 * EM_SETLIMITTEXT
3284 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3285 * However, the windows version is not complied to yet in all of edit.c
3287 * Additionally as the wrapper for RichEdit controls we need larger buffers
3288 * at present -1 will represent nolimit
3290 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3292 if (limit == 0xFFFFFFFF)
3293 es->buffer_limit = -1;
3294 else if (es->style & ES_MULTILINE) {
3295 if (limit)
3296 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3297 else
3298 es->buffer_limit = BUFLIMIT_MULTI;
3299 } else {
3300 if (limit)
3301 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3302 else
3303 es->buffer_limit = BUFLIMIT_SINGLE;
3308 /*********************************************************************
3310 * EM_SETMARGINS
3312 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3313 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3314 * margin according to the textmetrics of the current font.
3316 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3317 * of the char's width as the margin, but this is not how Windows handles this.
3318 * For all other fonts Windows sets the margins to zero.
3321 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3322 INT left, INT right)
3324 TEXTMETRICW tm;
3325 INT default_left_margin = 0; /* in pixels */
3326 INT default_right_margin = 0; /* in pixels */
3328 /* Set the default margins depending on the font */
3329 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3330 HDC dc = GetDC(es->hwndSelf);
3331 HFONT old_font = SelectObject(dc, es->font);
3332 GetTextMetricsW(dc, &tm);
3333 /* The default margins are only non zero for TrueType or Vector fonts */
3334 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3335 /* This must be calculated more exactly! But how? */
3336 default_left_margin = tm.tmAveCharWidth / 3;
3337 default_right_margin = tm.tmAveCharWidth / 3;
3339 SelectObject(dc, old_font);
3340 ReleaseDC(es->hwndSelf, dc);
3343 if (action & EC_LEFTMARGIN) {
3344 if (left != EC_USEFONTINFO)
3345 es->left_margin = left;
3346 else
3347 es->left_margin = default_left_margin;
3350 if (action & EC_RIGHTMARGIN) {
3351 if (right != EC_USEFONTINFO)
3352 es->right_margin = right;
3353 else
3354 es->right_margin = default_right_margin;
3356 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3360 /*********************************************************************
3362 * EM_SETPASSWORDCHAR
3365 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3367 LONG style;
3369 if (es->style & ES_MULTILINE)
3370 return;
3372 if (es->password_char == c)
3373 return;
3375 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3376 es->password_char = c;
3377 if (c) {
3378 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3379 es->style |= ES_PASSWORD;
3380 } else {
3381 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3382 es->style &= ~ES_PASSWORD;
3384 EDIT_UpdateText(es, NULL, TRUE);
3388 /*********************************************************************
3390 * EDIT_EM_SetSel
3392 * note: unlike the specs say: the order of start and end
3393 * _is_ preserved in Windows. (i.e. start can be > end)
3394 * In other words: this handler is OK
3397 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3399 UINT old_start = es->selection_start;
3400 UINT old_end = es->selection_end;
3401 UINT len = strlenW(es->text);
3403 if (start == (UINT)-1) {
3404 start = es->selection_end;
3405 end = es->selection_end;
3406 } else {
3407 start = min(start, len);
3408 end = min(end, len);
3410 es->selection_start = start;
3411 es->selection_end = end;
3412 if (after_wrap)
3413 es->flags |= EF_AFTER_WRAP;
3414 else
3415 es->flags &= ~EF_AFTER_WRAP;
3416 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3417 ORDER_UINT(start, end);
3418 ORDER_UINT(end, old_end);
3419 ORDER_UINT(start, old_start);
3420 ORDER_UINT(old_start, old_end);
3421 if (end != old_start)
3424 * One can also do
3425 * ORDER_UINT32(end, old_start);
3426 * EDIT_InvalidateText(es, start, end);
3427 * EDIT_InvalidateText(es, old_start, old_end);
3428 * in place of the following if statement.
3430 if (old_start > end )
3432 EDIT_InvalidateText(es, start, end);
3433 EDIT_InvalidateText(es, old_start, old_end);
3435 else
3437 EDIT_InvalidateText(es, start, old_start);
3438 EDIT_InvalidateText(es, end, old_end);
3441 else EDIT_InvalidateText(es, start, old_end);
3445 /*********************************************************************
3447 * EM_SETTABSTOPS
3450 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3452 if (!(es->style & ES_MULTILINE))
3453 return FALSE;
3454 if (es->tabs)
3455 HeapFree(GetProcessHeap(), 0, es->tabs);
3456 es->tabs_count = count;
3457 if (!count)
3458 es->tabs = NULL;
3459 else {
3460 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3461 memcpy(es->tabs, tabs, count * sizeof(INT));
3463 return TRUE;
3467 /*********************************************************************
3469 * EM_SETTABSTOPS16
3472 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3474 if (!(es->style & ES_MULTILINE))
3475 return FALSE;
3476 if (es->tabs)
3477 HeapFree(GetProcessHeap(), 0, es->tabs);
3478 es->tabs_count = count;
3479 if (!count)
3480 es->tabs = NULL;
3481 else {
3482 INT i;
3483 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3484 for (i = 0 ; i < count ; i++)
3485 es->tabs[i] = *tabs++;
3487 return TRUE;
3491 /*********************************************************************
3493 * EM_SETWORDBREAKPROC
3496 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam)
3498 if (es->word_break_proc == (void *)lParam)
3499 return;
3501 es->word_break_proc = (void *)lParam;
3502 es->word_break_proc16 = NULL;
3504 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3505 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3506 EDIT_UpdateText(es, NULL, TRUE);
3511 /*********************************************************************
3513 * EM_SETWORDBREAKPROC16
3516 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3518 if (es->word_break_proc16 == wbp)
3519 return;
3521 es->word_break_proc = NULL;
3522 es->word_break_proc16 = wbp;
3523 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3524 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3525 EDIT_UpdateText(es, NULL, TRUE);
3530 /*********************************************************************
3532 * EM_UNDO / WM_UNDO
3535 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3537 INT ulength;
3538 LPWSTR utext;
3540 /* Protect read-only edit control from modification */
3541 if(es->style & ES_READONLY)
3542 return FALSE;
3544 ulength = strlenW(es->undo_text);
3545 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3547 strcpyW(utext, es->undo_text);
3549 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3550 es->undo_insert_count, debugstr_w(utext));
3552 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3553 EDIT_EM_EmptyUndoBuffer(es);
3554 EDIT_EM_ReplaceSel(es, TRUE, utext, FALSE, TRUE);
3555 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3556 /* send the notification after the selection start and end are set */
3557 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3558 EDIT_EM_ScrollCaret(es);
3559 HeapFree(GetProcessHeap(), 0, utext);
3561 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3562 es->undo_insert_count, debugstr_w(es->undo_text));
3563 return TRUE;
3567 /*********************************************************************
3569 * WM_CHAR
3572 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3574 BOOL control;
3576 /* Protect read-only edit control from modification */
3577 if(es->style & ES_READONLY)
3578 return;
3580 control = GetKeyState(VK_CONTROL) & 0x8000;
3582 switch (c) {
3583 case '\r':
3584 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3585 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3586 break;
3587 case '\n':
3588 if (es->style & ES_MULTILINE) {
3589 if (es->style & ES_READONLY) {
3590 EDIT_MoveHome(es, FALSE);
3591 EDIT_MoveDown_ML(es, FALSE);
3592 } else {
3593 static const WCHAR cr_lfW[] = {'\r','\n',0};
3594 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3597 break;
3598 case '\t':
3599 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3601 static const WCHAR tabW[] = {'\t',0};
3602 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3604 break;
3605 case VK_BACK:
3606 if (!(es->style & ES_READONLY) && !control) {
3607 if (es->selection_start != es->selection_end)
3608 EDIT_WM_Clear(es);
3609 else {
3610 /* delete character left of caret */
3611 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3612 EDIT_MoveBackward(es, TRUE);
3613 EDIT_WM_Clear(es);
3616 break;
3617 case 0x03: /* ^C */
3618 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3619 break;
3620 case 0x16: /* ^V */
3621 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3622 break;
3623 case 0x18: /* ^X */
3624 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3625 break;
3627 default:
3628 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3629 WCHAR str[2];
3630 str[0] = c;
3631 str[1] = '\0';
3632 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3634 break;
3639 /*********************************************************************
3641 * WM_COMMAND
3644 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3646 if (code || control)
3647 return;
3649 switch (id) {
3650 case EM_UNDO:
3651 EDIT_EM_Undo(es);
3652 break;
3653 case WM_CUT:
3654 EDIT_WM_Cut(es);
3655 break;
3656 case WM_COPY:
3657 EDIT_WM_Copy(es);
3658 break;
3659 case WM_PASTE:
3660 EDIT_WM_Paste(es);
3661 break;
3662 case WM_CLEAR:
3663 EDIT_WM_Clear(es);
3664 break;
3665 case EM_SETSEL:
3666 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3667 EDIT_EM_ScrollCaret(es);
3668 break;
3669 default:
3670 ERR("unknown menu item, please report\n");
3671 break;
3676 /*********************************************************************
3678 * WM_CONTEXTMENU
3680 * Note: the resource files resource/sysres_??.rc cannot define a
3681 * single popup menu. Hence we use a (dummy) menubar
3682 * containing the single popup menu as its first item.
3684 * FIXME: the message identifiers have been chosen arbitrarily,
3685 * hence we use MF_BYPOSITION.
3686 * We might as well use the "real" values (anybody knows ?)
3687 * The menu definition is in resources/sysres_??.rc.
3688 * Once these are OK, we better use MF_BYCOMMAND here
3689 * (as we do in EDIT_WM_Command()).
3692 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3694 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3695 HMENU popup = GetSubMenu(menu, 0);
3696 UINT start = es->selection_start;
3697 UINT end = es->selection_end;
3699 ORDER_UINT(start, end);
3701 /* undo */
3702 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3703 /* cut */
3704 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3705 /* copy */
3706 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3707 /* paste */
3708 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3709 /* delete */
3710 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3711 /* select all */
3712 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3714 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3715 DestroyMenu(menu);
3719 /*********************************************************************
3721 * WM_COPY
3724 static void EDIT_WM_Copy(EDITSTATE *es)
3726 INT s = min(es->selection_start, es->selection_end);
3727 INT e = max(es->selection_start, es->selection_end);
3728 HGLOBAL hdst;
3729 LPWSTR dst;
3731 if (e == s) return;
3733 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3734 dst = GlobalLock(hdst);
3735 strncpyW(dst, es->text + s, e - s);
3736 dst[e - s] = 0; /* ensure 0 termination */
3737 TRACE("%s\n", debugstr_w(dst));
3738 GlobalUnlock(hdst);
3739 OpenClipboard(es->hwndSelf);
3740 EmptyClipboard();
3741 SetClipboardData(CF_UNICODETEXT, hdst);
3742 CloseClipboard();
3746 /*********************************************************************
3748 * WM_CREATE
3751 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3753 TRACE("%s\n", debugstr_w(name));
3755 * To initialize some final structure members, we call some helper
3756 * functions. However, since the EDITSTATE is not consistent (i.e.
3757 * not fully initialized), we should be very careful which
3758 * functions can be called, and in what order.
3760 EDIT_WM_SetFont(es, 0, FALSE);
3761 EDIT_EM_EmptyUndoBuffer(es);
3763 if (name && *name) {
3764 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3765 /* if we insert text to the editline, the text scrolls out
3766 * of the window, as the caret is placed after the insert
3767 * pos normally; thus we reset es->selection... to 0 and
3768 * update caret
3770 es->selection_start = es->selection_end = 0;
3771 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3772 * Messages are only to be sent when the USER does something to
3773 * change the contents. So I am removing this EN_CHANGE
3775 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3777 EDIT_EM_ScrollCaret(es);
3779 /* force scroll info update */
3780 EDIT_UpdateScrollInfo(es);
3781 return 0;
3785 /*********************************************************************
3787 * WM_DESTROY
3790 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3792 LINEDEF *pc, *pp;
3794 if (es->hloc32W) {
3795 while (LocalUnlock(es->hloc32W)) ;
3796 LocalFree(es->hloc32W);
3798 if (es->hloc32A) {
3799 while (LocalUnlock(es->hloc32A)) ;
3800 LocalFree(es->hloc32A);
3802 if (es->hloc16) {
3803 HINSTANCE16 hInstance = GetWindowWord( es->hwndSelf, GWL_HINSTANCE );
3804 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3805 LOCAL_Free(hInstance, es->hloc16);
3808 pc = es->first_line_def;
3809 while (pc)
3811 pp = pc->next;
3812 HeapFree(GetProcessHeap(), 0, pc);
3813 pc = pp;
3816 SetWindowLongW( es->hwndSelf, 0, 0 );
3817 HeapFree(GetProcessHeap(), 0, es);
3819 return 0;
3823 /*********************************************************************
3825 * WM_ERASEBKGND
3828 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3830 HBRUSH brush;
3831 RECT rc;
3833 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
3834 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3836 GetClientRect(es->hwndSelf, &rc);
3837 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3838 GetClipBox(dc, &rc);
3840 * FIXME: specs say that we should UnrealizeObject() the brush,
3841 * but the specs of UnrealizeObject() say that we shouldn't
3842 * unrealize a stock object. The default brush that
3843 * DefWndProc() returns is ... a stock object.
3845 FillRect(dc, &rc, brush);
3846 return -1;
3850 /*********************************************************************
3852 * WM_GETTEXT
3855 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3857 if(!count) return 0;
3859 if(unicode)
3861 LPWSTR textW = (LPWSTR)lParam;
3862 lstrcpynW(textW, es->text, count);
3863 return strlenW(textW);
3865 else
3867 LPSTR textA = (LPSTR)lParam;
3868 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3869 textA[count - 1] = 0; /* ensure 0 termination */
3870 return strlen(textA);
3874 /*********************************************************************
3876 * WM_HSCROLL
3879 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3881 INT dx;
3882 INT fw;
3884 if (!(es->style & ES_MULTILINE))
3885 return 0;
3887 if (!(es->style & ES_AUTOHSCROLL))
3888 return 0;
3890 dx = 0;
3891 fw = es->format_rect.right - es->format_rect.left;
3892 switch (action) {
3893 case SB_LINELEFT:
3894 TRACE("SB_LINELEFT\n");
3895 if (es->x_offset)
3896 dx = -es->char_width;
3897 break;
3898 case SB_LINERIGHT:
3899 TRACE("SB_LINERIGHT\n");
3900 if (es->x_offset < es->text_width)
3901 dx = es->char_width;
3902 break;
3903 case SB_PAGELEFT:
3904 TRACE("SB_PAGELEFT\n");
3905 if (es->x_offset)
3906 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3907 break;
3908 case SB_PAGERIGHT:
3909 TRACE("SB_PAGERIGHT\n");
3910 if (es->x_offset < es->text_width)
3911 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3912 break;
3913 case SB_LEFT:
3914 TRACE("SB_LEFT\n");
3915 if (es->x_offset)
3916 dx = -es->x_offset;
3917 break;
3918 case SB_RIGHT:
3919 TRACE("SB_RIGHT\n");
3920 if (es->x_offset < es->text_width)
3921 dx = es->text_width - es->x_offset;
3922 break;
3923 case SB_THUMBTRACK:
3924 TRACE("SB_THUMBTRACK %d\n", pos);
3925 es->flags |= EF_HSCROLL_TRACK;
3926 if(es->style & WS_HSCROLL)
3927 dx = pos - es->x_offset;
3928 else
3930 INT fw, new_x;
3931 /* Sanity check */
3932 if(pos < 0 || pos > 100) return 0;
3933 /* Assume default scroll range 0-100 */
3934 fw = es->format_rect.right - es->format_rect.left;
3935 new_x = pos * (es->text_width - fw) / 100;
3936 dx = es->text_width ? (new_x - es->x_offset) : 0;
3938 break;
3939 case SB_THUMBPOSITION:
3940 TRACE("SB_THUMBPOSITION %d\n", pos);
3941 es->flags &= ~EF_HSCROLL_TRACK;
3942 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3943 dx = pos - es->x_offset;
3944 else
3946 INT fw, new_x;
3947 /* Sanity check */
3948 if(pos < 0 || pos > 100) return 0;
3949 /* Assume default scroll range 0-100 */
3950 fw = es->format_rect.right - es->format_rect.left;
3951 new_x = pos * (es->text_width - fw) / 100;
3952 dx = es->text_width ? (new_x - es->x_offset) : 0;
3954 if (!dx) {
3955 /* force scroll info update */
3956 EDIT_UpdateScrollInfo(es);
3957 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
3959 break;
3960 case SB_ENDSCROLL:
3961 TRACE("SB_ENDSCROLL\n");
3962 break;
3964 * FIXME : the next two are undocumented !
3965 * Are we doing the right thing ?
3966 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3967 * although it's also a regular control message.
3969 case EM_GETTHUMB: /* this one is used by NT notepad */
3970 case EM_GETTHUMB16:
3972 LRESULT ret;
3973 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3974 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3975 else
3977 /* Assume default scroll range 0-100 */
3978 INT fw = es->format_rect.right - es->format_rect.left;
3979 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
3981 TRACE("EM_GETTHUMB: returning %ld\n", ret);
3982 return ret;
3984 case EM_LINESCROLL16:
3985 TRACE("EM_LINESCROLL16\n");
3986 dx = pos;
3987 break;
3989 default:
3990 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
3991 action, action);
3992 return 0;
3994 if (dx)
3996 INT fw = es->format_rect.right - es->format_rect.left;
3997 /* check if we are going to move too far */
3998 if(es->x_offset + dx + fw > es->text_width)
3999 dx = es->text_width - fw - es->x_offset;
4000 if(dx)
4001 EDIT_EM_LineScroll_internal(es, dx, 0);
4003 return 0;
4007 /*********************************************************************
4009 * EDIT_CheckCombo
4012 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4014 HWND hLBox = es->hwndListBox;
4015 HWND hCombo;
4016 BOOL bDropped;
4017 int nEUI;
4019 if (!hLBox)
4020 return FALSE;
4022 hCombo = GetParent(es->hwndSelf);
4023 bDropped = TRUE;
4024 nEUI = 0;
4026 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4028 if (key == VK_UP || key == VK_DOWN)
4030 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4031 nEUI = 1;
4033 if (msg == WM_KEYDOWN || nEUI)
4034 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4037 switch (msg)
4039 case WM_KEYDOWN:
4040 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4042 /* make sure ComboLBox pops up */
4043 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4044 key = VK_F4;
4045 nEUI = 2;
4048 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4049 break;
4051 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4052 if (nEUI)
4053 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4054 else
4055 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4056 break;
4059 if(nEUI == 2)
4060 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4062 return TRUE;
4066 /*********************************************************************
4068 * WM_KEYDOWN
4070 * Handling of special keys that don't produce a WM_CHAR
4071 * (i.e. non-printable keys) & Backspace & Delete
4074 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4076 BOOL shift;
4077 BOOL control;
4079 if (GetKeyState(VK_MENU) & 0x8000)
4080 return 0;
4082 shift = GetKeyState(VK_SHIFT) & 0x8000;
4083 control = GetKeyState(VK_CONTROL) & 0x8000;
4085 switch (key) {
4086 case VK_F4:
4087 case VK_UP:
4088 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4089 break;
4091 /* fall through */
4092 case VK_LEFT:
4093 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4094 EDIT_MoveUp_ML(es, shift);
4095 else
4096 if (control)
4097 EDIT_MoveWordBackward(es, shift);
4098 else
4099 EDIT_MoveBackward(es, shift);
4100 break;
4101 case VK_DOWN:
4102 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4103 break;
4104 /* fall through */
4105 case VK_RIGHT:
4106 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4107 EDIT_MoveDown_ML(es, shift);
4108 else if (control)
4109 EDIT_MoveWordForward(es, shift);
4110 else
4111 EDIT_MoveForward(es, shift);
4112 break;
4113 case VK_HOME:
4114 EDIT_MoveHome(es, shift);
4115 break;
4116 case VK_END:
4117 EDIT_MoveEnd(es, shift);
4118 break;
4119 case VK_PRIOR:
4120 if (es->style & ES_MULTILINE)
4121 EDIT_MovePageUp_ML(es, shift);
4122 else
4123 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4124 break;
4125 case VK_NEXT:
4126 if (es->style & ES_MULTILINE)
4127 EDIT_MovePageDown_ML(es, shift);
4128 else
4129 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4130 break;
4131 case VK_DELETE:
4132 if (!(es->style & ES_READONLY) && !(shift && control)) {
4133 if (es->selection_start != es->selection_end) {
4134 if (shift)
4135 EDIT_WM_Cut(es);
4136 else
4137 EDIT_WM_Clear(es);
4138 } else {
4139 if (shift) {
4140 /* delete character left of caret */
4141 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4142 EDIT_MoveBackward(es, TRUE);
4143 EDIT_WM_Clear(es);
4144 } else if (control) {
4145 /* delete to end of line */
4146 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4147 EDIT_MoveEnd(es, TRUE);
4148 EDIT_WM_Clear(es);
4149 } else {
4150 /* delete character right of caret */
4151 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4152 EDIT_MoveForward(es, TRUE);
4153 EDIT_WM_Clear(es);
4157 break;
4158 case VK_INSERT:
4159 if (shift) {
4160 if (!(es->style & ES_READONLY))
4161 EDIT_WM_Paste(es);
4162 } else if (control)
4163 EDIT_WM_Copy(es);
4164 break;
4165 case VK_RETURN:
4166 /* If the edit doesn't want the return send a message to the default object */
4167 if(!(es->style & ES_WANTRETURN))
4169 HWND hwndParent = GetParent(es->hwndSelf);
4170 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4171 if (HIWORD(dw) == DC_HASDEFID)
4173 SendMessageW( hwndParent, WM_COMMAND,
4174 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4175 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4178 break;
4180 return 0;
4184 /*********************************************************************
4186 * WM_KILLFOCUS
4189 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4191 es->flags &= ~EF_FOCUSED;
4192 DestroyCaret();
4193 if(!(es->style & ES_NOHIDESEL))
4194 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4195 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4196 return 0;
4200 /*********************************************************************
4202 * WM_LBUTTONDBLCLK
4204 * The caret position has been set on the WM_LBUTTONDOWN message
4207 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4209 INT s;
4210 INT e = es->selection_end;
4211 INT l;
4212 INT li;
4213 INT ll;
4215 if (!(es->flags & EF_FOCUSED))
4216 return 0;
4218 l = EDIT_EM_LineFromChar(es, e);
4219 li = EDIT_EM_LineIndex(es, l);
4220 ll = EDIT_EM_LineLength(es, e);
4221 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4222 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4223 EDIT_EM_SetSel(es, s, e, FALSE);
4224 EDIT_EM_ScrollCaret(es);
4225 return 0;
4229 /*********************************************************************
4231 * WM_LBUTTONDOWN
4234 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4236 INT e;
4237 BOOL after_wrap;
4239 if (!(es->flags & EF_FOCUSED))
4240 return 0;
4242 es->bCaptureState = TRUE;
4243 SetCapture(es->hwndSelf);
4244 EDIT_ConfinePoint(es, &x, &y);
4245 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4246 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4247 EDIT_EM_ScrollCaret(es);
4248 es->region_posx = es->region_posy = 0;
4249 SetTimer(es->hwndSelf, 0, 100, NULL);
4250 return 0;
4254 /*********************************************************************
4256 * WM_LBUTTONUP
4259 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4261 if (es->bCaptureState) {
4262 KillTimer(es->hwndSelf, 0);
4263 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4265 es->bCaptureState = FALSE;
4266 return 0;
4270 /*********************************************************************
4272 * WM_MBUTTONDOWN
4275 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4277 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4278 return 0;
4282 /*********************************************************************
4284 * WM_MOUSEMOVE
4287 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4289 INT e;
4290 BOOL after_wrap;
4291 INT prex, prey;
4293 if (GetCapture() != es->hwndSelf)
4294 return 0;
4297 * FIXME: gotta do some scrolling if outside client
4298 * area. Maybe reset the timer ?
4300 prex = x; prey = y;
4301 EDIT_ConfinePoint(es, &x, &y);
4302 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4303 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4304 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4305 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4306 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4307 return 0;
4311 /*********************************************************************
4313 * WM_NCCREATE
4315 * See also EDIT_WM_StyleChanged
4317 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4319 EDITSTATE *es;
4320 UINT alloc_size;
4322 TRACE("Creating %s edit control, style = %08lx\n",
4323 unicode ? "Unicode" : "ANSI", lpcs->style);
4325 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4326 return FALSE;
4327 SetWindowLongW( hwnd, 0, (LONG)es );
4330 * Note: since the EDITSTATE has not been fully initialized yet,
4331 * we can't use any API calls that may send
4332 * WM_XXX messages before WM_NCCREATE is completed.
4335 es->is_unicode = unicode;
4336 es->style = lpcs->style;
4338 es->bEnableState = !(es->style & WS_DISABLED);
4340 es->hwndSelf = hwnd;
4341 /* Save parent, which will be notified by EN_* messages */
4342 es->hwndParent = lpcs->hwndParent;
4344 if (es->style & ES_COMBO)
4345 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4347 /* Number overrides lowercase overrides uppercase (at least it
4348 * does in Win95). However I'll bet that ES_NUMBER would be
4349 * invalid under Win 3.1.
4351 if (es->style & ES_NUMBER) {
4352 ; /* do not override the ES_NUMBER */
4353 } else if (es->style & ES_LOWERCASE) {
4354 es->style &= ~ES_UPPERCASE;
4356 if (es->style & ES_MULTILINE) {
4357 es->buffer_limit = BUFLIMIT_MULTI;
4358 if (es->style & WS_VSCROLL)
4359 es->style |= ES_AUTOVSCROLL;
4360 if (es->style & WS_HSCROLL)
4361 es->style |= ES_AUTOHSCROLL;
4362 es->style &= ~ES_PASSWORD;
4363 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4364 /* Confirmed - RIGHT overrides CENTER */
4365 if (es->style & ES_RIGHT)
4366 es->style &= ~ES_CENTER;
4367 es->style &= ~WS_HSCROLL;
4368 es->style &= ~ES_AUTOHSCROLL;
4371 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4372 es->style |= ES_AUTOVSCROLL;
4373 } else {
4374 es->buffer_limit = BUFLIMIT_SINGLE;
4375 if (WIN31_LOOK == TWEAK_WineLook ||
4376 WIN95_LOOK == TWEAK_WineLook) {
4377 es->style &= ~ES_CENTER;
4378 es->style &= ~ES_RIGHT;
4379 } else {
4380 if (es->style & ES_RIGHT)
4381 es->style &= ~ES_CENTER;
4383 es->style &= ~WS_HSCROLL;
4384 es->style &= ~WS_VSCROLL;
4385 es->style &= ~ES_AUTOVSCROLL;
4386 es->style &= ~ES_WANTRETURN;
4387 if (es->style & ES_PASSWORD)
4388 es->password_char = '*';
4390 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4391 es->style |= ES_AUTOHSCROLL;
4394 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4395 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4396 return FALSE;
4397 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4399 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4400 return FALSE;
4401 es->undo_buffer_size = es->buffer_size;
4403 if (es->style & ES_MULTILINE)
4404 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4405 return FALSE;
4406 es->line_count = 1;
4409 * In Win95 look and feel, the WS_BORDER style is replaced by the
4410 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4411 * control a non client area. Not always. This coordinates in some
4412 * way with the window creation code in dialog.c When making
4413 * modifications please ensure that the code still works for edit
4414 * controls created directly with style 0x50800000, exStyle 0 (
4415 * which should have a single pixel border)
4417 if (TWEAK_WineLook != WIN31_LOOK)
4419 es->style &= ~WS_BORDER;
4421 else
4423 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4424 SetWindowLongW( hwnd, GWL_STYLE,
4425 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_BORDER );
4428 return TRUE;
4431 /*********************************************************************
4433 * WM_PAINT
4436 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam)
4438 PAINTSTRUCT ps;
4439 INT i;
4440 HDC dc;
4441 HFONT old_font = 0;
4442 RECT rc;
4443 RECT rcLine;
4444 RECT rcRgn;
4445 BOOL rev = es->bEnableState &&
4446 ((es->flags & EF_FOCUSED) ||
4447 (es->style & ES_NOHIDESEL));
4448 if (!wParam)
4449 dc = BeginPaint(es->hwndSelf, &ps);
4450 else
4451 dc = (HDC) wParam;
4452 if(es->style & WS_BORDER) {
4453 GetClientRect(es->hwndSelf, &rc);
4454 if(es->style & ES_MULTILINE) {
4455 if(es->style & WS_HSCROLL) rc.bottom++;
4456 if(es->style & WS_VSCROLL) rc.right++;
4458 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4460 IntersectClipRect(dc, es->format_rect.left,
4461 es->format_rect.top,
4462 es->format_rect.right,
4463 es->format_rect.bottom);
4464 if (es->style & ES_MULTILINE) {
4465 GetClientRect(es->hwndSelf, &rc);
4466 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4468 if (es->font)
4469 old_font = SelectObject(dc, es->font);
4470 EDIT_NotifyCtlColor(es, dc);
4472 if (!es->bEnableState)
4473 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4474 GetClipBox(dc, &rcRgn);
4475 if (es->style & ES_MULTILINE) {
4476 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4477 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4478 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4479 if (IntersectRect(&rc, &rcRgn, &rcLine))
4480 EDIT_PaintLine(es, dc, i, rev);
4482 } else {
4483 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4484 if (IntersectRect(&rc, &rcRgn, &rcLine))
4485 EDIT_PaintLine(es, dc, 0, rev);
4487 if (es->font)
4488 SelectObject(dc, old_font);
4490 if (!wParam)
4491 EndPaint(es->hwndSelf, &ps);
4495 /*********************************************************************
4497 * WM_PASTE
4500 static void EDIT_WM_Paste(EDITSTATE *es)
4502 HGLOBAL hsrc;
4503 LPWSTR src;
4505 /* Protect read-only edit control from modification */
4506 if(es->style & ES_READONLY)
4507 return;
4509 OpenClipboard(es->hwndSelf);
4510 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4511 src = (LPWSTR)GlobalLock(hsrc);
4512 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4513 GlobalUnlock(hsrc);
4515 CloseClipboard();
4519 /*********************************************************************
4521 * WM_SETFOCUS
4524 static void EDIT_WM_SetFocus(EDITSTATE *es)
4526 es->flags |= EF_FOCUSED;
4527 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4528 EDIT_SetCaretPos(es, es->selection_end,
4529 es->flags & EF_AFTER_WRAP);
4530 if(!(es->style & ES_NOHIDESEL))
4531 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4532 ShowCaret(es->hwndSelf);
4533 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4537 /*********************************************************************
4539 * WM_SETFONT
4541 * With Win95 look the margins are set to default font value unless
4542 * the system font (font == 0) is being set, in which case they are left
4543 * unchanged.
4546 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4548 TEXTMETRICW tm;
4549 HDC dc;
4550 HFONT old_font = 0;
4551 RECT r;
4553 es->font = font;
4554 dc = GetDC(es->hwndSelf);
4555 if (font)
4556 old_font = SelectObject(dc, font);
4557 GetTextMetricsW(dc, &tm);
4558 es->line_height = tm.tmHeight;
4559 es->char_width = tm.tmAveCharWidth;
4560 if (font)
4561 SelectObject(dc, old_font);
4562 ReleaseDC(es->hwndSelf, dc);
4563 if (font && (TWEAK_WineLook > WIN31_LOOK))
4564 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4565 EC_USEFONTINFO, EC_USEFONTINFO);
4567 /* Force the recalculation of the format rect for each font change */
4568 GetClientRect(es->hwndSelf, &r);
4569 EDIT_SetRectNP(es, &r);
4571 if (es->style & ES_MULTILINE)
4572 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4573 else
4574 EDIT_CalcLineWidth_SL(es);
4576 if (redraw)
4577 EDIT_UpdateText(es, NULL, TRUE);
4578 if (es->flags & EF_FOCUSED) {
4579 DestroyCaret();
4580 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4581 EDIT_SetCaretPos(es, es->selection_end,
4582 es->flags & EF_AFTER_WRAP);
4583 ShowCaret(es->hwndSelf);
4588 /*********************************************************************
4590 * WM_SETTEXT
4592 * NOTES
4593 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4594 * The modified flag is reset. No notifications are sent.
4596 * For single-line controls, reception of WM_SETTEXT triggers:
4597 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4600 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode)
4602 LPWSTR text = NULL;
4604 if(unicode)
4605 text = (LPWSTR)lParam;
4606 else if (lParam)
4608 LPCSTR textA = (LPCSTR)lParam;
4609 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4610 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4611 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4614 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4615 if (text) {
4616 TRACE("%s\n", debugstr_w(text));
4617 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4618 if(!unicode)
4619 HeapFree(GetProcessHeap(), 0, text);
4620 } else {
4621 static const WCHAR empty_stringW[] = {0};
4622 TRACE("<NULL>\n");
4623 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4625 es->x_offset = 0;
4626 es->flags &= ~EF_MODIFIED;
4627 EDIT_EM_SetSel(es, 0, 0, FALSE);
4628 /* Send the notification after the selection start and end have been set
4629 * edit control doesn't send notification on WM_SETTEXT
4630 * if it is multiline, or it is part of combobox
4632 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4634 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4635 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4637 EDIT_EM_ScrollCaret(es);
4641 /*********************************************************************
4643 * WM_SIZE
4646 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4648 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4649 RECT rc;
4650 TRACE("width = %d, height = %d\n", width, height);
4651 SetRect(&rc, 0, 0, width, height);
4652 EDIT_SetRectNP(es, &rc);
4653 EDIT_UpdateText(es, NULL, TRUE);
4658 /*********************************************************************
4660 * WM_STYLECHANGED
4662 * This message is sent by SetWindowLong on having changed either the Style
4663 * or the extended style.
4665 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4667 * See also EDIT_WM_NCCreate
4669 * It appears that the Windows version of the edit control allows the style
4670 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4671 * style variable which will generally be different. In this function we
4672 * update the internal style based on what changed in the externally visible
4673 * style.
4675 * Much of this content as based upon the MSDN, especially:
4676 * Platform SDK Documentation -> User Interface Services ->
4677 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4678 * Edit Control Styles
4680 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4682 if (GWL_STYLE == which) {
4683 DWORD style_change_mask;
4684 DWORD new_style;
4685 /* Only a subset of changes can be applied after the control
4686 * has been created.
4688 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4689 ES_NUMBER;
4690 if (es->style & ES_MULTILINE)
4691 style_change_mask |= ES_WANTRETURN;
4693 new_style = style->styleNew & style_change_mask;
4695 /* Number overrides lowercase overrides uppercase (at least it
4696 * does in Win95). However I'll bet that ES_NUMBER would be
4697 * invalid under Win 3.1.
4699 if (new_style & ES_NUMBER) {
4700 ; /* do not override the ES_NUMBER */
4701 } else if (new_style & ES_LOWERCASE) {
4702 new_style &= ~ES_UPPERCASE;
4705 es->style = (es->style & ~style_change_mask) | new_style;
4706 } else if (GWL_EXSTYLE == which) {
4707 ; /* FIXME - what is needed here */
4708 } else {
4709 WARN ("Invalid style change %d\n",which);
4712 return 0;
4715 /*********************************************************************
4717 * WM_SYSKEYDOWN
4720 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4722 if ((key == VK_BACK) && (key_data & 0x2000)) {
4723 if (EDIT_EM_CanUndo(es))
4724 EDIT_EM_Undo(es);
4725 return 0;
4726 } else if (key == VK_UP || key == VK_DOWN) {
4727 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4728 return 0;
4730 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4734 /*********************************************************************
4736 * WM_TIMER
4739 static void EDIT_WM_Timer(EDITSTATE *es)
4741 if (es->region_posx < 0) {
4742 EDIT_MoveBackward(es, TRUE);
4743 } else if (es->region_posx > 0) {
4744 EDIT_MoveForward(es, TRUE);
4747 * FIXME: gotta do some vertical scrolling here, like
4748 * EDIT_EM_LineScroll(hwnd, 0, 1);
4752 /*********************************************************************
4754 * WM_VSCROLL
4757 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4759 INT dy;
4761 if (!(es->style & ES_MULTILINE))
4762 return 0;
4764 if (!(es->style & ES_AUTOVSCROLL))
4765 return 0;
4767 dy = 0;
4768 switch (action) {
4769 case SB_LINEUP:
4770 case SB_LINEDOWN:
4771 case SB_PAGEUP:
4772 case SB_PAGEDOWN:
4773 TRACE("action %d\n", action);
4774 EDIT_EM_Scroll(es, action);
4775 return 0;
4776 case SB_TOP:
4777 TRACE("SB_TOP\n");
4778 dy = -es->y_offset;
4779 break;
4780 case SB_BOTTOM:
4781 TRACE("SB_BOTTOM\n");
4782 dy = es->line_count - 1 - es->y_offset;
4783 break;
4784 case SB_THUMBTRACK:
4785 TRACE("SB_THUMBTRACK %d\n", pos);
4786 es->flags |= EF_VSCROLL_TRACK;
4787 if(es->style & WS_VSCROLL)
4788 dy = pos - es->y_offset;
4789 else
4791 /* Assume default scroll range 0-100 */
4792 INT vlc, new_y;
4793 /* Sanity check */
4794 if(pos < 0 || pos > 100) return 0;
4795 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4796 new_y = pos * (es->line_count - vlc) / 100;
4797 dy = es->line_count ? (new_y - es->y_offset) : 0;
4798 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4799 es->line_count, es->y_offset, pos, dy);
4801 break;
4802 case SB_THUMBPOSITION:
4803 TRACE("SB_THUMBPOSITION %d\n", pos);
4804 es->flags &= ~EF_VSCROLL_TRACK;
4805 if(es->style & WS_VSCROLL)
4806 dy = pos - es->y_offset;
4807 else
4809 /* Assume default scroll range 0-100 */
4810 INT vlc, new_y;
4811 /* Sanity check */
4812 if(pos < 0 || pos > 100) return 0;
4813 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4814 new_y = pos * (es->line_count - vlc) / 100;
4815 dy = es->line_count ? (new_y - es->y_offset) : 0;
4816 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4817 es->line_count, es->y_offset, pos, dy);
4819 if (!dy)
4821 /* force scroll info update */
4822 EDIT_UpdateScrollInfo(es);
4823 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4825 break;
4826 case SB_ENDSCROLL:
4827 TRACE("SB_ENDSCROLL\n");
4828 break;
4830 * FIXME : the next two are undocumented !
4831 * Are we doing the right thing ?
4832 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4833 * although it's also a regular control message.
4835 case EM_GETTHUMB: /* this one is used by NT notepad */
4836 case EM_GETTHUMB16:
4838 LRESULT ret;
4839 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4840 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4841 else
4843 /* Assume default scroll range 0-100 */
4844 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4845 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4847 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4848 return ret;
4850 case EM_LINESCROLL16:
4851 TRACE("EM_LINESCROLL16 %d\n", pos);
4852 dy = pos;
4853 break;
4855 default:
4856 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4857 action, action);
4858 return 0;
4860 if (dy)
4861 EDIT_EM_LineScroll(es, 0, dy);
4862 return 0;
4865 /*********************************************************************
4867 * EDIT_UpdateText
4870 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4872 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4873 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4877 /*********************************************************************
4879 * EDIT_UpdateText
4882 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4884 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4885 InvalidateRect(es->hwndSelf, rc, bErase);