Append .dll extension in all cases (spotted by Mike Hearn).
[wine/wine-kai.git] / dlls / user / edit.c
blobec2d7dc3a5aef5d5b415a83cc887f535d00fb333
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 * NOTES
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_CENTER
42 * - ES_RIGHT
43 * - ES_LEFT
44 * - ES_OEMCONVERT
45 * -!ES_AUTOVSCROLL (every multi line control *is* auto vscroll)
46 * -!ES_AUTOHSCROLL (every single line control *is* auto hscroll)
48 * When there is no autoscrolling, the control should first check whether
49 * the new text would fit. If not, an EN_MAXTEXT should be sent.
50 * However, currently this would require the actual change to be made,
51 * then call EDIT_BuildLineDefs() and then find out that the new text doesn't
52 * fit. After all this, things should be put back in the state before the
53 * changes. Note that for multi line controls !ES_AUTOHSCROLL works : wordwrap.
57 #include "config.h"
59 #include <stdarg.h>
60 #include <string.h>
61 #include <stdlib.h>
63 #include "windef.h"
64 #include "winbase.h"
65 #include "winnt.h"
66 #include "wownt32.h"
67 #include "win.h"
68 #include "wine/winbase16.h"
69 #include "wine/winuser16.h"
70 #include "wine/unicode.h"
71 #include "controls.h"
72 #include "local.h"
73 #include "message.h"
74 #include "user.h"
75 #include "wine/debug.h"
77 WINE_DEFAULT_DEBUG_CHANNEL(edit);
78 WINE_DECLARE_DEBUG_CHANNEL(combo);
79 WINE_DECLARE_DEBUG_CHANNEL(relay);
81 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
82 FIXME: BTW, new specs say 65535 (do you dare ???) */
83 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
84 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
85 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
86 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
89 * extra flags for EDITSTATE.flags field
91 #define EF_MODIFIED 0x0001 /* text has been modified */
92 #define EF_FOCUSED 0x0002 /* we have input focus */
93 #define EF_UPDATE 0x0004 /* notify parent of changed state */
94 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
95 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
96 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
97 wrapped line, instead of in front of the next character */
98 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
100 typedef enum
102 END_0 = 0, /* line ends with terminating '\0' character */
103 END_WRAP, /* line is wrapped */
104 END_HARD, /* line ends with a hard return '\r\n' */
105 END_SOFT, /* line ends with a soft return '\r\r\n' */
106 END_RICH /* line ends with a single '\n' */
107 } LINE_END;
109 typedef struct tagLINEDEF {
110 INT length; /* bruto length of a line in bytes */
111 INT net_length; /* netto length of a line in visible characters */
112 LINE_END ending;
113 INT width; /* width of the line in pixels */
114 INT index; /* line index into the buffer */
115 struct tagLINEDEF *next;
116 } LINEDEF;
118 typedef struct
120 BOOL is_unicode; /* how the control was created */
121 LPWSTR text; /* the actual contents of the control */
122 UINT buffer_size; /* the size of the buffer in characters */
123 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
124 HFONT font; /* NULL means standard system font */
125 INT x_offset; /* scroll offset for multi lines this is in pixels
126 for single lines it's in characters */
127 INT line_height; /* height of a screen line in pixels */
128 INT char_width; /* average character width in pixels */
129 DWORD style; /* sane version of wnd->dwStyle */
130 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
131 INT undo_insert_count; /* number of characters inserted in sequence */
132 UINT undo_position; /* character index of the insertion and deletion */
133 LPWSTR undo_text; /* deleted text */
134 UINT undo_buffer_size; /* size of the deleted text buffer */
135 INT selection_start; /* == selection_end if no selection */
136 INT selection_end; /* == current caret position */
137 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
138 INT left_margin; /* in pixels */
139 INT right_margin; /* in pixels */
140 RECT format_rect;
141 INT text_width; /* width of the widest line in pixels for multi line controls
142 and just line width for single line controls */
143 INT region_posx; /* Position of cursor relative to region: */
144 INT region_posy; /* -1: to left, 0: within, 1: to right */
145 EDITWORDBREAKPROC16 word_break_proc16;
146 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
147 INT line_count; /* number of lines */
148 INT y_offset; /* scroll offset in number of lines */
149 BOOL bCaptureState; /* flag indicating whether mouse was captured */
150 BOOL bEnableState; /* flag keeping the enable state */
151 HWND hwndSelf; /* the our window handle */
152 HWND hwndParent; /* Handle of parent for sending EN_* messages.
153 Even if parent will change, EN_* messages
154 should be sent to the first parent. */
155 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
157 * only for multi line controls
159 INT lock_count; /* amount of re-entries in the EditWndProc */
160 INT tabs_count;
161 LPINT tabs;
162 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
163 HLOCAL hloc32W; /* our unicode local memory block */
164 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
165 or EM_SETHANDLE16 */
166 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
167 or EM_SETHANDLE */
168 } EDITSTATE;
171 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
172 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
174 /* used for disabled or read-only edit control */
175 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
176 do \
177 { /* Notify parent which has created this edit control */ \
178 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
179 SendMessageW(es->hwndParent, WM_COMMAND, \
180 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
181 (LPARAM)(es->hwndSelf)); \
182 } while(0)
184 /*********************************************************************
186 * Declarations
191 * These functions have trivial implementations
192 * We still like to call them internally
193 * "static inline" makes them more like macro's
195 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
196 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
197 static inline void EDIT_WM_Clear(EDITSTATE *es);
198 static inline void EDIT_WM_Cut(EDITSTATE *es);
201 * Helper functions only valid for one type of control
203 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
204 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
205 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
206 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
207 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
208 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
209 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
211 * Helper functions valid for both single line _and_ multi line controls
213 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
214 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
215 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
216 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
217 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
218 static void EDIT_LockBuffer(EDITSTATE *es);
219 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
220 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
221 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
222 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
223 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
224 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
225 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
226 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
227 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
228 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
229 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
230 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
231 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
232 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
233 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
235 * EM_XXX message handlers
237 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
238 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
239 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
240 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
241 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
242 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
243 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
244 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
245 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
246 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
247 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
248 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
249 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
250 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
251 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
252 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
253 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
254 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
255 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
256 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
257 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
258 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
259 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
260 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
261 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
262 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
263 static BOOL EDIT_EM_Undo(EDITSTATE *es);
265 * WM_XXX message handlers
267 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
268 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
269 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
270 static void EDIT_WM_Copy(EDITSTATE *es);
271 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
272 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
273 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
274 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
275 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
276 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
277 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
278 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
279 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
280 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
281 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
282 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
283 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
284 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
285 static void EDIT_WM_Paste(EDITSTATE *es);
286 static void EDIT_WM_SetFocus(EDITSTATE *es);
287 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
288 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
289 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
290 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
291 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
292 static void EDIT_WM_Timer(EDITSTATE *es);
293 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
294 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
295 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
297 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
298 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
300 /*********************************************************************
301 * edit class descriptor
303 const struct builtin_class_descr EDIT_builtin_class =
305 "Edit", /* name */
306 CS_DBLCLKS | CS_PARENTDC, /* style */
307 EditWndProcA, /* procA */
308 EditWndProcW, /* procW */
309 sizeof(EDITSTATE *), /* extra */
310 IDC_IBEAM, /* cursor */
311 0 /* brush */
315 /*********************************************************************
317 * EM_CANUNDO
320 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
322 return (es->undo_insert_count || strlenW(es->undo_text));
326 /*********************************************************************
328 * EM_EMPTYUNDOBUFFER
331 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
333 es->undo_insert_count = 0;
334 *es->undo_text = '\0';
338 /*********************************************************************
340 * WM_CLEAR
343 static inline void EDIT_WM_Clear(EDITSTATE *es)
345 static const WCHAR empty_stringW[] = {0};
347 /* Protect read-only edit control from modification */
348 if(es->style & ES_READONLY)
349 return;
351 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
355 /*********************************************************************
357 * WM_CUT
360 static inline void EDIT_WM_Cut(EDITSTATE *es)
362 EDIT_WM_Copy(es);
363 EDIT_WM_Clear(es);
367 /**********************************************************************
368 * get_app_version
370 * Returns the window version in case Wine emulates a later version
371 * of windows than the application expects.
373 * In a number of cases when windows runs an application that was
374 * designed for an earlier windows version, windows reverts
375 * to "old" behaviour of that earlier version.
377 * An example is a disabled edit control that needs to be painted.
378 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
379 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
380 * applications with an expected version 0f 4.0 or higher.
383 static DWORD get_app_version(void)
385 static DWORD version;
386 if (!version)
388 DWORD dwEmulatedVersion;
389 OSVERSIONINFOW info;
390 DWORD dwProcVersion = GetProcessVersion(0);
392 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
393 GetVersionExW( &info );
394 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
395 /* FIXME: this may not be 100% correct; see discussion on the
396 * wine developer list in Nov 1999 */
397 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
399 return version;
403 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
405 UINT msg;
407 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
408 msg = WM_CTLCOLORSTATIC;
409 else
410 msg = WM_CTLCOLOREDIT;
412 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
413 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
416 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
418 if(unicode)
419 return DefWindowProcW(hwnd, msg, wParam, lParam);
420 else
421 return DefWindowProcA(hwnd, msg, wParam, lParam);
424 /*********************************************************************
426 * EditWndProc_common
428 * The messages are in the order of the actual integer values
429 * (which can be found in include/windows.h)
430 * Wherever possible the 16 bit versions are converted to
431 * the 32 bit ones, so that we can 'fall through' to the
432 * helper functions. These are mostly 32 bit (with a few
433 * exceptions, clearly indicated by a '16' extension to their
434 * names).
437 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
438 WPARAM wParam, LPARAM lParam, BOOL unicode )
440 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
441 LRESULT result = 0;
443 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
445 if (!es && msg != WM_NCCREATE)
446 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
447 else if (msg == WM_NCCREATE)
448 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
449 else if (msg == WM_DESTROY)
450 return EDIT_WM_Destroy(es);
453 if (es) EDIT_LockBuffer(es);
455 switch (msg) {
456 case EM_GETSEL16:
457 wParam = 0;
458 lParam = 0;
459 /* fall through */
460 case EM_GETSEL:
461 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
462 break;
464 case EM_SETSEL16:
465 if ((short)LOWORD(lParam) == -1)
466 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
467 else
468 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
469 if (!wParam)
470 EDIT_EM_ScrollCaret(es);
471 result = 1;
472 break;
473 case EM_SETSEL:
474 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
475 EDIT_EM_ScrollCaret(es);
476 result = 1;
477 break;
479 case EM_GETRECT16:
480 if (lParam)
482 RECT16 *r16 = MapSL(lParam);
483 r16->left = es->format_rect.left;
484 r16->top = es->format_rect.top;
485 r16->right = es->format_rect.right;
486 r16->bottom = es->format_rect.bottom;
488 break;
489 case EM_GETRECT:
490 if (lParam)
491 CopyRect((LPRECT)lParam, &es->format_rect);
492 break;
494 case EM_SETRECT16:
495 if ((es->style & ES_MULTILINE) && lParam) {
496 RECT rc;
497 RECT16 *r16 = MapSL(lParam);
498 rc.left = r16->left;
499 rc.top = r16->top;
500 rc.right = r16->right;
501 rc.bottom = r16->bottom;
502 EDIT_SetRectNP(es, &rc);
503 EDIT_UpdateText(es, NULL, TRUE);
505 break;
506 case EM_SETRECT:
507 if ((es->style & ES_MULTILINE) && lParam) {
508 EDIT_SetRectNP(es, (LPRECT)lParam);
509 EDIT_UpdateText(es, NULL, TRUE);
511 break;
513 case EM_SETRECTNP16:
514 if ((es->style & ES_MULTILINE) && lParam) {
515 RECT rc;
516 RECT16 *r16 = MapSL(lParam);
517 rc.left = r16->left;
518 rc.top = r16->top;
519 rc.right = r16->right;
520 rc.bottom = r16->bottom;
521 EDIT_SetRectNP(es, &rc);
523 break;
524 case EM_SETRECTNP:
525 if ((es->style & ES_MULTILINE) && lParam)
526 EDIT_SetRectNP(es, (LPRECT)lParam);
527 break;
529 case EM_SCROLL16:
530 case EM_SCROLL:
531 result = EDIT_EM_Scroll(es, (INT)wParam);
532 break;
534 case EM_LINESCROLL16:
535 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
536 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
537 /* fall through */
538 case EM_LINESCROLL:
539 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
540 break;
542 case EM_SCROLLCARET16:
543 case EM_SCROLLCARET:
544 EDIT_EM_ScrollCaret(es);
545 result = 1;
546 break;
548 case EM_GETMODIFY16:
549 case EM_GETMODIFY:
550 result = ((es->flags & EF_MODIFIED) != 0);
551 break;
553 case EM_SETMODIFY16:
554 case EM_SETMODIFY:
555 if (wParam)
556 es->flags |= EF_MODIFIED;
557 else
558 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
559 break;
561 case EM_GETLINECOUNT16:
562 case EM_GETLINECOUNT:
563 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
564 break;
566 case EM_LINEINDEX16:
567 if ((INT16)wParam == -1)
568 wParam = (WPARAM)-1;
569 /* fall through */
570 case EM_LINEINDEX:
571 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
572 break;
574 case EM_SETHANDLE16:
575 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
576 break;
577 case EM_SETHANDLE:
578 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
579 break;
581 case EM_GETHANDLE16:
582 result = (LRESULT)EDIT_EM_GetHandle16(es);
583 break;
584 case EM_GETHANDLE:
585 result = (LRESULT)EDIT_EM_GetHandle(es);
586 break;
588 case EM_GETTHUMB16:
589 case EM_GETTHUMB:
590 result = EDIT_EM_GetThumb(es);
591 break;
593 /* these messages missing from specs */
594 case WM_USER+15:
595 case 0x00bf:
596 case WM_USER+16:
597 case 0x00c0:
598 case WM_USER+19:
599 case 0x00c3:
600 case WM_USER+26:
601 case 0x00ca:
602 FIXME("undocumented message 0x%x, please report\n", msg);
603 result = DefWindowProcW(hwnd, msg, wParam, lParam);
604 break;
606 case EM_LINELENGTH16:
607 case EM_LINELENGTH:
608 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
609 break;
611 case EM_REPLACESEL16:
612 lParam = (LPARAM)MapSL(lParam);
613 unicode = FALSE; /* 16-bit message is always ascii */
614 /* fall through */
615 case EM_REPLACESEL:
617 LPWSTR textW;
619 if(unicode)
620 textW = (LPWSTR)lParam;
621 else
623 LPSTR textA = (LPSTR)lParam;
624 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
625 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
626 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
629 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
630 result = 1;
632 if(!unicode)
633 HeapFree(GetProcessHeap(), 0, textW);
634 break;
637 case EM_GETLINE16:
638 lParam = (LPARAM)MapSL(lParam);
639 unicode = FALSE; /* 16-bit message is always ascii */
640 /* fall through */
641 case EM_GETLINE:
642 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
643 break;
645 case EM_LIMITTEXT16:
646 case EM_SETLIMITTEXT:
647 EDIT_EM_SetLimitText(es, (INT)wParam);
648 break;
650 case EM_CANUNDO16:
651 case EM_CANUNDO:
652 result = (LRESULT)EDIT_EM_CanUndo(es);
653 break;
655 case EM_UNDO16:
656 case EM_UNDO:
657 case WM_UNDO:
658 result = (LRESULT)EDIT_EM_Undo(es);
659 break;
661 case EM_FMTLINES16:
662 case EM_FMTLINES:
663 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
664 break;
666 case EM_LINEFROMCHAR16:
667 case EM_LINEFROMCHAR:
668 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
669 break;
671 case EM_SETTABSTOPS16:
672 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
673 break;
674 case EM_SETTABSTOPS:
675 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
676 break;
678 case EM_SETPASSWORDCHAR16:
679 unicode = FALSE; /* 16-bit message is always ascii */
680 /* fall through */
681 case EM_SETPASSWORDCHAR:
683 WCHAR charW = 0;
685 if(unicode)
686 charW = (WCHAR)wParam;
687 else
689 CHAR charA = wParam;
690 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
693 EDIT_EM_SetPasswordChar(es, charW);
694 break;
697 case EM_EMPTYUNDOBUFFER16:
698 case EM_EMPTYUNDOBUFFER:
699 EDIT_EM_EmptyUndoBuffer(es);
700 break;
702 case EM_GETFIRSTVISIBLELINE16:
703 result = es->y_offset;
704 break;
705 case EM_GETFIRSTVISIBLELINE:
706 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
707 break;
709 case EM_SETREADONLY16:
710 case EM_SETREADONLY:
711 if (wParam) {
712 SetWindowLongW( hwnd, GWL_STYLE,
713 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
714 es->style |= ES_READONLY;
715 } else {
716 SetWindowLongW( hwnd, GWL_STYLE,
717 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
718 es->style &= ~ES_READONLY;
720 result = 1;
721 break;
723 case EM_SETWORDBREAKPROC16:
724 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
725 break;
726 case EM_SETWORDBREAKPROC:
727 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
728 break;
730 case EM_GETWORDBREAKPROC16:
731 result = (LRESULT)es->word_break_proc16;
732 break;
733 case EM_GETWORDBREAKPROC:
734 result = (LRESULT)es->word_break_proc;
735 break;
737 case EM_GETPASSWORDCHAR16:
738 unicode = FALSE; /* 16-bit message is always ascii */
739 /* fall through */
740 case EM_GETPASSWORDCHAR:
742 if(unicode)
743 result = es->password_char;
744 else
746 WCHAR charW = es->password_char;
747 CHAR charA = 0;
748 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
749 result = charA;
751 break;
754 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
756 case EM_SETMARGINS:
757 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
758 break;
760 case EM_GETMARGINS:
761 result = MAKELONG(es->left_margin, es->right_margin);
762 break;
764 case EM_GETLIMITTEXT:
765 result = es->buffer_limit;
766 break;
768 case EM_POSFROMCHAR:
769 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
770 break;
772 case EM_CHARFROMPOS:
773 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
774 break;
776 /* End of the EM_ messages which were in numerical order; what order
777 * are these in? vaguely alphabetical?
780 case WM_GETDLGCODE:
781 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
783 if (es->style & ES_MULTILINE)
785 result |= DLGC_WANTALLKEYS;
786 break;
789 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
791 int vk = (int)((LPMSG)lParam)->wParam;
793 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
795 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
796 result |= DLGC_WANTMESSAGE;
799 break;
801 case WM_IME_CHAR:
802 if (!unicode)
804 WCHAR charW;
805 CHAR strng[2];
807 strng[0] = wParam >> 8;
808 strng[1] = wParam & 0xff;
809 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
810 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
811 EDIT_WM_Char(es, charW);
812 break;
814 /* fall through */
815 case WM_CHAR:
817 WCHAR charW;
819 if(unicode)
820 charW = wParam;
821 else
823 CHAR charA = wParam;
824 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
827 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
829 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
830 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
831 break;
833 EDIT_WM_Char(es, charW);
834 break;
837 case WM_CLEAR:
838 EDIT_WM_Clear(es);
839 break;
841 case WM_COMMAND:
842 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
843 break;
845 case WM_CONTEXTMENU:
846 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
847 break;
849 case WM_COPY:
850 EDIT_WM_Copy(es);
851 break;
853 case WM_CREATE:
854 if(unicode)
855 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
856 else
858 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
859 LPWSTR nameW = NULL;
860 if(nameA)
862 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
863 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
864 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
866 result = EDIT_WM_Create(es, nameW);
867 if(nameW)
868 HeapFree(GetProcessHeap(), 0, nameW);
870 break;
872 case WM_CUT:
873 EDIT_WM_Cut(es);
874 break;
876 case WM_ENABLE:
877 es->bEnableState = (BOOL) wParam;
878 EDIT_UpdateText(es, NULL, TRUE);
879 break;
881 case WM_ERASEBKGND:
882 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
883 break;
885 case WM_GETFONT:
886 result = (LRESULT)es->font;
887 break;
889 case WM_GETTEXT:
890 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
891 break;
893 case WM_GETTEXTLENGTH:
894 if (unicode) result = strlenW(es->text);
895 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
896 NULL, 0, NULL, NULL );
897 break;
899 case WM_HSCROLL:
900 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
901 break;
903 case WM_KEYDOWN:
904 result = EDIT_WM_KeyDown(es, (INT)wParam);
905 break;
907 case WM_KILLFOCUS:
908 result = EDIT_WM_KillFocus(es);
909 break;
911 case WM_LBUTTONDBLCLK:
912 result = EDIT_WM_LButtonDblClk(es);
913 break;
915 case WM_LBUTTONDOWN:
916 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
917 break;
919 case WM_LBUTTONUP:
920 result = EDIT_WM_LButtonUp(es);
921 break;
923 case WM_MBUTTONDOWN:
924 result = EDIT_WM_MButtonDown(es);
925 break;
927 case WM_MOUSEACTIVATE:
928 result = MA_ACTIVATE;
929 break;
931 case WM_MOUSEMOVE:
932 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
933 break;
935 case WM_PAINT:
936 EDIT_WM_Paint(es, (HDC)wParam);
937 break;
939 case WM_PASTE:
940 EDIT_WM_Paste(es);
941 break;
943 case WM_SETFOCUS:
944 EDIT_WM_SetFocus(es);
945 break;
947 case WM_SETFONT:
948 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
949 break;
951 case WM_SETREDRAW:
952 /* FIXME: actually set an internal flag and behave accordingly */
953 break;
955 case WM_SETTEXT:
956 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
957 result = TRUE;
958 break;
960 case WM_SIZE:
961 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
962 break;
964 case WM_STYLECHANGED:
965 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
966 break;
968 case WM_STYLECHANGING:
969 result = 0; /* See EDIT_WM_StyleChanged */
970 break;
972 case WM_SYSKEYDOWN:
973 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
974 break;
976 case WM_TIMER:
977 EDIT_WM_Timer(es);
978 break;
980 case WM_VSCROLL:
981 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
982 break;
984 case WM_MOUSEWHEEL:
986 int gcWheelDelta = 0;
987 UINT pulScrollLines = 3;
988 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
990 if (wParam & (MK_SHIFT | MK_CONTROL)) {
991 result = DefWindowProcW(hwnd, msg, wParam, lParam);
992 break;
994 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
995 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
997 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
998 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
999 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1002 break;
1003 default:
1004 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1005 break;
1008 if (es) EDIT_UnlockBuffer(es, FALSE);
1010 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1012 return result;
1015 /*********************************************************************
1017 * EditWndProcW (USER32.@)
1019 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1021 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1024 /*********************************************************************
1026 * EditWndProc (USER32.@)
1028 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1030 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1033 /*********************************************************************
1035 * EDIT_BuildLineDefs_ML
1037 * Build linked list of text lines.
1038 * Lines can end with '\0' (last line), a character (if it is wrapped),
1039 * a soft return '\r\r\n' or a hard return '\r\n'
1042 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1044 HDC dc;
1045 HFONT old_font = 0;
1046 LPWSTR current_position, cp;
1047 INT fw;
1048 LINEDEF *current_line;
1049 LINEDEF *previous_line;
1050 LINEDEF *start_line;
1051 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1052 INT line_count = es->line_count;
1053 INT orig_net_length;
1054 RECT rc;
1056 if (istart == iend && delta == 0)
1057 return;
1059 dc = GetDC(es->hwndSelf);
1060 if (es->font)
1061 old_font = SelectObject(dc, es->font);
1063 previous_line = NULL;
1064 current_line = es->first_line_def;
1066 /* Find starting line. istart must lie inside an existing line or
1067 * at the end of buffer */
1068 do {
1069 if (istart < current_line->index + current_line->length ||
1070 current_line->ending == END_0)
1071 break;
1073 previous_line = current_line;
1074 current_line = current_line->next;
1075 line_index++;
1076 } while (current_line);
1078 if (!current_line) /* Error occurred start is not inside previous buffer */
1080 FIXME(" modification occurred outside buffer\n");
1081 ReleaseDC(es->hwndSelf, dc);
1082 return;
1085 /* Remember start of modifications in order to calculate update region */
1086 nstart_line = line_index;
1087 nstart_index = current_line->index;
1089 /* We must start to reformat from the previous line since the modifications
1090 * may have caused the line to wrap upwards. */
1091 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1093 line_index--;
1094 current_line = previous_line;
1096 start_line = current_line;
1098 fw = es->format_rect.right - es->format_rect.left;
1099 current_position = es->text + current_line->index;
1100 do {
1101 if (current_line != start_line)
1103 if (!current_line || current_line->index + delta > current_position - es->text)
1105 /* The buffer has been expanded, create a new line and
1106 insert it into the link list */
1107 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1108 new_line->next = previous_line->next;
1109 previous_line->next = new_line;
1110 current_line = new_line;
1111 es->line_count++;
1113 else if (current_line->index + delta < current_position - es->text)
1115 /* The previous line merged with this line so we delete this extra entry */
1116 previous_line->next = current_line->next;
1117 HeapFree(GetProcessHeap(), 0, current_line);
1118 current_line = previous_line->next;
1119 es->line_count--;
1120 continue;
1122 else /* current_line->index + delta == current_position */
1124 if (current_position - es->text > iend)
1125 break; /* We reached end of line modifications */
1126 /* else recalulate this line */
1130 current_line->index = current_position - es->text;
1131 orig_net_length = current_line->net_length;
1133 /* Find end of line */
1134 cp = current_position;
1135 while (*cp) {
1136 if (*cp == '\n') break;
1137 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1138 break;
1139 cp++;
1142 /* Mark type of line termination */
1143 if (!(*cp)) {
1144 current_line->ending = END_0;
1145 current_line->net_length = strlenW(current_position);
1146 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1147 current_line->ending = END_SOFT;
1148 current_line->net_length = cp - current_position - 1;
1149 } else if (*cp == '\n') {
1150 current_line->ending = END_RICH;
1151 current_line->net_length = cp - current_position;
1152 } else {
1153 current_line->ending = END_HARD;
1154 current_line->net_length = cp - current_position;
1157 /* Calculate line width */
1158 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1159 current_position, current_line->net_length,
1160 es->tabs_count, es->tabs));
1162 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1163 if (!(es->style & ES_AUTOHSCROLL)) {
1164 if (current_line->width > fw) {
1165 INT next = 0;
1166 INT prev;
1167 do {
1168 prev = next;
1169 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1170 prev + 1, current_line->net_length, WB_RIGHT);
1171 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1172 current_position, next, es->tabs_count, es->tabs));
1173 } while (current_line->width <= fw);
1174 if (!prev) { /* Didn't find a line break so force a break */
1175 next = 0;
1176 do {
1177 prev = next;
1178 next++;
1179 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1180 current_position, next, es->tabs_count, es->tabs));
1181 } while (current_line->width <= fw);
1182 if (!prev)
1183 prev = 1;
1186 /* If the first line we are calculating, wrapped before istart, we must
1187 * adjust istart in order for this to be reflected in the update region. */
1188 if (current_line->index == nstart_index && istart > current_line->index + prev)
1189 istart = current_line->index + prev;
1190 /* else if we are updating the previous line before the first line we
1191 * are re-calculating and it expanded */
1192 else if (current_line == start_line &&
1193 current_line->index != nstart_index && orig_net_length < prev)
1195 /* Line expanded due to an upwards line wrap so we must partially include
1196 * previous line in update region */
1197 nstart_line = line_index;
1198 nstart_index = current_line->index;
1199 istart = current_line->index + orig_net_length;
1202 current_line->net_length = prev;
1203 current_line->ending = END_WRAP;
1204 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1205 current_line->net_length, es->tabs_count, es->tabs));
1207 else if (orig_net_length < current_line->net_length &&
1208 current_line == start_line &&
1209 current_line->index != nstart_index) {
1210 /* The previous line expanded but it's still not as wide as the client rect */
1211 /* The expansion is due to an upwards line wrap so we must partially include
1212 it in the update region */
1213 nstart_line = line_index;
1214 nstart_index = current_line->index;
1215 istart = current_line->index + orig_net_length;
1220 /* Adjust length to include line termination */
1221 switch (current_line->ending) {
1222 case END_SOFT:
1223 current_line->length = current_line->net_length + 3;
1224 break;
1225 case END_RICH:
1226 current_line->length = current_line->net_length + 1;
1227 break;
1228 case END_HARD:
1229 current_line->length = current_line->net_length + 2;
1230 break;
1231 case END_WRAP:
1232 case END_0:
1233 current_line->length = current_line->net_length;
1234 break;
1236 es->text_width = max(es->text_width, current_line->width);
1237 current_position += current_line->length;
1238 previous_line = current_line;
1239 current_line = current_line->next;
1240 line_index++;
1241 } while (previous_line->ending != END_0);
1243 /* Finish adjusting line indexes by delta or remove hanging lines */
1244 if (previous_line->ending == END_0)
1246 LINEDEF *pnext = NULL;
1248 previous_line->next = NULL;
1249 while (current_line)
1251 pnext = current_line->next;
1252 HeapFree(GetProcessHeap(), 0, current_line);
1253 current_line = pnext;
1254 es->line_count--;
1257 else
1259 while (current_line)
1261 current_line->index += delta;
1262 current_line = current_line->next;
1266 /* Calculate rest of modification rectangle */
1267 if (hrgn)
1269 HRGN tmphrgn;
1271 * We calculate two rectangles. One for the first line which may have
1272 * an indent with respect to the format rect. The other is a format-width
1273 * rectangle that spans the rest of the lines that changed or moved.
1275 rc.top = es->format_rect.top + nstart_line * es->line_height -
1276 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1277 rc.bottom = rc.top + es->line_height;
1278 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1279 es->text + nstart_index, istart - nstart_index,
1280 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1281 rc.right = es->format_rect.right;
1282 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1284 rc.top = rc.bottom;
1285 rc.left = es->format_rect.left;
1286 rc.right = es->format_rect.right;
1288 * If lines were added or removed we must re-paint the remainder of the
1289 * lines since the remaining lines were either shifted up or down.
1291 if (line_count < es->line_count) /* We added lines */
1292 rc.bottom = es->line_count * es->line_height;
1293 else if (line_count > es->line_count) /* We removed lines */
1294 rc.bottom = line_count * es->line_height;
1295 else
1296 rc.bottom = line_index * es->line_height;
1297 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1298 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1299 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1300 DeleteObject(tmphrgn);
1303 if (es->font)
1304 SelectObject(dc, old_font);
1306 ReleaseDC(es->hwndSelf, dc);
1309 /*********************************************************************
1311 * EDIT_CalcLineWidth_SL
1314 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1316 es->text_width = (short)LOWORD(EDIT_EM_PosFromChar(es, strlenW(es->text), FALSE));
1319 /*********************************************************************
1321 * EDIT_CallWordBreakProc
1323 * Call appropriate WordBreakProc (internal or external).
1325 * Note: The "start" argument should always be an index referring
1326 * to es->text. The actual wordbreak proc might be
1327 * 16 bit, so we can't always pass any 32 bit LPSTR.
1328 * Hence we assume that es->text is the buffer that holds
1329 * the string under examination (we can decide this for ourselves).
1332 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1334 INT ret, iWndsLocks;
1336 /* To avoid any deadlocks, all the locks on the window structures
1337 must be suspended before the control is passed to the application */
1338 iWndsLocks = WIN_SuspendWndsLock();
1340 if (es->word_break_proc16) {
1341 HGLOBAL16 hglob16;
1342 SEGPTR segptr;
1343 INT countA;
1344 WORD args[5];
1345 DWORD result;
1347 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1348 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1349 segptr = K32WOWGlobalLock16(hglob16);
1350 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1351 args[4] = SELECTOROF(segptr);
1352 args[3] = OFFSETOF(segptr);
1353 args[2] = index;
1354 args[1] = countA;
1355 args[0] = action;
1356 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1357 ret = LOWORD(result);
1358 GlobalUnlock16(hglob16);
1359 GlobalFree16(hglob16);
1361 else if (es->word_break_proc)
1363 if(es->is_unicode)
1365 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1367 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1368 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1369 ret = wbpW(es->text + start, index, count, action);
1371 else
1373 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1374 INT countA;
1375 CHAR *textA;
1377 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1378 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1379 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1380 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1381 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1382 ret = wbpA(textA, index, countA, action);
1383 HeapFree(GetProcessHeap(), 0, textA);
1386 else
1387 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1389 WIN_RestoreWndsLock(iWndsLocks);
1390 return ret;
1394 /*********************************************************************
1396 * EDIT_CharFromPos
1398 * Beware: This is not the function called on EM_CHARFROMPOS
1399 * The position _can_ be outside the formatting / client
1400 * rectangle
1401 * The return value is only the character index
1404 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1406 INT index;
1407 HDC dc;
1408 HFONT old_font = 0;
1410 if (es->style & ES_MULTILINE) {
1411 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1412 INT line_index = 0;
1413 LINEDEF *line_def = es->first_line_def;
1414 INT low, high;
1415 while ((line > 0) && line_def->next) {
1416 line_index += line_def->length;
1417 line_def = line_def->next;
1418 line--;
1420 x += es->x_offset - es->format_rect.left;
1421 if (x >= line_def->width) {
1422 if (after_wrap)
1423 *after_wrap = (line_def->ending == END_WRAP);
1424 return line_index + line_def->net_length;
1426 if (x <= 0) {
1427 if (after_wrap)
1428 *after_wrap = FALSE;
1429 return line_index;
1431 dc = GetDC(es->hwndSelf);
1432 if (es->font)
1433 old_font = SelectObject(dc, es->font);
1434 low = line_index + 1;
1435 high = line_index + line_def->net_length + 1;
1436 while (low < high - 1)
1438 INT mid = (low + high) / 2;
1439 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1440 else low = mid;
1442 index = low;
1444 if (after_wrap)
1445 *after_wrap = ((index == line_index + line_def->net_length) &&
1446 (line_def->ending == END_WRAP));
1447 } else {
1448 LPWSTR text;
1449 SIZE size;
1450 if (after_wrap)
1451 *after_wrap = FALSE;
1452 x -= es->format_rect.left;
1453 if (!x)
1454 return es->x_offset;
1455 text = EDIT_GetPasswordPointer_SL(es);
1456 dc = GetDC(es->hwndSelf);
1457 if (es->font)
1458 old_font = SelectObject(dc, es->font);
1459 if (x < 0)
1461 INT low = 0;
1462 INT high = es->x_offset;
1463 while (low < high - 1)
1465 INT mid = (low + high) / 2;
1466 GetTextExtentPoint32W( dc, text + mid,
1467 es->x_offset - mid, &size );
1468 if (size.cx > -x) low = mid;
1469 else high = mid;
1471 index = low;
1473 else
1475 INT low = es->x_offset;
1476 INT high = strlenW(es->text) + 1;
1477 while (low < high - 1)
1479 INT mid = (low + high) / 2;
1480 GetTextExtentPoint32W( dc, text + es->x_offset,
1481 mid - es->x_offset, &size );
1482 if (size.cx > x) high = mid;
1483 else low = mid;
1485 index = low;
1487 if (es->style & ES_PASSWORD)
1488 HeapFree(GetProcessHeap(), 0, text);
1490 if (es->font)
1491 SelectObject(dc, old_font);
1492 ReleaseDC(es->hwndSelf, dc);
1493 return index;
1497 /*********************************************************************
1499 * EDIT_ConfinePoint
1501 * adjusts the point to be within the formatting rectangle
1502 * (so CharFromPos returns the nearest _visible_ character)
1505 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1507 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1508 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1512 /*********************************************************************
1514 * EDIT_GetLineRect
1516 * Calculates the bounding rectangle for a line from a starting
1517 * column to an ending column.
1520 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1522 INT line_index = EDIT_EM_LineIndex(es, line);
1524 if (es->style & ES_MULTILINE)
1525 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1526 else
1527 rc->top = es->format_rect.top;
1528 rc->bottom = rc->top + es->line_height;
1529 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1530 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1534 /*********************************************************************
1536 * EDIT_GetPasswordPointer_SL
1538 * note: caller should free the (optionally) allocated buffer
1541 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1543 if (es->style & ES_PASSWORD) {
1544 INT len = strlenW(es->text);
1545 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1546 text[len] = '\0';
1547 while(len) text[--len] = es->password_char;
1548 return text;
1549 } else
1550 return es->text;
1554 /*********************************************************************
1556 * EDIT_LockBuffer
1558 * This acts as a LOCAL_Lock(), but it locks only once. This way
1559 * you can call it whenever you like, without unlocking.
1561 * Initially the edit control allocates a HLOCAL32 buffer
1562 * (32 bit linear memory handler). However, 16 bit application
1563 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1564 * handler). From that moment on we have to keep using this 16 bit memory
1565 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1566 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1567 * conversion.
1570 static void EDIT_LockBuffer(EDITSTATE *es)
1572 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1573 if (!es->text) {
1574 CHAR *textA = NULL;
1575 UINT countA = 0;
1576 BOOL _16bit = FALSE;
1578 if(es->hloc32W)
1580 if(es->hloc32A)
1582 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1583 textA = LocalLock(es->hloc32A);
1584 countA = strlen(textA) + 1;
1586 else if(es->hloc16)
1588 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1589 textA = LOCAL_Lock(hInstance, es->hloc16);
1590 countA = strlen(textA) + 1;
1591 _16bit = TRUE;
1594 else {
1595 ERR("no buffer ... please report\n");
1596 return;
1599 if(textA)
1601 HLOCAL hloc32W_new;
1602 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1603 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1604 if(countW_new > es->buffer_size + 1)
1606 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1607 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1608 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1609 if(hloc32W_new)
1611 es->hloc32W = hloc32W_new;
1612 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1613 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1615 else
1616 WARN("FAILED! Will synchronize partially\n");
1620 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1621 es->text = LocalLock(es->hloc32W);
1623 if(textA)
1625 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1626 if(_16bit)
1627 LOCAL_Unlock(hInstance, es->hloc16);
1628 else
1629 LocalUnlock(es->hloc32A);
1632 es->lock_count++;
1636 /*********************************************************************
1638 * EDIT_SL_InvalidateText
1640 * Called from EDIT_InvalidateText().
1641 * Does the job for single-line controls only.
1644 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1646 RECT line_rect;
1647 RECT rc;
1649 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1650 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1651 EDIT_UpdateText(es, &rc, TRUE);
1655 /*********************************************************************
1657 * EDIT_ML_InvalidateText
1659 * Called from EDIT_InvalidateText().
1660 * Does the job for multi-line controls only.
1663 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1665 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1666 INT sl = EDIT_EM_LineFromChar(es, start);
1667 INT el = EDIT_EM_LineFromChar(es, end);
1668 INT sc;
1669 INT ec;
1670 RECT rc1;
1671 RECT rcWnd;
1672 RECT rcLine;
1673 RECT rcUpdate;
1674 INT l;
1676 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1677 return;
1679 sc = start - EDIT_EM_LineIndex(es, sl);
1680 ec = end - EDIT_EM_LineIndex(es, el);
1681 if (sl < es->y_offset) {
1682 sl = es->y_offset;
1683 sc = 0;
1685 if (el > es->y_offset + vlc) {
1686 el = es->y_offset + vlc;
1687 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1689 GetClientRect(es->hwndSelf, &rc1);
1690 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1691 if (sl == el) {
1692 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1693 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1694 EDIT_UpdateText(es, &rcUpdate, TRUE);
1695 } else {
1696 EDIT_GetLineRect(es, sl, sc,
1697 EDIT_EM_LineLength(es,
1698 EDIT_EM_LineIndex(es, sl)),
1699 &rcLine);
1700 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1701 EDIT_UpdateText(es, &rcUpdate, TRUE);
1702 for (l = sl + 1 ; l < el ; l++) {
1703 EDIT_GetLineRect(es, l, 0,
1704 EDIT_EM_LineLength(es,
1705 EDIT_EM_LineIndex(es, l)),
1706 &rcLine);
1707 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1708 EDIT_UpdateText(es, &rcUpdate, TRUE);
1710 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1711 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1712 EDIT_UpdateText(es, &rcUpdate, TRUE);
1717 /*********************************************************************
1719 * EDIT_InvalidateText
1721 * Invalidate the text from offset start upto, but not including,
1722 * offset end. Useful for (re)painting the selection.
1723 * Regions outside the linewidth are not invalidated.
1724 * end == -1 means end == TextLength.
1725 * start and end need not be ordered.
1728 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1730 if (end == start)
1731 return;
1733 if (end == -1)
1734 end = strlenW(es->text);
1736 if (end < start) {
1737 INT tmp = start;
1738 start = end;
1739 end = tmp;
1742 if (es->style & ES_MULTILINE)
1743 EDIT_ML_InvalidateText(es, start, end);
1744 else
1745 EDIT_SL_InvalidateText(es, start, end);
1749 /*********************************************************************
1751 * EDIT_MakeFit
1753 * Try to fit size + 1 characters in the buffer.
1755 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1757 HLOCAL hNew32W;
1759 if (size <= es->buffer_size)
1760 return TRUE;
1762 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1764 /* Force edit to unlock it's buffer. es->text now NULL */
1765 EDIT_UnlockBuffer(es, TRUE);
1767 if (es->hloc32W) {
1768 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1769 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1770 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1771 es->hloc32W = hNew32W;
1772 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1776 EDIT_LockBuffer(es);
1778 if (es->buffer_size < size) {
1779 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1780 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1781 return FALSE;
1782 } else {
1783 TRACE("We now have %d+1\n", es->buffer_size);
1784 return TRUE;
1789 /*********************************************************************
1791 * EDIT_MakeUndoFit
1793 * Try to fit size + 1 bytes in the undo buffer.
1796 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1798 UINT alloc_size;
1800 if (size <= es->undo_buffer_size)
1801 return TRUE;
1803 TRACE("trying to ReAlloc to %d+1\n", size);
1805 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1806 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1807 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1808 return TRUE;
1810 else
1812 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1813 return FALSE;
1818 /*********************************************************************
1820 * EDIT_MoveBackward
1823 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1825 INT e = es->selection_end;
1827 if (e) {
1828 e--;
1829 if ((es->style & ES_MULTILINE) && e &&
1830 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1831 e--;
1832 if (e && (es->text[e - 1] == '\r'))
1833 e--;
1836 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1837 EDIT_EM_ScrollCaret(es);
1841 /*********************************************************************
1843 * EDIT_MoveDown_ML
1845 * Only for multi line controls
1846 * Move the caret one line down, on a column with the nearest
1847 * x coordinate on the screen (might be a different column).
1850 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1852 INT s = es->selection_start;
1853 INT e = es->selection_end;
1854 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1855 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1856 INT x = (short)LOWORD(pos);
1857 INT y = (short)HIWORD(pos);
1859 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1860 if (!extend)
1861 s = e;
1862 EDIT_EM_SetSel(es, s, e, after_wrap);
1863 EDIT_EM_ScrollCaret(es);
1867 /*********************************************************************
1869 * EDIT_MoveEnd
1872 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1874 BOOL after_wrap = FALSE;
1875 INT e;
1877 /* Pass a high value in x to make sure of receiving the end of the line */
1878 if (es->style & ES_MULTILINE)
1879 e = EDIT_CharFromPos(es, 0x3fffffff,
1880 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1881 else
1882 e = strlenW(es->text);
1883 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1884 EDIT_EM_ScrollCaret(es);
1888 /*********************************************************************
1890 * EDIT_MoveForward
1893 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1895 INT e = es->selection_end;
1897 if (es->text[e]) {
1898 e++;
1899 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1900 if (es->text[e] == '\n')
1901 e++;
1902 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1903 e += 2;
1906 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1907 EDIT_EM_ScrollCaret(es);
1911 /*********************************************************************
1913 * EDIT_MoveHome
1915 * Home key: move to beginning of line.
1918 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1920 INT e;
1922 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1923 if (es->style & ES_MULTILINE)
1924 e = EDIT_CharFromPos(es, -es->x_offset,
1925 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1926 else
1927 e = 0;
1928 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1929 EDIT_EM_ScrollCaret(es);
1933 /*********************************************************************
1935 * EDIT_MovePageDown_ML
1937 * Only for multi line controls
1938 * Move the caret one page down, on a column with the nearest
1939 * x coordinate on the screen (might be a different column).
1942 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1944 INT s = es->selection_start;
1945 INT e = es->selection_end;
1946 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1947 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1948 INT x = (short)LOWORD(pos);
1949 INT y = (short)HIWORD(pos);
1951 e = EDIT_CharFromPos(es, x,
1952 y + (es->format_rect.bottom - es->format_rect.top),
1953 &after_wrap);
1954 if (!extend)
1955 s = e;
1956 EDIT_EM_SetSel(es, s, e, after_wrap);
1957 EDIT_EM_ScrollCaret(es);
1961 /*********************************************************************
1963 * EDIT_MovePageUp_ML
1965 * Only for multi line controls
1966 * Move the caret one page up, on a column with the nearest
1967 * x coordinate on the screen (might be a different column).
1970 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1972 INT s = es->selection_start;
1973 INT e = es->selection_end;
1974 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1975 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1976 INT x = (short)LOWORD(pos);
1977 INT y = (short)HIWORD(pos);
1979 e = EDIT_CharFromPos(es, x,
1980 y - (es->format_rect.bottom - es->format_rect.top),
1981 &after_wrap);
1982 if (!extend)
1983 s = e;
1984 EDIT_EM_SetSel(es, s, e, after_wrap);
1985 EDIT_EM_ScrollCaret(es);
1989 /*********************************************************************
1991 * EDIT_MoveUp_ML
1993 * Only for multi line controls
1994 * Move the caret one line up, on a column with the nearest
1995 * x coordinate on the screen (might be a different column).
1998 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2000 INT s = es->selection_start;
2001 INT e = es->selection_end;
2002 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2003 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2004 INT x = (short)LOWORD(pos);
2005 INT y = (short)HIWORD(pos);
2007 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2008 if (!extend)
2009 s = e;
2010 EDIT_EM_SetSel(es, s, e, after_wrap);
2011 EDIT_EM_ScrollCaret(es);
2015 /*********************************************************************
2017 * EDIT_MoveWordBackward
2020 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2022 INT s = es->selection_start;
2023 INT e = es->selection_end;
2024 INT l;
2025 INT ll;
2026 INT li;
2028 l = EDIT_EM_LineFromChar(es, e);
2029 ll = EDIT_EM_LineLength(es, e);
2030 li = EDIT_EM_LineIndex(es, l);
2031 if (e - li == 0) {
2032 if (l) {
2033 li = EDIT_EM_LineIndex(es, l - 1);
2034 e = li + EDIT_EM_LineLength(es, li);
2036 } else {
2037 e = li + (INT)EDIT_CallWordBreakProc(es,
2038 li, e - li, ll, WB_LEFT);
2040 if (!extend)
2041 s = e;
2042 EDIT_EM_SetSel(es, s, e, FALSE);
2043 EDIT_EM_ScrollCaret(es);
2047 /*********************************************************************
2049 * EDIT_MoveWordForward
2052 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2054 INT s = es->selection_start;
2055 INT e = es->selection_end;
2056 INT l;
2057 INT ll;
2058 INT li;
2060 l = EDIT_EM_LineFromChar(es, e);
2061 ll = EDIT_EM_LineLength(es, e);
2062 li = EDIT_EM_LineIndex(es, l);
2063 if (e - li == ll) {
2064 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2065 e = EDIT_EM_LineIndex(es, l + 1);
2066 } else {
2067 e = li + EDIT_CallWordBreakProc(es,
2068 li, e - li + 1, ll, WB_RIGHT);
2070 if (!extend)
2071 s = e;
2072 EDIT_EM_SetSel(es, s, e, FALSE);
2073 EDIT_EM_ScrollCaret(es);
2077 /*********************************************************************
2079 * EDIT_PaintLine
2082 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2084 INT s = es->selection_start;
2085 INT e = es->selection_end;
2086 INT li;
2087 INT ll;
2088 INT x;
2089 INT y;
2090 LRESULT pos;
2092 if (es->style & ES_MULTILINE) {
2093 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2094 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2095 return;
2096 } else if (line)
2097 return;
2099 TRACE("line=%d\n", line);
2101 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2102 x = (short)LOWORD(pos);
2103 y = (short)HIWORD(pos);
2104 li = EDIT_EM_LineIndex(es, line);
2105 ll = EDIT_EM_LineLength(es, li);
2106 s = min(es->selection_start, es->selection_end);
2107 e = max(es->selection_start, es->selection_end);
2108 s = min(li + ll, max(li, s));
2109 e = min(li + ll, max(li, e));
2110 if (rev && (s != e) &&
2111 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2112 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2113 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2114 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2115 } else
2116 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2120 /*********************************************************************
2122 * EDIT_PaintText
2125 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2127 COLORREF BkColor;
2128 COLORREF TextColor;
2129 INT ret;
2130 INT li;
2131 INT BkMode;
2132 SIZE size;
2134 if (!count)
2135 return 0;
2136 BkMode = GetBkMode(dc);
2137 BkColor = GetBkColor(dc);
2138 TextColor = GetTextColor(dc);
2139 if (rev) {
2140 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2141 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2142 SetBkMode( dc, OPAQUE);
2144 li = EDIT_EM_LineIndex(es, line);
2145 if (es->style & ES_MULTILINE) {
2146 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2147 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2148 } else {
2149 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2150 TextOutW(dc, x, y, text + li + col, count);
2151 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2152 ret = size.cx;
2153 if (es->style & ES_PASSWORD)
2154 HeapFree(GetProcessHeap(), 0, text);
2156 if (rev) {
2157 SetBkColor(dc, BkColor);
2158 SetTextColor(dc, TextColor);
2159 SetBkMode( dc, BkMode);
2161 return ret;
2165 /*********************************************************************
2167 * EDIT_SetCaretPos
2170 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2171 BOOL after_wrap)
2173 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2174 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2175 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2179 /*********************************************************************
2181 * EDIT_SetRectNP
2183 * note: this is not (exactly) the handler called on EM_SETRECTNP
2184 * it is also used to set the rect of a single line control
2187 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2189 CopyRect(&es->format_rect, rc);
2190 if (es->style & WS_BORDER) {
2191 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2192 es->format_rect.left += bw;
2193 es->format_rect.top += bw;
2194 es->format_rect.right -= bw;
2195 es->format_rect.bottom -= bw;
2197 es->format_rect.left += es->left_margin;
2198 es->format_rect.right -= es->right_margin;
2199 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2200 if (es->style & ES_MULTILINE)
2202 INT fw, vlc, max_x_offset, max_y_offset;
2204 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2205 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2207 /* correct es->x_offset */
2208 fw = es->format_rect.right - es->format_rect.left;
2209 max_x_offset = es->text_width - fw;
2210 if(max_x_offset < 0) max_x_offset = 0;
2211 if(es->x_offset > max_x_offset)
2212 es->x_offset = max_x_offset;
2214 /* correct es->y_offset */
2215 max_y_offset = es->line_count - vlc;
2216 if(max_y_offset < 0) max_y_offset = 0;
2217 if(es->y_offset > max_y_offset)
2218 es->y_offset = max_y_offset;
2220 /* force scroll info update */
2221 EDIT_UpdateScrollInfo(es);
2223 else
2224 /* Windows doesn't care to fix text placement for SL controls */
2225 es->format_rect.bottom = es->format_rect.top + es->line_height;
2227 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2228 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2232 /*********************************************************************
2234 * EDIT_UnlockBuffer
2237 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2239 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2241 /* Edit window might be already destroyed */
2242 if(!IsWindow(es->hwndSelf))
2244 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2245 return;
2248 if (!es->lock_count) {
2249 ERR("lock_count == 0 ... please report\n");
2250 return;
2252 if (!es->text) {
2253 ERR("es->text == 0 ... please report\n");
2254 return;
2257 if (force || (es->lock_count == 1)) {
2258 if (es->hloc32W) {
2259 CHAR *textA = NULL;
2260 BOOL _16bit = FALSE;
2261 UINT countA = 0;
2262 UINT countW = strlenW(es->text) + 1;
2264 if(es->hloc32A)
2266 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2267 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2268 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2269 countA = LocalSize(es->hloc32A);
2270 if(countA_new > countA)
2272 HLOCAL hloc32A_new;
2273 UINT alloc_size = ROUND_TO_GROW(countA_new);
2274 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2275 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2276 if(hloc32A_new)
2278 es->hloc32A = hloc32A_new;
2279 countA = LocalSize(hloc32A_new);
2280 TRACE("Real new size %d bytes\n", countA);
2282 else
2283 WARN("FAILED! Will synchronize partially\n");
2285 textA = LocalLock(es->hloc32A);
2287 else if(es->hloc16)
2289 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2290 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2291 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2292 countA = LOCAL_Size(hInstance, es->hloc16);
2293 if(countA_new > countA)
2295 HLOCAL16 hloc16_new;
2296 UINT alloc_size = ROUND_TO_GROW(countA_new);
2297 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2298 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2299 if(hloc16_new)
2301 es->hloc16 = hloc16_new;
2302 countA = LOCAL_Size(hInstance, hloc16_new);
2303 TRACE("Real new size %d bytes\n", countA);
2305 else
2306 WARN("FAILED! Will synchronize partially\n");
2308 textA = LOCAL_Lock(hInstance, es->hloc16);
2309 _16bit = TRUE;
2312 if(textA)
2314 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2315 if(_16bit)
2316 LOCAL_Unlock(hInstance, es->hloc16);
2317 else
2318 LocalUnlock(es->hloc32A);
2321 LocalUnlock(es->hloc32W);
2322 es->text = NULL;
2324 else {
2325 ERR("no buffer ... please report\n");
2326 return;
2329 es->lock_count--;
2333 /*********************************************************************
2335 * EDIT_UpdateScrollInfo
2338 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2340 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2342 SCROLLINFO si;
2343 si.cbSize = sizeof(SCROLLINFO);
2344 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2345 si.nMin = 0;
2346 si.nMax = es->line_count - 1;
2347 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2348 si.nPos = es->y_offset;
2349 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2350 si.nMin, si.nMax, si.nPage, si.nPos);
2351 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2354 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2356 SCROLLINFO si;
2357 si.cbSize = sizeof(SCROLLINFO);
2358 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2359 si.nMin = 0;
2360 si.nMax = es->text_width - 1;
2361 si.nPage = es->format_rect.right - es->format_rect.left;
2362 si.nPos = es->x_offset;
2363 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2364 si.nMin, si.nMax, si.nPage, si.nPos);
2365 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2369 /*********************************************************************
2371 * EDIT_WordBreakProc
2373 * Find the beginning of words.
2374 * Note: unlike the specs for a WordBreakProc, this function only
2375 * allows to be called without linebreaks between s[0] upto
2376 * s[count - 1]. Remember it is only called
2377 * internally, so we can decide this for ourselves.
2380 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2382 INT ret = 0;
2384 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2386 if(!s) return 0;
2388 switch (action) {
2389 case WB_LEFT:
2390 if (!count)
2391 break;
2392 if (index)
2393 index--;
2394 if (s[index] == ' ') {
2395 while (index && (s[index] == ' '))
2396 index--;
2397 if (index) {
2398 while (index && (s[index] != ' '))
2399 index--;
2400 if (s[index] == ' ')
2401 index++;
2403 } else {
2404 while (index && (s[index] != ' '))
2405 index--;
2406 if (s[index] == ' ')
2407 index++;
2409 ret = index;
2410 break;
2411 case WB_RIGHT:
2412 if (!count)
2413 break;
2414 if (index)
2415 index--;
2416 if (s[index] == ' ')
2417 while ((index < count) && (s[index] == ' ')) index++;
2418 else {
2419 while (s[index] && (s[index] != ' ') && (index < count))
2420 index++;
2421 while ((s[index] == ' ') && (index < count)) index++;
2423 ret = index;
2424 break;
2425 case WB_ISDELIMITER:
2426 ret = (s[index] == ' ');
2427 break;
2428 default:
2429 ERR("unknown action code, please report !\n");
2430 break;
2432 return ret;
2436 /*********************************************************************
2438 * EM_CHARFROMPOS
2440 * returns line number (not index) in high-order word of result.
2441 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2442 * to Richedit, not to the edit control. Original documentation is valid.
2443 * FIXME: do the specs mean to return -1 if outside client area or
2444 * if outside formatting rectangle ???
2447 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2449 POINT pt;
2450 RECT rc;
2451 INT index;
2453 pt.x = x;
2454 pt.y = y;
2455 GetClientRect(es->hwndSelf, &rc);
2456 if (!PtInRect(&rc, pt))
2457 return -1;
2459 index = EDIT_CharFromPos(es, x, y, NULL);
2460 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2464 /*********************************************************************
2466 * EM_FMTLINES
2468 * Enable or disable soft breaks.
2470 * This means: insert or remove the soft linebreak character (\r\r\n).
2471 * Take care to check if the text still fits the buffer after insertion.
2472 * If not, notify with EN_ERRSPACE.
2475 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2477 es->flags &= ~EF_USE_SOFTBRK;
2478 if (add_eol) {
2479 es->flags |= EF_USE_SOFTBRK;
2480 FIXME("soft break enabled, not implemented\n");
2482 return add_eol;
2486 /*********************************************************************
2488 * EM_GETHANDLE
2490 * Hopefully this won't fire back at us.
2491 * We always start with a fixed buffer in the local heap.
2492 * Despite of the documentation says that the local heap is used
2493 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2494 * buffer on the local heap.
2497 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2499 HLOCAL hLocal;
2501 if (!(es->style & ES_MULTILINE))
2502 return 0;
2504 if(es->is_unicode)
2505 hLocal = es->hloc32W;
2506 else
2508 if(!es->hloc32A)
2510 CHAR *textA;
2511 UINT countA, alloc_size;
2512 TRACE("Allocating 32-bit ANSI alias buffer\n");
2513 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2514 alloc_size = ROUND_TO_GROW(countA);
2515 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2517 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2518 return 0;
2520 textA = LocalLock(es->hloc32A);
2521 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2522 LocalUnlock(es->hloc32A);
2524 hLocal = es->hloc32A;
2527 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2528 return hLocal;
2532 /*********************************************************************
2534 * EM_GETHANDLE16
2536 * Hopefully this won't fire back at us.
2537 * We always start with a buffer in 32 bit linear memory.
2538 * However, with this message a 16 bit application requests
2539 * a handle of 16 bit local heap memory, where it expects to find
2540 * the text.
2541 * It's a pitty that from this moment on we have to use this
2542 * local heap, because applications may rely on the handle
2543 * in the future.
2545 * In this function we'll try to switch to local heap.
2547 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2549 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2550 CHAR *textA;
2551 UINT countA, alloc_size;
2553 if (!(es->style & ES_MULTILINE))
2554 return 0;
2556 if (es->hloc16)
2557 return es->hloc16;
2559 if (!LOCAL_HeapSize(hInstance)) {
2560 if (!LocalInit16(hInstance, 0,
2561 GlobalSize16(hInstance))) {
2562 ERR("could not initialize local heap\n");
2563 return 0;
2565 TRACE("local heap initialized\n");
2568 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2569 alloc_size = ROUND_TO_GROW(countA);
2571 TRACE("Allocating 16-bit ANSI alias buffer\n");
2572 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2573 ERR("could not allocate new 16 bit buffer\n");
2574 return 0;
2577 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2578 ERR("could not lock new 16 bit buffer\n");
2579 LOCAL_Free(hInstance, es->hloc16);
2580 es->hloc16 = 0;
2581 return 0;
2584 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2585 LOCAL_Unlock(hInstance, es->hloc16);
2587 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2588 return es->hloc16;
2592 /*********************************************************************
2594 * EM_GETLINE
2597 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2599 LPWSTR src;
2600 INT line_len, dst_len;
2601 INT i;
2603 if (es->style & ES_MULTILINE) {
2604 if (line >= es->line_count)
2605 return 0;
2606 } else
2607 line = 0;
2608 i = EDIT_EM_LineIndex(es, line);
2609 src = es->text + i;
2610 line_len = EDIT_EM_LineLength(es, i);
2611 dst_len = *(WORD *)dst;
2612 if(unicode)
2614 if(dst_len <= line_len)
2616 memcpy(dst, src, dst_len * sizeof(WCHAR));
2617 return dst_len;
2619 else /* Append 0 if enough space */
2621 memcpy(dst, src, line_len * sizeof(WCHAR));
2622 dst[line_len] = 0;
2623 return line_len;
2626 else
2628 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2629 if(!ret) /* Insufficient buffer size */
2630 return dst_len;
2631 if(ret < dst_len) /* Append 0 if enough space */
2632 ((LPSTR)dst)[ret] = 0;
2633 return ret;
2638 /*********************************************************************
2640 * EM_GETSEL
2643 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2645 UINT s = es->selection_start;
2646 UINT e = es->selection_end;
2648 ORDER_UINT(s, e);
2649 if (start)
2650 *start = s;
2651 if (end)
2652 *end = e;
2653 return MAKELONG(s, e);
2657 /*********************************************************************
2659 * EM_GETTHUMB
2661 * FIXME: is this right ? (or should it be only VSCROLL)
2662 * (and maybe only for edit controls that really have their
2663 * own scrollbars) (and maybe only for multiline controls ?)
2664 * All in all: very poorly documented
2667 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2669 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2670 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2674 /*********************************************************************
2676 * EM_LINEFROMCHAR
2679 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2681 INT line;
2682 LINEDEF *line_def;
2684 if (!(es->style & ES_MULTILINE))
2685 return 0;
2686 if (index > (INT)strlenW(es->text))
2687 return es->line_count - 1;
2688 if (index == -1)
2689 index = min(es->selection_start, es->selection_end);
2691 line = 0;
2692 line_def = es->first_line_def;
2693 index -= line_def->length;
2694 while ((index >= 0) && line_def->next) {
2695 line++;
2696 line_def = line_def->next;
2697 index -= line_def->length;
2699 return line;
2703 /*********************************************************************
2705 * EM_LINEINDEX
2708 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2710 INT line_index;
2711 LINEDEF *line_def;
2713 if (!(es->style & ES_MULTILINE))
2714 return 0;
2715 if (line >= es->line_count)
2716 return -1;
2718 line_index = 0;
2719 line_def = es->first_line_def;
2720 if (line == -1) {
2721 INT index = es->selection_end - line_def->length;
2722 while ((index >= 0) && line_def->next) {
2723 line_index += line_def->length;
2724 line_def = line_def->next;
2725 index -= line_def->length;
2727 } else {
2728 while (line > 0) {
2729 line_index += line_def->length;
2730 line_def = line_def->next;
2731 line--;
2734 return line_index;
2738 /*********************************************************************
2740 * EM_LINELENGTH
2743 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2745 LINEDEF *line_def;
2747 if (!(es->style & ES_MULTILINE))
2748 return strlenW(es->text);
2750 if (index == -1) {
2751 /* get the number of remaining non-selected chars of selected lines */
2752 INT32 l; /* line number */
2753 INT32 li; /* index of first char in line */
2754 INT32 count;
2755 l = EDIT_EM_LineFromChar(es, es->selection_start);
2756 /* # chars before start of selection area */
2757 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2758 l = EDIT_EM_LineFromChar(es, es->selection_end);
2759 /* # chars after end of selection */
2760 li = EDIT_EM_LineIndex(es, l);
2761 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2762 return count;
2764 line_def = es->first_line_def;
2765 index -= line_def->length;
2766 while ((index >= 0) && line_def->next) {
2767 line_def = line_def->next;
2768 index -= line_def->length;
2770 return line_def->net_length;
2774 /*********************************************************************
2776 * EM_LINESCROLL
2778 * NOTE: dx is in average character widths, dy - in lines;
2781 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2783 if (!(es->style & ES_MULTILINE))
2784 return FALSE;
2786 dx *= es->char_width;
2787 return EDIT_EM_LineScroll_internal(es, dx, dy);
2790 /*********************************************************************
2792 * EDIT_EM_LineScroll_internal
2794 * Version of EDIT_EM_LineScroll for internal use.
2795 * It doesn't refuse if ES_MULTILINE is set and assumes that
2796 * dx is in pixels, dy - in lines.
2799 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2801 INT nyoff;
2802 INT x_offset_in_pixels;
2803 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2804 es->line_height;
2806 if (es->style & ES_MULTILINE)
2808 x_offset_in_pixels = es->x_offset;
2810 else
2812 dy = 0;
2813 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2816 if (-dx > x_offset_in_pixels)
2817 dx = -x_offset_in_pixels;
2818 if (dx > es->text_width - x_offset_in_pixels)
2819 dx = es->text_width - x_offset_in_pixels;
2820 nyoff = max(0, es->y_offset + dy);
2821 if (nyoff >= es->line_count - lines_per_page)
2822 nyoff = es->line_count - lines_per_page;
2823 dy = (es->y_offset - nyoff) * es->line_height;
2824 if (dx || dy) {
2825 RECT rc1;
2826 RECT rc;
2828 es->y_offset = nyoff;
2829 if(es->style & ES_MULTILINE)
2830 es->x_offset += dx;
2831 else
2832 es->x_offset += dx / es->char_width;
2834 GetClientRect(es->hwndSelf, &rc1);
2835 IntersectRect(&rc, &rc1, &es->format_rect);
2836 ScrollWindowEx(es->hwndSelf, -dx, dy,
2837 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2838 /* force scroll info update */
2839 EDIT_UpdateScrollInfo(es);
2841 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2842 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2843 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2844 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2845 return TRUE;
2849 /*********************************************************************
2851 * EM_POSFROMCHAR
2854 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2856 INT len = strlenW(es->text);
2857 INT l;
2858 INT li;
2859 INT x;
2860 INT y = 0;
2861 HDC dc;
2862 HFONT old_font = 0;
2863 SIZE size;
2865 index = min(index, len);
2866 dc = GetDC(es->hwndSelf);
2867 if (es->font)
2868 old_font = SelectObject(dc, es->font);
2869 if (es->style & ES_MULTILINE) {
2870 l = EDIT_EM_LineFromChar(es, index);
2871 y = (l - es->y_offset) * es->line_height;
2872 li = EDIT_EM_LineIndex(es, l);
2873 if (after_wrap && (li == index) && l) {
2874 INT l2 = l - 1;
2875 LINEDEF *line_def = es->first_line_def;
2876 while (l2) {
2877 line_def = line_def->next;
2878 l2--;
2880 if (line_def->ending == END_WRAP) {
2881 l--;
2882 y -= es->line_height;
2883 li = EDIT_EM_LineIndex(es, l);
2886 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2887 es->tabs_count, es->tabs)) - es->x_offset;
2888 } else {
2889 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2890 if (index < es->x_offset) {
2891 GetTextExtentPoint32W(dc, text + index,
2892 es->x_offset - index, &size);
2893 x = -size.cx;
2894 } else {
2895 GetTextExtentPoint32W(dc, text + es->x_offset,
2896 index - es->x_offset, &size);
2897 x = size.cx;
2899 y = 0;
2900 if (es->style & ES_PASSWORD)
2901 HeapFree(GetProcessHeap(), 0, text);
2903 x += es->format_rect.left;
2904 y += es->format_rect.top;
2905 if (es->font)
2906 SelectObject(dc, old_font);
2907 ReleaseDC(es->hwndSelf, dc);
2908 return MAKELONG((INT16)x, (INT16)y);
2912 /*********************************************************************
2914 * EM_REPLACESEL
2916 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2919 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2921 UINT strl = strlenW(lpsz_replace);
2922 UINT tl = strlenW(es->text);
2923 UINT utl;
2924 UINT s;
2925 UINT e;
2926 UINT i;
2927 UINT size;
2928 LPWSTR p;
2929 HRGN hrgn = 0;
2931 TRACE("%s, can_undo %d, send_update %d\n",
2932 debugstr_w(lpsz_replace), can_undo, send_update);
2934 s = es->selection_start;
2935 e = es->selection_end;
2937 if ((s == e) && !strl)
2938 return;
2940 ORDER_UINT(s, e);
2942 /* Issue the EN_MAXTEXT notification and continue with replacing text
2943 * such that buffer limit is honored. */
2944 size = tl - (e - s) + strl;
2945 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
2946 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
2947 strl = es->buffer_limit - (tl - (e-s));
2950 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2951 return;
2953 if (e != s) {
2954 /* there is something to be deleted */
2955 TRACE("deleting stuff.\n");
2956 if (can_undo) {
2957 utl = strlenW(es->undo_text);
2958 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2959 /* undo-buffer is extended to the right */
2960 EDIT_MakeUndoFit(es, utl + e - s);
2961 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
2962 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2963 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2964 /* undo-buffer is extended to the left */
2965 EDIT_MakeUndoFit(es, utl + e - s);
2966 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2967 p[e - s] = p[0];
2968 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2969 p[i] = (es->text + s)[i];
2970 es->undo_position = s;
2971 } else {
2972 /* new undo-buffer */
2973 EDIT_MakeUndoFit(es, e - s);
2974 strncpyW(es->undo_text, es->text + s, e - s + 1);
2975 es->undo_text[e - s] = 0; /* ensure 0 termination */
2976 es->undo_position = s;
2978 /* any deletion makes the old insertion-undo invalid */
2979 es->undo_insert_count = 0;
2980 } else
2981 EDIT_EM_EmptyUndoBuffer(es);
2983 /* now delete */
2984 strcpyW(es->text + s, es->text + e);
2986 if (strl) {
2987 /* there is an insertion */
2988 if (can_undo) {
2989 if ((s == es->undo_position) ||
2990 ((es->undo_insert_count) &&
2991 (s == es->undo_position + es->undo_insert_count)))
2993 * insertion is new and at delete position or
2994 * an extension to either left or right
2996 es->undo_insert_count += strl;
2997 else {
2998 /* new insertion undo */
2999 es->undo_position = s;
3000 es->undo_insert_count = strl;
3001 /* new insertion makes old delete-buffer invalid */
3002 *es->undo_text = '\0';
3004 } else
3005 EDIT_EM_EmptyUndoBuffer(es);
3007 /* now insert */
3008 tl = strlenW(es->text);
3009 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));
3010 for (p = es->text + tl ; p >= es->text + s ; p--)
3011 p[strl] = p[0];
3012 for (i = 0 , p = es->text + s ; i < strl ; i++)
3013 p[i] = lpsz_replace[i];
3014 if(es->style & ES_UPPERCASE)
3015 CharUpperBuffW(p, strl);
3016 else if(es->style & ES_LOWERCASE)
3017 CharLowerBuffW(p, strl);
3018 s += strl;
3020 if (es->style & ES_MULTILINE)
3022 INT s = min(es->selection_start, es->selection_end);
3024 hrgn = CreateRectRgn(0, 0, 0, 0);
3025 EDIT_BuildLineDefs_ML(es, s, s + strl,
3026 strl - abs(es->selection_end - es->selection_start), hrgn);
3028 else
3029 EDIT_CalcLineWidth_SL(es);
3031 EDIT_EM_SetSel(es, s, s, FALSE);
3032 es->flags |= EF_MODIFIED;
3033 if (send_update) es->flags |= EF_UPDATE;
3034 if (hrgn)
3036 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3037 DeleteObject(hrgn);
3039 else
3040 EDIT_UpdateText(es, NULL, TRUE);
3042 EDIT_EM_ScrollCaret(es);
3044 /* force scroll info update */
3045 EDIT_UpdateScrollInfo(es);
3048 if(es->flags & EF_UPDATE)
3050 es->flags &= ~EF_UPDATE;
3051 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3056 /*********************************************************************
3058 * EM_SCROLL
3061 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3063 INT dy;
3065 if (!(es->style & ES_MULTILINE))
3066 return (LRESULT)FALSE;
3068 dy = 0;
3070 switch (action) {
3071 case SB_LINEUP:
3072 if (es->y_offset)
3073 dy = -1;
3074 break;
3075 case SB_LINEDOWN:
3076 if (es->y_offset < es->line_count - 1)
3077 dy = 1;
3078 break;
3079 case SB_PAGEUP:
3080 if (es->y_offset)
3081 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3082 break;
3083 case SB_PAGEDOWN:
3084 if (es->y_offset < es->line_count - 1)
3085 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3086 break;
3087 default:
3088 return (LRESULT)FALSE;
3090 if (dy) {
3091 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3092 /* check if we are going to move too far */
3093 if(es->y_offset + dy > es->line_count - vlc)
3094 dy = es->line_count - vlc - es->y_offset;
3096 /* Notification is done in EDIT_EM_LineScroll */
3097 if(dy)
3098 EDIT_EM_LineScroll(es, 0, dy);
3100 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3104 /*********************************************************************
3106 * EM_SCROLLCARET
3109 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3111 if (es->style & ES_MULTILINE) {
3112 INT l;
3113 INT li;
3114 INT vlc;
3115 INT ww;
3116 INT cw = es->char_width;
3117 INT x;
3118 INT dy = 0;
3119 INT dx = 0;
3121 l = EDIT_EM_LineFromChar(es, es->selection_end);
3122 li = EDIT_EM_LineIndex(es, l);
3123 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3124 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3125 if (l >= es->y_offset + vlc)
3126 dy = l - vlc + 1 - es->y_offset;
3127 if (l < es->y_offset)
3128 dy = l - es->y_offset;
3129 ww = es->format_rect.right - es->format_rect.left;
3130 if (x < es->format_rect.left)
3131 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3132 if (x > es->format_rect.right)
3133 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3134 if (dy || dx)
3136 /* check if we are going to move too far */
3137 if(es->x_offset + dx + ww > es->text_width)
3138 dx = es->text_width - ww - es->x_offset;
3139 if(dx || dy)
3140 EDIT_EM_LineScroll_internal(es, dx, dy);
3142 } else {
3143 INT x;
3144 INT goal;
3145 INT format_width;
3147 if (!(es->style & ES_AUTOHSCROLL))
3148 return;
3150 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3151 format_width = es->format_rect.right - es->format_rect.left;
3152 if (x < es->format_rect.left) {
3153 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3154 do {
3155 es->x_offset--;
3156 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3157 } while ((x < goal) && es->x_offset);
3158 /* FIXME: use ScrollWindow() somehow to improve performance */
3159 EDIT_UpdateText(es, NULL, TRUE);
3160 } else if (x > es->format_rect.right) {
3161 INT x_last;
3162 INT len = strlenW(es->text);
3163 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3164 do {
3165 es->x_offset++;
3166 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3167 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3168 } while ((x > goal) && (x_last > es->format_rect.right));
3169 /* FIXME: use ScrollWindow() somehow to improve performance */
3170 EDIT_UpdateText(es, NULL, TRUE);
3174 if(es->flags & EF_FOCUSED)
3175 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3179 /*********************************************************************
3181 * EM_SETHANDLE
3183 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3186 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3188 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3190 if (!(es->style & ES_MULTILINE))
3191 return;
3193 if (!hloc) {
3194 WARN("called with NULL handle\n");
3195 return;
3198 EDIT_UnlockBuffer(es, TRUE);
3200 if(es->hloc16)
3202 LOCAL_Free(hInstance, es->hloc16);
3203 es->hloc16 = (HLOCAL16)NULL;
3206 if(es->is_unicode)
3208 if(es->hloc32A)
3210 LocalFree(es->hloc32A);
3211 es->hloc32A = NULL;
3213 es->hloc32W = hloc;
3215 else
3217 INT countW, countA;
3218 HLOCAL hloc32W_new;
3219 WCHAR *textW;
3220 CHAR *textA;
3222 countA = LocalSize(hloc);
3223 textA = LocalLock(hloc);
3224 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3225 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3227 ERR("Could not allocate new unicode buffer\n");
3228 return;
3230 textW = LocalLock(hloc32W_new);
3231 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3232 LocalUnlock(hloc32W_new);
3233 LocalUnlock(hloc);
3235 if(es->hloc32W)
3236 LocalFree(es->hloc32W);
3238 es->hloc32W = hloc32W_new;
3239 es->hloc32A = hloc;
3242 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3244 EDIT_LockBuffer(es);
3246 es->x_offset = es->y_offset = 0;
3247 es->selection_start = es->selection_end = 0;
3248 EDIT_EM_EmptyUndoBuffer(es);
3249 es->flags &= ~EF_MODIFIED;
3250 es->flags &= ~EF_UPDATE;
3251 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3252 EDIT_UpdateText(es, NULL, TRUE);
3253 EDIT_EM_ScrollCaret(es);
3254 /* force scroll info update */
3255 EDIT_UpdateScrollInfo(es);
3259 /*********************************************************************
3261 * EM_SETHANDLE16
3263 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3266 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3268 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3269 INT countW, countA;
3270 HLOCAL hloc32W_new;
3271 WCHAR *textW;
3272 CHAR *textA;
3274 if (!(es->style & ES_MULTILINE))
3275 return;
3277 if (!hloc) {
3278 WARN("called with NULL handle\n");
3279 return;
3282 EDIT_UnlockBuffer(es, TRUE);
3284 if(es->hloc32A)
3286 LocalFree(es->hloc32A);
3287 es->hloc32A = NULL;
3290 countA = LOCAL_Size(hInstance, hloc);
3291 textA = LOCAL_Lock(hInstance, hloc);
3292 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3293 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3295 ERR("Could not allocate new unicode buffer\n");
3296 return;
3298 textW = LocalLock(hloc32W_new);
3299 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3300 LocalUnlock(hloc32W_new);
3301 LOCAL_Unlock(hInstance, hloc);
3303 if(es->hloc32W)
3304 LocalFree(es->hloc32W);
3306 es->hloc32W = hloc32W_new;
3307 es->hloc16 = hloc;
3309 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3311 EDIT_LockBuffer(es);
3313 es->x_offset = es->y_offset = 0;
3314 es->selection_start = es->selection_end = 0;
3315 EDIT_EM_EmptyUndoBuffer(es);
3316 es->flags &= ~EF_MODIFIED;
3317 es->flags &= ~EF_UPDATE;
3318 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3319 EDIT_UpdateText(es, NULL, TRUE);
3320 EDIT_EM_ScrollCaret(es);
3321 /* force scroll info update */
3322 EDIT_UpdateScrollInfo(es);
3326 /*********************************************************************
3328 * EM_SETLIMITTEXT
3330 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3331 * However, the windows version is not complied to yet in all of edit.c
3333 * Additionally as the wrapper for RichEdit controls we need larger buffers
3334 * at present -1 will represent nolimit
3336 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3338 if (limit == 0xFFFFFFFF)
3339 es->buffer_limit = -1;
3340 else if (es->style & ES_MULTILINE) {
3341 if (limit)
3342 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3343 else
3344 es->buffer_limit = BUFLIMIT_MULTI;
3345 } else {
3346 if (limit)
3347 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3348 else
3349 es->buffer_limit = BUFLIMIT_SINGLE;
3354 /*********************************************************************
3356 * EM_SETMARGINS
3358 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3359 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3360 * margin according to the textmetrics of the current font.
3362 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3363 * of the char's width as the margin, but this is not how Windows handles this.
3364 * For all other fonts Windows sets the margins to zero.
3367 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3368 INT left, INT right)
3370 TEXTMETRICW tm;
3371 INT default_left_margin = 0; /* in pixels */
3372 INT default_right_margin = 0; /* in pixels */
3374 /* Set the default margins depending on the font */
3375 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3376 HDC dc = GetDC(es->hwndSelf);
3377 HFONT old_font = SelectObject(dc, es->font);
3378 GetTextMetricsW(dc, &tm);
3379 /* The default margins are only non zero for TrueType or Vector fonts */
3380 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3381 /* This must be calculated more exactly! But how? */
3382 default_left_margin = tm.tmAveCharWidth / 3;
3383 default_right_margin = tm.tmAveCharWidth / 3;
3385 SelectObject(dc, old_font);
3386 ReleaseDC(es->hwndSelf, dc);
3389 if (action & EC_LEFTMARGIN) {
3390 if (left != EC_USEFONTINFO)
3391 es->left_margin = left;
3392 else
3393 es->left_margin = default_left_margin;
3396 if (action & EC_RIGHTMARGIN) {
3397 if (right != EC_USEFONTINFO)
3398 es->right_margin = right;
3399 else
3400 es->right_margin = default_right_margin;
3402 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3406 /*********************************************************************
3408 * EM_SETPASSWORDCHAR
3411 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3413 LONG style;
3415 if (es->style & ES_MULTILINE)
3416 return;
3418 if (es->password_char == c)
3419 return;
3421 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3422 es->password_char = c;
3423 if (c) {
3424 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3425 es->style |= ES_PASSWORD;
3426 } else {
3427 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3428 es->style &= ~ES_PASSWORD;
3430 EDIT_UpdateText(es, NULL, TRUE);
3434 /*********************************************************************
3436 * EDIT_EM_SetSel
3438 * note: unlike the specs say: the order of start and end
3439 * _is_ preserved in Windows. (i.e. start can be > end)
3440 * In other words: this handler is OK
3443 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3445 UINT old_start = es->selection_start;
3446 UINT old_end = es->selection_end;
3447 UINT len = strlenW(es->text);
3449 if (start == (UINT)-1) {
3450 start = es->selection_end;
3451 end = es->selection_end;
3452 } else {
3453 start = min(start, len);
3454 end = min(end, len);
3456 es->selection_start = start;
3457 es->selection_end = end;
3458 if (after_wrap)
3459 es->flags |= EF_AFTER_WRAP;
3460 else
3461 es->flags &= ~EF_AFTER_WRAP;
3462 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3463 ORDER_UINT(start, end);
3464 ORDER_UINT(end, old_end);
3465 ORDER_UINT(start, old_start);
3466 ORDER_UINT(old_start, old_end);
3467 if (end != old_start)
3470 * One can also do
3471 * ORDER_UINT32(end, old_start);
3472 * EDIT_InvalidateText(es, start, end);
3473 * EDIT_InvalidateText(es, old_start, old_end);
3474 * in place of the following if statement.
3476 if (old_start > end )
3478 EDIT_InvalidateText(es, start, end);
3479 EDIT_InvalidateText(es, old_start, old_end);
3481 else
3483 EDIT_InvalidateText(es, start, old_start);
3484 EDIT_InvalidateText(es, end, old_end);
3487 else EDIT_InvalidateText(es, start, old_end);
3491 /*********************************************************************
3493 * EM_SETTABSTOPS
3496 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3498 if (!(es->style & ES_MULTILINE))
3499 return FALSE;
3500 if (es->tabs)
3501 HeapFree(GetProcessHeap(), 0, es->tabs);
3502 es->tabs_count = count;
3503 if (!count)
3504 es->tabs = NULL;
3505 else {
3506 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3507 memcpy(es->tabs, tabs, count * sizeof(INT));
3509 return TRUE;
3513 /*********************************************************************
3515 * EM_SETTABSTOPS16
3518 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3520 if (!(es->style & ES_MULTILINE))
3521 return FALSE;
3522 if (es->tabs)
3523 HeapFree(GetProcessHeap(), 0, es->tabs);
3524 es->tabs_count = count;
3525 if (!count)
3526 es->tabs = NULL;
3527 else {
3528 INT i;
3529 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3530 for (i = 0 ; i < count ; i++)
3531 es->tabs[i] = *tabs++;
3533 return TRUE;
3537 /*********************************************************************
3539 * EM_SETWORDBREAKPROC
3542 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3544 if (es->word_break_proc == wbp)
3545 return;
3547 es->word_break_proc = wbp;
3548 es->word_break_proc16 = NULL;
3550 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3551 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3552 EDIT_UpdateText(es, NULL, TRUE);
3557 /*********************************************************************
3559 * EM_SETWORDBREAKPROC16
3562 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3564 if (es->word_break_proc16 == wbp)
3565 return;
3567 es->word_break_proc = NULL;
3568 es->word_break_proc16 = wbp;
3569 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3570 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3571 EDIT_UpdateText(es, NULL, TRUE);
3576 /*********************************************************************
3578 * EM_UNDO / WM_UNDO
3581 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3583 INT ulength;
3584 LPWSTR utext;
3586 /* As per MSDN spec, for a single-line edit control,
3587 the return value is always TRUE */
3588 if( es->style & ES_READONLY )
3589 return !(es->style & ES_MULTILINE);
3591 ulength = strlenW(es->undo_text);
3593 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3595 strcpyW(utext, es->undo_text);
3597 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3598 es->undo_insert_count, debugstr_w(utext));
3600 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3601 EDIT_EM_EmptyUndoBuffer(es);
3602 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3603 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3604 /* send the notification after the selection start and end are set */
3605 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3606 EDIT_EM_ScrollCaret(es);
3607 HeapFree(GetProcessHeap(), 0, utext);
3609 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3610 es->undo_insert_count, debugstr_w(es->undo_text));
3611 return TRUE;
3615 /*********************************************************************
3617 * WM_CHAR
3620 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3622 BOOL control;
3624 control = GetKeyState(VK_CONTROL) & 0x8000;
3626 switch (c) {
3627 case '\r':
3628 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3629 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3630 break;
3631 case '\n':
3632 if (es->style & ES_MULTILINE) {
3633 if (es->style & ES_READONLY) {
3634 EDIT_MoveHome(es, FALSE);
3635 EDIT_MoveDown_ML(es, FALSE);
3636 } else {
3637 static const WCHAR cr_lfW[] = {'\r','\n',0};
3638 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3641 break;
3642 case '\t':
3643 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3645 static const WCHAR tabW[] = {'\t',0};
3646 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3648 break;
3649 case VK_BACK:
3650 if (!(es->style & ES_READONLY) && !control) {
3651 if (es->selection_start != es->selection_end)
3652 EDIT_WM_Clear(es);
3653 else {
3654 /* delete character left of caret */
3655 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3656 EDIT_MoveBackward(es, TRUE);
3657 EDIT_WM_Clear(es);
3660 break;
3661 case 0x03: /* ^C */
3662 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3663 break;
3664 case 0x16: /* ^V */
3665 if (!(es->style & ES_READONLY))
3666 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3667 break;
3668 case 0x18: /* ^X */
3669 if (!(es->style & ES_READONLY))
3670 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3671 break;
3673 default:
3674 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3675 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3676 break;
3678 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3679 WCHAR str[2];
3680 str[0] = c;
3681 str[1] = '\0';
3682 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3684 break;
3689 /*********************************************************************
3691 * WM_COMMAND
3694 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3696 if (code || control)
3697 return;
3699 switch (id) {
3700 case EM_UNDO:
3701 EDIT_EM_Undo(es);
3702 break;
3703 case WM_CUT:
3704 EDIT_WM_Cut(es);
3705 break;
3706 case WM_COPY:
3707 EDIT_WM_Copy(es);
3708 break;
3709 case WM_PASTE:
3710 EDIT_WM_Paste(es);
3711 break;
3712 case WM_CLEAR:
3713 EDIT_WM_Clear(es);
3714 break;
3715 case EM_SETSEL:
3716 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3717 EDIT_EM_ScrollCaret(es);
3718 break;
3719 default:
3720 ERR("unknown menu item, please report\n");
3721 break;
3726 /*********************************************************************
3728 * WM_CONTEXTMENU
3730 * Note: the resource files resource/sysres_??.rc cannot define a
3731 * single popup menu. Hence we use a (dummy) menubar
3732 * containing the single popup menu as its first item.
3734 * FIXME: the message identifiers have been chosen arbitrarily,
3735 * hence we use MF_BYPOSITION.
3736 * We might as well use the "real" values (anybody knows ?)
3737 * The menu definition is in resources/sysres_??.rc.
3738 * Once these are OK, we better use MF_BYCOMMAND here
3739 * (as we do in EDIT_WM_Command()).
3742 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3744 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3745 HMENU popup = GetSubMenu(menu, 0);
3746 UINT start = es->selection_start;
3747 UINT end = es->selection_end;
3749 ORDER_UINT(start, end);
3751 /* undo */
3752 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3753 /* cut */
3754 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3755 /* copy */
3756 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3757 /* paste */
3758 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3759 /* delete */
3760 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3761 /* select all */
3762 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3764 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3765 DestroyMenu(menu);
3769 /*********************************************************************
3771 * WM_COPY
3774 static void EDIT_WM_Copy(EDITSTATE *es)
3776 INT s = min(es->selection_start, es->selection_end);
3777 INT e = max(es->selection_start, es->selection_end);
3778 HGLOBAL hdst;
3779 LPWSTR dst;
3781 if (e == s) return;
3783 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3784 dst = GlobalLock(hdst);
3785 strncpyW(dst, es->text + s, e - s);
3786 dst[e - s] = 0; /* ensure 0 termination */
3787 TRACE("%s\n", debugstr_w(dst));
3788 GlobalUnlock(hdst);
3789 OpenClipboard(es->hwndSelf);
3790 EmptyClipboard();
3791 SetClipboardData(CF_UNICODETEXT, hdst);
3792 CloseClipboard();
3796 /*********************************************************************
3798 * WM_CREATE
3801 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3803 TRACE("%s\n", debugstr_w(name));
3805 * To initialize some final structure members, we call some helper
3806 * functions. However, since the EDITSTATE is not consistent (i.e.
3807 * not fully initialized), we should be very careful which
3808 * functions can be called, and in what order.
3810 EDIT_WM_SetFont(es, 0, FALSE);
3811 EDIT_EM_EmptyUndoBuffer(es);
3813 if (name && *name) {
3814 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3815 /* if we insert text to the editline, the text scrolls out
3816 * of the window, as the caret is placed after the insert
3817 * pos normally; thus we reset es->selection... to 0 and
3818 * update caret
3820 es->selection_start = es->selection_end = 0;
3821 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3822 * Messages are only to be sent when the USER does something to
3823 * change the contents. So I am removing this EN_CHANGE
3825 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3827 EDIT_EM_ScrollCaret(es);
3829 /* force scroll info update */
3830 EDIT_UpdateScrollInfo(es);
3831 /* The rule seems to return 1 here for success */
3832 /* Power Builder masked edit controls will crash */
3833 /* if not. */
3834 /* FIXME: is that in all cases so ? */
3835 return 1;
3839 /*********************************************************************
3841 * WM_DESTROY
3844 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
3846 LINEDEF *pc, *pp;
3848 if (es->hloc32W) {
3849 while (LocalUnlock(es->hloc32W)) ;
3850 LocalFree(es->hloc32W);
3852 if (es->hloc32A) {
3853 while (LocalUnlock(es->hloc32A)) ;
3854 LocalFree(es->hloc32A);
3856 if (es->hloc16) {
3857 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3858 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3859 LOCAL_Free(hInstance, es->hloc16);
3862 pc = es->first_line_def;
3863 while (pc)
3865 pp = pc->next;
3866 HeapFree(GetProcessHeap(), 0, pc);
3867 pc = pp;
3870 SetWindowLongW( es->hwndSelf, 0, 0 );
3871 HeapFree(GetProcessHeap(), 0, es);
3873 return 0;
3877 /*********************************************************************
3879 * WM_ERASEBKGND
3882 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
3884 /* we do the proper erase in EDIT_WM_Paint */
3885 return -1;
3889 /*********************************************************************
3891 * WM_GETTEXT
3894 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3896 if(!count) return 0;
3898 if(unicode)
3900 lstrcpynW(dst, es->text, count);
3901 return strlenW(dst);
3903 else
3905 LPSTR textA = (LPSTR)dst;
3906 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3907 textA[count - 1] = 0; /* ensure 0 termination */
3908 return strlen(textA);
3912 /*********************************************************************
3914 * WM_HSCROLL
3917 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3919 INT dx;
3920 INT fw;
3922 if (!(es->style & ES_MULTILINE))
3923 return 0;
3925 if (!(es->style & ES_AUTOHSCROLL))
3926 return 0;
3928 dx = 0;
3929 fw = es->format_rect.right - es->format_rect.left;
3930 switch (action) {
3931 case SB_LINELEFT:
3932 TRACE("SB_LINELEFT\n");
3933 if (es->x_offset)
3934 dx = -es->char_width;
3935 break;
3936 case SB_LINERIGHT:
3937 TRACE("SB_LINERIGHT\n");
3938 if (es->x_offset < es->text_width)
3939 dx = es->char_width;
3940 break;
3941 case SB_PAGELEFT:
3942 TRACE("SB_PAGELEFT\n");
3943 if (es->x_offset)
3944 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3945 break;
3946 case SB_PAGERIGHT:
3947 TRACE("SB_PAGERIGHT\n");
3948 if (es->x_offset < es->text_width)
3949 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3950 break;
3951 case SB_LEFT:
3952 TRACE("SB_LEFT\n");
3953 if (es->x_offset)
3954 dx = -es->x_offset;
3955 break;
3956 case SB_RIGHT:
3957 TRACE("SB_RIGHT\n");
3958 if (es->x_offset < es->text_width)
3959 dx = es->text_width - es->x_offset;
3960 break;
3961 case SB_THUMBTRACK:
3962 TRACE("SB_THUMBTRACK %d\n", pos);
3963 es->flags |= EF_HSCROLL_TRACK;
3964 if(es->style & WS_HSCROLL)
3965 dx = pos - es->x_offset;
3966 else
3968 INT fw, new_x;
3969 /* Sanity check */
3970 if(pos < 0 || pos > 100) return 0;
3971 /* Assume default scroll range 0-100 */
3972 fw = es->format_rect.right - es->format_rect.left;
3973 new_x = pos * (es->text_width - fw) / 100;
3974 dx = es->text_width ? (new_x - es->x_offset) : 0;
3976 break;
3977 case SB_THUMBPOSITION:
3978 TRACE("SB_THUMBPOSITION %d\n", pos);
3979 es->flags &= ~EF_HSCROLL_TRACK;
3980 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3981 dx = pos - es->x_offset;
3982 else
3984 INT fw, new_x;
3985 /* Sanity check */
3986 if(pos < 0 || pos > 100) return 0;
3987 /* Assume default scroll range 0-100 */
3988 fw = es->format_rect.right - es->format_rect.left;
3989 new_x = pos * (es->text_width - fw) / 100;
3990 dx = es->text_width ? (new_x - es->x_offset) : 0;
3992 if (!dx) {
3993 /* force scroll info update */
3994 EDIT_UpdateScrollInfo(es);
3995 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
3997 break;
3998 case SB_ENDSCROLL:
3999 TRACE("SB_ENDSCROLL\n");
4000 break;
4002 * FIXME : the next two are undocumented !
4003 * Are we doing the right thing ?
4004 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4005 * although it's also a regular control message.
4007 case EM_GETTHUMB: /* this one is used by NT notepad */
4008 case EM_GETTHUMB16:
4010 LRESULT ret;
4011 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4012 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4013 else
4015 /* Assume default scroll range 0-100 */
4016 INT fw = es->format_rect.right - es->format_rect.left;
4017 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4019 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4020 return ret;
4022 case EM_LINESCROLL16:
4023 TRACE("EM_LINESCROLL16\n");
4024 dx = pos;
4025 break;
4027 default:
4028 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4029 action, action);
4030 return 0;
4032 if (dx)
4034 INT fw = es->format_rect.right - es->format_rect.left;
4035 /* check if we are going to move too far */
4036 if(es->x_offset + dx + fw > es->text_width)
4037 dx = es->text_width - fw - es->x_offset;
4038 if(dx)
4039 EDIT_EM_LineScroll_internal(es, dx, 0);
4041 return 0;
4045 /*********************************************************************
4047 * EDIT_CheckCombo
4050 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4052 HWND hLBox = es->hwndListBox;
4053 HWND hCombo;
4054 BOOL bDropped;
4055 int nEUI;
4057 if (!hLBox)
4058 return FALSE;
4060 hCombo = GetParent(es->hwndSelf);
4061 bDropped = TRUE;
4062 nEUI = 0;
4064 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4066 if (key == VK_UP || key == VK_DOWN)
4068 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4069 nEUI = 1;
4071 if (msg == WM_KEYDOWN || nEUI)
4072 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4075 switch (msg)
4077 case WM_KEYDOWN:
4078 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4080 /* make sure ComboLBox pops up */
4081 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4082 key = VK_F4;
4083 nEUI = 2;
4086 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4087 break;
4089 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4090 if (nEUI)
4091 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4092 else
4093 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4094 break;
4097 if(nEUI == 2)
4098 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4100 return TRUE;
4104 /*********************************************************************
4106 * WM_KEYDOWN
4108 * Handling of special keys that don't produce a WM_CHAR
4109 * (i.e. non-printable keys) & Backspace & Delete
4112 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4114 BOOL shift;
4115 BOOL control;
4117 if (GetKeyState(VK_MENU) & 0x8000)
4118 return 0;
4120 shift = GetKeyState(VK_SHIFT) & 0x8000;
4121 control = GetKeyState(VK_CONTROL) & 0x8000;
4123 switch (key) {
4124 case VK_F4:
4125 case VK_UP:
4126 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4127 break;
4129 /* fall through */
4130 case VK_LEFT:
4131 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4132 EDIT_MoveUp_ML(es, shift);
4133 else
4134 if (control)
4135 EDIT_MoveWordBackward(es, shift);
4136 else
4137 EDIT_MoveBackward(es, shift);
4138 break;
4139 case VK_DOWN:
4140 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4141 break;
4142 /* fall through */
4143 case VK_RIGHT:
4144 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4145 EDIT_MoveDown_ML(es, shift);
4146 else if (control)
4147 EDIT_MoveWordForward(es, shift);
4148 else
4149 EDIT_MoveForward(es, shift);
4150 break;
4151 case VK_HOME:
4152 EDIT_MoveHome(es, shift);
4153 break;
4154 case VK_END:
4155 EDIT_MoveEnd(es, shift);
4156 break;
4157 case VK_PRIOR:
4158 if (es->style & ES_MULTILINE)
4159 EDIT_MovePageUp_ML(es, shift);
4160 else
4161 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4162 break;
4163 case VK_NEXT:
4164 if (es->style & ES_MULTILINE)
4165 EDIT_MovePageDown_ML(es, shift);
4166 else
4167 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4168 break;
4169 case VK_DELETE:
4170 if (!(es->style & ES_READONLY) && !(shift && control)) {
4171 if (es->selection_start != es->selection_end) {
4172 if (shift)
4173 EDIT_WM_Cut(es);
4174 else
4175 EDIT_WM_Clear(es);
4176 } else {
4177 if (shift) {
4178 /* delete character left of caret */
4179 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4180 EDIT_MoveBackward(es, TRUE);
4181 EDIT_WM_Clear(es);
4182 } else if (control) {
4183 /* delete to end of line */
4184 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4185 EDIT_MoveEnd(es, TRUE);
4186 EDIT_WM_Clear(es);
4187 } else {
4188 /* delete character right of caret */
4189 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4190 EDIT_MoveForward(es, TRUE);
4191 EDIT_WM_Clear(es);
4195 break;
4196 case VK_INSERT:
4197 if (shift) {
4198 if (!(es->style & ES_READONLY))
4199 EDIT_WM_Paste(es);
4200 } else if (control)
4201 EDIT_WM_Copy(es);
4202 break;
4203 case VK_RETURN:
4204 /* If the edit doesn't want the return send a message to the default object */
4205 if(!(es->style & ES_WANTRETURN))
4207 HWND hwndParent = GetParent(es->hwndSelf);
4208 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4209 if (HIWORD(dw) == DC_HASDEFID)
4211 SendMessageW( hwndParent, WM_COMMAND,
4212 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4213 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4216 break;
4218 return 0;
4222 /*********************************************************************
4224 * WM_KILLFOCUS
4227 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4229 es->flags &= ~EF_FOCUSED;
4230 DestroyCaret();
4231 if(!(es->style & ES_NOHIDESEL))
4232 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4233 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4234 return 0;
4238 /*********************************************************************
4240 * WM_LBUTTONDBLCLK
4242 * The caret position has been set on the WM_LBUTTONDOWN message
4245 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4247 INT s;
4248 INT e = es->selection_end;
4249 INT l;
4250 INT li;
4251 INT ll;
4253 if (!(es->flags & EF_FOCUSED))
4254 return 0;
4256 l = EDIT_EM_LineFromChar(es, e);
4257 li = EDIT_EM_LineIndex(es, l);
4258 ll = EDIT_EM_LineLength(es, e);
4259 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4260 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4261 EDIT_EM_SetSel(es, s, e, FALSE);
4262 EDIT_EM_ScrollCaret(es);
4263 return 0;
4267 /*********************************************************************
4269 * WM_LBUTTONDOWN
4272 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4274 INT e;
4275 BOOL after_wrap;
4277 SetFocus(es->hwndSelf);
4278 if (!(es->flags & EF_FOCUSED))
4279 return 0;
4281 es->bCaptureState = TRUE;
4282 SetCapture(es->hwndSelf);
4283 EDIT_ConfinePoint(es, &x, &y);
4284 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4285 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4286 EDIT_EM_ScrollCaret(es);
4287 es->region_posx = es->region_posy = 0;
4288 SetTimer(es->hwndSelf, 0, 100, NULL);
4289 return 0;
4293 /*********************************************************************
4295 * WM_LBUTTONUP
4298 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4300 if (es->bCaptureState) {
4301 KillTimer(es->hwndSelf, 0);
4302 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4304 es->bCaptureState = FALSE;
4305 return 0;
4309 /*********************************************************************
4311 * WM_MBUTTONDOWN
4314 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4316 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4317 return 0;
4321 /*********************************************************************
4323 * WM_MOUSEMOVE
4326 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4328 INT e;
4329 BOOL after_wrap;
4330 INT prex, prey;
4332 if (GetCapture() != es->hwndSelf)
4333 return 0;
4336 * FIXME: gotta do some scrolling if outside client
4337 * area. Maybe reset the timer ?
4339 prex = x; prey = y;
4340 EDIT_ConfinePoint(es, &x, &y);
4341 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4342 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4343 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4344 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4345 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4346 return 0;
4350 /*********************************************************************
4352 * WM_NCCREATE
4354 * See also EDIT_WM_StyleChanged
4356 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4358 EDITSTATE *es;
4359 UINT alloc_size;
4361 TRACE("Creating %s edit control, style = %08lx\n",
4362 unicode ? "Unicode" : "ANSI", lpcs->style);
4364 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4365 return FALSE;
4366 SetWindowLongW( hwnd, 0, (LONG)es );
4369 * Note: since the EDITSTATE has not been fully initialized yet,
4370 * we can't use any API calls that may send
4371 * WM_XXX messages before WM_NCCREATE is completed.
4374 es->is_unicode = unicode;
4375 es->style = lpcs->style;
4377 es->bEnableState = !(es->style & WS_DISABLED);
4379 es->hwndSelf = hwnd;
4380 /* Save parent, which will be notified by EN_* messages */
4381 es->hwndParent = lpcs->hwndParent;
4383 if (es->style & ES_COMBO)
4384 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4386 /* Number overrides lowercase overrides uppercase (at least it
4387 * does in Win95). However I'll bet that ES_NUMBER would be
4388 * invalid under Win 3.1.
4390 if (es->style & ES_NUMBER) {
4391 ; /* do not override the ES_NUMBER */
4392 } else if (es->style & ES_LOWERCASE) {
4393 es->style &= ~ES_UPPERCASE;
4395 if (es->style & ES_MULTILINE) {
4396 es->buffer_limit = BUFLIMIT_MULTI;
4397 if (es->style & WS_VSCROLL)
4398 es->style |= ES_AUTOVSCROLL;
4399 if (es->style & WS_HSCROLL)
4400 es->style |= ES_AUTOHSCROLL;
4401 es->style &= ~ES_PASSWORD;
4402 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4403 /* Confirmed - RIGHT overrides CENTER */
4404 if (es->style & ES_RIGHT)
4405 es->style &= ~ES_CENTER;
4406 es->style &= ~WS_HSCROLL;
4409 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4410 es->style |= ES_AUTOVSCROLL;
4411 } else {
4412 es->buffer_limit = BUFLIMIT_SINGLE;
4413 es->style &= ~ES_CENTER;
4414 es->style &= ~ES_RIGHT;
4415 es->style &= ~WS_HSCROLL;
4416 es->style &= ~WS_VSCROLL;
4417 if (es->style & ES_PASSWORD)
4418 es->password_char = '*';
4420 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4421 es->style |= ES_AUTOHSCROLL;
4424 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4425 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4426 return FALSE;
4427 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4429 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4430 return FALSE;
4431 es->undo_buffer_size = es->buffer_size;
4433 if (es->style & ES_MULTILINE)
4434 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4435 return FALSE;
4436 es->line_count = 1;
4439 * In Win95 look and feel, the WS_BORDER style is replaced by the
4440 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4441 * control a nonclient area so we don't need to draw the border.
4442 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4443 * a nonclient area and we should handle painting the border ourselves.
4445 * When making modifications please ensure that the code still works
4446 * for edit controls created directly with style 0x50800000, exStyle 0
4447 * (which should have a single pixel border)
4449 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4450 es->style &= ~WS_BORDER;
4451 else if (es->style & WS_BORDER)
4452 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4454 return TRUE;
4457 /*********************************************************************
4459 * WM_PAINT
4462 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4464 PAINTSTRUCT ps;
4465 INT i;
4466 HDC dc;
4467 HFONT old_font = 0;
4468 RECT rc;
4469 RECT rcClient;
4470 RECT rcLine;
4471 RECT rcRgn;
4472 HBRUSH brush;
4473 BOOL rev = es->bEnableState &&
4474 ((es->flags & EF_FOCUSED) ||
4475 (es->style & ES_NOHIDESEL));
4476 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4478 GetClientRect(es->hwndSelf, &rcClient);
4480 /* paint the background */
4481 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
4482 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
4483 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4484 GetClipBox(dc, &rc);
4485 FillRect(dc, &rc, brush);
4487 /* draw the border */
4488 if(es->style & WS_BORDER) {
4489 rc = rcClient;
4490 if(es->style & ES_MULTILINE) {
4491 if(es->style & WS_HSCROLL) rc.bottom++;
4492 if(es->style & WS_VSCROLL) rc.right++;
4494 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4496 IntersectClipRect(dc, es->format_rect.left,
4497 es->format_rect.top,
4498 es->format_rect.right,
4499 es->format_rect.bottom);
4500 if (es->style & ES_MULTILINE) {
4501 rc = rcClient;
4502 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4504 if (es->font)
4505 old_font = SelectObject(dc, es->font);
4506 EDIT_NotifyCtlColor(es, dc);
4508 if (!es->bEnableState)
4509 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4510 GetClipBox(dc, &rcRgn);
4511 if (es->style & ES_MULTILINE) {
4512 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4513 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4514 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4515 if (IntersectRect(&rc, &rcRgn, &rcLine))
4516 EDIT_PaintLine(es, dc, i, rev);
4518 } else {
4519 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4520 if (IntersectRect(&rc, &rcRgn, &rcLine))
4521 EDIT_PaintLine(es, dc, 0, rev);
4523 if (es->font)
4524 SelectObject(dc, old_font);
4526 if (!hdc)
4527 EndPaint(es->hwndSelf, &ps);
4531 /*********************************************************************
4533 * WM_PASTE
4536 static void EDIT_WM_Paste(EDITSTATE *es)
4538 HGLOBAL hsrc;
4539 LPWSTR src;
4541 /* Protect read-only edit control from modification */
4542 if(es->style & ES_READONLY)
4543 return;
4545 OpenClipboard(es->hwndSelf);
4546 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4547 src = (LPWSTR)GlobalLock(hsrc);
4548 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4549 GlobalUnlock(hsrc);
4551 CloseClipboard();
4555 /*********************************************************************
4557 * WM_SETFOCUS
4560 static void EDIT_WM_SetFocus(EDITSTATE *es)
4562 es->flags |= EF_FOCUSED;
4563 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4564 EDIT_SetCaretPos(es, es->selection_end,
4565 es->flags & EF_AFTER_WRAP);
4566 if(!(es->style & ES_NOHIDESEL))
4567 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4568 ShowCaret(es->hwndSelf);
4569 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4573 /*********************************************************************
4575 * WM_SETFONT
4577 * With Win95 look the margins are set to default font value unless
4578 * the system font (font == 0) is being set, in which case they are left
4579 * unchanged.
4582 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4584 TEXTMETRICW tm;
4585 HDC dc;
4586 HFONT old_font = 0;
4587 RECT r;
4589 es->font = font;
4590 dc = GetDC(es->hwndSelf);
4591 if (font)
4592 old_font = SelectObject(dc, font);
4593 GetTextMetricsW(dc, &tm);
4594 es->line_height = tm.tmHeight;
4595 es->char_width = tm.tmAveCharWidth;
4596 if (font)
4597 SelectObject(dc, old_font);
4598 ReleaseDC(es->hwndSelf, dc);
4599 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4600 EC_USEFONTINFO, EC_USEFONTINFO);
4602 /* Force the recalculation of the format rect for each font change */
4603 GetClientRect(es->hwndSelf, &r);
4604 EDIT_SetRectNP(es, &r);
4606 if (es->style & ES_MULTILINE)
4607 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4608 else
4609 EDIT_CalcLineWidth_SL(es);
4611 if (redraw)
4612 EDIT_UpdateText(es, NULL, TRUE);
4613 if (es->flags & EF_FOCUSED) {
4614 DestroyCaret();
4615 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4616 EDIT_SetCaretPos(es, es->selection_end,
4617 es->flags & EF_AFTER_WRAP);
4618 ShowCaret(es->hwndSelf);
4623 /*********************************************************************
4625 * WM_SETTEXT
4627 * NOTES
4628 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4629 * The modified flag is reset. No notifications are sent.
4631 * For single-line controls, reception of WM_SETTEXT triggers:
4632 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4635 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4637 if (!unicode && text)
4639 LPCSTR textA = (LPCSTR)text;
4640 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4641 LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
4642 if (textW)
4643 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4644 text = textW;
4647 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4648 if (text)
4650 TRACE("%s\n", debugstr_w(text));
4651 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4652 if(!unicode)
4653 HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
4655 else
4657 static const WCHAR empty_stringW[] = {0};
4658 TRACE("<NULL>\n");
4659 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4661 es->x_offset = 0;
4662 es->flags &= ~EF_MODIFIED;
4663 EDIT_EM_SetSel(es, 0, 0, FALSE);
4665 /* Send the notification after the selection start and end have been set
4666 * edit control doesn't send notification on WM_SETTEXT
4667 * if it is multiline, or it is part of combobox
4669 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4671 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4672 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4674 EDIT_EM_ScrollCaret(es);
4678 /*********************************************************************
4680 * WM_SIZE
4683 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4685 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4686 RECT rc;
4687 TRACE("width = %d, height = %d\n", width, height);
4688 SetRect(&rc, 0, 0, width, height);
4689 EDIT_SetRectNP(es, &rc);
4690 EDIT_UpdateText(es, NULL, TRUE);
4695 /*********************************************************************
4697 * WM_STYLECHANGED
4699 * This message is sent by SetWindowLong on having changed either the Style
4700 * or the extended style.
4702 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4704 * See also EDIT_WM_NCCreate
4706 * It appears that the Windows version of the edit control allows the style
4707 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4708 * style variable which will generally be different. In this function we
4709 * update the internal style based on what changed in the externally visible
4710 * style.
4712 * Much of this content as based upon the MSDN, especially:
4713 * Platform SDK Documentation -> User Interface Services ->
4714 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4715 * Edit Control Styles
4717 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4719 if (GWL_STYLE == which) {
4720 DWORD style_change_mask;
4721 DWORD new_style;
4722 /* Only a subset of changes can be applied after the control
4723 * has been created.
4725 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4726 ES_NUMBER;
4727 if (es->style & ES_MULTILINE)
4728 style_change_mask |= ES_WANTRETURN;
4730 new_style = style->styleNew & style_change_mask;
4732 /* Number overrides lowercase overrides uppercase (at least it
4733 * does in Win95). However I'll bet that ES_NUMBER would be
4734 * invalid under Win 3.1.
4736 if (new_style & ES_NUMBER) {
4737 ; /* do not override the ES_NUMBER */
4738 } else if (new_style & ES_LOWERCASE) {
4739 new_style &= ~ES_UPPERCASE;
4742 es->style = (es->style & ~style_change_mask) | new_style;
4743 } else if (GWL_EXSTYLE == which) {
4744 ; /* FIXME - what is needed here */
4745 } else {
4746 WARN ("Invalid style change %d\n",which);
4749 return 0;
4752 /*********************************************************************
4754 * WM_SYSKEYDOWN
4757 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4759 if ((key == VK_BACK) && (key_data & 0x2000)) {
4760 if (EDIT_EM_CanUndo(es))
4761 EDIT_EM_Undo(es);
4762 return 0;
4763 } else if (key == VK_UP || key == VK_DOWN) {
4764 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4765 return 0;
4767 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4771 /*********************************************************************
4773 * WM_TIMER
4776 static void EDIT_WM_Timer(EDITSTATE *es)
4778 if (es->region_posx < 0) {
4779 EDIT_MoveBackward(es, TRUE);
4780 } else if (es->region_posx > 0) {
4781 EDIT_MoveForward(es, TRUE);
4784 * FIXME: gotta do some vertical scrolling here, like
4785 * EDIT_EM_LineScroll(hwnd, 0, 1);
4789 /*********************************************************************
4791 * WM_VSCROLL
4794 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4796 INT dy;
4798 if (!(es->style & ES_MULTILINE))
4799 return 0;
4801 if (!(es->style & ES_AUTOVSCROLL))
4802 return 0;
4804 dy = 0;
4805 switch (action) {
4806 case SB_LINEUP:
4807 case SB_LINEDOWN:
4808 case SB_PAGEUP:
4809 case SB_PAGEDOWN:
4810 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4811 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4812 (action == SB_PAGEUP ? "SB_PAGEUP" :
4813 "SB_PAGEDOWN"))));
4814 EDIT_EM_Scroll(es, action);
4815 return 0;
4816 case SB_TOP:
4817 TRACE("SB_TOP\n");
4818 dy = -es->y_offset;
4819 break;
4820 case SB_BOTTOM:
4821 TRACE("SB_BOTTOM\n");
4822 dy = es->line_count - 1 - es->y_offset;
4823 break;
4824 case SB_THUMBTRACK:
4825 TRACE("SB_THUMBTRACK %d\n", pos);
4826 es->flags |= EF_VSCROLL_TRACK;
4827 if(es->style & WS_VSCROLL)
4828 dy = pos - es->y_offset;
4829 else
4831 /* Assume default scroll range 0-100 */
4832 INT vlc, new_y;
4833 /* Sanity check */
4834 if(pos < 0 || pos > 100) return 0;
4835 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4836 new_y = pos * (es->line_count - vlc) / 100;
4837 dy = es->line_count ? (new_y - es->y_offset) : 0;
4838 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4839 es->line_count, es->y_offset, pos, dy);
4841 break;
4842 case SB_THUMBPOSITION:
4843 TRACE("SB_THUMBPOSITION %d\n", pos);
4844 es->flags &= ~EF_VSCROLL_TRACK;
4845 if(es->style & WS_VSCROLL)
4846 dy = pos - es->y_offset;
4847 else
4849 /* Assume default scroll range 0-100 */
4850 INT vlc, new_y;
4851 /* Sanity check */
4852 if(pos < 0 || pos > 100) return 0;
4853 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4854 new_y = pos * (es->line_count - vlc) / 100;
4855 dy = es->line_count ? (new_y - es->y_offset) : 0;
4856 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4857 es->line_count, es->y_offset, pos, dy);
4859 if (!dy)
4861 /* force scroll info update */
4862 EDIT_UpdateScrollInfo(es);
4863 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4865 break;
4866 case SB_ENDSCROLL:
4867 TRACE("SB_ENDSCROLL\n");
4868 break;
4870 * FIXME : the next two are undocumented !
4871 * Are we doing the right thing ?
4872 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4873 * although it's also a regular control message.
4875 case EM_GETTHUMB: /* this one is used by NT notepad */
4876 case EM_GETTHUMB16:
4878 LRESULT ret;
4879 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4880 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4881 else
4883 /* Assume default scroll range 0-100 */
4884 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4885 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4887 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4888 return ret;
4890 case EM_LINESCROLL16:
4891 TRACE("EM_LINESCROLL16 %d\n", pos);
4892 dy = pos;
4893 break;
4895 default:
4896 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4897 action, action);
4898 return 0;
4900 if (dy)
4901 EDIT_EM_LineScroll(es, 0, dy);
4902 return 0;
4905 /*********************************************************************
4907 * EDIT_UpdateText
4910 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
4912 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4913 InvalidateRgn(es->hwndSelf, hrgn, bErase);
4917 /*********************************************************************
4919 * EDIT_UpdateText
4922 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
4924 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4925 InvalidateRect(es->hwndSelf, rc, bErase);