Only return valid file system names from RenderFILENAMEA/W() by using
[wine/wine64.git] / controls / edit.c
blob2eac5a4e00497d63eb344370de1c495f690f1e5e
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 <stdarg.h>
43 #include <string.h>
44 #include <stdlib.h>
46 #include "windef.h"
47 #include "winbase.h"
48 #include "winnt.h"
49 #include "wownt32.h"
50 #include "win.h"
51 #include "wine/winbase16.h"
52 #include "wine/winuser16.h"
53 #include "wine/unicode.h"
54 #include "controls.h"
55 #include "local.h"
56 #include "user.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(edit);
60 WINE_DECLARE_DEBUG_CHANNEL(combo);
61 WINE_DECLARE_DEBUG_CHANNEL(relay);
63 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
64 FIXME: BTW, new specs say 65535 (do you dare ???) */
65 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
66 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
67 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
68 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
71 * extra flags for EDITSTATE.flags field
73 #define EF_MODIFIED 0x0001 /* text has been modified */
74 #define EF_FOCUSED 0x0002 /* we have input focus */
75 #define EF_UPDATE 0x0004 /* notify parent of changed state */
76 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
77 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
79 wrapped line, instead of in front of the next character */
80 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
82 typedef enum
84 END_0 = 0, /* line ends with terminating '\0' character */
85 END_WRAP, /* line is wrapped */
86 END_HARD, /* line ends with a hard return '\r\n' */
87 END_SOFT, /* line ends with a soft return '\r\r\n' */
88 END_RICH /* line ends with a single '\n' */
89 } LINE_END;
91 typedef struct tagLINEDEF {
92 INT length; /* bruto length of a line in bytes */
93 INT net_length; /* netto length of a line in visible characters */
94 LINE_END ending;
95 INT width; /* width of the line in pixels */
96 INT index; /* line index into the buffer */
97 struct tagLINEDEF *next;
98 } LINEDEF;
100 typedef struct
102 BOOL is_unicode; /* how the control was created */
103 LPWSTR text; /* the actual contents of the control */
104 UINT buffer_size; /* the size of the buffer in characters */
105 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
106 HFONT font; /* NULL means standard system font */
107 INT x_offset; /* scroll offset for multi lines this is in pixels
108 for single lines it's in characters */
109 INT line_height; /* height of a screen line in pixels */
110 INT char_width; /* average character width in pixels */
111 DWORD style; /* sane version of wnd->dwStyle */
112 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
113 INT undo_insert_count; /* number of characters inserted in sequence */
114 UINT undo_position; /* character index of the insertion and deletion */
115 LPWSTR undo_text; /* deleted text */
116 UINT undo_buffer_size; /* size of the deleted text buffer */
117 INT selection_start; /* == selection_end if no selection */
118 INT selection_end; /* == current caret position */
119 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
120 INT left_margin; /* in pixels */
121 INT right_margin; /* in pixels */
122 RECT format_rect;
123 INT text_width; /* width of the widest line in pixels for multi line controls
124 and just line width for single line controls */
125 INT region_posx; /* Position of cursor relative to region: */
126 INT region_posy; /* -1: to left, 0: within, 1: to right */
127 EDITWORDBREAKPROC16 word_break_proc16;
128 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
129 INT line_count; /* number of lines */
130 INT y_offset; /* scroll offset in number of lines */
131 BOOL bCaptureState; /* flag indicating whether mouse was captured */
132 BOOL bEnableState; /* flag keeping the enable state */
133 HWND hwndSelf; /* the our window handle */
134 HWND hwndParent; /* Handle of parent for sending EN_* messages.
135 Even if parent will change, EN_* messages
136 should be sent to the first parent. */
137 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
139 * only for multi line controls
141 INT lock_count; /* amount of re-entries in the EditWndProc */
142 INT tabs_count;
143 LPINT tabs;
144 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
145 HLOCAL hloc32W; /* our unicode local memory block */
146 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
147 or EM_SETHANDLE16 */
148 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
149 or EM_SETHANDLE */
150 } EDITSTATE;
153 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
154 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
156 /* used for disabled or read-only edit control */
157 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
158 do \
159 { /* Notify parent which has created this edit control */ \
160 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
161 SendMessageW(es->hwndParent, WM_COMMAND, \
162 MAKEWPARAM(GetWindowLongW((es->hwndSelf),GWL_ID), wNotifyCode), \
163 (LPARAM)(es->hwndSelf)); \
164 } while(0)
166 /*********************************************************************
168 * Declarations
173 * These functions have trivial implementations
174 * We still like to call them internally
175 * "static inline" makes them more like macro's
177 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
178 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
179 static inline void EDIT_WM_Clear(EDITSTATE *es);
180 static inline void EDIT_WM_Cut(EDITSTATE *es);
183 * Helper functions only valid for one type of control
185 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
186 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
187 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
188 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
189 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
190 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
191 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
193 * Helper functions valid for both single line _and_ multi line controls
195 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
196 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
197 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
198 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
199 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
200 static void EDIT_LockBuffer(EDITSTATE *es);
201 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size, BOOL honor_limit);
202 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
203 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
204 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
205 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
206 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
207 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
208 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
209 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
210 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
211 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
212 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
213 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
214 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
215 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
217 * EM_XXX message handlers
219 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
220 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
221 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
222 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
223 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
224 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
225 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
226 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
227 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
228 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
229 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
230 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
231 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
232 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
233 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
234 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
235 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
236 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
237 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
238 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
239 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
240 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
241 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
242 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
243 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam);
244 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
245 static BOOL EDIT_EM_Undo(EDITSTATE *es);
247 * WM_XXX message handlers
249 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
250 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
251 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
252 static void EDIT_WM_Copy(EDITSTATE *es);
253 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
254 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
255 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
256 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
257 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
258 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
259 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
260 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
261 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
262 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
263 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
264 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
265 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
266 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam);
267 static void EDIT_WM_Paste(EDITSTATE *es);
268 static void EDIT_WM_SetFocus(EDITSTATE *es);
269 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
270 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode);
271 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
272 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
273 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
274 static void EDIT_WM_Timer(EDITSTATE *es);
275 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
276 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
277 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
279 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
280 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
282 /*********************************************************************
283 * edit class descriptor
285 const struct builtin_class_descr EDIT_builtin_class =
287 "Edit", /* name */
288 CS_DBLCLKS | CS_PARENTDC, /* style */
289 EditWndProcA, /* procA */
290 EditWndProcW, /* procW */
291 sizeof(EDITSTATE *), /* extra */
292 IDC_IBEAM, /* cursor */
293 0 /* brush */
297 /*********************************************************************
299 * EM_CANUNDO
302 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
304 return (es->undo_insert_count || strlenW(es->undo_text));
308 /*********************************************************************
310 * EM_EMPTYUNDOBUFFER
313 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
315 es->undo_insert_count = 0;
316 *es->undo_text = '\0';
320 /*********************************************************************
322 * WM_CLEAR
325 static inline void EDIT_WM_Clear(EDITSTATE *es)
327 static const WCHAR empty_stringW[] = {0};
329 /* Protect read-only edit control from modification */
330 if(es->style & ES_READONLY)
331 return;
333 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
337 /*********************************************************************
339 * WM_CUT
342 static inline void EDIT_WM_Cut(EDITSTATE *es)
344 EDIT_WM_Copy(es);
345 EDIT_WM_Clear(es);
349 /**********************************************************************
350 * get_app_version
352 * Returns the window version in case Wine emulates a later version
353 * of windows than the application expects.
355 * In a number of cases when windows runs an application that was
356 * designed for an earlier windows version, windows reverts
357 * to "old" behaviour of that earlier version.
359 * An example is a disabled edit control that needs to be painted.
360 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
361 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
362 * applications with an expected version 0f 4.0 or higher.
365 static DWORD get_app_version(void)
367 static DWORD version;
368 if (!version)
370 DWORD dwEmulatedVersion;
371 OSVERSIONINFOW info;
372 DWORD dwProcVersion = GetProcessVersion(0);
374 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
375 GetVersionExW( &info );
376 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
377 /* FIXME: this may not be 100% correct; see discussion on the
378 * wine developer list in Nov 1999 */
379 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
381 return version;
385 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
387 UINT msg;
389 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
390 msg = WM_CTLCOLORSTATIC;
391 else
392 msg = WM_CTLCOLOREDIT;
394 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
395 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
398 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
400 if(unicode)
401 return DefWindowProcW(hwnd, msg, wParam, lParam);
402 else
403 return DefWindowProcA(hwnd, msg, wParam, lParam);
406 /*********************************************************************
408 * EditWndProc_common
410 * The messages are in the order of the actual integer values
411 * (which can be found in include/windows.h)
412 * Wherever possible the 16 bit versions are converted to
413 * the 32 bit ones, so that we can 'fall through' to the
414 * helper functions. These are mostly 32 bit (with a few
415 * exceptions, clearly indicated by a '16' extension to their
416 * names).
419 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
420 WPARAM wParam, LPARAM lParam, BOOL unicode )
422 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
423 LRESULT result = 0;
425 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, msg, wParam, lParam);
427 if (!es && msg != WM_NCCREATE)
428 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
429 else if (msg == WM_NCCREATE)
430 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
431 else if (msg == WM_DESTROY)
432 return EDIT_WM_Destroy(es);
435 if (es) EDIT_LockBuffer(es);
437 switch (msg) {
438 case EM_GETSEL16:
439 wParam = 0;
440 lParam = 0;
441 /* fall through */
442 case EM_GETSEL:
443 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
444 break;
446 case EM_SETSEL16:
447 if ((short)LOWORD(lParam) == -1)
448 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
449 else
450 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
451 if (!wParam)
452 EDIT_EM_ScrollCaret(es);
453 result = 1;
454 break;
455 case EM_SETSEL:
456 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
457 EDIT_EM_ScrollCaret(es);
458 result = 1;
459 break;
461 case EM_GETRECT16:
462 if (lParam)
463 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
464 break;
465 case EM_GETRECT:
466 if (lParam)
467 CopyRect((LPRECT)lParam, &es->format_rect);
468 break;
470 case EM_SETRECT16:
471 if ((es->style & ES_MULTILINE) && lParam) {
472 RECT rc;
473 CONV_RECT16TO32(MapSL(lParam), &rc);
474 EDIT_SetRectNP(es, &rc);
475 EDIT_UpdateText(es, NULL, TRUE);
477 break;
478 case EM_SETRECT:
479 if ((es->style & ES_MULTILINE) && lParam) {
480 EDIT_SetRectNP(es, (LPRECT)lParam);
481 EDIT_UpdateText(es, NULL, TRUE);
483 break;
485 case EM_SETRECTNP16:
486 if ((es->style & ES_MULTILINE) && lParam) {
487 RECT rc;
488 CONV_RECT16TO32(MapSL(lParam), &rc);
489 EDIT_SetRectNP(es, &rc);
491 break;
492 case EM_SETRECTNP:
493 if ((es->style & ES_MULTILINE) && lParam)
494 EDIT_SetRectNP(es, (LPRECT)lParam);
495 break;
497 case EM_SCROLL16:
498 case EM_SCROLL:
499 result = EDIT_EM_Scroll(es, (INT)wParam);
500 break;
502 case EM_LINESCROLL16:
503 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
504 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
505 /* fall through */
506 case EM_LINESCROLL:
507 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
508 break;
510 case EM_SCROLLCARET16:
511 case EM_SCROLLCARET:
512 EDIT_EM_ScrollCaret(es);
513 result = 1;
514 break;
516 case EM_GETMODIFY16:
517 case EM_GETMODIFY:
518 result = ((es->flags & EF_MODIFIED) != 0);
519 break;
521 case EM_SETMODIFY16:
522 case EM_SETMODIFY:
523 if (wParam)
524 es->flags |= EF_MODIFIED;
525 else
526 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
527 break;
529 case EM_GETLINECOUNT16:
530 case EM_GETLINECOUNT:
531 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
532 break;
534 case EM_LINEINDEX16:
535 if ((INT16)wParam == -1)
536 wParam = (WPARAM)-1;
537 /* fall through */
538 case EM_LINEINDEX:
539 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
540 break;
542 case EM_SETHANDLE16:
543 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
544 break;
545 case EM_SETHANDLE:
546 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
547 break;
549 case EM_GETHANDLE16:
550 result = (LRESULT)EDIT_EM_GetHandle16(es);
551 break;
552 case EM_GETHANDLE:
553 result = (LRESULT)EDIT_EM_GetHandle(es);
554 break;
556 case EM_GETTHUMB16:
557 case EM_GETTHUMB:
558 result = EDIT_EM_GetThumb(es);
559 break;
561 /* these messages missing from specs */
562 case WM_USER+15:
563 case 0x00bf:
564 case WM_USER+16:
565 case 0x00c0:
566 case WM_USER+19:
567 case 0x00c3:
568 case WM_USER+26:
569 case 0x00ca:
570 FIXME("undocumented message 0x%x, please report\n", msg);
571 result = DefWindowProcW(hwnd, msg, wParam, lParam);
572 break;
574 case EM_LINELENGTH16:
575 case EM_LINELENGTH:
576 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
577 break;
579 case EM_REPLACESEL16:
580 lParam = (LPARAM)MapSL(lParam);
581 unicode = FALSE; /* 16-bit message is always ascii */
582 /* fall through */
583 case EM_REPLACESEL:
585 LPWSTR textW;
587 if(unicode)
588 textW = (LPWSTR)lParam;
589 else
591 LPSTR textA = (LPSTR)lParam;
592 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
593 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
594 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
597 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
598 result = 1;
600 if(!unicode)
601 HeapFree(GetProcessHeap(), 0, textW);
602 break;
605 case EM_GETLINE16:
606 lParam = (LPARAM)MapSL(lParam);
607 unicode = FALSE; /* 16-bit message is always ascii */
608 /* fall through */
609 case EM_GETLINE:
610 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
611 break;
613 case EM_LIMITTEXT16:
614 case EM_SETLIMITTEXT:
615 EDIT_EM_SetLimitText(es, (INT)wParam);
616 break;
618 case EM_CANUNDO16:
619 case EM_CANUNDO:
620 result = (LRESULT)EDIT_EM_CanUndo(es);
621 break;
623 case EM_UNDO16:
624 case EM_UNDO:
625 case WM_UNDO:
626 result = (LRESULT)EDIT_EM_Undo(es);
627 break;
629 case EM_FMTLINES16:
630 case EM_FMTLINES:
631 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
632 break;
634 case EM_LINEFROMCHAR16:
635 case EM_LINEFROMCHAR:
636 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
637 break;
639 case EM_SETTABSTOPS16:
640 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
641 break;
642 case EM_SETTABSTOPS:
643 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
644 break;
646 case EM_SETPASSWORDCHAR16:
647 unicode = FALSE; /* 16-bit message is always ascii */
648 /* fall through */
649 case EM_SETPASSWORDCHAR:
651 WCHAR charW = 0;
653 if(unicode)
654 charW = (WCHAR)wParam;
655 else
657 CHAR charA = wParam;
658 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
661 EDIT_EM_SetPasswordChar(es, charW);
662 break;
665 case EM_EMPTYUNDOBUFFER16:
666 case EM_EMPTYUNDOBUFFER:
667 EDIT_EM_EmptyUndoBuffer(es);
668 break;
670 case EM_GETFIRSTVISIBLELINE16:
671 result = es->y_offset;
672 break;
673 case EM_GETFIRSTVISIBLELINE:
674 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
675 break;
677 case EM_SETREADONLY16:
678 case EM_SETREADONLY:
679 if (wParam) {
680 SetWindowLongW( hwnd, GWL_STYLE,
681 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
682 es->style |= ES_READONLY;
683 } else {
684 SetWindowLongW( hwnd, GWL_STYLE,
685 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
686 es->style &= ~ES_READONLY;
688 result = 1;
689 break;
691 case EM_SETWORDBREAKPROC16:
692 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
693 break;
694 case EM_SETWORDBREAKPROC:
695 EDIT_EM_SetWordBreakProc(es, lParam);
696 break;
698 case EM_GETWORDBREAKPROC16:
699 result = (LRESULT)es->word_break_proc16;
700 break;
701 case EM_GETWORDBREAKPROC:
702 result = (LRESULT)es->word_break_proc;
703 break;
705 case EM_GETPASSWORDCHAR16:
706 unicode = FALSE; /* 16-bit message is always ascii */
707 /* fall through */
708 case EM_GETPASSWORDCHAR:
710 if(unicode)
711 result = es->password_char;
712 else
714 WCHAR charW = es->password_char;
715 CHAR charA = 0;
716 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
717 result = charA;
719 break;
722 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
724 case EM_SETMARGINS:
725 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
726 break;
728 case EM_GETMARGINS:
729 result = MAKELONG(es->left_margin, es->right_margin);
730 break;
732 case EM_GETLIMITTEXT:
733 result = es->buffer_limit;
734 break;
736 case EM_POSFROMCHAR:
737 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
738 break;
740 case EM_CHARFROMPOS:
741 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
742 break;
744 /* End of the EM_ messages which were in numerical order; what order
745 * are these in? vaguely alphabetical?
748 case WM_GETDLGCODE:
749 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
751 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
753 int vk = (int)((LPMSG)lParam)->wParam;
755 if (vk == VK_RETURN && (GetWindowLongW( hwnd, GWL_STYLE ) & ES_WANTRETURN))
757 result |= DLGC_WANTMESSAGE;
759 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
761 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
762 result |= DLGC_WANTMESSAGE;
765 break;
767 case WM_IME_CHAR:
768 if (!unicode)
770 WCHAR charW;
771 CHAR strng[2];
773 strng[0] = wParam >> 8;
774 strng[1] = wParam & 0xff;
775 MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
776 EDIT_WM_Char(es, charW);
777 break;
779 /* fall through */
780 case WM_CHAR:
782 WCHAR charW;
784 if(unicode)
785 charW = wParam;
786 else
788 CHAR charA = wParam;
789 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
792 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
794 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
795 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
796 break;
798 EDIT_WM_Char(es, charW);
799 break;
802 case WM_CLEAR:
803 EDIT_WM_Clear(es);
804 break;
806 case WM_COMMAND:
807 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
808 break;
810 case WM_CONTEXTMENU:
811 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
812 break;
814 case WM_COPY:
815 EDIT_WM_Copy(es);
816 break;
818 case WM_CREATE:
819 if(unicode)
820 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
821 else
823 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
824 LPWSTR nameW = NULL;
825 if(nameA)
827 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
828 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
829 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
831 result = EDIT_WM_Create(es, nameW);
832 if(nameW)
833 HeapFree(GetProcessHeap(), 0, nameW);
835 break;
837 case WM_CUT:
838 EDIT_WM_Cut(es);
839 break;
841 case WM_ENABLE:
842 es->bEnableState = (BOOL) wParam;
843 EDIT_UpdateText(es, NULL, TRUE);
844 break;
846 case WM_ERASEBKGND:
847 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
848 break;
850 case WM_GETFONT:
851 result = (LRESULT)es->font;
852 break;
854 case WM_GETTEXT:
855 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
856 break;
858 case WM_GETTEXTLENGTH:
859 if (unicode) result = strlenW(es->text);
860 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
861 NULL, 0, NULL, NULL );
862 break;
864 case WM_HSCROLL:
865 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
866 break;
868 case WM_KEYDOWN:
869 result = EDIT_WM_KeyDown(es, (INT)wParam);
870 break;
872 case WM_KILLFOCUS:
873 result = EDIT_WM_KillFocus(es);
874 break;
876 case WM_LBUTTONDBLCLK:
877 result = EDIT_WM_LButtonDblClk(es);
878 break;
880 case WM_LBUTTONDOWN:
881 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
882 break;
884 case WM_LBUTTONUP:
885 result = EDIT_WM_LButtonUp(es);
886 break;
888 case WM_MBUTTONDOWN:
889 result = EDIT_WM_MButtonDown(es);
890 break;
892 case WM_MOUSEACTIVATE:
893 result = MA_ACTIVATE;
894 break;
896 case WM_MOUSEMOVE:
897 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
898 break;
900 case WM_PAINT:
901 EDIT_WM_Paint(es, wParam);
902 break;
904 case WM_PASTE:
905 EDIT_WM_Paste(es);
906 break;
908 case WM_SETFOCUS:
909 EDIT_WM_SetFocus(es);
910 break;
912 case WM_SETFONT:
913 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
914 break;
916 case WM_SETREDRAW:
917 /* FIXME: actually set an internal flag and behave accordingly */
918 break;
920 case WM_SETTEXT:
921 EDIT_WM_SetText(es, lParam, unicode);
922 result = TRUE;
923 break;
925 case WM_SIZE:
926 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
927 break;
929 case WM_STYLECHANGED:
930 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
931 break;
933 case WM_STYLECHANGING:
934 result = 0; /* See EDIT_WM_StyleChanged */
935 break;
937 case WM_SYSKEYDOWN:
938 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
939 break;
941 case WM_TIMER:
942 EDIT_WM_Timer(es);
943 break;
945 case WM_VSCROLL:
946 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
947 break;
949 case WM_MOUSEWHEEL:
951 int gcWheelDelta = 0;
952 UINT pulScrollLines = 3;
953 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
955 if (wParam & (MK_SHIFT | MK_CONTROL)) {
956 result = DefWindowProcW(hwnd, msg, wParam, lParam);
957 break;
959 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
960 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
962 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
963 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
964 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
967 break;
968 default:
969 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
970 break;
973 if (es) EDIT_UnlockBuffer(es, FALSE);
975 return result;
978 /*********************************************************************
980 * EditWndProcW (USER32.@)
982 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
984 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
987 /*********************************************************************
989 * EditWndProc (USER32.@)
991 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
993 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
996 /*********************************************************************
998 * EDIT_BuildLineDefs_ML
1000 * Build linked list of text lines.
1001 * Lines can end with '\0' (last line), a character (if it is wrapped),
1002 * a soft return '\r\r\n' or a hard return '\r\n'
1005 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1007 HDC dc;
1008 HFONT old_font = 0;
1009 LPWSTR current_position, cp;
1010 INT fw;
1011 LINEDEF *current_line;
1012 LINEDEF *previous_line;
1013 LINEDEF *start_line;
1014 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1015 INT line_count = es->line_count;
1016 INT orig_net_length;
1017 RECT rc;
1019 if (istart == iend && delta == 0)
1020 return;
1022 dc = GetDC(es->hwndSelf);
1023 if (es->font)
1024 old_font = SelectObject(dc, es->font);
1026 previous_line = NULL;
1027 current_line = es->first_line_def;
1029 /* Find starting line. istart must lie inside an existing line or
1030 * at the end of buffer */
1031 do {
1032 if (istart < current_line->index + current_line->length ||
1033 current_line->ending == END_0)
1034 break;
1036 previous_line = current_line;
1037 current_line = current_line->next;
1038 line_index++;
1039 } while (current_line);
1041 if (!current_line) /* Error occurred start is not inside previous buffer */
1043 FIXME(" modification occurred outside buffer\n");
1044 ReleaseDC(es->hwndSelf, dc);
1045 return;
1048 /* Remember start of modifications in order to calculate update region */
1049 nstart_line = line_index;
1050 nstart_index = current_line->index;
1052 /* We must start to reformat from the previous line since the modifications
1053 * may have caused the line to wrap upwards. */
1054 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1056 line_index--;
1057 current_line = previous_line;
1059 start_line = current_line;
1061 fw = es->format_rect.right - es->format_rect.left;
1062 current_position = es->text + current_line->index;
1063 do {
1064 if (current_line != start_line)
1066 if (!current_line || current_line->index + delta > current_position - es->text)
1068 /* The buffer has been expanded, create a new line and
1069 insert it into the link list */
1070 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1071 new_line->next = previous_line->next;
1072 previous_line->next = new_line;
1073 current_line = new_line;
1074 es->line_count++;
1076 else if (current_line->index + delta < current_position - es->text)
1078 /* The previous line merged with this line so we delete this extra entry */
1079 previous_line->next = current_line->next;
1080 HeapFree(GetProcessHeap(), 0, current_line);
1081 current_line = previous_line->next;
1082 es->line_count--;
1083 continue;
1085 else /* current_line->index + delta == current_position */
1087 if (current_position - es->text > iend)
1088 break; /* We reached end of line modifications */
1089 /* else recalulate this line */
1093 current_line->index = current_position - es->text;
1094 orig_net_length = current_line->net_length;
1096 /* Find end of line */
1097 cp = current_position;
1098 while (*cp) {
1099 if (*cp == '\n') break;
1100 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1101 break;
1102 cp++;
1105 /* Mark type of line termination */
1106 if (!(*cp)) {
1107 current_line->ending = END_0;
1108 current_line->net_length = strlenW(current_position);
1109 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1110 current_line->ending = END_SOFT;
1111 current_line->net_length = cp - current_position - 1;
1112 } else if (*cp == '\n') {
1113 current_line->ending = END_RICH;
1114 current_line->net_length = cp - current_position;
1115 } else {
1116 current_line->ending = END_HARD;
1117 current_line->net_length = cp - current_position;
1120 /* Calculate line width */
1121 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1122 current_position, current_line->net_length,
1123 es->tabs_count, es->tabs));
1125 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1126 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1127 INT next = 0;
1128 INT prev;
1129 do {
1130 prev = next;
1131 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1132 prev + 1, current_line->net_length, WB_RIGHT);
1133 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1134 current_position, next, es->tabs_count, es->tabs));
1135 } while (current_line->width <= fw);
1136 if (!prev) { /* Didn't find a line break so force a break */
1137 next = 0;
1138 do {
1139 prev = next;
1140 next++;
1141 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1142 current_position, next, es->tabs_count, es->tabs));
1143 } while (current_line->width <= fw);
1144 if (!prev)
1145 prev = 1;
1148 /* If the first line we are calculating, wrapped before istart, we must
1149 * adjust istart in order for this to be reflected in the update region. */
1150 if (current_line->index == nstart_index && istart > current_line->index + prev)
1151 istart = current_line->index + prev;
1152 /* else if we are updating the previous line before the first line we
1153 * are re-calculating and it expanded */
1154 else if (current_line == start_line &&
1155 current_line->index != nstart_index && orig_net_length < prev)
1157 /* Line expanded due to an upwards line wrap so we must partially include
1158 * previous line in update region */
1159 nstart_line = line_index;
1160 nstart_index = current_line->index;
1161 istart = current_line->index + orig_net_length;
1164 current_line->net_length = prev;
1165 current_line->ending = END_WRAP;
1166 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1167 current_line->net_length, es->tabs_count, es->tabs));
1171 /* Adjust length to include line termination */
1172 switch (current_line->ending) {
1173 case END_SOFT:
1174 current_line->length = current_line->net_length + 3;
1175 break;
1176 case END_RICH:
1177 current_line->length = current_line->net_length + 1;
1178 break;
1179 case END_HARD:
1180 current_line->length = current_line->net_length + 2;
1181 break;
1182 case END_WRAP:
1183 case END_0:
1184 current_line->length = current_line->net_length;
1185 break;
1187 es->text_width = max(es->text_width, current_line->width);
1188 current_position += current_line->length;
1189 previous_line = current_line;
1190 current_line = current_line->next;
1191 line_index++;
1192 } while (previous_line->ending != END_0);
1194 /* Finish adjusting line indexes by delta or remove hanging lines */
1195 if (previous_line->ending == END_0)
1197 LINEDEF *pnext = NULL;
1199 previous_line->next = NULL;
1200 while (current_line)
1202 pnext = current_line->next;
1203 HeapFree(GetProcessHeap(), 0, current_line);
1204 current_line = pnext;
1205 es->line_count--;
1208 else
1210 while (current_line)
1212 current_line->index += delta;
1213 current_line = current_line->next;
1217 /* Calculate rest of modification rectangle */
1218 if (hrgn)
1220 HRGN tmphrgn;
1222 * We calculate two rectangles. One for the first line which may have
1223 * an indent with respect to the format rect. The other is a format-width
1224 * rectangle that spans the rest of the lines that changed or moved.
1226 rc.top = es->format_rect.top + nstart_line * es->line_height -
1227 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1228 rc.bottom = rc.top + es->line_height;
1229 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1230 es->text + nstart_index, istart - nstart_index,
1231 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1232 rc.right = es->format_rect.right;
1233 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1235 rc.top = rc.bottom;
1236 rc.left = es->format_rect.left;
1237 rc.right = es->format_rect.right;
1239 * If lines were added or removed we must re-paint the remainder of the
1240 * lines since the remaining lines were either shifted up or down.
1242 if (line_count < es->line_count) /* We added lines */
1243 rc.bottom = es->line_count * es->line_height;
1244 else if (line_count > es->line_count) /* We removed lines */
1245 rc.bottom = line_count * es->line_height;
1246 else
1247 rc.bottom = line_index * es->line_height;
1248 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1249 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1250 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1251 DeleteObject(tmphrgn);
1254 if (es->font)
1255 SelectObject(dc, old_font);
1257 ReleaseDC(es->hwndSelf, dc);
1260 /*********************************************************************
1262 * EDIT_CalcLineWidth_SL
1265 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1267 es->text_width = (short)LOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1270 /*********************************************************************
1272 * EDIT_CallWordBreakProc
1274 * Call appropriate WordBreakProc (internal or external).
1276 * Note: The "start" argument should always be an index referring
1277 * to es->text. The actual wordbreak proc might be
1278 * 16 bit, so we can't always pass any 32 bit LPSTR.
1279 * Hence we assume that es->text is the buffer that holds
1280 * the string under examination (we can decide this for ourselves).
1283 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1285 INT ret, iWndsLocks;
1287 /* To avoid any deadlocks, all the locks on the window structures
1288 must be suspended before the control is passed to the application */
1289 iWndsLocks = WIN_SuspendWndsLock();
1291 if (es->word_break_proc16) {
1292 HGLOBAL16 hglob16;
1293 SEGPTR segptr;
1294 INT countA;
1295 WORD args[5];
1296 DWORD result;
1298 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1299 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1300 segptr = K32WOWGlobalLock16(hglob16);
1301 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1302 args[4] = SELECTOROF(segptr);
1303 args[3] = OFFSETOF(segptr);
1304 args[2] = index;
1305 args[1] = countA;
1306 args[0] = action;
1307 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1308 ret = LOWORD(result);
1309 GlobalUnlock16(hglob16);
1310 GlobalFree16(hglob16);
1312 else if (es->word_break_proc)
1314 if(es->is_unicode)
1316 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1318 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1319 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1320 ret = wbpW(es->text + start, index, count, action);
1322 else
1324 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1325 INT countA;
1326 CHAR *textA;
1328 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1329 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1330 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1331 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1332 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1333 ret = wbpA(textA, index, countA, action);
1334 HeapFree(GetProcessHeap(), 0, textA);
1337 else
1338 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1340 WIN_RestoreWndsLock(iWndsLocks);
1341 return ret;
1345 /*********************************************************************
1347 * EDIT_CharFromPos
1349 * Beware: This is not the function called on EM_CHARFROMPOS
1350 * The position _can_ be outside the formatting / client
1351 * rectangle
1352 * The return value is only the character index
1355 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1357 INT index;
1358 HDC dc;
1359 HFONT old_font = 0;
1361 if (es->style & ES_MULTILINE) {
1362 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1363 INT line_index = 0;
1364 LINEDEF *line_def = es->first_line_def;
1365 INT low, high;
1366 while ((line > 0) && line_def->next) {
1367 line_index += line_def->length;
1368 line_def = line_def->next;
1369 line--;
1371 x += es->x_offset - es->format_rect.left;
1372 if (x >= line_def->width) {
1373 if (after_wrap)
1374 *after_wrap = (line_def->ending == END_WRAP);
1375 return line_index + line_def->net_length;
1377 if (x <= 0) {
1378 if (after_wrap)
1379 *after_wrap = FALSE;
1380 return line_index;
1382 dc = GetDC(es->hwndSelf);
1383 if (es->font)
1384 old_font = SelectObject(dc, es->font);
1385 low = line_index + 1;
1386 high = line_index + line_def->net_length + 1;
1387 while (low < high - 1)
1389 INT mid = (low + high) / 2;
1390 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1391 else low = mid;
1393 index = low;
1395 if (after_wrap)
1396 *after_wrap = ((index == line_index + line_def->net_length) &&
1397 (line_def->ending == END_WRAP));
1398 } else {
1399 LPWSTR text;
1400 SIZE size;
1401 if (after_wrap)
1402 *after_wrap = FALSE;
1403 x -= es->format_rect.left;
1404 if (!x)
1405 return es->x_offset;
1406 text = EDIT_GetPasswordPointer_SL(es);
1407 dc = GetDC(es->hwndSelf);
1408 if (es->font)
1409 old_font = SelectObject(dc, es->font);
1410 if (x < 0)
1412 INT low = 0;
1413 INT high = es->x_offset;
1414 while (low < high - 1)
1416 INT mid = (low + high) / 2;
1417 GetTextExtentPoint32W( dc, text + mid,
1418 es->x_offset - mid, &size );
1419 if (size.cx > -x) low = mid;
1420 else high = mid;
1422 index = low;
1424 else
1426 INT low = es->x_offset;
1427 INT high = strlenW(es->text) + 1;
1428 while (low < high - 1)
1430 INT mid = (low + high) / 2;
1431 GetTextExtentPoint32W( dc, text + es->x_offset,
1432 mid - es->x_offset, &size );
1433 if (size.cx > x) high = mid;
1434 else low = mid;
1436 index = low;
1438 if (es->style & ES_PASSWORD)
1439 HeapFree(GetProcessHeap(), 0, text);
1441 if (es->font)
1442 SelectObject(dc, old_font);
1443 ReleaseDC(es->hwndSelf, dc);
1444 return index;
1448 /*********************************************************************
1450 * EDIT_ConfinePoint
1452 * adjusts the point to be within the formatting rectangle
1453 * (so CharFromPos returns the nearest _visible_ character)
1456 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1458 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1459 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1463 /*********************************************************************
1465 * EDIT_GetLineRect
1467 * Calculates the bounding rectangle for a line from a starting
1468 * column to an ending column.
1471 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1473 INT line_index = EDIT_EM_LineIndex(es, line);
1475 if (es->style & ES_MULTILINE)
1476 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1477 else
1478 rc->top = es->format_rect.top;
1479 rc->bottom = rc->top + es->line_height;
1480 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1481 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1485 /*********************************************************************
1487 * EDIT_GetPasswordPointer_SL
1489 * note: caller should free the (optionally) allocated buffer
1492 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1494 if (es->style & ES_PASSWORD) {
1495 INT len = strlenW(es->text);
1496 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1497 text[len] = '\0';
1498 while(len) text[--len] = es->password_char;
1499 return text;
1500 } else
1501 return es->text;
1505 /*********************************************************************
1507 * EDIT_LockBuffer
1509 * This acts as a LOCAL_Lock(), but it locks only once. This way
1510 * you can call it whenever you like, without unlocking.
1512 * Initially the edit control allocates a HLOCAL32 buffer
1513 * (32 bit linear memory handler). However, 16 bit application
1514 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1515 * handler). From that moment on we have to keep using this 16 bit memory
1516 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1517 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1518 * conversion.
1521 static void EDIT_LockBuffer(EDITSTATE *es)
1523 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
1524 if (!es->text) {
1525 CHAR *textA = NULL;
1526 UINT countA = 0;
1527 BOOL _16bit = FALSE;
1529 if(es->hloc32W)
1531 if(es->hloc32A)
1533 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1534 textA = LocalLock(es->hloc32A);
1535 countA = strlen(textA) + 1;
1537 else if(es->hloc16)
1539 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1540 textA = LOCAL_Lock(hInstance, es->hloc16);
1541 countA = strlen(textA) + 1;
1542 _16bit = TRUE;
1545 else {
1546 ERR("no buffer ... please report\n");
1547 return;
1550 if(textA)
1552 HLOCAL hloc32W_new;
1553 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1554 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1555 if(countW_new > es->buffer_size + 1)
1557 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1558 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1559 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1560 if(hloc32W_new)
1562 es->hloc32W = hloc32W_new;
1563 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1564 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1566 else
1567 WARN("FAILED! Will synchronize partially\n");
1571 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1572 es->text = LocalLock(es->hloc32W);
1574 if(textA)
1576 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1577 if(_16bit)
1578 LOCAL_Unlock(hInstance, es->hloc16);
1579 else
1580 LocalUnlock(es->hloc32A);
1583 es->lock_count++;
1587 /*********************************************************************
1589 * EDIT_SL_InvalidateText
1591 * Called from EDIT_InvalidateText().
1592 * Does the job for single-line controls only.
1595 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1597 RECT line_rect;
1598 RECT rc;
1600 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1601 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1602 EDIT_UpdateText(es, &rc, TRUE);
1606 /*********************************************************************
1608 * EDIT_ML_InvalidateText
1610 * Called from EDIT_InvalidateText().
1611 * Does the job for multi-line controls only.
1614 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1616 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1617 INT sl = EDIT_EM_LineFromChar(es, start);
1618 INT el = EDIT_EM_LineFromChar(es, end);
1619 INT sc;
1620 INT ec;
1621 RECT rc1;
1622 RECT rcWnd;
1623 RECT rcLine;
1624 RECT rcUpdate;
1625 INT l;
1627 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1628 return;
1630 sc = start - EDIT_EM_LineIndex(es, sl);
1631 ec = end - EDIT_EM_LineIndex(es, el);
1632 if (sl < es->y_offset) {
1633 sl = es->y_offset;
1634 sc = 0;
1636 if (el > es->y_offset + vlc) {
1637 el = es->y_offset + vlc;
1638 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1640 GetClientRect(es->hwndSelf, &rc1);
1641 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1642 if (sl == el) {
1643 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1644 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1645 EDIT_UpdateText(es, &rcUpdate, TRUE);
1646 } else {
1647 EDIT_GetLineRect(es, sl, sc,
1648 EDIT_EM_LineLength(es,
1649 EDIT_EM_LineIndex(es, sl)),
1650 &rcLine);
1651 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1652 EDIT_UpdateText(es, &rcUpdate, TRUE);
1653 for (l = sl + 1 ; l < el ; l++) {
1654 EDIT_GetLineRect(es, l, 0,
1655 EDIT_EM_LineLength(es,
1656 EDIT_EM_LineIndex(es, l)),
1657 &rcLine);
1658 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1659 EDIT_UpdateText(es, &rcUpdate, TRUE);
1661 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1662 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1663 EDIT_UpdateText(es, &rcUpdate, TRUE);
1668 /*********************************************************************
1670 * EDIT_InvalidateText
1672 * Invalidate the text from offset start upto, but not including,
1673 * offset end. Useful for (re)painting the selection.
1674 * Regions outside the linewidth are not invalidated.
1675 * end == -1 means end == TextLength.
1676 * start and end need not be ordered.
1679 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1681 if (end == start)
1682 return;
1684 if (end == -1)
1685 end = strlenW(es->text);
1687 if (end < start) {
1688 INT tmp = start;
1689 start = end;
1690 end = tmp;
1693 if (es->style & ES_MULTILINE)
1694 EDIT_ML_InvalidateText(es, start, end);
1695 else
1696 EDIT_SL_InvalidateText(es, start, end);
1700 /*********************************************************************
1702 * EDIT_MakeFit
1704 * Try to fit size + 1 characters in the buffer.
1705 * Constrain to limits if honor_limit is TRUE.
1707 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size, BOOL honor_limit)
1709 HLOCAL hNew32W;
1711 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
1712 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
1713 return FALSE;
1716 if (size <= es->buffer_size)
1717 return TRUE;
1719 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1721 /* Force edit to unlock it's buffer. es->text now NULL */
1722 EDIT_UnlockBuffer(es, TRUE);
1724 if (es->hloc32W) {
1725 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1726 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1727 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1728 es->hloc32W = hNew32W;
1729 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1733 EDIT_LockBuffer(es);
1735 if (es->buffer_size < size) {
1736 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1737 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1738 return FALSE;
1739 } else {
1740 TRACE("We now have %d+1\n", es->buffer_size);
1741 return TRUE;
1746 /*********************************************************************
1748 * EDIT_MakeUndoFit
1750 * Try to fit size + 1 bytes in the undo buffer.
1753 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1755 UINT alloc_size;
1757 if (size <= es->undo_buffer_size)
1758 return TRUE;
1760 TRACE("trying to ReAlloc to %d+1\n", size);
1762 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1763 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1764 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1765 return TRUE;
1767 else
1769 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1770 return FALSE;
1775 /*********************************************************************
1777 * EDIT_MoveBackward
1780 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1782 INT e = es->selection_end;
1784 if (e) {
1785 e--;
1786 if ((es->style & ES_MULTILINE) && e &&
1787 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1788 e--;
1789 if (e && (es->text[e - 1] == '\r'))
1790 e--;
1793 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1794 EDIT_EM_ScrollCaret(es);
1798 /*********************************************************************
1800 * EDIT_MoveDown_ML
1802 * Only for multi line controls
1803 * Move the caret one line down, on a column with the nearest
1804 * x coordinate on the screen (might be a different column).
1807 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1809 INT s = es->selection_start;
1810 INT e = es->selection_end;
1811 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1812 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1813 INT x = (short)LOWORD(pos);
1814 INT y = (short)HIWORD(pos);
1816 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1817 if (!extend)
1818 s = e;
1819 EDIT_EM_SetSel(es, s, e, after_wrap);
1820 EDIT_EM_ScrollCaret(es);
1824 /*********************************************************************
1826 * EDIT_MoveEnd
1829 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1831 BOOL after_wrap = FALSE;
1832 INT e;
1834 /* Pass a high value in x to make sure of receiving the end of the line */
1835 if (es->style & ES_MULTILINE)
1836 e = EDIT_CharFromPos(es, 0x3fffffff,
1837 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1838 else
1839 e = strlenW(es->text);
1840 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1841 EDIT_EM_ScrollCaret(es);
1845 /*********************************************************************
1847 * EDIT_MoveForward
1850 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1852 INT e = es->selection_end;
1854 if (es->text[e]) {
1855 e++;
1856 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1857 if (es->text[e] == '\n')
1858 e++;
1859 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1860 e += 2;
1863 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1864 EDIT_EM_ScrollCaret(es);
1868 /*********************************************************************
1870 * EDIT_MoveHome
1872 * Home key: move to beginning of line.
1875 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1877 INT e;
1879 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1880 if (es->style & ES_MULTILINE)
1881 e = EDIT_CharFromPos(es, -es->x_offset,
1882 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1883 else
1884 e = 0;
1885 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1886 EDIT_EM_ScrollCaret(es);
1890 /*********************************************************************
1892 * EDIT_MovePageDown_ML
1894 * Only for multi line controls
1895 * Move the caret one page down, on a column with the nearest
1896 * x coordinate on the screen (might be a different column).
1899 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1901 INT s = es->selection_start;
1902 INT e = es->selection_end;
1903 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1904 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1905 INT x = (short)LOWORD(pos);
1906 INT y = (short)HIWORD(pos);
1908 e = EDIT_CharFromPos(es, x,
1909 y + (es->format_rect.bottom - es->format_rect.top),
1910 &after_wrap);
1911 if (!extend)
1912 s = e;
1913 EDIT_EM_SetSel(es, s, e, after_wrap);
1914 EDIT_EM_ScrollCaret(es);
1918 /*********************************************************************
1920 * EDIT_MovePageUp_ML
1922 * Only for multi line controls
1923 * Move the caret one page up, on a column with the nearest
1924 * x coordinate on the screen (might be a different column).
1927 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1929 INT s = es->selection_start;
1930 INT e = es->selection_end;
1931 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1932 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1933 INT x = (short)LOWORD(pos);
1934 INT y = (short)HIWORD(pos);
1936 e = EDIT_CharFromPos(es, x,
1937 y - (es->format_rect.bottom - es->format_rect.top),
1938 &after_wrap);
1939 if (!extend)
1940 s = e;
1941 EDIT_EM_SetSel(es, s, e, after_wrap);
1942 EDIT_EM_ScrollCaret(es);
1946 /*********************************************************************
1948 * EDIT_MoveUp_ML
1950 * Only for multi line controls
1951 * Move the caret one line up, on a column with the nearest
1952 * x coordinate on the screen (might be a different column).
1955 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1957 INT s = es->selection_start;
1958 INT e = es->selection_end;
1959 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1960 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1961 INT x = (short)LOWORD(pos);
1962 INT y = (short)HIWORD(pos);
1964 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1965 if (!extend)
1966 s = e;
1967 EDIT_EM_SetSel(es, s, e, after_wrap);
1968 EDIT_EM_ScrollCaret(es);
1972 /*********************************************************************
1974 * EDIT_MoveWordBackward
1977 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1979 INT s = es->selection_start;
1980 INT e = es->selection_end;
1981 INT l;
1982 INT ll;
1983 INT li;
1985 l = EDIT_EM_LineFromChar(es, e);
1986 ll = EDIT_EM_LineLength(es, e);
1987 li = EDIT_EM_LineIndex(es, l);
1988 if (e - li == 0) {
1989 if (l) {
1990 li = EDIT_EM_LineIndex(es, l - 1);
1991 e = li + EDIT_EM_LineLength(es, li);
1993 } else {
1994 e = li + (INT)EDIT_CallWordBreakProc(es,
1995 li, e - li, ll, WB_LEFT);
1997 if (!extend)
1998 s = e;
1999 EDIT_EM_SetSel(es, s, e, FALSE);
2000 EDIT_EM_ScrollCaret(es);
2004 /*********************************************************************
2006 * EDIT_MoveWordForward
2009 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2011 INT s = es->selection_start;
2012 INT e = es->selection_end;
2013 INT l;
2014 INT ll;
2015 INT li;
2017 l = EDIT_EM_LineFromChar(es, e);
2018 ll = EDIT_EM_LineLength(es, e);
2019 li = EDIT_EM_LineIndex(es, l);
2020 if (e - li == ll) {
2021 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2022 e = EDIT_EM_LineIndex(es, l + 1);
2023 } else {
2024 e = li + EDIT_CallWordBreakProc(es,
2025 li, e - li + 1, ll, WB_RIGHT);
2027 if (!extend)
2028 s = e;
2029 EDIT_EM_SetSel(es, s, e, FALSE);
2030 EDIT_EM_ScrollCaret(es);
2034 /*********************************************************************
2036 * EDIT_PaintLine
2039 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2041 INT s = es->selection_start;
2042 INT e = es->selection_end;
2043 INT li;
2044 INT ll;
2045 INT x;
2046 INT y;
2047 LRESULT pos;
2049 if (es->style & ES_MULTILINE) {
2050 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2051 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2052 return;
2053 } else if (line)
2054 return;
2056 TRACE("line=%d\n", line);
2058 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2059 x = (short)LOWORD(pos);
2060 y = (short)HIWORD(pos);
2061 li = EDIT_EM_LineIndex(es, line);
2062 ll = EDIT_EM_LineLength(es, li);
2063 s = min(es->selection_start, es->selection_end);
2064 e = max(es->selection_start, es->selection_end);
2065 s = min(li + ll, max(li, s));
2066 e = min(li + ll, max(li, e));
2067 if (rev && (s != e) &&
2068 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2069 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2070 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2071 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2072 } else
2073 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2077 /*********************************************************************
2079 * EDIT_PaintText
2082 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2084 COLORREF BkColor;
2085 COLORREF TextColor;
2086 INT ret;
2087 INT li;
2088 INT BkMode;
2089 SIZE size;
2091 if (!count)
2092 return 0;
2093 BkMode = GetBkMode(dc);
2094 BkColor = GetBkColor(dc);
2095 TextColor = GetTextColor(dc);
2096 if (rev) {
2097 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2098 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2099 SetBkMode( dc, OPAQUE);
2101 li = EDIT_EM_LineIndex(es, line);
2102 if (es->style & ES_MULTILINE) {
2103 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2104 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2105 } else {
2106 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2107 TextOutW(dc, x, y, text + li + col, count);
2108 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2109 ret = size.cx;
2110 if (es->style & ES_PASSWORD)
2111 HeapFree(GetProcessHeap(), 0, text);
2113 if (rev) {
2114 SetBkColor(dc, BkColor);
2115 SetTextColor(dc, TextColor);
2116 SetBkMode( dc, BkMode);
2118 return ret;
2122 /*********************************************************************
2124 * EDIT_SetCaretPos
2127 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2128 BOOL after_wrap)
2130 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2131 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2135 /*********************************************************************
2137 * EDIT_SetRectNP
2139 * note: this is not (exactly) the handler called on EM_SETRECTNP
2140 * it is also used to set the rect of a single line control
2143 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2145 CopyRect(&es->format_rect, rc);
2146 if (es->style & WS_BORDER) {
2147 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2148 if(TWEAK_WineLook == WIN31_LOOK)
2149 bw += 2;
2150 es->format_rect.left += bw;
2151 es->format_rect.top += bw;
2152 es->format_rect.right -= bw;
2153 es->format_rect.bottom -= bw;
2155 es->format_rect.left += es->left_margin;
2156 es->format_rect.right -= es->right_margin;
2157 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2158 if (es->style & ES_MULTILINE)
2160 INT fw, vlc, max_x_offset, max_y_offset;
2162 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2163 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2165 /* correct es->x_offset */
2166 fw = es->format_rect.right - es->format_rect.left;
2167 max_x_offset = es->text_width - fw;
2168 if(max_x_offset < 0) max_x_offset = 0;
2169 if(es->x_offset > max_x_offset)
2170 es->x_offset = max_x_offset;
2172 /* correct es->y_offset */
2173 max_y_offset = es->line_count - vlc;
2174 if(max_y_offset < 0) max_y_offset = 0;
2175 if(es->y_offset > max_y_offset)
2176 es->y_offset = max_y_offset;
2178 /* force scroll info update */
2179 EDIT_UpdateScrollInfo(es);
2181 else
2182 /* Windows doesn't care to fix text placement for SL controls */
2183 es->format_rect.bottom = es->format_rect.top + es->line_height;
2185 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2186 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2190 /*********************************************************************
2192 * EDIT_UnlockBuffer
2195 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2197 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2199 /* Edit window might be already destroyed */
2200 if(!IsWindow(es->hwndSelf))
2202 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2203 return;
2206 if (!es->lock_count) {
2207 ERR("lock_count == 0 ... please report\n");
2208 return;
2210 if (!es->text) {
2211 ERR("es->text == 0 ... please report\n");
2212 return;
2215 if (force || (es->lock_count == 1)) {
2216 if (es->hloc32W) {
2217 CHAR *textA = NULL;
2218 BOOL _16bit = FALSE;
2219 UINT countA = 0;
2220 UINT countW = strlenW(es->text) + 1;
2222 if(es->hloc32A)
2224 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2225 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2226 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2227 countA = LocalSize(es->hloc32A);
2228 if(countA_new > countA)
2230 HLOCAL hloc32A_new;
2231 UINT alloc_size = ROUND_TO_GROW(countA_new);
2232 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2233 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2234 if(hloc32A_new)
2236 es->hloc32A = hloc32A_new;
2237 countA = LocalSize(hloc32A_new);
2238 TRACE("Real new size %d bytes\n", countA);
2240 else
2241 WARN("FAILED! Will synchronize partially\n");
2243 textA = LocalLock(es->hloc32A);
2245 else if(es->hloc16)
2247 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2248 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2249 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2250 countA = LOCAL_Size(hInstance, es->hloc16);
2251 if(countA_new > countA)
2253 HLOCAL16 hloc16_new;
2254 UINT alloc_size = ROUND_TO_GROW(countA_new);
2255 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2256 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2257 if(hloc16_new)
2259 es->hloc16 = hloc16_new;
2260 countA = LOCAL_Size(hInstance, hloc16_new);
2261 TRACE("Real new size %d bytes\n", countA);
2263 else
2264 WARN("FAILED! Will synchronize partially\n");
2266 textA = LOCAL_Lock(hInstance, es->hloc16);
2267 _16bit = TRUE;
2270 if(textA)
2272 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2273 if(_16bit)
2274 LOCAL_Unlock(hInstance, es->hloc16);
2275 else
2276 LocalUnlock(es->hloc32A);
2279 LocalUnlock(es->hloc32W);
2280 es->text = NULL;
2282 else {
2283 ERR("no buffer ... please report\n");
2284 return;
2287 es->lock_count--;
2291 /*********************************************************************
2293 * EDIT_UpdateScrollInfo
2296 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2298 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2300 SCROLLINFO si;
2301 si.cbSize = sizeof(SCROLLINFO);
2302 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2303 si.nMin = 0;
2304 si.nMax = es->line_count - 1;
2305 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2306 si.nPos = es->y_offset;
2307 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2308 si.nMin, si.nMax, si.nPage, si.nPos);
2309 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2312 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2314 SCROLLINFO si;
2315 si.cbSize = sizeof(SCROLLINFO);
2316 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2317 si.nMin = 0;
2318 si.nMax = es->text_width - 1;
2319 si.nPage = es->format_rect.right - es->format_rect.left;
2320 si.nPos = es->x_offset;
2321 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2322 si.nMin, si.nMax, si.nPage, si.nPos);
2323 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2327 /*********************************************************************
2329 * EDIT_WordBreakProc
2331 * Find the beginning of words.
2332 * Note: unlike the specs for a WordBreakProc, this function only
2333 * allows to be called without linebreaks between s[0] upto
2334 * s[count - 1]. Remember it is only called
2335 * internally, so we can decide this for ourselves.
2338 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2340 INT ret = 0;
2342 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2344 if(!s) return 0;
2346 switch (action) {
2347 case WB_LEFT:
2348 if (!count)
2349 break;
2350 if (index)
2351 index--;
2352 if (s[index] == ' ') {
2353 while (index && (s[index] == ' '))
2354 index--;
2355 if (index) {
2356 while (index && (s[index] != ' '))
2357 index--;
2358 if (s[index] == ' ')
2359 index++;
2361 } else {
2362 while (index && (s[index] != ' '))
2363 index--;
2364 if (s[index] == ' ')
2365 index++;
2367 ret = index;
2368 break;
2369 case WB_RIGHT:
2370 if (!count)
2371 break;
2372 if (index)
2373 index--;
2374 if (s[index] == ' ')
2375 while ((index < count) && (s[index] == ' ')) index++;
2376 else {
2377 while (s[index] && (s[index] != ' ') && (index < count))
2378 index++;
2379 while ((s[index] == ' ') && (index < count)) index++;
2381 ret = index;
2382 break;
2383 case WB_ISDELIMITER:
2384 ret = (s[index] == ' ');
2385 break;
2386 default:
2387 ERR("unknown action code, please report !\n");
2388 break;
2390 return ret;
2394 /*********************************************************************
2396 * EM_CHARFROMPOS
2398 * returns line number (not index) in high-order word of result.
2399 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2400 * to Richedit, not to the edit control. Original documentation is valid.
2401 * FIXME: do the specs mean to return -1 if outside client area or
2402 * if outside formatting rectangle ???
2405 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2407 POINT pt;
2408 RECT rc;
2409 INT index;
2411 pt.x = x;
2412 pt.y = y;
2413 GetClientRect(es->hwndSelf, &rc);
2414 if (!PtInRect(&rc, pt))
2415 return -1;
2417 index = EDIT_CharFromPos(es, x, y, NULL);
2418 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2422 /*********************************************************************
2424 * EM_FMTLINES
2426 * Enable or disable soft breaks.
2428 * This means: insert or remove the soft linebreak character (\r\r\n).
2429 * Take care to check if the text still fits the buffer after insertion.
2430 * If not, notify with EN_ERRSPACE.
2433 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2435 es->flags &= ~EF_USE_SOFTBRK;
2436 if (add_eol) {
2437 es->flags |= EF_USE_SOFTBRK;
2438 FIXME("soft break enabled, not implemented\n");
2440 return add_eol;
2444 /*********************************************************************
2446 * EM_GETHANDLE
2448 * Hopefully this won't fire back at us.
2449 * We always start with a fixed buffer in the local heap.
2450 * Despite of the documentation says that the local heap is used
2451 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2452 * buffer on the local heap.
2455 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2457 HLOCAL hLocal;
2459 if (!(es->style & ES_MULTILINE))
2460 return 0;
2462 if(es->is_unicode)
2463 hLocal = es->hloc32W;
2464 else
2466 if(!es->hloc32A)
2468 CHAR *textA;
2469 UINT countA, alloc_size;
2470 TRACE("Allocating 32-bit ANSI alias buffer\n");
2471 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2472 alloc_size = ROUND_TO_GROW(countA);
2473 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2475 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2476 return 0;
2478 textA = LocalLock(es->hloc32A);
2479 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2480 LocalUnlock(es->hloc32A);
2482 hLocal = es->hloc32A;
2485 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2486 return hLocal;
2490 /*********************************************************************
2492 * EM_GETHANDLE16
2494 * Hopefully this won't fire back at us.
2495 * We always start with a buffer in 32 bit linear memory.
2496 * However, with this message a 16 bit application requests
2497 * a handle of 16 bit local heap memory, where it expects to find
2498 * the text.
2499 * It's a pitty that from this moment on we have to use this
2500 * local heap, because applications may rely on the handle
2501 * in the future.
2503 * In this function we'll try to switch to local heap.
2505 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2507 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2508 CHAR *textA;
2509 UINT countA, alloc_size;
2511 if (!(es->style & ES_MULTILINE))
2512 return 0;
2514 if (es->hloc16)
2515 return es->hloc16;
2517 if (!LOCAL_HeapSize(hInstance)) {
2518 if (!LocalInit16(hInstance, 0,
2519 GlobalSize16(hInstance))) {
2520 ERR("could not initialize local heap\n");
2521 return 0;
2523 TRACE("local heap initialized\n");
2526 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2527 alloc_size = ROUND_TO_GROW(countA);
2529 TRACE("Allocating 16-bit ANSI alias buffer\n");
2530 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2531 ERR("could not allocate new 16 bit buffer\n");
2532 return 0;
2535 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2536 ERR("could not lock new 16 bit buffer\n");
2537 LOCAL_Free(hInstance, es->hloc16);
2538 es->hloc16 = 0;
2539 return 0;
2542 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2543 LOCAL_Unlock(hInstance, es->hloc16);
2545 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2546 return es->hloc16;
2550 /*********************************************************************
2552 * EM_GETLINE
2555 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2557 LPWSTR src;
2558 INT line_len, dst_len;
2559 INT i;
2561 if (es->style & ES_MULTILINE) {
2562 if (line >= es->line_count)
2563 return 0;
2564 } else
2565 line = 0;
2566 i = EDIT_EM_LineIndex(es, line);
2567 src = es->text + i;
2568 line_len = EDIT_EM_LineLength(es, i);
2569 dst_len = *(WORD *)lParam;
2570 if(unicode)
2572 LPWSTR dst = (LPWSTR)lParam;
2573 if(dst_len <= line_len)
2575 memcpy(dst, src, dst_len * sizeof(WCHAR));
2576 return dst_len;
2578 else /* Append 0 if enough space */
2580 memcpy(dst, src, line_len * sizeof(WCHAR));
2581 dst[line_len] = 0;
2582 return line_len;
2585 else
2587 LPSTR dst = (LPSTR)lParam;
2588 INT ret;
2589 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2590 if(!ret) /* Insufficient buffer size */
2591 return dst_len;
2592 if(ret < dst_len) /* Append 0 if enough space */
2593 dst[ret] = 0;
2594 return ret;
2599 /*********************************************************************
2601 * EM_GETSEL
2604 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2606 UINT s = es->selection_start;
2607 UINT e = es->selection_end;
2609 ORDER_UINT(s, e);
2610 if (start)
2611 *start = s;
2612 if (end)
2613 *end = e;
2614 return MAKELONG(s, e);
2618 /*********************************************************************
2620 * EM_GETTHUMB
2622 * FIXME: is this right ? (or should it be only VSCROLL)
2623 * (and maybe only for edit controls that really have their
2624 * own scrollbars) (and maybe only for multiline controls ?)
2625 * All in all: very poorly documented
2628 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2630 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2631 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2635 /*********************************************************************
2637 * EM_LINEFROMCHAR
2640 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2642 INT line;
2643 LINEDEF *line_def;
2645 if (!(es->style & ES_MULTILINE))
2646 return 0;
2647 if (index > (INT)strlenW(es->text))
2648 return es->line_count - 1;
2649 if (index == -1)
2650 index = min(es->selection_start, es->selection_end);
2652 line = 0;
2653 line_def = es->first_line_def;
2654 index -= line_def->length;
2655 while ((index >= 0) && line_def->next) {
2656 line++;
2657 line_def = line_def->next;
2658 index -= line_def->length;
2660 return line;
2664 /*********************************************************************
2666 * EM_LINEINDEX
2669 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2671 INT line_index;
2672 LINEDEF *line_def;
2674 if (!(es->style & ES_MULTILINE))
2675 return 0;
2676 if (line >= es->line_count)
2677 return -1;
2679 line_index = 0;
2680 line_def = es->first_line_def;
2681 if (line == -1) {
2682 INT index = es->selection_end - line_def->length;
2683 while ((index >= 0) && line_def->next) {
2684 line_index += line_def->length;
2685 line_def = line_def->next;
2686 index -= line_def->length;
2688 } else {
2689 while (line > 0) {
2690 line_index += line_def->length;
2691 line_def = line_def->next;
2692 line--;
2695 return line_index;
2699 /*********************************************************************
2701 * EM_LINELENGTH
2704 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2706 LINEDEF *line_def;
2708 if (!(es->style & ES_MULTILINE))
2709 return strlenW(es->text);
2711 if (index == -1) {
2712 /* get the number of remaining non-selected chars of selected lines */
2713 INT32 l; /* line number */
2714 INT32 li; /* index of first char in line */
2715 INT32 count;
2716 l = EDIT_EM_LineFromChar(es, es->selection_start);
2717 /* # chars before start of selection area */
2718 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2719 l = EDIT_EM_LineFromChar(es, es->selection_end);
2720 /* # chars after end of selection */
2721 li = EDIT_EM_LineIndex(es, l);
2722 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2723 return count;
2725 line_def = es->first_line_def;
2726 index -= line_def->length;
2727 while ((index >= 0) && line_def->next) {
2728 line_def = line_def->next;
2729 index -= line_def->length;
2731 return line_def->net_length;
2735 /*********************************************************************
2737 * EM_LINESCROLL
2739 * NOTE: dx is in average character widths, dy - in lines;
2742 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2744 if (!(es->style & ES_MULTILINE))
2745 return FALSE;
2747 dx *= es->char_width;
2748 return EDIT_EM_LineScroll_internal(es, dx, dy);
2751 /*********************************************************************
2753 * EDIT_EM_LineScroll_internal
2755 * Version of EDIT_EM_LineScroll for internal use.
2756 * It doesn't refuse if ES_MULTILINE is set and assumes that
2757 * dx is in pixels, dy - in lines.
2760 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2762 INT nyoff;
2763 INT x_offset_in_pixels;
2765 if (es->style & ES_MULTILINE)
2767 x_offset_in_pixels = es->x_offset;
2769 else
2771 dy = 0;
2772 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2775 if (-dx > x_offset_in_pixels)
2776 dx = -x_offset_in_pixels;
2777 if (dx > es->text_width - x_offset_in_pixels)
2778 dx = es->text_width - x_offset_in_pixels;
2779 nyoff = max(0, es->y_offset + dy);
2780 if (nyoff >= es->line_count)
2781 nyoff = es->line_count - 1;
2782 dy = (es->y_offset - nyoff) * es->line_height;
2783 if (dx || dy) {
2784 RECT rc1;
2785 RECT rc;
2787 es->y_offset = nyoff;
2788 if(es->style & ES_MULTILINE)
2789 es->x_offset += dx;
2790 else
2791 es->x_offset += dx / es->char_width;
2793 GetClientRect(es->hwndSelf, &rc1);
2794 IntersectRect(&rc, &rc1, &es->format_rect);
2795 ScrollWindowEx(es->hwndSelf, -dx, dy,
2796 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2797 /* force scroll info update */
2798 EDIT_UpdateScrollInfo(es);
2800 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2801 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2802 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2803 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2804 return TRUE;
2808 /*********************************************************************
2810 * EM_POSFROMCHAR
2813 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2815 INT len = strlenW(es->text);
2816 INT l;
2817 INT li;
2818 INT x;
2819 INT y = 0;
2820 HDC dc;
2821 HFONT old_font = 0;
2822 SIZE size;
2824 index = min(index, len);
2825 dc = GetDC(es->hwndSelf);
2826 if (es->font)
2827 old_font = SelectObject(dc, es->font);
2828 if (es->style & ES_MULTILINE) {
2829 l = EDIT_EM_LineFromChar(es, index);
2830 y = (l - es->y_offset) * es->line_height;
2831 li = EDIT_EM_LineIndex(es, l);
2832 if (after_wrap && (li == index) && l) {
2833 INT l2 = l - 1;
2834 LINEDEF *line_def = es->first_line_def;
2835 while (l2) {
2836 line_def = line_def->next;
2837 l2--;
2839 if (line_def->ending == END_WRAP) {
2840 l--;
2841 y -= es->line_height;
2842 li = EDIT_EM_LineIndex(es, l);
2845 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2846 es->tabs_count, es->tabs)) - es->x_offset;
2847 } else {
2848 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2849 if (index < es->x_offset) {
2850 GetTextExtentPoint32W(dc, text + index,
2851 es->x_offset - index, &size);
2852 x = -size.cx;
2853 } else {
2854 GetTextExtentPoint32W(dc, text + es->x_offset,
2855 index - es->x_offset, &size);
2856 x = size.cx;
2858 y = 0;
2859 if (es->style & ES_PASSWORD)
2860 HeapFree(GetProcessHeap(), 0, text);
2862 x += es->format_rect.left;
2863 y += es->format_rect.top;
2864 if (es->font)
2865 SelectObject(dc, old_font);
2866 ReleaseDC(es->hwndSelf, dc);
2867 return MAKELONG((INT16)x, (INT16)y);
2871 /*********************************************************************
2873 * EM_REPLACESEL
2875 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2878 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2880 UINT strl = strlenW(lpsz_replace);
2881 UINT tl = strlenW(es->text);
2882 UINT utl;
2883 UINT s;
2884 UINT e;
2885 UINT i;
2886 LPWSTR p;
2887 HRGN hrgn = 0;
2889 TRACE("%s, can_undo %d, send_update %d\n",
2890 debugstr_w(lpsz_replace), can_undo, send_update);
2892 s = es->selection_start;
2893 e = es->selection_end;
2895 if ((s == e) && !strl)
2896 return;
2898 ORDER_UINT(s, e);
2900 if (!EDIT_MakeFit(es, tl - (e - s) + strl, honor_limit))
2901 return;
2903 if (e != s) {
2904 /* there is something to be deleted */
2905 TRACE("deleting stuff.\n");
2906 if (can_undo) {
2907 utl = strlenW(es->undo_text);
2908 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2909 /* undo-buffer is extended to the right */
2910 EDIT_MakeUndoFit(es, utl + e - s);
2911 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2912 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2913 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2914 /* undo-buffer is extended to the left */
2915 EDIT_MakeUndoFit(es, utl + e - s);
2916 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2917 p[e - s] = p[0];
2918 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2919 p[i] = (es->text + s)[i];
2920 es->undo_position = s;
2921 } else {
2922 /* new undo-buffer */
2923 EDIT_MakeUndoFit(es, e - s);
2924 strncpyW(es->undo_text, es->text + s, e - s + 1);
2925 es->undo_text[e - s] = 0; /* ensure 0 termination */
2926 es->undo_position = s;
2928 /* any deletion makes the old insertion-undo invalid */
2929 es->undo_insert_count = 0;
2930 } else
2931 EDIT_EM_EmptyUndoBuffer(es);
2933 /* now delete */
2934 strcpyW(es->text + s, es->text + e);
2936 if (strl) {
2937 /* there is an insertion */
2938 if (can_undo) {
2939 if ((s == es->undo_position) ||
2940 ((es->undo_insert_count) &&
2941 (s == es->undo_position + es->undo_insert_count)))
2943 * insertion is new and at delete position or
2944 * an extension to either left or right
2946 es->undo_insert_count += strl;
2947 else {
2948 /* new insertion undo */
2949 es->undo_position = s;
2950 es->undo_insert_count = strl;
2951 /* new insertion makes old delete-buffer invalid */
2952 *es->undo_text = '\0';
2954 } else
2955 EDIT_EM_EmptyUndoBuffer(es);
2957 /* now insert */
2958 tl = strlenW(es->text);
2959 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));
2960 for (p = es->text + tl ; p >= es->text + s ; p--)
2961 p[strl] = p[0];
2962 for (i = 0 , p = es->text + s ; i < strl ; i++)
2963 p[i] = lpsz_replace[i];
2964 if(es->style & ES_UPPERCASE)
2965 CharUpperBuffW(p, strl);
2966 else if(es->style & ES_LOWERCASE)
2967 CharLowerBuffW(p, strl);
2968 s += strl;
2970 if (es->style & ES_MULTILINE)
2972 INT s = min(es->selection_start, es->selection_end);
2974 hrgn = CreateRectRgn(0, 0, 0, 0);
2975 EDIT_BuildLineDefs_ML(es, s, s + strl,
2976 strl - abs(es->selection_end - es->selection_start), hrgn);
2978 else
2979 EDIT_CalcLineWidth_SL(es);
2981 EDIT_EM_SetSel(es, s, s, FALSE);
2982 es->flags |= EF_MODIFIED;
2983 if (send_update) es->flags |= EF_UPDATE;
2984 if (hrgn)
2986 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2987 DeleteObject(hrgn);
2989 else
2990 EDIT_UpdateText(es, NULL, TRUE);
2992 EDIT_EM_ScrollCaret(es);
2994 /* force scroll info update */
2995 EDIT_UpdateScrollInfo(es);
2998 if(es->flags & EF_UPDATE)
3000 es->flags &= ~EF_UPDATE;
3001 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3006 /*********************************************************************
3008 * EM_SCROLL
3011 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3013 INT dy;
3015 if (!(es->style & ES_MULTILINE))
3016 return (LRESULT)FALSE;
3018 dy = 0;
3020 switch (action) {
3021 case SB_LINEUP:
3022 if (es->y_offset)
3023 dy = -1;
3024 break;
3025 case SB_LINEDOWN:
3026 if (es->y_offset < es->line_count - 1)
3027 dy = 1;
3028 break;
3029 case SB_PAGEUP:
3030 if (es->y_offset)
3031 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3032 break;
3033 case SB_PAGEDOWN:
3034 if (es->y_offset < es->line_count - 1)
3035 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3036 break;
3037 default:
3038 return (LRESULT)FALSE;
3040 if (dy) {
3041 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3042 /* check if we are going to move too far */
3043 if(es->y_offset + dy > es->line_count - vlc)
3044 dy = es->line_count - vlc - es->y_offset;
3046 /* Notification is done in EDIT_EM_LineScroll */
3047 if(dy)
3048 EDIT_EM_LineScroll(es, 0, dy);
3050 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3054 /*********************************************************************
3056 * EM_SCROLLCARET
3059 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3061 if (es->style & ES_MULTILINE) {
3062 INT l;
3063 INT li;
3064 INT vlc;
3065 INT ww;
3066 INT cw = es->char_width;
3067 INT x;
3068 INT dy = 0;
3069 INT dx = 0;
3071 l = EDIT_EM_LineFromChar(es, es->selection_end);
3072 li = EDIT_EM_LineIndex(es, l);
3073 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3074 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3075 if (l >= es->y_offset + vlc)
3076 dy = l - vlc + 1 - es->y_offset;
3077 if (l < es->y_offset)
3078 dy = l - es->y_offset;
3079 ww = es->format_rect.right - es->format_rect.left;
3080 if (x < es->format_rect.left)
3081 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3082 if (x > es->format_rect.right)
3083 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3084 if (dy || dx)
3086 /* check if we are going to move too far */
3087 if(es->x_offset + dx + ww > es->text_width)
3088 dx = es->text_width - ww - es->x_offset;
3089 if(dx || dy)
3090 EDIT_EM_LineScroll_internal(es, dx, dy);
3092 } else {
3093 INT x;
3094 INT goal;
3095 INT format_width;
3097 if (!(es->style & ES_AUTOHSCROLL))
3098 return;
3100 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3101 format_width = es->format_rect.right - es->format_rect.left;
3102 if (x < es->format_rect.left) {
3103 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3104 do {
3105 es->x_offset--;
3106 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3107 } while ((x < goal) && es->x_offset);
3108 /* FIXME: use ScrollWindow() somehow to improve performance */
3109 EDIT_UpdateText(es, NULL, TRUE);
3110 } else if (x > es->format_rect.right) {
3111 INT x_last;
3112 INT len = strlenW(es->text);
3113 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3114 do {
3115 es->x_offset++;
3116 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3117 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3118 } while ((x > goal) && (x_last > es->format_rect.right));
3119 /* FIXME: use ScrollWindow() somehow to improve performance */
3120 EDIT_UpdateText(es, NULL, TRUE);
3124 if(es->flags & EF_FOCUSED)
3125 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3129 /*********************************************************************
3131 * EM_SETHANDLE
3133 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3136 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3138 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3140 if (!(es->style & ES_MULTILINE))
3141 return;
3143 if (!hloc) {
3144 WARN("called with NULL handle\n");
3145 return;
3148 EDIT_UnlockBuffer(es, TRUE);
3150 if(es->hloc16)
3152 LOCAL_Free(hInstance, es->hloc16);
3153 es->hloc16 = (HLOCAL16)NULL;
3156 if(es->is_unicode)
3158 if(es->hloc32A)
3160 LocalFree(es->hloc32A);
3161 es->hloc32A = NULL;
3163 es->hloc32W = hloc;
3165 else
3167 INT countW, countA;
3168 HLOCAL hloc32W_new;
3169 WCHAR *textW;
3170 CHAR *textA;
3172 countA = LocalSize(hloc);
3173 textA = LocalLock(hloc);
3174 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3175 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3177 ERR("Could not allocate new unicode buffer\n");
3178 return;
3180 textW = LocalLock(hloc32W_new);
3181 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3182 LocalUnlock(hloc32W_new);
3183 LocalUnlock(hloc);
3185 if(es->hloc32W)
3186 LocalFree(es->hloc32W);
3188 es->hloc32W = hloc32W_new;
3189 es->hloc32A = hloc;
3192 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3194 EDIT_LockBuffer(es);
3196 es->x_offset = es->y_offset = 0;
3197 es->selection_start = es->selection_end = 0;
3198 EDIT_EM_EmptyUndoBuffer(es);
3199 es->flags &= ~EF_MODIFIED;
3200 es->flags &= ~EF_UPDATE;
3201 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3202 EDIT_UpdateText(es, NULL, TRUE);
3203 EDIT_EM_ScrollCaret(es);
3204 /* force scroll info update */
3205 EDIT_UpdateScrollInfo(es);
3209 /*********************************************************************
3211 * EM_SETHANDLE16
3213 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3216 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3218 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3219 INT countW, countA;
3220 HLOCAL hloc32W_new;
3221 WCHAR *textW;
3222 CHAR *textA;
3224 if (!(es->style & ES_MULTILINE))
3225 return;
3227 if (!hloc) {
3228 WARN("called with NULL handle\n");
3229 return;
3232 EDIT_UnlockBuffer(es, TRUE);
3234 if(es->hloc32A)
3236 LocalFree(es->hloc32A);
3237 es->hloc32A = NULL;
3240 countA = LOCAL_Size(hInstance, hloc);
3241 textA = LOCAL_Lock(hInstance, hloc);
3242 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3243 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3245 ERR("Could not allocate new unicode buffer\n");
3246 return;
3248 textW = LocalLock(hloc32W_new);
3249 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3250 LocalUnlock(hloc32W_new);
3251 LOCAL_Unlock(hInstance, hloc);
3253 if(es->hloc32W)
3254 LocalFree(es->hloc32W);
3256 es->hloc32W = hloc32W_new;
3257 es->hloc16 = hloc;
3259 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3261 EDIT_LockBuffer(es);
3263 es->x_offset = es->y_offset = 0;
3264 es->selection_start = es->selection_end = 0;
3265 EDIT_EM_EmptyUndoBuffer(es);
3266 es->flags &= ~EF_MODIFIED;
3267 es->flags &= ~EF_UPDATE;
3268 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3269 EDIT_UpdateText(es, NULL, TRUE);
3270 EDIT_EM_ScrollCaret(es);
3271 /* force scroll info update */
3272 EDIT_UpdateScrollInfo(es);
3276 /*********************************************************************
3278 * EM_SETLIMITTEXT
3280 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3281 * However, the windows version is not complied to yet in all of edit.c
3283 * Additionally as the wrapper for RichEdit controls we need larger buffers
3284 * at present -1 will represent nolimit
3286 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3288 if (limit == 0xFFFFFFFF)
3289 es->buffer_limit = -1;
3290 else if (es->style & ES_MULTILINE) {
3291 if (limit)
3292 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3293 else
3294 es->buffer_limit = BUFLIMIT_MULTI;
3295 } else {
3296 if (limit)
3297 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3298 else
3299 es->buffer_limit = BUFLIMIT_SINGLE;
3304 /*********************************************************************
3306 * EM_SETMARGINS
3308 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3309 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3310 * margin according to the textmetrics of the current font.
3312 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3313 * of the char's width as the margin, but this is not how Windows handles this.
3314 * For all other fonts Windows sets the margins to zero.
3317 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3318 INT left, INT right)
3320 TEXTMETRICW tm;
3321 INT default_left_margin = 0; /* in pixels */
3322 INT default_right_margin = 0; /* in pixels */
3324 /* Set the default margins depending on the font */
3325 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3326 HDC dc = GetDC(es->hwndSelf);
3327 HFONT old_font = SelectObject(dc, es->font);
3328 GetTextMetricsW(dc, &tm);
3329 /* The default margins are only non zero for TrueType or Vector fonts */
3330 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3331 /* This must be calculated more exactly! But how? */
3332 default_left_margin = tm.tmAveCharWidth / 3;
3333 default_right_margin = tm.tmAveCharWidth / 3;
3335 SelectObject(dc, old_font);
3336 ReleaseDC(es->hwndSelf, dc);
3339 if (action & EC_LEFTMARGIN) {
3340 if (left != EC_USEFONTINFO)
3341 es->left_margin = left;
3342 else
3343 es->left_margin = default_left_margin;
3346 if (action & EC_RIGHTMARGIN) {
3347 if (right != EC_USEFONTINFO)
3348 es->right_margin = right;
3349 else
3350 es->right_margin = default_right_margin;
3352 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3356 /*********************************************************************
3358 * EM_SETPASSWORDCHAR
3361 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3363 LONG style;
3365 if (es->style & ES_MULTILINE)
3366 return;
3368 if (es->password_char == c)
3369 return;
3371 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3372 es->password_char = c;
3373 if (c) {
3374 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3375 es->style |= ES_PASSWORD;
3376 } else {
3377 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3378 es->style &= ~ES_PASSWORD;
3380 EDIT_UpdateText(es, NULL, TRUE);
3384 /*********************************************************************
3386 * EDIT_EM_SetSel
3388 * note: unlike the specs say: the order of start and end
3389 * _is_ preserved in Windows. (i.e. start can be > end)
3390 * In other words: this handler is OK
3393 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3395 UINT old_start = es->selection_start;
3396 UINT old_end = es->selection_end;
3397 UINT len = strlenW(es->text);
3399 if (start == (UINT)-1) {
3400 start = es->selection_end;
3401 end = es->selection_end;
3402 } else {
3403 start = min(start, len);
3404 end = min(end, len);
3406 es->selection_start = start;
3407 es->selection_end = end;
3408 if (after_wrap)
3409 es->flags |= EF_AFTER_WRAP;
3410 else
3411 es->flags &= ~EF_AFTER_WRAP;
3412 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3413 ORDER_UINT(start, end);
3414 ORDER_UINT(end, old_end);
3415 ORDER_UINT(start, old_start);
3416 ORDER_UINT(old_start, old_end);
3417 if (end != old_start)
3420 * One can also do
3421 * ORDER_UINT32(end, old_start);
3422 * EDIT_InvalidateText(es, start, end);
3423 * EDIT_InvalidateText(es, old_start, old_end);
3424 * in place of the following if statement.
3426 if (old_start > end )
3428 EDIT_InvalidateText(es, start, end);
3429 EDIT_InvalidateText(es, old_start, old_end);
3431 else
3433 EDIT_InvalidateText(es, start, old_start);
3434 EDIT_InvalidateText(es, end, old_end);
3437 else EDIT_InvalidateText(es, start, old_end);
3441 /*********************************************************************
3443 * EM_SETTABSTOPS
3446 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3448 if (!(es->style & ES_MULTILINE))
3449 return FALSE;
3450 if (es->tabs)
3451 HeapFree(GetProcessHeap(), 0, es->tabs);
3452 es->tabs_count = count;
3453 if (!count)
3454 es->tabs = NULL;
3455 else {
3456 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3457 memcpy(es->tabs, tabs, count * sizeof(INT));
3459 return TRUE;
3463 /*********************************************************************
3465 * EM_SETTABSTOPS16
3468 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3470 if (!(es->style & ES_MULTILINE))
3471 return FALSE;
3472 if (es->tabs)
3473 HeapFree(GetProcessHeap(), 0, es->tabs);
3474 es->tabs_count = count;
3475 if (!count)
3476 es->tabs = NULL;
3477 else {
3478 INT i;
3479 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3480 for (i = 0 ; i < count ; i++)
3481 es->tabs[i] = *tabs++;
3483 return TRUE;
3487 /*********************************************************************
3489 * EM_SETWORDBREAKPROC
3492 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam)
3494 if (es->word_break_proc == (void *)lParam)
3495 return;
3497 es->word_break_proc = (void *)lParam;
3498 es->word_break_proc16 = NULL;
3500 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3501 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3502 EDIT_UpdateText(es, NULL, TRUE);
3507 /*********************************************************************
3509 * EM_SETWORDBREAKPROC16
3512 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3514 if (es->word_break_proc16 == wbp)
3515 return;
3517 es->word_break_proc = NULL;
3518 es->word_break_proc16 = wbp;
3519 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3520 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3521 EDIT_UpdateText(es, NULL, TRUE);
3526 /*********************************************************************
3528 * EM_UNDO / WM_UNDO
3531 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3533 INT ulength;
3534 LPWSTR utext;
3536 /* Protect read-only edit control from modification */
3537 if(es->style & ES_READONLY)
3538 return FALSE;
3540 ulength = strlenW(es->undo_text);
3541 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3543 strcpyW(utext, es->undo_text);
3545 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3546 es->undo_insert_count, debugstr_w(utext));
3548 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3549 EDIT_EM_EmptyUndoBuffer(es);
3550 EDIT_EM_ReplaceSel(es, TRUE, utext, FALSE, TRUE);
3551 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3552 /* send the notification after the selection start and end are set */
3553 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3554 EDIT_EM_ScrollCaret(es);
3555 HeapFree(GetProcessHeap(), 0, utext);
3557 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3558 es->undo_insert_count, debugstr_w(es->undo_text));
3559 return TRUE;
3563 /*********************************************************************
3565 * WM_CHAR
3568 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3570 BOOL control;
3572 /* Protect read-only edit control from modification */
3573 if(es->style & ES_READONLY)
3574 return;
3576 control = GetKeyState(VK_CONTROL) & 0x8000;
3578 switch (c) {
3579 case '\r':
3580 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3581 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3582 break;
3583 case '\n':
3584 if (es->style & ES_MULTILINE) {
3585 if (es->style & ES_READONLY) {
3586 EDIT_MoveHome(es, FALSE);
3587 EDIT_MoveDown_ML(es, FALSE);
3588 } else {
3589 static const WCHAR cr_lfW[] = {'\r','\n',0};
3590 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3593 break;
3594 case '\t':
3595 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3597 static const WCHAR tabW[] = {'\t',0};
3598 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3600 break;
3601 case VK_BACK:
3602 if (!(es->style & ES_READONLY) && !control) {
3603 if (es->selection_start != es->selection_end)
3604 EDIT_WM_Clear(es);
3605 else {
3606 /* delete character left of caret */
3607 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3608 EDIT_MoveBackward(es, TRUE);
3609 EDIT_WM_Clear(es);
3612 break;
3613 case 0x03: /* ^C */
3614 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3615 break;
3616 case 0x16: /* ^V */
3617 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3618 break;
3619 case 0x18: /* ^X */
3620 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3621 break;
3623 default:
3624 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3625 WCHAR str[2];
3626 str[0] = c;
3627 str[1] = '\0';
3628 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3630 break;
3635 /*********************************************************************
3637 * WM_COMMAND
3640 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3642 if (code || control)
3643 return;
3645 switch (id) {
3646 case EM_UNDO:
3647 EDIT_EM_Undo(es);
3648 break;
3649 case WM_CUT:
3650 EDIT_WM_Cut(es);
3651 break;
3652 case WM_COPY:
3653 EDIT_WM_Copy(es);
3654 break;
3655 case WM_PASTE:
3656 EDIT_WM_Paste(es);
3657 break;
3658 case WM_CLEAR:
3659 EDIT_WM_Clear(es);
3660 break;
3661 case EM_SETSEL:
3662 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3663 EDIT_EM_ScrollCaret(es);
3664 break;
3665 default:
3666 ERR("unknown menu item, please report\n");
3667 break;
3672 /*********************************************************************
3674 * WM_CONTEXTMENU
3676 * Note: the resource files resource/sysres_??.rc cannot define a
3677 * single popup menu. Hence we use a (dummy) menubar
3678 * containing the single popup menu as its first item.
3680 * FIXME: the message identifiers have been chosen arbitrarily,
3681 * hence we use MF_BYPOSITION.
3682 * We might as well use the "real" values (anybody knows ?)
3683 * The menu definition is in resources/sysres_??.rc.
3684 * Once these are OK, we better use MF_BYCOMMAND here
3685 * (as we do in EDIT_WM_Command()).
3688 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3690 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3691 HMENU popup = GetSubMenu(menu, 0);
3692 UINT start = es->selection_start;
3693 UINT end = es->selection_end;
3695 ORDER_UINT(start, end);
3697 /* undo */
3698 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3699 /* cut */
3700 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3701 /* copy */
3702 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3703 /* paste */
3704 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3705 /* delete */
3706 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3707 /* select all */
3708 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3710 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3711 DestroyMenu(menu);
3715 /*********************************************************************
3717 * WM_COPY
3720 static void EDIT_WM_Copy(EDITSTATE *es)
3722 INT s = min(es->selection_start, es->selection_end);
3723 INT e = max(es->selection_start, es->selection_end);
3724 HGLOBAL hdst;
3725 LPWSTR dst;
3727 if (e == s) return;
3729 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3730 dst = GlobalLock(hdst);
3731 strncpyW(dst, es->text + s, e - s);
3732 dst[e - s] = 0; /* ensure 0 termination */
3733 TRACE("%s\n", debugstr_w(dst));
3734 GlobalUnlock(hdst);
3735 OpenClipboard(es->hwndSelf);
3736 EmptyClipboard();
3737 SetClipboardData(CF_UNICODETEXT, hdst);
3738 CloseClipboard();
3742 /*********************************************************************
3744 * WM_CREATE
3747 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3749 TRACE("%s\n", debugstr_w(name));
3751 * To initialize some final structure members, we call some helper
3752 * functions. However, since the EDITSTATE is not consistent (i.e.
3753 * not fully initialized), we should be very careful which
3754 * functions can be called, and in what order.
3756 EDIT_WM_SetFont(es, 0, FALSE);
3757 EDIT_EM_EmptyUndoBuffer(es);
3759 if (name && *name) {
3760 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3761 /* if we insert text to the editline, the text scrolls out
3762 * of the window, as the caret is placed after the insert
3763 * pos normally; thus we reset es->selection... to 0 and
3764 * update caret
3766 es->selection_start = es->selection_end = 0;
3767 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3768 * Messages are only to be sent when the USER does something to
3769 * change the contents. So I am removing this EN_CHANGE
3771 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3773 EDIT_EM_ScrollCaret(es);
3775 /* force scroll info update */
3776 EDIT_UpdateScrollInfo(es);
3777 /* The rule seems to return 1 here for success */
3778 /* Power Builder masked edit controls will crash */
3779 /* if not. */
3780 /* FIXME: is that in all cases so ? */
3781 return 1;
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 SetFocus(es->hwndSelf);
4240 if (!(es->flags & EF_FOCUSED))
4241 return 0;
4243 es->bCaptureState = TRUE;
4244 SetCapture(es->hwndSelf);
4245 EDIT_ConfinePoint(es, &x, &y);
4246 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4247 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4248 EDIT_EM_ScrollCaret(es);
4249 es->region_posx = es->region_posy = 0;
4250 SetTimer(es->hwndSelf, 0, 100, NULL);
4251 return 0;
4255 /*********************************************************************
4257 * WM_LBUTTONUP
4260 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4262 if (es->bCaptureState) {
4263 KillTimer(es->hwndSelf, 0);
4264 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4266 es->bCaptureState = FALSE;
4267 return 0;
4271 /*********************************************************************
4273 * WM_MBUTTONDOWN
4276 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4278 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4279 return 0;
4283 /*********************************************************************
4285 * WM_MOUSEMOVE
4288 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4290 INT e;
4291 BOOL after_wrap;
4292 INT prex, prey;
4294 if (GetCapture() != es->hwndSelf)
4295 return 0;
4298 * FIXME: gotta do some scrolling if outside client
4299 * area. Maybe reset the timer ?
4301 prex = x; prey = y;
4302 EDIT_ConfinePoint(es, &x, &y);
4303 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4304 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4305 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4306 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4307 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4308 return 0;
4312 /*********************************************************************
4314 * WM_NCCREATE
4316 * See also EDIT_WM_StyleChanged
4318 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4320 EDITSTATE *es;
4321 UINT alloc_size;
4323 TRACE("Creating %s edit control, style = %08lx\n",
4324 unicode ? "Unicode" : "ANSI", lpcs->style);
4326 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4327 return FALSE;
4328 SetWindowLongW( hwnd, 0, (LONG)es );
4331 * Note: since the EDITSTATE has not been fully initialized yet,
4332 * we can't use any API calls that may send
4333 * WM_XXX messages before WM_NCCREATE is completed.
4336 es->is_unicode = unicode;
4337 es->style = lpcs->style;
4339 es->bEnableState = !(es->style & WS_DISABLED);
4341 es->hwndSelf = hwnd;
4342 /* Save parent, which will be notified by EN_* messages */
4343 es->hwndParent = lpcs->hwndParent;
4345 if (es->style & ES_COMBO)
4346 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4348 /* Number overrides lowercase overrides uppercase (at least it
4349 * does in Win95). However I'll bet that ES_NUMBER would be
4350 * invalid under Win 3.1.
4352 if (es->style & ES_NUMBER) {
4353 ; /* do not override the ES_NUMBER */
4354 } else if (es->style & ES_LOWERCASE) {
4355 es->style &= ~ES_UPPERCASE;
4357 if (es->style & ES_MULTILINE) {
4358 es->buffer_limit = BUFLIMIT_MULTI;
4359 if (es->style & WS_VSCROLL)
4360 es->style |= ES_AUTOVSCROLL;
4361 if (es->style & WS_HSCROLL)
4362 es->style |= ES_AUTOHSCROLL;
4363 es->style &= ~ES_PASSWORD;
4364 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4365 /* Confirmed - RIGHT overrides CENTER */
4366 if (es->style & ES_RIGHT)
4367 es->style &= ~ES_CENTER;
4368 es->style &= ~WS_HSCROLL;
4369 es->style &= ~ES_AUTOHSCROLL;
4372 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4373 es->style |= ES_AUTOVSCROLL;
4374 } else {
4375 es->buffer_limit = BUFLIMIT_SINGLE;
4376 if (WIN31_LOOK == TWEAK_WineLook ||
4377 WIN95_LOOK == TWEAK_WineLook) {
4378 es->style &= ~ES_CENTER;
4379 es->style &= ~ES_RIGHT;
4380 } else {
4381 if (es->style & ES_RIGHT)
4382 es->style &= ~ES_CENTER;
4384 es->style &= ~WS_HSCROLL;
4385 es->style &= ~WS_VSCROLL;
4386 es->style &= ~ES_AUTOVSCROLL;
4387 es->style &= ~ES_WANTRETURN;
4388 if (es->style & ES_PASSWORD)
4389 es->password_char = '*';
4391 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4392 es->style |= ES_AUTOHSCROLL;
4395 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4396 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4397 return FALSE;
4398 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4400 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4401 return FALSE;
4402 es->undo_buffer_size = es->buffer_size;
4404 if (es->style & ES_MULTILINE)
4405 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4406 return FALSE;
4407 es->line_count = 1;
4410 * In Win95 look and feel, the WS_BORDER style is replaced by the
4411 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4412 * control a non client area. Not always. This coordinates in some
4413 * way with the window creation code in dialog.c When making
4414 * modifications please ensure that the code still works for edit
4415 * controls created directly with style 0x50800000, exStyle 0 (
4416 * which should have a single pixel border)
4418 if (TWEAK_WineLook != WIN31_LOOK)
4420 es->style &= ~WS_BORDER;
4422 else
4424 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4425 SetWindowLongW( hwnd, GWL_STYLE,
4426 GetWindowLongW( hwnd, GWL_STYLE ) & ~WS_BORDER );
4429 return TRUE;
4432 /*********************************************************************
4434 * WM_PAINT
4437 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam)
4439 PAINTSTRUCT ps;
4440 INT i;
4441 HDC dc;
4442 HFONT old_font = 0;
4443 RECT rc;
4444 RECT rcLine;
4445 RECT rcRgn;
4446 BOOL rev = es->bEnableState &&
4447 ((es->flags & EF_FOCUSED) ||
4448 (es->style & ES_NOHIDESEL));
4449 if (!wParam)
4450 dc = BeginPaint(es->hwndSelf, &ps);
4451 else
4452 dc = (HDC) wParam;
4453 if(es->style & WS_BORDER) {
4454 GetClientRect(es->hwndSelf, &rc);
4455 if(es->style & ES_MULTILINE) {
4456 if(es->style & WS_HSCROLL) rc.bottom++;
4457 if(es->style & WS_VSCROLL) rc.right++;
4459 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4461 IntersectClipRect(dc, es->format_rect.left,
4462 es->format_rect.top,
4463 es->format_rect.right,
4464 es->format_rect.bottom);
4465 if (es->style & ES_MULTILINE) {
4466 GetClientRect(es->hwndSelf, &rc);
4467 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4469 if (es->font)
4470 old_font = SelectObject(dc, es->font);
4471 EDIT_NotifyCtlColor(es, dc);
4473 if (!es->bEnableState)
4474 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4475 GetClipBox(dc, &rcRgn);
4476 if (es->style & ES_MULTILINE) {
4477 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4478 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4479 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4480 if (IntersectRect(&rc, &rcRgn, &rcLine))
4481 EDIT_PaintLine(es, dc, i, rev);
4483 } else {
4484 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4485 if (IntersectRect(&rc, &rcRgn, &rcLine))
4486 EDIT_PaintLine(es, dc, 0, rev);
4488 if (es->font)
4489 SelectObject(dc, old_font);
4491 if (!wParam)
4492 EndPaint(es->hwndSelf, &ps);
4496 /*********************************************************************
4498 * WM_PASTE
4501 static void EDIT_WM_Paste(EDITSTATE *es)
4503 HGLOBAL hsrc;
4504 LPWSTR src;
4506 /* Protect read-only edit control from modification */
4507 if(es->style & ES_READONLY)
4508 return;
4510 OpenClipboard(es->hwndSelf);
4511 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4512 src = (LPWSTR)GlobalLock(hsrc);
4513 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4514 GlobalUnlock(hsrc);
4516 CloseClipboard();
4520 /*********************************************************************
4522 * WM_SETFOCUS
4525 static void EDIT_WM_SetFocus(EDITSTATE *es)
4527 es->flags |= EF_FOCUSED;
4528 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4529 EDIT_SetCaretPos(es, es->selection_end,
4530 es->flags & EF_AFTER_WRAP);
4531 if(!(es->style & ES_NOHIDESEL))
4532 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4533 ShowCaret(es->hwndSelf);
4534 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4538 /*********************************************************************
4540 * WM_SETFONT
4542 * With Win95 look the margins are set to default font value unless
4543 * the system font (font == 0) is being set, in which case they are left
4544 * unchanged.
4547 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4549 TEXTMETRICW tm;
4550 HDC dc;
4551 HFONT old_font = 0;
4552 RECT r;
4554 es->font = font;
4555 dc = GetDC(es->hwndSelf);
4556 if (font)
4557 old_font = SelectObject(dc, font);
4558 GetTextMetricsW(dc, &tm);
4559 es->line_height = tm.tmHeight;
4560 es->char_width = tm.tmAveCharWidth;
4561 if (font)
4562 SelectObject(dc, old_font);
4563 ReleaseDC(es->hwndSelf, dc);
4564 if (font && (TWEAK_WineLook > WIN31_LOOK))
4565 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4566 EC_USEFONTINFO, EC_USEFONTINFO);
4568 /* Force the recalculation of the format rect for each font change */
4569 GetClientRect(es->hwndSelf, &r);
4570 EDIT_SetRectNP(es, &r);
4572 if (es->style & ES_MULTILINE)
4573 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4574 else
4575 EDIT_CalcLineWidth_SL(es);
4577 if (redraw)
4578 EDIT_UpdateText(es, NULL, TRUE);
4579 if (es->flags & EF_FOCUSED) {
4580 DestroyCaret();
4581 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4582 EDIT_SetCaretPos(es, es->selection_end,
4583 es->flags & EF_AFTER_WRAP);
4584 ShowCaret(es->hwndSelf);
4589 /*********************************************************************
4591 * WM_SETTEXT
4593 * NOTES
4594 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4595 * The modified flag is reset. No notifications are sent.
4597 * For single-line controls, reception of WM_SETTEXT triggers:
4598 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4601 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode)
4603 LPWSTR text = NULL;
4605 if(unicode)
4606 text = (LPWSTR)lParam;
4607 else if (lParam)
4609 LPCSTR textA = (LPCSTR)lParam;
4610 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4611 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4612 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4615 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4616 if (text) {
4617 TRACE("%s\n", debugstr_w(text));
4618 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4619 if(!unicode)
4620 HeapFree(GetProcessHeap(), 0, text);
4621 } else {
4622 static const WCHAR empty_stringW[] = {0};
4623 TRACE("<NULL>\n");
4624 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4626 es->x_offset = 0;
4627 es->flags &= ~EF_MODIFIED;
4628 EDIT_EM_SetSel(es, 0, 0, FALSE);
4629 /* Send the notification after the selection start and end have been set
4630 * edit control doesn't send notification on WM_SETTEXT
4631 * if it is multiline, or it is part of combobox
4633 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4635 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4636 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4638 EDIT_EM_ScrollCaret(es);
4642 /*********************************************************************
4644 * WM_SIZE
4647 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4649 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4650 RECT rc;
4651 TRACE("width = %d, height = %d\n", width, height);
4652 SetRect(&rc, 0, 0, width, height);
4653 EDIT_SetRectNP(es, &rc);
4654 EDIT_UpdateText(es, NULL, TRUE);
4659 /*********************************************************************
4661 * WM_STYLECHANGED
4663 * This message is sent by SetWindowLong on having changed either the Style
4664 * or the extended style.
4666 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4668 * See also EDIT_WM_NCCreate
4670 * It appears that the Windows version of the edit control allows the style
4671 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4672 * style variable which will generally be different. In this function we
4673 * update the internal style based on what changed in the externally visible
4674 * style.
4676 * Much of this content as based upon the MSDN, especially:
4677 * Platform SDK Documentation -> User Interface Services ->
4678 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4679 * Edit Control Styles
4681 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4683 if (GWL_STYLE == which) {
4684 DWORD style_change_mask;
4685 DWORD new_style;
4686 /* Only a subset of changes can be applied after the control
4687 * has been created.
4689 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4690 ES_NUMBER;
4691 if (es->style & ES_MULTILINE)
4692 style_change_mask |= ES_WANTRETURN;
4694 new_style = style->styleNew & style_change_mask;
4696 /* Number overrides lowercase overrides uppercase (at least it
4697 * does in Win95). However I'll bet that ES_NUMBER would be
4698 * invalid under Win 3.1.
4700 if (new_style & ES_NUMBER) {
4701 ; /* do not override the ES_NUMBER */
4702 } else if (new_style & ES_LOWERCASE) {
4703 new_style &= ~ES_UPPERCASE;
4706 es->style = (es->style & ~style_change_mask) | new_style;
4707 } else if (GWL_EXSTYLE == which) {
4708 ; /* FIXME - what is needed here */
4709 } else {
4710 WARN ("Invalid style change %d\n",which);
4713 return 0;
4716 /*********************************************************************
4718 * WM_SYSKEYDOWN
4721 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4723 if ((key == VK_BACK) && (key_data & 0x2000)) {
4724 if (EDIT_EM_CanUndo(es))
4725 EDIT_EM_Undo(es);
4726 return 0;
4727 } else if (key == VK_UP || key == VK_DOWN) {
4728 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4729 return 0;
4731 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4735 /*********************************************************************
4737 * WM_TIMER
4740 static void EDIT_WM_Timer(EDITSTATE *es)
4742 if (es->region_posx < 0) {
4743 EDIT_MoveBackward(es, TRUE);
4744 } else if (es->region_posx > 0) {
4745 EDIT_MoveForward(es, TRUE);
4748 * FIXME: gotta do some vertical scrolling here, like
4749 * EDIT_EM_LineScroll(hwnd, 0, 1);
4753 /*********************************************************************
4755 * WM_VSCROLL
4758 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4760 INT dy;
4762 if (!(es->style & ES_MULTILINE))
4763 return 0;
4765 if (!(es->style & ES_AUTOVSCROLL))
4766 return 0;
4768 dy = 0;
4769 switch (action) {
4770 case SB_LINEUP:
4771 case SB_LINEDOWN:
4772 case SB_PAGEUP:
4773 case SB_PAGEDOWN:
4774 TRACE("action %d\n", action);
4775 EDIT_EM_Scroll(es, action);
4776 return 0;
4777 case SB_TOP:
4778 TRACE("SB_TOP\n");
4779 dy = -es->y_offset;
4780 break;
4781 case SB_BOTTOM:
4782 TRACE("SB_BOTTOM\n");
4783 dy = es->line_count - 1 - es->y_offset;
4784 break;
4785 case SB_THUMBTRACK:
4786 TRACE("SB_THUMBTRACK %d\n", pos);
4787 es->flags |= EF_VSCROLL_TRACK;
4788 if(es->style & WS_VSCROLL)
4789 dy = pos - es->y_offset;
4790 else
4792 /* Assume default scroll range 0-100 */
4793 INT vlc, new_y;
4794 /* Sanity check */
4795 if(pos < 0 || pos > 100) return 0;
4796 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4797 new_y = pos * (es->line_count - vlc) / 100;
4798 dy = es->line_count ? (new_y - es->y_offset) : 0;
4799 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4800 es->line_count, es->y_offset, pos, dy);
4802 break;
4803 case SB_THUMBPOSITION:
4804 TRACE("SB_THUMBPOSITION %d\n", pos);
4805 es->flags &= ~EF_VSCROLL_TRACK;
4806 if(es->style & WS_VSCROLL)
4807 dy = pos - es->y_offset;
4808 else
4810 /* Assume default scroll range 0-100 */
4811 INT vlc, new_y;
4812 /* Sanity check */
4813 if(pos < 0 || pos > 100) return 0;
4814 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4815 new_y = pos * (es->line_count - vlc) / 100;
4816 dy = es->line_count ? (new_y - es->y_offset) : 0;
4817 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4818 es->line_count, es->y_offset, pos, dy);
4820 if (!dy)
4822 /* force scroll info update */
4823 EDIT_UpdateScrollInfo(es);
4824 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4826 break;
4827 case SB_ENDSCROLL:
4828 TRACE("SB_ENDSCROLL\n");
4829 break;
4831 * FIXME : the next two are undocumented !
4832 * Are we doing the right thing ?
4833 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4834 * although it's also a regular control message.
4836 case EM_GETTHUMB: /* this one is used by NT notepad */
4837 case EM_GETTHUMB16:
4839 LRESULT ret;
4840 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4841 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4842 else
4844 /* Assume default scroll range 0-100 */
4845 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4846 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4848 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4849 return ret;
4851 case EM_LINESCROLL16:
4852 TRACE("EM_LINESCROLL16 %d\n", pos);
4853 dy = pos;
4854 break;
4856 default:
4857 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4858 action, action);
4859 return 0;
4861 if (dy)
4862 EDIT_EM_LineScroll(es, 0, dy);
4863 return 0;
4866 /*********************************************************************
4868 * EDIT_UpdateText
4871 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4873 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4874 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4878 /*********************************************************************
4880 * EDIT_UpdateText
4883 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4885 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4886 InvalidateRect(es->hwndSelf, rc, bErase);