Rename ResourceHandle to Reserved in the IMAGE_RESOURCE_DATA_ENTRY
[wine.git] / controls / edit.c
blobfd5fe38e5a12adb6967d9d02a1f7e84a44af374a
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_OEMCONVERT
27 * -!ES_AUTOVSCROLL (every multi line control *is* auto vscroll)
28 * -!ES_AUTOHSCROLL (every single line control *is* auto hscroll)
30 * When there is no autoscrolling, the control should first check whether
31 * the new text would fit. If not, an EN_MAXTEXT should be sent.
32 * However, currently this would require the actual change to be made,
33 * then call EDIT_BuildLineDefs() and then find out that the new text doesn't
34 * fit. After all this, things should be put back in the state before the
35 * changes. Note that for multi line controls !ES_AUTOHSCROLL works : wordwrap.
39 #include "config.h"
41 #include <stdarg.h>
42 #include <string.h>
43 #include <stdlib.h>
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winnt.h"
48 #include "wownt32.h"
49 #include "win.h"
50 #include "wine/winbase16.h"
51 #include "wine/winuser16.h"
52 #include "wine/unicode.h"
53 #include "controls.h"
54 #include "local.h"
55 #include "message.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);
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 (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), 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 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
776 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
777 EDIT_WM_Char(es, charW);
778 break;
780 /* fall through */
781 case WM_CHAR:
783 WCHAR charW;
785 if(unicode)
786 charW = wParam;
787 else
789 CHAR charA = wParam;
790 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
793 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
795 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
796 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
797 break;
799 EDIT_WM_Char(es, charW);
800 break;
803 case WM_CLEAR:
804 EDIT_WM_Clear(es);
805 break;
807 case WM_COMMAND:
808 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
809 break;
811 case WM_CONTEXTMENU:
812 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
813 break;
815 case WM_COPY:
816 EDIT_WM_Copy(es);
817 break;
819 case WM_CREATE:
820 if(unicode)
821 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
822 else
824 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
825 LPWSTR nameW = NULL;
826 if(nameA)
828 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
829 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
830 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
832 result = EDIT_WM_Create(es, nameW);
833 if(nameW)
834 HeapFree(GetProcessHeap(), 0, nameW);
836 break;
838 case WM_CUT:
839 EDIT_WM_Cut(es);
840 break;
842 case WM_ENABLE:
843 es->bEnableState = (BOOL) wParam;
844 EDIT_UpdateText(es, NULL, TRUE);
845 break;
847 case WM_ERASEBKGND:
848 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
849 break;
851 case WM_GETFONT:
852 result = (LRESULT)es->font;
853 break;
855 case WM_GETTEXT:
856 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
857 break;
859 case WM_GETTEXTLENGTH:
860 if (unicode) result = strlenW(es->text);
861 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
862 NULL, 0, NULL, NULL );
863 break;
865 case WM_HSCROLL:
866 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
867 break;
869 case WM_KEYDOWN:
870 result = EDIT_WM_KeyDown(es, (INT)wParam);
871 break;
873 case WM_KILLFOCUS:
874 result = EDIT_WM_KillFocus(es);
875 break;
877 case WM_LBUTTONDBLCLK:
878 result = EDIT_WM_LButtonDblClk(es);
879 break;
881 case WM_LBUTTONDOWN:
882 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
883 break;
885 case WM_LBUTTONUP:
886 result = EDIT_WM_LButtonUp(es);
887 break;
889 case WM_MBUTTONDOWN:
890 result = EDIT_WM_MButtonDown(es);
891 break;
893 case WM_MOUSEACTIVATE:
894 result = MA_ACTIVATE;
895 break;
897 case WM_MOUSEMOVE:
898 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
899 break;
901 case WM_PAINT:
902 EDIT_WM_Paint(es, wParam);
903 break;
905 case WM_PASTE:
906 EDIT_WM_Paste(es);
907 break;
909 case WM_SETFOCUS:
910 EDIT_WM_SetFocus(es);
911 break;
913 case WM_SETFONT:
914 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
915 break;
917 case WM_SETREDRAW:
918 /* FIXME: actually set an internal flag and behave accordingly */
919 break;
921 case WM_SETTEXT:
922 EDIT_WM_SetText(es, lParam, unicode);
923 result = TRUE;
924 break;
926 case WM_SIZE:
927 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
928 break;
930 case WM_STYLECHANGED:
931 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
932 break;
934 case WM_STYLECHANGING:
935 result = 0; /* See EDIT_WM_StyleChanged */
936 break;
938 case WM_SYSKEYDOWN:
939 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
940 break;
942 case WM_TIMER:
943 EDIT_WM_Timer(es);
944 break;
946 case WM_VSCROLL:
947 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
948 break;
950 case WM_MOUSEWHEEL:
952 int gcWheelDelta = 0;
953 UINT pulScrollLines = 3;
954 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
956 if (wParam & (MK_SHIFT | MK_CONTROL)) {
957 result = DefWindowProcW(hwnd, msg, wParam, lParam);
958 break;
960 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
961 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
963 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
964 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
965 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
968 break;
969 default:
970 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
971 break;
974 if (es) EDIT_UnlockBuffer(es, FALSE);
976 return result;
979 /*********************************************************************
981 * EditWndProcW (USER32.@)
983 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
985 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
988 /*********************************************************************
990 * EditWndProc (USER32.@)
992 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
994 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
997 /*********************************************************************
999 * EDIT_BuildLineDefs_ML
1001 * Build linked list of text lines.
1002 * Lines can end with '\0' (last line), a character (if it is wrapped),
1003 * a soft return '\r\r\n' or a hard return '\r\n'
1006 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1008 HDC dc;
1009 HFONT old_font = 0;
1010 LPWSTR current_position, cp;
1011 INT fw;
1012 LINEDEF *current_line;
1013 LINEDEF *previous_line;
1014 LINEDEF *start_line;
1015 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1016 INT line_count = es->line_count;
1017 INT orig_net_length;
1018 RECT rc;
1020 if (istart == iend && delta == 0)
1021 return;
1023 dc = GetDC(es->hwndSelf);
1024 if (es->font)
1025 old_font = SelectObject(dc, es->font);
1027 previous_line = NULL;
1028 current_line = es->first_line_def;
1030 /* Find starting line. istart must lie inside an existing line or
1031 * at the end of buffer */
1032 do {
1033 if (istart < current_line->index + current_line->length ||
1034 current_line->ending == END_0)
1035 break;
1037 previous_line = current_line;
1038 current_line = current_line->next;
1039 line_index++;
1040 } while (current_line);
1042 if (!current_line) /* Error occurred start is not inside previous buffer */
1044 FIXME(" modification occurred outside buffer\n");
1045 ReleaseDC(es->hwndSelf, dc);
1046 return;
1049 /* Remember start of modifications in order to calculate update region */
1050 nstart_line = line_index;
1051 nstart_index = current_line->index;
1053 /* We must start to reformat from the previous line since the modifications
1054 * may have caused the line to wrap upwards. */
1055 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1057 line_index--;
1058 current_line = previous_line;
1060 start_line = current_line;
1062 fw = es->format_rect.right - es->format_rect.left;
1063 current_position = es->text + current_line->index;
1064 do {
1065 if (current_line != start_line)
1067 if (!current_line || current_line->index + delta > current_position - es->text)
1069 /* The buffer has been expanded, create a new line and
1070 insert it into the link list */
1071 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1072 new_line->next = previous_line->next;
1073 previous_line->next = new_line;
1074 current_line = new_line;
1075 es->line_count++;
1077 else if (current_line->index + delta < current_position - es->text)
1079 /* The previous line merged with this line so we delete this extra entry */
1080 previous_line->next = current_line->next;
1081 HeapFree(GetProcessHeap(), 0, current_line);
1082 current_line = previous_line->next;
1083 es->line_count--;
1084 continue;
1086 else /* current_line->index + delta == current_position */
1088 if (current_position - es->text > iend)
1089 break; /* We reached end of line modifications */
1090 /* else recalulate this line */
1094 current_line->index = current_position - es->text;
1095 orig_net_length = current_line->net_length;
1097 /* Find end of line */
1098 cp = current_position;
1099 while (*cp) {
1100 if (*cp == '\n') break;
1101 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1102 break;
1103 cp++;
1106 /* Mark type of line termination */
1107 if (!(*cp)) {
1108 current_line->ending = END_0;
1109 current_line->net_length = strlenW(current_position);
1110 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1111 current_line->ending = END_SOFT;
1112 current_line->net_length = cp - current_position - 1;
1113 } else if (*cp == '\n') {
1114 current_line->ending = END_RICH;
1115 current_line->net_length = cp - current_position;
1116 } else {
1117 current_line->ending = END_HARD;
1118 current_line->net_length = cp - current_position;
1121 /* Calculate line width */
1122 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1123 current_position, current_line->net_length,
1124 es->tabs_count, es->tabs));
1126 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1127 if (!(es->style & ES_AUTOHSCROLL)) {
1128 if (current_line->width > fw) {
1129 INT next = 0;
1130 INT prev;
1131 do {
1132 prev = next;
1133 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1134 prev + 1, current_line->net_length, WB_RIGHT);
1135 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1136 current_position, next, es->tabs_count, es->tabs));
1137 } while (current_line->width <= fw);
1138 if (!prev) { /* Didn't find a line break so force a break */
1139 next = 0;
1140 do {
1141 prev = next;
1142 next++;
1143 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1144 current_position, next, es->tabs_count, es->tabs));
1145 } while (current_line->width <= fw);
1146 if (!prev)
1147 prev = 1;
1150 /* If the first line we are calculating, wrapped before istart, we must
1151 * adjust istart in order for this to be reflected in the update region. */
1152 if (current_line->index == nstart_index && istart > current_line->index + prev)
1153 istart = current_line->index + prev;
1154 /* else if we are updating the previous line before the first line we
1155 * are re-calculating and it expanded */
1156 else if (current_line == start_line &&
1157 current_line->index != nstart_index && orig_net_length < prev)
1159 /* Line expanded due to an upwards line wrap so we must partially include
1160 * previous line in update region */
1161 nstart_line = line_index;
1162 nstart_index = current_line->index;
1163 istart = current_line->index + orig_net_length;
1166 current_line->net_length = prev;
1167 current_line->ending = END_WRAP;
1168 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1169 current_line->net_length, es->tabs_count, es->tabs));
1171 else if (orig_net_length < current_line->net_length &&
1172 current_line == start_line &&
1173 current_line->index != nstart_index) {
1174 /* The previous line expanded but it's still not as wide as the client rect */
1175 /* The expansion is due to an upwards line wrap so we must partially include
1176 it in the update region */
1177 nstart_line = line_index;
1178 nstart_index = current_line->index;
1179 istart = current_line->index + orig_net_length;
1184 /* Adjust length to include line termination */
1185 switch (current_line->ending) {
1186 case END_SOFT:
1187 current_line->length = current_line->net_length + 3;
1188 break;
1189 case END_RICH:
1190 current_line->length = current_line->net_length + 1;
1191 break;
1192 case END_HARD:
1193 current_line->length = current_line->net_length + 2;
1194 break;
1195 case END_WRAP:
1196 case END_0:
1197 current_line->length = current_line->net_length;
1198 break;
1200 es->text_width = max(es->text_width, current_line->width);
1201 current_position += current_line->length;
1202 previous_line = current_line;
1203 current_line = current_line->next;
1204 line_index++;
1205 } while (previous_line->ending != END_0);
1207 /* Finish adjusting line indexes by delta or remove hanging lines */
1208 if (previous_line->ending == END_0)
1210 LINEDEF *pnext = NULL;
1212 previous_line->next = NULL;
1213 while (current_line)
1215 pnext = current_line->next;
1216 HeapFree(GetProcessHeap(), 0, current_line);
1217 current_line = pnext;
1218 es->line_count--;
1221 else
1223 while (current_line)
1225 current_line->index += delta;
1226 current_line = current_line->next;
1230 /* Calculate rest of modification rectangle */
1231 if (hrgn)
1233 HRGN tmphrgn;
1235 * We calculate two rectangles. One for the first line which may have
1236 * an indent with respect to the format rect. The other is a format-width
1237 * rectangle that spans the rest of the lines that changed or moved.
1239 rc.top = es->format_rect.top + nstart_line * es->line_height -
1240 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1241 rc.bottom = rc.top + es->line_height;
1242 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1243 es->text + nstart_index, istart - nstart_index,
1244 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1245 rc.right = es->format_rect.right;
1246 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1248 rc.top = rc.bottom;
1249 rc.left = es->format_rect.left;
1250 rc.right = es->format_rect.right;
1252 * If lines were added or removed we must re-paint the remainder of the
1253 * lines since the remaining lines were either shifted up or down.
1255 if (line_count < es->line_count) /* We added lines */
1256 rc.bottom = es->line_count * es->line_height;
1257 else if (line_count > es->line_count) /* We removed lines */
1258 rc.bottom = line_count * es->line_height;
1259 else
1260 rc.bottom = line_index * es->line_height;
1261 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1262 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1263 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1264 DeleteObject(tmphrgn);
1267 if (es->font)
1268 SelectObject(dc, old_font);
1270 ReleaseDC(es->hwndSelf, dc);
1273 /*********************************************************************
1275 * EDIT_CalcLineWidth_SL
1278 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1280 es->text_width = (short)LOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1283 /*********************************************************************
1285 * EDIT_CallWordBreakProc
1287 * Call appropriate WordBreakProc (internal or external).
1289 * Note: The "start" argument should always be an index referring
1290 * to es->text. The actual wordbreak proc might be
1291 * 16 bit, so we can't always pass any 32 bit LPSTR.
1292 * Hence we assume that es->text is the buffer that holds
1293 * the string under examination (we can decide this for ourselves).
1296 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1298 INT ret, iWndsLocks;
1300 /* To avoid any deadlocks, all the locks on the window structures
1301 must be suspended before the control is passed to the application */
1302 iWndsLocks = WIN_SuspendWndsLock();
1304 if (es->word_break_proc16) {
1305 HGLOBAL16 hglob16;
1306 SEGPTR segptr;
1307 INT countA;
1308 WORD args[5];
1309 DWORD result;
1311 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1312 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1313 segptr = K32WOWGlobalLock16(hglob16);
1314 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1315 args[4] = SELECTOROF(segptr);
1316 args[3] = OFFSETOF(segptr);
1317 args[2] = index;
1318 args[1] = countA;
1319 args[0] = action;
1320 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1321 ret = LOWORD(result);
1322 GlobalUnlock16(hglob16);
1323 GlobalFree16(hglob16);
1325 else if (es->word_break_proc)
1327 if(es->is_unicode)
1329 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1331 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1332 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1333 ret = wbpW(es->text + start, index, count, action);
1335 else
1337 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1338 INT countA;
1339 CHAR *textA;
1341 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1342 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1343 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1344 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1345 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1346 ret = wbpA(textA, index, countA, action);
1347 HeapFree(GetProcessHeap(), 0, textA);
1350 else
1351 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1353 WIN_RestoreWndsLock(iWndsLocks);
1354 return ret;
1358 /*********************************************************************
1360 * EDIT_CharFromPos
1362 * Beware: This is not the function called on EM_CHARFROMPOS
1363 * The position _can_ be outside the formatting / client
1364 * rectangle
1365 * The return value is only the character index
1368 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1370 INT index;
1371 HDC dc;
1372 HFONT old_font = 0;
1374 if (es->style & ES_MULTILINE) {
1375 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1376 INT line_index = 0;
1377 LINEDEF *line_def = es->first_line_def;
1378 INT low, high;
1379 while ((line > 0) && line_def->next) {
1380 line_index += line_def->length;
1381 line_def = line_def->next;
1382 line--;
1384 x += es->x_offset - es->format_rect.left;
1385 if (x >= line_def->width) {
1386 if (after_wrap)
1387 *after_wrap = (line_def->ending == END_WRAP);
1388 return line_index + line_def->net_length;
1390 if (x <= 0) {
1391 if (after_wrap)
1392 *after_wrap = FALSE;
1393 return line_index;
1395 dc = GetDC(es->hwndSelf);
1396 if (es->font)
1397 old_font = SelectObject(dc, es->font);
1398 low = line_index + 1;
1399 high = line_index + line_def->net_length + 1;
1400 while (low < high - 1)
1402 INT mid = (low + high) / 2;
1403 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1404 else low = mid;
1406 index = low;
1408 if (after_wrap)
1409 *after_wrap = ((index == line_index + line_def->net_length) &&
1410 (line_def->ending == END_WRAP));
1411 } else {
1412 LPWSTR text;
1413 SIZE size;
1414 if (after_wrap)
1415 *after_wrap = FALSE;
1416 x -= es->format_rect.left;
1417 if (!x)
1418 return es->x_offset;
1419 text = EDIT_GetPasswordPointer_SL(es);
1420 dc = GetDC(es->hwndSelf);
1421 if (es->font)
1422 old_font = SelectObject(dc, es->font);
1423 if (x < 0)
1425 INT low = 0;
1426 INT high = es->x_offset;
1427 while (low < high - 1)
1429 INT mid = (low + high) / 2;
1430 GetTextExtentPoint32W( dc, text + mid,
1431 es->x_offset - mid, &size );
1432 if (size.cx > -x) low = mid;
1433 else high = mid;
1435 index = low;
1437 else
1439 INT low = es->x_offset;
1440 INT high = strlenW(es->text) + 1;
1441 while (low < high - 1)
1443 INT mid = (low + high) / 2;
1444 GetTextExtentPoint32W( dc, text + es->x_offset,
1445 mid - es->x_offset, &size );
1446 if (size.cx > x) high = mid;
1447 else low = mid;
1449 index = low;
1451 if (es->style & ES_PASSWORD)
1452 HeapFree(GetProcessHeap(), 0, text);
1454 if (es->font)
1455 SelectObject(dc, old_font);
1456 ReleaseDC(es->hwndSelf, dc);
1457 return index;
1461 /*********************************************************************
1463 * EDIT_ConfinePoint
1465 * adjusts the point to be within the formatting rectangle
1466 * (so CharFromPos returns the nearest _visible_ character)
1469 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1471 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1472 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1476 /*********************************************************************
1478 * EDIT_GetLineRect
1480 * Calculates the bounding rectangle for a line from a starting
1481 * column to an ending column.
1484 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1486 INT line_index = EDIT_EM_LineIndex(es, line);
1488 if (es->style & ES_MULTILINE)
1489 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1490 else
1491 rc->top = es->format_rect.top;
1492 rc->bottom = rc->top + es->line_height;
1493 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1494 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1498 /*********************************************************************
1500 * EDIT_GetPasswordPointer_SL
1502 * note: caller should free the (optionally) allocated buffer
1505 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1507 if (es->style & ES_PASSWORD) {
1508 INT len = strlenW(es->text);
1509 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1510 text[len] = '\0';
1511 while(len) text[--len] = es->password_char;
1512 return text;
1513 } else
1514 return es->text;
1518 /*********************************************************************
1520 * EDIT_LockBuffer
1522 * This acts as a LOCAL_Lock(), but it locks only once. This way
1523 * you can call it whenever you like, without unlocking.
1525 * Initially the edit control allocates a HLOCAL32 buffer
1526 * (32 bit linear memory handler). However, 16 bit application
1527 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1528 * handler). From that moment on we have to keep using this 16 bit memory
1529 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1530 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1531 * conversion.
1534 static void EDIT_LockBuffer(EDITSTATE *es)
1536 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
1537 if (!es->text) {
1538 CHAR *textA = NULL;
1539 UINT countA = 0;
1540 BOOL _16bit = FALSE;
1542 if(es->hloc32W)
1544 if(es->hloc32A)
1546 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1547 textA = LocalLock(es->hloc32A);
1548 countA = strlen(textA) + 1;
1550 else if(es->hloc16)
1552 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1553 textA = LOCAL_Lock(hInstance, es->hloc16);
1554 countA = strlen(textA) + 1;
1555 _16bit = TRUE;
1558 else {
1559 ERR("no buffer ... please report\n");
1560 return;
1563 if(textA)
1565 HLOCAL hloc32W_new;
1566 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1567 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1568 if(countW_new > es->buffer_size + 1)
1570 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1571 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1572 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1573 if(hloc32W_new)
1575 es->hloc32W = hloc32W_new;
1576 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1577 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1579 else
1580 WARN("FAILED! Will synchronize partially\n");
1584 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1585 es->text = LocalLock(es->hloc32W);
1587 if(textA)
1589 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1590 if(_16bit)
1591 LOCAL_Unlock(hInstance, es->hloc16);
1592 else
1593 LocalUnlock(es->hloc32A);
1596 es->lock_count++;
1600 /*********************************************************************
1602 * EDIT_SL_InvalidateText
1604 * Called from EDIT_InvalidateText().
1605 * Does the job for single-line controls only.
1608 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1610 RECT line_rect;
1611 RECT rc;
1613 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1614 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1615 EDIT_UpdateText(es, &rc, TRUE);
1619 /*********************************************************************
1621 * EDIT_ML_InvalidateText
1623 * Called from EDIT_InvalidateText().
1624 * Does the job for multi-line controls only.
1627 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1629 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1630 INT sl = EDIT_EM_LineFromChar(es, start);
1631 INT el = EDIT_EM_LineFromChar(es, end);
1632 INT sc;
1633 INT ec;
1634 RECT rc1;
1635 RECT rcWnd;
1636 RECT rcLine;
1637 RECT rcUpdate;
1638 INT l;
1640 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1641 return;
1643 sc = start - EDIT_EM_LineIndex(es, sl);
1644 ec = end - EDIT_EM_LineIndex(es, el);
1645 if (sl < es->y_offset) {
1646 sl = es->y_offset;
1647 sc = 0;
1649 if (el > es->y_offset + vlc) {
1650 el = es->y_offset + vlc;
1651 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1653 GetClientRect(es->hwndSelf, &rc1);
1654 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1655 if (sl == el) {
1656 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1657 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1658 EDIT_UpdateText(es, &rcUpdate, TRUE);
1659 } else {
1660 EDIT_GetLineRect(es, sl, sc,
1661 EDIT_EM_LineLength(es,
1662 EDIT_EM_LineIndex(es, sl)),
1663 &rcLine);
1664 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1665 EDIT_UpdateText(es, &rcUpdate, TRUE);
1666 for (l = sl + 1 ; l < el ; l++) {
1667 EDIT_GetLineRect(es, l, 0,
1668 EDIT_EM_LineLength(es,
1669 EDIT_EM_LineIndex(es, l)),
1670 &rcLine);
1671 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1672 EDIT_UpdateText(es, &rcUpdate, TRUE);
1674 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1675 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1676 EDIT_UpdateText(es, &rcUpdate, TRUE);
1681 /*********************************************************************
1683 * EDIT_InvalidateText
1685 * Invalidate the text from offset start upto, but not including,
1686 * offset end. Useful for (re)painting the selection.
1687 * Regions outside the linewidth are not invalidated.
1688 * end == -1 means end == TextLength.
1689 * start and end need not be ordered.
1692 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1694 if (end == start)
1695 return;
1697 if (end == -1)
1698 end = strlenW(es->text);
1700 if (end < start) {
1701 INT tmp = start;
1702 start = end;
1703 end = tmp;
1706 if (es->style & ES_MULTILINE)
1707 EDIT_ML_InvalidateText(es, start, end);
1708 else
1709 EDIT_SL_InvalidateText(es, start, end);
1713 /*********************************************************************
1715 * EDIT_MakeFit
1717 * Try to fit size + 1 characters in the buffer.
1719 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1721 HLOCAL hNew32W;
1723 if (size <= es->buffer_size)
1724 return TRUE;
1726 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1728 /* Force edit to unlock it's buffer. es->text now NULL */
1729 EDIT_UnlockBuffer(es, TRUE);
1731 if (es->hloc32W) {
1732 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1733 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1734 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1735 es->hloc32W = hNew32W;
1736 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1740 EDIT_LockBuffer(es);
1742 if (es->buffer_size < size) {
1743 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1744 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1745 return FALSE;
1746 } else {
1747 TRACE("We now have %d+1\n", es->buffer_size);
1748 return TRUE;
1753 /*********************************************************************
1755 * EDIT_MakeUndoFit
1757 * Try to fit size + 1 bytes in the undo buffer.
1760 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1762 UINT alloc_size;
1764 if (size <= es->undo_buffer_size)
1765 return TRUE;
1767 TRACE("trying to ReAlloc to %d+1\n", size);
1769 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1770 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1771 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1772 return TRUE;
1774 else
1776 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1777 return FALSE;
1782 /*********************************************************************
1784 * EDIT_MoveBackward
1787 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1789 INT e = es->selection_end;
1791 if (e) {
1792 e--;
1793 if ((es->style & ES_MULTILINE) && e &&
1794 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1795 e--;
1796 if (e && (es->text[e - 1] == '\r'))
1797 e--;
1800 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1801 EDIT_EM_ScrollCaret(es);
1805 /*********************************************************************
1807 * EDIT_MoveDown_ML
1809 * Only for multi line controls
1810 * Move the caret one line down, on a column with the nearest
1811 * x coordinate on the screen (might be a different column).
1814 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1816 INT s = es->selection_start;
1817 INT e = es->selection_end;
1818 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1819 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1820 INT x = (short)LOWORD(pos);
1821 INT y = (short)HIWORD(pos);
1823 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1824 if (!extend)
1825 s = e;
1826 EDIT_EM_SetSel(es, s, e, after_wrap);
1827 EDIT_EM_ScrollCaret(es);
1831 /*********************************************************************
1833 * EDIT_MoveEnd
1836 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1838 BOOL after_wrap = FALSE;
1839 INT e;
1841 /* Pass a high value in x to make sure of receiving the end of the line */
1842 if (es->style & ES_MULTILINE)
1843 e = EDIT_CharFromPos(es, 0x3fffffff,
1844 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1845 else
1846 e = strlenW(es->text);
1847 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1848 EDIT_EM_ScrollCaret(es);
1852 /*********************************************************************
1854 * EDIT_MoveForward
1857 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1859 INT e = es->selection_end;
1861 if (es->text[e]) {
1862 e++;
1863 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1864 if (es->text[e] == '\n')
1865 e++;
1866 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1867 e += 2;
1870 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1871 EDIT_EM_ScrollCaret(es);
1875 /*********************************************************************
1877 * EDIT_MoveHome
1879 * Home key: move to beginning of line.
1882 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1884 INT e;
1886 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1887 if (es->style & ES_MULTILINE)
1888 e = EDIT_CharFromPos(es, -es->x_offset,
1889 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1890 else
1891 e = 0;
1892 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1893 EDIT_EM_ScrollCaret(es);
1897 /*********************************************************************
1899 * EDIT_MovePageDown_ML
1901 * Only for multi line controls
1902 * Move the caret one page down, on a column with the nearest
1903 * x coordinate on the screen (might be a different column).
1906 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1908 INT s = es->selection_start;
1909 INT e = es->selection_end;
1910 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1911 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1912 INT x = (short)LOWORD(pos);
1913 INT y = (short)HIWORD(pos);
1915 e = EDIT_CharFromPos(es, x,
1916 y + (es->format_rect.bottom - es->format_rect.top),
1917 &after_wrap);
1918 if (!extend)
1919 s = e;
1920 EDIT_EM_SetSel(es, s, e, after_wrap);
1921 EDIT_EM_ScrollCaret(es);
1925 /*********************************************************************
1927 * EDIT_MovePageUp_ML
1929 * Only for multi line controls
1930 * Move the caret one page up, on a column with the nearest
1931 * x coordinate on the screen (might be a different column).
1934 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1936 INT s = es->selection_start;
1937 INT e = es->selection_end;
1938 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1939 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1940 INT x = (short)LOWORD(pos);
1941 INT y = (short)HIWORD(pos);
1943 e = EDIT_CharFromPos(es, x,
1944 y - (es->format_rect.bottom - es->format_rect.top),
1945 &after_wrap);
1946 if (!extend)
1947 s = e;
1948 EDIT_EM_SetSel(es, s, e, after_wrap);
1949 EDIT_EM_ScrollCaret(es);
1953 /*********************************************************************
1955 * EDIT_MoveUp_ML
1957 * Only for multi line controls
1958 * Move the caret one line up, on a column with the nearest
1959 * x coordinate on the screen (might be a different column).
1962 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1964 INT s = es->selection_start;
1965 INT e = es->selection_end;
1966 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1967 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1968 INT x = (short)LOWORD(pos);
1969 INT y = (short)HIWORD(pos);
1971 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1972 if (!extend)
1973 s = e;
1974 EDIT_EM_SetSel(es, s, e, after_wrap);
1975 EDIT_EM_ScrollCaret(es);
1979 /*********************************************************************
1981 * EDIT_MoveWordBackward
1984 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1986 INT s = es->selection_start;
1987 INT e = es->selection_end;
1988 INT l;
1989 INT ll;
1990 INT li;
1992 l = EDIT_EM_LineFromChar(es, e);
1993 ll = EDIT_EM_LineLength(es, e);
1994 li = EDIT_EM_LineIndex(es, l);
1995 if (e - li == 0) {
1996 if (l) {
1997 li = EDIT_EM_LineIndex(es, l - 1);
1998 e = li + EDIT_EM_LineLength(es, li);
2000 } else {
2001 e = li + (INT)EDIT_CallWordBreakProc(es,
2002 li, e - li, ll, WB_LEFT);
2004 if (!extend)
2005 s = e;
2006 EDIT_EM_SetSel(es, s, e, FALSE);
2007 EDIT_EM_ScrollCaret(es);
2011 /*********************************************************************
2013 * EDIT_MoveWordForward
2016 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2018 INT s = es->selection_start;
2019 INT e = es->selection_end;
2020 INT l;
2021 INT ll;
2022 INT li;
2024 l = EDIT_EM_LineFromChar(es, e);
2025 ll = EDIT_EM_LineLength(es, e);
2026 li = EDIT_EM_LineIndex(es, l);
2027 if (e - li == ll) {
2028 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2029 e = EDIT_EM_LineIndex(es, l + 1);
2030 } else {
2031 e = li + EDIT_CallWordBreakProc(es,
2032 li, e - li + 1, ll, WB_RIGHT);
2034 if (!extend)
2035 s = e;
2036 EDIT_EM_SetSel(es, s, e, FALSE);
2037 EDIT_EM_ScrollCaret(es);
2041 /*********************************************************************
2043 * EDIT_PaintLine
2046 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2048 INT s = es->selection_start;
2049 INT e = es->selection_end;
2050 INT li;
2051 INT ll;
2052 INT x;
2053 INT y;
2054 LRESULT pos;
2056 if (es->style & ES_MULTILINE) {
2057 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2058 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2059 return;
2060 } else if (line)
2061 return;
2063 TRACE("line=%d\n", line);
2065 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2066 x = (short)LOWORD(pos);
2067 y = (short)HIWORD(pos);
2068 li = EDIT_EM_LineIndex(es, line);
2069 ll = EDIT_EM_LineLength(es, li);
2070 s = min(es->selection_start, es->selection_end);
2071 e = max(es->selection_start, es->selection_end);
2072 s = min(li + ll, max(li, s));
2073 e = min(li + ll, max(li, e));
2074 if (rev && (s != e) &&
2075 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2076 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2077 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2078 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2079 } else
2080 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2084 /*********************************************************************
2086 * EDIT_PaintText
2089 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2091 COLORREF BkColor;
2092 COLORREF TextColor;
2093 INT ret;
2094 INT li;
2095 INT BkMode;
2096 SIZE size;
2098 if (!count)
2099 return 0;
2100 BkMode = GetBkMode(dc);
2101 BkColor = GetBkColor(dc);
2102 TextColor = GetTextColor(dc);
2103 if (rev) {
2104 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2105 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2106 SetBkMode( dc, OPAQUE);
2108 li = EDIT_EM_LineIndex(es, line);
2109 if (es->style & ES_MULTILINE) {
2110 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2111 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2112 } else {
2113 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2114 TextOutW(dc, x, y, text + li + col, count);
2115 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2116 ret = size.cx;
2117 if (es->style & ES_PASSWORD)
2118 HeapFree(GetProcessHeap(), 0, text);
2120 if (rev) {
2121 SetBkColor(dc, BkColor);
2122 SetTextColor(dc, TextColor);
2123 SetBkMode( dc, BkMode);
2125 return ret;
2129 /*********************************************************************
2131 * EDIT_SetCaretPos
2134 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2135 BOOL after_wrap)
2137 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2138 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2139 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2143 /*********************************************************************
2145 * EDIT_SetRectNP
2147 * note: this is not (exactly) the handler called on EM_SETRECTNP
2148 * it is also used to set the rect of a single line control
2151 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2153 CopyRect(&es->format_rect, rc);
2154 if (es->style & WS_BORDER) {
2155 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2156 es->format_rect.left += bw;
2157 es->format_rect.top += bw;
2158 es->format_rect.right -= bw;
2159 es->format_rect.bottom -= bw;
2161 es->format_rect.left += es->left_margin;
2162 es->format_rect.right -= es->right_margin;
2163 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2164 if (es->style & ES_MULTILINE)
2166 INT fw, vlc, max_x_offset, max_y_offset;
2168 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2169 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2171 /* correct es->x_offset */
2172 fw = es->format_rect.right - es->format_rect.left;
2173 max_x_offset = es->text_width - fw;
2174 if(max_x_offset < 0) max_x_offset = 0;
2175 if(es->x_offset > max_x_offset)
2176 es->x_offset = max_x_offset;
2178 /* correct es->y_offset */
2179 max_y_offset = es->line_count - vlc;
2180 if(max_y_offset < 0) max_y_offset = 0;
2181 if(es->y_offset > max_y_offset)
2182 es->y_offset = max_y_offset;
2184 /* force scroll info update */
2185 EDIT_UpdateScrollInfo(es);
2187 else
2188 /* Windows doesn't care to fix text placement for SL controls */
2189 es->format_rect.bottom = es->format_rect.top + es->line_height;
2191 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2192 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2196 /*********************************************************************
2198 * EDIT_UnlockBuffer
2201 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2203 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2205 /* Edit window might be already destroyed */
2206 if(!IsWindow(es->hwndSelf))
2208 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2209 return;
2212 if (!es->lock_count) {
2213 ERR("lock_count == 0 ... please report\n");
2214 return;
2216 if (!es->text) {
2217 ERR("es->text == 0 ... please report\n");
2218 return;
2221 if (force || (es->lock_count == 1)) {
2222 if (es->hloc32W) {
2223 CHAR *textA = NULL;
2224 BOOL _16bit = FALSE;
2225 UINT countA = 0;
2226 UINT countW = strlenW(es->text) + 1;
2228 if(es->hloc32A)
2230 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2231 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2232 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2233 countA = LocalSize(es->hloc32A);
2234 if(countA_new > countA)
2236 HLOCAL hloc32A_new;
2237 UINT alloc_size = ROUND_TO_GROW(countA_new);
2238 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2239 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2240 if(hloc32A_new)
2242 es->hloc32A = hloc32A_new;
2243 countA = LocalSize(hloc32A_new);
2244 TRACE("Real new size %d bytes\n", countA);
2246 else
2247 WARN("FAILED! Will synchronize partially\n");
2249 textA = LocalLock(es->hloc32A);
2251 else if(es->hloc16)
2253 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2254 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2255 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2256 countA = LOCAL_Size(hInstance, es->hloc16);
2257 if(countA_new > countA)
2259 HLOCAL16 hloc16_new;
2260 UINT alloc_size = ROUND_TO_GROW(countA_new);
2261 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2262 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2263 if(hloc16_new)
2265 es->hloc16 = hloc16_new;
2266 countA = LOCAL_Size(hInstance, hloc16_new);
2267 TRACE("Real new size %d bytes\n", countA);
2269 else
2270 WARN("FAILED! Will synchronize partially\n");
2272 textA = LOCAL_Lock(hInstance, es->hloc16);
2273 _16bit = TRUE;
2276 if(textA)
2278 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2279 if(_16bit)
2280 LOCAL_Unlock(hInstance, es->hloc16);
2281 else
2282 LocalUnlock(es->hloc32A);
2285 LocalUnlock(es->hloc32W);
2286 es->text = NULL;
2288 else {
2289 ERR("no buffer ... please report\n");
2290 return;
2293 es->lock_count--;
2297 /*********************************************************************
2299 * EDIT_UpdateScrollInfo
2302 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2304 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2306 SCROLLINFO si;
2307 si.cbSize = sizeof(SCROLLINFO);
2308 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2309 si.nMin = 0;
2310 si.nMax = es->line_count - 1;
2311 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2312 si.nPos = es->y_offset;
2313 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2314 si.nMin, si.nMax, si.nPage, si.nPos);
2315 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2318 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2320 SCROLLINFO si;
2321 si.cbSize = sizeof(SCROLLINFO);
2322 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2323 si.nMin = 0;
2324 si.nMax = es->text_width - 1;
2325 si.nPage = es->format_rect.right - es->format_rect.left;
2326 si.nPos = es->x_offset;
2327 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2328 si.nMin, si.nMax, si.nPage, si.nPos);
2329 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2333 /*********************************************************************
2335 * EDIT_WordBreakProc
2337 * Find the beginning of words.
2338 * Note: unlike the specs for a WordBreakProc, this function only
2339 * allows to be called without linebreaks between s[0] upto
2340 * s[count - 1]. Remember it is only called
2341 * internally, so we can decide this for ourselves.
2344 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2346 INT ret = 0;
2348 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2350 if(!s) return 0;
2352 switch (action) {
2353 case WB_LEFT:
2354 if (!count)
2355 break;
2356 if (index)
2357 index--;
2358 if (s[index] == ' ') {
2359 while (index && (s[index] == ' '))
2360 index--;
2361 if (index) {
2362 while (index && (s[index] != ' '))
2363 index--;
2364 if (s[index] == ' ')
2365 index++;
2367 } else {
2368 while (index && (s[index] != ' '))
2369 index--;
2370 if (s[index] == ' ')
2371 index++;
2373 ret = index;
2374 break;
2375 case WB_RIGHT:
2376 if (!count)
2377 break;
2378 if (index)
2379 index--;
2380 if (s[index] == ' ')
2381 while ((index < count) && (s[index] == ' ')) index++;
2382 else {
2383 while (s[index] && (s[index] != ' ') && (index < count))
2384 index++;
2385 while ((s[index] == ' ') && (index < count)) index++;
2387 ret = index;
2388 break;
2389 case WB_ISDELIMITER:
2390 ret = (s[index] == ' ');
2391 break;
2392 default:
2393 ERR("unknown action code, please report !\n");
2394 break;
2396 return ret;
2400 /*********************************************************************
2402 * EM_CHARFROMPOS
2404 * returns line number (not index) in high-order word of result.
2405 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2406 * to Richedit, not to the edit control. Original documentation is valid.
2407 * FIXME: do the specs mean to return -1 if outside client area or
2408 * if outside formatting rectangle ???
2411 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2413 POINT pt;
2414 RECT rc;
2415 INT index;
2417 pt.x = x;
2418 pt.y = y;
2419 GetClientRect(es->hwndSelf, &rc);
2420 if (!PtInRect(&rc, pt))
2421 return -1;
2423 index = EDIT_CharFromPos(es, x, y, NULL);
2424 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2428 /*********************************************************************
2430 * EM_FMTLINES
2432 * Enable or disable soft breaks.
2434 * This means: insert or remove the soft linebreak character (\r\r\n).
2435 * Take care to check if the text still fits the buffer after insertion.
2436 * If not, notify with EN_ERRSPACE.
2439 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2441 es->flags &= ~EF_USE_SOFTBRK;
2442 if (add_eol) {
2443 es->flags |= EF_USE_SOFTBRK;
2444 FIXME("soft break enabled, not implemented\n");
2446 return add_eol;
2450 /*********************************************************************
2452 * EM_GETHANDLE
2454 * Hopefully this won't fire back at us.
2455 * We always start with a fixed buffer in the local heap.
2456 * Despite of the documentation says that the local heap is used
2457 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2458 * buffer on the local heap.
2461 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2463 HLOCAL hLocal;
2465 if (!(es->style & ES_MULTILINE))
2466 return 0;
2468 if(es->is_unicode)
2469 hLocal = es->hloc32W;
2470 else
2472 if(!es->hloc32A)
2474 CHAR *textA;
2475 UINT countA, alloc_size;
2476 TRACE("Allocating 32-bit ANSI alias buffer\n");
2477 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2478 alloc_size = ROUND_TO_GROW(countA);
2479 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2481 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2482 return 0;
2484 textA = LocalLock(es->hloc32A);
2485 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2486 LocalUnlock(es->hloc32A);
2488 hLocal = es->hloc32A;
2491 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2492 return hLocal;
2496 /*********************************************************************
2498 * EM_GETHANDLE16
2500 * Hopefully this won't fire back at us.
2501 * We always start with a buffer in 32 bit linear memory.
2502 * However, with this message a 16 bit application requests
2503 * a handle of 16 bit local heap memory, where it expects to find
2504 * the text.
2505 * It's a pitty that from this moment on we have to use this
2506 * local heap, because applications may rely on the handle
2507 * in the future.
2509 * In this function we'll try to switch to local heap.
2511 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2513 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2514 CHAR *textA;
2515 UINT countA, alloc_size;
2517 if (!(es->style & ES_MULTILINE))
2518 return 0;
2520 if (es->hloc16)
2521 return es->hloc16;
2523 if (!LOCAL_HeapSize(hInstance)) {
2524 if (!LocalInit16(hInstance, 0,
2525 GlobalSize16(hInstance))) {
2526 ERR("could not initialize local heap\n");
2527 return 0;
2529 TRACE("local heap initialized\n");
2532 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2533 alloc_size = ROUND_TO_GROW(countA);
2535 TRACE("Allocating 16-bit ANSI alias buffer\n");
2536 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2537 ERR("could not allocate new 16 bit buffer\n");
2538 return 0;
2541 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2542 ERR("could not lock new 16 bit buffer\n");
2543 LOCAL_Free(hInstance, es->hloc16);
2544 es->hloc16 = 0;
2545 return 0;
2548 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2549 LOCAL_Unlock(hInstance, es->hloc16);
2551 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2552 return es->hloc16;
2556 /*********************************************************************
2558 * EM_GETLINE
2561 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2563 LPWSTR src;
2564 INT line_len, dst_len;
2565 INT i;
2567 if (es->style & ES_MULTILINE) {
2568 if (line >= es->line_count)
2569 return 0;
2570 } else
2571 line = 0;
2572 i = EDIT_EM_LineIndex(es, line);
2573 src = es->text + i;
2574 line_len = EDIT_EM_LineLength(es, i);
2575 dst_len = *(WORD *)lParam;
2576 if(unicode)
2578 LPWSTR dst = (LPWSTR)lParam;
2579 if(dst_len <= line_len)
2581 memcpy(dst, src, dst_len * sizeof(WCHAR));
2582 return dst_len;
2584 else /* Append 0 if enough space */
2586 memcpy(dst, src, line_len * sizeof(WCHAR));
2587 dst[line_len] = 0;
2588 return line_len;
2591 else
2593 LPSTR dst = (LPSTR)lParam;
2594 INT ret;
2595 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2596 if(!ret) /* Insufficient buffer size */
2597 return dst_len;
2598 if(ret < dst_len) /* Append 0 if enough space */
2599 dst[ret] = 0;
2600 return ret;
2605 /*********************************************************************
2607 * EM_GETSEL
2610 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2612 UINT s = es->selection_start;
2613 UINT e = es->selection_end;
2615 ORDER_UINT(s, e);
2616 if (start)
2617 *start = s;
2618 if (end)
2619 *end = e;
2620 return MAKELONG(s, e);
2624 /*********************************************************************
2626 * EM_GETTHUMB
2628 * FIXME: is this right ? (or should it be only VSCROLL)
2629 * (and maybe only for edit controls that really have their
2630 * own scrollbars) (and maybe only for multiline controls ?)
2631 * All in all: very poorly documented
2634 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2636 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2637 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2641 /*********************************************************************
2643 * EM_LINEFROMCHAR
2646 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2648 INT line;
2649 LINEDEF *line_def;
2651 if (!(es->style & ES_MULTILINE))
2652 return 0;
2653 if (index > (INT)strlenW(es->text))
2654 return es->line_count - 1;
2655 if (index == -1)
2656 index = min(es->selection_start, es->selection_end);
2658 line = 0;
2659 line_def = es->first_line_def;
2660 index -= line_def->length;
2661 while ((index >= 0) && line_def->next) {
2662 line++;
2663 line_def = line_def->next;
2664 index -= line_def->length;
2666 return line;
2670 /*********************************************************************
2672 * EM_LINEINDEX
2675 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2677 INT line_index;
2678 LINEDEF *line_def;
2680 if (!(es->style & ES_MULTILINE))
2681 return 0;
2682 if (line >= es->line_count)
2683 return -1;
2685 line_index = 0;
2686 line_def = es->first_line_def;
2687 if (line == -1) {
2688 INT index = es->selection_end - line_def->length;
2689 while ((index >= 0) && line_def->next) {
2690 line_index += line_def->length;
2691 line_def = line_def->next;
2692 index -= line_def->length;
2694 } else {
2695 while (line > 0) {
2696 line_index += line_def->length;
2697 line_def = line_def->next;
2698 line--;
2701 return line_index;
2705 /*********************************************************************
2707 * EM_LINELENGTH
2710 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2712 LINEDEF *line_def;
2714 if (!(es->style & ES_MULTILINE))
2715 return strlenW(es->text);
2717 if (index == -1) {
2718 /* get the number of remaining non-selected chars of selected lines */
2719 INT32 l; /* line number */
2720 INT32 li; /* index of first char in line */
2721 INT32 count;
2722 l = EDIT_EM_LineFromChar(es, es->selection_start);
2723 /* # chars before start of selection area */
2724 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2725 l = EDIT_EM_LineFromChar(es, es->selection_end);
2726 /* # chars after end of selection */
2727 li = EDIT_EM_LineIndex(es, l);
2728 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2729 return count;
2731 line_def = es->first_line_def;
2732 index -= line_def->length;
2733 while ((index >= 0) && line_def->next) {
2734 line_def = line_def->next;
2735 index -= line_def->length;
2737 return line_def->net_length;
2741 /*********************************************************************
2743 * EM_LINESCROLL
2745 * NOTE: dx is in average character widths, dy - in lines;
2748 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2750 if (!(es->style & ES_MULTILINE))
2751 return FALSE;
2753 dx *= es->char_width;
2754 return EDIT_EM_LineScroll_internal(es, dx, dy);
2757 /*********************************************************************
2759 * EDIT_EM_LineScroll_internal
2761 * Version of EDIT_EM_LineScroll for internal use.
2762 * It doesn't refuse if ES_MULTILINE is set and assumes that
2763 * dx is in pixels, dy - in lines.
2766 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2768 INT nyoff;
2769 INT x_offset_in_pixels;
2771 if (es->style & ES_MULTILINE)
2773 x_offset_in_pixels = es->x_offset;
2775 else
2777 dy = 0;
2778 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2781 if (-dx > x_offset_in_pixels)
2782 dx = -x_offset_in_pixels;
2783 if (dx > es->text_width - x_offset_in_pixels)
2784 dx = es->text_width - x_offset_in_pixels;
2785 nyoff = max(0, es->y_offset + dy);
2786 if (nyoff >= es->line_count)
2787 nyoff = es->line_count - 1;
2788 dy = (es->y_offset - nyoff) * es->line_height;
2789 if (dx || dy) {
2790 RECT rc1;
2791 RECT rc;
2793 es->y_offset = nyoff;
2794 if(es->style & ES_MULTILINE)
2795 es->x_offset += dx;
2796 else
2797 es->x_offset += dx / es->char_width;
2799 GetClientRect(es->hwndSelf, &rc1);
2800 IntersectRect(&rc, &rc1, &es->format_rect);
2801 ScrollWindowEx(es->hwndSelf, -dx, dy,
2802 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2803 /* force scroll info update */
2804 EDIT_UpdateScrollInfo(es);
2806 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2807 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2808 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2809 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2810 return TRUE;
2814 /*********************************************************************
2816 * EM_POSFROMCHAR
2819 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2821 INT len = strlenW(es->text);
2822 INT l;
2823 INT li;
2824 INT x;
2825 INT y = 0;
2826 HDC dc;
2827 HFONT old_font = 0;
2828 SIZE size;
2830 index = min(index, len);
2831 dc = GetDC(es->hwndSelf);
2832 if (es->font)
2833 old_font = SelectObject(dc, es->font);
2834 if (es->style & ES_MULTILINE) {
2835 l = EDIT_EM_LineFromChar(es, index);
2836 y = (l - es->y_offset) * es->line_height;
2837 li = EDIT_EM_LineIndex(es, l);
2838 if (after_wrap && (li == index) && l) {
2839 INT l2 = l - 1;
2840 LINEDEF *line_def = es->first_line_def;
2841 while (l2) {
2842 line_def = line_def->next;
2843 l2--;
2845 if (line_def->ending == END_WRAP) {
2846 l--;
2847 y -= es->line_height;
2848 li = EDIT_EM_LineIndex(es, l);
2851 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2852 es->tabs_count, es->tabs)) - es->x_offset;
2853 } else {
2854 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2855 if (index < es->x_offset) {
2856 GetTextExtentPoint32W(dc, text + index,
2857 es->x_offset - index, &size);
2858 x = -size.cx;
2859 } else {
2860 GetTextExtentPoint32W(dc, text + es->x_offset,
2861 index - es->x_offset, &size);
2862 x = size.cx;
2864 y = 0;
2865 if (es->style & ES_PASSWORD)
2866 HeapFree(GetProcessHeap(), 0, text);
2868 x += es->format_rect.left;
2869 y += es->format_rect.top;
2870 if (es->font)
2871 SelectObject(dc, old_font);
2872 ReleaseDC(es->hwndSelf, dc);
2873 return MAKELONG((INT16)x, (INT16)y);
2877 /*********************************************************************
2879 * EM_REPLACESEL
2881 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2884 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2886 UINT strl = strlenW(lpsz_replace);
2887 UINT tl = strlenW(es->text);
2888 UINT utl;
2889 UINT s;
2890 UINT e;
2891 UINT i;
2892 UINT size;
2893 LPWSTR p;
2894 HRGN hrgn = 0;
2896 TRACE("%s, can_undo %d, send_update %d\n",
2897 debugstr_w(lpsz_replace), can_undo, send_update);
2899 s = es->selection_start;
2900 e = es->selection_end;
2902 if ((s == e) && !strl)
2903 return;
2905 ORDER_UINT(s, e);
2907 /* Issue the EN_MAXTEXT notification and continue with replacing text
2908 * such that buffer limit is honored. */
2909 size = tl - (e - s) + strl;
2910 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
2911 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
2912 strl = es->buffer_limit - (tl - (e-s));
2915 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2916 return;
2918 if (e != s) {
2919 /* there is something to be deleted */
2920 TRACE("deleting stuff.\n");
2921 if (can_undo) {
2922 utl = strlenW(es->undo_text);
2923 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2924 /* undo-buffer is extended to the right */
2925 EDIT_MakeUndoFit(es, utl + e - s);
2926 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2927 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2928 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2929 /* undo-buffer is extended to the left */
2930 EDIT_MakeUndoFit(es, utl + e - s);
2931 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2932 p[e - s] = p[0];
2933 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2934 p[i] = (es->text + s)[i];
2935 es->undo_position = s;
2936 } else {
2937 /* new undo-buffer */
2938 EDIT_MakeUndoFit(es, e - s);
2939 strncpyW(es->undo_text, es->text + s, e - s + 1);
2940 es->undo_text[e - s] = 0; /* ensure 0 termination */
2941 es->undo_position = s;
2943 /* any deletion makes the old insertion-undo invalid */
2944 es->undo_insert_count = 0;
2945 } else
2946 EDIT_EM_EmptyUndoBuffer(es);
2948 /* now delete */
2949 strcpyW(es->text + s, es->text + e);
2951 if (strl) {
2952 /* there is an insertion */
2953 if (can_undo) {
2954 if ((s == es->undo_position) ||
2955 ((es->undo_insert_count) &&
2956 (s == es->undo_position + es->undo_insert_count)))
2958 * insertion is new and at delete position or
2959 * an extension to either left or right
2961 es->undo_insert_count += strl;
2962 else {
2963 /* new insertion undo */
2964 es->undo_position = s;
2965 es->undo_insert_count = strl;
2966 /* new insertion makes old delete-buffer invalid */
2967 *es->undo_text = '\0';
2969 } else
2970 EDIT_EM_EmptyUndoBuffer(es);
2972 /* now insert */
2973 tl = strlenW(es->text);
2974 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));
2975 for (p = es->text + tl ; p >= es->text + s ; p--)
2976 p[strl] = p[0];
2977 for (i = 0 , p = es->text + s ; i < strl ; i++)
2978 p[i] = lpsz_replace[i];
2979 if(es->style & ES_UPPERCASE)
2980 CharUpperBuffW(p, strl);
2981 else if(es->style & ES_LOWERCASE)
2982 CharLowerBuffW(p, strl);
2983 s += strl;
2985 if (es->style & ES_MULTILINE)
2987 INT s = min(es->selection_start, es->selection_end);
2989 hrgn = CreateRectRgn(0, 0, 0, 0);
2990 EDIT_BuildLineDefs_ML(es, s, s + strl,
2991 strl - abs(es->selection_end - es->selection_start), hrgn);
2993 else
2994 EDIT_CalcLineWidth_SL(es);
2996 EDIT_EM_SetSel(es, s, s, FALSE);
2997 es->flags |= EF_MODIFIED;
2998 if (send_update) es->flags |= EF_UPDATE;
2999 if (hrgn)
3001 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3002 DeleteObject(hrgn);
3004 else
3005 EDIT_UpdateText(es, NULL, TRUE);
3007 EDIT_EM_ScrollCaret(es);
3009 /* force scroll info update */
3010 EDIT_UpdateScrollInfo(es);
3013 if(es->flags & EF_UPDATE)
3015 es->flags &= ~EF_UPDATE;
3016 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3021 /*********************************************************************
3023 * EM_SCROLL
3026 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3028 INT dy;
3030 if (!(es->style & ES_MULTILINE))
3031 return (LRESULT)FALSE;
3033 dy = 0;
3035 switch (action) {
3036 case SB_LINEUP:
3037 if (es->y_offset)
3038 dy = -1;
3039 break;
3040 case SB_LINEDOWN:
3041 if (es->y_offset < es->line_count - 1)
3042 dy = 1;
3043 break;
3044 case SB_PAGEUP:
3045 if (es->y_offset)
3046 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3047 break;
3048 case SB_PAGEDOWN:
3049 if (es->y_offset < es->line_count - 1)
3050 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3051 break;
3052 default:
3053 return (LRESULT)FALSE;
3055 if (dy) {
3056 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3057 /* check if we are going to move too far */
3058 if(es->y_offset + dy > es->line_count - vlc)
3059 dy = es->line_count - vlc - es->y_offset;
3061 /* Notification is done in EDIT_EM_LineScroll */
3062 if(dy)
3063 EDIT_EM_LineScroll(es, 0, dy);
3065 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3069 /*********************************************************************
3071 * EM_SCROLLCARET
3074 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3076 if (es->style & ES_MULTILINE) {
3077 INT l;
3078 INT li;
3079 INT vlc;
3080 INT ww;
3081 INT cw = es->char_width;
3082 INT x;
3083 INT dy = 0;
3084 INT dx = 0;
3086 l = EDIT_EM_LineFromChar(es, es->selection_end);
3087 li = EDIT_EM_LineIndex(es, l);
3088 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3089 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3090 if (l >= es->y_offset + vlc)
3091 dy = l - vlc + 1 - es->y_offset;
3092 if (l < es->y_offset)
3093 dy = l - es->y_offset;
3094 ww = es->format_rect.right - es->format_rect.left;
3095 if (x < es->format_rect.left)
3096 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3097 if (x > es->format_rect.right)
3098 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3099 if (dy || dx)
3101 /* check if we are going to move too far */
3102 if(es->x_offset + dx + ww > es->text_width)
3103 dx = es->text_width - ww - es->x_offset;
3104 if(dx || dy)
3105 EDIT_EM_LineScroll_internal(es, dx, dy);
3107 } else {
3108 INT x;
3109 INT goal;
3110 INT format_width;
3112 if (!(es->style & ES_AUTOHSCROLL))
3113 return;
3115 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3116 format_width = es->format_rect.right - es->format_rect.left;
3117 if (x < es->format_rect.left) {
3118 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3119 do {
3120 es->x_offset--;
3121 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3122 } while ((x < goal) && es->x_offset);
3123 /* FIXME: use ScrollWindow() somehow to improve performance */
3124 EDIT_UpdateText(es, NULL, TRUE);
3125 } else if (x > es->format_rect.right) {
3126 INT x_last;
3127 INT len = strlenW(es->text);
3128 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3129 do {
3130 es->x_offset++;
3131 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3132 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3133 } while ((x > goal) && (x_last > es->format_rect.right));
3134 /* FIXME: use ScrollWindow() somehow to improve performance */
3135 EDIT_UpdateText(es, NULL, TRUE);
3139 if(es->flags & EF_FOCUSED)
3140 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3144 /*********************************************************************
3146 * EM_SETHANDLE
3148 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3151 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3153 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3155 if (!(es->style & ES_MULTILINE))
3156 return;
3158 if (!hloc) {
3159 WARN("called with NULL handle\n");
3160 return;
3163 EDIT_UnlockBuffer(es, TRUE);
3165 if(es->hloc16)
3167 LOCAL_Free(hInstance, es->hloc16);
3168 es->hloc16 = (HLOCAL16)NULL;
3171 if(es->is_unicode)
3173 if(es->hloc32A)
3175 LocalFree(es->hloc32A);
3176 es->hloc32A = NULL;
3178 es->hloc32W = hloc;
3180 else
3182 INT countW, countA;
3183 HLOCAL hloc32W_new;
3184 WCHAR *textW;
3185 CHAR *textA;
3187 countA = LocalSize(hloc);
3188 textA = LocalLock(hloc);
3189 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3190 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3192 ERR("Could not allocate new unicode buffer\n");
3193 return;
3195 textW = LocalLock(hloc32W_new);
3196 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3197 LocalUnlock(hloc32W_new);
3198 LocalUnlock(hloc);
3200 if(es->hloc32W)
3201 LocalFree(es->hloc32W);
3203 es->hloc32W = hloc32W_new;
3204 es->hloc32A = hloc;
3207 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3209 EDIT_LockBuffer(es);
3211 es->x_offset = es->y_offset = 0;
3212 es->selection_start = es->selection_end = 0;
3213 EDIT_EM_EmptyUndoBuffer(es);
3214 es->flags &= ~EF_MODIFIED;
3215 es->flags &= ~EF_UPDATE;
3216 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3217 EDIT_UpdateText(es, NULL, TRUE);
3218 EDIT_EM_ScrollCaret(es);
3219 /* force scroll info update */
3220 EDIT_UpdateScrollInfo(es);
3224 /*********************************************************************
3226 * EM_SETHANDLE16
3228 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3231 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3233 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3234 INT countW, countA;
3235 HLOCAL hloc32W_new;
3236 WCHAR *textW;
3237 CHAR *textA;
3239 if (!(es->style & ES_MULTILINE))
3240 return;
3242 if (!hloc) {
3243 WARN("called with NULL handle\n");
3244 return;
3247 EDIT_UnlockBuffer(es, TRUE);
3249 if(es->hloc32A)
3251 LocalFree(es->hloc32A);
3252 es->hloc32A = NULL;
3255 countA = LOCAL_Size(hInstance, hloc);
3256 textA = LOCAL_Lock(hInstance, hloc);
3257 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3258 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3260 ERR("Could not allocate new unicode buffer\n");
3261 return;
3263 textW = LocalLock(hloc32W_new);
3264 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3265 LocalUnlock(hloc32W_new);
3266 LOCAL_Unlock(hInstance, hloc);
3268 if(es->hloc32W)
3269 LocalFree(es->hloc32W);
3271 es->hloc32W = hloc32W_new;
3272 es->hloc16 = hloc;
3274 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3276 EDIT_LockBuffer(es);
3278 es->x_offset = es->y_offset = 0;
3279 es->selection_start = es->selection_end = 0;
3280 EDIT_EM_EmptyUndoBuffer(es);
3281 es->flags &= ~EF_MODIFIED;
3282 es->flags &= ~EF_UPDATE;
3283 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3284 EDIT_UpdateText(es, NULL, TRUE);
3285 EDIT_EM_ScrollCaret(es);
3286 /* force scroll info update */
3287 EDIT_UpdateScrollInfo(es);
3291 /*********************************************************************
3293 * EM_SETLIMITTEXT
3295 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3296 * However, the windows version is not complied to yet in all of edit.c
3298 * Additionally as the wrapper for RichEdit controls we need larger buffers
3299 * at present -1 will represent nolimit
3301 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3303 if (limit == 0xFFFFFFFF)
3304 es->buffer_limit = -1;
3305 else if (es->style & ES_MULTILINE) {
3306 if (limit)
3307 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3308 else
3309 es->buffer_limit = BUFLIMIT_MULTI;
3310 } else {
3311 if (limit)
3312 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3313 else
3314 es->buffer_limit = BUFLIMIT_SINGLE;
3319 /*********************************************************************
3321 * EM_SETMARGINS
3323 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3324 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3325 * margin according to the textmetrics of the current font.
3327 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3328 * of the char's width as the margin, but this is not how Windows handles this.
3329 * For all other fonts Windows sets the margins to zero.
3332 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3333 INT left, INT right)
3335 TEXTMETRICW tm;
3336 INT default_left_margin = 0; /* in pixels */
3337 INT default_right_margin = 0; /* in pixels */
3339 /* Set the default margins depending on the font */
3340 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3341 HDC dc = GetDC(es->hwndSelf);
3342 HFONT old_font = SelectObject(dc, es->font);
3343 GetTextMetricsW(dc, &tm);
3344 /* The default margins are only non zero for TrueType or Vector fonts */
3345 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3346 /* This must be calculated more exactly! But how? */
3347 default_left_margin = tm.tmAveCharWidth / 3;
3348 default_right_margin = tm.tmAveCharWidth / 3;
3350 SelectObject(dc, old_font);
3351 ReleaseDC(es->hwndSelf, dc);
3354 if (action & EC_LEFTMARGIN) {
3355 if (left != EC_USEFONTINFO)
3356 es->left_margin = left;
3357 else
3358 es->left_margin = default_left_margin;
3361 if (action & EC_RIGHTMARGIN) {
3362 if (right != EC_USEFONTINFO)
3363 es->right_margin = right;
3364 else
3365 es->right_margin = default_right_margin;
3367 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3371 /*********************************************************************
3373 * EM_SETPASSWORDCHAR
3376 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3378 LONG style;
3380 if (es->style & ES_MULTILINE)
3381 return;
3383 if (es->password_char == c)
3384 return;
3386 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3387 es->password_char = c;
3388 if (c) {
3389 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3390 es->style |= ES_PASSWORD;
3391 } else {
3392 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3393 es->style &= ~ES_PASSWORD;
3395 EDIT_UpdateText(es, NULL, TRUE);
3399 /*********************************************************************
3401 * EDIT_EM_SetSel
3403 * note: unlike the specs say: the order of start and end
3404 * _is_ preserved in Windows. (i.e. start can be > end)
3405 * In other words: this handler is OK
3408 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3410 UINT old_start = es->selection_start;
3411 UINT old_end = es->selection_end;
3412 UINT len = strlenW(es->text);
3414 if (start == (UINT)-1) {
3415 start = es->selection_end;
3416 end = es->selection_end;
3417 } else {
3418 start = min(start, len);
3419 end = min(end, len);
3421 es->selection_start = start;
3422 es->selection_end = end;
3423 if (after_wrap)
3424 es->flags |= EF_AFTER_WRAP;
3425 else
3426 es->flags &= ~EF_AFTER_WRAP;
3427 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3428 ORDER_UINT(start, end);
3429 ORDER_UINT(end, old_end);
3430 ORDER_UINT(start, old_start);
3431 ORDER_UINT(old_start, old_end);
3432 if (end != old_start)
3435 * One can also do
3436 * ORDER_UINT32(end, old_start);
3437 * EDIT_InvalidateText(es, start, end);
3438 * EDIT_InvalidateText(es, old_start, old_end);
3439 * in place of the following if statement.
3441 if (old_start > end )
3443 EDIT_InvalidateText(es, start, end);
3444 EDIT_InvalidateText(es, old_start, old_end);
3446 else
3448 EDIT_InvalidateText(es, start, old_start);
3449 EDIT_InvalidateText(es, end, old_end);
3452 else EDIT_InvalidateText(es, start, old_end);
3456 /*********************************************************************
3458 * EM_SETTABSTOPS
3461 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3463 if (!(es->style & ES_MULTILINE))
3464 return FALSE;
3465 if (es->tabs)
3466 HeapFree(GetProcessHeap(), 0, es->tabs);
3467 es->tabs_count = count;
3468 if (!count)
3469 es->tabs = NULL;
3470 else {
3471 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3472 memcpy(es->tabs, tabs, count * sizeof(INT));
3474 return TRUE;
3478 /*********************************************************************
3480 * EM_SETTABSTOPS16
3483 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3485 if (!(es->style & ES_MULTILINE))
3486 return FALSE;
3487 if (es->tabs)
3488 HeapFree(GetProcessHeap(), 0, es->tabs);
3489 es->tabs_count = count;
3490 if (!count)
3491 es->tabs = NULL;
3492 else {
3493 INT i;
3494 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3495 for (i = 0 ; i < count ; i++)
3496 es->tabs[i] = *tabs++;
3498 return TRUE;
3502 /*********************************************************************
3504 * EM_SETWORDBREAKPROC
3507 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, LPARAM lParam)
3509 if (es->word_break_proc == (void *)lParam)
3510 return;
3512 es->word_break_proc = (void *)lParam;
3513 es->word_break_proc16 = NULL;
3515 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3516 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3517 EDIT_UpdateText(es, NULL, TRUE);
3522 /*********************************************************************
3524 * EM_SETWORDBREAKPROC16
3527 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3529 if (es->word_break_proc16 == wbp)
3530 return;
3532 es->word_break_proc = NULL;
3533 es->word_break_proc16 = wbp;
3534 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3535 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3536 EDIT_UpdateText(es, NULL, TRUE);
3541 /*********************************************************************
3543 * EM_UNDO / WM_UNDO
3546 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3548 INT ulength;
3549 LPWSTR utext;
3551 /* As per MSDN spec, for a single-line edit control,
3552 the return value is always TRUE */
3553 if( es->style & ES_READONLY )
3554 return !(es->style & ES_MULTILINE);
3556 ulength = strlenW(es->undo_text);
3558 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3560 strcpyW(utext, es->undo_text);
3562 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3563 es->undo_insert_count, debugstr_w(utext));
3565 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3566 EDIT_EM_EmptyUndoBuffer(es);
3567 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3568 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3569 /* send the notification after the selection start and end are set */
3570 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3571 EDIT_EM_ScrollCaret(es);
3572 HeapFree(GetProcessHeap(), 0, utext);
3574 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3575 es->undo_insert_count, debugstr_w(es->undo_text));
3576 return TRUE;
3580 /*********************************************************************
3582 * WM_CHAR
3585 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3587 BOOL control;
3589 /* Protect read-only edit control from modification */
3590 if(es->style & ES_READONLY)
3591 return;
3593 control = GetKeyState(VK_CONTROL) & 0x8000;
3595 switch (c) {
3596 case '\r':
3597 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3598 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3599 break;
3600 case '\n':
3601 if (es->style & ES_MULTILINE) {
3602 if (es->style & ES_READONLY) {
3603 EDIT_MoveHome(es, FALSE);
3604 EDIT_MoveDown_ML(es, FALSE);
3605 } else {
3606 static const WCHAR cr_lfW[] = {'\r','\n',0};
3607 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3610 break;
3611 case '\t':
3612 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3614 static const WCHAR tabW[] = {'\t',0};
3615 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3617 break;
3618 case VK_BACK:
3619 if (!(es->style & ES_READONLY) && !control) {
3620 if (es->selection_start != es->selection_end)
3621 EDIT_WM_Clear(es);
3622 else {
3623 /* delete character left of caret */
3624 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3625 EDIT_MoveBackward(es, TRUE);
3626 EDIT_WM_Clear(es);
3629 break;
3630 case 0x03: /* ^C */
3631 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3632 break;
3633 case 0x16: /* ^V */
3634 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3635 break;
3636 case 0x18: /* ^X */
3637 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3638 break;
3640 default:
3641 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3642 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3643 break;
3645 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3646 WCHAR str[2];
3647 str[0] = c;
3648 str[1] = '\0';
3649 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3651 break;
3656 /*********************************************************************
3658 * WM_COMMAND
3661 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3663 if (code || control)
3664 return;
3666 switch (id) {
3667 case EM_UNDO:
3668 EDIT_EM_Undo(es);
3669 break;
3670 case WM_CUT:
3671 EDIT_WM_Cut(es);
3672 break;
3673 case WM_COPY:
3674 EDIT_WM_Copy(es);
3675 break;
3676 case WM_PASTE:
3677 EDIT_WM_Paste(es);
3678 break;
3679 case WM_CLEAR:
3680 EDIT_WM_Clear(es);
3681 break;
3682 case EM_SETSEL:
3683 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3684 EDIT_EM_ScrollCaret(es);
3685 break;
3686 default:
3687 ERR("unknown menu item, please report\n");
3688 break;
3693 /*********************************************************************
3695 * WM_CONTEXTMENU
3697 * Note: the resource files resource/sysres_??.rc cannot define a
3698 * single popup menu. Hence we use a (dummy) menubar
3699 * containing the single popup menu as its first item.
3701 * FIXME: the message identifiers have been chosen arbitrarily,
3702 * hence we use MF_BYPOSITION.
3703 * We might as well use the "real" values (anybody knows ?)
3704 * The menu definition is in resources/sysres_??.rc.
3705 * Once these are OK, we better use MF_BYCOMMAND here
3706 * (as we do in EDIT_WM_Command()).
3709 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3711 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3712 HMENU popup = GetSubMenu(menu, 0);
3713 UINT start = es->selection_start;
3714 UINT end = es->selection_end;
3716 ORDER_UINT(start, end);
3718 /* undo */
3719 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3720 /* cut */
3721 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3722 /* copy */
3723 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3724 /* paste */
3725 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3726 /* delete */
3727 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3728 /* select all */
3729 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3731 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3732 DestroyMenu(menu);
3736 /*********************************************************************
3738 * WM_COPY
3741 static void EDIT_WM_Copy(EDITSTATE *es)
3743 INT s = min(es->selection_start, es->selection_end);
3744 INT e = max(es->selection_start, es->selection_end);
3745 HGLOBAL hdst;
3746 LPWSTR dst;
3748 if (e == s) return;
3750 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3751 dst = GlobalLock(hdst);
3752 strncpyW(dst, es->text + s, e - s);
3753 dst[e - s] = 0; /* ensure 0 termination */
3754 TRACE("%s\n", debugstr_w(dst));
3755 GlobalUnlock(hdst);
3756 OpenClipboard(es->hwndSelf);
3757 EmptyClipboard();
3758 SetClipboardData(CF_UNICODETEXT, hdst);
3759 CloseClipboard();
3763 /*********************************************************************
3765 * WM_CREATE
3768 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3770 TRACE("%s\n", debugstr_w(name));
3772 * To initialize some final structure members, we call some helper
3773 * functions. However, since the EDITSTATE is not consistent (i.e.
3774 * not fully initialized), we should be very careful which
3775 * functions can be called, and in what order.
3777 EDIT_WM_SetFont(es, 0, FALSE);
3778 EDIT_EM_EmptyUndoBuffer(es);
3780 if (name && *name) {
3781 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3782 /* if we insert text to the editline, the text scrolls out
3783 * of the window, as the caret is placed after the insert
3784 * pos normally; thus we reset es->selection... to 0 and
3785 * update caret
3787 es->selection_start = es->selection_end = 0;
3788 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3789 * Messages are only to be sent when the USER does something to
3790 * change the contents. So I am removing this EN_CHANGE
3792 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3794 EDIT_EM_ScrollCaret(es);
3796 /* force scroll info update */
3797 EDIT_UpdateScrollInfo(es);
3798 /* The rule seems to return 1 here for success */
3799 /* Power Builder masked edit controls will crash */
3800 /* if not. */
3801 /* FIXME: is that in all cases so ? */
3802 return 1;
3806 /*********************************************************************
3808 * WM_DESTROY
3811 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3813 LINEDEF *pc, *pp;
3815 if (es->hloc32W) {
3816 while (LocalUnlock(es->hloc32W)) ;
3817 LocalFree(es->hloc32W);
3819 if (es->hloc32A) {
3820 while (LocalUnlock(es->hloc32A)) ;
3821 LocalFree(es->hloc32A);
3823 if (es->hloc16) {
3824 HINSTANCE16 hInstance = GetWindowWord( es->hwndSelf, GWL_HINSTANCE );
3825 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3826 LOCAL_Free(hInstance, es->hloc16);
3829 pc = es->first_line_def;
3830 while (pc)
3832 pp = pc->next;
3833 HeapFree(GetProcessHeap(), 0, pc);
3834 pc = pp;
3837 SetWindowLongW( es->hwndSelf, 0, 0 );
3838 HeapFree(GetProcessHeap(), 0, es);
3840 return 0;
3844 /*********************************************************************
3846 * WM_ERASEBKGND
3849 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3851 HBRUSH brush;
3852 RECT rc;
3854 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
3855 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3857 GetClientRect(es->hwndSelf, &rc);
3858 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3859 GetClipBox(dc, &rc);
3861 * FIXME: specs say that we should UnrealizeObject() the brush,
3862 * but the specs of UnrealizeObject() say that we shouldn't
3863 * unrealize a stock object. The default brush that
3864 * DefWndProc() returns is ... a stock object.
3866 FillRect(dc, &rc, brush);
3867 return -1;
3871 /*********************************************************************
3873 * WM_GETTEXT
3876 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3878 if(!count) return 0;
3880 if(unicode)
3882 LPWSTR textW = (LPWSTR)lParam;
3883 lstrcpynW(textW, es->text, count);
3884 return strlenW(textW);
3886 else
3888 LPSTR textA = (LPSTR)lParam;
3889 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3890 textA[count - 1] = 0; /* ensure 0 termination */
3891 return strlen(textA);
3895 /*********************************************************************
3897 * WM_HSCROLL
3900 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3902 INT dx;
3903 INT fw;
3905 if (!(es->style & ES_MULTILINE))
3906 return 0;
3908 if (!(es->style & ES_AUTOHSCROLL))
3909 return 0;
3911 dx = 0;
3912 fw = es->format_rect.right - es->format_rect.left;
3913 switch (action) {
3914 case SB_LINELEFT:
3915 TRACE("SB_LINELEFT\n");
3916 if (es->x_offset)
3917 dx = -es->char_width;
3918 break;
3919 case SB_LINERIGHT:
3920 TRACE("SB_LINERIGHT\n");
3921 if (es->x_offset < es->text_width)
3922 dx = es->char_width;
3923 break;
3924 case SB_PAGELEFT:
3925 TRACE("SB_PAGELEFT\n");
3926 if (es->x_offset)
3927 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3928 break;
3929 case SB_PAGERIGHT:
3930 TRACE("SB_PAGERIGHT\n");
3931 if (es->x_offset < es->text_width)
3932 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3933 break;
3934 case SB_LEFT:
3935 TRACE("SB_LEFT\n");
3936 if (es->x_offset)
3937 dx = -es->x_offset;
3938 break;
3939 case SB_RIGHT:
3940 TRACE("SB_RIGHT\n");
3941 if (es->x_offset < es->text_width)
3942 dx = es->text_width - es->x_offset;
3943 break;
3944 case SB_THUMBTRACK:
3945 TRACE("SB_THUMBTRACK %d\n", pos);
3946 es->flags |= EF_HSCROLL_TRACK;
3947 if(es->style & WS_HSCROLL)
3948 dx = pos - es->x_offset;
3949 else
3951 INT fw, new_x;
3952 /* Sanity check */
3953 if(pos < 0 || pos > 100) return 0;
3954 /* Assume default scroll range 0-100 */
3955 fw = es->format_rect.right - es->format_rect.left;
3956 new_x = pos * (es->text_width - fw) / 100;
3957 dx = es->text_width ? (new_x - es->x_offset) : 0;
3959 break;
3960 case SB_THUMBPOSITION:
3961 TRACE("SB_THUMBPOSITION %d\n", pos);
3962 es->flags &= ~EF_HSCROLL_TRACK;
3963 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3964 dx = pos - es->x_offset;
3965 else
3967 INT fw, new_x;
3968 /* Sanity check */
3969 if(pos < 0 || pos > 100) return 0;
3970 /* Assume default scroll range 0-100 */
3971 fw = es->format_rect.right - es->format_rect.left;
3972 new_x = pos * (es->text_width - fw) / 100;
3973 dx = es->text_width ? (new_x - es->x_offset) : 0;
3975 if (!dx) {
3976 /* force scroll info update */
3977 EDIT_UpdateScrollInfo(es);
3978 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
3980 break;
3981 case SB_ENDSCROLL:
3982 TRACE("SB_ENDSCROLL\n");
3983 break;
3985 * FIXME : the next two are undocumented !
3986 * Are we doing the right thing ?
3987 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3988 * although it's also a regular control message.
3990 case EM_GETTHUMB: /* this one is used by NT notepad */
3991 case EM_GETTHUMB16:
3993 LRESULT ret;
3994 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3995 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3996 else
3998 /* Assume default scroll range 0-100 */
3999 INT fw = es->format_rect.right - es->format_rect.left;
4000 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4002 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4003 return ret;
4005 case EM_LINESCROLL16:
4006 TRACE("EM_LINESCROLL16\n");
4007 dx = pos;
4008 break;
4010 default:
4011 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4012 action, action);
4013 return 0;
4015 if (dx)
4017 INT fw = es->format_rect.right - es->format_rect.left;
4018 /* check if we are going to move too far */
4019 if(es->x_offset + dx + fw > es->text_width)
4020 dx = es->text_width - fw - es->x_offset;
4021 if(dx)
4022 EDIT_EM_LineScroll_internal(es, dx, 0);
4024 return 0;
4028 /*********************************************************************
4030 * EDIT_CheckCombo
4033 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4035 HWND hLBox = es->hwndListBox;
4036 HWND hCombo;
4037 BOOL bDropped;
4038 int nEUI;
4040 if (!hLBox)
4041 return FALSE;
4043 hCombo = GetParent(es->hwndSelf);
4044 bDropped = TRUE;
4045 nEUI = 0;
4047 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4049 if (key == VK_UP || key == VK_DOWN)
4051 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4052 nEUI = 1;
4054 if (msg == WM_KEYDOWN || nEUI)
4055 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4058 switch (msg)
4060 case WM_KEYDOWN:
4061 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4063 /* make sure ComboLBox pops up */
4064 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4065 key = VK_F4;
4066 nEUI = 2;
4069 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4070 break;
4072 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4073 if (nEUI)
4074 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4075 else
4076 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4077 break;
4080 if(nEUI == 2)
4081 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4083 return TRUE;
4087 /*********************************************************************
4089 * WM_KEYDOWN
4091 * Handling of special keys that don't produce a WM_CHAR
4092 * (i.e. non-printable keys) & Backspace & Delete
4095 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4097 BOOL shift;
4098 BOOL control;
4100 if (GetKeyState(VK_MENU) & 0x8000)
4101 return 0;
4103 shift = GetKeyState(VK_SHIFT) & 0x8000;
4104 control = GetKeyState(VK_CONTROL) & 0x8000;
4106 switch (key) {
4107 case VK_F4:
4108 case VK_UP:
4109 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4110 break;
4112 /* fall through */
4113 case VK_LEFT:
4114 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4115 EDIT_MoveUp_ML(es, shift);
4116 else
4117 if (control)
4118 EDIT_MoveWordBackward(es, shift);
4119 else
4120 EDIT_MoveBackward(es, shift);
4121 break;
4122 case VK_DOWN:
4123 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4124 break;
4125 /* fall through */
4126 case VK_RIGHT:
4127 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4128 EDIT_MoveDown_ML(es, shift);
4129 else if (control)
4130 EDIT_MoveWordForward(es, shift);
4131 else
4132 EDIT_MoveForward(es, shift);
4133 break;
4134 case VK_HOME:
4135 EDIT_MoveHome(es, shift);
4136 break;
4137 case VK_END:
4138 EDIT_MoveEnd(es, shift);
4139 break;
4140 case VK_PRIOR:
4141 if (es->style & ES_MULTILINE)
4142 EDIT_MovePageUp_ML(es, shift);
4143 else
4144 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4145 break;
4146 case VK_NEXT:
4147 if (es->style & ES_MULTILINE)
4148 EDIT_MovePageDown_ML(es, shift);
4149 else
4150 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4151 break;
4152 case VK_DELETE:
4153 if (!(es->style & ES_READONLY) && !(shift && control)) {
4154 if (es->selection_start != es->selection_end) {
4155 if (shift)
4156 EDIT_WM_Cut(es);
4157 else
4158 EDIT_WM_Clear(es);
4159 } else {
4160 if (shift) {
4161 /* delete character left of caret */
4162 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4163 EDIT_MoveBackward(es, TRUE);
4164 EDIT_WM_Clear(es);
4165 } else if (control) {
4166 /* delete to end of line */
4167 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4168 EDIT_MoveEnd(es, TRUE);
4169 EDIT_WM_Clear(es);
4170 } else {
4171 /* delete character right of caret */
4172 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4173 EDIT_MoveForward(es, TRUE);
4174 EDIT_WM_Clear(es);
4178 break;
4179 case VK_INSERT:
4180 if (shift) {
4181 if (!(es->style & ES_READONLY))
4182 EDIT_WM_Paste(es);
4183 } else if (control)
4184 EDIT_WM_Copy(es);
4185 break;
4186 case VK_RETURN:
4187 /* If the edit doesn't want the return send a message to the default object */
4188 if(!(es->style & ES_WANTRETURN))
4190 HWND hwndParent = GetParent(es->hwndSelf);
4191 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4192 if (HIWORD(dw) == DC_HASDEFID)
4194 SendMessageW( hwndParent, WM_COMMAND,
4195 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4196 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4199 break;
4201 return 0;
4205 /*********************************************************************
4207 * WM_KILLFOCUS
4210 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4212 es->flags &= ~EF_FOCUSED;
4213 DestroyCaret();
4214 if(!(es->style & ES_NOHIDESEL))
4215 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4216 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4217 return 0;
4221 /*********************************************************************
4223 * WM_LBUTTONDBLCLK
4225 * The caret position has been set on the WM_LBUTTONDOWN message
4228 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4230 INT s;
4231 INT e = es->selection_end;
4232 INT l;
4233 INT li;
4234 INT ll;
4236 if (!(es->flags & EF_FOCUSED))
4237 return 0;
4239 l = EDIT_EM_LineFromChar(es, e);
4240 li = EDIT_EM_LineIndex(es, l);
4241 ll = EDIT_EM_LineLength(es, e);
4242 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4243 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4244 EDIT_EM_SetSel(es, s, e, FALSE);
4245 EDIT_EM_ScrollCaret(es);
4246 return 0;
4250 /*********************************************************************
4252 * WM_LBUTTONDOWN
4255 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4257 INT e;
4258 BOOL after_wrap;
4260 SetFocus(es->hwndSelf);
4261 if (!(es->flags & EF_FOCUSED))
4262 return 0;
4264 es->bCaptureState = TRUE;
4265 SetCapture(es->hwndSelf);
4266 EDIT_ConfinePoint(es, &x, &y);
4267 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4268 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4269 EDIT_EM_ScrollCaret(es);
4270 es->region_posx = es->region_posy = 0;
4271 SetTimer(es->hwndSelf, 0, 100, NULL);
4272 return 0;
4276 /*********************************************************************
4278 * WM_LBUTTONUP
4281 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4283 if (es->bCaptureState) {
4284 KillTimer(es->hwndSelf, 0);
4285 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4287 es->bCaptureState = FALSE;
4288 return 0;
4292 /*********************************************************************
4294 * WM_MBUTTONDOWN
4297 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4299 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4300 return 0;
4304 /*********************************************************************
4306 * WM_MOUSEMOVE
4309 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4311 INT e;
4312 BOOL after_wrap;
4313 INT prex, prey;
4315 if (GetCapture() != es->hwndSelf)
4316 return 0;
4319 * FIXME: gotta do some scrolling if outside client
4320 * area. Maybe reset the timer ?
4322 prex = x; prey = y;
4323 EDIT_ConfinePoint(es, &x, &y);
4324 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4325 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4326 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4327 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4328 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4329 return 0;
4333 /*********************************************************************
4335 * WM_NCCREATE
4337 * See also EDIT_WM_StyleChanged
4339 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4341 EDITSTATE *es;
4342 UINT alloc_size;
4344 TRACE("Creating %s edit control, style = %08lx\n",
4345 unicode ? "Unicode" : "ANSI", lpcs->style);
4347 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4348 return FALSE;
4349 SetWindowLongW( hwnd, 0, (LONG)es );
4352 * Note: since the EDITSTATE has not been fully initialized yet,
4353 * we can't use any API calls that may send
4354 * WM_XXX messages before WM_NCCREATE is completed.
4357 es->is_unicode = unicode;
4358 es->style = lpcs->style;
4360 es->bEnableState = !(es->style & WS_DISABLED);
4362 es->hwndSelf = hwnd;
4363 /* Save parent, which will be notified by EN_* messages */
4364 es->hwndParent = lpcs->hwndParent;
4366 if (es->style & ES_COMBO)
4367 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4369 /* Number overrides lowercase overrides uppercase (at least it
4370 * does in Win95). However I'll bet that ES_NUMBER would be
4371 * invalid under Win 3.1.
4373 if (es->style & ES_NUMBER) {
4374 ; /* do not override the ES_NUMBER */
4375 } else if (es->style & ES_LOWERCASE) {
4376 es->style &= ~ES_UPPERCASE;
4378 if (es->style & ES_MULTILINE) {
4379 es->buffer_limit = BUFLIMIT_MULTI;
4380 if (es->style & WS_VSCROLL)
4381 es->style |= ES_AUTOVSCROLL;
4382 if (es->style & WS_HSCROLL)
4383 es->style |= ES_AUTOHSCROLL;
4384 es->style &= ~ES_PASSWORD;
4385 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4386 /* Confirmed - RIGHT overrides CENTER */
4387 if (es->style & ES_RIGHT)
4388 es->style &= ~ES_CENTER;
4389 es->style &= ~WS_HSCROLL;
4390 es->style &= ~ES_AUTOHSCROLL;
4393 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4394 es->style |= ES_AUTOVSCROLL;
4395 } else {
4396 es->buffer_limit = BUFLIMIT_SINGLE;
4397 es->style &= ~ES_CENTER;
4398 es->style &= ~ES_RIGHT;
4399 es->style &= ~WS_HSCROLL;
4400 es->style &= ~WS_VSCROLL;
4401 es->style &= ~ES_AUTOVSCROLL;
4402 es->style &= ~ES_WANTRETURN;
4403 if (es->style & ES_PASSWORD)
4404 es->password_char = '*';
4406 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4407 es->style |= ES_AUTOHSCROLL;
4410 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4411 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4412 return FALSE;
4413 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4415 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4416 return FALSE;
4417 es->undo_buffer_size = es->buffer_size;
4419 if (es->style & ES_MULTILINE)
4420 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4421 return FALSE;
4422 es->line_count = 1;
4425 * In Win95 look and feel, the WS_BORDER style is replaced by the
4426 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4427 * control a non client area. Not always. This coordinates in some
4428 * way with the window creation code in dialog.c When making
4429 * modifications please ensure that the code still works for edit
4430 * controls created directly with style 0x50800000, exStyle 0 (
4431 * which should have a single pixel border)
4433 es->style &= ~WS_BORDER;
4435 return TRUE;
4438 /*********************************************************************
4440 * WM_PAINT
4443 static void EDIT_WM_Paint(EDITSTATE *es, WPARAM wParam)
4445 PAINTSTRUCT ps;
4446 INT i;
4447 HDC dc;
4448 HFONT old_font = 0;
4449 RECT rc;
4450 RECT rcLine;
4451 RECT rcRgn;
4452 BOOL rev = es->bEnableState &&
4453 ((es->flags & EF_FOCUSED) ||
4454 (es->style & ES_NOHIDESEL));
4455 if (!wParam)
4456 dc = BeginPaint(es->hwndSelf, &ps);
4457 else
4458 dc = (HDC) wParam;
4459 if(es->style & WS_BORDER) {
4460 GetClientRect(es->hwndSelf, &rc);
4461 if(es->style & ES_MULTILINE) {
4462 if(es->style & WS_HSCROLL) rc.bottom++;
4463 if(es->style & WS_VSCROLL) rc.right++;
4465 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4467 IntersectClipRect(dc, es->format_rect.left,
4468 es->format_rect.top,
4469 es->format_rect.right,
4470 es->format_rect.bottom);
4471 if (es->style & ES_MULTILINE) {
4472 GetClientRect(es->hwndSelf, &rc);
4473 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4475 if (es->font)
4476 old_font = SelectObject(dc, es->font);
4477 EDIT_NotifyCtlColor(es, dc);
4479 if (!es->bEnableState)
4480 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4481 GetClipBox(dc, &rcRgn);
4482 if (es->style & ES_MULTILINE) {
4483 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4484 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4485 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4486 if (IntersectRect(&rc, &rcRgn, &rcLine))
4487 EDIT_PaintLine(es, dc, i, rev);
4489 } else {
4490 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4491 if (IntersectRect(&rc, &rcRgn, &rcLine))
4492 EDIT_PaintLine(es, dc, 0, rev);
4494 if (es->font)
4495 SelectObject(dc, old_font);
4497 if (!wParam)
4498 EndPaint(es->hwndSelf, &ps);
4502 /*********************************************************************
4504 * WM_PASTE
4507 static void EDIT_WM_Paste(EDITSTATE *es)
4509 HGLOBAL hsrc;
4510 LPWSTR src;
4512 /* Protect read-only edit control from modification */
4513 if(es->style & ES_READONLY)
4514 return;
4516 OpenClipboard(es->hwndSelf);
4517 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4518 src = (LPWSTR)GlobalLock(hsrc);
4519 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4520 GlobalUnlock(hsrc);
4522 CloseClipboard();
4526 /*********************************************************************
4528 * WM_SETFOCUS
4531 static void EDIT_WM_SetFocus(EDITSTATE *es)
4533 es->flags |= EF_FOCUSED;
4534 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4535 EDIT_SetCaretPos(es, es->selection_end,
4536 es->flags & EF_AFTER_WRAP);
4537 if(!(es->style & ES_NOHIDESEL))
4538 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4539 ShowCaret(es->hwndSelf);
4540 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4544 /*********************************************************************
4546 * WM_SETFONT
4548 * With Win95 look the margins are set to default font value unless
4549 * the system font (font == 0) is being set, in which case they are left
4550 * unchanged.
4553 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4555 TEXTMETRICW tm;
4556 HDC dc;
4557 HFONT old_font = 0;
4558 RECT r;
4560 es->font = font;
4561 dc = GetDC(es->hwndSelf);
4562 if (font)
4563 old_font = SelectObject(dc, font);
4564 GetTextMetricsW(dc, &tm);
4565 es->line_height = tm.tmHeight;
4566 es->char_width = tm.tmAveCharWidth;
4567 if (font)
4568 SelectObject(dc, old_font);
4569 ReleaseDC(es->hwndSelf, dc);
4570 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4571 EC_USEFONTINFO, EC_USEFONTINFO);
4573 /* Force the recalculation of the format rect for each font change */
4574 GetClientRect(es->hwndSelf, &r);
4575 EDIT_SetRectNP(es, &r);
4577 if (es->style & ES_MULTILINE)
4578 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4579 else
4580 EDIT_CalcLineWidth_SL(es);
4582 if (redraw)
4583 EDIT_UpdateText(es, NULL, TRUE);
4584 if (es->flags & EF_FOCUSED) {
4585 DestroyCaret();
4586 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4587 EDIT_SetCaretPos(es, es->selection_end,
4588 es->flags & EF_AFTER_WRAP);
4589 ShowCaret(es->hwndSelf);
4594 /*********************************************************************
4596 * WM_SETTEXT
4598 * NOTES
4599 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4600 * The modified flag is reset. No notifications are sent.
4602 * For single-line controls, reception of WM_SETTEXT triggers:
4603 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4606 static void EDIT_WM_SetText(EDITSTATE *es, LPARAM lParam, BOOL unicode)
4608 LPWSTR text = NULL;
4610 if(unicode)
4611 text = (LPWSTR)lParam;
4612 else if (lParam)
4614 LPCSTR textA = (LPCSTR)lParam;
4615 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4616 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4617 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4620 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4621 if (text) {
4622 TRACE("%s\n", debugstr_w(text));
4623 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4624 if(!unicode)
4625 HeapFree(GetProcessHeap(), 0, text);
4626 } else {
4627 static const WCHAR empty_stringW[] = {0};
4628 TRACE("<NULL>\n");
4629 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4631 es->x_offset = 0;
4632 es->flags &= ~EF_MODIFIED;
4633 EDIT_EM_SetSel(es, 0, 0, FALSE);
4634 /* Send the notification after the selection start and end have been set
4635 * edit control doesn't send notification on WM_SETTEXT
4636 * if it is multiline, or it is part of combobox
4638 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4640 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4641 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4643 EDIT_EM_ScrollCaret(es);
4647 /*********************************************************************
4649 * WM_SIZE
4652 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4654 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4655 RECT rc;
4656 TRACE("width = %d, height = %d\n", width, height);
4657 SetRect(&rc, 0, 0, width, height);
4658 EDIT_SetRectNP(es, &rc);
4659 EDIT_UpdateText(es, NULL, TRUE);
4664 /*********************************************************************
4666 * WM_STYLECHANGED
4668 * This message is sent by SetWindowLong on having changed either the Style
4669 * or the extended style.
4671 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4673 * See also EDIT_WM_NCCreate
4675 * It appears that the Windows version of the edit control allows the style
4676 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4677 * style variable which will generally be different. In this function we
4678 * update the internal style based on what changed in the externally visible
4679 * style.
4681 * Much of this content as based upon the MSDN, especially:
4682 * Platform SDK Documentation -> User Interface Services ->
4683 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4684 * Edit Control Styles
4686 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4688 if (GWL_STYLE == which) {
4689 DWORD style_change_mask;
4690 DWORD new_style;
4691 /* Only a subset of changes can be applied after the control
4692 * has been created.
4694 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4695 ES_NUMBER;
4696 if (es->style & ES_MULTILINE)
4697 style_change_mask |= ES_WANTRETURN;
4699 new_style = style->styleNew & style_change_mask;
4701 /* Number overrides lowercase overrides uppercase (at least it
4702 * does in Win95). However I'll bet that ES_NUMBER would be
4703 * invalid under Win 3.1.
4705 if (new_style & ES_NUMBER) {
4706 ; /* do not override the ES_NUMBER */
4707 } else if (new_style & ES_LOWERCASE) {
4708 new_style &= ~ES_UPPERCASE;
4711 es->style = (es->style & ~style_change_mask) | new_style;
4712 } else if (GWL_EXSTYLE == which) {
4713 ; /* FIXME - what is needed here */
4714 } else {
4715 WARN ("Invalid style change %d\n",which);
4718 return 0;
4721 /*********************************************************************
4723 * WM_SYSKEYDOWN
4726 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4728 if ((key == VK_BACK) && (key_data & 0x2000)) {
4729 if (EDIT_EM_CanUndo(es))
4730 EDIT_EM_Undo(es);
4731 return 0;
4732 } else if (key == VK_UP || key == VK_DOWN) {
4733 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4734 return 0;
4736 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4740 /*********************************************************************
4742 * WM_TIMER
4745 static void EDIT_WM_Timer(EDITSTATE *es)
4747 if (es->region_posx < 0) {
4748 EDIT_MoveBackward(es, TRUE);
4749 } else if (es->region_posx > 0) {
4750 EDIT_MoveForward(es, TRUE);
4753 * FIXME: gotta do some vertical scrolling here, like
4754 * EDIT_EM_LineScroll(hwnd, 0, 1);
4758 /*********************************************************************
4760 * WM_VSCROLL
4763 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4765 INT dy;
4767 if (!(es->style & ES_MULTILINE))
4768 return 0;
4770 if (!(es->style & ES_AUTOVSCROLL))
4771 return 0;
4773 dy = 0;
4774 switch (action) {
4775 case SB_LINEUP:
4776 case SB_LINEDOWN:
4777 case SB_PAGEUP:
4778 case SB_PAGEDOWN:
4779 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4780 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4781 (action == SB_PAGEUP ? "SB_PAGEUP" :
4782 "SB_PAGEDOWN"))));
4783 EDIT_EM_Scroll(es, action);
4784 return 0;
4785 case SB_TOP:
4786 TRACE("SB_TOP\n");
4787 dy = -es->y_offset;
4788 break;
4789 case SB_BOTTOM:
4790 TRACE("SB_BOTTOM\n");
4791 dy = es->line_count - 1 - es->y_offset;
4792 break;
4793 case SB_THUMBTRACK:
4794 TRACE("SB_THUMBTRACK %d\n", pos);
4795 es->flags |= EF_VSCROLL_TRACK;
4796 if(es->style & WS_VSCROLL)
4797 dy = pos - es->y_offset;
4798 else
4800 /* Assume default scroll range 0-100 */
4801 INT vlc, new_y;
4802 /* Sanity check */
4803 if(pos < 0 || pos > 100) return 0;
4804 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4805 new_y = pos * (es->line_count - vlc) / 100;
4806 dy = es->line_count ? (new_y - es->y_offset) : 0;
4807 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4808 es->line_count, es->y_offset, pos, dy);
4810 break;
4811 case SB_THUMBPOSITION:
4812 TRACE("SB_THUMBPOSITION %d\n", pos);
4813 es->flags &= ~EF_VSCROLL_TRACK;
4814 if(es->style & WS_VSCROLL)
4815 dy = pos - es->y_offset;
4816 else
4818 /* Assume default scroll range 0-100 */
4819 INT vlc, new_y;
4820 /* Sanity check */
4821 if(pos < 0 || pos > 100) return 0;
4822 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4823 new_y = pos * (es->line_count - vlc) / 100;
4824 dy = es->line_count ? (new_y - es->y_offset) : 0;
4825 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4826 es->line_count, es->y_offset, pos, dy);
4828 if (!dy)
4830 /* force scroll info update */
4831 EDIT_UpdateScrollInfo(es);
4832 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4834 break;
4835 case SB_ENDSCROLL:
4836 TRACE("SB_ENDSCROLL\n");
4837 break;
4839 * FIXME : the next two are undocumented !
4840 * Are we doing the right thing ?
4841 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4842 * although it's also a regular control message.
4844 case EM_GETTHUMB: /* this one is used by NT notepad */
4845 case EM_GETTHUMB16:
4847 LRESULT ret;
4848 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4849 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4850 else
4852 /* Assume default scroll range 0-100 */
4853 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4854 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4856 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4857 return ret;
4859 case EM_LINESCROLL16:
4860 TRACE("EM_LINESCROLL16 %d\n", pos);
4861 dy = pos;
4862 break;
4864 default:
4865 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4866 action, action);
4867 return 0;
4869 if (dy)
4870 EDIT_EM_LineScroll(es, 0, dy);
4871 return 0;
4874 /*********************************************************************
4876 * EDIT_UpdateText
4879 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4881 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4882 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4886 /*********************************************************************
4888 * EDIT_UpdateText
4891 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4893 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4894 InvalidateRect(es->hwndSelf, rc, bErase);