user32: Handle WM_CHAR messages better in edit controls.
[wine/wine-kai.git] / dlls / user32 / edit.c
blobdd5ba0a867900697ace3903af2f1dc51a9250874
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_RETURN:
815 SendMessageW(GetParent(hwnd), WM_COMMAND, IDOK, (LPARAM)GetDlgItem(GetParent(hwnd), IDOK));
816 break;
817 case VK_ESCAPE:
818 SendMessageW(GetParent(hwnd), WM_CLOSE, 0, 0);
819 break;
820 case VK_TAB:
821 SendMessageW(GetParent(hwnd), WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0);
822 break;
823 default:
824 break;
828 break;
830 case WM_IME_CHAR:
831 if (!unicode)
833 WCHAR charW;
834 CHAR strng[2];
836 strng[0] = wParam >> 8;
837 strng[1] = wParam & 0xff;
838 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
839 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
840 EDIT_WM_Char(es, charW);
841 break;
843 /* fall through */
844 case WM_CHAR:
846 WCHAR charW;
848 if(unicode)
849 charW = wParam;
850 else
852 CHAR charA = wParam;
853 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
856 if (es->hwndListBox)
858 if (charW == VK_RETURN || charW == VK_ESCAPE)
860 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
862 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
863 break;
867 else
869 if (charW == VK_TAB || charW == VK_RETURN)
870 break;
872 EDIT_WM_Char(es, charW);
873 break;
876 case WM_CLEAR:
877 EDIT_WM_Clear(es);
878 break;
880 case WM_COMMAND:
881 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
882 break;
884 case WM_CONTEXTMENU:
885 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
886 break;
888 case WM_COPY:
889 EDIT_WM_Copy(es);
890 break;
892 case WM_CREATE:
893 if(unicode)
894 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
895 else
897 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
898 LPWSTR nameW = NULL;
899 if(nameA)
901 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
902 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
903 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
905 result = EDIT_WM_Create(es, nameW);
906 HeapFree(GetProcessHeap(), 0, nameW);
908 break;
910 case WM_CUT:
911 EDIT_WM_Cut(es);
912 break;
914 case WM_ENABLE:
915 es->bEnableState = (BOOL) wParam;
916 EDIT_UpdateText(es, NULL, TRUE);
917 break;
919 case WM_ERASEBKGND:
920 /* we do the proper erase in EDIT_WM_Paint */
921 result = 1;
922 break;
924 case WM_GETFONT:
925 result = (LRESULT)es->font;
926 break;
928 case WM_GETTEXT:
929 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
930 break;
932 case WM_GETTEXTLENGTH:
933 if (unicode) result = get_text_length(es);
934 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
935 NULL, 0, NULL, NULL );
936 break;
938 case WM_HSCROLL:
939 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
940 break;
942 case WM_KEYDOWN:
943 result = EDIT_WM_KeyDown(es, (INT)wParam);
944 break;
946 case WM_KILLFOCUS:
947 result = EDIT_WM_KillFocus(es);
948 break;
950 case WM_LBUTTONDBLCLK:
951 result = EDIT_WM_LButtonDblClk(es);
952 break;
954 case WM_LBUTTONDOWN:
955 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
956 break;
958 case WM_LBUTTONUP:
959 result = EDIT_WM_LButtonUp(es);
960 break;
962 case WM_MBUTTONDOWN:
963 result = EDIT_WM_MButtonDown(es);
964 break;
966 case WM_MOUSEMOVE:
967 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
968 break;
970 case WM_PRINTCLIENT:
971 case WM_PAINT:
972 EDIT_WM_Paint(es, (HDC)wParam);
973 break;
975 case WM_PASTE:
976 EDIT_WM_Paste(es);
977 break;
979 case WM_SETFOCUS:
980 EDIT_WM_SetFocus(es);
981 break;
983 case WM_SETFONT:
984 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
985 break;
987 case WM_SETREDRAW:
988 /* FIXME: actually set an internal flag and behave accordingly */
989 break;
991 case WM_SETTEXT:
992 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
993 result = TRUE;
994 break;
996 case WM_SIZE:
997 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
998 break;
1000 case WM_STYLECHANGED:
1001 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
1002 break;
1004 case WM_STYLECHANGING:
1005 result = 0; /* See EDIT_WM_StyleChanged */
1006 break;
1008 case WM_SYSKEYDOWN:
1009 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
1010 break;
1012 case WM_TIMER:
1013 EDIT_WM_Timer(es);
1014 break;
1016 case WM_VSCROLL:
1017 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
1018 break;
1020 case WM_MOUSEWHEEL:
1022 int gcWheelDelta = 0;
1023 UINT pulScrollLines = 3;
1024 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1026 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1027 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1028 break;
1030 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1031 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1033 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1034 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1035 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1038 break;
1041 /* IME messages to make the edit control IME aware */
1042 case WM_IME_SETCONTEXT:
1043 break;
1045 case WM_IME_STARTCOMPOSITION:
1047 * FIXME in IME: This message is not always sent like it should be
1049 if (es->selection_start != es->selection_end)
1051 static const WCHAR empty_stringW[] = {0};
1052 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1054 es->composition_start = es->selection_end;
1055 es->composition_len = 0;
1056 break;
1058 case WM_IME_COMPOSITION:
1060 int caret_pos = es->selection_end;
1061 if (es->composition_len == 0)
1063 if (es->selection_start != es->selection_end)
1065 static const WCHAR empty_stringW[] = {0};
1066 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1069 es->composition_start = es->selection_end;
1071 EDIT_ImeComposition(hwnd,lParam,es);
1072 EDIT_SetCaretPos(es, caret_pos, es->flags & EF_AFTER_WRAP);
1073 break;
1076 case WM_IME_ENDCOMPOSITION:
1077 es->composition_len= 0;
1078 break;
1080 case WM_IME_COMPOSITIONFULL:
1081 break;
1083 case WM_IME_SELECT:
1084 break;
1086 case WM_IME_CONTROL:
1087 break;
1089 default:
1090 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1091 break;
1094 if (es) EDIT_UnlockBuffer(es, FALSE);
1096 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1098 return result;
1101 /*********************************************************************
1103 * EditWndProcW (USER32.@)
1105 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1107 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1110 /*********************************************************************
1112 * EditWndProc (USER32.@)
1114 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1116 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1119 /*********************************************************************
1121 * EDIT_BuildLineDefs_ML
1123 * Build linked list of text lines.
1124 * Lines can end with '\0' (last line), a character (if it is wrapped),
1125 * a soft return '\r\r\n' or a hard return '\r\n'
1128 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1130 HDC dc;
1131 HFONT old_font = 0;
1132 LPWSTR current_position, cp;
1133 INT fw;
1134 LINEDEF *current_line;
1135 LINEDEF *previous_line;
1136 LINEDEF *start_line;
1137 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1138 INT line_count = es->line_count;
1139 INT orig_net_length;
1140 RECT rc;
1142 if (istart == iend && delta == 0)
1143 return;
1145 dc = GetDC(es->hwndSelf);
1146 if (es->font)
1147 old_font = SelectObject(dc, es->font);
1149 previous_line = NULL;
1150 current_line = es->first_line_def;
1152 /* Find starting line. istart must lie inside an existing line or
1153 * at the end of buffer */
1154 do {
1155 if (istart < current_line->index + current_line->length ||
1156 current_line->ending == END_0)
1157 break;
1159 previous_line = current_line;
1160 current_line = current_line->next;
1161 line_index++;
1162 } while (current_line);
1164 if (!current_line) /* Error occurred start is not inside previous buffer */
1166 FIXME(" modification occurred outside buffer\n");
1167 ReleaseDC(es->hwndSelf, dc);
1168 return;
1171 /* Remember start of modifications in order to calculate update region */
1172 nstart_line = line_index;
1173 nstart_index = current_line->index;
1175 /* We must start to reformat from the previous line since the modifications
1176 * may have caused the line to wrap upwards. */
1177 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1179 line_index--;
1180 current_line = previous_line;
1182 start_line = current_line;
1184 fw = es->format_rect.right - es->format_rect.left;
1185 current_position = es->text + current_line->index;
1186 do {
1187 if (current_line != start_line)
1189 if (!current_line || current_line->index + delta > current_position - es->text)
1191 /* The buffer has been expanded, create a new line and
1192 insert it into the link list */
1193 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1194 new_line->next = previous_line->next;
1195 previous_line->next = new_line;
1196 current_line = new_line;
1197 es->line_count++;
1199 else if (current_line->index + delta < current_position - es->text)
1201 /* The previous line merged with this line so we delete this extra entry */
1202 previous_line->next = current_line->next;
1203 HeapFree(GetProcessHeap(), 0, current_line);
1204 current_line = previous_line->next;
1205 es->line_count--;
1206 continue;
1208 else /* current_line->index + delta == current_position */
1210 if (current_position - es->text > iend)
1211 break; /* We reached end of line modifications */
1212 /* else recalulate this line */
1216 current_line->index = current_position - es->text;
1217 orig_net_length = current_line->net_length;
1219 /* Find end of line */
1220 cp = current_position;
1221 while (*cp) {
1222 if (*cp == '\n') break;
1223 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1224 break;
1225 cp++;
1228 /* Mark type of line termination */
1229 if (!(*cp)) {
1230 current_line->ending = END_0;
1231 current_line->net_length = strlenW(current_position);
1232 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1233 current_line->ending = END_SOFT;
1234 current_line->net_length = cp - current_position - 1;
1235 } else if (*cp == '\n') {
1236 current_line->ending = END_RICH;
1237 current_line->net_length = cp - current_position;
1238 } else {
1239 current_line->ending = END_HARD;
1240 current_line->net_length = cp - current_position;
1243 /* Calculate line width */
1244 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1245 current_position, current_line->net_length,
1246 es->tabs_count, es->tabs));
1248 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1249 if (!(es->style & ES_AUTOHSCROLL)) {
1250 if (current_line->width > fw) {
1251 INT next = 0;
1252 INT prev;
1253 do {
1254 prev = next;
1255 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1256 prev + 1, current_line->net_length, WB_RIGHT);
1257 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1258 current_position, next, es->tabs_count, es->tabs));
1259 } while (current_line->width <= fw);
1260 if (!prev) { /* Didn't find a line break so force a break */
1261 next = 0;
1262 do {
1263 prev = next;
1264 next++;
1265 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1266 current_position, next, es->tabs_count, es->tabs));
1267 } while (current_line->width <= fw);
1268 if (!prev)
1269 prev = 1;
1272 /* If the first line we are calculating, wrapped before istart, we must
1273 * adjust istart in order for this to be reflected in the update region. */
1274 if (current_line->index == nstart_index && istart > current_line->index + prev)
1275 istart = current_line->index + prev;
1276 /* else if we are updating the previous line before the first line we
1277 * are re-calculating and it expanded */
1278 else if (current_line == start_line &&
1279 current_line->index != nstart_index && orig_net_length < prev)
1281 /* Line expanded due to an upwards line wrap so we must partially include
1282 * previous line in update region */
1283 nstart_line = line_index;
1284 nstart_index = current_line->index;
1285 istart = current_line->index + orig_net_length;
1288 current_line->net_length = prev;
1289 current_line->ending = END_WRAP;
1290 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1291 current_line->net_length, es->tabs_count, es->tabs));
1293 else if (orig_net_length < current_line->net_length &&
1294 current_line == start_line &&
1295 current_line->index != nstart_index) {
1296 /* The previous line expanded but it's still not as wide as the client rect */
1297 /* The expansion is due to an upwards line wrap so we must partially include
1298 it in the update region */
1299 nstart_line = line_index;
1300 nstart_index = current_line->index;
1301 istart = current_line->index + orig_net_length;
1306 /* Adjust length to include line termination */
1307 switch (current_line->ending) {
1308 case END_SOFT:
1309 current_line->length = current_line->net_length + 3;
1310 break;
1311 case END_RICH:
1312 current_line->length = current_line->net_length + 1;
1313 break;
1314 case END_HARD:
1315 current_line->length = current_line->net_length + 2;
1316 break;
1317 case END_WRAP:
1318 case END_0:
1319 current_line->length = current_line->net_length;
1320 break;
1322 es->text_width = max(es->text_width, current_line->width);
1323 current_position += current_line->length;
1324 previous_line = current_line;
1325 current_line = current_line->next;
1326 line_index++;
1327 } while (previous_line->ending != END_0);
1329 /* Finish adjusting line indexes by delta or remove hanging lines */
1330 if (previous_line->ending == END_0)
1332 LINEDEF *pnext = NULL;
1334 previous_line->next = NULL;
1335 while (current_line)
1337 pnext = current_line->next;
1338 HeapFree(GetProcessHeap(), 0, current_line);
1339 current_line = pnext;
1340 es->line_count--;
1343 else if (delta != 0)
1345 while (current_line)
1347 current_line->index += delta;
1348 current_line = current_line->next;
1352 /* Calculate rest of modification rectangle */
1353 if (hrgn)
1355 HRGN tmphrgn;
1357 * We calculate two rectangles. One for the first line which may have
1358 * an indent with respect to the format rect. The other is a format-width
1359 * rectangle that spans the rest of the lines that changed or moved.
1361 rc.top = es->format_rect.top + nstart_line * es->line_height -
1362 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1363 rc.bottom = rc.top + es->line_height;
1364 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1365 rc.left = es->format_rect.left;
1366 else
1367 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1368 es->text + nstart_index, istart - nstart_index,
1369 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1370 rc.right = es->format_rect.right;
1371 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1373 rc.top = rc.bottom;
1374 rc.left = es->format_rect.left;
1375 rc.right = es->format_rect.right;
1377 * If lines were added or removed we must re-paint the remainder of the
1378 * lines since the remaining lines were either shifted up or down.
1380 if (line_count < es->line_count) /* We added lines */
1381 rc.bottom = es->line_count * es->line_height;
1382 else if (line_count > es->line_count) /* We removed lines */
1383 rc.bottom = line_count * es->line_height;
1384 else
1385 rc.bottom = line_index * es->line_height;
1386 rc.bottom += es->format_rect.top;
1387 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1388 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1389 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1390 DeleteObject(tmphrgn);
1393 if (es->font)
1394 SelectObject(dc, old_font);
1396 ReleaseDC(es->hwndSelf, dc);
1399 /*********************************************************************
1401 * EDIT_CalcLineWidth_SL
1404 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1406 SIZE size;
1407 LPWSTR text;
1408 HDC dc;
1409 HFONT old_font = 0;
1411 text = EDIT_GetPasswordPointer_SL(es);
1413 dc = GetDC(es->hwndSelf);
1414 if (es->font)
1415 old_font = SelectObject(dc, es->font);
1417 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1419 if (es->font)
1420 SelectObject(dc, old_font);
1421 ReleaseDC(es->hwndSelf, dc);
1423 if (es->style & ES_PASSWORD)
1424 HeapFree(GetProcessHeap(), 0, text);
1426 es->text_width = size.cx;
1429 /*********************************************************************
1431 * EDIT_CallWordBreakProc
1433 * Call appropriate WordBreakProc (internal or external).
1435 * Note: The "start" argument should always be an index referring
1436 * to es->text. The actual wordbreak proc might be
1437 * 16 bit, so we can't always pass any 32 bit LPSTR.
1438 * Hence we assume that es->text is the buffer that holds
1439 * the string under examination (we can decide this for ourselves).
1442 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1444 INT ret;
1446 if (es->word_break_proc16) {
1447 HGLOBAL16 hglob16;
1448 SEGPTR segptr;
1449 INT countA;
1450 WORD args[5];
1451 DWORD result;
1453 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1454 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1455 segptr = WOWGlobalLock16(hglob16);
1456 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1457 args[4] = SELECTOROF(segptr);
1458 args[3] = OFFSETOF(segptr);
1459 args[2] = index;
1460 args[1] = countA;
1461 args[0] = action;
1462 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1463 ret = LOWORD(result);
1464 GlobalUnlock16(hglob16);
1465 GlobalFree16(hglob16);
1467 else if (es->word_break_proc)
1469 if(es->is_unicode)
1471 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1473 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1474 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1475 ret = wbpW(es->text + start, index, count, action);
1477 else
1479 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1480 INT countA;
1481 CHAR *textA;
1483 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1484 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1485 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1486 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1487 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1488 ret = wbpA(textA, index, countA, action);
1489 HeapFree(GetProcessHeap(), 0, textA);
1492 else
1493 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1495 return ret;
1499 /*********************************************************************
1501 * EDIT_CharFromPos
1503 * Beware: This is not the function called on EM_CHARFROMPOS
1504 * The position _can_ be outside the formatting / client
1505 * rectangle
1506 * The return value is only the character index
1509 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1511 INT index;
1512 HDC dc;
1513 HFONT old_font = 0;
1514 INT x_high = 0, x_low = 0;
1516 if (es->style & ES_MULTILINE) {
1517 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1518 INT line_index = 0;
1519 LINEDEF *line_def = es->first_line_def;
1520 INT low, high;
1521 while ((line > 0) && line_def->next) {
1522 line_index += line_def->length;
1523 line_def = line_def->next;
1524 line--;
1526 x += es->x_offset - es->format_rect.left;
1527 if (es->style & ES_RIGHT)
1528 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1529 else if (es->style & ES_CENTER)
1530 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1531 if (x >= line_def->width) {
1532 if (after_wrap)
1533 *after_wrap = (line_def->ending == END_WRAP);
1534 return line_index + line_def->net_length;
1536 if (x <= 0) {
1537 if (after_wrap)
1538 *after_wrap = FALSE;
1539 return line_index;
1541 dc = GetDC(es->hwndSelf);
1542 if (es->font)
1543 old_font = SelectObject(dc, es->font);
1544 low = line_index;
1545 high = line_index + line_def->net_length + 1;
1546 while (low < high - 1)
1548 INT mid = (low + high) / 2;
1549 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1550 if (x_now > x) {
1551 high = mid;
1552 x_high = x_now;
1553 } else {
1554 low = mid;
1555 x_low = x_now;
1558 if (abs(x_high - x) + 1 <= abs(x_low - x))
1559 index = high;
1560 else
1561 index = low;
1563 if (after_wrap)
1564 *after_wrap = ((index == line_index + line_def->net_length) &&
1565 (line_def->ending == END_WRAP));
1566 } else {
1567 LPWSTR text;
1568 SIZE size;
1569 if (after_wrap)
1570 *after_wrap = FALSE;
1571 x -= es->format_rect.left;
1572 if (!x)
1573 return es->x_offset;
1575 if (!es->x_offset)
1577 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1578 if (es->style & ES_RIGHT)
1579 x -= indent;
1580 else if (es->style & ES_CENTER)
1581 x -= indent / 2;
1584 text = EDIT_GetPasswordPointer_SL(es);
1585 dc = GetDC(es->hwndSelf);
1586 if (es->font)
1587 old_font = SelectObject(dc, es->font);
1588 if (x < 0)
1590 INT low = 0;
1591 INT high = es->x_offset;
1592 while (low < high - 1)
1594 INT mid = (low + high) / 2;
1595 GetTextExtentPoint32W( dc, text + mid,
1596 es->x_offset - mid, &size );
1597 if (size.cx > -x) {
1598 low = mid;
1599 x_low = size.cx;
1600 } else {
1601 high = mid;
1602 x_high = size.cx;
1605 if (abs(x_high + x) <= abs(x_low + x) + 1)
1606 index = high;
1607 else
1608 index = low;
1610 else
1612 INT low = es->x_offset;
1613 INT high = get_text_length(es) + 1;
1614 while (low < high - 1)
1616 INT mid = (low + high) / 2;
1617 GetTextExtentPoint32W( dc, text + es->x_offset,
1618 mid - es->x_offset, &size );
1619 if (size.cx > x) {
1620 high = mid;
1621 x_high = size.cx;
1622 } else {
1623 low = mid;
1624 x_low = size.cx;
1627 if (abs(x_high - x) <= abs(x_low - x) + 1)
1628 index = high;
1629 else
1630 index = low;
1632 if (es->style & ES_PASSWORD)
1633 HeapFree(GetProcessHeap(), 0, text);
1635 if (es->font)
1636 SelectObject(dc, old_font);
1637 ReleaseDC(es->hwndSelf, dc);
1638 return index;
1642 /*********************************************************************
1644 * EDIT_ConfinePoint
1646 * adjusts the point to be within the formatting rectangle
1647 * (so CharFromPos returns the nearest _visible_ character)
1650 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
1652 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1653 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1657 /*********************************************************************
1659 * EDIT_GetLineRect
1661 * Calculates the bounding rectangle for a line from a starting
1662 * column to an ending column.
1665 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1667 INT line_index = EDIT_EM_LineIndex(es, line);
1669 if (es->style & ES_MULTILINE)
1670 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1671 else
1672 rc->top = es->format_rect.top;
1673 rc->bottom = rc->top + es->line_height;
1674 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1675 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1679 /*********************************************************************
1681 * EDIT_GetPasswordPointer_SL
1683 * note: caller should free the (optionally) allocated buffer
1686 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1688 if (es->style & ES_PASSWORD) {
1689 INT len = get_text_length(es);
1690 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1691 text[len] = '\0';
1692 while(len) text[--len] = es->password_char;
1693 return text;
1694 } else
1695 return es->text;
1699 /*********************************************************************
1701 * EDIT_LockBuffer
1703 * This acts as a LocalLock16(), but it locks only once. This way
1704 * you can call it whenever you like, without unlocking.
1706 * Initially the edit control allocates a HLOCAL32 buffer
1707 * (32 bit linear memory handler). However, 16 bit application
1708 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1709 * handler). From that moment on we have to keep using this 16 bit memory
1710 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1711 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1712 * conversion.
1715 static void EDIT_LockBuffer(EDITSTATE *es)
1717 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1718 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1720 if (!es->text) {
1721 CHAR *textA = NULL;
1722 UINT countA = 0;
1723 BOOL _16bit = FALSE;
1725 if(es->hloc32W)
1727 if(es->hloc32A)
1729 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1730 textA = LocalLock(es->hloc32A);
1731 countA = strlen(textA) + 1;
1733 else if(es->hloc16)
1735 HANDLE16 oldDS = stack16->ds;
1736 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1737 stack16->ds = hInstance;
1738 textA = MapSL(LocalLock16(es->hloc16));
1739 stack16->ds = oldDS;
1740 countA = strlen(textA) + 1;
1741 _16bit = TRUE;
1744 else {
1745 ERR("no buffer ... please report\n");
1746 return;
1749 if(textA)
1751 HLOCAL hloc32W_new;
1752 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1753 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1754 if(countW_new > es->buffer_size + 1)
1756 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1757 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1758 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1759 if(hloc32W_new)
1761 es->hloc32W = hloc32W_new;
1762 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1763 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1765 else
1766 WARN("FAILED! Will synchronize partially\n");
1770 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1771 es->text = LocalLock(es->hloc32W);
1773 if(textA)
1775 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1776 if(_16bit)
1778 HANDLE16 oldDS = stack16->ds;
1779 stack16->ds = hInstance;
1780 LocalUnlock16(es->hloc16);
1781 stack16->ds = oldDS;
1783 else
1784 LocalUnlock(es->hloc32A);
1787 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1788 es->lock_count++;
1792 /*********************************************************************
1794 * EDIT_SL_InvalidateText
1796 * Called from EDIT_InvalidateText().
1797 * Does the job for single-line controls only.
1800 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1802 RECT line_rect;
1803 RECT rc;
1805 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1806 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1807 EDIT_UpdateText(es, &rc, TRUE);
1811 /*********************************************************************
1813 * EDIT_ML_InvalidateText
1815 * Called from EDIT_InvalidateText().
1816 * Does the job for multi-line controls only.
1819 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1821 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1822 INT sl = EDIT_EM_LineFromChar(es, start);
1823 INT el = EDIT_EM_LineFromChar(es, end);
1824 INT sc;
1825 INT ec;
1826 RECT rc1;
1827 RECT rcWnd;
1828 RECT rcLine;
1829 RECT rcUpdate;
1830 INT l;
1832 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1833 return;
1835 sc = start - EDIT_EM_LineIndex(es, sl);
1836 ec = end - EDIT_EM_LineIndex(es, el);
1837 if (sl < es->y_offset) {
1838 sl = es->y_offset;
1839 sc = 0;
1841 if (el > es->y_offset + vlc) {
1842 el = es->y_offset + vlc;
1843 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1845 GetClientRect(es->hwndSelf, &rc1);
1846 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1847 if (sl == el) {
1848 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1849 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1850 EDIT_UpdateText(es, &rcUpdate, TRUE);
1851 } else {
1852 EDIT_GetLineRect(es, sl, sc,
1853 EDIT_EM_LineLength(es,
1854 EDIT_EM_LineIndex(es, sl)),
1855 &rcLine);
1856 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1857 EDIT_UpdateText(es, &rcUpdate, TRUE);
1858 for (l = sl + 1 ; l < el ; l++) {
1859 EDIT_GetLineRect(es, l, 0,
1860 EDIT_EM_LineLength(es,
1861 EDIT_EM_LineIndex(es, l)),
1862 &rcLine);
1863 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1864 EDIT_UpdateText(es, &rcUpdate, TRUE);
1866 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1867 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1868 EDIT_UpdateText(es, &rcUpdate, TRUE);
1873 /*********************************************************************
1875 * EDIT_InvalidateText
1877 * Invalidate the text from offset start up to, but not including,
1878 * offset end. Useful for (re)painting the selection.
1879 * Regions outside the linewidth are not invalidated.
1880 * end == -1 means end == TextLength.
1881 * start and end need not be ordered.
1884 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1886 if (end == start)
1887 return;
1889 if (end == -1)
1890 end = get_text_length(es);
1892 if (end < start) {
1893 INT tmp = start;
1894 start = end;
1895 end = tmp;
1898 if (es->style & ES_MULTILINE)
1899 EDIT_ML_InvalidateText(es, start, end);
1900 else
1901 EDIT_SL_InvalidateText(es, start, end);
1905 /*********************************************************************
1907 * EDIT_MakeFit
1909 * Try to fit size + 1 characters in the buffer.
1911 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1913 HLOCAL hNew32W;
1915 if (size <= es->buffer_size)
1916 return TRUE;
1918 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1920 /* Force edit to unlock it's buffer. es->text now NULL */
1921 EDIT_UnlockBuffer(es, TRUE);
1923 if (es->hloc32W) {
1924 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1925 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1926 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1927 es->hloc32W = hNew32W;
1928 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1932 EDIT_LockBuffer(es);
1934 if (es->buffer_size < size) {
1935 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1936 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1937 return FALSE;
1938 } else {
1939 TRACE("We now have %d+1\n", es->buffer_size);
1940 return TRUE;
1945 /*********************************************************************
1947 * EDIT_MakeUndoFit
1949 * Try to fit size + 1 bytes in the undo buffer.
1952 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1954 UINT alloc_size;
1956 if (size <= es->undo_buffer_size)
1957 return TRUE;
1959 TRACE("trying to ReAlloc to %d+1\n", size);
1961 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1962 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1963 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1964 return TRUE;
1966 else
1968 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1969 return FALSE;
1974 /*********************************************************************
1976 * EDIT_MoveBackward
1979 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1981 INT e = es->selection_end;
1983 if (e) {
1984 e--;
1985 if ((es->style & ES_MULTILINE) && e &&
1986 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1987 e--;
1988 if (e && (es->text[e - 1] == '\r'))
1989 e--;
1992 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1993 EDIT_EM_ScrollCaret(es);
1997 /*********************************************************************
1999 * EDIT_MoveDown_ML
2001 * Only for multi line controls
2002 * Move the caret one line down, on a column with the nearest
2003 * x coordinate on the screen (might be a different column).
2006 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
2008 INT s = es->selection_start;
2009 INT e = es->selection_end;
2010 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2011 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2012 INT x = (short)LOWORD(pos);
2013 INT y = (short)HIWORD(pos);
2015 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
2016 if (!extend)
2017 s = e;
2018 EDIT_EM_SetSel(es, s, e, after_wrap);
2019 EDIT_EM_ScrollCaret(es);
2023 /*********************************************************************
2025 * EDIT_MoveEnd
2028 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
2030 BOOL after_wrap = FALSE;
2031 INT e;
2033 /* Pass a high value in x to make sure of receiving the end of the line */
2034 if (es->style & ES_MULTILINE)
2035 e = EDIT_CharFromPos(es, 0x3fffffff,
2036 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2037 else
2038 e = get_text_length(es);
2039 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2040 EDIT_EM_ScrollCaret(es);
2044 /*********************************************************************
2046 * EDIT_MoveForward
2049 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2051 INT e = es->selection_end;
2053 if (es->text[e]) {
2054 e++;
2055 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2056 if (es->text[e] == '\n')
2057 e++;
2058 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2059 e += 2;
2062 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2063 EDIT_EM_ScrollCaret(es);
2067 /*********************************************************************
2069 * EDIT_MoveHome
2071 * Home key: move to beginning of line.
2074 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2076 INT e;
2078 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2079 if (es->style & ES_MULTILINE)
2080 e = EDIT_CharFromPos(es, -es->x_offset,
2081 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2082 else
2083 e = 0;
2084 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2085 EDIT_EM_ScrollCaret(es);
2089 /*********************************************************************
2091 * EDIT_MovePageDown_ML
2093 * Only for multi line controls
2094 * Move the caret one page down, on a column with the nearest
2095 * x coordinate on the screen (might be a different column).
2098 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2100 INT s = es->selection_start;
2101 INT e = es->selection_end;
2102 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2103 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2104 INT x = (short)LOWORD(pos);
2105 INT y = (short)HIWORD(pos);
2107 e = EDIT_CharFromPos(es, x,
2108 y + (es->format_rect.bottom - es->format_rect.top),
2109 &after_wrap);
2110 if (!extend)
2111 s = e;
2112 EDIT_EM_SetSel(es, s, e, after_wrap);
2113 EDIT_EM_ScrollCaret(es);
2117 /*********************************************************************
2119 * EDIT_MovePageUp_ML
2121 * Only for multi line controls
2122 * Move the caret one page up, on a column with the nearest
2123 * x coordinate on the screen (might be a different column).
2126 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2128 INT s = es->selection_start;
2129 INT e = es->selection_end;
2130 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2131 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2132 INT x = (short)LOWORD(pos);
2133 INT y = (short)HIWORD(pos);
2135 e = EDIT_CharFromPos(es, x,
2136 y - (es->format_rect.bottom - es->format_rect.top),
2137 &after_wrap);
2138 if (!extend)
2139 s = e;
2140 EDIT_EM_SetSel(es, s, e, after_wrap);
2141 EDIT_EM_ScrollCaret(es);
2145 /*********************************************************************
2147 * EDIT_MoveUp_ML
2149 * Only for multi line controls
2150 * Move the caret one line up, on a column with the nearest
2151 * x coordinate on the screen (might be a different column).
2154 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2156 INT s = es->selection_start;
2157 INT e = es->selection_end;
2158 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2159 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2160 INT x = (short)LOWORD(pos);
2161 INT y = (short)HIWORD(pos);
2163 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2164 if (!extend)
2165 s = e;
2166 EDIT_EM_SetSel(es, s, e, after_wrap);
2167 EDIT_EM_ScrollCaret(es);
2171 /*********************************************************************
2173 * EDIT_MoveWordBackward
2176 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2178 INT s = es->selection_start;
2179 INT e = es->selection_end;
2180 INT l;
2181 INT ll;
2182 INT li;
2184 l = EDIT_EM_LineFromChar(es, e);
2185 ll = EDIT_EM_LineLength(es, e);
2186 li = EDIT_EM_LineIndex(es, l);
2187 if (e - li == 0) {
2188 if (l) {
2189 li = EDIT_EM_LineIndex(es, l - 1);
2190 e = li + EDIT_EM_LineLength(es, li);
2192 } else {
2193 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2195 if (!extend)
2196 s = e;
2197 EDIT_EM_SetSel(es, s, e, FALSE);
2198 EDIT_EM_ScrollCaret(es);
2202 /*********************************************************************
2204 * EDIT_MoveWordForward
2207 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2209 INT s = es->selection_start;
2210 INT e = es->selection_end;
2211 INT l;
2212 INT ll;
2213 INT li;
2215 l = EDIT_EM_LineFromChar(es, e);
2216 ll = EDIT_EM_LineLength(es, e);
2217 li = EDIT_EM_LineIndex(es, l);
2218 if (e - li == ll) {
2219 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2220 e = EDIT_EM_LineIndex(es, l + 1);
2221 } else {
2222 e = li + EDIT_CallWordBreakProc(es,
2223 li, e - li + 1, ll, WB_RIGHT);
2225 if (!extend)
2226 s = e;
2227 EDIT_EM_SetSel(es, s, e, FALSE);
2228 EDIT_EM_ScrollCaret(es);
2232 /*********************************************************************
2234 * EDIT_PaintLine
2237 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2239 INT s = es->selection_start;
2240 INT e = es->selection_end;
2241 INT li;
2242 INT ll;
2243 INT x;
2244 INT y;
2245 LRESULT pos;
2247 if (es->style & ES_MULTILINE) {
2248 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2249 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2250 return;
2251 } else if (line)
2252 return;
2254 TRACE("line=%d\n", line);
2256 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2257 x = (short)LOWORD(pos);
2258 y = (short)HIWORD(pos);
2259 li = EDIT_EM_LineIndex(es, line);
2260 ll = EDIT_EM_LineLength(es, li);
2261 s = min(es->selection_start, es->selection_end);
2262 e = max(es->selection_start, es->selection_end);
2263 s = min(li + ll, max(li, s));
2264 e = min(li + ll, max(li, e));
2265 if (rev && (s != e) &&
2266 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2267 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2268 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2269 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2270 } else
2271 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2275 /*********************************************************************
2277 * EDIT_PaintText
2280 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2282 COLORREF BkColor;
2283 COLORREF TextColor;
2284 LOGFONTW underline_font;
2285 HFONT hUnderline = 0;
2286 HFONT old_font = 0;
2287 INT ret;
2288 INT li;
2289 INT BkMode;
2290 SIZE size;
2292 if (!count)
2293 return 0;
2294 BkMode = GetBkMode(dc);
2295 BkColor = GetBkColor(dc);
2296 TextColor = GetTextColor(dc);
2297 if (rev) {
2298 if (es->composition_len == 0)
2300 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2301 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2302 SetBkMode( dc, OPAQUE);
2304 else
2306 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2307 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2308 underline_font.lfUnderline = TRUE;
2309 hUnderline = CreateFontIndirectW(&underline_font);
2310 old_font = SelectObject(dc,hUnderline);
2313 li = EDIT_EM_LineIndex(es, line);
2314 if (es->style & ES_MULTILINE) {
2315 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2316 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2317 } else {
2318 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2319 TextOutW(dc, x, y, text + li + col, count);
2320 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2321 ret = size.cx;
2322 if (es->style & ES_PASSWORD)
2323 HeapFree(GetProcessHeap(), 0, text);
2325 if (rev) {
2326 if (es->composition_len == 0)
2328 SetBkColor(dc, BkColor);
2329 SetTextColor(dc, TextColor);
2330 SetBkMode( dc, BkMode);
2332 else
2334 if (old_font)
2335 SelectObject(dc,old_font);
2336 if (hUnderline)
2337 DeleteObject(hUnderline);
2340 return ret;
2344 /*********************************************************************
2346 * EDIT_SetCaretPos
2349 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2350 BOOL after_wrap)
2352 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2353 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2354 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2358 /*********************************************************************
2360 * EDIT_AdjustFormatRect
2362 * Adjusts the format rectangle for the current font and the
2363 * current client rectangle.
2366 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2368 RECT ClientRect;
2370 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2371 if (es->style & ES_MULTILINE)
2373 INT fw, vlc, max_x_offset, max_y_offset;
2375 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2376 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2378 /* correct es->x_offset */
2379 fw = es->format_rect.right - es->format_rect.left;
2380 max_x_offset = es->text_width - fw;
2381 if(max_x_offset < 0) max_x_offset = 0;
2382 if(es->x_offset > max_x_offset)
2383 es->x_offset = max_x_offset;
2385 /* correct es->y_offset */
2386 max_y_offset = es->line_count - vlc;
2387 if(max_y_offset < 0) max_y_offset = 0;
2388 if(es->y_offset > max_y_offset)
2389 es->y_offset = max_y_offset;
2391 /* force scroll info update */
2392 EDIT_UpdateScrollInfo(es);
2394 else
2395 /* Windows doesn't care to fix text placement for SL controls */
2396 es->format_rect.bottom = es->format_rect.top + es->line_height;
2398 /* Always stay within the client area */
2399 GetClientRect(es->hwndSelf, &ClientRect);
2400 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2402 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2403 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2405 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2409 /*********************************************************************
2411 * EDIT_SetRectNP
2413 * note: this is not (exactly) the handler called on EM_SETRECTNP
2414 * it is also used to set the rect of a single line control
2417 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2419 LONG_PTR ExStyle;
2420 INT bw, bh;
2421 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2423 CopyRect(&es->format_rect, rc);
2425 if (ExStyle & WS_EX_CLIENTEDGE) {
2426 es->format_rect.left++;
2427 es->format_rect.right--;
2429 if (es->format_rect.bottom - es->format_rect.top
2430 >= es->line_height + 2)
2432 es->format_rect.top++;
2433 es->format_rect.bottom--;
2436 else if (es->style & WS_BORDER) {
2437 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2438 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2439 es->format_rect.left += bw;
2440 es->format_rect.right -= bw;
2441 if (es->format_rect.bottom - es->format_rect.top
2442 >= es->line_height + 2 * bh)
2444 es->format_rect.top += bh;
2445 es->format_rect.bottom -= bh;
2449 es->format_rect.left += es->left_margin;
2450 es->format_rect.right -= es->right_margin;
2451 EDIT_AdjustFormatRect(es);
2455 /*********************************************************************
2457 * EDIT_UnlockBuffer
2460 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2463 /* Edit window might be already destroyed */
2464 if(!IsWindow(es->hwndSelf))
2466 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2467 return;
2470 if (!es->lock_count) {
2471 ERR("lock_count == 0 ... please report\n");
2472 return;
2474 if (!es->text) {
2475 ERR("es->text == 0 ... please report\n");
2476 return;
2479 if (force || (es->lock_count == 1)) {
2480 if (es->hloc32W) {
2481 CHAR *textA = NULL;
2482 UINT countA = 0;
2483 UINT countW = get_text_length(es) + 1;
2484 STACK16FRAME* stack16 = NULL;
2485 HANDLE16 oldDS = 0;
2487 if(es->hloc32A)
2489 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2490 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2491 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2492 countA = LocalSize(es->hloc32A);
2493 if(countA_new > countA)
2495 HLOCAL hloc32A_new;
2496 UINT alloc_size = ROUND_TO_GROW(countA_new);
2497 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2498 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2499 if(hloc32A_new)
2501 es->hloc32A = hloc32A_new;
2502 countA = LocalSize(hloc32A_new);
2503 TRACE("Real new size %d bytes\n", countA);
2505 else
2506 WARN("FAILED! Will synchronize partially\n");
2508 textA = LocalLock(es->hloc32A);
2510 else if(es->hloc16)
2512 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2514 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2515 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2517 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2518 oldDS = stack16->ds;
2519 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2521 countA = LocalSize16(es->hloc16);
2522 if(countA_new > countA)
2524 HLOCAL16 hloc16_new;
2525 UINT alloc_size = ROUND_TO_GROW(countA_new);
2526 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2527 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2528 if(hloc16_new)
2530 es->hloc16 = hloc16_new;
2531 countA = LocalSize16(hloc16_new);
2532 TRACE("Real new size %d bytes\n", countA);
2534 else
2535 WARN("FAILED! Will synchronize partially\n");
2537 textA = MapSL(LocalLock16(es->hloc16));
2540 if(textA)
2542 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2543 if(stack16)
2544 LocalUnlock16(es->hloc16);
2545 else
2546 LocalUnlock(es->hloc32A);
2549 if (stack16) stack16->ds = oldDS;
2550 LocalUnlock(es->hloc32W);
2551 es->text = NULL;
2553 else {
2554 ERR("no buffer ... please report\n");
2555 return;
2558 es->lock_count--;
2562 /*********************************************************************
2564 * EDIT_UpdateScrollInfo
2567 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2569 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2571 SCROLLINFO si;
2572 si.cbSize = sizeof(SCROLLINFO);
2573 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2574 si.nMin = 0;
2575 si.nMax = es->line_count - 1;
2576 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2577 si.nPos = es->y_offset;
2578 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2579 si.nMin, si.nMax, si.nPage, si.nPos);
2580 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2583 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2585 SCROLLINFO si;
2586 si.cbSize = sizeof(SCROLLINFO);
2587 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2588 si.nMin = 0;
2589 si.nMax = es->text_width - 1;
2590 si.nPage = es->format_rect.right - es->format_rect.left;
2591 si.nPos = es->x_offset;
2592 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2593 si.nMin, si.nMax, si.nPage, si.nPos);
2594 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2598 /*********************************************************************
2600 * EDIT_WordBreakProc
2602 * Find the beginning of words.
2603 * Note: unlike the specs for a WordBreakProc, this function only
2604 * allows to be called without linebreaks between s[0] up to
2605 * s[count - 1]. Remember it is only called
2606 * internally, so we can decide this for ourselves.
2609 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2611 INT ret = 0;
2613 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2615 if(!s) return 0;
2617 switch (action) {
2618 case WB_LEFT:
2619 if (!count)
2620 break;
2621 if (index)
2622 index--;
2623 if (s[index] == ' ') {
2624 while (index && (s[index] == ' '))
2625 index--;
2626 if (index) {
2627 while (index && (s[index] != ' '))
2628 index--;
2629 if (s[index] == ' ')
2630 index++;
2632 } else {
2633 while (index && (s[index] != ' '))
2634 index--;
2635 if (s[index] == ' ')
2636 index++;
2638 ret = index;
2639 break;
2640 case WB_RIGHT:
2641 if (!count)
2642 break;
2643 if (index)
2644 index--;
2645 if (s[index] == ' ')
2646 while ((index < count) && (s[index] == ' ')) index++;
2647 else {
2648 while (s[index] && (s[index] != ' ') && (index < count))
2649 index++;
2650 while ((s[index] == ' ') && (index < count)) index++;
2652 ret = index;
2653 break;
2654 case WB_ISDELIMITER:
2655 ret = (s[index] == ' ');
2656 break;
2657 default:
2658 ERR("unknown action code, please report !\n");
2659 break;
2661 return ret;
2665 /*********************************************************************
2667 * EM_CHARFROMPOS
2669 * returns line number (not index) in high-order word of result.
2670 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2671 * to Richedit, not to the edit control. Original documentation is valid.
2672 * FIXME: do the specs mean to return -1 if outside client area or
2673 * if outside formatting rectangle ???
2676 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2678 POINT pt;
2679 RECT rc;
2680 INT index;
2682 pt.x = x;
2683 pt.y = y;
2684 GetClientRect(es->hwndSelf, &rc);
2685 if (!PtInRect(&rc, pt))
2686 return -1;
2688 index = EDIT_CharFromPos(es, x, y, NULL);
2689 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2693 /*********************************************************************
2695 * EM_FMTLINES
2697 * Enable or disable soft breaks.
2699 * This means: insert or remove the soft linebreak character (\r\r\n).
2700 * Take care to check if the text still fits the buffer after insertion.
2701 * If not, notify with EN_ERRSPACE.
2704 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2706 es->flags &= ~EF_USE_SOFTBRK;
2707 if (add_eol) {
2708 es->flags |= EF_USE_SOFTBRK;
2709 FIXME("soft break enabled, not implemented\n");
2711 return add_eol;
2715 /*********************************************************************
2717 * EM_GETHANDLE
2719 * Hopefully this won't fire back at us.
2720 * We always start with a fixed buffer in the local heap.
2721 * Despite of the documentation says that the local heap is used
2722 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2723 * buffer on the local heap.
2726 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2728 HLOCAL hLocal;
2730 if (!(es->style & ES_MULTILINE))
2731 return 0;
2733 if(es->is_unicode)
2734 hLocal = es->hloc32W;
2735 else
2737 if(!es->hloc32A)
2739 CHAR *textA;
2740 UINT countA, alloc_size;
2741 TRACE("Allocating 32-bit ANSI alias buffer\n");
2742 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2743 alloc_size = ROUND_TO_GROW(countA);
2744 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2746 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2747 return 0;
2749 textA = LocalLock(es->hloc32A);
2750 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2751 LocalUnlock(es->hloc32A);
2753 hLocal = es->hloc32A;
2756 es->flags |= EF_APP_HAS_HANDLE;
2757 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2758 return hLocal;
2762 /*********************************************************************
2764 * EM_GETHANDLE16
2766 * Hopefully this won't fire back at us.
2767 * We always start with a buffer in 32 bit linear memory.
2768 * However, with this message a 16 bit application requests
2769 * a handle of 16 bit local heap memory, where it expects to find
2770 * the text.
2771 * It's a pitty that from this moment on we have to use this
2772 * local heap, because applications may rely on the handle
2773 * in the future.
2775 * In this function we'll try to switch to local heap.
2777 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2779 CHAR *textA;
2780 UINT countA, alloc_size;
2781 STACK16FRAME* stack16;
2782 HANDLE16 oldDS;
2784 if (!(es->style & ES_MULTILINE))
2785 return 0;
2787 if (es->hloc16)
2788 return es->hloc16;
2790 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2791 oldDS = stack16->ds;
2792 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2794 if (!LocalHeapSize16()) {
2796 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2797 ERR("could not initialize local heap\n");
2798 goto done;
2800 TRACE("local heap initialized\n");
2803 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2804 alloc_size = ROUND_TO_GROW(countA);
2806 TRACE("Allocating 16-bit ANSI alias buffer\n");
2807 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2808 ERR("could not allocate new 16 bit buffer\n");
2809 goto done;
2812 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2813 ERR("could not lock new 16 bit buffer\n");
2814 LocalFree16(es->hloc16);
2815 es->hloc16 = 0;
2816 goto done;
2819 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2820 LocalUnlock16(es->hloc16);
2821 es->flags |= EF_APP_HAS_HANDLE;
2823 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2825 done:
2826 stack16->ds = oldDS;
2827 return es->hloc16;
2831 /*********************************************************************
2833 * EM_GETLINE
2836 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2838 LPWSTR src;
2839 INT line_len, dst_len;
2840 INT i;
2842 if (es->style & ES_MULTILINE) {
2843 if (line >= es->line_count)
2844 return 0;
2845 } else
2846 line = 0;
2847 i = EDIT_EM_LineIndex(es, line);
2848 src = es->text + i;
2849 line_len = EDIT_EM_LineLength(es, i);
2850 dst_len = *(WORD *)dst;
2851 if(unicode)
2853 if(dst_len <= line_len)
2855 memcpy(dst, src, dst_len * sizeof(WCHAR));
2856 return dst_len;
2858 else /* Append 0 if enough space */
2860 memcpy(dst, src, line_len * sizeof(WCHAR));
2861 dst[line_len] = 0;
2862 return line_len;
2865 else
2867 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2868 if(!ret && line_len) /* Insufficient buffer size */
2869 return dst_len;
2870 if(ret < dst_len) /* Append 0 if enough space */
2871 ((LPSTR)dst)[ret] = 0;
2872 return ret;
2877 /*********************************************************************
2879 * EM_GETSEL
2882 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2884 UINT s = es->selection_start;
2885 UINT e = es->selection_end;
2887 ORDER_UINT(s, e);
2888 if (start)
2889 *start = s;
2890 if (end)
2891 *end = e;
2892 return MAKELONG(s, e);
2896 /*********************************************************************
2898 * EM_GETTHUMB
2900 * FIXME: is this right ? (or should it be only VSCROLL)
2901 * (and maybe only for edit controls that really have their
2902 * own scrollbars) (and maybe only for multiline controls ?)
2903 * All in all: very poorly documented
2906 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2908 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2909 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2913 /*********************************************************************
2915 * EM_LINEFROMCHAR
2918 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2920 INT line;
2921 LINEDEF *line_def;
2923 if (!(es->style & ES_MULTILINE))
2924 return 0;
2925 if (index > (INT)get_text_length(es))
2926 return es->line_count - 1;
2927 if (index == -1)
2928 index = min(es->selection_start, es->selection_end);
2930 line = 0;
2931 line_def = es->first_line_def;
2932 index -= line_def->length;
2933 while ((index >= 0) && line_def->next) {
2934 line++;
2935 line_def = line_def->next;
2936 index -= line_def->length;
2938 return line;
2942 /*********************************************************************
2944 * EM_LINEINDEX
2947 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
2949 INT line_index;
2950 const LINEDEF *line_def;
2952 if (!(es->style & ES_MULTILINE))
2953 return 0;
2954 if (line >= es->line_count)
2955 return -1;
2957 line_index = 0;
2958 line_def = es->first_line_def;
2959 if (line == -1) {
2960 INT index = es->selection_end - line_def->length;
2961 while ((index >= 0) && line_def->next) {
2962 line_index += line_def->length;
2963 line_def = line_def->next;
2964 index -= line_def->length;
2966 } else {
2967 while (line > 0) {
2968 line_index += line_def->length;
2969 line_def = line_def->next;
2970 line--;
2973 return line_index;
2977 /*********************************************************************
2979 * EM_LINELENGTH
2982 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2984 LINEDEF *line_def;
2986 if (!(es->style & ES_MULTILINE))
2987 return get_text_length(es);
2989 if (index == -1) {
2990 /* get the number of remaining non-selected chars of selected lines */
2991 INT32 l; /* line number */
2992 INT32 li; /* index of first char in line */
2993 INT32 count;
2994 l = EDIT_EM_LineFromChar(es, es->selection_start);
2995 /* # chars before start of selection area */
2996 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2997 l = EDIT_EM_LineFromChar(es, es->selection_end);
2998 /* # chars after end of selection */
2999 li = EDIT_EM_LineIndex(es, l);
3000 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
3001 return count;
3003 line_def = es->first_line_def;
3004 index -= line_def->length;
3005 while ((index >= 0) && line_def->next) {
3006 line_def = line_def->next;
3007 index -= line_def->length;
3009 return line_def->net_length;
3013 /*********************************************************************
3015 * EM_LINESCROLL
3017 * NOTE: dx is in average character widths, dy - in lines;
3020 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
3022 if (!(es->style & ES_MULTILINE))
3023 return FALSE;
3025 dx *= es->char_width;
3026 return EDIT_EM_LineScroll_internal(es, dx, dy);
3029 /*********************************************************************
3031 * EDIT_EM_LineScroll_internal
3033 * Version of EDIT_EM_LineScroll for internal use.
3034 * It doesn't refuse if ES_MULTILINE is set and assumes that
3035 * dx is in pixels, dy - in lines.
3038 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3040 INT nyoff;
3041 INT x_offset_in_pixels;
3042 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3043 es->line_height;
3045 if (es->style & ES_MULTILINE)
3047 x_offset_in_pixels = es->x_offset;
3049 else
3051 dy = 0;
3052 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3055 if (-dx > x_offset_in_pixels)
3056 dx = -x_offset_in_pixels;
3057 if (dx > es->text_width - x_offset_in_pixels)
3058 dx = es->text_width - x_offset_in_pixels;
3059 nyoff = max(0, es->y_offset + dy);
3060 if (nyoff >= es->line_count - lines_per_page)
3061 nyoff = max(0, es->line_count - lines_per_page);
3062 dy = (es->y_offset - nyoff) * es->line_height;
3063 if (dx || dy) {
3064 RECT rc1;
3065 RECT rc;
3067 es->y_offset = nyoff;
3068 if(es->style & ES_MULTILINE)
3069 es->x_offset += dx;
3070 else
3071 es->x_offset += dx / es->char_width;
3073 GetClientRect(es->hwndSelf, &rc1);
3074 IntersectRect(&rc, &rc1, &es->format_rect);
3075 ScrollWindowEx(es->hwndSelf, -dx, dy,
3076 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3077 /* force scroll info update */
3078 EDIT_UpdateScrollInfo(es);
3080 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3081 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3082 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3083 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3084 return TRUE;
3088 /*********************************************************************
3090 * EM_POSFROMCHAR
3093 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3095 INT len = get_text_length(es);
3096 INT l;
3097 INT li;
3098 INT x;
3099 INT y = 0;
3100 INT w;
3101 INT lw = 0;
3102 INT ll = 0;
3103 HDC dc;
3104 HFONT old_font = 0;
3105 SIZE size;
3106 LINEDEF *line_def;
3108 index = min(index, len);
3109 dc = GetDC(es->hwndSelf);
3110 if (es->font)
3111 old_font = SelectObject(dc, es->font);
3112 if (es->style & ES_MULTILINE) {
3113 l = EDIT_EM_LineFromChar(es, index);
3114 y = (l - es->y_offset) * es->line_height;
3115 li = EDIT_EM_LineIndex(es, l);
3116 if (after_wrap && (li == index) && l) {
3117 INT l2 = l - 1;
3118 line_def = es->first_line_def;
3119 while (l2) {
3120 line_def = line_def->next;
3121 l2--;
3123 if (line_def->ending == END_WRAP) {
3124 l--;
3125 y -= es->line_height;
3126 li = EDIT_EM_LineIndex(es, l);
3130 line_def = es->first_line_def;
3131 while (line_def->index != li)
3132 line_def = line_def->next;
3134 ll = line_def->net_length;
3135 lw = line_def->width;
3137 w = es->format_rect.right - es->format_rect.left;
3138 if (es->style & ES_RIGHT)
3140 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3141 es->tabs_count, es->tabs)) - es->x_offset;
3142 x = w - x;
3144 else if (es->style & ES_CENTER)
3146 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3147 es->tabs_count, es->tabs)) - es->x_offset;
3148 x += (w - lw) / 2;
3150 else /* ES_LEFT */
3152 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3153 es->tabs_count, es->tabs)) - es->x_offset;
3155 } else {
3156 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3157 if (index < es->x_offset) {
3158 GetTextExtentPoint32W(dc, text + index,
3159 es->x_offset - index, &size);
3160 x = -size.cx;
3161 } else {
3162 GetTextExtentPoint32W(dc, text + es->x_offset,
3163 index - es->x_offset, &size);
3164 x = size.cx;
3166 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3168 w = es->format_rect.right - es->format_rect.left;
3169 if (w > es->text_width)
3171 if (es->style & ES_RIGHT)
3172 x += w - es->text_width;
3173 else if (es->style & ES_CENTER)
3174 x += (w - es->text_width) / 2;
3178 y = 0;
3179 if (es->style & ES_PASSWORD)
3180 HeapFree(GetProcessHeap(), 0, text);
3182 x += es->format_rect.left;
3183 y += es->format_rect.top;
3184 if (es->font)
3185 SelectObject(dc, old_font);
3186 ReleaseDC(es->hwndSelf, dc);
3187 return MAKELONG((INT16)x, (INT16)y);
3191 /*********************************************************************
3193 * EM_REPLACESEL
3195 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3198 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3200 UINT strl = strlenW(lpsz_replace);
3201 UINT tl = get_text_length(es);
3202 UINT utl;
3203 UINT s;
3204 UINT e;
3205 UINT i;
3206 UINT size;
3207 LPWSTR p;
3208 HRGN hrgn = 0;
3209 LPWSTR buf = NULL;
3210 UINT bufl = 0;
3212 TRACE("%s, can_undo %d, send_update %d\n",
3213 debugstr_w(lpsz_replace), can_undo, send_update);
3215 s = es->selection_start;
3216 e = es->selection_end;
3218 if ((s == e) && !strl)
3219 return;
3221 ORDER_UINT(s, e);
3223 size = tl - (e - s) + strl;
3224 if (!size)
3225 es->text_width = 0;
3227 /* Issue the EN_MAXTEXT notification and continue with replacing text
3228 * such that buffer limit is honored. */
3229 if ((honor_limit) && (size > es->buffer_limit)) {
3230 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3231 /* Buffer limit can be smaller than the actual length of text in combobox */
3232 if (es->buffer_limit < (tl - (e-s)))
3233 strl = 0;
3234 else
3235 strl = es->buffer_limit - (tl - (e-s));
3238 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3239 return;
3241 if (e != s) {
3242 /* there is something to be deleted */
3243 TRACE("deleting stuff.\n");
3244 bufl = e - s;
3245 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3246 if (!buf) return;
3247 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3248 buf[bufl] = 0; /* ensure 0 termination */
3249 /* now delete */
3250 strcpyW(es->text + s, es->text + e);
3251 text_buffer_changed(es);
3253 if (strl) {
3254 /* there is an insertion */
3255 tl = get_text_length(es);
3256 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));
3257 for (p = es->text + tl ; p >= es->text + s ; p--)
3258 p[strl] = p[0];
3259 for (i = 0 , p = es->text + s ; i < strl ; i++)
3260 p[i] = lpsz_replace[i];
3261 if(es->style & ES_UPPERCASE)
3262 CharUpperBuffW(p, strl);
3263 else if(es->style & ES_LOWERCASE)
3264 CharLowerBuffW(p, strl);
3265 text_buffer_changed(es);
3267 if (es->style & ES_MULTILINE)
3269 INT st = min(es->selection_start, es->selection_end);
3270 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3272 hrgn = CreateRectRgn(0, 0, 0, 0);
3273 EDIT_BuildLineDefs_ML(es, st, st + strl,
3274 strl - abs(es->selection_end - es->selection_start), hrgn);
3275 /* if text is too long undo all changes */
3276 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3277 if (strl)
3278 strcpyW(es->text + e, es->text + e + strl);
3279 if (e != s)
3280 for (i = 0 , p = es->text ; i < e - s ; i++)
3281 p[i + s] = buf[i];
3282 text_buffer_changed(es);
3283 EDIT_BuildLineDefs_ML(es, s, e,
3284 abs(es->selection_end - es->selection_start) - strl, hrgn);
3285 strl = 0;
3286 e = s;
3287 hrgn = CreateRectRgn(0, 0, 0, 0);
3288 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3291 else {
3292 INT fw = es->format_rect.right - es->format_rect.left;
3293 EDIT_CalcLineWidth_SL(es);
3294 /* remove chars that don't fit */
3295 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3296 while ((es->text_width > fw) && s + strl >= s) {
3297 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3298 strl--;
3299 EDIT_CalcLineWidth_SL(es);
3301 text_buffer_changed(es);
3302 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3306 if (e != s) {
3307 if (can_undo) {
3308 utl = strlenW(es->undo_text);
3309 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3310 /* undo-buffer is extended to the right */
3311 EDIT_MakeUndoFit(es, utl + e - s);
3312 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3313 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3314 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3315 /* undo-buffer is extended to the left */
3316 EDIT_MakeUndoFit(es, utl + e - s);
3317 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3318 p[e - s] = p[0];
3319 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3320 p[i] = buf[i];
3321 es->undo_position = s;
3322 } else {
3323 /* new undo-buffer */
3324 EDIT_MakeUndoFit(es, e - s);
3325 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3326 es->undo_text[e - s] = 0; /* ensure 0 termination */
3327 es->undo_position = s;
3329 /* any deletion makes the old insertion-undo invalid */
3330 es->undo_insert_count = 0;
3331 } else
3332 EDIT_EM_EmptyUndoBuffer(es);
3334 if (strl) {
3335 if (can_undo) {
3336 if ((s == es->undo_position) ||
3337 ((es->undo_insert_count) &&
3338 (s == es->undo_position + es->undo_insert_count)))
3340 * insertion is new and at delete position or
3341 * an extension to either left or right
3343 es->undo_insert_count += strl;
3344 else {
3345 /* new insertion undo */
3346 es->undo_position = s;
3347 es->undo_insert_count = strl;
3348 /* new insertion makes old delete-buffer invalid */
3349 *es->undo_text = '\0';
3351 } else
3352 EDIT_EM_EmptyUndoBuffer(es);
3355 if (bufl)
3356 HeapFree(GetProcessHeap(), 0, buf);
3358 s += strl;
3360 /* If text has been deleted and we're right or center aligned then scroll rightward */
3361 if (es->style & (ES_RIGHT | ES_CENTER))
3363 INT delta = strl - abs(es->selection_end - es->selection_start);
3365 if (delta < 0 && es->x_offset)
3367 if (abs(delta) > es->x_offset)
3368 es->x_offset = 0;
3369 else
3370 es->x_offset += delta;
3374 EDIT_EM_SetSel(es, s, s, FALSE);
3375 es->flags |= EF_MODIFIED;
3376 if (send_update) es->flags |= EF_UPDATE;
3377 if (hrgn)
3379 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3380 DeleteObject(hrgn);
3382 else
3383 EDIT_UpdateText(es, NULL, TRUE);
3385 EDIT_EM_ScrollCaret(es);
3387 /* force scroll info update */
3388 EDIT_UpdateScrollInfo(es);
3391 if(send_update || (es->flags & EF_UPDATE))
3393 es->flags &= ~EF_UPDATE;
3394 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3399 /*********************************************************************
3401 * EM_SCROLL
3404 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3406 INT dy;
3408 if (!(es->style & ES_MULTILINE))
3409 return (LRESULT)FALSE;
3411 dy = 0;
3413 switch (action) {
3414 case SB_LINEUP:
3415 if (es->y_offset)
3416 dy = -1;
3417 break;
3418 case SB_LINEDOWN:
3419 if (es->y_offset < es->line_count - 1)
3420 dy = 1;
3421 break;
3422 case SB_PAGEUP:
3423 if (es->y_offset)
3424 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3425 break;
3426 case SB_PAGEDOWN:
3427 if (es->y_offset < es->line_count - 1)
3428 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3429 break;
3430 default:
3431 return (LRESULT)FALSE;
3433 if (dy) {
3434 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3435 /* check if we are going to move too far */
3436 if(es->y_offset + dy > es->line_count - vlc)
3437 dy = es->line_count - vlc - es->y_offset;
3439 /* Notification is done in EDIT_EM_LineScroll */
3440 if(dy)
3441 EDIT_EM_LineScroll(es, 0, dy);
3443 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3447 /*********************************************************************
3449 * EM_SCROLLCARET
3452 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3454 if (es->style & ES_MULTILINE) {
3455 INT l;
3456 INT li;
3457 INT vlc;
3458 INT ww;
3459 INT cw = es->char_width;
3460 INT x;
3461 INT dy = 0;
3462 INT dx = 0;
3464 l = EDIT_EM_LineFromChar(es, es->selection_end);
3465 li = EDIT_EM_LineIndex(es, l);
3466 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3467 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3468 if (l >= es->y_offset + vlc)
3469 dy = l - vlc + 1 - es->y_offset;
3470 if (l < es->y_offset)
3471 dy = l - es->y_offset;
3472 ww = es->format_rect.right - es->format_rect.left;
3473 if (x < es->format_rect.left)
3474 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3475 if (x > es->format_rect.right)
3476 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3477 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3479 /* check if we are going to move too far */
3480 if(es->x_offset + dx + ww > es->text_width)
3481 dx = es->text_width - ww - es->x_offset;
3482 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3483 EDIT_EM_LineScroll_internal(es, dx, dy);
3485 } else {
3486 INT x;
3487 INT goal;
3488 INT format_width;
3490 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3491 format_width = es->format_rect.right - es->format_rect.left;
3492 if (x < es->format_rect.left) {
3493 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3494 do {
3495 es->x_offset--;
3496 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3497 } while ((x < goal) && es->x_offset);
3498 /* FIXME: use ScrollWindow() somehow to improve performance */
3499 EDIT_UpdateText(es, NULL, TRUE);
3500 } else if (x > es->format_rect.right) {
3501 INT x_last;
3502 INT len = get_text_length(es);
3503 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3504 do {
3505 es->x_offset++;
3506 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3507 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3508 } while ((x > goal) && (x_last > es->format_rect.right));
3509 /* FIXME: use ScrollWindow() somehow to improve performance */
3510 EDIT_UpdateText(es, NULL, TRUE);
3514 if(es->flags & EF_FOCUSED)
3515 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3519 /*********************************************************************
3521 * EM_SETHANDLE
3523 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3526 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3528 if (!(es->style & ES_MULTILINE))
3529 return;
3531 if (!hloc) {
3532 WARN("called with NULL handle\n");
3533 return;
3536 EDIT_UnlockBuffer(es, TRUE);
3538 if(es->hloc16)
3540 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3541 HANDLE16 oldDS = stack16->ds;
3543 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3544 LocalFree16(es->hloc16);
3545 stack16->ds = oldDS;
3546 es->hloc16 = 0;
3549 if(es->is_unicode)
3551 if(es->hloc32A)
3553 LocalFree(es->hloc32A);
3554 es->hloc32A = NULL;
3556 es->hloc32W = hloc;
3558 else
3560 INT countW, countA;
3561 HLOCAL hloc32W_new;
3562 WCHAR *textW;
3563 CHAR *textA;
3565 countA = LocalSize(hloc);
3566 textA = LocalLock(hloc);
3567 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3568 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3570 ERR("Could not allocate new unicode buffer\n");
3571 return;
3573 textW = LocalLock(hloc32W_new);
3574 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3575 LocalUnlock(hloc32W_new);
3576 LocalUnlock(hloc);
3578 if(es->hloc32W)
3579 LocalFree(es->hloc32W);
3581 es->hloc32W = hloc32W_new;
3582 es->hloc32A = hloc;
3585 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3587 es->flags |= EF_APP_HAS_HANDLE;
3588 EDIT_LockBuffer(es);
3590 es->x_offset = es->y_offset = 0;
3591 es->selection_start = es->selection_end = 0;
3592 EDIT_EM_EmptyUndoBuffer(es);
3593 es->flags &= ~EF_MODIFIED;
3594 es->flags &= ~EF_UPDATE;
3595 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3596 EDIT_UpdateText(es, NULL, TRUE);
3597 EDIT_EM_ScrollCaret(es);
3598 /* force scroll info update */
3599 EDIT_UpdateScrollInfo(es);
3603 /*********************************************************************
3605 * EM_SETHANDLE16
3607 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3610 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3612 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3613 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3614 HANDLE16 oldDS = stack16->ds;
3615 INT countW, countA;
3616 HLOCAL hloc32W_new;
3617 WCHAR *textW;
3618 CHAR *textA;
3620 if (!(es->style & ES_MULTILINE))
3621 return;
3623 if (!hloc) {
3624 WARN("called with NULL handle\n");
3625 return;
3628 EDIT_UnlockBuffer(es, TRUE);
3630 if(es->hloc32A)
3632 LocalFree(es->hloc32A);
3633 es->hloc32A = NULL;
3636 stack16->ds = hInstance;
3637 countA = LocalSize16(hloc);
3638 textA = MapSL(LocalLock16(hloc));
3639 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3640 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3642 ERR("Could not allocate new unicode buffer\n");
3643 return;
3645 textW = LocalLock(hloc32W_new);
3646 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3647 LocalUnlock(hloc32W_new);
3648 LocalUnlock16(hloc);
3649 stack16->ds = oldDS;
3651 if(es->hloc32W)
3652 LocalFree(es->hloc32W);
3654 es->hloc32W = hloc32W_new;
3655 es->hloc16 = hloc;
3657 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3659 es->flags |= EF_APP_HAS_HANDLE;
3660 EDIT_LockBuffer(es);
3662 es->x_offset = es->y_offset = 0;
3663 es->selection_start = es->selection_end = 0;
3664 EDIT_EM_EmptyUndoBuffer(es);
3665 es->flags &= ~EF_MODIFIED;
3666 es->flags &= ~EF_UPDATE;
3667 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3668 EDIT_UpdateText(es, NULL, TRUE);
3669 EDIT_EM_ScrollCaret(es);
3670 /* force scroll info update */
3671 EDIT_UpdateScrollInfo(es);
3675 /*********************************************************************
3677 * EM_SETLIMITTEXT
3679 * NOTE: this version currently implements WinNT limits
3682 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
3684 if (!limit) limit = ~0u;
3685 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
3686 es->buffer_limit = limit;
3690 /*********************************************************************
3692 * EM_SETMARGINS
3694 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3695 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3696 * margin according to the textmetrics of the current font.
3698 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3699 * of the char's width as the margin, but this is not how Windows handles this.
3700 * For all other fonts Windows sets the margins to zero.
3702 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3703 * edit control is equal to or larger than a certain size.
3704 * Interestingly if one subtracts both the left and right margins from
3705 * this size one always seems to get an even number. The extents of
3706 * the (four character) string "'**'" match this quite closely, so
3707 * we'll use this until we come up with a better idea.
3709 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3711 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3712 SIZE sz;
3714 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3715 return sz.cx + left + right;
3718 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3719 WORD left, WORD right, BOOL repaint)
3721 TEXTMETRICW tm;
3722 INT default_left_margin = 0; /* in pixels */
3723 INT default_right_margin = 0; /* in pixels */
3725 /* Set the default margins depending on the font */
3726 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3727 HDC dc = GetDC(es->hwndSelf);
3728 HFONT old_font = SelectObject(dc, es->font);
3729 GetTextMetricsW(dc, &tm);
3730 /* The default margins are only non zero for TrueType or Vector fonts */
3731 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3732 int min_size;
3733 RECT rc;
3734 /* This must be calculated more exactly! But how? */
3735 default_left_margin = tm.tmAveCharWidth / 2;
3736 default_right_margin = tm.tmAveCharWidth / 2;
3737 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3738 GetClientRect(es->hwndSelf, &rc);
3739 if(rc.right - rc.left < min_size) {
3740 default_left_margin = es->left_margin;
3741 default_right_margin = es->right_margin;
3744 SelectObject(dc, old_font);
3745 ReleaseDC(es->hwndSelf, dc);
3748 if (action & EC_LEFTMARGIN) {
3749 es->format_rect.left -= es->left_margin;
3750 if (left != EC_USEFONTINFO)
3751 es->left_margin = left;
3752 else
3753 es->left_margin = default_left_margin;
3754 es->format_rect.left += es->left_margin;
3757 if (action & EC_RIGHTMARGIN) {
3758 es->format_rect.right += es->right_margin;
3759 if (right != EC_USEFONTINFO)
3760 es->right_margin = right;
3761 else
3762 es->right_margin = default_right_margin;
3763 es->format_rect.right -= es->right_margin;
3766 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3767 EDIT_AdjustFormatRect(es);
3768 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3771 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3775 /*********************************************************************
3777 * EM_SETPASSWORDCHAR
3780 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3782 LONG style;
3784 if (es->style & ES_MULTILINE)
3785 return;
3787 if (es->password_char == c)
3788 return;
3790 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3791 es->password_char = c;
3792 if (c) {
3793 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3794 es->style |= ES_PASSWORD;
3795 } else {
3796 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3797 es->style &= ~ES_PASSWORD;
3799 EDIT_UpdateText(es, NULL, TRUE);
3803 /*********************************************************************
3805 * EDIT_EM_SetSel
3807 * note: unlike the specs say: the order of start and end
3808 * _is_ preserved in Windows. (i.e. start can be > end)
3809 * In other words: this handler is OK
3812 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3814 UINT old_start = es->selection_start;
3815 UINT old_end = es->selection_end;
3816 UINT len = get_text_length(es);
3818 if (start == (UINT)-1) {
3819 start = es->selection_end;
3820 end = es->selection_end;
3821 } else {
3822 start = min(start, len);
3823 end = min(end, len);
3825 es->selection_start = start;
3826 es->selection_end = end;
3827 if (after_wrap)
3828 es->flags |= EF_AFTER_WRAP;
3829 else
3830 es->flags &= ~EF_AFTER_WRAP;
3831 /* Compute the necessary invalidation region. */
3832 /* Note that we don't need to invalidate regions which have
3833 * "never" been selected, or those which are "still" selected.
3834 * In fact, every time we hit a selection boundary, we can
3835 * *toggle* whether we need to invalidate. Thus we can optimize by
3836 * *sorting* the interval endpoints. Let's assume that we sort them
3837 * in this order:
3838 * start <= end <= old_start <= old_end
3839 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3840 * in 5 comparisons; ie it's impossible to do better than the
3841 * following: */
3842 ORDER_UINT(end, old_end);
3843 ORDER_UINT(start, old_start);
3844 ORDER_UINT(old_start, old_end);
3845 ORDER_UINT(start, end);
3846 /* Note that at this point 'end' and 'old_start' are not in order, but
3847 * start is definitely the min. and old_end is definitely the max. */
3848 if (end != old_start)
3851 * One can also do
3852 * ORDER_UINT32(end, old_start);
3853 * EDIT_InvalidateText(es, start, end);
3854 * EDIT_InvalidateText(es, old_start, old_end);
3855 * in place of the following if statement.
3856 * (That would complete the optimal five-comparison four-element sort.)
3858 if (old_start > end )
3860 EDIT_InvalidateText(es, start, end);
3861 EDIT_InvalidateText(es, old_start, old_end);
3863 else
3865 EDIT_InvalidateText(es, start, old_start);
3866 EDIT_InvalidateText(es, end, old_end);
3869 else EDIT_InvalidateText(es, start, old_end);
3873 /*********************************************************************
3875 * EM_SETTABSTOPS
3878 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3880 if (!(es->style & ES_MULTILINE))
3881 return FALSE;
3882 HeapFree(GetProcessHeap(), 0, es->tabs);
3883 es->tabs_count = count;
3884 if (!count)
3885 es->tabs = NULL;
3886 else {
3887 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3888 memcpy(es->tabs, tabs, count * sizeof(INT));
3890 return TRUE;
3894 /*********************************************************************
3896 * EM_SETTABSTOPS16
3899 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs)
3901 if (!(es->style & ES_MULTILINE))
3902 return FALSE;
3903 HeapFree(GetProcessHeap(), 0, es->tabs);
3904 es->tabs_count = count;
3905 if (!count)
3906 es->tabs = NULL;
3907 else {
3908 INT i;
3909 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3910 for (i = 0 ; i < count ; i++)
3911 es->tabs[i] = *tabs++;
3913 return TRUE;
3917 /*********************************************************************
3919 * EM_SETWORDBREAKPROC
3922 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3924 if (es->word_break_proc == wbp)
3925 return;
3927 es->word_break_proc = wbp;
3928 es->word_break_proc16 = NULL;
3930 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3931 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3932 EDIT_UpdateText(es, NULL, TRUE);
3937 /*********************************************************************
3939 * EM_SETWORDBREAKPROC16
3942 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3944 if (es->word_break_proc16 == wbp)
3945 return;
3947 es->word_break_proc = NULL;
3948 es->word_break_proc16 = wbp;
3949 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3950 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3951 EDIT_UpdateText(es, NULL, TRUE);
3956 /*********************************************************************
3958 * EM_UNDO / WM_UNDO
3961 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3963 INT ulength;
3964 LPWSTR utext;
3966 /* As per MSDN spec, for a single-line edit control,
3967 the return value is always TRUE */
3968 if( es->style & ES_READONLY )
3969 return !(es->style & ES_MULTILINE);
3971 ulength = strlenW(es->undo_text);
3973 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3975 strcpyW(utext, es->undo_text);
3977 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3978 es->undo_insert_count, debugstr_w(utext));
3980 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3981 EDIT_EM_EmptyUndoBuffer(es);
3982 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3983 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3984 /* send the notification after the selection start and end are set */
3985 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3986 EDIT_EM_ScrollCaret(es);
3987 HeapFree(GetProcessHeap(), 0, utext);
3989 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3990 es->undo_insert_count, debugstr_w(es->undo_text));
3991 return TRUE;
3995 /*********************************************************************
3997 * WM_CHAR
4000 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
4002 BOOL control;
4004 control = GetKeyState(VK_CONTROL) & 0x8000;
4006 switch (c) {
4007 case '\r':
4008 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
4009 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
4010 break;
4011 case '\n':
4012 if (es->style & ES_MULTILINE) {
4013 if (es->style & ES_READONLY) {
4014 EDIT_MoveHome(es, FALSE);
4015 EDIT_MoveDown_ML(es, FALSE);
4016 } else {
4017 static const WCHAR cr_lfW[] = {'\r','\n',0};
4018 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
4021 break;
4022 case '\t':
4023 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4025 static const WCHAR tabW[] = {'\t',0};
4026 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4028 break;
4029 case VK_BACK:
4030 if (!(es->style & ES_READONLY) && !control) {
4031 if (es->selection_start != es->selection_end)
4032 EDIT_WM_Clear(es);
4033 else {
4034 /* delete character left of caret */
4035 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4036 EDIT_MoveBackward(es, TRUE);
4037 EDIT_WM_Clear(es);
4040 break;
4041 case 0x03: /* ^C */
4042 if (!(es->style & ES_PASSWORD))
4043 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4044 break;
4045 case 0x16: /* ^V */
4046 if (!(es->style & ES_READONLY))
4047 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4048 break;
4049 case 0x18: /* ^X */
4050 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
4051 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4052 break;
4053 case 0x1A: /* ^Z */
4054 if (!(es->style & ES_READONLY))
4055 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
4056 break;
4058 default:
4059 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4060 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4061 break;
4063 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4064 WCHAR str[2];
4065 str[0] = c;
4066 str[1] = '\0';
4067 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4069 break;
4074 /*********************************************************************
4076 * WM_COMMAND
4079 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4081 if (code || control)
4082 return;
4084 switch (id) {
4085 case EM_UNDO:
4086 EDIT_EM_Undo(es);
4087 break;
4088 case WM_CUT:
4089 EDIT_WM_Cut(es);
4090 break;
4091 case WM_COPY:
4092 EDIT_WM_Copy(es);
4093 break;
4094 case WM_PASTE:
4095 EDIT_WM_Paste(es);
4096 break;
4097 case WM_CLEAR:
4098 EDIT_WM_Clear(es);
4099 break;
4100 case EM_SETSEL:
4101 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4102 EDIT_EM_ScrollCaret(es);
4103 break;
4104 default:
4105 ERR("unknown menu item, please report\n");
4106 break;
4111 /*********************************************************************
4113 * WM_CONTEXTMENU
4115 * Note: the resource files resource/sysres_??.rc cannot define a
4116 * single popup menu. Hence we use a (dummy) menubar
4117 * containing the single popup menu as its first item.
4119 * FIXME: the message identifiers have been chosen arbitrarily,
4120 * hence we use MF_BYPOSITION.
4121 * We might as well use the "real" values (anybody knows ?)
4122 * The menu definition is in resources/sysres_??.rc.
4123 * Once these are OK, we better use MF_BYCOMMAND here
4124 * (as we do in EDIT_WM_Command()).
4127 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4129 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4130 HMENU popup = GetSubMenu(menu, 0);
4131 UINT start = es->selection_start;
4132 UINT end = es->selection_end;
4134 ORDER_UINT(start, end);
4136 /* undo */
4137 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4138 /* cut */
4139 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4140 /* copy */
4141 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4142 /* paste */
4143 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4144 /* delete */
4145 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4146 /* select all */
4147 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4149 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4151 RECT rc;
4152 /* Windows places the menu at the edit's center in this case */
4153 GetClientRect(es->hwndSelf, &rc);
4154 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4155 x = rc.left + (rc.right - rc.left) / 2;
4156 y = rc.top + (rc.bottom - rc.top) / 2;
4159 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4160 DestroyMenu(menu);
4164 /*********************************************************************
4166 * WM_COPY
4169 static void EDIT_WM_Copy(EDITSTATE *es)
4171 INT s = min(es->selection_start, es->selection_end);
4172 INT e = max(es->selection_start, es->selection_end);
4173 HGLOBAL hdst;
4174 LPWSTR dst;
4175 DWORD len;
4177 if (e == s) return;
4179 len = e - s;
4180 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4181 dst = GlobalLock(hdst);
4182 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4183 dst[len] = 0; /* ensure 0 termination */
4184 TRACE("%s\n", debugstr_w(dst));
4185 GlobalUnlock(hdst);
4186 OpenClipboard(es->hwndSelf);
4187 EmptyClipboard();
4188 SetClipboardData(CF_UNICODETEXT, hdst);
4189 CloseClipboard();
4193 /*********************************************************************
4195 * WM_CREATE
4198 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4200 RECT clientRect;
4202 TRACE("%s\n", debugstr_w(name));
4204 * To initialize some final structure members, we call some helper
4205 * functions. However, since the EDITSTATE is not consistent (i.e.
4206 * not fully initialized), we should be very careful which
4207 * functions can be called, and in what order.
4209 EDIT_WM_SetFont(es, 0, FALSE);
4210 EDIT_EM_EmptyUndoBuffer(es);
4212 /* We need to calculate the format rect
4213 (applications may send EM_SETMARGINS before the control gets visible) */
4214 GetClientRect(es->hwndSelf, &clientRect);
4215 EDIT_SetRectNP(es, &clientRect);
4217 if (name && *name) {
4218 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4219 /* if we insert text to the editline, the text scrolls out
4220 * of the window, as the caret is placed after the insert
4221 * pos normally; thus we reset es->selection... to 0 and
4222 * update caret
4224 es->selection_start = es->selection_end = 0;
4225 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4226 * Messages are only to be sent when the USER does something to
4227 * change the contents. So I am removing this EN_CHANGE
4229 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4231 EDIT_EM_ScrollCaret(es);
4233 /* force scroll info update */
4234 EDIT_UpdateScrollInfo(es);
4235 /* The rule seems to return 1 here for success */
4236 /* Power Builder masked edit controls will crash */
4237 /* if not. */
4238 /* FIXME: is that in all cases so ? */
4239 return 1;
4243 /*********************************************************************
4245 * WM_DESTROY
4248 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4250 LINEDEF *pc, *pp;
4252 if (es->hloc32W) {
4253 while (LocalUnlock(es->hloc32W)) ;
4254 LocalFree(es->hloc32W);
4256 if (es->hloc32A) {
4257 while (LocalUnlock(es->hloc32A)) ;
4258 LocalFree(es->hloc32A);
4260 if (es->hloc16) {
4261 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4262 HANDLE16 oldDS = stack16->ds;
4264 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4265 while (LocalUnlock16(es->hloc16)) ;
4266 LocalFree16(es->hloc16);
4267 stack16->ds = oldDS;
4270 pc = es->first_line_def;
4271 while (pc)
4273 pp = pc->next;
4274 HeapFree(GetProcessHeap(), 0, pc);
4275 pc = pp;
4278 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4279 HeapFree(GetProcessHeap(), 0, es);
4281 return 0;
4285 /*********************************************************************
4287 * WM_GETTEXT
4290 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4292 if(!count) return 0;
4294 if(unicode)
4296 lstrcpynW(dst, es->text, count);
4297 return strlenW(dst);
4299 else
4301 LPSTR textA = (LPSTR)dst;
4302 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4303 textA[count - 1] = 0; /* ensure 0 termination */
4304 return strlen(textA);
4308 /*********************************************************************
4310 * WM_HSCROLL
4313 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4315 INT dx;
4316 INT fw;
4318 if (!(es->style & ES_MULTILINE))
4319 return 0;
4321 if (!(es->style & ES_AUTOHSCROLL))
4322 return 0;
4324 dx = 0;
4325 fw = es->format_rect.right - es->format_rect.left;
4326 switch (action) {
4327 case SB_LINELEFT:
4328 TRACE("SB_LINELEFT\n");
4329 if (es->x_offset)
4330 dx = -es->char_width;
4331 break;
4332 case SB_LINERIGHT:
4333 TRACE("SB_LINERIGHT\n");
4334 if (es->x_offset < es->text_width)
4335 dx = es->char_width;
4336 break;
4337 case SB_PAGELEFT:
4338 TRACE("SB_PAGELEFT\n");
4339 if (es->x_offset)
4340 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4341 break;
4342 case SB_PAGERIGHT:
4343 TRACE("SB_PAGERIGHT\n");
4344 if (es->x_offset < es->text_width)
4345 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4346 break;
4347 case SB_LEFT:
4348 TRACE("SB_LEFT\n");
4349 if (es->x_offset)
4350 dx = -es->x_offset;
4351 break;
4352 case SB_RIGHT:
4353 TRACE("SB_RIGHT\n");
4354 if (es->x_offset < es->text_width)
4355 dx = es->text_width - es->x_offset;
4356 break;
4357 case SB_THUMBTRACK:
4358 TRACE("SB_THUMBTRACK %d\n", pos);
4359 es->flags |= EF_HSCROLL_TRACK;
4360 if(es->style & WS_HSCROLL)
4361 dx = pos - es->x_offset;
4362 else
4364 INT fw, new_x;
4365 /* Sanity check */
4366 if(pos < 0 || pos > 100) return 0;
4367 /* Assume default scroll range 0-100 */
4368 fw = es->format_rect.right - es->format_rect.left;
4369 new_x = pos * (es->text_width - fw) / 100;
4370 dx = es->text_width ? (new_x - es->x_offset) : 0;
4372 break;
4373 case SB_THUMBPOSITION:
4374 TRACE("SB_THUMBPOSITION %d\n", pos);
4375 es->flags &= ~EF_HSCROLL_TRACK;
4376 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4377 dx = pos - es->x_offset;
4378 else
4380 INT fw, new_x;
4381 /* Sanity check */
4382 if(pos < 0 || pos > 100) return 0;
4383 /* Assume default scroll range 0-100 */
4384 fw = es->format_rect.right - es->format_rect.left;
4385 new_x = pos * (es->text_width - fw) / 100;
4386 dx = es->text_width ? (new_x - es->x_offset) : 0;
4388 if (!dx) {
4389 /* force scroll info update */
4390 EDIT_UpdateScrollInfo(es);
4391 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4393 break;
4394 case SB_ENDSCROLL:
4395 TRACE("SB_ENDSCROLL\n");
4396 break;
4398 * FIXME : the next two are undocumented !
4399 * Are we doing the right thing ?
4400 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4401 * although it's also a regular control message.
4403 case EM_GETTHUMB: /* this one is used by NT notepad */
4404 case EM_GETTHUMB16:
4406 LRESULT ret;
4407 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4408 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4409 else
4411 /* Assume default scroll range 0-100 */
4412 INT fw = es->format_rect.right - es->format_rect.left;
4413 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4415 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4416 return ret;
4418 case EM_LINESCROLL16:
4419 TRACE("EM_LINESCROLL16\n");
4420 dx = pos;
4421 break;
4423 default:
4424 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4425 action, action);
4426 return 0;
4428 if (dx)
4430 INT fw = es->format_rect.right - es->format_rect.left;
4431 /* check if we are going to move too far */
4432 if(es->x_offset + dx + fw > es->text_width)
4433 dx = es->text_width - fw - es->x_offset;
4434 if(dx)
4435 EDIT_EM_LineScroll_internal(es, dx, 0);
4437 return 0;
4441 /*********************************************************************
4443 * EDIT_CheckCombo
4446 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4448 HWND hLBox = es->hwndListBox;
4449 HWND hCombo;
4450 BOOL bDropped;
4451 int nEUI;
4453 if (!hLBox)
4454 return FALSE;
4456 hCombo = GetParent(es->hwndSelf);
4457 bDropped = TRUE;
4458 nEUI = 0;
4460 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4462 if (key == VK_UP || key == VK_DOWN)
4464 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4465 nEUI = 1;
4467 if (msg == WM_KEYDOWN || nEUI)
4468 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4471 switch (msg)
4473 case WM_KEYDOWN:
4474 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4476 /* make sure ComboLBox pops up */
4477 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4478 key = VK_F4;
4479 nEUI = 2;
4482 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4483 break;
4485 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4486 if (nEUI)
4487 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4488 else
4489 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4490 break;
4493 if(nEUI == 2)
4494 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4496 return TRUE;
4500 /*********************************************************************
4502 * WM_KEYDOWN
4504 * Handling of special keys that don't produce a WM_CHAR
4505 * (i.e. non-printable keys) & Backspace & Delete
4508 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4510 BOOL shift;
4511 BOOL control;
4513 if (GetKeyState(VK_MENU) & 0x8000)
4514 return 0;
4516 shift = GetKeyState(VK_SHIFT) & 0x8000;
4517 control = GetKeyState(VK_CONTROL) & 0x8000;
4519 switch (key) {
4520 case VK_F4:
4521 case VK_UP:
4522 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4523 break;
4525 /* fall through */
4526 case VK_LEFT:
4527 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4528 EDIT_MoveUp_ML(es, shift);
4529 else
4530 if (control)
4531 EDIT_MoveWordBackward(es, shift);
4532 else
4533 EDIT_MoveBackward(es, shift);
4534 break;
4535 case VK_DOWN:
4536 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4537 break;
4538 /* fall through */
4539 case VK_RIGHT:
4540 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4541 EDIT_MoveDown_ML(es, shift);
4542 else if (control)
4543 EDIT_MoveWordForward(es, shift);
4544 else
4545 EDIT_MoveForward(es, shift);
4546 break;
4547 case VK_HOME:
4548 EDIT_MoveHome(es, shift);
4549 break;
4550 case VK_END:
4551 EDIT_MoveEnd(es, shift);
4552 break;
4553 case VK_PRIOR:
4554 if (es->style & ES_MULTILINE)
4555 EDIT_MovePageUp_ML(es, shift);
4556 else
4557 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4558 break;
4559 case VK_NEXT:
4560 if (es->style & ES_MULTILINE)
4561 EDIT_MovePageDown_ML(es, shift);
4562 else
4563 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4564 break;
4565 case VK_DELETE:
4566 if (!(es->style & ES_READONLY) && !(shift && control)) {
4567 if (es->selection_start != es->selection_end) {
4568 if (shift)
4569 EDIT_WM_Cut(es);
4570 else
4571 EDIT_WM_Clear(es);
4572 } else {
4573 if (shift) {
4574 /* delete character left of caret */
4575 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4576 EDIT_MoveBackward(es, TRUE);
4577 EDIT_WM_Clear(es);
4578 } else if (control) {
4579 /* delete to end of line */
4580 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4581 EDIT_MoveEnd(es, TRUE);
4582 EDIT_WM_Clear(es);
4583 } else {
4584 /* delete character right of caret */
4585 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4586 EDIT_MoveForward(es, TRUE);
4587 EDIT_WM_Clear(es);
4591 break;
4592 case VK_INSERT:
4593 if (shift) {
4594 if (!(es->style & ES_READONLY))
4595 EDIT_WM_Paste(es);
4596 } else if (control)
4597 EDIT_WM_Copy(es);
4598 break;
4599 case VK_RETURN:
4600 /* If the edit doesn't want the return send a message to the default object */
4601 if(!(es->style & ES_WANTRETURN))
4603 HWND hwndParent = GetParent(es->hwndSelf);
4604 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4605 if (HIWORD(dw) == DC_HASDEFID)
4607 SendMessageW( hwndParent, WM_COMMAND,
4608 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4609 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4612 break;
4614 return 0;
4618 /*********************************************************************
4620 * WM_KILLFOCUS
4623 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4625 es->flags &= ~EF_FOCUSED;
4626 DestroyCaret();
4627 if(!(es->style & ES_NOHIDESEL))
4628 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4629 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4630 return 0;
4634 /*********************************************************************
4636 * WM_LBUTTONDBLCLK
4638 * The caret position has been set on the WM_LBUTTONDOWN message
4641 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4643 INT s;
4644 INT e = es->selection_end;
4645 INT l;
4646 INT li;
4647 INT ll;
4649 es->bCaptureState = TRUE;
4650 SetCapture(es->hwndSelf);
4652 l = EDIT_EM_LineFromChar(es, e);
4653 li = EDIT_EM_LineIndex(es, l);
4654 ll = EDIT_EM_LineLength(es, e);
4655 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4656 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4657 EDIT_EM_SetSel(es, s, e, FALSE);
4658 EDIT_EM_ScrollCaret(es);
4659 es->region_posx = es->region_posy = 0;
4660 SetTimer(es->hwndSelf, 0, 100, NULL);
4661 return 0;
4665 /*********************************************************************
4667 * WM_LBUTTONDOWN
4670 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4672 INT e;
4673 BOOL after_wrap;
4675 es->bCaptureState = TRUE;
4676 SetCapture(es->hwndSelf);
4677 EDIT_ConfinePoint(es, &x, &y);
4678 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4679 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4680 EDIT_EM_ScrollCaret(es);
4681 es->region_posx = es->region_posy = 0;
4682 SetTimer(es->hwndSelf, 0, 100, NULL);
4684 if (!(es->flags & EF_FOCUSED))
4685 SetFocus(es->hwndSelf);
4687 return 0;
4691 /*********************************************************************
4693 * WM_LBUTTONUP
4696 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4698 if (es->bCaptureState) {
4699 KillTimer(es->hwndSelf, 0);
4700 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4702 es->bCaptureState = FALSE;
4703 return 0;
4707 /*********************************************************************
4709 * WM_MBUTTONDOWN
4712 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4714 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4715 return 0;
4719 /*********************************************************************
4721 * WM_MOUSEMOVE
4724 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4726 INT e;
4727 BOOL after_wrap;
4728 INT prex, prey;
4730 /* If the mouse has been captured by process other than the edit control itself,
4731 * the windows edit controls will not select the strings with mouse move.
4733 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4734 return 0;
4737 * FIXME: gotta do some scrolling if outside client
4738 * area. Maybe reset the timer ?
4740 prex = x; prey = y;
4741 EDIT_ConfinePoint(es, &x, &y);
4742 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4743 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4744 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4745 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4746 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4747 return 0;
4751 /*********************************************************************
4753 * WM_NCCREATE
4755 * See also EDIT_WM_StyleChanged
4757 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4759 EDITSTATE *es;
4760 UINT alloc_size;
4762 TRACE("Creating %s edit control, style = %08x\n",
4763 unicode ? "Unicode" : "ANSI", lpcs->style);
4765 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4766 return FALSE;
4767 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4770 * Note: since the EDITSTATE has not been fully initialized yet,
4771 * we can't use any API calls that may send
4772 * WM_XXX messages before WM_NCCREATE is completed.
4775 es->is_unicode = unicode;
4776 es->style = lpcs->style;
4778 es->bEnableState = !(es->style & WS_DISABLED);
4780 es->hwndSelf = hwnd;
4781 /* Save parent, which will be notified by EN_* messages */
4782 es->hwndParent = lpcs->hwndParent;
4784 if (es->style & ES_COMBO)
4785 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4787 /* Number overrides lowercase overrides uppercase (at least it
4788 * does in Win95). However I'll bet that ES_NUMBER would be
4789 * invalid under Win 3.1.
4791 if (es->style & ES_NUMBER) {
4792 ; /* do not override the ES_NUMBER */
4793 } else if (es->style & ES_LOWERCASE) {
4794 es->style &= ~ES_UPPERCASE;
4796 if (es->style & ES_MULTILINE) {
4797 es->buffer_limit = BUFLIMIT_INITIAL;
4798 if (es->style & WS_VSCROLL)
4799 es->style |= ES_AUTOVSCROLL;
4800 if (es->style & WS_HSCROLL)
4801 es->style |= ES_AUTOHSCROLL;
4802 es->style &= ~ES_PASSWORD;
4803 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4804 /* Confirmed - RIGHT overrides CENTER */
4805 if (es->style & ES_RIGHT)
4806 es->style &= ~ES_CENTER;
4807 es->style &= ~WS_HSCROLL;
4808 es->style &= ~ES_AUTOHSCROLL;
4810 } else {
4811 es->buffer_limit = BUFLIMIT_INITIAL;
4812 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4813 es->style &= ~ES_CENTER;
4814 es->style &= ~WS_HSCROLL;
4815 es->style &= ~WS_VSCROLL;
4816 if (es->style & ES_PASSWORD)
4817 es->password_char = '*';
4820 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4821 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4822 return FALSE;
4823 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4825 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4826 return FALSE;
4827 es->undo_buffer_size = es->buffer_size;
4829 if (es->style & ES_MULTILINE)
4830 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4831 return FALSE;
4832 es->line_count = 1;
4835 * In Win95 look and feel, the WS_BORDER style is replaced by the
4836 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4837 * control a nonclient area so we don't need to draw the border.
4838 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4839 * a nonclient area and we should handle painting the border ourselves.
4841 * When making modifications please ensure that the code still works
4842 * for edit controls created directly with style 0x50800000, exStyle 0
4843 * (which should have a single pixel border)
4845 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4846 es->style &= ~WS_BORDER;
4847 else if (es->style & WS_BORDER)
4848 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4850 return TRUE;
4853 /*********************************************************************
4855 * WM_PAINT
4858 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4860 PAINTSTRUCT ps;
4861 INT i;
4862 HDC dc;
4863 HFONT old_font = 0;
4864 RECT rc;
4865 RECT rcClient;
4866 RECT rcLine;
4867 RECT rcRgn;
4868 HBRUSH brush;
4869 HBRUSH old_brush;
4870 INT bw, bh;
4871 BOOL rev = es->bEnableState &&
4872 ((es->flags & EF_FOCUSED) ||
4873 (es->style & ES_NOHIDESEL));
4874 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4876 GetClientRect(es->hwndSelf, &rcClient);
4878 /* get the background brush */
4879 brush = EDIT_NotifyCtlColor(es, dc);
4881 /* paint the border and the background */
4882 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4884 if(es->style & WS_BORDER) {
4885 bw = GetSystemMetrics(SM_CXBORDER);
4886 bh = GetSystemMetrics(SM_CYBORDER);
4887 rc = rcClient;
4888 if(es->style & ES_MULTILINE) {
4889 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4890 if(es->style & WS_VSCROLL) rc.right+=bw;
4893 /* Draw the frame. Same code as in nonclient.c */
4894 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4895 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4896 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4897 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4898 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4899 SelectObject(dc, old_brush);
4901 /* Keep the border clean */
4902 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4903 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4906 GetClipBox(dc, &rc);
4907 FillRect(dc, &rc, brush);
4909 IntersectClipRect(dc, es->format_rect.left,
4910 es->format_rect.top,
4911 es->format_rect.right,
4912 es->format_rect.bottom);
4913 if (es->style & ES_MULTILINE) {
4914 rc = rcClient;
4915 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4917 if (es->font)
4918 old_font = SelectObject(dc, es->font);
4920 if (!es->bEnableState)
4921 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4922 GetClipBox(dc, &rcRgn);
4923 if (es->style & ES_MULTILINE) {
4924 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4925 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4926 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4927 if (IntersectRect(&rc, &rcRgn, &rcLine))
4928 EDIT_PaintLine(es, dc, i, rev);
4930 } else {
4931 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4932 if (IntersectRect(&rc, &rcRgn, &rcLine))
4933 EDIT_PaintLine(es, dc, 0, rev);
4935 if (es->font)
4936 SelectObject(dc, old_font);
4938 if (!hdc)
4939 EndPaint(es->hwndSelf, &ps);
4943 /*********************************************************************
4945 * WM_PASTE
4948 static void EDIT_WM_Paste(EDITSTATE *es)
4950 HGLOBAL hsrc;
4951 LPWSTR src;
4953 /* Protect read-only edit control from modification */
4954 if(es->style & ES_READONLY)
4955 return;
4957 OpenClipboard(es->hwndSelf);
4958 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4959 src = (LPWSTR)GlobalLock(hsrc);
4960 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4961 GlobalUnlock(hsrc);
4963 else if (es->style & ES_PASSWORD) {
4964 /* clear selected text in password edit box even with empty clipboard */
4965 const WCHAR empty_strW[] = { 0 };
4966 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
4968 CloseClipboard();
4972 /*********************************************************************
4974 * WM_SETFOCUS
4977 static void EDIT_WM_SetFocus(EDITSTATE *es)
4979 es->flags |= EF_FOCUSED;
4981 if (!(es->style & ES_NOHIDESEL))
4982 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4984 /* single line edit updates itself */
4985 if (!(es->style & ES_MULTILINE))
4987 HDC hdc = GetDC(es->hwndSelf);
4988 EDIT_WM_Paint(es, hdc);
4989 ReleaseDC(es->hwndSelf, hdc);
4992 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4993 EDIT_SetCaretPos(es, es->selection_end,
4994 es->flags & EF_AFTER_WRAP);
4995 ShowCaret(es->hwndSelf);
4996 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
5000 /*********************************************************************
5002 * WM_SETFONT
5004 * With Win95 look the margins are set to default font value unless
5005 * the system font (font == 0) is being set, in which case they are left
5006 * unchanged.
5009 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
5011 TEXTMETRICW tm;
5012 HDC dc;
5013 HFONT old_font = 0;
5014 RECT clientRect;
5016 es->font = font;
5017 dc = GetDC(es->hwndSelf);
5018 if (font)
5019 old_font = SelectObject(dc, font);
5020 GetTextMetricsW(dc, &tm);
5021 es->line_height = tm.tmHeight;
5022 es->char_width = tm.tmAveCharWidth;
5023 if (font)
5024 SelectObject(dc, old_font);
5025 ReleaseDC(es->hwndSelf, dc);
5027 /* Reset the format rect and the margins */
5028 GetClientRect(es->hwndSelf, &clientRect);
5029 EDIT_SetRectNP(es, &clientRect);
5030 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5031 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5033 if (es->style & ES_MULTILINE)
5034 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5035 else
5036 EDIT_CalcLineWidth_SL(es);
5038 if (redraw)
5039 EDIT_UpdateText(es, NULL, TRUE);
5040 if (es->flags & EF_FOCUSED) {
5041 DestroyCaret();
5042 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5043 EDIT_SetCaretPos(es, es->selection_end,
5044 es->flags & EF_AFTER_WRAP);
5045 ShowCaret(es->hwndSelf);
5050 /*********************************************************************
5052 * WM_SETTEXT
5054 * NOTES
5055 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5056 * The modified flag is reset. No notifications are sent.
5058 * For single-line controls, reception of WM_SETTEXT triggers:
5059 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5062 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5064 LPWSTR textW = NULL;
5065 if (!unicode && text)
5067 LPCSTR textA = (LPCSTR)text;
5068 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5069 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5070 if (textW)
5071 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5072 text = textW;
5075 if (es->flags & EF_UPDATE)
5076 /* fixed this bug once; complain if we see it about to happen again. */
5077 ERR("SetSel may generate UPDATE message whose handler may reset "
5078 "selection.\n");
5080 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5081 if (text)
5083 TRACE("%s\n", debugstr_w(text));
5084 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5085 if(!unicode)
5086 HeapFree(GetProcessHeap(), 0, textW);
5088 else
5090 static const WCHAR empty_stringW[] = {0};
5091 TRACE("<NULL>\n");
5092 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5094 es->x_offset = 0;
5095 es->flags &= ~EF_MODIFIED;
5096 EDIT_EM_SetSel(es, 0, 0, FALSE);
5098 /* Send the notification after the selection start and end have been set
5099 * edit control doesn't send notification on WM_SETTEXT
5100 * if it is multiline, or it is part of combobox
5102 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5104 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5105 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5107 EDIT_EM_ScrollCaret(es);
5108 EDIT_UpdateScrollInfo(es);
5112 /*********************************************************************
5114 * WM_SIZE
5117 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5119 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5120 RECT rc;
5121 TRACE("width = %d, height = %d\n", width, height);
5122 SetRect(&rc, 0, 0, width, height);
5123 EDIT_SetRectNP(es, &rc);
5124 EDIT_UpdateText(es, NULL, TRUE);
5129 /*********************************************************************
5131 * WM_STYLECHANGED
5133 * This message is sent by SetWindowLong on having changed either the Style
5134 * or the extended style.
5136 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5138 * See also EDIT_WM_NCCreate
5140 * It appears that the Windows version of the edit control allows the style
5141 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5142 * style variable which will generally be different. In this function we
5143 * update the internal style based on what changed in the externally visible
5144 * style.
5146 * Much of this content as based upon the MSDN, especially:
5147 * Platform SDK Documentation -> User Interface Services ->
5148 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5149 * Edit Control Styles
5151 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5153 if (GWL_STYLE == which) {
5154 DWORD style_change_mask;
5155 DWORD new_style;
5156 /* Only a subset of changes can be applied after the control
5157 * has been created.
5159 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5160 ES_NUMBER;
5161 if (es->style & ES_MULTILINE)
5162 style_change_mask |= ES_WANTRETURN;
5164 new_style = style->styleNew & style_change_mask;
5166 /* Number overrides lowercase overrides uppercase (at least it
5167 * does in Win95). However I'll bet that ES_NUMBER would be
5168 * invalid under Win 3.1.
5170 if (new_style & ES_NUMBER) {
5171 ; /* do not override the ES_NUMBER */
5172 } else if (new_style & ES_LOWERCASE) {
5173 new_style &= ~ES_UPPERCASE;
5176 es->style = (es->style & ~style_change_mask) | new_style;
5177 } else if (GWL_EXSTYLE == which) {
5178 ; /* FIXME - what is needed here */
5179 } else {
5180 WARN ("Invalid style change %ld\n",which);
5183 return 0;
5186 /*********************************************************************
5188 * WM_SYSKEYDOWN
5191 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5193 if ((key == VK_BACK) && (key_data & 0x2000)) {
5194 if (EDIT_EM_CanUndo(es))
5195 EDIT_EM_Undo(es);
5196 return 0;
5197 } else if (key == VK_UP || key == VK_DOWN) {
5198 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5199 return 0;
5201 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5205 /*********************************************************************
5207 * WM_TIMER
5210 static void EDIT_WM_Timer(EDITSTATE *es)
5212 if (es->region_posx < 0) {
5213 EDIT_MoveBackward(es, TRUE);
5214 } else if (es->region_posx > 0) {
5215 EDIT_MoveForward(es, TRUE);
5218 * FIXME: gotta do some vertical scrolling here, like
5219 * EDIT_EM_LineScroll(hwnd, 0, 1);
5223 /*********************************************************************
5225 * WM_VSCROLL
5228 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5230 INT dy;
5232 if (!(es->style & ES_MULTILINE))
5233 return 0;
5235 if (!(es->style & ES_AUTOVSCROLL))
5236 return 0;
5238 dy = 0;
5239 switch (action) {
5240 case SB_LINEUP:
5241 case SB_LINEDOWN:
5242 case SB_PAGEUP:
5243 case SB_PAGEDOWN:
5244 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5245 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5246 (action == SB_PAGEUP ? "SB_PAGEUP" :
5247 "SB_PAGEDOWN"))));
5248 EDIT_EM_Scroll(es, action);
5249 return 0;
5250 case SB_TOP:
5251 TRACE("SB_TOP\n");
5252 dy = -es->y_offset;
5253 break;
5254 case SB_BOTTOM:
5255 TRACE("SB_BOTTOM\n");
5256 dy = es->line_count - 1 - es->y_offset;
5257 break;
5258 case SB_THUMBTRACK:
5259 TRACE("SB_THUMBTRACK %d\n", pos);
5260 es->flags |= EF_VSCROLL_TRACK;
5261 if(es->style & WS_VSCROLL)
5262 dy = pos - es->y_offset;
5263 else
5265 /* Assume default scroll range 0-100 */
5266 INT vlc, new_y;
5267 /* Sanity check */
5268 if(pos < 0 || pos > 100) return 0;
5269 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5270 new_y = pos * (es->line_count - vlc) / 100;
5271 dy = es->line_count ? (new_y - es->y_offset) : 0;
5272 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5273 es->line_count, es->y_offset, pos, dy);
5275 break;
5276 case SB_THUMBPOSITION:
5277 TRACE("SB_THUMBPOSITION %d\n", pos);
5278 es->flags &= ~EF_VSCROLL_TRACK;
5279 if(es->style & WS_VSCROLL)
5280 dy = pos - es->y_offset;
5281 else
5283 /* Assume default scroll range 0-100 */
5284 INT vlc, new_y;
5285 /* Sanity check */
5286 if(pos < 0 || pos > 100) return 0;
5287 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5288 new_y = pos * (es->line_count - vlc) / 100;
5289 dy = es->line_count ? (new_y - es->y_offset) : 0;
5290 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5291 es->line_count, es->y_offset, pos, dy);
5293 if (!dy)
5295 /* force scroll info update */
5296 EDIT_UpdateScrollInfo(es);
5297 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5299 break;
5300 case SB_ENDSCROLL:
5301 TRACE("SB_ENDSCROLL\n");
5302 break;
5304 * FIXME : the next two are undocumented !
5305 * Are we doing the right thing ?
5306 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5307 * although it's also a regular control message.
5309 case EM_GETTHUMB: /* this one is used by NT notepad */
5310 case EM_GETTHUMB16:
5312 LRESULT ret;
5313 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5314 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5315 else
5317 /* Assume default scroll range 0-100 */
5318 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5319 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5321 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5322 return ret;
5324 case EM_LINESCROLL16:
5325 TRACE("EM_LINESCROLL16 %d\n", pos);
5326 dy = pos;
5327 break;
5329 default:
5330 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5331 action, action);
5332 return 0;
5334 if (dy)
5335 EDIT_EM_LineScroll(es, 0, dy);
5336 return 0;
5339 /*********************************************************************
5341 * EDIT_UpdateText
5344 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5346 if (es->flags & EF_UPDATE) {
5347 es->flags &= ~EF_UPDATE;
5348 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5350 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5354 /*********************************************************************
5356 * EDIT_UpdateText
5359 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
5361 if (es->flags & EF_UPDATE) {
5362 es->flags &= ~EF_UPDATE;
5363 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5365 InvalidateRect(es->hwndSelf, rc, bErase);
5368 /********************************************************************
5370 * The Following code is to handle inline editing from IMEs
5373 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5375 LONG buflen;
5376 LPWSTR lpCompStr = NULL;
5377 HIMC hIMC;
5378 LPSTR lpCompStrAttr = NULL;
5379 DWORD dwBufLenAttr;
5381 if (!(hIMC = ImmGetContext(hwnd)))
5382 return;
5384 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5386 if (buflen < 0)
5388 ImmReleaseContext(hwnd, hIMC);
5389 return;
5392 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
5393 if (!lpCompStr)
5395 ERR("Unable to allocate IME CompositionString\n");
5396 ImmReleaseContext(hwnd,hIMC);
5397 return;
5400 if (buflen)
5401 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
5402 lpCompStr[buflen/sizeof(WCHAR)] = 0;
5404 if (CompFlag & GCS_COMPATTR)
5407 * We do not use the attributes yet. it would tell us what characters
5408 * are in transition and which are converted or decided upon
5410 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5411 if (dwBufLenAttr)
5413 dwBufLenAttr ++;
5414 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5415 if (!lpCompStrAttr)
5417 ERR("Unable to allocate IME Attribute String\n");
5418 HeapFree(GetProcessHeap(),0,lpCompStr);
5419 ImmReleaseContext(hwnd,hIMC);
5420 return;
5422 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5423 dwBufLenAttr);
5424 lpCompStrAttr[dwBufLenAttr] = 0;
5426 else
5427 lpCompStrAttr = NULL;
5430 /* check for change in composition start */
5431 if (es->selection_end < es->composition_start)
5432 es->composition_start = es->selection_end;
5434 /* replace existing selection string */
5435 es->selection_start = es->composition_start;
5437 if (es->composition_len > 0)
5438 es->selection_end = es->composition_start + es->composition_len;
5439 else
5440 es->selection_end = es->selection_start;
5442 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5443 es->composition_len = abs(es->composition_start - es->selection_end);
5445 es->selection_start = es->composition_start;
5446 es->selection_end = es->selection_start + es->composition_len;
5448 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5449 HeapFree(GetProcessHeap(),0,lpCompStr);
5450 ImmReleaseContext(hwnd,hIMC);
5453 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5455 LONG buflen;
5456 LPWSTR lpResultStr;
5457 HIMC hIMC;
5459 if ( !(hIMC = ImmGetContext(hwnd)))
5460 return;
5462 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5463 if (buflen <= 0)
5465 ImmReleaseContext(hwnd, hIMC);
5466 return;
5469 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
5470 if (!lpResultStr)
5472 ERR("Unable to alloc buffer for IME string\n");
5473 ImmReleaseContext(hwnd, hIMC);
5474 return;
5477 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
5478 lpResultStr[buflen/sizeof(WCHAR)] = 0;
5480 /* check for change in composition start */
5481 if (es->selection_end < es->composition_start)
5482 es->composition_start = es->selection_end;
5484 es->selection_start = es->composition_start;
5485 es->selection_end = es->composition_start + es->composition_len;
5486 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5487 es->composition_start = es->selection_end;
5488 es->composition_len = 0;
5490 HeapFree(GetProcessHeap(),0,lpResultStr);
5491 ImmReleaseContext(hwnd, hIMC);
5494 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5496 if (CompFlag & GCS_RESULTSTR)
5497 EDIT_GetResultStr(hwnd,es);
5498 if (CompFlag & GCS_COMPSTR)
5499 EDIT_GetCompositionStr(hwnd, CompFlag, es);