user32: Correctly handle VK_RETURN for edit controls with ES_WANTRETURN style.
[wine/multimedia.git] / dlls / user32 / edit.c
blobe6d2530db798cf7a4b65089a8840ec34d103ac39
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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_OEMCONVERT
45 #include "config.h"
47 #include <stdarg.h>
48 #include <string.h>
49 #include <stdlib.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnt.h"
54 #include "wownt32.h"
55 #include "win.h"
56 #include "imm.h"
57 #include "wine/winbase16.h"
58 #include "wine/winuser16.h"
59 #include "wine/unicode.h"
60 #include "controls.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
69 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
70 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
71 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
74 * extra flags for EDITSTATE.flags field
76 #define EF_MODIFIED 0x0001 /* text has been modified */
77 #define EF_FOCUSED 0x0002 /* we have input focus */
78 #define EF_UPDATE 0x0004 /* notify parent of changed state */
79 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
80 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
82 wrapped line, instead of in front of the next character */
83 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
84 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
85 the text buffer if this is clear. */
86 typedef enum
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
93 } LINE_END;
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
98 LINE_END ending;
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
102 } LINEDEF;
104 typedef struct
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
109 UINT buffer_size; /* the size of the buffer in characters */
110 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
111 HFONT font; /* NULL means standard system font */
112 INT x_offset; /* scroll offset for multi lines this is in pixels
113 for single lines it's in characters */
114 INT line_height; /* height of a screen line in pixels */
115 INT char_width; /* average character width in pixels */
116 DWORD style; /* sane version of wnd->dwStyle */
117 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
118 INT undo_insert_count; /* number of characters inserted in sequence */
119 UINT undo_position; /* character index of the insertion and deletion */
120 LPWSTR undo_text; /* deleted text */
121 UINT undo_buffer_size; /* size of the deleted text buffer */
122 INT selection_start; /* == selection_end if no selection */
123 INT selection_end; /* == current caret position */
124 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
125 INT left_margin; /* in pixels */
126 INT right_margin; /* in pixels */
127 RECT format_rect;
128 INT text_width; /* width of the widest line in pixels for multi line controls
129 and just line width for single line controls */
130 INT region_posx; /* Position of cursor relative to region: */
131 INT region_posy; /* -1: to left, 0: within, 1: to right */
132 EDITWORDBREAKPROC16 word_break_proc16;
133 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
134 INT line_count; /* number of lines */
135 INT y_offset; /* scroll offset in number of lines */
136 BOOL bCaptureState; /* flag indicating whether mouse was captured */
137 BOOL bEnableState; /* flag keeping the enable state */
138 HWND hwndSelf; /* the our window handle */
139 HWND hwndParent; /* Handle of parent for sending EN_* messages.
140 Even if parent will change, EN_* messages
141 should be sent to the first parent. */
142 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
144 * only for multi line controls
146 INT lock_count; /* amount of re-entries in the EditWndProc */
147 INT tabs_count;
148 LPINT tabs;
149 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
150 HLOCAL hloc32W; /* our unicode local memory block */
151 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
152 or EM_SETHANDLE16 */
153 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
154 or EM_SETHANDLE */
156 * IME Data
158 UINT composition_len; /* length of composition, 0 == no composition */
159 int composition_start; /* the character position for the composition */
160 } EDITSTATE;
163 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
164 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
166 /* used for disabled or read-only edit control */
167 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
168 do \
169 { /* Notify parent which has created this edit control */ \
170 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
171 SendMessageW(es->hwndParent, WM_COMMAND, \
172 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
173 (LPARAM)(es->hwndSelf)); \
174 } while(0)
176 /*********************************************************************
178 * Declarations
183 * These functions have trivial implementations
184 * We still like to call them internally
185 * "static inline" makes them more like macro's
187 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es);
188 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
189 static inline void EDIT_WM_Clear(EDITSTATE *es);
190 static inline void EDIT_WM_Cut(EDITSTATE *es);
193 * Helper functions only valid for one type of control
195 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
196 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
197 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
198 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
199 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
200 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
201 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
203 * Helper functions valid for both single line _and_ multi line controls
205 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
206 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
207 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y);
208 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
209 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
210 static void EDIT_LockBuffer(EDITSTATE *es);
211 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
212 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
213 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
214 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
215 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
216 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
217 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
218 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
219 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
220 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
221 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
222 static void EDIT_AdjustFormatRect(EDITSTATE *es);
223 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *lprc);
224 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
225 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
226 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
228 * EM_XXX message handlers
230 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
231 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
232 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
233 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
234 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
235 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end);
236 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
237 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
238 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line);
239 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
240 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
241 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
242 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
243 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
244 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
245 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
246 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
247 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
248 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit);
249 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, WORD left, WORD right, BOOL repaint);
250 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
251 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
252 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs);
253 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs);
254 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
255 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
256 static BOOL EDIT_EM_Undo(EDITSTATE *es);
258 * WM_XXX message handlers
260 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
261 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
262 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
263 static void EDIT_WM_Copy(EDITSTATE *es);
264 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
265 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
266 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
267 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
268 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
269 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
270 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
271 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
272 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
273 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
274 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
275 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
276 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
277 static void EDIT_WM_Paste(EDITSTATE *es);
278 static void EDIT_WM_SetFocus(EDITSTATE *es);
279 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
280 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
281 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
282 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
283 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
284 static void EDIT_WM_Timer(EDITSTATE *es);
285 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
286 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase);
287 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
288 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
290 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
291 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
293 /*********************************************************************
294 * edit class descriptor
296 static const WCHAR editW[] = {'E','d','i','t',0};
297 const struct builtin_class_descr EDIT_builtin_class =
299 editW, /* name */
300 CS_DBLCLKS | CS_PARENTDC, /* style */
301 EditWndProcA, /* procA */
302 EditWndProcW, /* procW */
303 sizeof(EDITSTATE *), /* extra */
304 IDC_IBEAM, /* cursor */
305 0 /* brush */
309 /*********************************************************************
311 * EM_CANUNDO
314 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
316 return (es->undo_insert_count || strlenW(es->undo_text));
320 /*********************************************************************
322 * EM_EMPTYUNDOBUFFER
325 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
327 es->undo_insert_count = 0;
328 *es->undo_text = '\0';
332 /*********************************************************************
334 * WM_CLEAR
337 static inline void EDIT_WM_Clear(EDITSTATE *es)
339 static const WCHAR empty_stringW[] = {0};
341 /* Protect read-only edit control from modification */
342 if(es->style & ES_READONLY)
343 return;
345 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
349 /*********************************************************************
351 * WM_CUT
354 static inline void EDIT_WM_Cut(EDITSTATE *es)
356 EDIT_WM_Copy(es);
357 EDIT_WM_Clear(es);
361 /**********************************************************************
362 * get_app_version
364 * Returns the window version in case Wine emulates a later version
365 * of windows than the application expects.
367 * In a number of cases when windows runs an application that was
368 * designed for an earlier windows version, windows reverts
369 * to "old" behaviour of that earlier version.
371 * An example is a disabled edit control that needs to be painted.
372 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
373 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
374 * applications with an expected version 0f 4.0 or higher.
377 static DWORD get_app_version(void)
379 static DWORD version;
380 if (!version)
382 DWORD dwEmulatedVersion;
383 OSVERSIONINFOW info;
384 DWORD dwProcVersion = GetProcessVersion(0);
386 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
387 GetVersionExW( &info );
388 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
389 /* FIXME: this may not be 100% correct; see discussion on the
390 * wine developer list in Nov 1999 */
391 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
393 return version;
396 static inline UINT get_text_length(EDITSTATE *es)
398 if(es->text_length == (UINT)-1)
399 es->text_length = strlenW(es->text);
400 return es->text_length;
403 static inline void text_buffer_changed(EDITSTATE *es)
405 es->text_length = (UINT)-1;
408 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
410 HBRUSH hbrush;
411 UINT msg;
413 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
414 msg = WM_CTLCOLORSTATIC;
415 else
416 msg = WM_CTLCOLOREDIT;
418 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
419 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
420 if (!hbrush)
421 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
422 return hbrush;
425 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
427 if(unicode)
428 return DefWindowProcW(hwnd, msg, wParam, lParam);
429 else
430 return DefWindowProcA(hwnd, msg, wParam, lParam);
433 /*********************************************************************
435 * EditWndProc_common
437 * The messages are in the order of the actual integer values
438 * (which can be found in include/windows.h)
439 * Wherever possible the 16 bit versions are converted to
440 * the 32 bit ones, so that we can 'fall through' to the
441 * helper functions. These are mostly 32 bit (with a few
442 * exceptions, clearly indicated by a '16' extension to their
443 * names).
446 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
447 WPARAM wParam, LPARAM lParam, BOOL unicode )
449 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
450 LRESULT result = 0;
452 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
454 if (!es && msg != WM_NCCREATE)
455 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
457 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
459 switch (msg) {
460 case EM_GETSEL16:
461 wParam = 0;
462 lParam = 0;
463 /* fall through */
464 case EM_GETSEL:
465 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
466 break;
468 case EM_SETSEL16:
469 if ((short)LOWORD(lParam) == -1)
470 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
471 else
472 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
473 if (!wParam)
474 EDIT_EM_ScrollCaret(es);
475 result = 1;
476 break;
477 case EM_SETSEL:
478 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
479 EDIT_EM_ScrollCaret(es);
480 result = 1;
481 break;
483 case EM_GETRECT16:
484 if (lParam)
486 RECT16 *r16 = MapSL(lParam);
487 r16->left = es->format_rect.left;
488 r16->top = es->format_rect.top;
489 r16->right = es->format_rect.right;
490 r16->bottom = es->format_rect.bottom;
492 break;
493 case EM_GETRECT:
494 if (lParam)
495 CopyRect((LPRECT)lParam, &es->format_rect);
496 break;
498 case EM_SETRECT16:
499 if ((es->style & ES_MULTILINE) && lParam) {
500 RECT rc;
501 RECT16 *r16 = MapSL(lParam);
502 rc.left = r16->left;
503 rc.top = r16->top;
504 rc.right = r16->right;
505 rc.bottom = r16->bottom;
506 EDIT_SetRectNP(es, &rc);
507 EDIT_UpdateText(es, NULL, TRUE);
509 break;
510 case EM_SETRECT:
511 if ((es->style & ES_MULTILINE) && lParam) {
512 EDIT_SetRectNP(es, (LPRECT)lParam);
513 EDIT_UpdateText(es, NULL, TRUE);
515 break;
517 case EM_SETRECTNP16:
518 if ((es->style & ES_MULTILINE) && lParam) {
519 RECT rc;
520 RECT16 *r16 = MapSL(lParam);
521 rc.left = r16->left;
522 rc.top = r16->top;
523 rc.right = r16->right;
524 rc.bottom = r16->bottom;
525 EDIT_SetRectNP(es, &rc);
527 break;
528 case EM_SETRECTNP:
529 if ((es->style & ES_MULTILINE) && lParam)
530 EDIT_SetRectNP(es, (LPRECT)lParam);
531 break;
533 case EM_SCROLL16:
534 case EM_SCROLL:
535 result = EDIT_EM_Scroll(es, (INT)wParam);
536 break;
538 case EM_LINESCROLL16:
539 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
540 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
541 /* fall through */
542 case EM_LINESCROLL:
543 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
544 break;
546 case EM_SCROLLCARET16:
547 case EM_SCROLLCARET:
548 EDIT_EM_ScrollCaret(es);
549 result = 1;
550 break;
552 case EM_GETMODIFY16:
553 case EM_GETMODIFY:
554 result = ((es->flags & EF_MODIFIED) != 0);
555 break;
557 case EM_SETMODIFY16:
558 case EM_SETMODIFY:
559 if (wParam)
560 es->flags |= EF_MODIFIED;
561 else
562 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
563 break;
565 case EM_GETLINECOUNT16:
566 case EM_GETLINECOUNT:
567 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
568 break;
570 case EM_LINEINDEX16:
571 if ((INT16)wParam == -1)
572 wParam = (WPARAM)-1;
573 /* fall through */
574 case EM_LINEINDEX:
575 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
576 break;
578 case EM_SETHANDLE16:
579 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
580 break;
581 case EM_SETHANDLE:
582 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
583 break;
585 case EM_GETHANDLE16:
586 result = (LRESULT)EDIT_EM_GetHandle16(es);
587 break;
588 case EM_GETHANDLE:
589 result = (LRESULT)EDIT_EM_GetHandle(es);
590 break;
592 case EM_GETTHUMB16:
593 case EM_GETTHUMB:
594 result = EDIT_EM_GetThumb(es);
595 break;
597 /* these messages missing from specs */
598 case WM_USER+15:
599 case 0x00bf:
600 case WM_USER+16:
601 case 0x00c0:
602 case WM_USER+19:
603 case 0x00c3:
604 case WM_USER+26:
605 case 0x00ca:
606 FIXME("undocumented message 0x%x, please report\n", msg);
607 result = DefWindowProcW(hwnd, msg, wParam, lParam);
608 break;
610 case EM_LINELENGTH16:
611 case EM_LINELENGTH:
612 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
613 break;
615 case EM_REPLACESEL16:
616 lParam = (LPARAM)MapSL(lParam);
617 unicode = FALSE; /* 16-bit message is always ascii */
618 /* fall through */
619 case EM_REPLACESEL:
621 LPWSTR textW;
623 if(unicode)
624 textW = (LPWSTR)lParam;
625 else
627 LPSTR textA = (LPSTR)lParam;
628 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
629 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
630 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
633 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
634 result = 1;
636 if(!unicode)
637 HeapFree(GetProcessHeap(), 0, textW);
638 break;
641 case EM_GETLINE16:
642 lParam = (LPARAM)MapSL(lParam);
643 unicode = FALSE; /* 16-bit message is always ascii */
644 /* fall through */
645 case EM_GETLINE:
646 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
647 break;
649 case EM_LIMITTEXT16:
650 case EM_SETLIMITTEXT:
651 EDIT_EM_SetLimitText(es, wParam);
652 break;
654 case EM_CANUNDO16:
655 case EM_CANUNDO:
656 result = (LRESULT)EDIT_EM_CanUndo(es);
657 break;
659 case EM_UNDO16:
660 case EM_UNDO:
661 case WM_UNDO:
662 result = (LRESULT)EDIT_EM_Undo(es);
663 break;
665 case EM_FMTLINES16:
666 case EM_FMTLINES:
667 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
668 break;
670 case EM_LINEFROMCHAR16:
671 case EM_LINEFROMCHAR:
672 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
673 break;
675 case EM_SETTABSTOPS16:
676 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
677 break;
678 case EM_SETTABSTOPS:
679 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
680 break;
682 case EM_SETPASSWORDCHAR16:
683 unicode = FALSE; /* 16-bit message is always ascii */
684 /* fall through */
685 case EM_SETPASSWORDCHAR:
687 WCHAR charW = 0;
689 if(unicode)
690 charW = (WCHAR)wParam;
691 else
693 CHAR charA = wParam;
694 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
697 EDIT_EM_SetPasswordChar(es, charW);
698 break;
701 case EM_EMPTYUNDOBUFFER16:
702 case EM_EMPTYUNDOBUFFER:
703 EDIT_EM_EmptyUndoBuffer(es);
704 break;
706 case EM_GETFIRSTVISIBLELINE16:
707 result = es->y_offset;
708 break;
709 case EM_GETFIRSTVISIBLELINE:
710 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
711 break;
713 case EM_SETREADONLY16:
714 case EM_SETREADONLY:
715 if (wParam) {
716 SetWindowLongW( hwnd, GWL_STYLE,
717 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
718 es->style |= ES_READONLY;
719 } else {
720 SetWindowLongW( hwnd, GWL_STYLE,
721 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
722 es->style &= ~ES_READONLY;
724 result = 1;
725 break;
727 case EM_SETWORDBREAKPROC16:
728 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
729 break;
730 case EM_SETWORDBREAKPROC:
731 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
732 break;
734 case EM_GETWORDBREAKPROC16:
735 result = (LRESULT)es->word_break_proc16;
736 break;
737 case EM_GETWORDBREAKPROC:
738 result = (LRESULT)es->word_break_proc;
739 break;
741 case EM_GETPASSWORDCHAR16:
742 unicode = FALSE; /* 16-bit message is always ascii */
743 /* fall through */
744 case EM_GETPASSWORDCHAR:
746 if(unicode)
747 result = es->password_char;
748 else
750 WCHAR charW = es->password_char;
751 CHAR charA = 0;
752 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
753 result = charA;
755 break;
758 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
760 case EM_SETMARGINS:
761 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
762 break;
764 case EM_GETMARGINS:
765 result = MAKELONG(es->left_margin, es->right_margin);
766 break;
768 case EM_GETLIMITTEXT:
769 result = es->buffer_limit;
770 break;
772 case EM_POSFROMCHAR:
773 if ((INT)wParam >= get_text_length(es)) result = -1;
774 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
775 break;
777 case EM_CHARFROMPOS:
778 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
779 break;
781 /* End of the EM_ messages which were in numerical order; what order
782 * are these in? vaguely alphabetical?
785 case WM_NCCREATE:
786 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
787 break;
789 case WM_DESTROY:
790 result = EDIT_WM_Destroy(es);
791 es = NULL;
792 break;
794 case WM_GETDLGCODE:
795 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
797 if (es->style & ES_MULTILINE)
798 result |= DLGC_WANTALLKEYS;
800 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
802 int vk = (int)((LPMSG)lParam)->wParam;
804 if (es->hwndListBox)
806 if (vk == VK_RETURN || vk == VK_ESCAPE)
807 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
808 result |= DLGC_WANTMESSAGE;
810 else
812 switch (vk)
814 case VK_ESCAPE:
815 SendMessageW(GetParent(hwnd), WM_CLOSE, 0, 0);
816 break;
817 case VK_TAB:
818 SendMessageW(GetParent(hwnd), WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0);
819 break;
820 default:
821 break;
825 break;
827 case WM_IME_CHAR:
828 if (!unicode)
830 WCHAR charW;
831 CHAR strng[2];
833 strng[0] = wParam >> 8;
834 strng[1] = wParam & 0xff;
835 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
836 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
837 EDIT_WM_Char(es, charW);
838 break;
840 /* fall through */
841 case WM_CHAR:
843 WCHAR charW;
845 if(unicode)
846 charW = wParam;
847 else
849 CHAR charA = wParam;
850 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
853 if (es->hwndListBox)
855 if (charW == VK_RETURN || charW == VK_ESCAPE)
857 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
859 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
860 break;
864 else
866 if (charW == VK_TAB || charW == VK_RETURN)
867 break;
869 EDIT_WM_Char(es, charW);
870 break;
873 case WM_CLEAR:
874 EDIT_WM_Clear(es);
875 break;
877 case WM_COMMAND:
878 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
879 break;
881 case WM_CONTEXTMENU:
882 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
883 break;
885 case WM_COPY:
886 EDIT_WM_Copy(es);
887 break;
889 case WM_CREATE:
890 if(unicode)
891 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
892 else
894 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
895 LPWSTR nameW = NULL;
896 if(nameA)
898 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
899 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
900 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
902 result = EDIT_WM_Create(es, nameW);
903 HeapFree(GetProcessHeap(), 0, nameW);
905 break;
907 case WM_CUT:
908 EDIT_WM_Cut(es);
909 break;
911 case WM_ENABLE:
912 es->bEnableState = (BOOL) wParam;
913 EDIT_UpdateText(es, NULL, TRUE);
914 break;
916 case WM_ERASEBKGND:
917 /* we do the proper erase in EDIT_WM_Paint */
918 result = 1;
919 break;
921 case WM_GETFONT:
922 result = (LRESULT)es->font;
923 break;
925 case WM_GETTEXT:
926 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
927 break;
929 case WM_GETTEXTLENGTH:
930 if (unicode) result = get_text_length(es);
931 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
932 NULL, 0, NULL, NULL );
933 break;
935 case WM_HSCROLL:
936 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
937 break;
939 case WM_KEYDOWN:
940 result = EDIT_WM_KeyDown(es, (INT)wParam);
941 break;
943 case WM_KILLFOCUS:
944 result = EDIT_WM_KillFocus(es);
945 break;
947 case WM_LBUTTONDBLCLK:
948 result = EDIT_WM_LButtonDblClk(es);
949 break;
951 case WM_LBUTTONDOWN:
952 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
953 break;
955 case WM_LBUTTONUP:
956 result = EDIT_WM_LButtonUp(es);
957 break;
959 case WM_MBUTTONDOWN:
960 result = EDIT_WM_MButtonDown(es);
961 break;
963 case WM_MOUSEMOVE:
964 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
965 break;
967 case WM_PRINTCLIENT:
968 case WM_PAINT:
969 EDIT_WM_Paint(es, (HDC)wParam);
970 break;
972 case WM_PASTE:
973 EDIT_WM_Paste(es);
974 break;
976 case WM_SETFOCUS:
977 EDIT_WM_SetFocus(es);
978 break;
980 case WM_SETFONT:
981 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
982 break;
984 case WM_SETREDRAW:
985 /* FIXME: actually set an internal flag and behave accordingly */
986 break;
988 case WM_SETTEXT:
989 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
990 result = TRUE;
991 break;
993 case WM_SIZE:
994 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
995 break;
997 case WM_STYLECHANGED:
998 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
999 break;
1001 case WM_STYLECHANGING:
1002 result = 0; /* See EDIT_WM_StyleChanged */
1003 break;
1005 case WM_SYSKEYDOWN:
1006 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
1007 break;
1009 case WM_TIMER:
1010 EDIT_WM_Timer(es);
1011 break;
1013 case WM_VSCROLL:
1014 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
1015 break;
1017 case WM_MOUSEWHEEL:
1019 int gcWheelDelta = 0;
1020 UINT pulScrollLines = 3;
1021 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1023 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1024 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1025 break;
1027 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1028 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1030 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1031 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1032 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1035 break;
1038 /* IME messages to make the edit control IME aware */
1039 case WM_IME_SETCONTEXT:
1040 break;
1042 case WM_IME_STARTCOMPOSITION:
1044 * FIXME in IME: This message is not always sent like it should be
1046 if (es->selection_start != es->selection_end)
1048 static const WCHAR empty_stringW[] = {0};
1049 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1051 es->composition_start = es->selection_end;
1052 es->composition_len = 0;
1053 break;
1055 case WM_IME_COMPOSITION:
1057 int caret_pos = es->selection_end;
1058 if (es->composition_len == 0)
1060 if (es->selection_start != es->selection_end)
1062 static const WCHAR empty_stringW[] = {0};
1063 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1066 es->composition_start = es->selection_end;
1068 EDIT_ImeComposition(hwnd,lParam,es);
1069 EDIT_SetCaretPos(es, caret_pos, es->flags & EF_AFTER_WRAP);
1070 break;
1073 case WM_IME_ENDCOMPOSITION:
1074 es->composition_len= 0;
1075 break;
1077 case WM_IME_COMPOSITIONFULL:
1078 break;
1080 case WM_IME_SELECT:
1081 break;
1083 case WM_IME_CONTROL:
1084 break;
1086 default:
1087 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1088 break;
1091 if (es) EDIT_UnlockBuffer(es, FALSE);
1093 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1095 return result;
1098 /*********************************************************************
1100 * EditWndProcW (USER32.@)
1102 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1104 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1107 /*********************************************************************
1109 * EditWndProc (USER32.@)
1111 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1113 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1116 /*********************************************************************
1118 * EDIT_BuildLineDefs_ML
1120 * Build linked list of text lines.
1121 * Lines can end with '\0' (last line), a character (if it is wrapped),
1122 * a soft return '\r\r\n' or a hard return '\r\n'
1125 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1127 HDC dc;
1128 HFONT old_font = 0;
1129 LPWSTR current_position, cp;
1130 INT fw;
1131 LINEDEF *current_line;
1132 LINEDEF *previous_line;
1133 LINEDEF *start_line;
1134 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1135 INT line_count = es->line_count;
1136 INT orig_net_length;
1137 RECT rc;
1139 if (istart == iend && delta == 0)
1140 return;
1142 dc = GetDC(es->hwndSelf);
1143 if (es->font)
1144 old_font = SelectObject(dc, es->font);
1146 previous_line = NULL;
1147 current_line = es->first_line_def;
1149 /* Find starting line. istart must lie inside an existing line or
1150 * at the end of buffer */
1151 do {
1152 if (istart < current_line->index + current_line->length ||
1153 current_line->ending == END_0)
1154 break;
1156 previous_line = current_line;
1157 current_line = current_line->next;
1158 line_index++;
1159 } while (current_line);
1161 if (!current_line) /* Error occurred start is not inside previous buffer */
1163 FIXME(" modification occurred outside buffer\n");
1164 ReleaseDC(es->hwndSelf, dc);
1165 return;
1168 /* Remember start of modifications in order to calculate update region */
1169 nstart_line = line_index;
1170 nstart_index = current_line->index;
1172 /* We must start to reformat from the previous line since the modifications
1173 * may have caused the line to wrap upwards. */
1174 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1176 line_index--;
1177 current_line = previous_line;
1179 start_line = current_line;
1181 fw = es->format_rect.right - es->format_rect.left;
1182 current_position = es->text + current_line->index;
1183 do {
1184 if (current_line != start_line)
1186 if (!current_line || current_line->index + delta > current_position - es->text)
1188 /* The buffer has been expanded, create a new line and
1189 insert it into the link list */
1190 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1191 new_line->next = previous_line->next;
1192 previous_line->next = new_line;
1193 current_line = new_line;
1194 es->line_count++;
1196 else if (current_line->index + delta < current_position - es->text)
1198 /* The previous line merged with this line so we delete this extra entry */
1199 previous_line->next = current_line->next;
1200 HeapFree(GetProcessHeap(), 0, current_line);
1201 current_line = previous_line->next;
1202 es->line_count--;
1203 continue;
1205 else /* current_line->index + delta == current_position */
1207 if (current_position - es->text > iend)
1208 break; /* We reached end of line modifications */
1209 /* else recalulate this line */
1213 current_line->index = current_position - es->text;
1214 orig_net_length = current_line->net_length;
1216 /* Find end of line */
1217 cp = current_position;
1218 while (*cp) {
1219 if (*cp == '\n') break;
1220 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1221 break;
1222 cp++;
1225 /* Mark type of line termination */
1226 if (!(*cp)) {
1227 current_line->ending = END_0;
1228 current_line->net_length = strlenW(current_position);
1229 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1230 current_line->ending = END_SOFT;
1231 current_line->net_length = cp - current_position - 1;
1232 } else if (*cp == '\n') {
1233 current_line->ending = END_RICH;
1234 current_line->net_length = cp - current_position;
1235 } else {
1236 current_line->ending = END_HARD;
1237 current_line->net_length = cp - current_position;
1240 /* Calculate line width */
1241 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1242 current_position, current_line->net_length,
1243 es->tabs_count, es->tabs));
1245 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1246 if (!(es->style & ES_AUTOHSCROLL)) {
1247 if (current_line->width > fw) {
1248 INT next = 0;
1249 INT prev;
1250 do {
1251 prev = next;
1252 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1253 prev + 1, current_line->net_length, WB_RIGHT);
1254 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1255 current_position, next, es->tabs_count, es->tabs));
1256 } while (current_line->width <= fw);
1257 if (!prev) { /* Didn't find a line break so force a break */
1258 next = 0;
1259 do {
1260 prev = next;
1261 next++;
1262 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1263 current_position, next, es->tabs_count, es->tabs));
1264 } while (current_line->width <= fw);
1265 if (!prev)
1266 prev = 1;
1269 /* If the first line we are calculating, wrapped before istart, we must
1270 * adjust istart in order for this to be reflected in the update region. */
1271 if (current_line->index == nstart_index && istart > current_line->index + prev)
1272 istart = current_line->index + prev;
1273 /* else if we are updating the previous line before the first line we
1274 * are re-calculating and it expanded */
1275 else if (current_line == start_line &&
1276 current_line->index != nstart_index && orig_net_length < prev)
1278 /* Line expanded due to an upwards line wrap so we must partially include
1279 * previous line in update region */
1280 nstart_line = line_index;
1281 nstart_index = current_line->index;
1282 istart = current_line->index + orig_net_length;
1285 current_line->net_length = prev;
1286 current_line->ending = END_WRAP;
1287 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1288 current_line->net_length, es->tabs_count, es->tabs));
1290 else if (orig_net_length < current_line->net_length &&
1291 current_line == start_line &&
1292 current_line->index != nstart_index) {
1293 /* The previous line expanded but it's still not as wide as the client rect */
1294 /* The expansion is due to an upwards line wrap so we must partially include
1295 it in the update region */
1296 nstart_line = line_index;
1297 nstart_index = current_line->index;
1298 istart = current_line->index + orig_net_length;
1303 /* Adjust length to include line termination */
1304 switch (current_line->ending) {
1305 case END_SOFT:
1306 current_line->length = current_line->net_length + 3;
1307 break;
1308 case END_RICH:
1309 current_line->length = current_line->net_length + 1;
1310 break;
1311 case END_HARD:
1312 current_line->length = current_line->net_length + 2;
1313 break;
1314 case END_WRAP:
1315 case END_0:
1316 current_line->length = current_line->net_length;
1317 break;
1319 es->text_width = max(es->text_width, current_line->width);
1320 current_position += current_line->length;
1321 previous_line = current_line;
1322 current_line = current_line->next;
1323 line_index++;
1324 } while (previous_line->ending != END_0);
1326 /* Finish adjusting line indexes by delta or remove hanging lines */
1327 if (previous_line->ending == END_0)
1329 LINEDEF *pnext = NULL;
1331 previous_line->next = NULL;
1332 while (current_line)
1334 pnext = current_line->next;
1335 HeapFree(GetProcessHeap(), 0, current_line);
1336 current_line = pnext;
1337 es->line_count--;
1340 else if (delta != 0)
1342 while (current_line)
1344 current_line->index += delta;
1345 current_line = current_line->next;
1349 /* Calculate rest of modification rectangle */
1350 if (hrgn)
1352 HRGN tmphrgn;
1354 * We calculate two rectangles. One for the first line which may have
1355 * an indent with respect to the format rect. The other is a format-width
1356 * rectangle that spans the rest of the lines that changed or moved.
1358 rc.top = es->format_rect.top + nstart_line * es->line_height -
1359 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1360 rc.bottom = rc.top + es->line_height;
1361 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1362 rc.left = es->format_rect.left;
1363 else
1364 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1365 es->text + nstart_index, istart - nstart_index,
1366 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1367 rc.right = es->format_rect.right;
1368 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1370 rc.top = rc.bottom;
1371 rc.left = es->format_rect.left;
1372 rc.right = es->format_rect.right;
1374 * If lines were added or removed we must re-paint the remainder of the
1375 * lines since the remaining lines were either shifted up or down.
1377 if (line_count < es->line_count) /* We added lines */
1378 rc.bottom = es->line_count * es->line_height;
1379 else if (line_count > es->line_count) /* We removed lines */
1380 rc.bottom = line_count * es->line_height;
1381 else
1382 rc.bottom = line_index * es->line_height;
1383 rc.bottom += es->format_rect.top;
1384 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1385 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1386 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1387 DeleteObject(tmphrgn);
1390 if (es->font)
1391 SelectObject(dc, old_font);
1393 ReleaseDC(es->hwndSelf, dc);
1396 /*********************************************************************
1398 * EDIT_CalcLineWidth_SL
1401 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1403 SIZE size;
1404 LPWSTR text;
1405 HDC dc;
1406 HFONT old_font = 0;
1408 text = EDIT_GetPasswordPointer_SL(es);
1410 dc = GetDC(es->hwndSelf);
1411 if (es->font)
1412 old_font = SelectObject(dc, es->font);
1414 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1416 if (es->font)
1417 SelectObject(dc, old_font);
1418 ReleaseDC(es->hwndSelf, dc);
1420 if (es->style & ES_PASSWORD)
1421 HeapFree(GetProcessHeap(), 0, text);
1423 es->text_width = size.cx;
1426 /*********************************************************************
1428 * EDIT_CallWordBreakProc
1430 * Call appropriate WordBreakProc (internal or external).
1432 * Note: The "start" argument should always be an index referring
1433 * to es->text. The actual wordbreak proc might be
1434 * 16 bit, so we can't always pass any 32 bit LPSTR.
1435 * Hence we assume that es->text is the buffer that holds
1436 * the string under examination (we can decide this for ourselves).
1439 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1441 INT ret;
1443 if (es->word_break_proc16) {
1444 HGLOBAL16 hglob16;
1445 SEGPTR segptr;
1446 INT countA;
1447 WORD args[5];
1448 DWORD result;
1450 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1451 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1452 segptr = WOWGlobalLock16(hglob16);
1453 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1454 args[4] = SELECTOROF(segptr);
1455 args[3] = OFFSETOF(segptr);
1456 args[2] = index;
1457 args[1] = countA;
1458 args[0] = action;
1459 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1460 ret = LOWORD(result);
1461 GlobalUnlock16(hglob16);
1462 GlobalFree16(hglob16);
1464 else if (es->word_break_proc)
1466 if(es->is_unicode)
1468 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1470 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1471 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1472 ret = wbpW(es->text + start, index, count, action);
1474 else
1476 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1477 INT countA;
1478 CHAR *textA;
1480 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1481 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1482 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1483 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1484 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1485 ret = wbpA(textA, index, countA, action);
1486 HeapFree(GetProcessHeap(), 0, textA);
1489 else
1490 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1492 return ret;
1496 /*********************************************************************
1498 * EDIT_CharFromPos
1500 * Beware: This is not the function called on EM_CHARFROMPOS
1501 * The position _can_ be outside the formatting / client
1502 * rectangle
1503 * The return value is only the character index
1506 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1508 INT index;
1509 HDC dc;
1510 HFONT old_font = 0;
1511 INT x_high = 0, x_low = 0;
1513 if (es->style & ES_MULTILINE) {
1514 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1515 INT line_index = 0;
1516 LINEDEF *line_def = es->first_line_def;
1517 INT low, high;
1518 while ((line > 0) && line_def->next) {
1519 line_index += line_def->length;
1520 line_def = line_def->next;
1521 line--;
1523 x += es->x_offset - es->format_rect.left;
1524 if (es->style & ES_RIGHT)
1525 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1526 else if (es->style & ES_CENTER)
1527 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1528 if (x >= line_def->width) {
1529 if (after_wrap)
1530 *after_wrap = (line_def->ending == END_WRAP);
1531 return line_index + line_def->net_length;
1533 if (x <= 0) {
1534 if (after_wrap)
1535 *after_wrap = FALSE;
1536 return line_index;
1538 dc = GetDC(es->hwndSelf);
1539 if (es->font)
1540 old_font = SelectObject(dc, es->font);
1541 low = line_index;
1542 high = line_index + line_def->net_length + 1;
1543 while (low < high - 1)
1545 INT mid = (low + high) / 2;
1546 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1547 if (x_now > x) {
1548 high = mid;
1549 x_high = x_now;
1550 } else {
1551 low = mid;
1552 x_low = x_now;
1555 if (abs(x_high - x) + 1 <= abs(x_low - x))
1556 index = high;
1557 else
1558 index = low;
1560 if (after_wrap)
1561 *after_wrap = ((index == line_index + line_def->net_length) &&
1562 (line_def->ending == END_WRAP));
1563 } else {
1564 LPWSTR text;
1565 SIZE size;
1566 if (after_wrap)
1567 *after_wrap = FALSE;
1568 x -= es->format_rect.left;
1569 if (!x)
1570 return es->x_offset;
1572 if (!es->x_offset)
1574 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1575 if (es->style & ES_RIGHT)
1576 x -= indent;
1577 else if (es->style & ES_CENTER)
1578 x -= indent / 2;
1581 text = EDIT_GetPasswordPointer_SL(es);
1582 dc = GetDC(es->hwndSelf);
1583 if (es->font)
1584 old_font = SelectObject(dc, es->font);
1585 if (x < 0)
1587 INT low = 0;
1588 INT high = es->x_offset;
1589 while (low < high - 1)
1591 INT mid = (low + high) / 2;
1592 GetTextExtentPoint32W( dc, text + mid,
1593 es->x_offset - mid, &size );
1594 if (size.cx > -x) {
1595 low = mid;
1596 x_low = size.cx;
1597 } else {
1598 high = mid;
1599 x_high = size.cx;
1602 if (abs(x_high + x) <= abs(x_low + x) + 1)
1603 index = high;
1604 else
1605 index = low;
1607 else
1609 INT low = es->x_offset;
1610 INT high = get_text_length(es) + 1;
1611 while (low < high - 1)
1613 INT mid = (low + high) / 2;
1614 GetTextExtentPoint32W( dc, text + es->x_offset,
1615 mid - es->x_offset, &size );
1616 if (size.cx > x) {
1617 high = mid;
1618 x_high = size.cx;
1619 } else {
1620 low = mid;
1621 x_low = size.cx;
1624 if (abs(x_high - x) <= abs(x_low - x) + 1)
1625 index = high;
1626 else
1627 index = low;
1629 if (es->style & ES_PASSWORD)
1630 HeapFree(GetProcessHeap(), 0, text);
1632 if (es->font)
1633 SelectObject(dc, old_font);
1634 ReleaseDC(es->hwndSelf, dc);
1635 return index;
1639 /*********************************************************************
1641 * EDIT_ConfinePoint
1643 * adjusts the point to be within the formatting rectangle
1644 * (so CharFromPos returns the nearest _visible_ character)
1647 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
1649 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1650 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1654 /*********************************************************************
1656 * EDIT_GetLineRect
1658 * Calculates the bounding rectangle for a line from a starting
1659 * column to an ending column.
1662 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1664 INT line_index = EDIT_EM_LineIndex(es, line);
1666 if (es->style & ES_MULTILINE)
1667 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1668 else
1669 rc->top = es->format_rect.top;
1670 rc->bottom = rc->top + es->line_height;
1671 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1672 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1676 /*********************************************************************
1678 * EDIT_GetPasswordPointer_SL
1680 * note: caller should free the (optionally) allocated buffer
1683 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1685 if (es->style & ES_PASSWORD) {
1686 INT len = get_text_length(es);
1687 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1688 text[len] = '\0';
1689 while(len) text[--len] = es->password_char;
1690 return text;
1691 } else
1692 return es->text;
1696 /*********************************************************************
1698 * EDIT_LockBuffer
1700 * This acts as a LocalLock16(), but it locks only once. This way
1701 * you can call it whenever you like, without unlocking.
1703 * Initially the edit control allocates a HLOCAL32 buffer
1704 * (32 bit linear memory handler). However, 16 bit application
1705 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1706 * handler). From that moment on we have to keep using this 16 bit memory
1707 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1708 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1709 * conversion.
1712 static void EDIT_LockBuffer(EDITSTATE *es)
1714 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1715 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1717 if (!es->text) {
1718 CHAR *textA = NULL;
1719 UINT countA = 0;
1720 BOOL _16bit = FALSE;
1722 if(es->hloc32W)
1724 if(es->hloc32A)
1726 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1727 textA = LocalLock(es->hloc32A);
1728 countA = strlen(textA) + 1;
1730 else if(es->hloc16)
1732 HANDLE16 oldDS = stack16->ds;
1733 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1734 stack16->ds = hInstance;
1735 textA = MapSL(LocalLock16(es->hloc16));
1736 stack16->ds = oldDS;
1737 countA = strlen(textA) + 1;
1738 _16bit = TRUE;
1741 else {
1742 ERR("no buffer ... please report\n");
1743 return;
1746 if(textA)
1748 HLOCAL hloc32W_new;
1749 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1750 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1751 if(countW_new > es->buffer_size + 1)
1753 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1754 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1755 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1756 if(hloc32W_new)
1758 es->hloc32W = hloc32W_new;
1759 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1760 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1762 else
1763 WARN("FAILED! Will synchronize partially\n");
1767 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1768 es->text = LocalLock(es->hloc32W);
1770 if(textA)
1772 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1773 if(_16bit)
1775 HANDLE16 oldDS = stack16->ds;
1776 stack16->ds = hInstance;
1777 LocalUnlock16(es->hloc16);
1778 stack16->ds = oldDS;
1780 else
1781 LocalUnlock(es->hloc32A);
1784 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1785 es->lock_count++;
1789 /*********************************************************************
1791 * EDIT_SL_InvalidateText
1793 * Called from EDIT_InvalidateText().
1794 * Does the job for single-line controls only.
1797 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1799 RECT line_rect;
1800 RECT rc;
1802 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1803 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1804 EDIT_UpdateText(es, &rc, TRUE);
1808 /*********************************************************************
1810 * EDIT_ML_InvalidateText
1812 * Called from EDIT_InvalidateText().
1813 * Does the job for multi-line controls only.
1816 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1818 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1819 INT sl = EDIT_EM_LineFromChar(es, start);
1820 INT el = EDIT_EM_LineFromChar(es, end);
1821 INT sc;
1822 INT ec;
1823 RECT rc1;
1824 RECT rcWnd;
1825 RECT rcLine;
1826 RECT rcUpdate;
1827 INT l;
1829 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1830 return;
1832 sc = start - EDIT_EM_LineIndex(es, sl);
1833 ec = end - EDIT_EM_LineIndex(es, el);
1834 if (sl < es->y_offset) {
1835 sl = es->y_offset;
1836 sc = 0;
1838 if (el > es->y_offset + vlc) {
1839 el = es->y_offset + vlc;
1840 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1842 GetClientRect(es->hwndSelf, &rc1);
1843 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1844 if (sl == el) {
1845 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1846 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1847 EDIT_UpdateText(es, &rcUpdate, TRUE);
1848 } else {
1849 EDIT_GetLineRect(es, sl, sc,
1850 EDIT_EM_LineLength(es,
1851 EDIT_EM_LineIndex(es, sl)),
1852 &rcLine);
1853 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1854 EDIT_UpdateText(es, &rcUpdate, TRUE);
1855 for (l = sl + 1 ; l < el ; l++) {
1856 EDIT_GetLineRect(es, l, 0,
1857 EDIT_EM_LineLength(es,
1858 EDIT_EM_LineIndex(es, l)),
1859 &rcLine);
1860 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1861 EDIT_UpdateText(es, &rcUpdate, TRUE);
1863 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1864 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1865 EDIT_UpdateText(es, &rcUpdate, TRUE);
1870 /*********************************************************************
1872 * EDIT_InvalidateText
1874 * Invalidate the text from offset start up to, but not including,
1875 * offset end. Useful for (re)painting the selection.
1876 * Regions outside the linewidth are not invalidated.
1877 * end == -1 means end == TextLength.
1878 * start and end need not be ordered.
1881 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1883 if (end == start)
1884 return;
1886 if (end == -1)
1887 end = get_text_length(es);
1889 if (end < start) {
1890 INT tmp = start;
1891 start = end;
1892 end = tmp;
1895 if (es->style & ES_MULTILINE)
1896 EDIT_ML_InvalidateText(es, start, end);
1897 else
1898 EDIT_SL_InvalidateText(es, start, end);
1902 /*********************************************************************
1904 * EDIT_MakeFit
1906 * Try to fit size + 1 characters in the buffer.
1908 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1910 HLOCAL hNew32W;
1912 if (size <= es->buffer_size)
1913 return TRUE;
1915 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1917 /* Force edit to unlock it's buffer. es->text now NULL */
1918 EDIT_UnlockBuffer(es, TRUE);
1920 if (es->hloc32W) {
1921 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1922 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1923 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1924 es->hloc32W = hNew32W;
1925 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1929 EDIT_LockBuffer(es);
1931 if (es->buffer_size < size) {
1932 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1933 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1934 return FALSE;
1935 } else {
1936 TRACE("We now have %d+1\n", es->buffer_size);
1937 return TRUE;
1942 /*********************************************************************
1944 * EDIT_MakeUndoFit
1946 * Try to fit size + 1 bytes in the undo buffer.
1949 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1951 UINT alloc_size;
1953 if (size <= es->undo_buffer_size)
1954 return TRUE;
1956 TRACE("trying to ReAlloc to %d+1\n", size);
1958 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1959 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1960 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1961 return TRUE;
1963 else
1965 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1966 return FALSE;
1971 /*********************************************************************
1973 * EDIT_MoveBackward
1976 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1978 INT e = es->selection_end;
1980 if (e) {
1981 e--;
1982 if ((es->style & ES_MULTILINE) && e &&
1983 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1984 e--;
1985 if (e && (es->text[e - 1] == '\r'))
1986 e--;
1989 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1990 EDIT_EM_ScrollCaret(es);
1994 /*********************************************************************
1996 * EDIT_MoveDown_ML
1998 * Only for multi line controls
1999 * Move the caret one line down, on a column with the nearest
2000 * x coordinate on the screen (might be a different column).
2003 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
2005 INT s = es->selection_start;
2006 INT e = es->selection_end;
2007 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2008 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2009 INT x = (short)LOWORD(pos);
2010 INT y = (short)HIWORD(pos);
2012 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
2013 if (!extend)
2014 s = e;
2015 EDIT_EM_SetSel(es, s, e, after_wrap);
2016 EDIT_EM_ScrollCaret(es);
2020 /*********************************************************************
2022 * EDIT_MoveEnd
2025 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
2027 BOOL after_wrap = FALSE;
2028 INT e;
2030 /* Pass a high value in x to make sure of receiving the end of the line */
2031 if (es->style & ES_MULTILINE)
2032 e = EDIT_CharFromPos(es, 0x3fffffff,
2033 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2034 else
2035 e = get_text_length(es);
2036 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2037 EDIT_EM_ScrollCaret(es);
2041 /*********************************************************************
2043 * EDIT_MoveForward
2046 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2048 INT e = es->selection_end;
2050 if (es->text[e]) {
2051 e++;
2052 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2053 if (es->text[e] == '\n')
2054 e++;
2055 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2056 e += 2;
2059 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2060 EDIT_EM_ScrollCaret(es);
2064 /*********************************************************************
2066 * EDIT_MoveHome
2068 * Home key: move to beginning of line.
2071 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2073 INT e;
2075 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2076 if (es->style & ES_MULTILINE)
2077 e = EDIT_CharFromPos(es, -es->x_offset,
2078 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2079 else
2080 e = 0;
2081 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2082 EDIT_EM_ScrollCaret(es);
2086 /*********************************************************************
2088 * EDIT_MovePageDown_ML
2090 * Only for multi line controls
2091 * Move the caret one page down, on a column with the nearest
2092 * x coordinate on the screen (might be a different column).
2095 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2097 INT s = es->selection_start;
2098 INT e = es->selection_end;
2099 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2100 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2101 INT x = (short)LOWORD(pos);
2102 INT y = (short)HIWORD(pos);
2104 e = EDIT_CharFromPos(es, x,
2105 y + (es->format_rect.bottom - es->format_rect.top),
2106 &after_wrap);
2107 if (!extend)
2108 s = e;
2109 EDIT_EM_SetSel(es, s, e, after_wrap);
2110 EDIT_EM_ScrollCaret(es);
2114 /*********************************************************************
2116 * EDIT_MovePageUp_ML
2118 * Only for multi line controls
2119 * Move the caret one page up, on a column with the nearest
2120 * x coordinate on the screen (might be a different column).
2123 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2125 INT s = es->selection_start;
2126 INT e = es->selection_end;
2127 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2128 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2129 INT x = (short)LOWORD(pos);
2130 INT y = (short)HIWORD(pos);
2132 e = EDIT_CharFromPos(es, x,
2133 y - (es->format_rect.bottom - es->format_rect.top),
2134 &after_wrap);
2135 if (!extend)
2136 s = e;
2137 EDIT_EM_SetSel(es, s, e, after_wrap);
2138 EDIT_EM_ScrollCaret(es);
2142 /*********************************************************************
2144 * EDIT_MoveUp_ML
2146 * Only for multi line controls
2147 * Move the caret one line up, on a column with the nearest
2148 * x coordinate on the screen (might be a different column).
2151 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2153 INT s = es->selection_start;
2154 INT e = es->selection_end;
2155 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2156 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2157 INT x = (short)LOWORD(pos);
2158 INT y = (short)HIWORD(pos);
2160 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2161 if (!extend)
2162 s = e;
2163 EDIT_EM_SetSel(es, s, e, after_wrap);
2164 EDIT_EM_ScrollCaret(es);
2168 /*********************************************************************
2170 * EDIT_MoveWordBackward
2173 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2175 INT s = es->selection_start;
2176 INT e = es->selection_end;
2177 INT l;
2178 INT ll;
2179 INT li;
2181 l = EDIT_EM_LineFromChar(es, e);
2182 ll = EDIT_EM_LineLength(es, e);
2183 li = EDIT_EM_LineIndex(es, l);
2184 if (e - li == 0) {
2185 if (l) {
2186 li = EDIT_EM_LineIndex(es, l - 1);
2187 e = li + EDIT_EM_LineLength(es, li);
2189 } else {
2190 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2192 if (!extend)
2193 s = e;
2194 EDIT_EM_SetSel(es, s, e, FALSE);
2195 EDIT_EM_ScrollCaret(es);
2199 /*********************************************************************
2201 * EDIT_MoveWordForward
2204 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2206 INT s = es->selection_start;
2207 INT e = es->selection_end;
2208 INT l;
2209 INT ll;
2210 INT li;
2212 l = EDIT_EM_LineFromChar(es, e);
2213 ll = EDIT_EM_LineLength(es, e);
2214 li = EDIT_EM_LineIndex(es, l);
2215 if (e - li == ll) {
2216 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2217 e = EDIT_EM_LineIndex(es, l + 1);
2218 } else {
2219 e = li + EDIT_CallWordBreakProc(es,
2220 li, e - li + 1, ll, WB_RIGHT);
2222 if (!extend)
2223 s = e;
2224 EDIT_EM_SetSel(es, s, e, FALSE);
2225 EDIT_EM_ScrollCaret(es);
2229 /*********************************************************************
2231 * EDIT_PaintLine
2234 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2236 INT s = es->selection_start;
2237 INT e = es->selection_end;
2238 INT li;
2239 INT ll;
2240 INT x;
2241 INT y;
2242 LRESULT pos;
2244 if (es->style & ES_MULTILINE) {
2245 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2246 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2247 return;
2248 } else if (line)
2249 return;
2251 TRACE("line=%d\n", line);
2253 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2254 x = (short)LOWORD(pos);
2255 y = (short)HIWORD(pos);
2256 li = EDIT_EM_LineIndex(es, line);
2257 ll = EDIT_EM_LineLength(es, li);
2258 s = min(es->selection_start, es->selection_end);
2259 e = max(es->selection_start, es->selection_end);
2260 s = min(li + ll, max(li, s));
2261 e = min(li + ll, max(li, e));
2262 if (rev && (s != e) &&
2263 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2264 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2265 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2266 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2267 } else
2268 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2272 /*********************************************************************
2274 * EDIT_PaintText
2277 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2279 COLORREF BkColor;
2280 COLORREF TextColor;
2281 LOGFONTW underline_font;
2282 HFONT hUnderline = 0;
2283 HFONT old_font = 0;
2284 INT ret;
2285 INT li;
2286 INT BkMode;
2287 SIZE size;
2289 if (!count)
2290 return 0;
2291 BkMode = GetBkMode(dc);
2292 BkColor = GetBkColor(dc);
2293 TextColor = GetTextColor(dc);
2294 if (rev) {
2295 if (es->composition_len == 0)
2297 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2298 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2299 SetBkMode( dc, OPAQUE);
2301 else
2303 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2304 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2305 underline_font.lfUnderline = TRUE;
2306 hUnderline = CreateFontIndirectW(&underline_font);
2307 old_font = SelectObject(dc,hUnderline);
2310 li = EDIT_EM_LineIndex(es, line);
2311 if (es->style & ES_MULTILINE) {
2312 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2313 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2314 } else {
2315 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2316 TextOutW(dc, x, y, text + li + col, count);
2317 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2318 ret = size.cx;
2319 if (es->style & ES_PASSWORD)
2320 HeapFree(GetProcessHeap(), 0, text);
2322 if (rev) {
2323 if (es->composition_len == 0)
2325 SetBkColor(dc, BkColor);
2326 SetTextColor(dc, TextColor);
2327 SetBkMode( dc, BkMode);
2329 else
2331 if (old_font)
2332 SelectObject(dc,old_font);
2333 if (hUnderline)
2334 DeleteObject(hUnderline);
2337 return ret;
2341 /*********************************************************************
2343 * EDIT_SetCaretPos
2346 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2347 BOOL after_wrap)
2349 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2350 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2351 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2355 /*********************************************************************
2357 * EDIT_AdjustFormatRect
2359 * Adjusts the format rectangle for the current font and the
2360 * current client rectangle.
2363 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2365 RECT ClientRect;
2367 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2368 if (es->style & ES_MULTILINE)
2370 INT fw, vlc, max_x_offset, max_y_offset;
2372 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2373 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2375 /* correct es->x_offset */
2376 fw = es->format_rect.right - es->format_rect.left;
2377 max_x_offset = es->text_width - fw;
2378 if(max_x_offset < 0) max_x_offset = 0;
2379 if(es->x_offset > max_x_offset)
2380 es->x_offset = max_x_offset;
2382 /* correct es->y_offset */
2383 max_y_offset = es->line_count - vlc;
2384 if(max_y_offset < 0) max_y_offset = 0;
2385 if(es->y_offset > max_y_offset)
2386 es->y_offset = max_y_offset;
2388 /* force scroll info update */
2389 EDIT_UpdateScrollInfo(es);
2391 else
2392 /* Windows doesn't care to fix text placement for SL controls */
2393 es->format_rect.bottom = es->format_rect.top + es->line_height;
2395 /* Always stay within the client area */
2396 GetClientRect(es->hwndSelf, &ClientRect);
2397 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2399 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2400 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2402 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2406 /*********************************************************************
2408 * EDIT_SetRectNP
2410 * note: this is not (exactly) the handler called on EM_SETRECTNP
2411 * it is also used to set the rect of a single line control
2414 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2416 LONG_PTR ExStyle;
2417 INT bw, bh;
2418 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2420 CopyRect(&es->format_rect, rc);
2422 if (ExStyle & WS_EX_CLIENTEDGE) {
2423 es->format_rect.left++;
2424 es->format_rect.right--;
2426 if (es->format_rect.bottom - es->format_rect.top
2427 >= es->line_height + 2)
2429 es->format_rect.top++;
2430 es->format_rect.bottom--;
2433 else if (es->style & WS_BORDER) {
2434 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2435 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2436 es->format_rect.left += bw;
2437 es->format_rect.right -= bw;
2438 if (es->format_rect.bottom - es->format_rect.top
2439 >= es->line_height + 2 * bh)
2441 es->format_rect.top += bh;
2442 es->format_rect.bottom -= bh;
2446 es->format_rect.left += es->left_margin;
2447 es->format_rect.right -= es->right_margin;
2448 EDIT_AdjustFormatRect(es);
2452 /*********************************************************************
2454 * EDIT_UnlockBuffer
2457 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2460 /* Edit window might be already destroyed */
2461 if(!IsWindow(es->hwndSelf))
2463 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2464 return;
2467 if (!es->lock_count) {
2468 ERR("lock_count == 0 ... please report\n");
2469 return;
2471 if (!es->text) {
2472 ERR("es->text == 0 ... please report\n");
2473 return;
2476 if (force || (es->lock_count == 1)) {
2477 if (es->hloc32W) {
2478 CHAR *textA = NULL;
2479 UINT countA = 0;
2480 UINT countW = get_text_length(es) + 1;
2481 STACK16FRAME* stack16 = NULL;
2482 HANDLE16 oldDS = 0;
2484 if(es->hloc32A)
2486 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2487 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2488 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2489 countA = LocalSize(es->hloc32A);
2490 if(countA_new > countA)
2492 HLOCAL hloc32A_new;
2493 UINT alloc_size = ROUND_TO_GROW(countA_new);
2494 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2495 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2496 if(hloc32A_new)
2498 es->hloc32A = hloc32A_new;
2499 countA = LocalSize(hloc32A_new);
2500 TRACE("Real new size %d bytes\n", countA);
2502 else
2503 WARN("FAILED! Will synchronize partially\n");
2505 textA = LocalLock(es->hloc32A);
2507 else if(es->hloc16)
2509 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2511 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2512 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2514 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2515 oldDS = stack16->ds;
2516 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2518 countA = LocalSize16(es->hloc16);
2519 if(countA_new > countA)
2521 HLOCAL16 hloc16_new;
2522 UINT alloc_size = ROUND_TO_GROW(countA_new);
2523 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2524 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2525 if(hloc16_new)
2527 es->hloc16 = hloc16_new;
2528 countA = LocalSize16(hloc16_new);
2529 TRACE("Real new size %d bytes\n", countA);
2531 else
2532 WARN("FAILED! Will synchronize partially\n");
2534 textA = MapSL(LocalLock16(es->hloc16));
2537 if(textA)
2539 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2540 if(stack16)
2541 LocalUnlock16(es->hloc16);
2542 else
2543 LocalUnlock(es->hloc32A);
2546 if (stack16) stack16->ds = oldDS;
2547 LocalUnlock(es->hloc32W);
2548 es->text = NULL;
2550 else {
2551 ERR("no buffer ... please report\n");
2552 return;
2555 es->lock_count--;
2559 /*********************************************************************
2561 * EDIT_UpdateScrollInfo
2564 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2566 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2568 SCROLLINFO si;
2569 si.cbSize = sizeof(SCROLLINFO);
2570 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2571 si.nMin = 0;
2572 si.nMax = es->line_count - 1;
2573 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2574 si.nPos = es->y_offset;
2575 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2576 si.nMin, si.nMax, si.nPage, si.nPos);
2577 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2580 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2582 SCROLLINFO si;
2583 si.cbSize = sizeof(SCROLLINFO);
2584 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2585 si.nMin = 0;
2586 si.nMax = es->text_width - 1;
2587 si.nPage = es->format_rect.right - es->format_rect.left;
2588 si.nPos = es->x_offset;
2589 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2590 si.nMin, si.nMax, si.nPage, si.nPos);
2591 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2595 /*********************************************************************
2597 * EDIT_WordBreakProc
2599 * Find the beginning of words.
2600 * Note: unlike the specs for a WordBreakProc, this function only
2601 * allows to be called without linebreaks between s[0] up to
2602 * s[count - 1]. Remember it is only called
2603 * internally, so we can decide this for ourselves.
2606 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2608 INT ret = 0;
2610 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2612 if(!s) return 0;
2614 switch (action) {
2615 case WB_LEFT:
2616 if (!count)
2617 break;
2618 if (index)
2619 index--;
2620 if (s[index] == ' ') {
2621 while (index && (s[index] == ' '))
2622 index--;
2623 if (index) {
2624 while (index && (s[index] != ' '))
2625 index--;
2626 if (s[index] == ' ')
2627 index++;
2629 } else {
2630 while (index && (s[index] != ' '))
2631 index--;
2632 if (s[index] == ' ')
2633 index++;
2635 ret = index;
2636 break;
2637 case WB_RIGHT:
2638 if (!count)
2639 break;
2640 if (index)
2641 index--;
2642 if (s[index] == ' ')
2643 while ((index < count) && (s[index] == ' ')) index++;
2644 else {
2645 while (s[index] && (s[index] != ' ') && (index < count))
2646 index++;
2647 while ((s[index] == ' ') && (index < count)) index++;
2649 ret = index;
2650 break;
2651 case WB_ISDELIMITER:
2652 ret = (s[index] == ' ');
2653 break;
2654 default:
2655 ERR("unknown action code, please report !\n");
2656 break;
2658 return ret;
2662 /*********************************************************************
2664 * EM_CHARFROMPOS
2666 * returns line number (not index) in high-order word of result.
2667 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2668 * to Richedit, not to the edit control. Original documentation is valid.
2669 * FIXME: do the specs mean to return -1 if outside client area or
2670 * if outside formatting rectangle ???
2673 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2675 POINT pt;
2676 RECT rc;
2677 INT index;
2679 pt.x = x;
2680 pt.y = y;
2681 GetClientRect(es->hwndSelf, &rc);
2682 if (!PtInRect(&rc, pt))
2683 return -1;
2685 index = EDIT_CharFromPos(es, x, y, NULL);
2686 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2690 /*********************************************************************
2692 * EM_FMTLINES
2694 * Enable or disable soft breaks.
2696 * This means: insert or remove the soft linebreak character (\r\r\n).
2697 * Take care to check if the text still fits the buffer after insertion.
2698 * If not, notify with EN_ERRSPACE.
2701 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2703 es->flags &= ~EF_USE_SOFTBRK;
2704 if (add_eol) {
2705 es->flags |= EF_USE_SOFTBRK;
2706 FIXME("soft break enabled, not implemented\n");
2708 return add_eol;
2712 /*********************************************************************
2714 * EM_GETHANDLE
2716 * Hopefully this won't fire back at us.
2717 * We always start with a fixed buffer in the local heap.
2718 * Despite of the documentation says that the local heap is used
2719 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2720 * buffer on the local heap.
2723 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2725 HLOCAL hLocal;
2727 if (!(es->style & ES_MULTILINE))
2728 return 0;
2730 if(es->is_unicode)
2731 hLocal = es->hloc32W;
2732 else
2734 if(!es->hloc32A)
2736 CHAR *textA;
2737 UINT countA, alloc_size;
2738 TRACE("Allocating 32-bit ANSI alias buffer\n");
2739 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2740 alloc_size = ROUND_TO_GROW(countA);
2741 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2743 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2744 return 0;
2746 textA = LocalLock(es->hloc32A);
2747 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2748 LocalUnlock(es->hloc32A);
2750 hLocal = es->hloc32A;
2753 es->flags |= EF_APP_HAS_HANDLE;
2754 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2755 return hLocal;
2759 /*********************************************************************
2761 * EM_GETHANDLE16
2763 * Hopefully this won't fire back at us.
2764 * We always start with a buffer in 32 bit linear memory.
2765 * However, with this message a 16 bit application requests
2766 * a handle of 16 bit local heap memory, where it expects to find
2767 * the text.
2768 * It's a pitty that from this moment on we have to use this
2769 * local heap, because applications may rely on the handle
2770 * in the future.
2772 * In this function we'll try to switch to local heap.
2774 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2776 CHAR *textA;
2777 UINT countA, alloc_size;
2778 STACK16FRAME* stack16;
2779 HANDLE16 oldDS;
2781 if (!(es->style & ES_MULTILINE))
2782 return 0;
2784 if (es->hloc16)
2785 return es->hloc16;
2787 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2788 oldDS = stack16->ds;
2789 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2791 if (!LocalHeapSize16()) {
2793 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2794 ERR("could not initialize local heap\n");
2795 goto done;
2797 TRACE("local heap initialized\n");
2800 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2801 alloc_size = ROUND_TO_GROW(countA);
2803 TRACE("Allocating 16-bit ANSI alias buffer\n");
2804 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2805 ERR("could not allocate new 16 bit buffer\n");
2806 goto done;
2809 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2810 ERR("could not lock new 16 bit buffer\n");
2811 LocalFree16(es->hloc16);
2812 es->hloc16 = 0;
2813 goto done;
2816 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2817 LocalUnlock16(es->hloc16);
2818 es->flags |= EF_APP_HAS_HANDLE;
2820 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2822 done:
2823 stack16->ds = oldDS;
2824 return es->hloc16;
2828 /*********************************************************************
2830 * EM_GETLINE
2833 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2835 LPWSTR src;
2836 INT line_len, dst_len;
2837 INT i;
2839 if (es->style & ES_MULTILINE) {
2840 if (line >= es->line_count)
2841 return 0;
2842 } else
2843 line = 0;
2844 i = EDIT_EM_LineIndex(es, line);
2845 src = es->text + i;
2846 line_len = EDIT_EM_LineLength(es, i);
2847 dst_len = *(WORD *)dst;
2848 if(unicode)
2850 if(dst_len <= line_len)
2852 memcpy(dst, src, dst_len * sizeof(WCHAR));
2853 return dst_len;
2855 else /* Append 0 if enough space */
2857 memcpy(dst, src, line_len * sizeof(WCHAR));
2858 dst[line_len] = 0;
2859 return line_len;
2862 else
2864 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2865 if(!ret && line_len) /* Insufficient buffer size */
2866 return dst_len;
2867 if(ret < dst_len) /* Append 0 if enough space */
2868 ((LPSTR)dst)[ret] = 0;
2869 return ret;
2874 /*********************************************************************
2876 * EM_GETSEL
2879 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2881 UINT s = es->selection_start;
2882 UINT e = es->selection_end;
2884 ORDER_UINT(s, e);
2885 if (start)
2886 *start = s;
2887 if (end)
2888 *end = e;
2889 return MAKELONG(s, e);
2893 /*********************************************************************
2895 * EM_GETTHUMB
2897 * FIXME: is this right ? (or should it be only VSCROLL)
2898 * (and maybe only for edit controls that really have their
2899 * own scrollbars) (and maybe only for multiline controls ?)
2900 * All in all: very poorly documented
2903 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2905 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2906 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2910 /*********************************************************************
2912 * EM_LINEFROMCHAR
2915 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2917 INT line;
2918 LINEDEF *line_def;
2920 if (!(es->style & ES_MULTILINE))
2921 return 0;
2922 if (index > (INT)get_text_length(es))
2923 return es->line_count - 1;
2924 if (index == -1)
2925 index = min(es->selection_start, es->selection_end);
2927 line = 0;
2928 line_def = es->first_line_def;
2929 index -= line_def->length;
2930 while ((index >= 0) && line_def->next) {
2931 line++;
2932 line_def = line_def->next;
2933 index -= line_def->length;
2935 return line;
2939 /*********************************************************************
2941 * EM_LINEINDEX
2944 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
2946 INT line_index;
2947 const LINEDEF *line_def;
2949 if (!(es->style & ES_MULTILINE))
2950 return 0;
2951 if (line >= es->line_count)
2952 return -1;
2954 line_index = 0;
2955 line_def = es->first_line_def;
2956 if (line == -1) {
2957 INT index = es->selection_end - line_def->length;
2958 while ((index >= 0) && line_def->next) {
2959 line_index += line_def->length;
2960 line_def = line_def->next;
2961 index -= line_def->length;
2963 } else {
2964 while (line > 0) {
2965 line_index += line_def->length;
2966 line_def = line_def->next;
2967 line--;
2970 return line_index;
2974 /*********************************************************************
2976 * EM_LINELENGTH
2979 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2981 LINEDEF *line_def;
2983 if (!(es->style & ES_MULTILINE))
2984 return get_text_length(es);
2986 if (index == -1) {
2987 /* get the number of remaining non-selected chars of selected lines */
2988 INT32 l; /* line number */
2989 INT32 li; /* index of first char in line */
2990 INT32 count;
2991 l = EDIT_EM_LineFromChar(es, es->selection_start);
2992 /* # chars before start of selection area */
2993 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2994 l = EDIT_EM_LineFromChar(es, es->selection_end);
2995 /* # chars after end of selection */
2996 li = EDIT_EM_LineIndex(es, l);
2997 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2998 return count;
3000 line_def = es->first_line_def;
3001 index -= line_def->length;
3002 while ((index >= 0) && line_def->next) {
3003 line_def = line_def->next;
3004 index -= line_def->length;
3006 return line_def->net_length;
3010 /*********************************************************************
3012 * EM_LINESCROLL
3014 * NOTE: dx is in average character widths, dy - in lines;
3017 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
3019 if (!(es->style & ES_MULTILINE))
3020 return FALSE;
3022 dx *= es->char_width;
3023 return EDIT_EM_LineScroll_internal(es, dx, dy);
3026 /*********************************************************************
3028 * EDIT_EM_LineScroll_internal
3030 * Version of EDIT_EM_LineScroll for internal use.
3031 * It doesn't refuse if ES_MULTILINE is set and assumes that
3032 * dx is in pixels, dy - in lines.
3035 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3037 INT nyoff;
3038 INT x_offset_in_pixels;
3039 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3040 es->line_height;
3042 if (es->style & ES_MULTILINE)
3044 x_offset_in_pixels = es->x_offset;
3046 else
3048 dy = 0;
3049 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3052 if (-dx > x_offset_in_pixels)
3053 dx = -x_offset_in_pixels;
3054 if (dx > es->text_width - x_offset_in_pixels)
3055 dx = es->text_width - x_offset_in_pixels;
3056 nyoff = max(0, es->y_offset + dy);
3057 if (nyoff >= es->line_count - lines_per_page)
3058 nyoff = max(0, es->line_count - lines_per_page);
3059 dy = (es->y_offset - nyoff) * es->line_height;
3060 if (dx || dy) {
3061 RECT rc1;
3062 RECT rc;
3064 es->y_offset = nyoff;
3065 if(es->style & ES_MULTILINE)
3066 es->x_offset += dx;
3067 else
3068 es->x_offset += dx / es->char_width;
3070 GetClientRect(es->hwndSelf, &rc1);
3071 IntersectRect(&rc, &rc1, &es->format_rect);
3072 ScrollWindowEx(es->hwndSelf, -dx, dy,
3073 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3074 /* force scroll info update */
3075 EDIT_UpdateScrollInfo(es);
3077 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3078 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3079 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3080 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3081 return TRUE;
3085 /*********************************************************************
3087 * EM_POSFROMCHAR
3090 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3092 INT len = get_text_length(es);
3093 INT l;
3094 INT li;
3095 INT x;
3096 INT y = 0;
3097 INT w;
3098 INT lw = 0;
3099 INT ll = 0;
3100 HDC dc;
3101 HFONT old_font = 0;
3102 SIZE size;
3103 LINEDEF *line_def;
3105 index = min(index, len);
3106 dc = GetDC(es->hwndSelf);
3107 if (es->font)
3108 old_font = SelectObject(dc, es->font);
3109 if (es->style & ES_MULTILINE) {
3110 l = EDIT_EM_LineFromChar(es, index);
3111 y = (l - es->y_offset) * es->line_height;
3112 li = EDIT_EM_LineIndex(es, l);
3113 if (after_wrap && (li == index) && l) {
3114 INT l2 = l - 1;
3115 line_def = es->first_line_def;
3116 while (l2) {
3117 line_def = line_def->next;
3118 l2--;
3120 if (line_def->ending == END_WRAP) {
3121 l--;
3122 y -= es->line_height;
3123 li = EDIT_EM_LineIndex(es, l);
3127 line_def = es->first_line_def;
3128 while (line_def->index != li)
3129 line_def = line_def->next;
3131 ll = line_def->net_length;
3132 lw = line_def->width;
3134 w = es->format_rect.right - es->format_rect.left;
3135 if (es->style & ES_RIGHT)
3137 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3138 es->tabs_count, es->tabs)) - es->x_offset;
3139 x = w - x;
3141 else if (es->style & ES_CENTER)
3143 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3144 es->tabs_count, es->tabs)) - es->x_offset;
3145 x += (w - lw) / 2;
3147 else /* ES_LEFT */
3149 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3150 es->tabs_count, es->tabs)) - es->x_offset;
3152 } else {
3153 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3154 if (index < es->x_offset) {
3155 GetTextExtentPoint32W(dc, text + index,
3156 es->x_offset - index, &size);
3157 x = -size.cx;
3158 } else {
3159 GetTextExtentPoint32W(dc, text + es->x_offset,
3160 index - es->x_offset, &size);
3161 x = size.cx;
3163 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3165 w = es->format_rect.right - es->format_rect.left;
3166 if (w > es->text_width)
3168 if (es->style & ES_RIGHT)
3169 x += w - es->text_width;
3170 else if (es->style & ES_CENTER)
3171 x += (w - es->text_width) / 2;
3175 y = 0;
3176 if (es->style & ES_PASSWORD)
3177 HeapFree(GetProcessHeap(), 0, text);
3179 x += es->format_rect.left;
3180 y += es->format_rect.top;
3181 if (es->font)
3182 SelectObject(dc, old_font);
3183 ReleaseDC(es->hwndSelf, dc);
3184 return MAKELONG((INT16)x, (INT16)y);
3188 /*********************************************************************
3190 * EM_REPLACESEL
3192 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3195 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3197 UINT strl = strlenW(lpsz_replace);
3198 UINT tl = get_text_length(es);
3199 UINT utl;
3200 UINT s;
3201 UINT e;
3202 UINT i;
3203 UINT size;
3204 LPWSTR p;
3205 HRGN hrgn = 0;
3206 LPWSTR buf = NULL;
3207 UINT bufl = 0;
3209 TRACE("%s, can_undo %d, send_update %d\n",
3210 debugstr_w(lpsz_replace), can_undo, send_update);
3212 s = es->selection_start;
3213 e = es->selection_end;
3215 if ((s == e) && !strl)
3216 return;
3218 ORDER_UINT(s, e);
3220 size = tl - (e - s) + strl;
3221 if (!size)
3222 es->text_width = 0;
3224 /* Issue the EN_MAXTEXT notification and continue with replacing text
3225 * such that buffer limit is honored. */
3226 if ((honor_limit) && (size > es->buffer_limit)) {
3227 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3228 /* Buffer limit can be smaller than the actual length of text in combobox */
3229 if (es->buffer_limit < (tl - (e-s)))
3230 strl = 0;
3231 else
3232 strl = es->buffer_limit - (tl - (e-s));
3235 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3236 return;
3238 if (e != s) {
3239 /* there is something to be deleted */
3240 TRACE("deleting stuff.\n");
3241 bufl = e - s;
3242 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3243 if (!buf) return;
3244 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3245 buf[bufl] = 0; /* ensure 0 termination */
3246 /* now delete */
3247 strcpyW(es->text + s, es->text + e);
3248 text_buffer_changed(es);
3250 if (strl) {
3251 /* there is an insertion */
3252 tl = get_text_length(es);
3253 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));
3254 for (p = es->text + tl ; p >= es->text + s ; p--)
3255 p[strl] = p[0];
3256 for (i = 0 , p = es->text + s ; i < strl ; i++)
3257 p[i] = lpsz_replace[i];
3258 if(es->style & ES_UPPERCASE)
3259 CharUpperBuffW(p, strl);
3260 else if(es->style & ES_LOWERCASE)
3261 CharLowerBuffW(p, strl);
3262 text_buffer_changed(es);
3264 if (es->style & ES_MULTILINE)
3266 INT st = min(es->selection_start, es->selection_end);
3267 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3269 hrgn = CreateRectRgn(0, 0, 0, 0);
3270 EDIT_BuildLineDefs_ML(es, st, st + strl,
3271 strl - abs(es->selection_end - es->selection_start), hrgn);
3272 /* if text is too long undo all changes */
3273 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3274 if (strl)
3275 strcpyW(es->text + e, es->text + e + strl);
3276 if (e != s)
3277 for (i = 0 , p = es->text ; i < e - s ; i++)
3278 p[i + s] = buf[i];
3279 text_buffer_changed(es);
3280 EDIT_BuildLineDefs_ML(es, s, e,
3281 abs(es->selection_end - es->selection_start) - strl, hrgn);
3282 strl = 0;
3283 e = s;
3284 hrgn = CreateRectRgn(0, 0, 0, 0);
3285 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3288 else {
3289 INT fw = es->format_rect.right - es->format_rect.left;
3290 EDIT_CalcLineWidth_SL(es);
3291 /* remove chars that don't fit */
3292 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3293 while ((es->text_width > fw) && s + strl >= s) {
3294 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3295 strl--;
3296 EDIT_CalcLineWidth_SL(es);
3298 text_buffer_changed(es);
3299 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3303 if (e != s) {
3304 if (can_undo) {
3305 utl = strlenW(es->undo_text);
3306 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3307 /* undo-buffer is extended to the right */
3308 EDIT_MakeUndoFit(es, utl + e - s);
3309 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3310 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3311 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3312 /* undo-buffer is extended to the left */
3313 EDIT_MakeUndoFit(es, utl + e - s);
3314 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3315 p[e - s] = p[0];
3316 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3317 p[i] = buf[i];
3318 es->undo_position = s;
3319 } else {
3320 /* new undo-buffer */
3321 EDIT_MakeUndoFit(es, e - s);
3322 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3323 es->undo_text[e - s] = 0; /* ensure 0 termination */
3324 es->undo_position = s;
3326 /* any deletion makes the old insertion-undo invalid */
3327 es->undo_insert_count = 0;
3328 } else
3329 EDIT_EM_EmptyUndoBuffer(es);
3331 if (strl) {
3332 if (can_undo) {
3333 if ((s == es->undo_position) ||
3334 ((es->undo_insert_count) &&
3335 (s == es->undo_position + es->undo_insert_count)))
3337 * insertion is new and at delete position or
3338 * an extension to either left or right
3340 es->undo_insert_count += strl;
3341 else {
3342 /* new insertion undo */
3343 es->undo_position = s;
3344 es->undo_insert_count = strl;
3345 /* new insertion makes old delete-buffer invalid */
3346 *es->undo_text = '\0';
3348 } else
3349 EDIT_EM_EmptyUndoBuffer(es);
3352 if (bufl)
3353 HeapFree(GetProcessHeap(), 0, buf);
3355 s += strl;
3357 /* If text has been deleted and we're right or center aligned then scroll rightward */
3358 if (es->style & (ES_RIGHT | ES_CENTER))
3360 INT delta = strl - abs(es->selection_end - es->selection_start);
3362 if (delta < 0 && es->x_offset)
3364 if (abs(delta) > es->x_offset)
3365 es->x_offset = 0;
3366 else
3367 es->x_offset += delta;
3371 EDIT_EM_SetSel(es, s, s, FALSE);
3372 es->flags |= EF_MODIFIED;
3373 if (send_update) es->flags |= EF_UPDATE;
3374 if (hrgn)
3376 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3377 DeleteObject(hrgn);
3379 else
3380 EDIT_UpdateText(es, NULL, TRUE);
3382 EDIT_EM_ScrollCaret(es);
3384 /* force scroll info update */
3385 EDIT_UpdateScrollInfo(es);
3388 if(send_update || (es->flags & EF_UPDATE))
3390 es->flags &= ~EF_UPDATE;
3391 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3396 /*********************************************************************
3398 * EM_SCROLL
3401 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3403 INT dy;
3405 if (!(es->style & ES_MULTILINE))
3406 return (LRESULT)FALSE;
3408 dy = 0;
3410 switch (action) {
3411 case SB_LINEUP:
3412 if (es->y_offset)
3413 dy = -1;
3414 break;
3415 case SB_LINEDOWN:
3416 if (es->y_offset < es->line_count - 1)
3417 dy = 1;
3418 break;
3419 case SB_PAGEUP:
3420 if (es->y_offset)
3421 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3422 break;
3423 case SB_PAGEDOWN:
3424 if (es->y_offset < es->line_count - 1)
3425 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3426 break;
3427 default:
3428 return (LRESULT)FALSE;
3430 if (dy) {
3431 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3432 /* check if we are going to move too far */
3433 if(es->y_offset + dy > es->line_count - vlc)
3434 dy = es->line_count - vlc - es->y_offset;
3436 /* Notification is done in EDIT_EM_LineScroll */
3437 if(dy)
3438 EDIT_EM_LineScroll(es, 0, dy);
3440 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3444 /*********************************************************************
3446 * EM_SCROLLCARET
3449 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3451 if (es->style & ES_MULTILINE) {
3452 INT l;
3453 INT li;
3454 INT vlc;
3455 INT ww;
3456 INT cw = es->char_width;
3457 INT x;
3458 INT dy = 0;
3459 INT dx = 0;
3461 l = EDIT_EM_LineFromChar(es, es->selection_end);
3462 li = EDIT_EM_LineIndex(es, l);
3463 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3464 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3465 if (l >= es->y_offset + vlc)
3466 dy = l - vlc + 1 - es->y_offset;
3467 if (l < es->y_offset)
3468 dy = l - es->y_offset;
3469 ww = es->format_rect.right - es->format_rect.left;
3470 if (x < es->format_rect.left)
3471 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3472 if (x > es->format_rect.right)
3473 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3474 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3476 /* check if we are going to move too far */
3477 if(es->x_offset + dx + ww > es->text_width)
3478 dx = es->text_width - ww - es->x_offset;
3479 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3480 EDIT_EM_LineScroll_internal(es, dx, dy);
3482 } else {
3483 INT x;
3484 INT goal;
3485 INT format_width;
3487 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3488 format_width = es->format_rect.right - es->format_rect.left;
3489 if (x < es->format_rect.left) {
3490 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3491 do {
3492 es->x_offset--;
3493 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3494 } while ((x < goal) && es->x_offset);
3495 /* FIXME: use ScrollWindow() somehow to improve performance */
3496 EDIT_UpdateText(es, NULL, TRUE);
3497 } else if (x > es->format_rect.right) {
3498 INT x_last;
3499 INT len = get_text_length(es);
3500 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3501 do {
3502 es->x_offset++;
3503 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3504 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3505 } while ((x > goal) && (x_last > es->format_rect.right));
3506 /* FIXME: use ScrollWindow() somehow to improve performance */
3507 EDIT_UpdateText(es, NULL, TRUE);
3511 if(es->flags & EF_FOCUSED)
3512 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3516 /*********************************************************************
3518 * EM_SETHANDLE
3520 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3523 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3525 if (!(es->style & ES_MULTILINE))
3526 return;
3528 if (!hloc) {
3529 WARN("called with NULL handle\n");
3530 return;
3533 EDIT_UnlockBuffer(es, TRUE);
3535 if(es->hloc16)
3537 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3538 HANDLE16 oldDS = stack16->ds;
3540 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3541 LocalFree16(es->hloc16);
3542 stack16->ds = oldDS;
3543 es->hloc16 = 0;
3546 if(es->is_unicode)
3548 if(es->hloc32A)
3550 LocalFree(es->hloc32A);
3551 es->hloc32A = NULL;
3553 es->hloc32W = hloc;
3555 else
3557 INT countW, countA;
3558 HLOCAL hloc32W_new;
3559 WCHAR *textW;
3560 CHAR *textA;
3562 countA = LocalSize(hloc);
3563 textA = LocalLock(hloc);
3564 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3565 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3567 ERR("Could not allocate new unicode buffer\n");
3568 return;
3570 textW = LocalLock(hloc32W_new);
3571 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3572 LocalUnlock(hloc32W_new);
3573 LocalUnlock(hloc);
3575 if(es->hloc32W)
3576 LocalFree(es->hloc32W);
3578 es->hloc32W = hloc32W_new;
3579 es->hloc32A = hloc;
3582 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3584 es->flags |= EF_APP_HAS_HANDLE;
3585 EDIT_LockBuffer(es);
3587 es->x_offset = es->y_offset = 0;
3588 es->selection_start = es->selection_end = 0;
3589 EDIT_EM_EmptyUndoBuffer(es);
3590 es->flags &= ~EF_MODIFIED;
3591 es->flags &= ~EF_UPDATE;
3592 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3593 EDIT_UpdateText(es, NULL, TRUE);
3594 EDIT_EM_ScrollCaret(es);
3595 /* force scroll info update */
3596 EDIT_UpdateScrollInfo(es);
3600 /*********************************************************************
3602 * EM_SETHANDLE16
3604 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3607 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3609 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3610 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3611 HANDLE16 oldDS = stack16->ds;
3612 INT countW, countA;
3613 HLOCAL hloc32W_new;
3614 WCHAR *textW;
3615 CHAR *textA;
3617 if (!(es->style & ES_MULTILINE))
3618 return;
3620 if (!hloc) {
3621 WARN("called with NULL handle\n");
3622 return;
3625 EDIT_UnlockBuffer(es, TRUE);
3627 if(es->hloc32A)
3629 LocalFree(es->hloc32A);
3630 es->hloc32A = NULL;
3633 stack16->ds = hInstance;
3634 countA = LocalSize16(hloc);
3635 textA = MapSL(LocalLock16(hloc));
3636 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3637 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3639 ERR("Could not allocate new unicode buffer\n");
3640 return;
3642 textW = LocalLock(hloc32W_new);
3643 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3644 LocalUnlock(hloc32W_new);
3645 LocalUnlock16(hloc);
3646 stack16->ds = oldDS;
3648 if(es->hloc32W)
3649 LocalFree(es->hloc32W);
3651 es->hloc32W = hloc32W_new;
3652 es->hloc16 = hloc;
3654 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3656 es->flags |= EF_APP_HAS_HANDLE;
3657 EDIT_LockBuffer(es);
3659 es->x_offset = es->y_offset = 0;
3660 es->selection_start = es->selection_end = 0;
3661 EDIT_EM_EmptyUndoBuffer(es);
3662 es->flags &= ~EF_MODIFIED;
3663 es->flags &= ~EF_UPDATE;
3664 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3665 EDIT_UpdateText(es, NULL, TRUE);
3666 EDIT_EM_ScrollCaret(es);
3667 /* force scroll info update */
3668 EDIT_UpdateScrollInfo(es);
3672 /*********************************************************************
3674 * EM_SETLIMITTEXT
3676 * NOTE: this version currently implements WinNT limits
3679 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
3681 if (!limit) limit = ~0u;
3682 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
3683 es->buffer_limit = limit;
3687 /*********************************************************************
3689 * EM_SETMARGINS
3691 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3692 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3693 * margin according to the textmetrics of the current font.
3695 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3696 * of the char's width as the margin, but this is not how Windows handles this.
3697 * For all other fonts Windows sets the margins to zero.
3699 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3700 * edit control is equal to or larger than a certain size.
3701 * Interestingly if one subtracts both the left and right margins from
3702 * this size one always seems to get an even number. The extents of
3703 * the (four character) string "'**'" match this quite closely, so
3704 * we'll use this until we come up with a better idea.
3706 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3708 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3709 SIZE sz;
3711 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3712 return sz.cx + left + right;
3715 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3716 WORD left, WORD right, BOOL repaint)
3718 TEXTMETRICW tm;
3719 INT default_left_margin = 0; /* in pixels */
3720 INT default_right_margin = 0; /* in pixels */
3722 /* Set the default margins depending on the font */
3723 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3724 HDC dc = GetDC(es->hwndSelf);
3725 HFONT old_font = SelectObject(dc, es->font);
3726 GetTextMetricsW(dc, &tm);
3727 /* The default margins are only non zero for TrueType or Vector fonts */
3728 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3729 int min_size;
3730 RECT rc;
3731 /* This must be calculated more exactly! But how? */
3732 default_left_margin = tm.tmAveCharWidth / 2;
3733 default_right_margin = tm.tmAveCharWidth / 2;
3734 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3735 GetClientRect(es->hwndSelf, &rc);
3736 if(rc.right - rc.left < min_size) {
3737 default_left_margin = es->left_margin;
3738 default_right_margin = es->right_margin;
3741 SelectObject(dc, old_font);
3742 ReleaseDC(es->hwndSelf, dc);
3745 if (action & EC_LEFTMARGIN) {
3746 es->format_rect.left -= es->left_margin;
3747 if (left != EC_USEFONTINFO)
3748 es->left_margin = left;
3749 else
3750 es->left_margin = default_left_margin;
3751 es->format_rect.left += es->left_margin;
3754 if (action & EC_RIGHTMARGIN) {
3755 es->format_rect.right += es->right_margin;
3756 if (right != EC_USEFONTINFO)
3757 es->right_margin = right;
3758 else
3759 es->right_margin = default_right_margin;
3760 es->format_rect.right -= es->right_margin;
3763 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3764 EDIT_AdjustFormatRect(es);
3765 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3768 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3772 /*********************************************************************
3774 * EM_SETPASSWORDCHAR
3777 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3779 LONG style;
3781 if (es->style & ES_MULTILINE)
3782 return;
3784 if (es->password_char == c)
3785 return;
3787 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3788 es->password_char = c;
3789 if (c) {
3790 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3791 es->style |= ES_PASSWORD;
3792 } else {
3793 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3794 es->style &= ~ES_PASSWORD;
3796 EDIT_UpdateText(es, NULL, TRUE);
3800 /*********************************************************************
3802 * EDIT_EM_SetSel
3804 * note: unlike the specs say: the order of start and end
3805 * _is_ preserved in Windows. (i.e. start can be > end)
3806 * In other words: this handler is OK
3809 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3811 UINT old_start = es->selection_start;
3812 UINT old_end = es->selection_end;
3813 UINT len = get_text_length(es);
3815 if (start == (UINT)-1) {
3816 start = es->selection_end;
3817 end = es->selection_end;
3818 } else {
3819 start = min(start, len);
3820 end = min(end, len);
3822 es->selection_start = start;
3823 es->selection_end = end;
3824 if (after_wrap)
3825 es->flags |= EF_AFTER_WRAP;
3826 else
3827 es->flags &= ~EF_AFTER_WRAP;
3828 /* Compute the necessary invalidation region. */
3829 /* Note that we don't need to invalidate regions which have
3830 * "never" been selected, or those which are "still" selected.
3831 * In fact, every time we hit a selection boundary, we can
3832 * *toggle* whether we need to invalidate. Thus we can optimize by
3833 * *sorting* the interval endpoints. Let's assume that we sort them
3834 * in this order:
3835 * start <= end <= old_start <= old_end
3836 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3837 * in 5 comparisons; ie it's impossible to do better than the
3838 * following: */
3839 ORDER_UINT(end, old_end);
3840 ORDER_UINT(start, old_start);
3841 ORDER_UINT(old_start, old_end);
3842 ORDER_UINT(start, end);
3843 /* Note that at this point 'end' and 'old_start' are not in order, but
3844 * start is definitely the min. and old_end is definitely the max. */
3845 if (end != old_start)
3848 * One can also do
3849 * ORDER_UINT32(end, old_start);
3850 * EDIT_InvalidateText(es, start, end);
3851 * EDIT_InvalidateText(es, old_start, old_end);
3852 * in place of the following if statement.
3853 * (That would complete the optimal five-comparison four-element sort.)
3855 if (old_start > end )
3857 EDIT_InvalidateText(es, start, end);
3858 EDIT_InvalidateText(es, old_start, old_end);
3860 else
3862 EDIT_InvalidateText(es, start, old_start);
3863 EDIT_InvalidateText(es, end, old_end);
3866 else EDIT_InvalidateText(es, start, old_end);
3870 /*********************************************************************
3872 * EM_SETTABSTOPS
3875 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3877 if (!(es->style & ES_MULTILINE))
3878 return FALSE;
3879 HeapFree(GetProcessHeap(), 0, es->tabs);
3880 es->tabs_count = count;
3881 if (!count)
3882 es->tabs = NULL;
3883 else {
3884 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3885 memcpy(es->tabs, tabs, count * sizeof(INT));
3887 return TRUE;
3891 /*********************************************************************
3893 * EM_SETTABSTOPS16
3896 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs)
3898 if (!(es->style & ES_MULTILINE))
3899 return FALSE;
3900 HeapFree(GetProcessHeap(), 0, es->tabs);
3901 es->tabs_count = count;
3902 if (!count)
3903 es->tabs = NULL;
3904 else {
3905 INT i;
3906 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3907 for (i = 0 ; i < count ; i++)
3908 es->tabs[i] = *tabs++;
3910 return TRUE;
3914 /*********************************************************************
3916 * EM_SETWORDBREAKPROC
3919 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3921 if (es->word_break_proc == wbp)
3922 return;
3924 es->word_break_proc = wbp;
3925 es->word_break_proc16 = NULL;
3927 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3928 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3929 EDIT_UpdateText(es, NULL, TRUE);
3934 /*********************************************************************
3936 * EM_SETWORDBREAKPROC16
3939 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3941 if (es->word_break_proc16 == wbp)
3942 return;
3944 es->word_break_proc = NULL;
3945 es->word_break_proc16 = wbp;
3946 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3947 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3948 EDIT_UpdateText(es, NULL, TRUE);
3953 /*********************************************************************
3955 * EM_UNDO / WM_UNDO
3958 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3960 INT ulength;
3961 LPWSTR utext;
3963 /* As per MSDN spec, for a single-line edit control,
3964 the return value is always TRUE */
3965 if( es->style & ES_READONLY )
3966 return !(es->style & ES_MULTILINE);
3968 ulength = strlenW(es->undo_text);
3970 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3972 strcpyW(utext, es->undo_text);
3974 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3975 es->undo_insert_count, debugstr_w(utext));
3977 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3978 EDIT_EM_EmptyUndoBuffer(es);
3979 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3980 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3981 /* send the notification after the selection start and end are set */
3982 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3983 EDIT_EM_ScrollCaret(es);
3984 HeapFree(GetProcessHeap(), 0, utext);
3986 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3987 es->undo_insert_count, debugstr_w(es->undo_text));
3988 return TRUE;
3992 /*********************************************************************
3994 * WM_CHAR
3997 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3999 BOOL control;
4001 control = GetKeyState(VK_CONTROL) & 0x8000;
4003 switch (c) {
4004 case '\r':
4005 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
4006 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
4007 break;
4008 case '\n':
4009 if (es->style & ES_MULTILINE) {
4010 if (es->style & ES_READONLY) {
4011 EDIT_MoveHome(es, FALSE);
4012 EDIT_MoveDown_ML(es, FALSE);
4013 } else {
4014 static const WCHAR cr_lfW[] = {'\r','\n',0};
4015 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
4018 break;
4019 case '\t':
4020 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4022 static const WCHAR tabW[] = {'\t',0};
4023 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4025 break;
4026 case VK_BACK:
4027 if (!(es->style & ES_READONLY) && !control) {
4028 if (es->selection_start != es->selection_end)
4029 EDIT_WM_Clear(es);
4030 else {
4031 /* delete character left of caret */
4032 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4033 EDIT_MoveBackward(es, TRUE);
4034 EDIT_WM_Clear(es);
4037 break;
4038 case 0x03: /* ^C */
4039 if (!(es->style & ES_PASSWORD))
4040 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4041 break;
4042 case 0x16: /* ^V */
4043 if (!(es->style & ES_READONLY))
4044 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4045 break;
4046 case 0x18: /* ^X */
4047 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
4048 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4049 break;
4050 case 0x1A: /* ^Z */
4051 if (!(es->style & ES_READONLY))
4052 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
4053 break;
4055 default:
4056 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4057 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4058 break;
4060 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4061 WCHAR str[2];
4062 str[0] = c;
4063 str[1] = '\0';
4064 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4066 break;
4071 /*********************************************************************
4073 * WM_COMMAND
4076 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4078 if (code || control)
4079 return;
4081 switch (id) {
4082 case EM_UNDO:
4083 EDIT_EM_Undo(es);
4084 break;
4085 case WM_CUT:
4086 EDIT_WM_Cut(es);
4087 break;
4088 case WM_COPY:
4089 EDIT_WM_Copy(es);
4090 break;
4091 case WM_PASTE:
4092 EDIT_WM_Paste(es);
4093 break;
4094 case WM_CLEAR:
4095 EDIT_WM_Clear(es);
4096 break;
4097 case EM_SETSEL:
4098 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4099 EDIT_EM_ScrollCaret(es);
4100 break;
4101 default:
4102 ERR("unknown menu item, please report\n");
4103 break;
4108 /*********************************************************************
4110 * WM_CONTEXTMENU
4112 * Note: the resource files resource/sysres_??.rc cannot define a
4113 * single popup menu. Hence we use a (dummy) menubar
4114 * containing the single popup menu as its first item.
4116 * FIXME: the message identifiers have been chosen arbitrarily,
4117 * hence we use MF_BYPOSITION.
4118 * We might as well use the "real" values (anybody knows ?)
4119 * The menu definition is in resources/sysres_??.rc.
4120 * Once these are OK, we better use MF_BYCOMMAND here
4121 * (as we do in EDIT_WM_Command()).
4124 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4126 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4127 HMENU popup = GetSubMenu(menu, 0);
4128 UINT start = es->selection_start;
4129 UINT end = es->selection_end;
4131 ORDER_UINT(start, end);
4133 /* undo */
4134 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4135 /* cut */
4136 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4137 /* copy */
4138 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4139 /* paste */
4140 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4141 /* delete */
4142 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4143 /* select all */
4144 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4146 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4148 RECT rc;
4149 /* Windows places the menu at the edit's center in this case */
4150 GetClientRect(es->hwndSelf, &rc);
4151 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4152 x = rc.left + (rc.right - rc.left) / 2;
4153 y = rc.top + (rc.bottom - rc.top) / 2;
4156 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4157 DestroyMenu(menu);
4161 /*********************************************************************
4163 * WM_COPY
4166 static void EDIT_WM_Copy(EDITSTATE *es)
4168 INT s = min(es->selection_start, es->selection_end);
4169 INT e = max(es->selection_start, es->selection_end);
4170 HGLOBAL hdst;
4171 LPWSTR dst;
4172 DWORD len;
4174 if (e == s) return;
4176 len = e - s;
4177 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4178 dst = GlobalLock(hdst);
4179 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4180 dst[len] = 0; /* ensure 0 termination */
4181 TRACE("%s\n", debugstr_w(dst));
4182 GlobalUnlock(hdst);
4183 OpenClipboard(es->hwndSelf);
4184 EmptyClipboard();
4185 SetClipboardData(CF_UNICODETEXT, hdst);
4186 CloseClipboard();
4190 /*********************************************************************
4192 * WM_CREATE
4195 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4197 RECT clientRect;
4199 TRACE("%s\n", debugstr_w(name));
4201 * To initialize some final structure members, we call some helper
4202 * functions. However, since the EDITSTATE is not consistent (i.e.
4203 * not fully initialized), we should be very careful which
4204 * functions can be called, and in what order.
4206 EDIT_WM_SetFont(es, 0, FALSE);
4207 EDIT_EM_EmptyUndoBuffer(es);
4209 /* We need to calculate the format rect
4210 (applications may send EM_SETMARGINS before the control gets visible) */
4211 GetClientRect(es->hwndSelf, &clientRect);
4212 EDIT_SetRectNP(es, &clientRect);
4214 if (name && *name) {
4215 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4216 /* if we insert text to the editline, the text scrolls out
4217 * of the window, as the caret is placed after the insert
4218 * pos normally; thus we reset es->selection... to 0 and
4219 * update caret
4221 es->selection_start = es->selection_end = 0;
4222 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4223 * Messages are only to be sent when the USER does something to
4224 * change the contents. So I am removing this EN_CHANGE
4226 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4228 EDIT_EM_ScrollCaret(es);
4230 /* force scroll info update */
4231 EDIT_UpdateScrollInfo(es);
4232 /* The rule seems to return 1 here for success */
4233 /* Power Builder masked edit controls will crash */
4234 /* if not. */
4235 /* FIXME: is that in all cases so ? */
4236 return 1;
4240 /*********************************************************************
4242 * WM_DESTROY
4245 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4247 LINEDEF *pc, *pp;
4249 if (es->hloc32W) {
4250 while (LocalUnlock(es->hloc32W)) ;
4251 LocalFree(es->hloc32W);
4253 if (es->hloc32A) {
4254 while (LocalUnlock(es->hloc32A)) ;
4255 LocalFree(es->hloc32A);
4257 if (es->hloc16) {
4258 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4259 HANDLE16 oldDS = stack16->ds;
4261 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4262 while (LocalUnlock16(es->hloc16)) ;
4263 LocalFree16(es->hloc16);
4264 stack16->ds = oldDS;
4267 pc = es->first_line_def;
4268 while (pc)
4270 pp = pc->next;
4271 HeapFree(GetProcessHeap(), 0, pc);
4272 pc = pp;
4275 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4276 HeapFree(GetProcessHeap(), 0, es);
4278 return 0;
4282 /*********************************************************************
4284 * WM_GETTEXT
4287 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4289 if(!count) return 0;
4291 if(unicode)
4293 lstrcpynW(dst, es->text, count);
4294 return strlenW(dst);
4296 else
4298 LPSTR textA = (LPSTR)dst;
4299 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4300 textA[count - 1] = 0; /* ensure 0 termination */
4301 return strlen(textA);
4305 /*********************************************************************
4307 * WM_HSCROLL
4310 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4312 INT dx;
4313 INT fw;
4315 if (!(es->style & ES_MULTILINE))
4316 return 0;
4318 if (!(es->style & ES_AUTOHSCROLL))
4319 return 0;
4321 dx = 0;
4322 fw = es->format_rect.right - es->format_rect.left;
4323 switch (action) {
4324 case SB_LINELEFT:
4325 TRACE("SB_LINELEFT\n");
4326 if (es->x_offset)
4327 dx = -es->char_width;
4328 break;
4329 case SB_LINERIGHT:
4330 TRACE("SB_LINERIGHT\n");
4331 if (es->x_offset < es->text_width)
4332 dx = es->char_width;
4333 break;
4334 case SB_PAGELEFT:
4335 TRACE("SB_PAGELEFT\n");
4336 if (es->x_offset)
4337 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4338 break;
4339 case SB_PAGERIGHT:
4340 TRACE("SB_PAGERIGHT\n");
4341 if (es->x_offset < es->text_width)
4342 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4343 break;
4344 case SB_LEFT:
4345 TRACE("SB_LEFT\n");
4346 if (es->x_offset)
4347 dx = -es->x_offset;
4348 break;
4349 case SB_RIGHT:
4350 TRACE("SB_RIGHT\n");
4351 if (es->x_offset < es->text_width)
4352 dx = es->text_width - es->x_offset;
4353 break;
4354 case SB_THUMBTRACK:
4355 TRACE("SB_THUMBTRACK %d\n", pos);
4356 es->flags |= EF_HSCROLL_TRACK;
4357 if(es->style & WS_HSCROLL)
4358 dx = pos - es->x_offset;
4359 else
4361 INT fw, new_x;
4362 /* Sanity check */
4363 if(pos < 0 || pos > 100) return 0;
4364 /* Assume default scroll range 0-100 */
4365 fw = es->format_rect.right - es->format_rect.left;
4366 new_x = pos * (es->text_width - fw) / 100;
4367 dx = es->text_width ? (new_x - es->x_offset) : 0;
4369 break;
4370 case SB_THUMBPOSITION:
4371 TRACE("SB_THUMBPOSITION %d\n", pos);
4372 es->flags &= ~EF_HSCROLL_TRACK;
4373 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4374 dx = pos - es->x_offset;
4375 else
4377 INT fw, new_x;
4378 /* Sanity check */
4379 if(pos < 0 || pos > 100) return 0;
4380 /* Assume default scroll range 0-100 */
4381 fw = es->format_rect.right - es->format_rect.left;
4382 new_x = pos * (es->text_width - fw) / 100;
4383 dx = es->text_width ? (new_x - es->x_offset) : 0;
4385 if (!dx) {
4386 /* force scroll info update */
4387 EDIT_UpdateScrollInfo(es);
4388 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4390 break;
4391 case SB_ENDSCROLL:
4392 TRACE("SB_ENDSCROLL\n");
4393 break;
4395 * FIXME : the next two are undocumented !
4396 * Are we doing the right thing ?
4397 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4398 * although it's also a regular control message.
4400 case EM_GETTHUMB: /* this one is used by NT notepad */
4401 case EM_GETTHUMB16:
4403 LRESULT ret;
4404 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4405 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4406 else
4408 /* Assume default scroll range 0-100 */
4409 INT fw = es->format_rect.right - es->format_rect.left;
4410 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4412 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4413 return ret;
4415 case EM_LINESCROLL16:
4416 TRACE("EM_LINESCROLL16\n");
4417 dx = pos;
4418 break;
4420 default:
4421 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4422 action, action);
4423 return 0;
4425 if (dx)
4427 INT fw = es->format_rect.right - es->format_rect.left;
4428 /* check if we are going to move too far */
4429 if(es->x_offset + dx + fw > es->text_width)
4430 dx = es->text_width - fw - es->x_offset;
4431 if(dx)
4432 EDIT_EM_LineScroll_internal(es, dx, 0);
4434 return 0;
4438 /*********************************************************************
4440 * EDIT_CheckCombo
4443 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4445 HWND hLBox = es->hwndListBox;
4446 HWND hCombo;
4447 BOOL bDropped;
4448 int nEUI;
4450 if (!hLBox)
4451 return FALSE;
4453 hCombo = GetParent(es->hwndSelf);
4454 bDropped = TRUE;
4455 nEUI = 0;
4457 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4459 if (key == VK_UP || key == VK_DOWN)
4461 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4462 nEUI = 1;
4464 if (msg == WM_KEYDOWN || nEUI)
4465 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4468 switch (msg)
4470 case WM_KEYDOWN:
4471 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4473 /* make sure ComboLBox pops up */
4474 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4475 key = VK_F4;
4476 nEUI = 2;
4479 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4480 break;
4482 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4483 if (nEUI)
4484 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4485 else
4486 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4487 break;
4490 if(nEUI == 2)
4491 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4493 return TRUE;
4497 /*********************************************************************
4499 * WM_KEYDOWN
4501 * Handling of special keys that don't produce a WM_CHAR
4502 * (i.e. non-printable keys) & Backspace & Delete
4505 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4507 BOOL shift;
4508 BOOL control;
4510 if (GetKeyState(VK_MENU) & 0x8000)
4511 return 0;
4513 shift = GetKeyState(VK_SHIFT) & 0x8000;
4514 control = GetKeyState(VK_CONTROL) & 0x8000;
4516 switch (key) {
4517 case VK_F4:
4518 case VK_UP:
4519 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4520 break;
4522 /* fall through */
4523 case VK_LEFT:
4524 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4525 EDIT_MoveUp_ML(es, shift);
4526 else
4527 if (control)
4528 EDIT_MoveWordBackward(es, shift);
4529 else
4530 EDIT_MoveBackward(es, shift);
4531 break;
4532 case VK_DOWN:
4533 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4534 break;
4535 /* fall through */
4536 case VK_RIGHT:
4537 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4538 EDIT_MoveDown_ML(es, shift);
4539 else if (control)
4540 EDIT_MoveWordForward(es, shift);
4541 else
4542 EDIT_MoveForward(es, shift);
4543 break;
4544 case VK_HOME:
4545 EDIT_MoveHome(es, shift);
4546 break;
4547 case VK_END:
4548 EDIT_MoveEnd(es, shift);
4549 break;
4550 case VK_PRIOR:
4551 if (es->style & ES_MULTILINE)
4552 EDIT_MovePageUp_ML(es, shift);
4553 else
4554 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4555 break;
4556 case VK_NEXT:
4557 if (es->style & ES_MULTILINE)
4558 EDIT_MovePageDown_ML(es, shift);
4559 else
4560 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4561 break;
4562 case VK_DELETE:
4563 if (!(es->style & ES_READONLY) && !(shift && control)) {
4564 if (es->selection_start != es->selection_end) {
4565 if (shift)
4566 EDIT_WM_Cut(es);
4567 else
4568 EDIT_WM_Clear(es);
4569 } else {
4570 if (shift) {
4571 /* delete character left of caret */
4572 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4573 EDIT_MoveBackward(es, TRUE);
4574 EDIT_WM_Clear(es);
4575 } else if (control) {
4576 /* delete to end of line */
4577 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4578 EDIT_MoveEnd(es, TRUE);
4579 EDIT_WM_Clear(es);
4580 } else {
4581 /* delete character right of caret */
4582 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4583 EDIT_MoveForward(es, TRUE);
4584 EDIT_WM_Clear(es);
4588 break;
4589 case VK_INSERT:
4590 if (shift) {
4591 if (!(es->style & ES_READONLY))
4592 EDIT_WM_Paste(es);
4593 } else if (control)
4594 EDIT_WM_Copy(es);
4595 break;
4596 case VK_RETURN:
4597 /* If the edit doesn't want the return send a message to the default object */
4598 if(!(es->style & ES_WANTRETURN))
4600 HWND hwndParent = GetParent(es->hwndSelf);
4601 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4602 if (HIWORD(dw) == DC_HASDEFID)
4604 SendMessageW( hwndParent, WM_COMMAND,
4605 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4606 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4608 else
4609 SendMessageW( hwndParent, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndParent, IDOK ) );
4611 break;
4613 return 0;
4617 /*********************************************************************
4619 * WM_KILLFOCUS
4622 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4624 es->flags &= ~EF_FOCUSED;
4625 DestroyCaret();
4626 if(!(es->style & ES_NOHIDESEL))
4627 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4628 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4629 return 0;
4633 /*********************************************************************
4635 * WM_LBUTTONDBLCLK
4637 * The caret position has been set on the WM_LBUTTONDOWN message
4640 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4642 INT s;
4643 INT e = es->selection_end;
4644 INT l;
4645 INT li;
4646 INT ll;
4648 es->bCaptureState = TRUE;
4649 SetCapture(es->hwndSelf);
4651 l = EDIT_EM_LineFromChar(es, e);
4652 li = EDIT_EM_LineIndex(es, l);
4653 ll = EDIT_EM_LineLength(es, e);
4654 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4655 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4656 EDIT_EM_SetSel(es, s, e, FALSE);
4657 EDIT_EM_ScrollCaret(es);
4658 es->region_posx = es->region_posy = 0;
4659 SetTimer(es->hwndSelf, 0, 100, NULL);
4660 return 0;
4664 /*********************************************************************
4666 * WM_LBUTTONDOWN
4669 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4671 INT e;
4672 BOOL after_wrap;
4674 es->bCaptureState = TRUE;
4675 SetCapture(es->hwndSelf);
4676 EDIT_ConfinePoint(es, &x, &y);
4677 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4678 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4679 EDIT_EM_ScrollCaret(es);
4680 es->region_posx = es->region_posy = 0;
4681 SetTimer(es->hwndSelf, 0, 100, NULL);
4683 if (!(es->flags & EF_FOCUSED))
4684 SetFocus(es->hwndSelf);
4686 return 0;
4690 /*********************************************************************
4692 * WM_LBUTTONUP
4695 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4697 if (es->bCaptureState) {
4698 KillTimer(es->hwndSelf, 0);
4699 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4701 es->bCaptureState = FALSE;
4702 return 0;
4706 /*********************************************************************
4708 * WM_MBUTTONDOWN
4711 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4713 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4714 return 0;
4718 /*********************************************************************
4720 * WM_MOUSEMOVE
4723 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4725 INT e;
4726 BOOL after_wrap;
4727 INT prex, prey;
4729 /* If the mouse has been captured by process other than the edit control itself,
4730 * the windows edit controls will not select the strings with mouse move.
4732 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4733 return 0;
4736 * FIXME: gotta do some scrolling if outside client
4737 * area. Maybe reset the timer ?
4739 prex = x; prey = y;
4740 EDIT_ConfinePoint(es, &x, &y);
4741 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4742 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4743 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4744 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4745 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4746 return 0;
4750 /*********************************************************************
4752 * WM_NCCREATE
4754 * See also EDIT_WM_StyleChanged
4756 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4758 EDITSTATE *es;
4759 UINT alloc_size;
4761 TRACE("Creating %s edit control, style = %08x\n",
4762 unicode ? "Unicode" : "ANSI", lpcs->style);
4764 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4765 return FALSE;
4766 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4769 * Note: since the EDITSTATE has not been fully initialized yet,
4770 * we can't use any API calls that may send
4771 * WM_XXX messages before WM_NCCREATE is completed.
4774 es->is_unicode = unicode;
4775 es->style = lpcs->style;
4777 es->bEnableState = !(es->style & WS_DISABLED);
4779 es->hwndSelf = hwnd;
4780 /* Save parent, which will be notified by EN_* messages */
4781 es->hwndParent = lpcs->hwndParent;
4783 if (es->style & ES_COMBO)
4784 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4786 /* Number overrides lowercase overrides uppercase (at least it
4787 * does in Win95). However I'll bet that ES_NUMBER would be
4788 * invalid under Win 3.1.
4790 if (es->style & ES_NUMBER) {
4791 ; /* do not override the ES_NUMBER */
4792 } else if (es->style & ES_LOWERCASE) {
4793 es->style &= ~ES_UPPERCASE;
4795 if (es->style & ES_MULTILINE) {
4796 es->buffer_limit = BUFLIMIT_INITIAL;
4797 if (es->style & WS_VSCROLL)
4798 es->style |= ES_AUTOVSCROLL;
4799 if (es->style & WS_HSCROLL)
4800 es->style |= ES_AUTOHSCROLL;
4801 es->style &= ~ES_PASSWORD;
4802 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4803 /* Confirmed - RIGHT overrides CENTER */
4804 if (es->style & ES_RIGHT)
4805 es->style &= ~ES_CENTER;
4806 es->style &= ~WS_HSCROLL;
4807 es->style &= ~ES_AUTOHSCROLL;
4809 } else {
4810 es->buffer_limit = BUFLIMIT_INITIAL;
4811 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4812 es->style &= ~ES_CENTER;
4813 es->style &= ~WS_HSCROLL;
4814 es->style &= ~WS_VSCROLL;
4815 if (es->style & ES_PASSWORD)
4816 es->password_char = '*';
4819 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4820 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4821 return FALSE;
4822 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4824 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4825 return FALSE;
4826 es->undo_buffer_size = es->buffer_size;
4828 if (es->style & ES_MULTILINE)
4829 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4830 return FALSE;
4831 es->line_count = 1;
4834 * In Win95 look and feel, the WS_BORDER style is replaced by the
4835 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4836 * control a nonclient area so we don't need to draw the border.
4837 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4838 * a nonclient area and we should handle painting the border ourselves.
4840 * When making modifications please ensure that the code still works
4841 * for edit controls created directly with style 0x50800000, exStyle 0
4842 * (which should have a single pixel border)
4844 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4845 es->style &= ~WS_BORDER;
4846 else if (es->style & WS_BORDER)
4847 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4849 return TRUE;
4852 /*********************************************************************
4854 * WM_PAINT
4857 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4859 PAINTSTRUCT ps;
4860 INT i;
4861 HDC dc;
4862 HFONT old_font = 0;
4863 RECT rc;
4864 RECT rcClient;
4865 RECT rcLine;
4866 RECT rcRgn;
4867 HBRUSH brush;
4868 HBRUSH old_brush;
4869 INT bw, bh;
4870 BOOL rev = es->bEnableState &&
4871 ((es->flags & EF_FOCUSED) ||
4872 (es->style & ES_NOHIDESEL));
4873 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4875 GetClientRect(es->hwndSelf, &rcClient);
4877 /* get the background brush */
4878 brush = EDIT_NotifyCtlColor(es, dc);
4880 /* paint the border and the background */
4881 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4883 if(es->style & WS_BORDER) {
4884 bw = GetSystemMetrics(SM_CXBORDER);
4885 bh = GetSystemMetrics(SM_CYBORDER);
4886 rc = rcClient;
4887 if(es->style & ES_MULTILINE) {
4888 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4889 if(es->style & WS_VSCROLL) rc.right+=bw;
4892 /* Draw the frame. Same code as in nonclient.c */
4893 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4894 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4895 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4896 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4897 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4898 SelectObject(dc, old_brush);
4900 /* Keep the border clean */
4901 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4902 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4905 GetClipBox(dc, &rc);
4906 FillRect(dc, &rc, brush);
4908 IntersectClipRect(dc, es->format_rect.left,
4909 es->format_rect.top,
4910 es->format_rect.right,
4911 es->format_rect.bottom);
4912 if (es->style & ES_MULTILINE) {
4913 rc = rcClient;
4914 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4916 if (es->font)
4917 old_font = SelectObject(dc, es->font);
4919 if (!es->bEnableState)
4920 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4921 GetClipBox(dc, &rcRgn);
4922 if (es->style & ES_MULTILINE) {
4923 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4924 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4925 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4926 if (IntersectRect(&rc, &rcRgn, &rcLine))
4927 EDIT_PaintLine(es, dc, i, rev);
4929 } else {
4930 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4931 if (IntersectRect(&rc, &rcRgn, &rcLine))
4932 EDIT_PaintLine(es, dc, 0, rev);
4934 if (es->font)
4935 SelectObject(dc, old_font);
4937 if (!hdc)
4938 EndPaint(es->hwndSelf, &ps);
4942 /*********************************************************************
4944 * WM_PASTE
4947 static void EDIT_WM_Paste(EDITSTATE *es)
4949 HGLOBAL hsrc;
4950 LPWSTR src;
4952 /* Protect read-only edit control from modification */
4953 if(es->style & ES_READONLY)
4954 return;
4956 OpenClipboard(es->hwndSelf);
4957 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4958 src = (LPWSTR)GlobalLock(hsrc);
4959 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4960 GlobalUnlock(hsrc);
4962 else if (es->style & ES_PASSWORD) {
4963 /* clear selected text in password edit box even with empty clipboard */
4964 const WCHAR empty_strW[] = { 0 };
4965 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
4967 CloseClipboard();
4971 /*********************************************************************
4973 * WM_SETFOCUS
4976 static void EDIT_WM_SetFocus(EDITSTATE *es)
4978 es->flags |= EF_FOCUSED;
4980 if (!(es->style & ES_NOHIDESEL))
4981 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4983 /* single line edit updates itself */
4984 if (!(es->style & ES_MULTILINE))
4986 HDC hdc = GetDC(es->hwndSelf);
4987 EDIT_WM_Paint(es, hdc);
4988 ReleaseDC(es->hwndSelf, hdc);
4991 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4992 EDIT_SetCaretPos(es, es->selection_end,
4993 es->flags & EF_AFTER_WRAP);
4994 ShowCaret(es->hwndSelf);
4995 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
4999 /*********************************************************************
5001 * WM_SETFONT
5003 * With Win95 look the margins are set to default font value unless
5004 * the system font (font == 0) is being set, in which case they are left
5005 * unchanged.
5008 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
5010 TEXTMETRICW tm;
5011 HDC dc;
5012 HFONT old_font = 0;
5013 RECT clientRect;
5015 es->font = font;
5016 dc = GetDC(es->hwndSelf);
5017 if (font)
5018 old_font = SelectObject(dc, font);
5019 GetTextMetricsW(dc, &tm);
5020 es->line_height = tm.tmHeight;
5021 es->char_width = tm.tmAveCharWidth;
5022 if (font)
5023 SelectObject(dc, old_font);
5024 ReleaseDC(es->hwndSelf, dc);
5026 /* Reset the format rect and the margins */
5027 GetClientRect(es->hwndSelf, &clientRect);
5028 EDIT_SetRectNP(es, &clientRect);
5029 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5030 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5032 if (es->style & ES_MULTILINE)
5033 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5034 else
5035 EDIT_CalcLineWidth_SL(es);
5037 if (redraw)
5038 EDIT_UpdateText(es, NULL, TRUE);
5039 if (es->flags & EF_FOCUSED) {
5040 DestroyCaret();
5041 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5042 EDIT_SetCaretPos(es, es->selection_end,
5043 es->flags & EF_AFTER_WRAP);
5044 ShowCaret(es->hwndSelf);
5049 /*********************************************************************
5051 * WM_SETTEXT
5053 * NOTES
5054 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5055 * The modified flag is reset. No notifications are sent.
5057 * For single-line controls, reception of WM_SETTEXT triggers:
5058 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5061 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5063 LPWSTR textW = NULL;
5064 if (!unicode && text)
5066 LPCSTR textA = (LPCSTR)text;
5067 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5068 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5069 if (textW)
5070 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5071 text = textW;
5074 if (es->flags & EF_UPDATE)
5075 /* fixed this bug once; complain if we see it about to happen again. */
5076 ERR("SetSel may generate UPDATE message whose handler may reset "
5077 "selection.\n");
5079 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5080 if (text)
5082 TRACE("%s\n", debugstr_w(text));
5083 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5084 if(!unicode)
5085 HeapFree(GetProcessHeap(), 0, textW);
5087 else
5089 static const WCHAR empty_stringW[] = {0};
5090 TRACE("<NULL>\n");
5091 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5093 es->x_offset = 0;
5094 es->flags &= ~EF_MODIFIED;
5095 EDIT_EM_SetSel(es, 0, 0, FALSE);
5097 /* Send the notification after the selection start and end have been set
5098 * edit control doesn't send notification on WM_SETTEXT
5099 * if it is multiline, or it is part of combobox
5101 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5103 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5104 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5106 EDIT_EM_ScrollCaret(es);
5107 EDIT_UpdateScrollInfo(es);
5111 /*********************************************************************
5113 * WM_SIZE
5116 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5118 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5119 RECT rc;
5120 TRACE("width = %d, height = %d\n", width, height);
5121 SetRect(&rc, 0, 0, width, height);
5122 EDIT_SetRectNP(es, &rc);
5123 EDIT_UpdateText(es, NULL, TRUE);
5128 /*********************************************************************
5130 * WM_STYLECHANGED
5132 * This message is sent by SetWindowLong on having changed either the Style
5133 * or the extended style.
5135 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5137 * See also EDIT_WM_NCCreate
5139 * It appears that the Windows version of the edit control allows the style
5140 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5141 * style variable which will generally be different. In this function we
5142 * update the internal style based on what changed in the externally visible
5143 * style.
5145 * Much of this content as based upon the MSDN, especially:
5146 * Platform SDK Documentation -> User Interface Services ->
5147 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5148 * Edit Control Styles
5150 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5152 if (GWL_STYLE == which) {
5153 DWORD style_change_mask;
5154 DWORD new_style;
5155 /* Only a subset of changes can be applied after the control
5156 * has been created.
5158 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5159 ES_NUMBER;
5160 if (es->style & ES_MULTILINE)
5161 style_change_mask |= ES_WANTRETURN;
5163 new_style = style->styleNew & style_change_mask;
5165 /* Number overrides lowercase overrides uppercase (at least it
5166 * does in Win95). However I'll bet that ES_NUMBER would be
5167 * invalid under Win 3.1.
5169 if (new_style & ES_NUMBER) {
5170 ; /* do not override the ES_NUMBER */
5171 } else if (new_style & ES_LOWERCASE) {
5172 new_style &= ~ES_UPPERCASE;
5175 es->style = (es->style & ~style_change_mask) | new_style;
5176 } else if (GWL_EXSTYLE == which) {
5177 ; /* FIXME - what is needed here */
5178 } else {
5179 WARN ("Invalid style change %ld\n",which);
5182 return 0;
5185 /*********************************************************************
5187 * WM_SYSKEYDOWN
5190 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5192 if ((key == VK_BACK) && (key_data & 0x2000)) {
5193 if (EDIT_EM_CanUndo(es))
5194 EDIT_EM_Undo(es);
5195 return 0;
5196 } else if (key == VK_UP || key == VK_DOWN) {
5197 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5198 return 0;
5200 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5204 /*********************************************************************
5206 * WM_TIMER
5209 static void EDIT_WM_Timer(EDITSTATE *es)
5211 if (es->region_posx < 0) {
5212 EDIT_MoveBackward(es, TRUE);
5213 } else if (es->region_posx > 0) {
5214 EDIT_MoveForward(es, TRUE);
5217 * FIXME: gotta do some vertical scrolling here, like
5218 * EDIT_EM_LineScroll(hwnd, 0, 1);
5222 /*********************************************************************
5224 * WM_VSCROLL
5227 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5229 INT dy;
5231 if (!(es->style & ES_MULTILINE))
5232 return 0;
5234 if (!(es->style & ES_AUTOVSCROLL))
5235 return 0;
5237 dy = 0;
5238 switch (action) {
5239 case SB_LINEUP:
5240 case SB_LINEDOWN:
5241 case SB_PAGEUP:
5242 case SB_PAGEDOWN:
5243 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5244 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5245 (action == SB_PAGEUP ? "SB_PAGEUP" :
5246 "SB_PAGEDOWN"))));
5247 EDIT_EM_Scroll(es, action);
5248 return 0;
5249 case SB_TOP:
5250 TRACE("SB_TOP\n");
5251 dy = -es->y_offset;
5252 break;
5253 case SB_BOTTOM:
5254 TRACE("SB_BOTTOM\n");
5255 dy = es->line_count - 1 - es->y_offset;
5256 break;
5257 case SB_THUMBTRACK:
5258 TRACE("SB_THUMBTRACK %d\n", pos);
5259 es->flags |= EF_VSCROLL_TRACK;
5260 if(es->style & WS_VSCROLL)
5261 dy = pos - es->y_offset;
5262 else
5264 /* Assume default scroll range 0-100 */
5265 INT vlc, new_y;
5266 /* Sanity check */
5267 if(pos < 0 || pos > 100) return 0;
5268 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5269 new_y = pos * (es->line_count - vlc) / 100;
5270 dy = es->line_count ? (new_y - es->y_offset) : 0;
5271 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5272 es->line_count, es->y_offset, pos, dy);
5274 break;
5275 case SB_THUMBPOSITION:
5276 TRACE("SB_THUMBPOSITION %d\n", pos);
5277 es->flags &= ~EF_VSCROLL_TRACK;
5278 if(es->style & WS_VSCROLL)
5279 dy = pos - es->y_offset;
5280 else
5282 /* Assume default scroll range 0-100 */
5283 INT vlc, new_y;
5284 /* Sanity check */
5285 if(pos < 0 || pos > 100) return 0;
5286 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5287 new_y = pos * (es->line_count - vlc) / 100;
5288 dy = es->line_count ? (new_y - es->y_offset) : 0;
5289 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5290 es->line_count, es->y_offset, pos, dy);
5292 if (!dy)
5294 /* force scroll info update */
5295 EDIT_UpdateScrollInfo(es);
5296 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5298 break;
5299 case SB_ENDSCROLL:
5300 TRACE("SB_ENDSCROLL\n");
5301 break;
5303 * FIXME : the next two are undocumented !
5304 * Are we doing the right thing ?
5305 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5306 * although it's also a regular control message.
5308 case EM_GETTHUMB: /* this one is used by NT notepad */
5309 case EM_GETTHUMB16:
5311 LRESULT ret;
5312 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5313 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5314 else
5316 /* Assume default scroll range 0-100 */
5317 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5318 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5320 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5321 return ret;
5323 case EM_LINESCROLL16:
5324 TRACE("EM_LINESCROLL16 %d\n", pos);
5325 dy = pos;
5326 break;
5328 default:
5329 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5330 action, action);
5331 return 0;
5333 if (dy)
5334 EDIT_EM_LineScroll(es, 0, dy);
5335 return 0;
5338 /*********************************************************************
5340 * EDIT_UpdateText
5343 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5345 if (es->flags & EF_UPDATE) {
5346 es->flags &= ~EF_UPDATE;
5347 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5349 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5353 /*********************************************************************
5355 * EDIT_UpdateText
5358 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
5360 if (es->flags & EF_UPDATE) {
5361 es->flags &= ~EF_UPDATE;
5362 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5364 InvalidateRect(es->hwndSelf, rc, bErase);
5367 /********************************************************************
5369 * The Following code is to handle inline editing from IMEs
5372 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5374 LONG buflen;
5375 LPWSTR lpCompStr = NULL;
5376 HIMC hIMC;
5377 LPSTR lpCompStrAttr = NULL;
5378 DWORD dwBufLenAttr;
5380 if (!(hIMC = ImmGetContext(hwnd)))
5381 return;
5383 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5385 if (buflen < 0)
5387 ImmReleaseContext(hwnd, hIMC);
5388 return;
5391 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
5392 if (!lpCompStr)
5394 ERR("Unable to allocate IME CompositionString\n");
5395 ImmReleaseContext(hwnd,hIMC);
5396 return;
5399 if (buflen)
5400 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
5401 lpCompStr[buflen/sizeof(WCHAR)] = 0;
5403 if (CompFlag & GCS_COMPATTR)
5406 * We do not use the attributes yet. it would tell us what characters
5407 * are in transition and which are converted or decided upon
5409 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5410 if (dwBufLenAttr)
5412 dwBufLenAttr ++;
5413 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5414 if (!lpCompStrAttr)
5416 ERR("Unable to allocate IME Attribute String\n");
5417 HeapFree(GetProcessHeap(),0,lpCompStr);
5418 ImmReleaseContext(hwnd,hIMC);
5419 return;
5421 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5422 dwBufLenAttr);
5423 lpCompStrAttr[dwBufLenAttr] = 0;
5425 else
5426 lpCompStrAttr = NULL;
5429 /* check for change in composition start */
5430 if (es->selection_end < es->composition_start)
5431 es->composition_start = es->selection_end;
5433 /* replace existing selection string */
5434 es->selection_start = es->composition_start;
5436 if (es->composition_len > 0)
5437 es->selection_end = es->composition_start + es->composition_len;
5438 else
5439 es->selection_end = es->selection_start;
5441 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5442 es->composition_len = abs(es->composition_start - es->selection_end);
5444 es->selection_start = es->composition_start;
5445 es->selection_end = es->selection_start + es->composition_len;
5447 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5448 HeapFree(GetProcessHeap(),0,lpCompStr);
5449 ImmReleaseContext(hwnd,hIMC);
5452 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5454 LONG buflen;
5455 LPWSTR lpResultStr;
5456 HIMC hIMC;
5458 if ( !(hIMC = ImmGetContext(hwnd)))
5459 return;
5461 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5462 if (buflen <= 0)
5464 ImmReleaseContext(hwnd, hIMC);
5465 return;
5468 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
5469 if (!lpResultStr)
5471 ERR("Unable to alloc buffer for IME string\n");
5472 ImmReleaseContext(hwnd, hIMC);
5473 return;
5476 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
5477 lpResultStr[buflen/sizeof(WCHAR)] = 0;
5479 /* check for change in composition start */
5480 if (es->selection_end < es->composition_start)
5481 es->composition_start = es->selection_end;
5483 es->selection_start = es->composition_start;
5484 es->selection_end = es->composition_start + es->composition_len;
5485 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5486 es->composition_start = es->selection_end;
5487 es->composition_len = 0;
5489 HeapFree(GetProcessHeap(),0,lpResultStr);
5490 ImmReleaseContext(hwnd, hIMC);
5493 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5495 if (CompFlag & GCS_RESULTSTR)
5496 EDIT_GetResultStr(hwnd,es);
5497 if (CompFlag & GCS_COMPSTR)
5498 EDIT_GetCompositionStr(hwnd, CompFlag, es);