user32: Cache the text buffer length to avoid excessive calls to strlenW.
[wine/dibdrv.git] / dlls / user32 / edit.c
blob658aaf80e198acfd42c5cc58b4ca6a247f25e2c7
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_MULTI 65534 /* maximum buffer size (not including '\0')
69 FIXME: BTW, new specs say 65535 (do you dare ???) */
70 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
71 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
72 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
73 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
76 * extra flags for EDITSTATE.flags field
78 #define EF_MODIFIED 0x0001 /* text has been modified */
79 #define EF_FOCUSED 0x0002 /* we have input focus */
80 #define EF_UPDATE 0x0004 /* notify parent of changed state */
81 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
82 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
83 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
84 wrapped line, instead of in front of the next character */
85 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
86 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
87 the text buffer if this is clear. */
88 typedef enum
90 END_0 = 0, /* line ends with terminating '\0' character */
91 END_WRAP, /* line is wrapped */
92 END_HARD, /* line ends with a hard return '\r\n' */
93 END_SOFT, /* line ends with a soft return '\r\r\n' */
94 END_RICH /* line ends with a single '\n' */
95 } LINE_END;
97 typedef struct tagLINEDEF {
98 INT length; /* bruto length of a line in bytes */
99 INT net_length; /* netto length of a line in visible characters */
100 LINE_END ending;
101 INT width; /* width of the line in pixels */
102 INT index; /* line index into the buffer */
103 struct tagLINEDEF *next;
104 } LINEDEF;
106 typedef struct
108 BOOL is_unicode; /* how the control was created */
109 LPWSTR text; /* the actual contents of the control */
110 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
111 UINT buffer_size; /* the size of the buffer in characters */
112 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
113 HFONT font; /* NULL means standard system font */
114 INT x_offset; /* scroll offset for multi lines this is in pixels
115 for single lines it's in characters */
116 INT line_height; /* height of a screen line in pixels */
117 INT char_width; /* average character width in pixels */
118 DWORD style; /* sane version of wnd->dwStyle */
119 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
120 INT undo_insert_count; /* number of characters inserted in sequence */
121 UINT undo_position; /* character index of the insertion and deletion */
122 LPWSTR undo_text; /* deleted text */
123 UINT undo_buffer_size; /* size of the deleted text buffer */
124 INT selection_start; /* == selection_end if no selection */
125 INT selection_end; /* == current caret position */
126 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
127 INT left_margin; /* in pixels */
128 INT right_margin; /* in pixels */
129 RECT format_rect;
130 INT text_width; /* width of the widest line in pixels for multi line controls
131 and just line width for single line controls */
132 INT region_posx; /* Position of cursor relative to region: */
133 INT region_posy; /* -1: to left, 0: within, 1: to right */
134 EDITWORDBREAKPROC16 word_break_proc16;
135 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
136 INT line_count; /* number of lines */
137 INT y_offset; /* scroll offset in number of lines */
138 BOOL bCaptureState; /* flag indicating whether mouse was captured */
139 BOOL bEnableState; /* flag keeping the enable state */
140 HWND hwndSelf; /* the our window handle */
141 HWND hwndParent; /* Handle of parent for sending EN_* messages.
142 Even if parent will change, EN_* messages
143 should be sent to the first parent. */
144 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
146 * only for multi line controls
148 INT lock_count; /* amount of re-entries in the EditWndProc */
149 INT tabs_count;
150 LPINT tabs;
151 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
152 HLOCAL hloc32W; /* our unicode local memory block */
153 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
154 or EM_SETHANDLE16 */
155 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
156 or EM_SETHANDLE */
158 * IME Data
160 UINT composition_len; /* length of composition, 0 == no composition */
161 int composition_start; /* the character position for the composition */
162 } EDITSTATE;
165 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
166 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
168 /* used for disabled or read-only edit control */
169 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
170 do \
171 { /* Notify parent which has created this edit control */ \
172 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
173 SendMessageW(es->hwndParent, WM_COMMAND, \
174 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
175 (LPARAM)(es->hwndSelf)); \
176 } while(0)
178 /*********************************************************************
180 * Declarations
185 * These functions have trivial implementations
186 * We still like to call them internally
187 * "static inline" makes them more like macro's
189 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
190 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
191 static inline void EDIT_WM_Clear(EDITSTATE *es);
192 static inline void EDIT_WM_Cut(EDITSTATE *es);
195 * Helper functions only valid for one type of control
197 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
198 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
199 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
200 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
201 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
202 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
203 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
205 * Helper functions valid for both single line _and_ multi line controls
207 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
208 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
209 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
210 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
211 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
212 static void EDIT_LockBuffer(EDITSTATE *es);
213 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
214 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
215 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
216 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
217 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
218 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
219 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
220 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
221 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
222 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
223 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
224 static void EDIT_AdjustFormatRect(EDITSTATE *es);
225 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
226 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
227 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
228 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
230 * EM_XXX message handlers
232 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
233 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
234 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
235 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
236 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
237 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
238 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
239 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
240 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
241 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
242 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
243 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
244 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
245 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
246 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
247 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
248 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
249 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
250 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
251 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, WORD left, WORD right, BOOL repaint);
252 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
253 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
254 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
255 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
256 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
257 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
258 static BOOL EDIT_EM_Undo(EDITSTATE *es);
260 * WM_XXX message handlers
262 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
263 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
264 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
265 static void EDIT_WM_Copy(EDITSTATE *es);
266 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
267 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
268 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
269 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
270 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
271 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
272 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
273 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
274 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
275 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
276 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
277 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
278 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
279 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
280 static void EDIT_WM_Paste(EDITSTATE *es);
281 static void EDIT_WM_SetFocus(EDITSTATE *es);
282 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
283 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
284 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
285 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
286 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
287 static void EDIT_WM_Timer(EDITSTATE *es);
288 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
289 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
290 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
291 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
293 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
294 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
296 /*********************************************************************
297 * edit class descriptor
299 const struct builtin_class_descr EDIT_builtin_class =
301 "Edit", /* name */
302 CS_DBLCLKS | CS_PARENTDC, /* style */
303 EditWndProcA, /* procA */
304 EditWndProcW, /* procW */
305 sizeof(EDITSTATE *), /* extra */
306 IDC_IBEAM, /* cursor */
307 0 /* brush */
311 /*********************************************************************
313 * EM_CANUNDO
316 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
318 return (es->undo_insert_count || strlenW(es->undo_text));
322 /*********************************************************************
324 * EM_EMPTYUNDOBUFFER
327 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
329 es->undo_insert_count = 0;
330 *es->undo_text = '\0';
334 /*********************************************************************
336 * WM_CLEAR
339 static inline void EDIT_WM_Clear(EDITSTATE *es)
341 static const WCHAR empty_stringW[] = {0};
343 /* Protect read-only edit control from modification */
344 if(es->style & ES_READONLY)
345 return;
347 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
351 /*********************************************************************
353 * WM_CUT
356 static inline void EDIT_WM_Cut(EDITSTATE *es)
358 EDIT_WM_Copy(es);
359 EDIT_WM_Clear(es);
363 /**********************************************************************
364 * get_app_version
366 * Returns the window version in case Wine emulates a later version
367 * of windows than the application expects.
369 * In a number of cases when windows runs an application that was
370 * designed for an earlier windows version, windows reverts
371 * to "old" behaviour of that earlier version.
373 * An example is a disabled edit control that needs to be painted.
374 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
375 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
376 * applications with an expected version 0f 4.0 or higher.
379 static DWORD get_app_version(void)
381 static DWORD version;
382 if (!version)
384 DWORD dwEmulatedVersion;
385 OSVERSIONINFOW info;
386 DWORD dwProcVersion = GetProcessVersion(0);
388 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
389 GetVersionExW( &info );
390 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
391 /* FIXME: this may not be 100% correct; see discussion on the
392 * wine developer list in Nov 1999 */
393 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
395 return version;
398 static inline UINT get_text_length(EDITSTATE *es)
400 if(es->text_length == (UINT)-1)
401 es->text_length = strlenW(es->text);
402 return es->text_length;
405 static inline void text_buffer_changed(EDITSTATE *es)
407 es->text_length = (UINT)-1;
410 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
412 HBRUSH hbrush;
413 UINT msg;
415 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
416 msg = WM_CTLCOLORSTATIC;
417 else
418 msg = WM_CTLCOLOREDIT;
420 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
421 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
422 if (!hbrush)
423 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
424 return hbrush;
427 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
429 if(unicode)
430 return DefWindowProcW(hwnd, msg, wParam, lParam);
431 else
432 return DefWindowProcA(hwnd, msg, wParam, lParam);
435 /*********************************************************************
437 * EditWndProc_common
439 * The messages are in the order of the actual integer values
440 * (which can be found in include/windows.h)
441 * Wherever possible the 16 bit versions are converted to
442 * the 32 bit ones, so that we can 'fall through' to the
443 * helper functions. These are mostly 32 bit (with a few
444 * exceptions, clearly indicated by a '16' extension to their
445 * names).
448 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
449 WPARAM wParam, LPARAM lParam, BOOL unicode )
451 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
452 LRESULT result = 0;
454 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
456 if (!es && msg != WM_NCCREATE)
457 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
459 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
461 switch (msg) {
462 case EM_GETSEL16:
463 wParam = 0;
464 lParam = 0;
465 /* fall through */
466 case EM_GETSEL:
467 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
468 break;
470 case EM_SETSEL16:
471 if ((short)LOWORD(lParam) == -1)
472 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
473 else
474 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
475 if (!wParam)
476 EDIT_EM_ScrollCaret(es);
477 result = 1;
478 break;
479 case EM_SETSEL:
480 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
481 EDIT_EM_ScrollCaret(es);
482 result = 1;
483 break;
485 case EM_GETRECT16:
486 if (lParam)
488 RECT16 *r16 = MapSL(lParam);
489 r16->left = es->format_rect.left;
490 r16->top = es->format_rect.top;
491 r16->right = es->format_rect.right;
492 r16->bottom = es->format_rect.bottom;
494 break;
495 case EM_GETRECT:
496 if (lParam)
497 CopyRect((LPRECT)lParam, &es->format_rect);
498 break;
500 case EM_SETRECT16:
501 if ((es->style & ES_MULTILINE) && lParam) {
502 RECT rc;
503 RECT16 *r16 = MapSL(lParam);
504 rc.left = r16->left;
505 rc.top = r16->top;
506 rc.right = r16->right;
507 rc.bottom = r16->bottom;
508 EDIT_SetRectNP(es, &rc);
509 EDIT_UpdateText(es, NULL, TRUE);
511 break;
512 case EM_SETRECT:
513 if ((es->style & ES_MULTILINE) && lParam) {
514 EDIT_SetRectNP(es, (LPRECT)lParam);
515 EDIT_UpdateText(es, NULL, TRUE);
517 break;
519 case EM_SETRECTNP16:
520 if ((es->style & ES_MULTILINE) && lParam) {
521 RECT rc;
522 RECT16 *r16 = MapSL(lParam);
523 rc.left = r16->left;
524 rc.top = r16->top;
525 rc.right = r16->right;
526 rc.bottom = r16->bottom;
527 EDIT_SetRectNP(es, &rc);
529 break;
530 case EM_SETRECTNP:
531 if ((es->style & ES_MULTILINE) && lParam)
532 EDIT_SetRectNP(es, (LPRECT)lParam);
533 break;
535 case EM_SCROLL16:
536 case EM_SCROLL:
537 result = EDIT_EM_Scroll(es, (INT)wParam);
538 break;
540 case EM_LINESCROLL16:
541 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
542 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
543 /* fall through */
544 case EM_LINESCROLL:
545 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
546 break;
548 case EM_SCROLLCARET16:
549 case EM_SCROLLCARET:
550 EDIT_EM_ScrollCaret(es);
551 result = 1;
552 break;
554 case EM_GETMODIFY16:
555 case EM_GETMODIFY:
556 result = ((es->flags & EF_MODIFIED) != 0);
557 break;
559 case EM_SETMODIFY16:
560 case EM_SETMODIFY:
561 if (wParam)
562 es->flags |= EF_MODIFIED;
563 else
564 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
565 break;
567 case EM_GETLINECOUNT16:
568 case EM_GETLINECOUNT:
569 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
570 break;
572 case EM_LINEINDEX16:
573 if ((INT16)wParam == -1)
574 wParam = (WPARAM)-1;
575 /* fall through */
576 case EM_LINEINDEX:
577 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
578 break;
580 case EM_SETHANDLE16:
581 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
582 break;
583 case EM_SETHANDLE:
584 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
585 break;
587 case EM_GETHANDLE16:
588 result = (LRESULT)EDIT_EM_GetHandle16(es);
589 break;
590 case EM_GETHANDLE:
591 result = (LRESULT)EDIT_EM_GetHandle(es);
592 break;
594 case EM_GETTHUMB16:
595 case EM_GETTHUMB:
596 result = EDIT_EM_GetThumb(es);
597 break;
599 /* these messages missing from specs */
600 case WM_USER+15:
601 case 0x00bf:
602 case WM_USER+16:
603 case 0x00c0:
604 case WM_USER+19:
605 case 0x00c3:
606 case WM_USER+26:
607 case 0x00ca:
608 FIXME("undocumented message 0x%x, please report\n", msg);
609 result = DefWindowProcW(hwnd, msg, wParam, lParam);
610 break;
612 case EM_LINELENGTH16:
613 case EM_LINELENGTH:
614 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
615 break;
617 case EM_REPLACESEL16:
618 lParam = (LPARAM)MapSL(lParam);
619 unicode = FALSE; /* 16-bit message is always ascii */
620 /* fall through */
621 case EM_REPLACESEL:
623 LPWSTR textW;
625 if(unicode)
626 textW = (LPWSTR)lParam;
627 else
629 LPSTR textA = (LPSTR)lParam;
630 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
631 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
632 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
635 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
636 result = 1;
638 if(!unicode)
639 HeapFree(GetProcessHeap(), 0, textW);
640 break;
643 case EM_GETLINE16:
644 lParam = (LPARAM)MapSL(lParam);
645 unicode = FALSE; /* 16-bit message is always ascii */
646 /* fall through */
647 case EM_GETLINE:
648 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
649 break;
651 case EM_LIMITTEXT16:
652 case EM_SETLIMITTEXT:
653 EDIT_EM_SetLimitText(es, (INT)wParam);
654 break;
656 case EM_CANUNDO16:
657 case EM_CANUNDO:
658 result = (LRESULT)EDIT_EM_CanUndo(es);
659 break;
661 case EM_UNDO16:
662 case EM_UNDO:
663 case WM_UNDO:
664 result = (LRESULT)EDIT_EM_Undo(es);
665 break;
667 case EM_FMTLINES16:
668 case EM_FMTLINES:
669 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
670 break;
672 case EM_LINEFROMCHAR16:
673 case EM_LINEFROMCHAR:
674 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
675 break;
677 case EM_SETTABSTOPS16:
678 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
679 break;
680 case EM_SETTABSTOPS:
681 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
682 break;
684 case EM_SETPASSWORDCHAR16:
685 unicode = FALSE; /* 16-bit message is always ascii */
686 /* fall through */
687 case EM_SETPASSWORDCHAR:
689 WCHAR charW = 0;
691 if(unicode)
692 charW = (WCHAR)wParam;
693 else
695 CHAR charA = wParam;
696 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
699 EDIT_EM_SetPasswordChar(es, charW);
700 break;
703 case EM_EMPTYUNDOBUFFER16:
704 case EM_EMPTYUNDOBUFFER:
705 EDIT_EM_EmptyUndoBuffer(es);
706 break;
708 case EM_GETFIRSTVISIBLELINE16:
709 result = es->y_offset;
710 break;
711 case EM_GETFIRSTVISIBLELINE:
712 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
713 break;
715 case EM_SETREADONLY16:
716 case EM_SETREADONLY:
717 if (wParam) {
718 SetWindowLongW( hwnd, GWL_STYLE,
719 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
720 es->style |= ES_READONLY;
721 } else {
722 SetWindowLongW( hwnd, GWL_STYLE,
723 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
724 es->style &= ~ES_READONLY;
726 result = 1;
727 break;
729 case EM_SETWORDBREAKPROC16:
730 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
731 break;
732 case EM_SETWORDBREAKPROC:
733 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
734 break;
736 case EM_GETWORDBREAKPROC16:
737 result = (LRESULT)es->word_break_proc16;
738 break;
739 case EM_GETWORDBREAKPROC:
740 result = (LRESULT)es->word_break_proc;
741 break;
743 case EM_GETPASSWORDCHAR16:
744 unicode = FALSE; /* 16-bit message is always ascii */
745 /* fall through */
746 case EM_GETPASSWORDCHAR:
748 if(unicode)
749 result = es->password_char;
750 else
752 WCHAR charW = es->password_char;
753 CHAR charA = 0;
754 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
755 result = charA;
757 break;
760 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
762 case EM_SETMARGINS:
763 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
764 break;
766 case EM_GETMARGINS:
767 result = MAKELONG(es->left_margin, es->right_margin);
768 break;
770 case EM_GETLIMITTEXT:
771 result = es->buffer_limit;
772 break;
774 case EM_POSFROMCHAR:
775 if ((INT)wParam >= get_text_length(es)) result = -1;
776 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
777 break;
779 case EM_CHARFROMPOS:
780 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
781 break;
783 /* End of the EM_ messages which were in numerical order; what order
784 * are these in? vaguely alphabetical?
787 case WM_NCCREATE:
788 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
789 break;
791 case WM_DESTROY:
792 result = EDIT_WM_Destroy(es);
793 es = NULL;
794 break;
796 case WM_GETDLGCODE:
797 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
799 if (es->style & ES_MULTILINE)
801 result |= DLGC_WANTALLKEYS;
802 break;
805 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
807 int vk = (int)((LPMSG)lParam)->wParam;
809 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
811 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
812 result |= DLGC_WANTMESSAGE;
815 break;
817 case WM_IME_CHAR:
818 if (!unicode)
820 WCHAR charW;
821 CHAR strng[2];
823 strng[0] = wParam >> 8;
824 strng[1] = wParam & 0xff;
825 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
826 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
827 EDIT_WM_Char(es, charW);
828 break;
830 /* fall through */
831 case WM_CHAR:
833 WCHAR charW;
835 if(unicode)
836 charW = wParam;
837 else
839 CHAR charA = wParam;
840 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
843 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
845 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
846 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
847 break;
849 EDIT_WM_Char(es, charW);
850 break;
853 case WM_CLEAR:
854 EDIT_WM_Clear(es);
855 break;
857 case WM_COMMAND:
858 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
859 break;
861 case WM_CONTEXTMENU:
862 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
863 break;
865 case WM_COPY:
866 EDIT_WM_Copy(es);
867 break;
869 case WM_CREATE:
870 if(unicode)
871 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
872 else
874 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
875 LPWSTR nameW = NULL;
876 if(nameA)
878 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
879 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
880 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
882 result = EDIT_WM_Create(es, nameW);
883 HeapFree(GetProcessHeap(), 0, nameW);
885 break;
887 case WM_CUT:
888 EDIT_WM_Cut(es);
889 break;
891 case WM_ENABLE:
892 es->bEnableState = (BOOL) wParam;
893 EDIT_UpdateText(es, NULL, TRUE);
894 break;
896 case WM_ERASEBKGND:
897 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
898 break;
900 case WM_GETFONT:
901 result = (LRESULT)es->font;
902 break;
904 case WM_GETTEXT:
905 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
906 break;
908 case WM_GETTEXTLENGTH:
909 if (unicode) result = get_text_length(es);
910 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
911 NULL, 0, NULL, NULL );
912 break;
914 case WM_HSCROLL:
915 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
916 break;
918 case WM_KEYDOWN:
919 result = EDIT_WM_KeyDown(es, (INT)wParam);
920 break;
922 case WM_KILLFOCUS:
923 result = EDIT_WM_KillFocus(es);
924 break;
926 case WM_LBUTTONDBLCLK:
927 result = EDIT_WM_LButtonDblClk(es);
928 break;
930 case WM_LBUTTONDOWN:
931 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
932 break;
934 case WM_LBUTTONUP:
935 result = EDIT_WM_LButtonUp(es);
936 break;
938 case WM_MBUTTONDOWN:
939 result = EDIT_WM_MButtonDown(es);
940 break;
942 case WM_MOUSEMOVE:
943 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
944 break;
946 case WM_PRINTCLIENT:
947 case WM_PAINT:
948 EDIT_WM_Paint(es, (HDC)wParam);
949 break;
951 case WM_PASTE:
952 EDIT_WM_Paste(es);
953 break;
955 case WM_SETFOCUS:
956 EDIT_WM_SetFocus(es);
957 break;
959 case WM_SETFONT:
960 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
961 break;
963 case WM_SETREDRAW:
964 /* FIXME: actually set an internal flag and behave accordingly */
965 break;
967 case WM_SETTEXT:
968 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
969 result = TRUE;
970 break;
972 case WM_SIZE:
973 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
974 break;
976 case WM_STYLECHANGED:
977 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
978 break;
980 case WM_STYLECHANGING:
981 result = 0; /* See EDIT_WM_StyleChanged */
982 break;
984 case WM_SYSKEYDOWN:
985 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
986 break;
988 case WM_TIMER:
989 EDIT_WM_Timer(es);
990 break;
992 case WM_VSCROLL:
993 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
994 break;
996 case WM_MOUSEWHEEL:
998 int gcWheelDelta = 0;
999 UINT pulScrollLines = 3;
1000 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1002 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1003 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1004 break;
1006 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1007 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1009 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1010 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1011 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1014 break;
1017 /* IME messages to make the edit control IME aware */
1018 case WM_IME_SETCONTEXT:
1019 break;
1021 case WM_IME_STARTCOMPOSITION:
1023 * FIXME in IME: This message is not always sent like it should be
1025 if (es->selection_start != es->selection_end)
1027 static const WCHAR empty_stringW[] = {0};
1028 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1030 es->composition_start = es->selection_end;
1031 es->composition_len = 0;
1032 break;
1034 case WM_IME_COMPOSITION:
1035 if (es->composition_len == 0)
1037 if (es->selection_start != es->selection_end)
1039 static const WCHAR empty_stringW[] = {0};
1040 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1043 es->composition_start = es->selection_end;
1045 EDIT_ImeComposition(hwnd,lParam,es);
1046 break;
1048 case WM_IME_ENDCOMPOSITION:
1049 es->composition_len= 0;
1050 break;
1052 case WM_IME_COMPOSITIONFULL:
1053 break;
1055 case WM_IME_SELECT:
1056 break;
1058 case WM_IME_CONTROL:
1059 break;
1061 default:
1062 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1063 break;
1066 if (es) EDIT_UnlockBuffer(es, FALSE);
1068 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1070 return result;
1073 /*********************************************************************
1075 * EditWndProcW (USER32.@)
1077 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1079 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1082 /*********************************************************************
1084 * EditWndProc (USER32.@)
1086 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1088 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1091 /*********************************************************************
1093 * EDIT_BuildLineDefs_ML
1095 * Build linked list of text lines.
1096 * Lines can end with '\0' (last line), a character (if it is wrapped),
1097 * a soft return '\r\r\n' or a hard return '\r\n'
1100 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1102 HDC dc;
1103 HFONT old_font = 0;
1104 LPWSTR current_position, cp;
1105 INT fw;
1106 LINEDEF *current_line;
1107 LINEDEF *previous_line;
1108 LINEDEF *start_line;
1109 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1110 INT line_count = es->line_count;
1111 INT orig_net_length;
1112 RECT rc;
1114 if (istart == iend && delta == 0)
1115 return;
1117 dc = GetDC(es->hwndSelf);
1118 if (es->font)
1119 old_font = SelectObject(dc, es->font);
1121 previous_line = NULL;
1122 current_line = es->first_line_def;
1124 /* Find starting line. istart must lie inside an existing line or
1125 * at the end of buffer */
1126 do {
1127 if (istart < current_line->index + current_line->length ||
1128 current_line->ending == END_0)
1129 break;
1131 previous_line = current_line;
1132 current_line = current_line->next;
1133 line_index++;
1134 } while (current_line);
1136 if (!current_line) /* Error occurred start is not inside previous buffer */
1138 FIXME(" modification occurred outside buffer\n");
1139 ReleaseDC(es->hwndSelf, dc);
1140 return;
1143 /* Remember start of modifications in order to calculate update region */
1144 nstart_line = line_index;
1145 nstart_index = current_line->index;
1147 /* We must start to reformat from the previous line since the modifications
1148 * may have caused the line to wrap upwards. */
1149 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1151 line_index--;
1152 current_line = previous_line;
1154 start_line = current_line;
1156 fw = es->format_rect.right - es->format_rect.left;
1157 current_position = es->text + current_line->index;
1158 do {
1159 if (current_line != start_line)
1161 if (!current_line || current_line->index + delta > current_position - es->text)
1163 /* The buffer has been expanded, create a new line and
1164 insert it into the link list */
1165 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1166 new_line->next = previous_line->next;
1167 previous_line->next = new_line;
1168 current_line = new_line;
1169 es->line_count++;
1171 else if (current_line->index + delta < current_position - es->text)
1173 /* The previous line merged with this line so we delete this extra entry */
1174 previous_line->next = current_line->next;
1175 HeapFree(GetProcessHeap(), 0, current_line);
1176 current_line = previous_line->next;
1177 es->line_count--;
1178 continue;
1180 else /* current_line->index + delta == current_position */
1182 if (current_position - es->text > iend)
1183 break; /* We reached end of line modifications */
1184 /* else recalulate this line */
1188 current_line->index = current_position - es->text;
1189 orig_net_length = current_line->net_length;
1191 /* Find end of line */
1192 cp = current_position;
1193 while (*cp) {
1194 if (*cp == '\n') break;
1195 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1196 break;
1197 cp++;
1200 /* Mark type of line termination */
1201 if (!(*cp)) {
1202 current_line->ending = END_0;
1203 current_line->net_length = strlenW(current_position);
1204 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1205 current_line->ending = END_SOFT;
1206 current_line->net_length = cp - current_position - 1;
1207 } else if (*cp == '\n') {
1208 current_line->ending = END_RICH;
1209 current_line->net_length = cp - current_position;
1210 } else {
1211 current_line->ending = END_HARD;
1212 current_line->net_length = cp - current_position;
1215 /* Calculate line width */
1216 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1217 current_position, current_line->net_length,
1218 es->tabs_count, es->tabs));
1220 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1221 if (!(es->style & ES_AUTOHSCROLL)) {
1222 if (current_line->width > fw) {
1223 INT next = 0;
1224 INT prev;
1225 do {
1226 prev = next;
1227 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1228 prev + 1, current_line->net_length, WB_RIGHT);
1229 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1230 current_position, next, es->tabs_count, es->tabs));
1231 } while (current_line->width <= fw);
1232 if (!prev) { /* Didn't find a line break so force a break */
1233 next = 0;
1234 do {
1235 prev = next;
1236 next++;
1237 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1238 current_position, next, es->tabs_count, es->tabs));
1239 } while (current_line->width <= fw);
1240 if (!prev)
1241 prev = 1;
1244 /* If the first line we are calculating, wrapped before istart, we must
1245 * adjust istart in order for this to be reflected in the update region. */
1246 if (current_line->index == nstart_index && istart > current_line->index + prev)
1247 istart = current_line->index + prev;
1248 /* else if we are updating the previous line before the first line we
1249 * are re-calculating and it expanded */
1250 else if (current_line == start_line &&
1251 current_line->index != nstart_index && orig_net_length < prev)
1253 /* Line expanded due to an upwards line wrap so we must partially include
1254 * previous line in update region */
1255 nstart_line = line_index;
1256 nstart_index = current_line->index;
1257 istart = current_line->index + orig_net_length;
1260 current_line->net_length = prev;
1261 current_line->ending = END_WRAP;
1262 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1263 current_line->net_length, es->tabs_count, es->tabs));
1265 else if (orig_net_length < current_line->net_length &&
1266 current_line == start_line &&
1267 current_line->index != nstart_index) {
1268 /* The previous line expanded but it's still not as wide as the client rect */
1269 /* The expansion is due to an upwards line wrap so we must partially include
1270 it in the update region */
1271 nstart_line = line_index;
1272 nstart_index = current_line->index;
1273 istart = current_line->index + orig_net_length;
1278 /* Adjust length to include line termination */
1279 switch (current_line->ending) {
1280 case END_SOFT:
1281 current_line->length = current_line->net_length + 3;
1282 break;
1283 case END_RICH:
1284 current_line->length = current_line->net_length + 1;
1285 break;
1286 case END_HARD:
1287 current_line->length = current_line->net_length + 2;
1288 break;
1289 case END_WRAP:
1290 case END_0:
1291 current_line->length = current_line->net_length;
1292 break;
1294 es->text_width = max(es->text_width, current_line->width);
1295 current_position += current_line->length;
1296 previous_line = current_line;
1297 current_line = current_line->next;
1298 line_index++;
1299 } while (previous_line->ending != END_0);
1301 /* Finish adjusting line indexes by delta or remove hanging lines */
1302 if (previous_line->ending == END_0)
1304 LINEDEF *pnext = NULL;
1306 previous_line->next = NULL;
1307 while (current_line)
1309 pnext = current_line->next;
1310 HeapFree(GetProcessHeap(), 0, current_line);
1311 current_line = pnext;
1312 es->line_count--;
1315 else if (delta != 0)
1317 while (current_line)
1319 current_line->index += delta;
1320 current_line = current_line->next;
1324 /* Calculate rest of modification rectangle */
1325 if (hrgn)
1327 HRGN tmphrgn;
1329 * We calculate two rectangles. One for the first line which may have
1330 * an indent with respect to the format rect. The other is a format-width
1331 * rectangle that spans the rest of the lines that changed or moved.
1333 rc.top = es->format_rect.top + nstart_line * es->line_height -
1334 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1335 rc.bottom = rc.top + es->line_height;
1336 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1337 rc.left = es->format_rect.left;
1338 else
1339 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1340 es->text + nstart_index, istart - nstart_index,
1341 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1342 rc.right = es->format_rect.right;
1343 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1345 rc.top = rc.bottom;
1346 rc.left = es->format_rect.left;
1347 rc.right = es->format_rect.right;
1349 * If lines were added or removed we must re-paint the remainder of the
1350 * lines since the remaining lines were either shifted up or down.
1352 if (line_count < es->line_count) /* We added lines */
1353 rc.bottom = es->line_count * es->line_height;
1354 else if (line_count > es->line_count) /* We removed lines */
1355 rc.bottom = line_count * es->line_height;
1356 else
1357 rc.bottom = line_index * es->line_height;
1358 rc.bottom += es->format_rect.top;
1359 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1360 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1361 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1362 DeleteObject(tmphrgn);
1365 if (es->font)
1366 SelectObject(dc, old_font);
1368 ReleaseDC(es->hwndSelf, dc);
1371 /*********************************************************************
1373 * EDIT_CalcLineWidth_SL
1376 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1378 SIZE size;
1379 LPWSTR text;
1380 HDC dc;
1381 HFONT old_font = 0;
1383 text = EDIT_GetPasswordPointer_SL(es);
1385 dc = GetDC(es->hwndSelf);
1386 if (es->font)
1387 old_font = SelectObject(dc, es->font);
1389 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1391 if (es->font)
1392 SelectObject(dc, old_font);
1393 ReleaseDC(es->hwndSelf, dc);
1395 if (es->style & ES_PASSWORD)
1396 HeapFree(GetProcessHeap(), 0, text);
1398 es->text_width = size.cx;
1401 /*********************************************************************
1403 * EDIT_CallWordBreakProc
1405 * Call appropriate WordBreakProc (internal or external).
1407 * Note: The "start" argument should always be an index referring
1408 * to es->text. The actual wordbreak proc might be
1409 * 16 bit, so we can't always pass any 32 bit LPSTR.
1410 * Hence we assume that es->text is the buffer that holds
1411 * the string under examination (we can decide this for ourselves).
1414 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1416 INT ret;
1418 if (es->word_break_proc16) {
1419 HGLOBAL16 hglob16;
1420 SEGPTR segptr;
1421 INT countA;
1422 WORD args[5];
1423 DWORD result;
1425 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1426 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1427 segptr = WOWGlobalLock16(hglob16);
1428 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1429 args[4] = SELECTOROF(segptr);
1430 args[3] = OFFSETOF(segptr);
1431 args[2] = index;
1432 args[1] = countA;
1433 args[0] = action;
1434 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1435 ret = LOWORD(result);
1436 GlobalUnlock16(hglob16);
1437 GlobalFree16(hglob16);
1439 else if (es->word_break_proc)
1441 if(es->is_unicode)
1443 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1445 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1446 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1447 ret = wbpW(es->text + start, index, count, action);
1449 else
1451 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1452 INT countA;
1453 CHAR *textA;
1455 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1456 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1457 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1458 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1459 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1460 ret = wbpA(textA, index, countA, action);
1461 HeapFree(GetProcessHeap(), 0, textA);
1464 else
1465 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1467 return ret;
1471 /*********************************************************************
1473 * EDIT_CharFromPos
1475 * Beware: This is not the function called on EM_CHARFROMPOS
1476 * The position _can_ be outside the formatting / client
1477 * rectangle
1478 * The return value is only the character index
1481 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1483 INT index;
1484 HDC dc;
1485 HFONT old_font = 0;
1486 INT x_high = 0, x_low = 0;
1488 if (es->style & ES_MULTILINE) {
1489 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1490 INT line_index = 0;
1491 LINEDEF *line_def = es->first_line_def;
1492 INT low, high;
1493 while ((line > 0) && line_def->next) {
1494 line_index += line_def->length;
1495 line_def = line_def->next;
1496 line--;
1498 x += es->x_offset - es->format_rect.left;
1499 if (es->style & ES_RIGHT)
1500 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1501 else if (es->style & ES_CENTER)
1502 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1503 if (x >= line_def->width) {
1504 if (after_wrap)
1505 *after_wrap = (line_def->ending == END_WRAP);
1506 return line_index + line_def->net_length;
1508 if (x <= 0) {
1509 if (after_wrap)
1510 *after_wrap = FALSE;
1511 return line_index;
1513 dc = GetDC(es->hwndSelf);
1514 if (es->font)
1515 old_font = SelectObject(dc, es->font);
1516 low = line_index;
1517 high = line_index + line_def->net_length + 1;
1518 while (low < high - 1)
1520 INT mid = (low + high) / 2;
1521 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1522 if (x_now > x) {
1523 high = mid;
1524 x_high = x_now;
1525 } else {
1526 low = mid;
1527 x_low = x_now;
1530 if (abs(x_high - x) + 1 <= abs(x_low - x))
1531 index = high;
1532 else
1533 index = low;
1535 if (after_wrap)
1536 *after_wrap = ((index == line_index + line_def->net_length) &&
1537 (line_def->ending == END_WRAP));
1538 } else {
1539 LPWSTR text;
1540 SIZE size;
1541 if (after_wrap)
1542 *after_wrap = FALSE;
1543 x -= es->format_rect.left;
1544 if (!x)
1545 return es->x_offset;
1547 if (!es->x_offset)
1549 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1550 if (es->style & ES_RIGHT)
1551 x -= indent;
1552 else if (es->style & ES_CENTER)
1553 x -= indent / 2;
1556 text = EDIT_GetPasswordPointer_SL(es);
1557 dc = GetDC(es->hwndSelf);
1558 if (es->font)
1559 old_font = SelectObject(dc, es->font);
1560 if (x < 0)
1562 INT low = 0;
1563 INT high = es->x_offset;
1564 while (low < high - 1)
1566 INT mid = (low + high) / 2;
1567 GetTextExtentPoint32W( dc, text + mid,
1568 es->x_offset - mid, &size );
1569 if (size.cx > -x) {
1570 low = mid;
1571 x_low = size.cx;
1572 } else {
1573 high = mid;
1574 x_high = size.cx;
1577 if (abs(x_high + x) <= abs(x_low + x) + 1)
1578 index = high;
1579 else
1580 index = low;
1582 else
1584 INT low = es->x_offset;
1585 INT high = get_text_length(es) + 1;
1586 while (low < high - 1)
1588 INT mid = (low + high) / 2;
1589 GetTextExtentPoint32W( dc, text + es->x_offset,
1590 mid - es->x_offset, &size );
1591 if (size.cx > x) {
1592 high = mid;
1593 x_high = size.cx;
1594 } else {
1595 low = mid;
1596 x_low = size.cx;
1599 if (abs(x_high - x) <= abs(x_low - x) + 1)
1600 index = high;
1601 else
1602 index = low;
1604 if (es->style & ES_PASSWORD)
1605 HeapFree(GetProcessHeap(), 0, text);
1607 if (es->font)
1608 SelectObject(dc, old_font);
1609 ReleaseDC(es->hwndSelf, dc);
1610 return index;
1614 /*********************************************************************
1616 * EDIT_ConfinePoint
1618 * adjusts the point to be within the formatting rectangle
1619 * (so CharFromPos returns the nearest _visible_ character)
1622 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1624 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1625 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1629 /*********************************************************************
1631 * EDIT_GetLineRect
1633 * Calculates the bounding rectangle for a line from a starting
1634 * column to an ending column.
1637 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1639 INT line_index = EDIT_EM_LineIndex(es, line);
1641 if (es->style & ES_MULTILINE)
1642 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1643 else
1644 rc->top = es->format_rect.top;
1645 rc->bottom = rc->top + es->line_height;
1646 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1647 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1651 /*********************************************************************
1653 * EDIT_GetPasswordPointer_SL
1655 * note: caller should free the (optionally) allocated buffer
1658 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1660 if (es->style & ES_PASSWORD) {
1661 INT len = get_text_length(es);
1662 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1663 text[len] = '\0';
1664 while(len) text[--len] = es->password_char;
1665 return text;
1666 } else
1667 return es->text;
1671 /*********************************************************************
1673 * EDIT_LockBuffer
1675 * This acts as a LocalLock16(), but it locks only once. This way
1676 * you can call it whenever you like, without unlocking.
1678 * Initially the edit control allocates a HLOCAL32 buffer
1679 * (32 bit linear memory handler). However, 16 bit application
1680 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1681 * handler). From that moment on we have to keep using this 16 bit memory
1682 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1683 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1684 * conversion.
1687 static void EDIT_LockBuffer(EDITSTATE *es)
1689 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1690 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1692 if (!es->text) {
1693 CHAR *textA = NULL;
1694 UINT countA = 0;
1695 BOOL _16bit = FALSE;
1697 if(es->hloc32W)
1699 if(es->hloc32A)
1701 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1702 textA = LocalLock(es->hloc32A);
1703 countA = strlen(textA) + 1;
1705 else if(es->hloc16)
1707 HANDLE16 oldDS = stack16->ds;
1708 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1709 stack16->ds = hInstance;
1710 textA = MapSL(LocalLock16(es->hloc16));
1711 stack16->ds = oldDS;
1712 countA = strlen(textA) + 1;
1713 _16bit = TRUE;
1716 else {
1717 ERR("no buffer ... please report\n");
1718 return;
1721 if(textA)
1723 HLOCAL hloc32W_new;
1724 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1725 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1726 if(countW_new > es->buffer_size + 1)
1728 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1729 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1730 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1731 if(hloc32W_new)
1733 es->hloc32W = hloc32W_new;
1734 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1735 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1737 else
1738 WARN("FAILED! Will synchronize partially\n");
1742 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1743 es->text = LocalLock(es->hloc32W);
1745 if(textA)
1747 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1748 if(_16bit)
1750 HANDLE16 oldDS = stack16->ds;
1751 stack16->ds = hInstance;
1752 LocalUnlock16(es->hloc16);
1753 stack16->ds = oldDS;
1755 else
1756 LocalUnlock(es->hloc32A);
1759 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1760 es->lock_count++;
1764 /*********************************************************************
1766 * EDIT_SL_InvalidateText
1768 * Called from EDIT_InvalidateText().
1769 * Does the job for single-line controls only.
1772 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1774 RECT line_rect;
1775 RECT rc;
1777 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1778 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1779 EDIT_UpdateText(es, &rc, TRUE);
1783 /*********************************************************************
1785 * EDIT_ML_InvalidateText
1787 * Called from EDIT_InvalidateText().
1788 * Does the job for multi-line controls only.
1791 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1793 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1794 INT sl = EDIT_EM_LineFromChar(es, start);
1795 INT el = EDIT_EM_LineFromChar(es, end);
1796 INT sc;
1797 INT ec;
1798 RECT rc1;
1799 RECT rcWnd;
1800 RECT rcLine;
1801 RECT rcUpdate;
1802 INT l;
1804 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1805 return;
1807 sc = start - EDIT_EM_LineIndex(es, sl);
1808 ec = end - EDIT_EM_LineIndex(es, el);
1809 if (sl < es->y_offset) {
1810 sl = es->y_offset;
1811 sc = 0;
1813 if (el > es->y_offset + vlc) {
1814 el = es->y_offset + vlc;
1815 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1817 GetClientRect(es->hwndSelf, &rc1);
1818 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1819 if (sl == el) {
1820 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1821 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1822 EDIT_UpdateText(es, &rcUpdate, TRUE);
1823 } else {
1824 EDIT_GetLineRect(es, sl, sc,
1825 EDIT_EM_LineLength(es,
1826 EDIT_EM_LineIndex(es, sl)),
1827 &rcLine);
1828 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1829 EDIT_UpdateText(es, &rcUpdate, TRUE);
1830 for (l = sl + 1 ; l < el ; l++) {
1831 EDIT_GetLineRect(es, l, 0,
1832 EDIT_EM_LineLength(es,
1833 EDIT_EM_LineIndex(es, l)),
1834 &rcLine);
1835 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1836 EDIT_UpdateText(es, &rcUpdate, TRUE);
1838 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1839 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1840 EDIT_UpdateText(es, &rcUpdate, TRUE);
1845 /*********************************************************************
1847 * EDIT_InvalidateText
1849 * Invalidate the text from offset start up to, but not including,
1850 * offset end. Useful for (re)painting the selection.
1851 * Regions outside the linewidth are not invalidated.
1852 * end == -1 means end == TextLength.
1853 * start and end need not be ordered.
1856 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1858 if (end == start)
1859 return;
1861 if (end == -1)
1862 end = get_text_length(es);
1864 if (end < start) {
1865 INT tmp = start;
1866 start = end;
1867 end = tmp;
1870 if (es->style & ES_MULTILINE)
1871 EDIT_ML_InvalidateText(es, start, end);
1872 else
1873 EDIT_SL_InvalidateText(es, start, end);
1877 /*********************************************************************
1879 * EDIT_MakeFit
1881 * Try to fit size + 1 characters in the buffer.
1883 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1885 HLOCAL hNew32W;
1887 if (size <= es->buffer_size)
1888 return TRUE;
1890 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1892 /* Force edit to unlock it's buffer. es->text now NULL */
1893 EDIT_UnlockBuffer(es, TRUE);
1895 if (es->hloc32W) {
1896 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1897 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1898 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1899 es->hloc32W = hNew32W;
1900 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1904 EDIT_LockBuffer(es);
1906 if (es->buffer_size < size) {
1907 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1908 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1909 return FALSE;
1910 } else {
1911 TRACE("We now have %d+1\n", es->buffer_size);
1912 return TRUE;
1917 /*********************************************************************
1919 * EDIT_MakeUndoFit
1921 * Try to fit size + 1 bytes in the undo buffer.
1924 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1926 UINT alloc_size;
1928 if (size <= es->undo_buffer_size)
1929 return TRUE;
1931 TRACE("trying to ReAlloc to %d+1\n", size);
1933 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1934 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1935 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1936 return TRUE;
1938 else
1940 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1941 return FALSE;
1946 /*********************************************************************
1948 * EDIT_MoveBackward
1951 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1953 INT e = es->selection_end;
1955 if (e) {
1956 e--;
1957 if ((es->style & ES_MULTILINE) && e &&
1958 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1959 e--;
1960 if (e && (es->text[e - 1] == '\r'))
1961 e--;
1964 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1965 EDIT_EM_ScrollCaret(es);
1969 /*********************************************************************
1971 * EDIT_MoveDown_ML
1973 * Only for multi line controls
1974 * Move the caret one line down, on a column with the nearest
1975 * x coordinate on the screen (might be a different column).
1978 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1980 INT s = es->selection_start;
1981 INT e = es->selection_end;
1982 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1983 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1984 INT x = (short)LOWORD(pos);
1985 INT y = (short)HIWORD(pos);
1987 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1988 if (!extend)
1989 s = e;
1990 EDIT_EM_SetSel(es, s, e, after_wrap);
1991 EDIT_EM_ScrollCaret(es);
1995 /*********************************************************************
1997 * EDIT_MoveEnd
2000 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
2002 BOOL after_wrap = FALSE;
2003 INT e;
2005 /* Pass a high value in x to make sure of receiving the end of the line */
2006 if (es->style & ES_MULTILINE)
2007 e = EDIT_CharFromPos(es, 0x3fffffff,
2008 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2009 else
2010 e = get_text_length(es);
2011 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2012 EDIT_EM_ScrollCaret(es);
2016 /*********************************************************************
2018 * EDIT_MoveForward
2021 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2023 INT e = es->selection_end;
2025 if (es->text[e]) {
2026 e++;
2027 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2028 if (es->text[e] == '\n')
2029 e++;
2030 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2031 e += 2;
2034 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2035 EDIT_EM_ScrollCaret(es);
2039 /*********************************************************************
2041 * EDIT_MoveHome
2043 * Home key: move to beginning of line.
2046 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2048 INT e;
2050 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2051 if (es->style & ES_MULTILINE)
2052 e = EDIT_CharFromPos(es, -es->x_offset,
2053 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2054 else
2055 e = 0;
2056 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2057 EDIT_EM_ScrollCaret(es);
2061 /*********************************************************************
2063 * EDIT_MovePageDown_ML
2065 * Only for multi line controls
2066 * Move the caret one page down, on a column with the nearest
2067 * x coordinate on the screen (might be a different column).
2070 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2072 INT s = es->selection_start;
2073 INT e = es->selection_end;
2074 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2075 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2076 INT x = (short)LOWORD(pos);
2077 INT y = (short)HIWORD(pos);
2079 e = EDIT_CharFromPos(es, x,
2080 y + (es->format_rect.bottom - es->format_rect.top),
2081 &after_wrap);
2082 if (!extend)
2083 s = e;
2084 EDIT_EM_SetSel(es, s, e, after_wrap);
2085 EDIT_EM_ScrollCaret(es);
2089 /*********************************************************************
2091 * EDIT_MovePageUp_ML
2093 * Only for multi line controls
2094 * Move the caret one page up, on a column with the nearest
2095 * x coordinate on the screen (might be a different column).
2098 static void EDIT_MovePageUp_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_MoveUp_ML
2121 * Only for multi line controls
2122 * Move the caret one line up, on a column with the nearest
2123 * x coordinate on the screen (might be a different column).
2126 static void EDIT_MoveUp_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, y - es->line_height, &after_wrap);
2136 if (!extend)
2137 s = e;
2138 EDIT_EM_SetSel(es, s, e, after_wrap);
2139 EDIT_EM_ScrollCaret(es);
2143 /*********************************************************************
2145 * EDIT_MoveWordBackward
2148 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2150 INT s = es->selection_start;
2151 INT e = es->selection_end;
2152 INT l;
2153 INT ll;
2154 INT li;
2156 l = EDIT_EM_LineFromChar(es, e);
2157 ll = EDIT_EM_LineLength(es, e);
2158 li = EDIT_EM_LineIndex(es, l);
2159 if (e - li == 0) {
2160 if (l) {
2161 li = EDIT_EM_LineIndex(es, l - 1);
2162 e = li + EDIT_EM_LineLength(es, li);
2164 } else {
2165 e = li + (INT)EDIT_CallWordBreakProc(es,
2166 li, e - li, ll, WB_LEFT);
2168 if (!extend)
2169 s = e;
2170 EDIT_EM_SetSel(es, s, e, FALSE);
2171 EDIT_EM_ScrollCaret(es);
2175 /*********************************************************************
2177 * EDIT_MoveWordForward
2180 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2182 INT s = es->selection_start;
2183 INT e = es->selection_end;
2184 INT l;
2185 INT ll;
2186 INT li;
2188 l = EDIT_EM_LineFromChar(es, e);
2189 ll = EDIT_EM_LineLength(es, e);
2190 li = EDIT_EM_LineIndex(es, l);
2191 if (e - li == ll) {
2192 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2193 e = EDIT_EM_LineIndex(es, l + 1);
2194 } else {
2195 e = li + EDIT_CallWordBreakProc(es,
2196 li, e - li + 1, ll, WB_RIGHT);
2198 if (!extend)
2199 s = e;
2200 EDIT_EM_SetSel(es, s, e, FALSE);
2201 EDIT_EM_ScrollCaret(es);
2205 /*********************************************************************
2207 * EDIT_PaintLine
2210 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2212 INT s = es->selection_start;
2213 INT e = es->selection_end;
2214 INT li;
2215 INT ll;
2216 INT x;
2217 INT y;
2218 LRESULT pos;
2220 if (es->style & ES_MULTILINE) {
2221 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2222 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2223 return;
2224 } else if (line)
2225 return;
2227 TRACE("line=%d\n", line);
2229 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2230 x = (short)LOWORD(pos);
2231 y = (short)HIWORD(pos);
2232 li = EDIT_EM_LineIndex(es, line);
2233 ll = EDIT_EM_LineLength(es, li);
2234 s = min(es->selection_start, es->selection_end);
2235 e = max(es->selection_start, es->selection_end);
2236 s = min(li + ll, max(li, s));
2237 e = min(li + ll, max(li, e));
2238 if (rev && (s != e) &&
2239 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2240 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2241 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2242 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2243 } else
2244 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2248 /*********************************************************************
2250 * EDIT_PaintText
2253 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2255 COLORREF BkColor;
2256 COLORREF TextColor;
2257 LOGFONTW underline_font;
2258 HFONT hUnderline = 0;
2259 HFONT old_font = 0;
2260 INT ret;
2261 INT li;
2262 INT BkMode;
2263 SIZE size;
2265 if (!count)
2266 return 0;
2267 BkMode = GetBkMode(dc);
2268 BkColor = GetBkColor(dc);
2269 TextColor = GetTextColor(dc);
2270 if (rev) {
2271 if (es->composition_len == 0)
2273 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2274 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2275 SetBkMode( dc, OPAQUE);
2277 else
2279 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2280 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2281 underline_font.lfUnderline = TRUE;
2282 hUnderline = CreateFontIndirectW(&underline_font);
2283 old_font = SelectObject(dc,hUnderline);
2286 li = EDIT_EM_LineIndex(es, line);
2287 if (es->style & ES_MULTILINE) {
2288 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2289 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2290 } else {
2291 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2292 TextOutW(dc, x, y, text + li + col, count);
2293 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2294 ret = size.cx;
2295 if (es->style & ES_PASSWORD)
2296 HeapFree(GetProcessHeap(), 0, text);
2298 if (rev) {
2299 if (es->composition_len == 0)
2301 SetBkColor(dc, BkColor);
2302 SetTextColor(dc, TextColor);
2303 SetBkMode( dc, BkMode);
2305 else
2307 if (old_font)
2308 SelectObject(dc,old_font);
2309 if (hUnderline)
2310 DeleteObject(hUnderline);
2313 return ret;
2317 /*********************************************************************
2319 * EDIT_SetCaretPos
2322 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2323 BOOL after_wrap)
2325 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2326 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2327 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2331 /*********************************************************************
2333 * EDIT_AdjustFormatRect
2335 * Adjusts the format rectangle for the current font and the
2336 * current client rectangle.
2339 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2341 RECT ClientRect;
2343 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2344 if (es->style & ES_MULTILINE)
2346 INT fw, vlc, max_x_offset, max_y_offset;
2348 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2349 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2351 /* correct es->x_offset */
2352 fw = es->format_rect.right - es->format_rect.left;
2353 max_x_offset = es->text_width - fw;
2354 if(max_x_offset < 0) max_x_offset = 0;
2355 if(es->x_offset > max_x_offset)
2356 es->x_offset = max_x_offset;
2358 /* correct es->y_offset */
2359 max_y_offset = es->line_count - vlc;
2360 if(max_y_offset < 0) max_y_offset = 0;
2361 if(es->y_offset > max_y_offset)
2362 es->y_offset = max_y_offset;
2364 /* force scroll info update */
2365 EDIT_UpdateScrollInfo(es);
2367 else
2368 /* Windows doesn't care to fix text placement for SL controls */
2369 es->format_rect.bottom = es->format_rect.top + es->line_height;
2371 /* Always stay within the client area */
2372 GetClientRect(es->hwndSelf, &ClientRect);
2373 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2375 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2376 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2378 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2382 /*********************************************************************
2384 * EDIT_SetRectNP
2386 * note: this is not (exactly) the handler called on EM_SETRECTNP
2387 * it is also used to set the rect of a single line control
2390 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2392 LONG_PTR ExStyle;
2393 INT bw, bh;
2394 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2396 CopyRect(&es->format_rect, rc);
2398 if (ExStyle & WS_EX_CLIENTEDGE) {
2399 es->format_rect.left++;
2400 es->format_rect.right--;
2402 if (es->format_rect.bottom - es->format_rect.top
2403 >= es->line_height + 2)
2405 es->format_rect.top++;
2406 es->format_rect.bottom--;
2409 else if (es->style & WS_BORDER) {
2410 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2411 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2412 es->format_rect.left += bw;
2413 es->format_rect.right -= bw;
2414 if (es->format_rect.bottom - es->format_rect.top
2415 >= es->line_height + 2 * bh)
2417 es->format_rect.top += bh;
2418 es->format_rect.bottom -= bh;
2422 es->format_rect.left += es->left_margin;
2423 es->format_rect.right -= es->right_margin;
2424 EDIT_AdjustFormatRect(es);
2428 /*********************************************************************
2430 * EDIT_UnlockBuffer
2433 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2436 /* Edit window might be already destroyed */
2437 if(!IsWindow(es->hwndSelf))
2439 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2440 return;
2443 if (!es->lock_count) {
2444 ERR("lock_count == 0 ... please report\n");
2445 return;
2447 if (!es->text) {
2448 ERR("es->text == 0 ... please report\n");
2449 return;
2452 if (force || (es->lock_count == 1)) {
2453 if (es->hloc32W) {
2454 CHAR *textA = NULL;
2455 UINT countA = 0;
2456 UINT countW = get_text_length(es) + 1;
2457 STACK16FRAME* stack16 = NULL;
2458 HANDLE16 oldDS = 0;
2460 if(es->hloc32A)
2462 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2463 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2464 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2465 countA = LocalSize(es->hloc32A);
2466 if(countA_new > countA)
2468 HLOCAL hloc32A_new;
2469 UINT alloc_size = ROUND_TO_GROW(countA_new);
2470 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2471 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2472 if(hloc32A_new)
2474 es->hloc32A = hloc32A_new;
2475 countA = LocalSize(hloc32A_new);
2476 TRACE("Real new size %d bytes\n", countA);
2478 else
2479 WARN("FAILED! Will synchronize partially\n");
2481 textA = LocalLock(es->hloc32A);
2483 else if(es->hloc16)
2485 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2487 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2488 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2490 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2491 oldDS = stack16->ds;
2492 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2494 countA = LocalSize16(es->hloc16);
2495 if(countA_new > countA)
2497 HLOCAL16 hloc16_new;
2498 UINT alloc_size = ROUND_TO_GROW(countA_new);
2499 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2500 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2501 if(hloc16_new)
2503 es->hloc16 = hloc16_new;
2504 countA = LocalSize16(hloc16_new);
2505 TRACE("Real new size %d bytes\n", countA);
2507 else
2508 WARN("FAILED! Will synchronize partially\n");
2510 textA = MapSL(LocalLock16(es->hloc16));
2513 if(textA)
2515 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2516 if(stack16)
2517 LocalUnlock16(es->hloc16);
2518 else
2519 LocalUnlock(es->hloc32A);
2522 if (stack16) stack16->ds = oldDS;
2523 LocalUnlock(es->hloc32W);
2524 es->text = NULL;
2526 else {
2527 ERR("no buffer ... please report\n");
2528 return;
2531 es->lock_count--;
2535 /*********************************************************************
2537 * EDIT_UpdateScrollInfo
2540 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2542 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2544 SCROLLINFO si;
2545 si.cbSize = sizeof(SCROLLINFO);
2546 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2547 si.nMin = 0;
2548 si.nMax = es->line_count - 1;
2549 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2550 si.nPos = es->y_offset;
2551 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2552 si.nMin, si.nMax, si.nPage, si.nPos);
2553 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2556 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2558 SCROLLINFO si;
2559 si.cbSize = sizeof(SCROLLINFO);
2560 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2561 si.nMin = 0;
2562 si.nMax = es->text_width - 1;
2563 si.nPage = es->format_rect.right - es->format_rect.left;
2564 si.nPos = es->x_offset;
2565 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2566 si.nMin, si.nMax, si.nPage, si.nPos);
2567 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2571 /*********************************************************************
2573 * EDIT_WordBreakProc
2575 * Find the beginning of words.
2576 * Note: unlike the specs for a WordBreakProc, this function only
2577 * allows to be called without linebreaks between s[0] up to
2578 * s[count - 1]. Remember it is only called
2579 * internally, so we can decide this for ourselves.
2582 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2584 INT ret = 0;
2586 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2588 if(!s) return 0;
2590 switch (action) {
2591 case WB_LEFT:
2592 if (!count)
2593 break;
2594 if (index)
2595 index--;
2596 if (s[index] == ' ') {
2597 while (index && (s[index] == ' '))
2598 index--;
2599 if (index) {
2600 while (index && (s[index] != ' '))
2601 index--;
2602 if (s[index] == ' ')
2603 index++;
2605 } else {
2606 while (index && (s[index] != ' '))
2607 index--;
2608 if (s[index] == ' ')
2609 index++;
2611 ret = index;
2612 break;
2613 case WB_RIGHT:
2614 if (!count)
2615 break;
2616 if (index)
2617 index--;
2618 if (s[index] == ' ')
2619 while ((index < count) && (s[index] == ' ')) index++;
2620 else {
2621 while (s[index] && (s[index] != ' ') && (index < count))
2622 index++;
2623 while ((s[index] == ' ') && (index < count)) index++;
2625 ret = index;
2626 break;
2627 case WB_ISDELIMITER:
2628 ret = (s[index] == ' ');
2629 break;
2630 default:
2631 ERR("unknown action code, please report !\n");
2632 break;
2634 return ret;
2638 /*********************************************************************
2640 * EM_CHARFROMPOS
2642 * returns line number (not index) in high-order word of result.
2643 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2644 * to Richedit, not to the edit control. Original documentation is valid.
2645 * FIXME: do the specs mean to return -1 if outside client area or
2646 * if outside formatting rectangle ???
2649 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2651 POINT pt;
2652 RECT rc;
2653 INT index;
2655 pt.x = x;
2656 pt.y = y;
2657 GetClientRect(es->hwndSelf, &rc);
2658 if (!PtInRect(&rc, pt))
2659 return -1;
2661 index = EDIT_CharFromPos(es, x, y, NULL);
2662 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2666 /*********************************************************************
2668 * EM_FMTLINES
2670 * Enable or disable soft breaks.
2672 * This means: insert or remove the soft linebreak character (\r\r\n).
2673 * Take care to check if the text still fits the buffer after insertion.
2674 * If not, notify with EN_ERRSPACE.
2677 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2679 es->flags &= ~EF_USE_SOFTBRK;
2680 if (add_eol) {
2681 es->flags |= EF_USE_SOFTBRK;
2682 FIXME("soft break enabled, not implemented\n");
2684 return add_eol;
2688 /*********************************************************************
2690 * EM_GETHANDLE
2692 * Hopefully this won't fire back at us.
2693 * We always start with a fixed buffer in the local heap.
2694 * Despite of the documentation says that the local heap is used
2695 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2696 * buffer on the local heap.
2699 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2701 HLOCAL hLocal;
2703 if (!(es->style & ES_MULTILINE))
2704 return 0;
2706 if(es->is_unicode)
2707 hLocal = es->hloc32W;
2708 else
2710 if(!es->hloc32A)
2712 CHAR *textA;
2713 UINT countA, alloc_size;
2714 TRACE("Allocating 32-bit ANSI alias buffer\n");
2715 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2716 alloc_size = ROUND_TO_GROW(countA);
2717 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2719 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2720 return 0;
2722 textA = LocalLock(es->hloc32A);
2723 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2724 LocalUnlock(es->hloc32A);
2726 hLocal = es->hloc32A;
2729 es->flags |= EF_APP_HAS_HANDLE;
2730 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2731 return hLocal;
2735 /*********************************************************************
2737 * EM_GETHANDLE16
2739 * Hopefully this won't fire back at us.
2740 * We always start with a buffer in 32 bit linear memory.
2741 * However, with this message a 16 bit application requests
2742 * a handle of 16 bit local heap memory, where it expects to find
2743 * the text.
2744 * It's a pitty that from this moment on we have to use this
2745 * local heap, because applications may rely on the handle
2746 * in the future.
2748 * In this function we'll try to switch to local heap.
2750 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2752 CHAR *textA;
2753 UINT countA, alloc_size;
2754 STACK16FRAME* stack16;
2755 HANDLE16 oldDS;
2757 if (!(es->style & ES_MULTILINE))
2758 return 0;
2760 if (es->hloc16)
2761 return es->hloc16;
2763 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2764 oldDS = stack16->ds;
2765 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2767 if (!LocalHeapSize16()) {
2769 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2770 ERR("could not initialize local heap\n");
2771 goto done;
2773 TRACE("local heap initialized\n");
2776 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2777 alloc_size = ROUND_TO_GROW(countA);
2779 TRACE("Allocating 16-bit ANSI alias buffer\n");
2780 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2781 ERR("could not allocate new 16 bit buffer\n");
2782 goto done;
2785 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2786 ERR("could not lock new 16 bit buffer\n");
2787 LocalFree16(es->hloc16);
2788 es->hloc16 = 0;
2789 goto done;
2792 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2793 LocalUnlock16(es->hloc16);
2794 es->flags |= EF_APP_HAS_HANDLE;
2796 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2798 done:
2799 stack16->ds = oldDS;
2800 return es->hloc16;
2804 /*********************************************************************
2806 * EM_GETLINE
2809 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2811 LPWSTR src;
2812 INT line_len, dst_len;
2813 INT i;
2815 if (es->style & ES_MULTILINE) {
2816 if (line >= es->line_count)
2817 return 0;
2818 } else
2819 line = 0;
2820 i = EDIT_EM_LineIndex(es, line);
2821 src = es->text + i;
2822 line_len = EDIT_EM_LineLength(es, i);
2823 dst_len = *(WORD *)dst;
2824 if(unicode)
2826 if(dst_len <= line_len)
2828 memcpy(dst, src, dst_len * sizeof(WCHAR));
2829 return dst_len;
2831 else /* Append 0 if enough space */
2833 memcpy(dst, src, line_len * sizeof(WCHAR));
2834 dst[line_len] = 0;
2835 return line_len;
2838 else
2840 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2841 if(!ret && line_len) /* Insufficient buffer size */
2842 return dst_len;
2843 if(ret < dst_len) /* Append 0 if enough space */
2844 ((LPSTR)dst)[ret] = 0;
2845 return ret;
2850 /*********************************************************************
2852 * EM_GETSEL
2855 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2857 UINT s = es->selection_start;
2858 UINT e = es->selection_end;
2860 ORDER_UINT(s, e);
2861 if (start)
2862 *start = s;
2863 if (end)
2864 *end = e;
2865 return MAKELONG(s, e);
2869 /*********************************************************************
2871 * EM_GETTHUMB
2873 * FIXME: is this right ? (or should it be only VSCROLL)
2874 * (and maybe only for edit controls that really have their
2875 * own scrollbars) (and maybe only for multiline controls ?)
2876 * All in all: very poorly documented
2879 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2881 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2882 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2886 /*********************************************************************
2888 * EM_LINEFROMCHAR
2891 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2893 INT line;
2894 LINEDEF *line_def;
2896 if (!(es->style & ES_MULTILINE))
2897 return 0;
2898 if (index > (INT)get_text_length(es))
2899 return es->line_count - 1;
2900 if (index == -1)
2901 index = min(es->selection_start, es->selection_end);
2903 line = 0;
2904 line_def = es->first_line_def;
2905 index -= line_def->length;
2906 while ((index >= 0) && line_def->next) {
2907 line++;
2908 line_def = line_def->next;
2909 index -= line_def->length;
2911 return line;
2915 /*********************************************************************
2917 * EM_LINEINDEX
2920 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2922 INT line_index;
2923 LINEDEF *line_def;
2925 if (!(es->style & ES_MULTILINE))
2926 return 0;
2927 if (line >= es->line_count)
2928 return -1;
2930 line_index = 0;
2931 line_def = es->first_line_def;
2932 if (line == -1) {
2933 INT index = es->selection_end - line_def->length;
2934 while ((index >= 0) && line_def->next) {
2935 line_index += line_def->length;
2936 line_def = line_def->next;
2937 index -= line_def->length;
2939 } else {
2940 while (line > 0) {
2941 line_index += line_def->length;
2942 line_def = line_def->next;
2943 line--;
2946 return line_index;
2950 /*********************************************************************
2952 * EM_LINELENGTH
2955 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2957 LINEDEF *line_def;
2959 if (!(es->style & ES_MULTILINE))
2960 return get_text_length(es);
2962 if (index == -1) {
2963 /* get the number of remaining non-selected chars of selected lines */
2964 INT32 l; /* line number */
2965 INT32 li; /* index of first char in line */
2966 INT32 count;
2967 l = EDIT_EM_LineFromChar(es, es->selection_start);
2968 /* # chars before start of selection area */
2969 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2970 l = EDIT_EM_LineFromChar(es, es->selection_end);
2971 /* # chars after end of selection */
2972 li = EDIT_EM_LineIndex(es, l);
2973 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2974 return count;
2976 line_def = es->first_line_def;
2977 index -= line_def->length;
2978 while ((index >= 0) && line_def->next) {
2979 line_def = line_def->next;
2980 index -= line_def->length;
2982 return line_def->net_length;
2986 /*********************************************************************
2988 * EM_LINESCROLL
2990 * NOTE: dx is in average character widths, dy - in lines;
2993 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2995 if (!(es->style & ES_MULTILINE))
2996 return FALSE;
2998 dx *= es->char_width;
2999 return EDIT_EM_LineScroll_internal(es, dx, dy);
3002 /*********************************************************************
3004 * EDIT_EM_LineScroll_internal
3006 * Version of EDIT_EM_LineScroll for internal use.
3007 * It doesn't refuse if ES_MULTILINE is set and assumes that
3008 * dx is in pixels, dy - in lines.
3011 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3013 INT nyoff;
3014 INT x_offset_in_pixels;
3015 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3016 es->line_height;
3018 if (es->style & ES_MULTILINE)
3020 x_offset_in_pixels = es->x_offset;
3022 else
3024 dy = 0;
3025 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3028 if (-dx > x_offset_in_pixels)
3029 dx = -x_offset_in_pixels;
3030 if (dx > es->text_width - x_offset_in_pixels)
3031 dx = es->text_width - x_offset_in_pixels;
3032 nyoff = max(0, es->y_offset + dy);
3033 if (nyoff >= es->line_count - lines_per_page)
3034 nyoff = max(0, es->line_count - lines_per_page);
3035 dy = (es->y_offset - nyoff) * es->line_height;
3036 if (dx || dy) {
3037 RECT rc1;
3038 RECT rc;
3040 es->y_offset = nyoff;
3041 if(es->style & ES_MULTILINE)
3042 es->x_offset += dx;
3043 else
3044 es->x_offset += dx / es->char_width;
3046 GetClientRect(es->hwndSelf, &rc1);
3047 IntersectRect(&rc, &rc1, &es->format_rect);
3048 ScrollWindowEx(es->hwndSelf, -dx, dy,
3049 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3050 /* force scroll info update */
3051 EDIT_UpdateScrollInfo(es);
3053 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3054 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3055 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3056 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3057 return TRUE;
3061 /*********************************************************************
3063 * EM_POSFROMCHAR
3066 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3068 INT len = get_text_length(es);
3069 INT l;
3070 INT li;
3071 INT x;
3072 INT y = 0;
3073 INT w;
3074 INT lw = 0;
3075 INT ll = 0;
3076 HDC dc;
3077 HFONT old_font = 0;
3078 SIZE size;
3079 LINEDEF *line_def;
3081 index = min(index, len);
3082 dc = GetDC(es->hwndSelf);
3083 if (es->font)
3084 old_font = SelectObject(dc, es->font);
3085 if (es->style & ES_MULTILINE) {
3086 l = EDIT_EM_LineFromChar(es, index);
3087 y = (l - es->y_offset) * es->line_height;
3088 li = EDIT_EM_LineIndex(es, l);
3089 if (after_wrap && (li == index) && l) {
3090 INT l2 = l - 1;
3091 line_def = es->first_line_def;
3092 while (l2) {
3093 line_def = line_def->next;
3094 l2--;
3096 if (line_def->ending == END_WRAP) {
3097 l--;
3098 y -= es->line_height;
3099 li = EDIT_EM_LineIndex(es, l);
3103 line_def = es->first_line_def;
3104 while (line_def->index != li)
3105 line_def = line_def->next;
3107 ll = line_def->net_length;
3108 lw = line_def->width;
3110 w = es->format_rect.right - es->format_rect.left;
3111 if (es->style & ES_RIGHT)
3113 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3114 es->tabs_count, es->tabs)) - es->x_offset;
3115 x = w - x;
3117 else if (es->style & ES_CENTER)
3119 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3120 es->tabs_count, es->tabs)) - es->x_offset;
3121 x += (w - lw) / 2;
3123 else /* ES_LEFT */
3125 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3126 es->tabs_count, es->tabs)) - es->x_offset;
3128 } else {
3129 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3130 if (index < es->x_offset) {
3131 GetTextExtentPoint32W(dc, text + index,
3132 es->x_offset - index, &size);
3133 x = -size.cx;
3134 } else {
3135 GetTextExtentPoint32W(dc, text + es->x_offset,
3136 index - es->x_offset, &size);
3137 x = size.cx;
3139 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3141 w = es->format_rect.right - es->format_rect.left;
3142 if (w > es->text_width)
3144 if (es->style & ES_RIGHT)
3145 x += w - es->text_width;
3146 else if (es->style & ES_CENTER)
3147 x += (w - es->text_width) / 2;
3151 y = 0;
3152 if (es->style & ES_PASSWORD)
3153 HeapFree(GetProcessHeap(), 0, text);
3155 x += es->format_rect.left;
3156 y += es->format_rect.top;
3157 if (es->font)
3158 SelectObject(dc, old_font);
3159 ReleaseDC(es->hwndSelf, dc);
3160 return MAKELONG((INT16)x, (INT16)y);
3164 /*********************************************************************
3166 * EM_REPLACESEL
3168 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3171 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3173 UINT strl = strlenW(lpsz_replace);
3174 UINT tl = get_text_length(es);
3175 UINT utl;
3176 UINT s;
3177 UINT e;
3178 UINT i;
3179 UINT size;
3180 LPWSTR p;
3181 HRGN hrgn = 0;
3182 LPWSTR buf = NULL;
3183 UINT bufl = 0;
3185 TRACE("%s, can_undo %d, send_update %d\n",
3186 debugstr_w(lpsz_replace), can_undo, send_update);
3188 s = es->selection_start;
3189 e = es->selection_end;
3191 if ((s == e) && !strl)
3192 return;
3194 ORDER_UINT(s, e);
3196 size = tl - (e - s) + strl;
3197 if (!size)
3198 es->text_width = 0;
3200 /* Issue the EN_MAXTEXT notification and continue with replacing text
3201 * such that buffer limit is honored. */
3202 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3203 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3204 strl = es->buffer_limit - (tl - (e-s));
3207 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3208 return;
3210 if (e != s) {
3211 /* there is something to be deleted */
3212 TRACE("deleting stuff.\n");
3213 bufl = e - s;
3214 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3215 if (!buf) return;
3216 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3217 buf[bufl] = 0; /* ensure 0 termination */
3218 /* now delete */
3219 strcpyW(es->text + s, es->text + e);
3220 text_buffer_changed(es);
3222 if (strl) {
3223 /* there is an insertion */
3224 tl = get_text_length(es);
3225 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));
3226 for (p = es->text + tl ; p >= es->text + s ; p--)
3227 p[strl] = p[0];
3228 for (i = 0 , p = es->text + s ; i < strl ; i++)
3229 p[i] = lpsz_replace[i];
3230 if(es->style & ES_UPPERCASE)
3231 CharUpperBuffW(p, strl);
3232 else if(es->style & ES_LOWERCASE)
3233 CharLowerBuffW(p, strl);
3234 text_buffer_changed(es);
3236 if (es->style & ES_MULTILINE)
3238 INT st = min(es->selection_start, es->selection_end);
3239 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3241 hrgn = CreateRectRgn(0, 0, 0, 0);
3242 EDIT_BuildLineDefs_ML(es, st, st + strl,
3243 strl - abs(es->selection_end - es->selection_start), hrgn);
3244 /* if text is too long undo all changes */
3245 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3246 if (strl)
3247 strcpyW(es->text + e, es->text + e + strl);
3248 if (e != s)
3249 for (i = 0 , p = es->text ; i < e - s ; i++)
3250 p[i + s] = buf[i];
3251 text_buffer_changed(es);
3252 EDIT_BuildLineDefs_ML(es, s, e,
3253 abs(es->selection_end - es->selection_start) - strl, hrgn);
3254 strl = 0;
3255 e = s;
3256 hrgn = CreateRectRgn(0, 0, 0, 0);
3257 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3260 else {
3261 INT fw = es->format_rect.right - es->format_rect.left;
3262 EDIT_CalcLineWidth_SL(es);
3263 /* remove chars that don't fit */
3264 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3265 while ((es->text_width > fw) && s + strl >= s) {
3266 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3267 strl--;
3268 EDIT_CalcLineWidth_SL(es);
3270 text_buffer_changed(es);
3271 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3275 if (e != s) {
3276 if (can_undo) {
3277 utl = strlenW(es->undo_text);
3278 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3279 /* undo-buffer is extended to the right */
3280 EDIT_MakeUndoFit(es, utl + e - s);
3281 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3282 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3283 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3284 /* undo-buffer is extended to the left */
3285 EDIT_MakeUndoFit(es, utl + e - s);
3286 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3287 p[e - s] = p[0];
3288 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3289 p[i] = buf[i];
3290 es->undo_position = s;
3291 } else {
3292 /* new undo-buffer */
3293 EDIT_MakeUndoFit(es, e - s);
3294 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3295 es->undo_text[e - s] = 0; /* ensure 0 termination */
3296 es->undo_position = s;
3298 /* any deletion makes the old insertion-undo invalid */
3299 es->undo_insert_count = 0;
3300 } else
3301 EDIT_EM_EmptyUndoBuffer(es);
3303 if (strl) {
3304 if (can_undo) {
3305 if ((s == es->undo_position) ||
3306 ((es->undo_insert_count) &&
3307 (s == es->undo_position + es->undo_insert_count)))
3309 * insertion is new and at delete position or
3310 * an extension to either left or right
3312 es->undo_insert_count += strl;
3313 else {
3314 /* new insertion undo */
3315 es->undo_position = s;
3316 es->undo_insert_count = strl;
3317 /* new insertion makes old delete-buffer invalid */
3318 *es->undo_text = '\0';
3320 } else
3321 EDIT_EM_EmptyUndoBuffer(es);
3324 if (bufl)
3325 HeapFree(GetProcessHeap(), 0, buf);
3327 s += strl;
3329 /* If text has been deleted and we're right or center aligned then scroll rightward */
3330 if (es->style & (ES_RIGHT | ES_CENTER))
3332 INT delta = strl - abs(es->selection_end - es->selection_start);
3334 if (delta < 0 && es->x_offset)
3336 if (abs(delta) > es->x_offset)
3337 es->x_offset = 0;
3338 else
3339 es->x_offset += delta;
3343 EDIT_EM_SetSel(es, s, s, FALSE);
3344 es->flags |= EF_MODIFIED;
3345 if (send_update) es->flags |= EF_UPDATE;
3346 if (hrgn)
3348 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3349 DeleteObject(hrgn);
3351 else
3352 EDIT_UpdateText(es, NULL, TRUE);
3354 EDIT_EM_ScrollCaret(es);
3356 /* force scroll info update */
3357 EDIT_UpdateScrollInfo(es);
3360 if(send_update || (es->flags & EF_UPDATE))
3362 es->flags &= ~EF_UPDATE;
3363 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3368 /*********************************************************************
3370 * EM_SCROLL
3373 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3375 INT dy;
3377 if (!(es->style & ES_MULTILINE))
3378 return (LRESULT)FALSE;
3380 dy = 0;
3382 switch (action) {
3383 case SB_LINEUP:
3384 if (es->y_offset)
3385 dy = -1;
3386 break;
3387 case SB_LINEDOWN:
3388 if (es->y_offset < es->line_count - 1)
3389 dy = 1;
3390 break;
3391 case SB_PAGEUP:
3392 if (es->y_offset)
3393 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3394 break;
3395 case SB_PAGEDOWN:
3396 if (es->y_offset < es->line_count - 1)
3397 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3398 break;
3399 default:
3400 return (LRESULT)FALSE;
3402 if (dy) {
3403 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3404 /* check if we are going to move too far */
3405 if(es->y_offset + dy > es->line_count - vlc)
3406 dy = es->line_count - vlc - es->y_offset;
3408 /* Notification is done in EDIT_EM_LineScroll */
3409 if(dy)
3410 EDIT_EM_LineScroll(es, 0, dy);
3412 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3416 /*********************************************************************
3418 * EM_SCROLLCARET
3421 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3423 if (es->style & ES_MULTILINE) {
3424 INT l;
3425 INT li;
3426 INT vlc;
3427 INT ww;
3428 INT cw = es->char_width;
3429 INT x;
3430 INT dy = 0;
3431 INT dx = 0;
3433 l = EDIT_EM_LineFromChar(es, es->selection_end);
3434 li = EDIT_EM_LineIndex(es, l);
3435 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3436 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3437 if (l >= es->y_offset + vlc)
3438 dy = l - vlc + 1 - es->y_offset;
3439 if (l < es->y_offset)
3440 dy = l - es->y_offset;
3441 ww = es->format_rect.right - es->format_rect.left;
3442 if (x < es->format_rect.left)
3443 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3444 if (x > es->format_rect.right)
3445 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3446 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3448 /* check if we are going to move too far */
3449 if(es->x_offset + dx + ww > es->text_width)
3450 dx = es->text_width - ww - es->x_offset;
3451 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3452 EDIT_EM_LineScroll_internal(es, dx, dy);
3454 } else {
3455 INT x;
3456 INT goal;
3457 INT format_width;
3459 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3460 format_width = es->format_rect.right - es->format_rect.left;
3461 if (x < es->format_rect.left) {
3462 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3463 do {
3464 es->x_offset--;
3465 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3466 } while ((x < goal) && es->x_offset);
3467 /* FIXME: use ScrollWindow() somehow to improve performance */
3468 EDIT_UpdateText(es, NULL, TRUE);
3469 } else if (x > es->format_rect.right) {
3470 INT x_last;
3471 INT len = get_text_length(es);
3472 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3473 do {
3474 es->x_offset++;
3475 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3476 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3477 } while ((x > goal) && (x_last > es->format_rect.right));
3478 /* FIXME: use ScrollWindow() somehow to improve performance */
3479 EDIT_UpdateText(es, NULL, TRUE);
3483 if(es->flags & EF_FOCUSED)
3484 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3488 /*********************************************************************
3490 * EM_SETHANDLE
3492 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3495 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3497 if (!(es->style & ES_MULTILINE))
3498 return;
3500 if (!hloc) {
3501 WARN("called with NULL handle\n");
3502 return;
3505 EDIT_UnlockBuffer(es, TRUE);
3507 if(es->hloc16)
3509 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3510 HANDLE16 oldDS = stack16->ds;
3512 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3513 LocalFree16(es->hloc16);
3514 stack16->ds = oldDS;
3515 es->hloc16 = 0;
3518 if(es->is_unicode)
3520 if(es->hloc32A)
3522 LocalFree(es->hloc32A);
3523 es->hloc32A = NULL;
3525 es->hloc32W = hloc;
3527 else
3529 INT countW, countA;
3530 HLOCAL hloc32W_new;
3531 WCHAR *textW;
3532 CHAR *textA;
3534 countA = LocalSize(hloc);
3535 textA = LocalLock(hloc);
3536 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3537 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3539 ERR("Could not allocate new unicode buffer\n");
3540 return;
3542 textW = LocalLock(hloc32W_new);
3543 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3544 LocalUnlock(hloc32W_new);
3545 LocalUnlock(hloc);
3547 if(es->hloc32W)
3548 LocalFree(es->hloc32W);
3550 es->hloc32W = hloc32W_new;
3551 es->hloc32A = hloc;
3554 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3556 es->flags |= EF_APP_HAS_HANDLE;
3557 EDIT_LockBuffer(es);
3559 es->x_offset = es->y_offset = 0;
3560 es->selection_start = es->selection_end = 0;
3561 EDIT_EM_EmptyUndoBuffer(es);
3562 es->flags &= ~EF_MODIFIED;
3563 es->flags &= ~EF_UPDATE;
3564 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3565 EDIT_UpdateText(es, NULL, TRUE);
3566 EDIT_EM_ScrollCaret(es);
3567 /* force scroll info update */
3568 EDIT_UpdateScrollInfo(es);
3572 /*********************************************************************
3574 * EM_SETHANDLE16
3576 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3579 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3581 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3582 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3583 HANDLE16 oldDS = stack16->ds;
3584 INT countW, countA;
3585 HLOCAL hloc32W_new;
3586 WCHAR *textW;
3587 CHAR *textA;
3589 if (!(es->style & ES_MULTILINE))
3590 return;
3592 if (!hloc) {
3593 WARN("called with NULL handle\n");
3594 return;
3597 EDIT_UnlockBuffer(es, TRUE);
3599 if(es->hloc32A)
3601 LocalFree(es->hloc32A);
3602 es->hloc32A = NULL;
3605 stack16->ds = hInstance;
3606 countA = LocalSize16(hloc);
3607 textA = MapSL(LocalLock16(hloc));
3608 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3609 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3611 ERR("Could not allocate new unicode buffer\n");
3612 return;
3614 textW = LocalLock(hloc32W_new);
3615 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3616 LocalUnlock(hloc32W_new);
3617 LocalUnlock16(hloc);
3618 stack16->ds = oldDS;
3620 if(es->hloc32W)
3621 LocalFree(es->hloc32W);
3623 es->hloc32W = hloc32W_new;
3624 es->hloc16 = hloc;
3626 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3628 es->flags |= EF_APP_HAS_HANDLE;
3629 EDIT_LockBuffer(es);
3631 es->x_offset = es->y_offset = 0;
3632 es->selection_start = es->selection_end = 0;
3633 EDIT_EM_EmptyUndoBuffer(es);
3634 es->flags &= ~EF_MODIFIED;
3635 es->flags &= ~EF_UPDATE;
3636 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3637 EDIT_UpdateText(es, NULL, TRUE);
3638 EDIT_EM_ScrollCaret(es);
3639 /* force scroll info update */
3640 EDIT_UpdateScrollInfo(es);
3644 /*********************************************************************
3646 * EM_SETLIMITTEXT
3648 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3649 * However, the windows version is not complied to yet in all of edit.c
3651 * Additionally as the wrapper for RichEdit controls we need larger buffers
3652 * at present -1 will represent nolimit
3654 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3656 if (limit == 0xFFFFFFFF)
3657 es->buffer_limit = -1;
3658 else if (es->style & ES_MULTILINE) {
3659 if (limit)
3660 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3661 else
3662 es->buffer_limit = BUFLIMIT_MULTI;
3663 } else {
3664 if (limit)
3665 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3666 else
3667 es->buffer_limit = BUFLIMIT_SINGLE;
3672 /*********************************************************************
3674 * EM_SETMARGINS
3676 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3677 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3678 * margin according to the textmetrics of the current font.
3680 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3681 * of the char's width as the margin, but this is not how Windows handles this.
3682 * For all other fonts Windows sets the margins to zero.
3684 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3685 * edit control is equal to or larger than a certain size.
3686 * Interestingly if one subtracts both the left and right margins from
3687 * this size one always seems to get an even number. The extents of
3688 * the (four character) string "'**'" match this quite closely, so
3689 * we'll use this until we come up with a better idea.
3691 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3693 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3694 SIZE sz;
3696 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3697 return sz.cx + left + right;
3700 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3701 WORD left, WORD right, BOOL repaint)
3703 TEXTMETRICW tm;
3704 INT default_left_margin = 0; /* in pixels */
3705 INT default_right_margin = 0; /* in pixels */
3707 /* Set the default margins depending on the font */
3708 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3709 HDC dc = GetDC(es->hwndSelf);
3710 HFONT old_font = SelectObject(dc, es->font);
3711 GetTextMetricsW(dc, &tm);
3712 /* The default margins are only non zero for TrueType or Vector fonts */
3713 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3714 int min_size;
3715 RECT rc;
3716 /* This must be calculated more exactly! But how? */
3717 default_left_margin = tm.tmAveCharWidth / 2;
3718 default_right_margin = tm.tmAveCharWidth / 2;
3719 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3720 GetClientRect(es->hwndSelf, &rc);
3721 if(rc.right - rc.left < min_size) {
3722 default_left_margin = es->left_margin;
3723 default_right_margin = es->right_margin;
3726 SelectObject(dc, old_font);
3727 ReleaseDC(es->hwndSelf, dc);
3730 if (action & EC_LEFTMARGIN) {
3731 es->format_rect.left -= es->left_margin;
3732 if (left != EC_USEFONTINFO)
3733 es->left_margin = left;
3734 else
3735 es->left_margin = default_left_margin;
3736 es->format_rect.left += es->left_margin;
3739 if (action & EC_RIGHTMARGIN) {
3740 es->format_rect.right += es->right_margin;
3741 if (right != EC_USEFONTINFO)
3742 es->right_margin = right;
3743 else
3744 es->right_margin = default_right_margin;
3745 es->format_rect.right -= es->right_margin;
3748 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3749 EDIT_AdjustFormatRect(es);
3750 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3753 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3757 /*********************************************************************
3759 * EM_SETPASSWORDCHAR
3762 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3764 LONG style;
3766 if (es->style & ES_MULTILINE)
3767 return;
3769 if (es->password_char == c)
3770 return;
3772 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3773 es->password_char = c;
3774 if (c) {
3775 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3776 es->style |= ES_PASSWORD;
3777 } else {
3778 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3779 es->style &= ~ES_PASSWORD;
3781 EDIT_UpdateText(es, NULL, TRUE);
3785 /*********************************************************************
3787 * EDIT_EM_SetSel
3789 * note: unlike the specs say: the order of start and end
3790 * _is_ preserved in Windows. (i.e. start can be > end)
3791 * In other words: this handler is OK
3794 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3796 UINT old_start = es->selection_start;
3797 UINT old_end = es->selection_end;
3798 UINT len = get_text_length(es);
3800 if (start == (UINT)-1) {
3801 start = es->selection_end;
3802 end = es->selection_end;
3803 } else {
3804 start = min(start, len);
3805 end = min(end, len);
3807 es->selection_start = start;
3808 es->selection_end = end;
3809 if (after_wrap)
3810 es->flags |= EF_AFTER_WRAP;
3811 else
3812 es->flags &= ~EF_AFTER_WRAP;
3813 /* Compute the necessary invalidation region. */
3814 /* Note that we don't need to invalidate regions which have
3815 * "never" been selected, or those which are "still" selected.
3816 * In fact, every time we hit a selection boundary, we can
3817 * *toggle* whether we need to invalidate. Thus we can optimize by
3818 * *sorting* the interval endpoints. Let's assume that we sort them
3819 * in this order:
3820 * start <= end <= old_start <= old_end
3821 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3822 * in 5 comparisons; ie it's impossible to do better than the
3823 * following: */
3824 ORDER_UINT(end, old_end);
3825 ORDER_UINT(start, old_start);
3826 ORDER_UINT(old_start, old_end);
3827 ORDER_UINT(start, end);
3828 /* Note that at this point 'end' and 'old_start' are not in order, but
3829 * start is definitely the min. and old_end is definitely the max. */
3830 if (end != old_start)
3833 * One can also do
3834 * ORDER_UINT32(end, old_start);
3835 * EDIT_InvalidateText(es, start, end);
3836 * EDIT_InvalidateText(es, old_start, old_end);
3837 * in place of the following if statement.
3838 * (That would complete the optimal five-comparison four-element sort.)
3840 if (old_start > end )
3842 EDIT_InvalidateText(es, start, end);
3843 EDIT_InvalidateText(es, old_start, old_end);
3845 else
3847 EDIT_InvalidateText(es, start, old_start);
3848 EDIT_InvalidateText(es, end, old_end);
3851 else EDIT_InvalidateText(es, start, old_end);
3855 /*********************************************************************
3857 * EM_SETTABSTOPS
3860 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3862 if (!(es->style & ES_MULTILINE))
3863 return FALSE;
3864 HeapFree(GetProcessHeap(), 0, es->tabs);
3865 es->tabs_count = count;
3866 if (!count)
3867 es->tabs = NULL;
3868 else {
3869 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3870 memcpy(es->tabs, tabs, count * sizeof(INT));
3872 return TRUE;
3876 /*********************************************************************
3878 * EM_SETTABSTOPS16
3881 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3883 if (!(es->style & ES_MULTILINE))
3884 return FALSE;
3885 HeapFree(GetProcessHeap(), 0, es->tabs);
3886 es->tabs_count = count;
3887 if (!count)
3888 es->tabs = NULL;
3889 else {
3890 INT i;
3891 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3892 for (i = 0 ; i < count ; i++)
3893 es->tabs[i] = *tabs++;
3895 return TRUE;
3899 /*********************************************************************
3901 * EM_SETWORDBREAKPROC
3904 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3906 if (es->word_break_proc == wbp)
3907 return;
3909 es->word_break_proc = wbp;
3910 es->word_break_proc16 = NULL;
3912 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3913 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3914 EDIT_UpdateText(es, NULL, TRUE);
3919 /*********************************************************************
3921 * EM_SETWORDBREAKPROC16
3924 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3926 if (es->word_break_proc16 == wbp)
3927 return;
3929 es->word_break_proc = NULL;
3930 es->word_break_proc16 = wbp;
3931 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3932 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3933 EDIT_UpdateText(es, NULL, TRUE);
3938 /*********************************************************************
3940 * EM_UNDO / WM_UNDO
3943 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3945 INT ulength;
3946 LPWSTR utext;
3948 /* As per MSDN spec, for a single-line edit control,
3949 the return value is always TRUE */
3950 if( es->style & ES_READONLY )
3951 return !(es->style & ES_MULTILINE);
3953 ulength = strlenW(es->undo_text);
3955 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3957 strcpyW(utext, es->undo_text);
3959 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3960 es->undo_insert_count, debugstr_w(utext));
3962 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3963 EDIT_EM_EmptyUndoBuffer(es);
3964 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3965 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3966 /* send the notification after the selection start and end are set */
3967 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3968 EDIT_EM_ScrollCaret(es);
3969 HeapFree(GetProcessHeap(), 0, utext);
3971 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3972 es->undo_insert_count, debugstr_w(es->undo_text));
3973 return TRUE;
3977 /*********************************************************************
3979 * WM_CHAR
3982 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3984 BOOL control;
3986 control = GetKeyState(VK_CONTROL) & 0x8000;
3988 switch (c) {
3989 case '\r':
3990 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3991 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3992 break;
3993 case '\n':
3994 if (es->style & ES_MULTILINE) {
3995 if (es->style & ES_READONLY) {
3996 EDIT_MoveHome(es, FALSE);
3997 EDIT_MoveDown_ML(es, FALSE);
3998 } else {
3999 static const WCHAR cr_lfW[] = {'\r','\n',0};
4000 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
4003 break;
4004 case '\t':
4005 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4007 static const WCHAR tabW[] = {'\t',0};
4008 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4010 break;
4011 case VK_BACK:
4012 if (!(es->style & ES_READONLY) && !control) {
4013 if (es->selection_start != es->selection_end)
4014 EDIT_WM_Clear(es);
4015 else {
4016 /* delete character left of caret */
4017 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4018 EDIT_MoveBackward(es, TRUE);
4019 EDIT_WM_Clear(es);
4022 break;
4023 case 0x03: /* ^C */
4024 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4025 break;
4026 case 0x16: /* ^V */
4027 if (!(es->style & ES_READONLY))
4028 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4029 break;
4030 case 0x18: /* ^X */
4031 if (!(es->style & ES_READONLY))
4032 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4033 break;
4035 default:
4036 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4037 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4038 break;
4040 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4041 WCHAR str[2];
4042 str[0] = c;
4043 str[1] = '\0';
4044 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4046 break;
4051 /*********************************************************************
4053 * WM_COMMAND
4056 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4058 if (code || control)
4059 return;
4061 switch (id) {
4062 case EM_UNDO:
4063 EDIT_EM_Undo(es);
4064 break;
4065 case WM_CUT:
4066 EDIT_WM_Cut(es);
4067 break;
4068 case WM_COPY:
4069 EDIT_WM_Copy(es);
4070 break;
4071 case WM_PASTE:
4072 EDIT_WM_Paste(es);
4073 break;
4074 case WM_CLEAR:
4075 EDIT_WM_Clear(es);
4076 break;
4077 case EM_SETSEL:
4078 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4079 EDIT_EM_ScrollCaret(es);
4080 break;
4081 default:
4082 ERR("unknown menu item, please report\n");
4083 break;
4088 /*********************************************************************
4090 * WM_CONTEXTMENU
4092 * Note: the resource files resource/sysres_??.rc cannot define a
4093 * single popup menu. Hence we use a (dummy) menubar
4094 * containing the single popup menu as its first item.
4096 * FIXME: the message identifiers have been chosen arbitrarily,
4097 * hence we use MF_BYPOSITION.
4098 * We might as well use the "real" values (anybody knows ?)
4099 * The menu definition is in resources/sysres_??.rc.
4100 * Once these are OK, we better use MF_BYCOMMAND here
4101 * (as we do in EDIT_WM_Command()).
4104 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4106 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4107 HMENU popup = GetSubMenu(menu, 0);
4108 UINT start = es->selection_start;
4109 UINT end = es->selection_end;
4111 ORDER_UINT(start, end);
4113 /* undo */
4114 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4115 /* cut */
4116 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4117 /* copy */
4118 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4119 /* paste */
4120 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4121 /* delete */
4122 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4123 /* select all */
4124 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4126 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4128 RECT rc;
4129 /* Windows places the menu at the edit's center in this case */
4130 GetClientRect(es->hwndSelf, &rc);
4131 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4132 x = rc.left + (rc.right - rc.left) / 2;
4133 y = rc.top + (rc.bottom - rc.top) / 2;
4136 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4137 DestroyMenu(menu);
4141 /*********************************************************************
4143 * WM_COPY
4146 static void EDIT_WM_Copy(EDITSTATE *es)
4148 INT s = min(es->selection_start, es->selection_end);
4149 INT e = max(es->selection_start, es->selection_end);
4150 HGLOBAL hdst;
4151 LPWSTR dst;
4152 DWORD len;
4154 if (e == s) return;
4156 len = e - s;
4157 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4158 dst = GlobalLock(hdst);
4159 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4160 dst[len] = 0; /* ensure 0 termination */
4161 TRACE("%s\n", debugstr_w(dst));
4162 GlobalUnlock(hdst);
4163 OpenClipboard(es->hwndSelf);
4164 EmptyClipboard();
4165 SetClipboardData(CF_UNICODETEXT, hdst);
4166 CloseClipboard();
4170 /*********************************************************************
4172 * WM_CREATE
4175 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4177 RECT clientRect;
4179 TRACE("%s\n", debugstr_w(name));
4181 * To initialize some final structure members, we call some helper
4182 * functions. However, since the EDITSTATE is not consistent (i.e.
4183 * not fully initialized), we should be very careful which
4184 * functions can be called, and in what order.
4186 EDIT_WM_SetFont(es, 0, FALSE);
4187 EDIT_EM_EmptyUndoBuffer(es);
4189 /* We need to calculate the format rect
4190 (applications may send EM_SETMARGINS before the control gets visible) */
4191 GetClientRect(es->hwndSelf, &clientRect);
4192 EDIT_SetRectNP(es, &clientRect);
4194 if (name && *name) {
4195 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4196 /* if we insert text to the editline, the text scrolls out
4197 * of the window, as the caret is placed after the insert
4198 * pos normally; thus we reset es->selection... to 0 and
4199 * update caret
4201 es->selection_start = es->selection_end = 0;
4202 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4203 * Messages are only to be sent when the USER does something to
4204 * change the contents. So I am removing this EN_CHANGE
4206 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4208 EDIT_EM_ScrollCaret(es);
4210 /* force scroll info update */
4211 EDIT_UpdateScrollInfo(es);
4212 /* The rule seems to return 1 here for success */
4213 /* Power Builder masked edit controls will crash */
4214 /* if not. */
4215 /* FIXME: is that in all cases so ? */
4216 return 1;
4220 /*********************************************************************
4222 * WM_DESTROY
4225 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4227 LINEDEF *pc, *pp;
4229 if (es->hloc32W) {
4230 while (LocalUnlock(es->hloc32W)) ;
4231 LocalFree(es->hloc32W);
4233 if (es->hloc32A) {
4234 while (LocalUnlock(es->hloc32A)) ;
4235 LocalFree(es->hloc32A);
4237 if (es->hloc16) {
4238 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
4239 HANDLE16 oldDS = stack16->ds;
4241 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4242 while (LocalUnlock16(es->hloc16)) ;
4243 LocalFree16(es->hloc16);
4244 stack16->ds = oldDS;
4247 pc = es->first_line_def;
4248 while (pc)
4250 pp = pc->next;
4251 HeapFree(GetProcessHeap(), 0, pc);
4252 pc = pp;
4255 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4256 HeapFree(GetProcessHeap(), 0, es);
4258 return 0;
4262 /*********************************************************************
4264 * WM_ERASEBKGND
4267 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4269 /* we do the proper erase in EDIT_WM_Paint */
4270 return 1;
4274 /*********************************************************************
4276 * WM_GETTEXT
4279 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4281 if(!count) return 0;
4283 if(unicode)
4285 lstrcpynW(dst, es->text, count);
4286 return strlenW(dst);
4288 else
4290 LPSTR textA = (LPSTR)dst;
4291 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4292 textA[count - 1] = 0; /* ensure 0 termination */
4293 return strlen(textA);
4297 /*********************************************************************
4299 * WM_HSCROLL
4302 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4304 INT dx;
4305 INT fw;
4307 if (!(es->style & ES_MULTILINE))
4308 return 0;
4310 if (!(es->style & ES_AUTOHSCROLL))
4311 return 0;
4313 dx = 0;
4314 fw = es->format_rect.right - es->format_rect.left;
4315 switch (action) {
4316 case SB_LINELEFT:
4317 TRACE("SB_LINELEFT\n");
4318 if (es->x_offset)
4319 dx = -es->char_width;
4320 break;
4321 case SB_LINERIGHT:
4322 TRACE("SB_LINERIGHT\n");
4323 if (es->x_offset < es->text_width)
4324 dx = es->char_width;
4325 break;
4326 case SB_PAGELEFT:
4327 TRACE("SB_PAGELEFT\n");
4328 if (es->x_offset)
4329 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4330 break;
4331 case SB_PAGERIGHT:
4332 TRACE("SB_PAGERIGHT\n");
4333 if (es->x_offset < es->text_width)
4334 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4335 break;
4336 case SB_LEFT:
4337 TRACE("SB_LEFT\n");
4338 if (es->x_offset)
4339 dx = -es->x_offset;
4340 break;
4341 case SB_RIGHT:
4342 TRACE("SB_RIGHT\n");
4343 if (es->x_offset < es->text_width)
4344 dx = es->text_width - es->x_offset;
4345 break;
4346 case SB_THUMBTRACK:
4347 TRACE("SB_THUMBTRACK %d\n", pos);
4348 es->flags |= EF_HSCROLL_TRACK;
4349 if(es->style & WS_HSCROLL)
4350 dx = pos - es->x_offset;
4351 else
4353 INT fw, new_x;
4354 /* Sanity check */
4355 if(pos < 0 || pos > 100) return 0;
4356 /* Assume default scroll range 0-100 */
4357 fw = es->format_rect.right - es->format_rect.left;
4358 new_x = pos * (es->text_width - fw) / 100;
4359 dx = es->text_width ? (new_x - es->x_offset) : 0;
4361 break;
4362 case SB_THUMBPOSITION:
4363 TRACE("SB_THUMBPOSITION %d\n", pos);
4364 es->flags &= ~EF_HSCROLL_TRACK;
4365 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4366 dx = pos - es->x_offset;
4367 else
4369 INT fw, new_x;
4370 /* Sanity check */
4371 if(pos < 0 || pos > 100) return 0;
4372 /* Assume default scroll range 0-100 */
4373 fw = es->format_rect.right - es->format_rect.left;
4374 new_x = pos * (es->text_width - fw) / 100;
4375 dx = es->text_width ? (new_x - es->x_offset) : 0;
4377 if (!dx) {
4378 /* force scroll info update */
4379 EDIT_UpdateScrollInfo(es);
4380 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4382 break;
4383 case SB_ENDSCROLL:
4384 TRACE("SB_ENDSCROLL\n");
4385 break;
4387 * FIXME : the next two are undocumented !
4388 * Are we doing the right thing ?
4389 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4390 * although it's also a regular control message.
4392 case EM_GETTHUMB: /* this one is used by NT notepad */
4393 case EM_GETTHUMB16:
4395 LRESULT ret;
4396 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4397 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4398 else
4400 /* Assume default scroll range 0-100 */
4401 INT fw = es->format_rect.right - es->format_rect.left;
4402 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4404 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4405 return ret;
4407 case EM_LINESCROLL16:
4408 TRACE("EM_LINESCROLL16\n");
4409 dx = pos;
4410 break;
4412 default:
4413 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4414 action, action);
4415 return 0;
4417 if (dx)
4419 INT fw = es->format_rect.right - es->format_rect.left;
4420 /* check if we are going to move too far */
4421 if(es->x_offset + dx + fw > es->text_width)
4422 dx = es->text_width - fw - es->x_offset;
4423 if(dx)
4424 EDIT_EM_LineScroll_internal(es, dx, 0);
4426 return 0;
4430 /*********************************************************************
4432 * EDIT_CheckCombo
4435 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4437 HWND hLBox = es->hwndListBox;
4438 HWND hCombo;
4439 BOOL bDropped;
4440 int nEUI;
4442 if (!hLBox)
4443 return FALSE;
4445 hCombo = GetParent(es->hwndSelf);
4446 bDropped = TRUE;
4447 nEUI = 0;
4449 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4451 if (key == VK_UP || key == VK_DOWN)
4453 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4454 nEUI = 1;
4456 if (msg == WM_KEYDOWN || nEUI)
4457 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4460 switch (msg)
4462 case WM_KEYDOWN:
4463 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4465 /* make sure ComboLBox pops up */
4466 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4467 key = VK_F4;
4468 nEUI = 2;
4471 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4472 break;
4474 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4475 if (nEUI)
4476 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4477 else
4478 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4479 break;
4482 if(nEUI == 2)
4483 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4485 return TRUE;
4489 /*********************************************************************
4491 * WM_KEYDOWN
4493 * Handling of special keys that don't produce a WM_CHAR
4494 * (i.e. non-printable keys) & Backspace & Delete
4497 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4499 BOOL shift;
4500 BOOL control;
4502 if (GetKeyState(VK_MENU) & 0x8000)
4503 return 0;
4505 shift = GetKeyState(VK_SHIFT) & 0x8000;
4506 control = GetKeyState(VK_CONTROL) & 0x8000;
4508 switch (key) {
4509 case VK_F4:
4510 case VK_UP:
4511 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4512 break;
4514 /* fall through */
4515 case VK_LEFT:
4516 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4517 EDIT_MoveUp_ML(es, shift);
4518 else
4519 if (control)
4520 EDIT_MoveWordBackward(es, shift);
4521 else
4522 EDIT_MoveBackward(es, shift);
4523 break;
4524 case VK_DOWN:
4525 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4526 break;
4527 /* fall through */
4528 case VK_RIGHT:
4529 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4530 EDIT_MoveDown_ML(es, shift);
4531 else if (control)
4532 EDIT_MoveWordForward(es, shift);
4533 else
4534 EDIT_MoveForward(es, shift);
4535 break;
4536 case VK_HOME:
4537 EDIT_MoveHome(es, shift);
4538 break;
4539 case VK_END:
4540 EDIT_MoveEnd(es, shift);
4541 break;
4542 case VK_PRIOR:
4543 if (es->style & ES_MULTILINE)
4544 EDIT_MovePageUp_ML(es, shift);
4545 else
4546 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4547 break;
4548 case VK_NEXT:
4549 if (es->style & ES_MULTILINE)
4550 EDIT_MovePageDown_ML(es, shift);
4551 else
4552 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4553 break;
4554 case VK_DELETE:
4555 if (!(es->style & ES_READONLY) && !(shift && control)) {
4556 if (es->selection_start != es->selection_end) {
4557 if (shift)
4558 EDIT_WM_Cut(es);
4559 else
4560 EDIT_WM_Clear(es);
4561 } else {
4562 if (shift) {
4563 /* delete character left of caret */
4564 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4565 EDIT_MoveBackward(es, TRUE);
4566 EDIT_WM_Clear(es);
4567 } else if (control) {
4568 /* delete to end of line */
4569 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4570 EDIT_MoveEnd(es, TRUE);
4571 EDIT_WM_Clear(es);
4572 } else {
4573 /* delete character right of caret */
4574 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4575 EDIT_MoveForward(es, TRUE);
4576 EDIT_WM_Clear(es);
4580 break;
4581 case VK_INSERT:
4582 if (shift) {
4583 if (!(es->style & ES_READONLY))
4584 EDIT_WM_Paste(es);
4585 } else if (control)
4586 EDIT_WM_Copy(es);
4587 break;
4588 case VK_RETURN:
4589 /* If the edit doesn't want the return send a message to the default object */
4590 if(!(es->style & ES_WANTRETURN))
4592 HWND hwndParent = GetParent(es->hwndSelf);
4593 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4594 if (HIWORD(dw) == DC_HASDEFID)
4596 SendMessageW( hwndParent, WM_COMMAND,
4597 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4598 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4601 break;
4603 return 0;
4607 /*********************************************************************
4609 * WM_KILLFOCUS
4612 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4614 es->flags &= ~EF_FOCUSED;
4615 DestroyCaret();
4616 if(!(es->style & ES_NOHIDESEL))
4617 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4618 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4619 return 0;
4623 /*********************************************************************
4625 * WM_LBUTTONDBLCLK
4627 * The caret position has been set on the WM_LBUTTONDOWN message
4630 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4632 INT s;
4633 INT e = es->selection_end;
4634 INT l;
4635 INT li;
4636 INT ll;
4638 es->bCaptureState = TRUE;
4639 SetCapture(es->hwndSelf);
4641 l = EDIT_EM_LineFromChar(es, e);
4642 li = EDIT_EM_LineIndex(es, l);
4643 ll = EDIT_EM_LineLength(es, e);
4644 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4645 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4646 EDIT_EM_SetSel(es, s, e, FALSE);
4647 EDIT_EM_ScrollCaret(es);
4648 es->region_posx = es->region_posy = 0;
4649 SetTimer(es->hwndSelf, 0, 100, NULL);
4650 return 0;
4654 /*********************************************************************
4656 * WM_LBUTTONDOWN
4659 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4661 INT e;
4662 BOOL after_wrap;
4664 es->bCaptureState = TRUE;
4665 SetCapture(es->hwndSelf);
4666 EDIT_ConfinePoint(es, &x, &y);
4667 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4668 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4669 EDIT_EM_ScrollCaret(es);
4670 es->region_posx = es->region_posy = 0;
4671 SetTimer(es->hwndSelf, 0, 100, NULL);
4673 if (!(es->flags & EF_FOCUSED))
4674 SetFocus(es->hwndSelf);
4676 return 0;
4680 /*********************************************************************
4682 * WM_LBUTTONUP
4685 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4687 if (es->bCaptureState) {
4688 KillTimer(es->hwndSelf, 0);
4689 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4691 es->bCaptureState = FALSE;
4692 return 0;
4696 /*********************************************************************
4698 * WM_MBUTTONDOWN
4701 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4703 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4704 return 0;
4708 /*********************************************************************
4710 * WM_MOUSEMOVE
4713 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4715 INT e;
4716 BOOL after_wrap;
4717 INT prex, prey;
4719 /* If the mouse has been captured by process other than the edit control itself,
4720 * the windows edit controls will not select the strings with mouse move.
4722 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4723 return 0;
4726 * FIXME: gotta do some scrolling if outside client
4727 * area. Maybe reset the timer ?
4729 prex = x; prey = y;
4730 EDIT_ConfinePoint(es, &x, &y);
4731 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4732 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4733 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4734 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4735 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4736 return 0;
4740 /*********************************************************************
4742 * WM_NCCREATE
4744 * See also EDIT_WM_StyleChanged
4746 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4748 EDITSTATE *es;
4749 UINT alloc_size;
4751 TRACE("Creating %s edit control, style = %08x\n",
4752 unicode ? "Unicode" : "ANSI", lpcs->style);
4754 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4755 return FALSE;
4756 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4759 * Note: since the EDITSTATE has not been fully initialized yet,
4760 * we can't use any API calls that may send
4761 * WM_XXX messages before WM_NCCREATE is completed.
4764 es->is_unicode = unicode;
4765 es->style = lpcs->style;
4767 es->bEnableState = !(es->style & WS_DISABLED);
4769 es->hwndSelf = hwnd;
4770 /* Save parent, which will be notified by EN_* messages */
4771 es->hwndParent = lpcs->hwndParent;
4773 if (es->style & ES_COMBO)
4774 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4776 /* Number overrides lowercase overrides uppercase (at least it
4777 * does in Win95). However I'll bet that ES_NUMBER would be
4778 * invalid under Win 3.1.
4780 if (es->style & ES_NUMBER) {
4781 ; /* do not override the ES_NUMBER */
4782 } else if (es->style & ES_LOWERCASE) {
4783 es->style &= ~ES_UPPERCASE;
4785 if (es->style & ES_MULTILINE) {
4786 es->buffer_limit = BUFLIMIT_MULTI;
4787 if (es->style & WS_VSCROLL)
4788 es->style |= ES_AUTOVSCROLL;
4789 if (es->style & WS_HSCROLL)
4790 es->style |= ES_AUTOHSCROLL;
4791 es->style &= ~ES_PASSWORD;
4792 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4793 /* Confirmed - RIGHT overrides CENTER */
4794 if (es->style & ES_RIGHT)
4795 es->style &= ~ES_CENTER;
4796 es->style &= ~WS_HSCROLL;
4797 es->style &= ~ES_AUTOHSCROLL;
4799 } else {
4800 es->buffer_limit = BUFLIMIT_SINGLE;
4801 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4802 es->style &= ~ES_CENTER;
4803 es->style &= ~WS_HSCROLL;
4804 es->style &= ~WS_VSCROLL;
4805 if (es->style & ES_PASSWORD)
4806 es->password_char = '*';
4809 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4810 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4811 return FALSE;
4812 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4814 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4815 return FALSE;
4816 es->undo_buffer_size = es->buffer_size;
4818 if (es->style & ES_MULTILINE)
4819 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4820 return FALSE;
4821 es->line_count = 1;
4824 * In Win95 look and feel, the WS_BORDER style is replaced by the
4825 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4826 * control a nonclient area so we don't need to draw the border.
4827 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4828 * a nonclient area and we should handle painting the border ourselves.
4830 * When making modifications please ensure that the code still works
4831 * for edit controls created directly with style 0x50800000, exStyle 0
4832 * (which should have a single pixel border)
4834 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4835 es->style &= ~WS_BORDER;
4836 else if (es->style & WS_BORDER)
4837 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4839 return TRUE;
4842 /*********************************************************************
4844 * WM_PAINT
4847 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4849 PAINTSTRUCT ps;
4850 INT i;
4851 HDC dc;
4852 HFONT old_font = 0;
4853 RECT rc;
4854 RECT rcClient;
4855 RECT rcLine;
4856 RECT rcRgn;
4857 HBRUSH brush;
4858 HBRUSH old_brush;
4859 INT bw, bh;
4860 BOOL rev = es->bEnableState &&
4861 ((es->flags & EF_FOCUSED) ||
4862 (es->style & ES_NOHIDESEL));
4863 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4865 GetClientRect(es->hwndSelf, &rcClient);
4867 /* get the background brush */
4868 brush = EDIT_NotifyCtlColor(es, dc);
4870 /* paint the border and the background */
4871 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4873 if(es->style & WS_BORDER) {
4874 bw = GetSystemMetrics(SM_CXBORDER);
4875 bh = GetSystemMetrics(SM_CYBORDER);
4876 rc = rcClient;
4877 if(es->style & ES_MULTILINE) {
4878 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4879 if(es->style & WS_VSCROLL) rc.right+=bw;
4882 /* Draw the frame. Same code as in nonclient.c */
4883 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4884 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4885 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4886 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4887 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4888 SelectObject(dc, old_brush);
4890 /* Keep the border clean */
4891 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4892 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4895 GetClipBox(dc, &rc);
4896 FillRect(dc, &rc, brush);
4898 IntersectClipRect(dc, es->format_rect.left,
4899 es->format_rect.top,
4900 es->format_rect.right,
4901 es->format_rect.bottom);
4902 if (es->style & ES_MULTILINE) {
4903 rc = rcClient;
4904 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4906 if (es->font)
4907 old_font = SelectObject(dc, es->font);
4909 if (!es->bEnableState)
4910 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4911 GetClipBox(dc, &rcRgn);
4912 if (es->style & ES_MULTILINE) {
4913 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4914 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4915 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4916 if (IntersectRect(&rc, &rcRgn, &rcLine))
4917 EDIT_PaintLine(es, dc, i, rev);
4919 } else {
4920 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4921 if (IntersectRect(&rc, &rcRgn, &rcLine))
4922 EDIT_PaintLine(es, dc, 0, rev);
4924 if (es->font)
4925 SelectObject(dc, old_font);
4927 if (!hdc)
4928 EndPaint(es->hwndSelf, &ps);
4932 /*********************************************************************
4934 * WM_PASTE
4937 static void EDIT_WM_Paste(EDITSTATE *es)
4939 HGLOBAL hsrc;
4940 LPWSTR src;
4942 /* Protect read-only edit control from modification */
4943 if(es->style & ES_READONLY)
4944 return;
4946 OpenClipboard(es->hwndSelf);
4947 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4948 src = (LPWSTR)GlobalLock(hsrc);
4949 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4950 GlobalUnlock(hsrc);
4952 CloseClipboard();
4956 /*********************************************************************
4958 * WM_SETFOCUS
4961 static void EDIT_WM_SetFocus(EDITSTATE *es)
4963 es->flags |= EF_FOCUSED;
4965 if (!(es->style & ES_NOHIDESEL))
4966 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4968 /* single line edit updates itself */
4969 if (!(es->style & ES_MULTILINE))
4971 HDC hdc = GetDC(es->hwndSelf);
4972 EDIT_WM_Paint(es, hdc);
4973 ReleaseDC(es->hwndSelf, hdc);
4976 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4977 EDIT_SetCaretPos(es, es->selection_end,
4978 es->flags & EF_AFTER_WRAP);
4979 ShowCaret(es->hwndSelf);
4980 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
4984 /*********************************************************************
4986 * WM_SETFONT
4988 * With Win95 look the margins are set to default font value unless
4989 * the system font (font == 0) is being set, in which case they are left
4990 * unchanged.
4993 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4995 TEXTMETRICW tm;
4996 HDC dc;
4997 HFONT old_font = 0;
4998 RECT clientRect;
5000 es->font = font;
5001 dc = GetDC(es->hwndSelf);
5002 if (font)
5003 old_font = SelectObject(dc, font);
5004 GetTextMetricsW(dc, &tm);
5005 es->line_height = tm.tmHeight;
5006 es->char_width = tm.tmAveCharWidth;
5007 if (font)
5008 SelectObject(dc, old_font);
5009 ReleaseDC(es->hwndSelf, dc);
5011 /* Reset the format rect and the margins */
5012 GetClientRect(es->hwndSelf, &clientRect);
5013 EDIT_SetRectNP(es, &clientRect);
5014 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5015 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5017 if (es->style & ES_MULTILINE)
5018 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5019 else
5020 EDIT_CalcLineWidth_SL(es);
5022 if (redraw)
5023 EDIT_UpdateText(es, NULL, TRUE);
5024 if (es->flags & EF_FOCUSED) {
5025 DestroyCaret();
5026 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5027 EDIT_SetCaretPos(es, es->selection_end,
5028 es->flags & EF_AFTER_WRAP);
5029 ShowCaret(es->hwndSelf);
5034 /*********************************************************************
5036 * WM_SETTEXT
5038 * NOTES
5039 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5040 * The modified flag is reset. No notifications are sent.
5042 * For single-line controls, reception of WM_SETTEXT triggers:
5043 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5046 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5048 LPWSTR textW = NULL;
5049 if (!unicode && text)
5051 LPCSTR textA = (LPCSTR)text;
5052 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5053 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5054 if (textW)
5055 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5056 text = textW;
5059 if (es->flags & EF_UPDATE)
5060 /* fixed this bug once; complain if we see it about to happen again. */
5061 ERR("SetSel may generate UPDATE message whose handler may reset "
5062 "selection.\n");
5064 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5065 if (text)
5067 TRACE("%s\n", debugstr_w(text));
5068 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5069 if(!unicode)
5070 HeapFree(GetProcessHeap(), 0, textW);
5072 else
5074 static const WCHAR empty_stringW[] = {0};
5075 TRACE("<NULL>\n");
5076 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5078 es->x_offset = 0;
5079 es->flags &= ~EF_MODIFIED;
5080 EDIT_EM_SetSel(es, 0, 0, FALSE);
5082 /* Send the notification after the selection start and end have been set
5083 * edit control doesn't send notification on WM_SETTEXT
5084 * if it is multiline, or it is part of combobox
5086 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5088 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5089 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5091 EDIT_EM_ScrollCaret(es);
5092 EDIT_UpdateScrollInfo(es);
5096 /*********************************************************************
5098 * WM_SIZE
5101 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5103 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5104 RECT rc;
5105 TRACE("width = %d, height = %d\n", width, height);
5106 SetRect(&rc, 0, 0, width, height);
5107 EDIT_SetRectNP(es, &rc);
5108 EDIT_UpdateText(es, NULL, TRUE);
5113 /*********************************************************************
5115 * WM_STYLECHANGED
5117 * This message is sent by SetWindowLong on having changed either the Style
5118 * or the extended style.
5120 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5122 * See also EDIT_WM_NCCreate
5124 * It appears that the Windows version of the edit control allows the style
5125 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5126 * style variable which will generally be different. In this function we
5127 * update the internal style based on what changed in the externally visible
5128 * style.
5130 * Much of this content as based upon the MSDN, especially:
5131 * Platform SDK Documentation -> User Interface Services ->
5132 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5133 * Edit Control Styles
5135 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5137 if (GWL_STYLE == which) {
5138 DWORD style_change_mask;
5139 DWORD new_style;
5140 /* Only a subset of changes can be applied after the control
5141 * has been created.
5143 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5144 ES_NUMBER;
5145 if (es->style & ES_MULTILINE)
5146 style_change_mask |= ES_WANTRETURN;
5148 new_style = style->styleNew & style_change_mask;
5150 /* Number overrides lowercase overrides uppercase (at least it
5151 * does in Win95). However I'll bet that ES_NUMBER would be
5152 * invalid under Win 3.1.
5154 if (new_style & ES_NUMBER) {
5155 ; /* do not override the ES_NUMBER */
5156 } else if (new_style & ES_LOWERCASE) {
5157 new_style &= ~ES_UPPERCASE;
5160 es->style = (es->style & ~style_change_mask) | new_style;
5161 } else if (GWL_EXSTYLE == which) {
5162 ; /* FIXME - what is needed here */
5163 } else {
5164 WARN ("Invalid style change %d\n",which);
5167 return 0;
5170 /*********************************************************************
5172 * WM_SYSKEYDOWN
5175 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5177 if ((key == VK_BACK) && (key_data & 0x2000)) {
5178 if (EDIT_EM_CanUndo(es))
5179 EDIT_EM_Undo(es);
5180 return 0;
5181 } else if (key == VK_UP || key == VK_DOWN) {
5182 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5183 return 0;
5185 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5189 /*********************************************************************
5191 * WM_TIMER
5194 static void EDIT_WM_Timer(EDITSTATE *es)
5196 if (es->region_posx < 0) {
5197 EDIT_MoveBackward(es, TRUE);
5198 } else if (es->region_posx > 0) {
5199 EDIT_MoveForward(es, TRUE);
5202 * FIXME: gotta do some vertical scrolling here, like
5203 * EDIT_EM_LineScroll(hwnd, 0, 1);
5207 /*********************************************************************
5209 * WM_VSCROLL
5212 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5214 INT dy;
5216 if (!(es->style & ES_MULTILINE))
5217 return 0;
5219 if (!(es->style & ES_AUTOVSCROLL))
5220 return 0;
5222 dy = 0;
5223 switch (action) {
5224 case SB_LINEUP:
5225 case SB_LINEDOWN:
5226 case SB_PAGEUP:
5227 case SB_PAGEDOWN:
5228 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5229 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5230 (action == SB_PAGEUP ? "SB_PAGEUP" :
5231 "SB_PAGEDOWN"))));
5232 EDIT_EM_Scroll(es, action);
5233 return 0;
5234 case SB_TOP:
5235 TRACE("SB_TOP\n");
5236 dy = -es->y_offset;
5237 break;
5238 case SB_BOTTOM:
5239 TRACE("SB_BOTTOM\n");
5240 dy = es->line_count - 1 - es->y_offset;
5241 break;
5242 case SB_THUMBTRACK:
5243 TRACE("SB_THUMBTRACK %d\n", pos);
5244 es->flags |= EF_VSCROLL_TRACK;
5245 if(es->style & WS_VSCROLL)
5246 dy = pos - es->y_offset;
5247 else
5249 /* Assume default scroll range 0-100 */
5250 INT vlc, new_y;
5251 /* Sanity check */
5252 if(pos < 0 || pos > 100) return 0;
5253 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5254 new_y = pos * (es->line_count - vlc) / 100;
5255 dy = es->line_count ? (new_y - es->y_offset) : 0;
5256 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5257 es->line_count, es->y_offset, pos, dy);
5259 break;
5260 case SB_THUMBPOSITION:
5261 TRACE("SB_THUMBPOSITION %d\n", pos);
5262 es->flags &= ~EF_VSCROLL_TRACK;
5263 if(es->style & WS_VSCROLL)
5264 dy = pos - es->y_offset;
5265 else
5267 /* Assume default scroll range 0-100 */
5268 INT vlc, new_y;
5269 /* Sanity check */
5270 if(pos < 0 || pos > 100) return 0;
5271 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5272 new_y = pos * (es->line_count - vlc) / 100;
5273 dy = es->line_count ? (new_y - es->y_offset) : 0;
5274 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5275 es->line_count, es->y_offset, pos, dy);
5277 if (!dy)
5279 /* force scroll info update */
5280 EDIT_UpdateScrollInfo(es);
5281 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5283 break;
5284 case SB_ENDSCROLL:
5285 TRACE("SB_ENDSCROLL\n");
5286 break;
5288 * FIXME : the next two are undocumented !
5289 * Are we doing the right thing ?
5290 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5291 * although it's also a regular control message.
5293 case EM_GETTHUMB: /* this one is used by NT notepad */
5294 case EM_GETTHUMB16:
5296 LRESULT ret;
5297 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5298 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5299 else
5301 /* Assume default scroll range 0-100 */
5302 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5303 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5305 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5306 return ret;
5308 case EM_LINESCROLL16:
5309 TRACE("EM_LINESCROLL16 %d\n", pos);
5310 dy = pos;
5311 break;
5313 default:
5314 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5315 action, action);
5316 return 0;
5318 if (dy)
5319 EDIT_EM_LineScroll(es, 0, dy);
5320 return 0;
5323 /*********************************************************************
5325 * EDIT_UpdateText
5328 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5330 if (es->flags & EF_UPDATE) {
5331 es->flags &= ~EF_UPDATE;
5332 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5334 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5338 /*********************************************************************
5340 * EDIT_UpdateText
5343 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5345 if (es->flags & EF_UPDATE) {
5346 es->flags &= ~EF_UPDATE;
5347 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5349 InvalidateRect(es->hwndSelf, rc, bErase);
5352 /********************************************************************
5354 * The Following code is to handle inline editing from IMEs
5357 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5359 DWORD dwBufLen;
5360 LPWSTR lpCompStr = NULL;
5361 HIMC hIMC;
5362 LPSTR lpCompStrAttr = NULL;
5363 DWORD dwBufLenAttr;
5365 if (!(hIMC = ImmGetContext(hwnd)))
5366 return;
5368 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5370 if (dwBufLen < 0)
5372 ImmReleaseContext(hwnd, hIMC);
5373 return;
5376 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
5377 if (!lpCompStr)
5379 ERR("Unable to allocate IME CompositionString\n");
5380 ImmReleaseContext(hwnd,hIMC);
5381 return;
5384 if (dwBufLen)
5385 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
5386 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
5388 if (CompFlag & GCS_COMPATTR)
5391 * We do not use the attributes yet. it would tell us what characters
5392 * are in transition and which are converted or decided upon
5394 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5395 if (dwBufLenAttr)
5397 dwBufLenAttr ++;
5398 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5399 if (!lpCompStrAttr)
5401 ERR("Unable to allocate IME Attribute String\n");
5402 HeapFree(GetProcessHeap(),0,lpCompStr);
5403 ImmReleaseContext(hwnd,hIMC);
5404 return;
5406 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5407 dwBufLenAttr);
5408 lpCompStrAttr[dwBufLenAttr] = 0;
5410 else
5411 lpCompStrAttr = NULL;
5414 /* check for change in composition start */
5415 if (es->selection_end < es->composition_start)
5416 es->composition_start = es->selection_end;
5418 /* replace existing selection string */
5419 es->selection_start = es->composition_start;
5421 if (es->composition_len > 0)
5422 es->selection_end = es->composition_start + es->composition_len;
5423 else
5424 es->selection_end = es->selection_start;
5426 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5427 es->composition_len = abs(es->composition_start - es->selection_end);
5429 es->selection_start = es->composition_start;
5430 es->selection_end = es->selection_start + es->composition_len;
5432 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5433 HeapFree(GetProcessHeap(),0,lpCompStr);
5434 ImmReleaseContext(hwnd,hIMC);
5437 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5439 DWORD dwBufLen;
5440 LPWSTR lpResultStr;
5441 HIMC hIMC;
5443 if ( !(hIMC = ImmGetContext(hwnd)))
5444 return;
5446 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5447 if (dwBufLen <= 0)
5449 ImmReleaseContext(hwnd, hIMC);
5450 return;
5453 lpResultStr = HeapAlloc(GetProcessHeap(),0, dwBufLen+sizeof(WCHAR));
5454 if (!lpResultStr)
5456 ERR("Unable to alloc buffer for IME string\n");
5457 ImmReleaseContext(hwnd, hIMC);
5458 return;
5461 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, dwBufLen);
5462 lpResultStr[dwBufLen/sizeof(WCHAR)] = 0;
5464 /* check for change in composition start */
5465 if (es->selection_end < es->composition_start)
5466 es->composition_start = es->selection_end;
5468 es->selection_start = es->composition_start;
5469 es->selection_end = es->composition_start + es->composition_len;
5470 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5471 es->composition_start = es->selection_end;
5472 es->composition_len = 0;
5474 HeapFree(GetProcessHeap(),0,lpResultStr);
5475 ImmReleaseContext(hwnd, hIMC);
5478 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5480 if (CompFlag & GCS_RESULTSTR)
5481 EDIT_GetResultStr(hwnd,es);
5482 if (CompFlag & GCS_COMPSTR)
5483 EDIT_GetCompositionStr(hwnd, CompFlag, es);