user32: Factor in EDIT_WM_EraseBkGnd().
[wine/multimedia.git] / dlls / user32 / edit.c
blob7f9f24ea05512d55546088e0251c627b916fbc57
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTES
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_OEMCONVERT
45 #include "config.h"
47 #include <stdarg.h>
48 #include <string.h>
49 #include <stdlib.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnt.h"
54 #include "wownt32.h"
55 #include "win.h"
56 #include "imm.h"
57 #include "wine/winbase16.h"
58 #include "wine/winuser16.h"
59 #include "wine/unicode.h"
60 #include "controls.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
69 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
70 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
71 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
74 * extra flags for EDITSTATE.flags field
76 #define EF_MODIFIED 0x0001 /* text has been modified */
77 #define EF_FOCUSED 0x0002 /* we have input focus */
78 #define EF_UPDATE 0x0004 /* notify parent of changed state */
79 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
80 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
82 wrapped line, instead of in front of the next character */
83 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
84 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
85 the text buffer if this is clear. */
86 typedef enum
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
93 } LINE_END;
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
98 LINE_END ending;
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
102 } LINEDEF;
104 typedef struct
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
109 UINT buffer_size; /* the size of the buffer in characters */
110 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
111 HFONT font; /* NULL means standard system font */
112 INT x_offset; /* scroll offset for multi lines this is in pixels
113 for single lines it's in characters */
114 INT line_height; /* height of a screen line in pixels */
115 INT char_width; /* average character width in pixels */
116 DWORD style; /* sane version of wnd->dwStyle */
117 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
118 INT undo_insert_count; /* number of characters inserted in sequence */
119 UINT undo_position; /* character index of the insertion and deletion */
120 LPWSTR undo_text; /* deleted text */
121 UINT undo_buffer_size; /* size of the deleted text buffer */
122 INT selection_start; /* == selection_end if no selection */
123 INT selection_end; /* == current caret position */
124 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
125 INT left_margin; /* in pixels */
126 INT right_margin; /* in pixels */
127 RECT format_rect;
128 INT text_width; /* width of the widest line in pixels for multi line controls
129 and just line width for single line controls */
130 INT region_posx; /* Position of cursor relative to region: */
131 INT region_posy; /* -1: to left, 0: within, 1: to right */
132 EDITWORDBREAKPROC16 word_break_proc16;
133 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
134 INT line_count; /* number of lines */
135 INT y_offset; /* scroll offset in number of lines */
136 BOOL bCaptureState; /* flag indicating whether mouse was captured */
137 BOOL bEnableState; /* flag keeping the enable state */
138 HWND hwndSelf; /* the our window handle */
139 HWND hwndParent; /* Handle of parent for sending EN_* messages.
140 Even if parent will change, EN_* messages
141 should be sent to the first parent. */
142 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
144 * only for multi line controls
146 INT lock_count; /* amount of re-entries in the EditWndProc */
147 INT tabs_count;
148 LPINT tabs;
149 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
150 HLOCAL hloc32W; /* our unicode local memory block */
151 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
152 or EM_SETHANDLE16 */
153 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
154 or EM_SETHANDLE */
156 * IME Data
158 UINT composition_len; /* length of composition, 0 == no composition */
159 int composition_start; /* the character position for the composition */
160 } EDITSTATE;
163 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
164 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
166 /* used for disabled or read-only edit control */
167 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
168 do \
169 { /* Notify parent which has created this edit control */ \
170 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
171 SendMessageW(es->hwndParent, WM_COMMAND, \
172 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
173 (LPARAM)(es->hwndSelf)); \
174 } while(0)
176 /*********************************************************************
178 * Declarations
183 * These functions have trivial implementations
184 * We still like to call them internally
185 * "static inline" makes them more like macro's
187 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es);
188 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
189 static inline void EDIT_WM_Clear(EDITSTATE *es);
190 static inline void EDIT_WM_Cut(EDITSTATE *es);
193 * Helper functions only valid for one type of control
195 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
196 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
197 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
198 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
199 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
200 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
201 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
203 * Helper functions valid for both single line _and_ multi line controls
205 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
206 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
207 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y);
208 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
209 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
210 static void EDIT_LockBuffer(EDITSTATE *es);
211 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
212 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
213 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
214 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
215 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
216 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
217 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
218 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
219 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
220 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
221 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
222 static void EDIT_AdjustFormatRect(EDITSTATE *es);
223 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *lprc);
224 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
225 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
226 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
228 * EM_XXX message handlers
230 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
231 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
232 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
233 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
234 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
235 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end);
236 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
237 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
238 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line);
239 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
240 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
241 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
242 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
243 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
244 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
245 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
246 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
247 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
248 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit);
249 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, WORD left, WORD right, BOOL repaint);
250 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
251 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
252 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs);
253 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs);
254 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
255 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
256 static BOOL EDIT_EM_Undo(EDITSTATE *es);
258 * WM_XXX message handlers
260 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
261 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
262 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
263 static void EDIT_WM_Copy(EDITSTATE *es);
264 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
265 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
266 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
267 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
268 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
269 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
270 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
271 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
272 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
273 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
274 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
275 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
276 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
277 static void EDIT_WM_Paste(EDITSTATE *es);
278 static void EDIT_WM_SetFocus(EDITSTATE *es);
279 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
280 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
281 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
282 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
283 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
284 static void EDIT_WM_Timer(EDITSTATE *es);
285 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
286 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase);
287 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
288 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
290 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
291 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
293 /*********************************************************************
294 * edit class descriptor
296 static const WCHAR editW[] = {'E','d','i','t',0};
297 const struct builtin_class_descr EDIT_builtin_class =
299 editW, /* name */
300 CS_DBLCLKS | CS_PARENTDC, /* style */
301 EditWndProcA, /* procA */
302 EditWndProcW, /* procW */
303 sizeof(EDITSTATE *), /* extra */
304 IDC_IBEAM, /* cursor */
305 0 /* brush */
309 /*********************************************************************
311 * EM_CANUNDO
314 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
316 return (es->undo_insert_count || strlenW(es->undo_text));
320 /*********************************************************************
322 * EM_EMPTYUNDOBUFFER
325 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
327 es->undo_insert_count = 0;
328 *es->undo_text = '\0';
332 /*********************************************************************
334 * WM_CLEAR
337 static inline void EDIT_WM_Clear(EDITSTATE *es)
339 static const WCHAR empty_stringW[] = {0};
341 /* Protect read-only edit control from modification */
342 if(es->style & ES_READONLY)
343 return;
345 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
349 /*********************************************************************
351 * WM_CUT
354 static inline void EDIT_WM_Cut(EDITSTATE *es)
356 EDIT_WM_Copy(es);
357 EDIT_WM_Clear(es);
361 /**********************************************************************
362 * get_app_version
364 * Returns the window version in case Wine emulates a later version
365 * of windows than the application expects.
367 * In a number of cases when windows runs an application that was
368 * designed for an earlier windows version, windows reverts
369 * to "old" behaviour of that earlier version.
371 * An example is a disabled edit control that needs to be painted.
372 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
373 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
374 * applications with an expected version 0f 4.0 or higher.
377 static DWORD get_app_version(void)
379 static DWORD version;
380 if (!version)
382 DWORD dwEmulatedVersion;
383 OSVERSIONINFOW info;
384 DWORD dwProcVersion = GetProcessVersion(0);
386 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
387 GetVersionExW( &info );
388 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
389 /* FIXME: this may not be 100% correct; see discussion on the
390 * wine developer list in Nov 1999 */
391 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
393 return version;
396 static inline UINT get_text_length(EDITSTATE *es)
398 if(es->text_length == (UINT)-1)
399 es->text_length = strlenW(es->text);
400 return es->text_length;
403 static inline void text_buffer_changed(EDITSTATE *es)
405 es->text_length = (UINT)-1;
408 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
410 HBRUSH hbrush;
411 UINT msg;
413 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
414 msg = WM_CTLCOLORSTATIC;
415 else
416 msg = WM_CTLCOLOREDIT;
418 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
419 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
420 if (!hbrush)
421 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
422 return hbrush;
425 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
427 if(unicode)
428 return DefWindowProcW(hwnd, msg, wParam, lParam);
429 else
430 return DefWindowProcA(hwnd, msg, wParam, lParam);
433 /*********************************************************************
435 * EditWndProc_common
437 * The messages are in the order of the actual integer values
438 * (which can be found in include/windows.h)
439 * Wherever possible the 16 bit versions are converted to
440 * the 32 bit ones, so that we can 'fall through' to the
441 * helper functions. These are mostly 32 bit (with a few
442 * exceptions, clearly indicated by a '16' extension to their
443 * names).
446 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
447 WPARAM wParam, LPARAM lParam, BOOL unicode )
449 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
450 LRESULT result = 0;
452 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
454 if (!es && msg != WM_NCCREATE)
455 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
457 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
459 switch (msg) {
460 case EM_GETSEL16:
461 wParam = 0;
462 lParam = 0;
463 /* fall through */
464 case EM_GETSEL:
465 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
466 break;
468 case EM_SETSEL16:
469 if ((short)LOWORD(lParam) == -1)
470 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
471 else
472 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
473 if (!wParam)
474 EDIT_EM_ScrollCaret(es);
475 result = 1;
476 break;
477 case EM_SETSEL:
478 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
479 EDIT_EM_ScrollCaret(es);
480 result = 1;
481 break;
483 case EM_GETRECT16:
484 if (lParam)
486 RECT16 *r16 = MapSL(lParam);
487 r16->left = es->format_rect.left;
488 r16->top = es->format_rect.top;
489 r16->right = es->format_rect.right;
490 r16->bottom = es->format_rect.bottom;
492 break;
493 case EM_GETRECT:
494 if (lParam)
495 CopyRect((LPRECT)lParam, &es->format_rect);
496 break;
498 case EM_SETRECT16:
499 if ((es->style & ES_MULTILINE) && lParam) {
500 RECT rc;
501 RECT16 *r16 = MapSL(lParam);
502 rc.left = r16->left;
503 rc.top = r16->top;
504 rc.right = r16->right;
505 rc.bottom = r16->bottom;
506 EDIT_SetRectNP(es, &rc);
507 EDIT_UpdateText(es, NULL, TRUE);
509 break;
510 case EM_SETRECT:
511 if ((es->style & ES_MULTILINE) && lParam) {
512 EDIT_SetRectNP(es, (LPRECT)lParam);
513 EDIT_UpdateText(es, NULL, TRUE);
515 break;
517 case EM_SETRECTNP16:
518 if ((es->style & ES_MULTILINE) && lParam) {
519 RECT rc;
520 RECT16 *r16 = MapSL(lParam);
521 rc.left = r16->left;
522 rc.top = r16->top;
523 rc.right = r16->right;
524 rc.bottom = r16->bottom;
525 EDIT_SetRectNP(es, &rc);
527 break;
528 case EM_SETRECTNP:
529 if ((es->style & ES_MULTILINE) && lParam)
530 EDIT_SetRectNP(es, (LPRECT)lParam);
531 break;
533 case EM_SCROLL16:
534 case EM_SCROLL:
535 result = EDIT_EM_Scroll(es, (INT)wParam);
536 break;
538 case EM_LINESCROLL16:
539 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
540 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
541 /* fall through */
542 case EM_LINESCROLL:
543 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
544 break;
546 case EM_SCROLLCARET16:
547 case EM_SCROLLCARET:
548 EDIT_EM_ScrollCaret(es);
549 result = 1;
550 break;
552 case EM_GETMODIFY16:
553 case EM_GETMODIFY:
554 result = ((es->flags & EF_MODIFIED) != 0);
555 break;
557 case EM_SETMODIFY16:
558 case EM_SETMODIFY:
559 if (wParam)
560 es->flags |= EF_MODIFIED;
561 else
562 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
563 break;
565 case EM_GETLINECOUNT16:
566 case EM_GETLINECOUNT:
567 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
568 break;
570 case EM_LINEINDEX16:
571 if ((INT16)wParam == -1)
572 wParam = (WPARAM)-1;
573 /* fall through */
574 case EM_LINEINDEX:
575 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
576 break;
578 case EM_SETHANDLE16:
579 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
580 break;
581 case EM_SETHANDLE:
582 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
583 break;
585 case EM_GETHANDLE16:
586 result = (LRESULT)EDIT_EM_GetHandle16(es);
587 break;
588 case EM_GETHANDLE:
589 result = (LRESULT)EDIT_EM_GetHandle(es);
590 break;
592 case EM_GETTHUMB16:
593 case EM_GETTHUMB:
594 result = EDIT_EM_GetThumb(es);
595 break;
597 /* these messages missing from specs */
598 case WM_USER+15:
599 case 0x00bf:
600 case WM_USER+16:
601 case 0x00c0:
602 case WM_USER+19:
603 case 0x00c3:
604 case WM_USER+26:
605 case 0x00ca:
606 FIXME("undocumented message 0x%x, please report\n", msg);
607 result = DefWindowProcW(hwnd, msg, wParam, lParam);
608 break;
610 case EM_LINELENGTH16:
611 case EM_LINELENGTH:
612 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
613 break;
615 case EM_REPLACESEL16:
616 lParam = (LPARAM)MapSL(lParam);
617 unicode = FALSE; /* 16-bit message is always ascii */
618 /* fall through */
619 case EM_REPLACESEL:
621 LPWSTR textW;
623 if(unicode)
624 textW = (LPWSTR)lParam;
625 else
627 LPSTR textA = (LPSTR)lParam;
628 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
629 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
630 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
633 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
634 result = 1;
636 if(!unicode)
637 HeapFree(GetProcessHeap(), 0, textW);
638 break;
641 case EM_GETLINE16:
642 lParam = (LPARAM)MapSL(lParam);
643 unicode = FALSE; /* 16-bit message is always ascii */
644 /* fall through */
645 case EM_GETLINE:
646 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
647 break;
649 case EM_LIMITTEXT16:
650 case EM_SETLIMITTEXT:
651 EDIT_EM_SetLimitText(es, wParam);
652 break;
654 case EM_CANUNDO16:
655 case EM_CANUNDO:
656 result = (LRESULT)EDIT_EM_CanUndo(es);
657 break;
659 case EM_UNDO16:
660 case EM_UNDO:
661 case WM_UNDO:
662 result = (LRESULT)EDIT_EM_Undo(es);
663 break;
665 case EM_FMTLINES16:
666 case EM_FMTLINES:
667 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
668 break;
670 case EM_LINEFROMCHAR16:
671 case EM_LINEFROMCHAR:
672 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
673 break;
675 case EM_SETTABSTOPS16:
676 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
677 break;
678 case EM_SETTABSTOPS:
679 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
680 break;
682 case EM_SETPASSWORDCHAR16:
683 unicode = FALSE; /* 16-bit message is always ascii */
684 /* fall through */
685 case EM_SETPASSWORDCHAR:
687 WCHAR charW = 0;
689 if(unicode)
690 charW = (WCHAR)wParam;
691 else
693 CHAR charA = wParam;
694 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
697 EDIT_EM_SetPasswordChar(es, charW);
698 break;
701 case EM_EMPTYUNDOBUFFER16:
702 case EM_EMPTYUNDOBUFFER:
703 EDIT_EM_EmptyUndoBuffer(es);
704 break;
706 case EM_GETFIRSTVISIBLELINE16:
707 result = es->y_offset;
708 break;
709 case EM_GETFIRSTVISIBLELINE:
710 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
711 break;
713 case EM_SETREADONLY16:
714 case EM_SETREADONLY:
715 if (wParam) {
716 SetWindowLongW( hwnd, GWL_STYLE,
717 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
718 es->style |= ES_READONLY;
719 } else {
720 SetWindowLongW( hwnd, GWL_STYLE,
721 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
722 es->style &= ~ES_READONLY;
724 result = 1;
725 break;
727 case EM_SETWORDBREAKPROC16:
728 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
729 break;
730 case EM_SETWORDBREAKPROC:
731 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
732 break;
734 case EM_GETWORDBREAKPROC16:
735 result = (LRESULT)es->word_break_proc16;
736 break;
737 case EM_GETWORDBREAKPROC:
738 result = (LRESULT)es->word_break_proc;
739 break;
741 case EM_GETPASSWORDCHAR16:
742 unicode = FALSE; /* 16-bit message is always ascii */
743 /* fall through */
744 case EM_GETPASSWORDCHAR:
746 if(unicode)
747 result = es->password_char;
748 else
750 WCHAR charW = es->password_char;
751 CHAR charA = 0;
752 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
753 result = charA;
755 break;
758 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
760 case EM_SETMARGINS:
761 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
762 break;
764 case EM_GETMARGINS:
765 result = MAKELONG(es->left_margin, es->right_margin);
766 break;
768 case EM_GETLIMITTEXT:
769 result = es->buffer_limit;
770 break;
772 case EM_POSFROMCHAR:
773 if ((INT)wParam >= get_text_length(es)) result = -1;
774 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
775 break;
777 case EM_CHARFROMPOS:
778 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
779 break;
781 /* End of the EM_ messages which were in numerical order; what order
782 * are these in? vaguely alphabetical?
785 case WM_NCCREATE:
786 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
787 break;
789 case WM_DESTROY:
790 result = EDIT_WM_Destroy(es);
791 es = NULL;
792 break;
794 case WM_GETDLGCODE:
795 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
797 if (es->style & ES_MULTILINE)
799 result |= DLGC_WANTALLKEYS;
800 break;
803 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
805 int vk = (int)((LPMSG)lParam)->wParam;
807 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
809 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
810 result |= DLGC_WANTMESSAGE;
813 break;
815 case WM_IME_CHAR:
816 if (!unicode)
818 WCHAR charW;
819 CHAR strng[2];
821 strng[0] = wParam >> 8;
822 strng[1] = wParam & 0xff;
823 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
824 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
825 EDIT_WM_Char(es, charW);
826 break;
828 /* fall through */
829 case WM_CHAR:
831 WCHAR charW;
833 if(unicode)
834 charW = wParam;
835 else
837 CHAR charA = wParam;
838 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
841 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
843 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
844 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
845 break;
847 EDIT_WM_Char(es, charW);
848 break;
851 case WM_CLEAR:
852 EDIT_WM_Clear(es);
853 break;
855 case WM_COMMAND:
856 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
857 break;
859 case WM_CONTEXTMENU:
860 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
861 break;
863 case WM_COPY:
864 EDIT_WM_Copy(es);
865 break;
867 case WM_CREATE:
868 if(unicode)
869 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
870 else
872 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
873 LPWSTR nameW = NULL;
874 if(nameA)
876 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
877 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
878 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
880 result = EDIT_WM_Create(es, nameW);
881 HeapFree(GetProcessHeap(), 0, nameW);
883 break;
885 case WM_CUT:
886 EDIT_WM_Cut(es);
887 break;
889 case WM_ENABLE:
890 es->bEnableState = (BOOL) wParam;
891 EDIT_UpdateText(es, NULL, TRUE);
892 break;
894 case WM_ERASEBKGND:
895 /* we do the proper erase in EDIT_WM_Paint */
896 result = 1;
897 break;
899 case WM_GETFONT:
900 result = (LRESULT)es->font;
901 break;
903 case WM_GETTEXT:
904 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
905 break;
907 case WM_GETTEXTLENGTH:
908 if (unicode) result = get_text_length(es);
909 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
910 NULL, 0, NULL, NULL );
911 break;
913 case WM_HSCROLL:
914 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
915 break;
917 case WM_KEYDOWN:
918 result = EDIT_WM_KeyDown(es, (INT)wParam);
919 break;
921 case WM_KILLFOCUS:
922 result = EDIT_WM_KillFocus(es);
923 break;
925 case WM_LBUTTONDBLCLK:
926 result = EDIT_WM_LButtonDblClk(es);
927 break;
929 case WM_LBUTTONDOWN:
930 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
931 break;
933 case WM_LBUTTONUP:
934 result = EDIT_WM_LButtonUp(es);
935 break;
937 case WM_MBUTTONDOWN:
938 result = EDIT_WM_MButtonDown(es);
939 break;
941 case WM_MOUSEMOVE:
942 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
943 break;
945 case WM_PRINTCLIENT:
946 case WM_PAINT:
947 EDIT_WM_Paint(es, (HDC)wParam);
948 break;
950 case WM_PASTE:
951 EDIT_WM_Paste(es);
952 break;
954 case WM_SETFOCUS:
955 EDIT_WM_SetFocus(es);
956 break;
958 case WM_SETFONT:
959 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
960 break;
962 case WM_SETREDRAW:
963 /* FIXME: actually set an internal flag and behave accordingly */
964 break;
966 case WM_SETTEXT:
967 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
968 result = TRUE;
969 break;
971 case WM_SIZE:
972 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
973 break;
975 case WM_STYLECHANGED:
976 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
977 break;
979 case WM_STYLECHANGING:
980 result = 0; /* See EDIT_WM_StyleChanged */
981 break;
983 case WM_SYSKEYDOWN:
984 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
985 break;
987 case WM_TIMER:
988 EDIT_WM_Timer(es);
989 break;
991 case WM_VSCROLL:
992 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
993 break;
995 case WM_MOUSEWHEEL:
997 int gcWheelDelta = 0;
998 UINT pulScrollLines = 3;
999 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1001 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1002 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1003 break;
1005 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1006 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1008 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1009 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1010 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1013 break;
1016 /* IME messages to make the edit control IME aware */
1017 case WM_IME_SETCONTEXT:
1018 break;
1020 case WM_IME_STARTCOMPOSITION:
1022 * FIXME in IME: This message is not always sent like it should be
1024 if (es->selection_start != es->selection_end)
1026 static const WCHAR empty_stringW[] = {0};
1027 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1029 es->composition_start = es->selection_end;
1030 es->composition_len = 0;
1031 break;
1033 case WM_IME_COMPOSITION:
1035 int caret_pos = es->selection_end;
1036 if (es->composition_len == 0)
1038 if (es->selection_start != es->selection_end)
1040 static const WCHAR empty_stringW[] = {0};
1041 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1044 es->composition_start = es->selection_end;
1046 EDIT_ImeComposition(hwnd,lParam,es);
1047 EDIT_SetCaretPos(es, caret_pos, es->flags & EF_AFTER_WRAP);
1048 break;
1051 case WM_IME_ENDCOMPOSITION:
1052 es->composition_len= 0;
1053 break;
1055 case WM_IME_COMPOSITIONFULL:
1056 break;
1058 case WM_IME_SELECT:
1059 break;
1061 case WM_IME_CONTROL:
1062 break;
1064 default:
1065 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1066 break;
1069 if (es) EDIT_UnlockBuffer(es, FALSE);
1071 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1073 return result;
1076 /*********************************************************************
1078 * EditWndProcW (USER32.@)
1080 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1082 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1085 /*********************************************************************
1087 * EditWndProc (USER32.@)
1089 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1091 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1094 /*********************************************************************
1096 * EDIT_BuildLineDefs_ML
1098 * Build linked list of text lines.
1099 * Lines can end with '\0' (last line), a character (if it is wrapped),
1100 * a soft return '\r\r\n' or a hard return '\r\n'
1103 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1105 HDC dc;
1106 HFONT old_font = 0;
1107 LPWSTR current_position, cp;
1108 INT fw;
1109 LINEDEF *current_line;
1110 LINEDEF *previous_line;
1111 LINEDEF *start_line;
1112 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1113 INT line_count = es->line_count;
1114 INT orig_net_length;
1115 RECT rc;
1117 if (istart == iend && delta == 0)
1118 return;
1120 dc = GetDC(es->hwndSelf);
1121 if (es->font)
1122 old_font = SelectObject(dc, es->font);
1124 previous_line = NULL;
1125 current_line = es->first_line_def;
1127 /* Find starting line. istart must lie inside an existing line or
1128 * at the end of buffer */
1129 do {
1130 if (istart < current_line->index + current_line->length ||
1131 current_line->ending == END_0)
1132 break;
1134 previous_line = current_line;
1135 current_line = current_line->next;
1136 line_index++;
1137 } while (current_line);
1139 if (!current_line) /* Error occurred start is not inside previous buffer */
1141 FIXME(" modification occurred outside buffer\n");
1142 ReleaseDC(es->hwndSelf, dc);
1143 return;
1146 /* Remember start of modifications in order to calculate update region */
1147 nstart_line = line_index;
1148 nstart_index = current_line->index;
1150 /* We must start to reformat from the previous line since the modifications
1151 * may have caused the line to wrap upwards. */
1152 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1154 line_index--;
1155 current_line = previous_line;
1157 start_line = current_line;
1159 fw = es->format_rect.right - es->format_rect.left;
1160 current_position = es->text + current_line->index;
1161 do {
1162 if (current_line != start_line)
1164 if (!current_line || current_line->index + delta > current_position - es->text)
1166 /* The buffer has been expanded, create a new line and
1167 insert it into the link list */
1168 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1169 new_line->next = previous_line->next;
1170 previous_line->next = new_line;
1171 current_line = new_line;
1172 es->line_count++;
1174 else if (current_line->index + delta < current_position - es->text)
1176 /* The previous line merged with this line so we delete this extra entry */
1177 previous_line->next = current_line->next;
1178 HeapFree(GetProcessHeap(), 0, current_line);
1179 current_line = previous_line->next;
1180 es->line_count--;
1181 continue;
1183 else /* current_line->index + delta == current_position */
1185 if (current_position - es->text > iend)
1186 break; /* We reached end of line modifications */
1187 /* else recalulate this line */
1191 current_line->index = current_position - es->text;
1192 orig_net_length = current_line->net_length;
1194 /* Find end of line */
1195 cp = current_position;
1196 while (*cp) {
1197 if (*cp == '\n') break;
1198 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1199 break;
1200 cp++;
1203 /* Mark type of line termination */
1204 if (!(*cp)) {
1205 current_line->ending = END_0;
1206 current_line->net_length = strlenW(current_position);
1207 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1208 current_line->ending = END_SOFT;
1209 current_line->net_length = cp - current_position - 1;
1210 } else if (*cp == '\n') {
1211 current_line->ending = END_RICH;
1212 current_line->net_length = cp - current_position;
1213 } else {
1214 current_line->ending = END_HARD;
1215 current_line->net_length = cp - current_position;
1218 /* Calculate line width */
1219 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1220 current_position, current_line->net_length,
1221 es->tabs_count, es->tabs));
1223 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1224 if (!(es->style & ES_AUTOHSCROLL)) {
1225 if (current_line->width > fw) {
1226 INT next = 0;
1227 INT prev;
1228 do {
1229 prev = next;
1230 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1231 prev + 1, current_line->net_length, WB_RIGHT);
1232 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1233 current_position, next, es->tabs_count, es->tabs));
1234 } while (current_line->width <= fw);
1235 if (!prev) { /* Didn't find a line break so force a break */
1236 next = 0;
1237 do {
1238 prev = next;
1239 next++;
1240 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1241 current_position, next, es->tabs_count, es->tabs));
1242 } while (current_line->width <= fw);
1243 if (!prev)
1244 prev = 1;
1247 /* If the first line we are calculating, wrapped before istart, we must
1248 * adjust istart in order for this to be reflected in the update region. */
1249 if (current_line->index == nstart_index && istart > current_line->index + prev)
1250 istart = current_line->index + prev;
1251 /* else if we are updating the previous line before the first line we
1252 * are re-calculating and it expanded */
1253 else if (current_line == start_line &&
1254 current_line->index != nstart_index && orig_net_length < prev)
1256 /* Line expanded due to an upwards line wrap so we must partially include
1257 * previous line in update region */
1258 nstart_line = line_index;
1259 nstart_index = current_line->index;
1260 istart = current_line->index + orig_net_length;
1263 current_line->net_length = prev;
1264 current_line->ending = END_WRAP;
1265 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1266 current_line->net_length, es->tabs_count, es->tabs));
1268 else if (orig_net_length < current_line->net_length &&
1269 current_line == start_line &&
1270 current_line->index != nstart_index) {
1271 /* The previous line expanded but it's still not as wide as the client rect */
1272 /* The expansion is due to an upwards line wrap so we must partially include
1273 it in the update region */
1274 nstart_line = line_index;
1275 nstart_index = current_line->index;
1276 istart = current_line->index + orig_net_length;
1281 /* Adjust length to include line termination */
1282 switch (current_line->ending) {
1283 case END_SOFT:
1284 current_line->length = current_line->net_length + 3;
1285 break;
1286 case END_RICH:
1287 current_line->length = current_line->net_length + 1;
1288 break;
1289 case END_HARD:
1290 current_line->length = current_line->net_length + 2;
1291 break;
1292 case END_WRAP:
1293 case END_0:
1294 current_line->length = current_line->net_length;
1295 break;
1297 es->text_width = max(es->text_width, current_line->width);
1298 current_position += current_line->length;
1299 previous_line = current_line;
1300 current_line = current_line->next;
1301 line_index++;
1302 } while (previous_line->ending != END_0);
1304 /* Finish adjusting line indexes by delta or remove hanging lines */
1305 if (previous_line->ending == END_0)
1307 LINEDEF *pnext = NULL;
1309 previous_line->next = NULL;
1310 while (current_line)
1312 pnext = current_line->next;
1313 HeapFree(GetProcessHeap(), 0, current_line);
1314 current_line = pnext;
1315 es->line_count--;
1318 else if (delta != 0)
1320 while (current_line)
1322 current_line->index += delta;
1323 current_line = current_line->next;
1327 /* Calculate rest of modification rectangle */
1328 if (hrgn)
1330 HRGN tmphrgn;
1332 * We calculate two rectangles. One for the first line which may have
1333 * an indent with respect to the format rect. The other is a format-width
1334 * rectangle that spans the rest of the lines that changed or moved.
1336 rc.top = es->format_rect.top + nstart_line * es->line_height -
1337 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1338 rc.bottom = rc.top + es->line_height;
1339 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1340 rc.left = es->format_rect.left;
1341 else
1342 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1343 es->text + nstart_index, istart - nstart_index,
1344 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1345 rc.right = es->format_rect.right;
1346 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1348 rc.top = rc.bottom;
1349 rc.left = es->format_rect.left;
1350 rc.right = es->format_rect.right;
1352 * If lines were added or removed we must re-paint the remainder of the
1353 * lines since the remaining lines were either shifted up or down.
1355 if (line_count < es->line_count) /* We added lines */
1356 rc.bottom = es->line_count * es->line_height;
1357 else if (line_count > es->line_count) /* We removed lines */
1358 rc.bottom = line_count * es->line_height;
1359 else
1360 rc.bottom = line_index * es->line_height;
1361 rc.bottom += es->format_rect.top;
1362 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1363 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1364 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1365 DeleteObject(tmphrgn);
1368 if (es->font)
1369 SelectObject(dc, old_font);
1371 ReleaseDC(es->hwndSelf, dc);
1374 /*********************************************************************
1376 * EDIT_CalcLineWidth_SL
1379 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1381 SIZE size;
1382 LPWSTR text;
1383 HDC dc;
1384 HFONT old_font = 0;
1386 text = EDIT_GetPasswordPointer_SL(es);
1388 dc = GetDC(es->hwndSelf);
1389 if (es->font)
1390 old_font = SelectObject(dc, es->font);
1392 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1394 if (es->font)
1395 SelectObject(dc, old_font);
1396 ReleaseDC(es->hwndSelf, dc);
1398 if (es->style & ES_PASSWORD)
1399 HeapFree(GetProcessHeap(), 0, text);
1401 es->text_width = size.cx;
1404 /*********************************************************************
1406 * EDIT_CallWordBreakProc
1408 * Call appropriate WordBreakProc (internal or external).
1410 * Note: The "start" argument should always be an index referring
1411 * to es->text. The actual wordbreak proc might be
1412 * 16 bit, so we can't always pass any 32 bit LPSTR.
1413 * Hence we assume that es->text is the buffer that holds
1414 * the string under examination (we can decide this for ourselves).
1417 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1419 INT ret;
1421 if (es->word_break_proc16) {
1422 HGLOBAL16 hglob16;
1423 SEGPTR segptr;
1424 INT countA;
1425 WORD args[5];
1426 DWORD result;
1428 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1429 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1430 segptr = WOWGlobalLock16(hglob16);
1431 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1432 args[4] = SELECTOROF(segptr);
1433 args[3] = OFFSETOF(segptr);
1434 args[2] = index;
1435 args[1] = countA;
1436 args[0] = action;
1437 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1438 ret = LOWORD(result);
1439 GlobalUnlock16(hglob16);
1440 GlobalFree16(hglob16);
1442 else if (es->word_break_proc)
1444 if(es->is_unicode)
1446 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1448 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1449 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1450 ret = wbpW(es->text + start, index, count, action);
1452 else
1454 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1455 INT countA;
1456 CHAR *textA;
1458 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1459 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1460 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1461 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1462 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1463 ret = wbpA(textA, index, countA, action);
1464 HeapFree(GetProcessHeap(), 0, textA);
1467 else
1468 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1470 return ret;
1474 /*********************************************************************
1476 * EDIT_CharFromPos
1478 * Beware: This is not the function called on EM_CHARFROMPOS
1479 * The position _can_ be outside the formatting / client
1480 * rectangle
1481 * The return value is only the character index
1484 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1486 INT index;
1487 HDC dc;
1488 HFONT old_font = 0;
1489 INT x_high = 0, x_low = 0;
1491 if (es->style & ES_MULTILINE) {
1492 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1493 INT line_index = 0;
1494 LINEDEF *line_def = es->first_line_def;
1495 INT low, high;
1496 while ((line > 0) && line_def->next) {
1497 line_index += line_def->length;
1498 line_def = line_def->next;
1499 line--;
1501 x += es->x_offset - es->format_rect.left;
1502 if (es->style & ES_RIGHT)
1503 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1504 else if (es->style & ES_CENTER)
1505 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1506 if (x >= line_def->width) {
1507 if (after_wrap)
1508 *after_wrap = (line_def->ending == END_WRAP);
1509 return line_index + line_def->net_length;
1511 if (x <= 0) {
1512 if (after_wrap)
1513 *after_wrap = FALSE;
1514 return line_index;
1516 dc = GetDC(es->hwndSelf);
1517 if (es->font)
1518 old_font = SelectObject(dc, es->font);
1519 low = line_index;
1520 high = line_index + line_def->net_length + 1;
1521 while (low < high - 1)
1523 INT mid = (low + high) / 2;
1524 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1525 if (x_now > x) {
1526 high = mid;
1527 x_high = x_now;
1528 } else {
1529 low = mid;
1530 x_low = x_now;
1533 if (abs(x_high - x) + 1 <= abs(x_low - x))
1534 index = high;
1535 else
1536 index = low;
1538 if (after_wrap)
1539 *after_wrap = ((index == line_index + line_def->net_length) &&
1540 (line_def->ending == END_WRAP));
1541 } else {
1542 LPWSTR text;
1543 SIZE size;
1544 if (after_wrap)
1545 *after_wrap = FALSE;
1546 x -= es->format_rect.left;
1547 if (!x)
1548 return es->x_offset;
1550 if (!es->x_offset)
1552 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1553 if (es->style & ES_RIGHT)
1554 x -= indent;
1555 else if (es->style & ES_CENTER)
1556 x -= indent / 2;
1559 text = EDIT_GetPasswordPointer_SL(es);
1560 dc = GetDC(es->hwndSelf);
1561 if (es->font)
1562 old_font = SelectObject(dc, es->font);
1563 if (x < 0)
1565 INT low = 0;
1566 INT high = es->x_offset;
1567 while (low < high - 1)
1569 INT mid = (low + high) / 2;
1570 GetTextExtentPoint32W( dc, text + mid,
1571 es->x_offset - mid, &size );
1572 if (size.cx > -x) {
1573 low = mid;
1574 x_low = size.cx;
1575 } else {
1576 high = mid;
1577 x_high = size.cx;
1580 if (abs(x_high + x) <= abs(x_low + x) + 1)
1581 index = high;
1582 else
1583 index = low;
1585 else
1587 INT low = es->x_offset;
1588 INT high = get_text_length(es) + 1;
1589 while (low < high - 1)
1591 INT mid = (low + high) / 2;
1592 GetTextExtentPoint32W( dc, text + es->x_offset,
1593 mid - es->x_offset, &size );
1594 if (size.cx > x) {
1595 high = mid;
1596 x_high = size.cx;
1597 } else {
1598 low = mid;
1599 x_low = size.cx;
1602 if (abs(x_high - x) <= abs(x_low - x) + 1)
1603 index = high;
1604 else
1605 index = low;
1607 if (es->style & ES_PASSWORD)
1608 HeapFree(GetProcessHeap(), 0, text);
1610 if (es->font)
1611 SelectObject(dc, old_font);
1612 ReleaseDC(es->hwndSelf, dc);
1613 return index;
1617 /*********************************************************************
1619 * EDIT_ConfinePoint
1621 * adjusts the point to be within the formatting rectangle
1622 * (so CharFromPos returns the nearest _visible_ character)
1625 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
1627 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1628 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1632 /*********************************************************************
1634 * EDIT_GetLineRect
1636 * Calculates the bounding rectangle for a line from a starting
1637 * column to an ending column.
1640 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1642 INT line_index = EDIT_EM_LineIndex(es, line);
1644 if (es->style & ES_MULTILINE)
1645 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1646 else
1647 rc->top = es->format_rect.top;
1648 rc->bottom = rc->top + es->line_height;
1649 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1650 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1654 /*********************************************************************
1656 * EDIT_GetPasswordPointer_SL
1658 * note: caller should free the (optionally) allocated buffer
1661 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1663 if (es->style & ES_PASSWORD) {
1664 INT len = get_text_length(es);
1665 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1666 text[len] = '\0';
1667 while(len) text[--len] = es->password_char;
1668 return text;
1669 } else
1670 return es->text;
1674 /*********************************************************************
1676 * EDIT_LockBuffer
1678 * This acts as a LocalLock16(), but it locks only once. This way
1679 * you can call it whenever you like, without unlocking.
1681 * Initially the edit control allocates a HLOCAL32 buffer
1682 * (32 bit linear memory handler). However, 16 bit application
1683 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1684 * handler). From that moment on we have to keep using this 16 bit memory
1685 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1686 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1687 * conversion.
1690 static void EDIT_LockBuffer(EDITSTATE *es)
1692 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1693 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1695 if (!es->text) {
1696 CHAR *textA = NULL;
1697 UINT countA = 0;
1698 BOOL _16bit = FALSE;
1700 if(es->hloc32W)
1702 if(es->hloc32A)
1704 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1705 textA = LocalLock(es->hloc32A);
1706 countA = strlen(textA) + 1;
1708 else if(es->hloc16)
1710 HANDLE16 oldDS = stack16->ds;
1711 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1712 stack16->ds = hInstance;
1713 textA = MapSL(LocalLock16(es->hloc16));
1714 stack16->ds = oldDS;
1715 countA = strlen(textA) + 1;
1716 _16bit = TRUE;
1719 else {
1720 ERR("no buffer ... please report\n");
1721 return;
1724 if(textA)
1726 HLOCAL hloc32W_new;
1727 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1728 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1729 if(countW_new > es->buffer_size + 1)
1731 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1732 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1733 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1734 if(hloc32W_new)
1736 es->hloc32W = hloc32W_new;
1737 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1738 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1740 else
1741 WARN("FAILED! Will synchronize partially\n");
1745 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1746 es->text = LocalLock(es->hloc32W);
1748 if(textA)
1750 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1751 if(_16bit)
1753 HANDLE16 oldDS = stack16->ds;
1754 stack16->ds = hInstance;
1755 LocalUnlock16(es->hloc16);
1756 stack16->ds = oldDS;
1758 else
1759 LocalUnlock(es->hloc32A);
1762 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1763 es->lock_count++;
1767 /*********************************************************************
1769 * EDIT_SL_InvalidateText
1771 * Called from EDIT_InvalidateText().
1772 * Does the job for single-line controls only.
1775 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1777 RECT line_rect;
1778 RECT rc;
1780 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1781 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1782 EDIT_UpdateText(es, &rc, TRUE);
1786 /*********************************************************************
1788 * EDIT_ML_InvalidateText
1790 * Called from EDIT_InvalidateText().
1791 * Does the job for multi-line controls only.
1794 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1796 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1797 INT sl = EDIT_EM_LineFromChar(es, start);
1798 INT el = EDIT_EM_LineFromChar(es, end);
1799 INT sc;
1800 INT ec;
1801 RECT rc1;
1802 RECT rcWnd;
1803 RECT rcLine;
1804 RECT rcUpdate;
1805 INT l;
1807 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1808 return;
1810 sc = start - EDIT_EM_LineIndex(es, sl);
1811 ec = end - EDIT_EM_LineIndex(es, el);
1812 if (sl < es->y_offset) {
1813 sl = es->y_offset;
1814 sc = 0;
1816 if (el > es->y_offset + vlc) {
1817 el = es->y_offset + vlc;
1818 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1820 GetClientRect(es->hwndSelf, &rc1);
1821 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1822 if (sl == el) {
1823 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1824 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1825 EDIT_UpdateText(es, &rcUpdate, TRUE);
1826 } else {
1827 EDIT_GetLineRect(es, sl, sc,
1828 EDIT_EM_LineLength(es,
1829 EDIT_EM_LineIndex(es, sl)),
1830 &rcLine);
1831 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1832 EDIT_UpdateText(es, &rcUpdate, TRUE);
1833 for (l = sl + 1 ; l < el ; l++) {
1834 EDIT_GetLineRect(es, l, 0,
1835 EDIT_EM_LineLength(es,
1836 EDIT_EM_LineIndex(es, l)),
1837 &rcLine);
1838 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1839 EDIT_UpdateText(es, &rcUpdate, TRUE);
1841 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1842 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1843 EDIT_UpdateText(es, &rcUpdate, TRUE);
1848 /*********************************************************************
1850 * EDIT_InvalidateText
1852 * Invalidate the text from offset start up to, but not including,
1853 * offset end. Useful for (re)painting the selection.
1854 * Regions outside the linewidth are not invalidated.
1855 * end == -1 means end == TextLength.
1856 * start and end need not be ordered.
1859 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1861 if (end == start)
1862 return;
1864 if (end == -1)
1865 end = get_text_length(es);
1867 if (end < start) {
1868 INT tmp = start;
1869 start = end;
1870 end = tmp;
1873 if (es->style & ES_MULTILINE)
1874 EDIT_ML_InvalidateText(es, start, end);
1875 else
1876 EDIT_SL_InvalidateText(es, start, end);
1880 /*********************************************************************
1882 * EDIT_MakeFit
1884 * Try to fit size + 1 characters in the buffer.
1886 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1888 HLOCAL hNew32W;
1890 if (size <= es->buffer_size)
1891 return TRUE;
1893 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1895 /* Force edit to unlock it's buffer. es->text now NULL */
1896 EDIT_UnlockBuffer(es, TRUE);
1898 if (es->hloc32W) {
1899 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1900 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1901 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1902 es->hloc32W = hNew32W;
1903 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1907 EDIT_LockBuffer(es);
1909 if (es->buffer_size < size) {
1910 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1911 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1912 return FALSE;
1913 } else {
1914 TRACE("We now have %d+1\n", es->buffer_size);
1915 return TRUE;
1920 /*********************************************************************
1922 * EDIT_MakeUndoFit
1924 * Try to fit size + 1 bytes in the undo buffer.
1927 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1929 UINT alloc_size;
1931 if (size <= es->undo_buffer_size)
1932 return TRUE;
1934 TRACE("trying to ReAlloc to %d+1\n", size);
1936 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1937 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1938 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1939 return TRUE;
1941 else
1943 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1944 return FALSE;
1949 /*********************************************************************
1951 * EDIT_MoveBackward
1954 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1956 INT e = es->selection_end;
1958 if (e) {
1959 e--;
1960 if ((es->style & ES_MULTILINE) && e &&
1961 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1962 e--;
1963 if (e && (es->text[e - 1] == '\r'))
1964 e--;
1967 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1968 EDIT_EM_ScrollCaret(es);
1972 /*********************************************************************
1974 * EDIT_MoveDown_ML
1976 * Only for multi line controls
1977 * Move the caret one line down, on a column with the nearest
1978 * x coordinate on the screen (might be a different column).
1981 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1983 INT s = es->selection_start;
1984 INT e = es->selection_end;
1985 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1986 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1987 INT x = (short)LOWORD(pos);
1988 INT y = (short)HIWORD(pos);
1990 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1991 if (!extend)
1992 s = e;
1993 EDIT_EM_SetSel(es, s, e, after_wrap);
1994 EDIT_EM_ScrollCaret(es);
1998 /*********************************************************************
2000 * EDIT_MoveEnd
2003 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
2005 BOOL after_wrap = FALSE;
2006 INT e;
2008 /* Pass a high value in x to make sure of receiving the end of the line */
2009 if (es->style & ES_MULTILINE)
2010 e = EDIT_CharFromPos(es, 0x3fffffff,
2011 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2012 else
2013 e = get_text_length(es);
2014 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2015 EDIT_EM_ScrollCaret(es);
2019 /*********************************************************************
2021 * EDIT_MoveForward
2024 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2026 INT e = es->selection_end;
2028 if (es->text[e]) {
2029 e++;
2030 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2031 if (es->text[e] == '\n')
2032 e++;
2033 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2034 e += 2;
2037 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2038 EDIT_EM_ScrollCaret(es);
2042 /*********************************************************************
2044 * EDIT_MoveHome
2046 * Home key: move to beginning of line.
2049 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2051 INT e;
2053 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2054 if (es->style & ES_MULTILINE)
2055 e = EDIT_CharFromPos(es, -es->x_offset,
2056 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2057 else
2058 e = 0;
2059 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2060 EDIT_EM_ScrollCaret(es);
2064 /*********************************************************************
2066 * EDIT_MovePageDown_ML
2068 * Only for multi line controls
2069 * Move the caret one page down, on a column with the nearest
2070 * x coordinate on the screen (might be a different column).
2073 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2075 INT s = es->selection_start;
2076 INT e = es->selection_end;
2077 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2078 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2079 INT x = (short)LOWORD(pos);
2080 INT y = (short)HIWORD(pos);
2082 e = EDIT_CharFromPos(es, x,
2083 y + (es->format_rect.bottom - es->format_rect.top),
2084 &after_wrap);
2085 if (!extend)
2086 s = e;
2087 EDIT_EM_SetSel(es, s, e, after_wrap);
2088 EDIT_EM_ScrollCaret(es);
2092 /*********************************************************************
2094 * EDIT_MovePageUp_ML
2096 * Only for multi line controls
2097 * Move the caret one page up, on a column with the nearest
2098 * x coordinate on the screen (might be a different column).
2101 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2103 INT s = es->selection_start;
2104 INT e = es->selection_end;
2105 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2106 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2107 INT x = (short)LOWORD(pos);
2108 INT y = (short)HIWORD(pos);
2110 e = EDIT_CharFromPos(es, x,
2111 y - (es->format_rect.bottom - es->format_rect.top),
2112 &after_wrap);
2113 if (!extend)
2114 s = e;
2115 EDIT_EM_SetSel(es, s, e, after_wrap);
2116 EDIT_EM_ScrollCaret(es);
2120 /*********************************************************************
2122 * EDIT_MoveUp_ML
2124 * Only for multi line controls
2125 * Move the caret one line up, on a column with the nearest
2126 * x coordinate on the screen (might be a different column).
2129 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2131 INT s = es->selection_start;
2132 INT e = es->selection_end;
2133 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2134 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2135 INT x = (short)LOWORD(pos);
2136 INT y = (short)HIWORD(pos);
2138 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2139 if (!extend)
2140 s = e;
2141 EDIT_EM_SetSel(es, s, e, after_wrap);
2142 EDIT_EM_ScrollCaret(es);
2146 /*********************************************************************
2148 * EDIT_MoveWordBackward
2151 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2153 INT s = es->selection_start;
2154 INT e = es->selection_end;
2155 INT l;
2156 INT ll;
2157 INT li;
2159 l = EDIT_EM_LineFromChar(es, e);
2160 ll = EDIT_EM_LineLength(es, e);
2161 li = EDIT_EM_LineIndex(es, l);
2162 if (e - li == 0) {
2163 if (l) {
2164 li = EDIT_EM_LineIndex(es, l - 1);
2165 e = li + EDIT_EM_LineLength(es, li);
2167 } else {
2168 e = li + (INT)EDIT_CallWordBreakProc(es,
2169 li, e - li, ll, WB_LEFT);
2171 if (!extend)
2172 s = e;
2173 EDIT_EM_SetSel(es, s, e, FALSE);
2174 EDIT_EM_ScrollCaret(es);
2178 /*********************************************************************
2180 * EDIT_MoveWordForward
2183 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2185 INT s = es->selection_start;
2186 INT e = es->selection_end;
2187 INT l;
2188 INT ll;
2189 INT li;
2191 l = EDIT_EM_LineFromChar(es, e);
2192 ll = EDIT_EM_LineLength(es, e);
2193 li = EDIT_EM_LineIndex(es, l);
2194 if (e - li == ll) {
2195 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2196 e = EDIT_EM_LineIndex(es, l + 1);
2197 } else {
2198 e = li + EDIT_CallWordBreakProc(es,
2199 li, e - li + 1, ll, WB_RIGHT);
2201 if (!extend)
2202 s = e;
2203 EDIT_EM_SetSel(es, s, e, FALSE);
2204 EDIT_EM_ScrollCaret(es);
2208 /*********************************************************************
2210 * EDIT_PaintLine
2213 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2215 INT s = es->selection_start;
2216 INT e = es->selection_end;
2217 INT li;
2218 INT ll;
2219 INT x;
2220 INT y;
2221 LRESULT pos;
2223 if (es->style & ES_MULTILINE) {
2224 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2225 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2226 return;
2227 } else if (line)
2228 return;
2230 TRACE("line=%d\n", line);
2232 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2233 x = (short)LOWORD(pos);
2234 y = (short)HIWORD(pos);
2235 li = EDIT_EM_LineIndex(es, line);
2236 ll = EDIT_EM_LineLength(es, li);
2237 s = min(es->selection_start, es->selection_end);
2238 e = max(es->selection_start, es->selection_end);
2239 s = min(li + ll, max(li, s));
2240 e = min(li + ll, max(li, e));
2241 if (rev && (s != e) &&
2242 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2243 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2244 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2245 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2246 } else
2247 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2251 /*********************************************************************
2253 * EDIT_PaintText
2256 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2258 COLORREF BkColor;
2259 COLORREF TextColor;
2260 LOGFONTW underline_font;
2261 HFONT hUnderline = 0;
2262 HFONT old_font = 0;
2263 INT ret;
2264 INT li;
2265 INT BkMode;
2266 SIZE size;
2268 if (!count)
2269 return 0;
2270 BkMode = GetBkMode(dc);
2271 BkColor = GetBkColor(dc);
2272 TextColor = GetTextColor(dc);
2273 if (rev) {
2274 if (es->composition_len == 0)
2276 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2277 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2278 SetBkMode( dc, OPAQUE);
2280 else
2282 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2283 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2284 underline_font.lfUnderline = TRUE;
2285 hUnderline = CreateFontIndirectW(&underline_font);
2286 old_font = SelectObject(dc,hUnderline);
2289 li = EDIT_EM_LineIndex(es, line);
2290 if (es->style & ES_MULTILINE) {
2291 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2292 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2293 } else {
2294 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2295 TextOutW(dc, x, y, text + li + col, count);
2296 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2297 ret = size.cx;
2298 if (es->style & ES_PASSWORD)
2299 HeapFree(GetProcessHeap(), 0, text);
2301 if (rev) {
2302 if (es->composition_len == 0)
2304 SetBkColor(dc, BkColor);
2305 SetTextColor(dc, TextColor);
2306 SetBkMode( dc, BkMode);
2308 else
2310 if (old_font)
2311 SelectObject(dc,old_font);
2312 if (hUnderline)
2313 DeleteObject(hUnderline);
2316 return ret;
2320 /*********************************************************************
2322 * EDIT_SetCaretPos
2325 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2326 BOOL after_wrap)
2328 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2329 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2330 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2334 /*********************************************************************
2336 * EDIT_AdjustFormatRect
2338 * Adjusts the format rectangle for the current font and the
2339 * current client rectangle.
2342 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2344 RECT ClientRect;
2346 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2347 if (es->style & ES_MULTILINE)
2349 INT fw, vlc, max_x_offset, max_y_offset;
2351 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2352 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2354 /* correct es->x_offset */
2355 fw = es->format_rect.right - es->format_rect.left;
2356 max_x_offset = es->text_width - fw;
2357 if(max_x_offset < 0) max_x_offset = 0;
2358 if(es->x_offset > max_x_offset)
2359 es->x_offset = max_x_offset;
2361 /* correct es->y_offset */
2362 max_y_offset = es->line_count - vlc;
2363 if(max_y_offset < 0) max_y_offset = 0;
2364 if(es->y_offset > max_y_offset)
2365 es->y_offset = max_y_offset;
2367 /* force scroll info update */
2368 EDIT_UpdateScrollInfo(es);
2370 else
2371 /* Windows doesn't care to fix text placement for SL controls */
2372 es->format_rect.bottom = es->format_rect.top + es->line_height;
2374 /* Always stay within the client area */
2375 GetClientRect(es->hwndSelf, &ClientRect);
2376 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2378 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2379 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2381 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2385 /*********************************************************************
2387 * EDIT_SetRectNP
2389 * note: this is not (exactly) the handler called on EM_SETRECTNP
2390 * it is also used to set the rect of a single line control
2393 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2395 LONG_PTR ExStyle;
2396 INT bw, bh;
2397 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2399 CopyRect(&es->format_rect, rc);
2401 if (ExStyle & WS_EX_CLIENTEDGE) {
2402 es->format_rect.left++;
2403 es->format_rect.right--;
2405 if (es->format_rect.bottom - es->format_rect.top
2406 >= es->line_height + 2)
2408 es->format_rect.top++;
2409 es->format_rect.bottom--;
2412 else if (es->style & WS_BORDER) {
2413 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2414 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2415 es->format_rect.left += bw;
2416 es->format_rect.right -= bw;
2417 if (es->format_rect.bottom - es->format_rect.top
2418 >= es->line_height + 2 * bh)
2420 es->format_rect.top += bh;
2421 es->format_rect.bottom -= bh;
2425 es->format_rect.left += es->left_margin;
2426 es->format_rect.right -= es->right_margin;
2427 EDIT_AdjustFormatRect(es);
2431 /*********************************************************************
2433 * EDIT_UnlockBuffer
2436 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2439 /* Edit window might be already destroyed */
2440 if(!IsWindow(es->hwndSelf))
2442 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2443 return;
2446 if (!es->lock_count) {
2447 ERR("lock_count == 0 ... please report\n");
2448 return;
2450 if (!es->text) {
2451 ERR("es->text == 0 ... please report\n");
2452 return;
2455 if (force || (es->lock_count == 1)) {
2456 if (es->hloc32W) {
2457 CHAR *textA = NULL;
2458 UINT countA = 0;
2459 UINT countW = get_text_length(es) + 1;
2460 STACK16FRAME* stack16 = NULL;
2461 HANDLE16 oldDS = 0;
2463 if(es->hloc32A)
2465 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2466 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2467 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2468 countA = LocalSize(es->hloc32A);
2469 if(countA_new > countA)
2471 HLOCAL hloc32A_new;
2472 UINT alloc_size = ROUND_TO_GROW(countA_new);
2473 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2474 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2475 if(hloc32A_new)
2477 es->hloc32A = hloc32A_new;
2478 countA = LocalSize(hloc32A_new);
2479 TRACE("Real new size %d bytes\n", countA);
2481 else
2482 WARN("FAILED! Will synchronize partially\n");
2484 textA = LocalLock(es->hloc32A);
2486 else if(es->hloc16)
2488 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2490 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2491 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2493 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2494 oldDS = stack16->ds;
2495 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2497 countA = LocalSize16(es->hloc16);
2498 if(countA_new > countA)
2500 HLOCAL16 hloc16_new;
2501 UINT alloc_size = ROUND_TO_GROW(countA_new);
2502 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2503 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2504 if(hloc16_new)
2506 es->hloc16 = hloc16_new;
2507 countA = LocalSize16(hloc16_new);
2508 TRACE("Real new size %d bytes\n", countA);
2510 else
2511 WARN("FAILED! Will synchronize partially\n");
2513 textA = MapSL(LocalLock16(es->hloc16));
2516 if(textA)
2518 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2519 if(stack16)
2520 LocalUnlock16(es->hloc16);
2521 else
2522 LocalUnlock(es->hloc32A);
2525 if (stack16) stack16->ds = oldDS;
2526 LocalUnlock(es->hloc32W);
2527 es->text = NULL;
2529 else {
2530 ERR("no buffer ... please report\n");
2531 return;
2534 es->lock_count--;
2538 /*********************************************************************
2540 * EDIT_UpdateScrollInfo
2543 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2545 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2547 SCROLLINFO si;
2548 si.cbSize = sizeof(SCROLLINFO);
2549 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2550 si.nMin = 0;
2551 si.nMax = es->line_count - 1;
2552 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2553 si.nPos = es->y_offset;
2554 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2555 si.nMin, si.nMax, si.nPage, si.nPos);
2556 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2559 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2561 SCROLLINFO si;
2562 si.cbSize = sizeof(SCROLLINFO);
2563 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2564 si.nMin = 0;
2565 si.nMax = es->text_width - 1;
2566 si.nPage = es->format_rect.right - es->format_rect.left;
2567 si.nPos = es->x_offset;
2568 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2569 si.nMin, si.nMax, si.nPage, si.nPos);
2570 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2574 /*********************************************************************
2576 * EDIT_WordBreakProc
2578 * Find the beginning of words.
2579 * Note: unlike the specs for a WordBreakProc, this function only
2580 * allows to be called without linebreaks between s[0] up to
2581 * s[count - 1]. Remember it is only called
2582 * internally, so we can decide this for ourselves.
2585 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2587 INT ret = 0;
2589 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2591 if(!s) return 0;
2593 switch (action) {
2594 case WB_LEFT:
2595 if (!count)
2596 break;
2597 if (index)
2598 index--;
2599 if (s[index] == ' ') {
2600 while (index && (s[index] == ' '))
2601 index--;
2602 if (index) {
2603 while (index && (s[index] != ' '))
2604 index--;
2605 if (s[index] == ' ')
2606 index++;
2608 } else {
2609 while (index && (s[index] != ' '))
2610 index--;
2611 if (s[index] == ' ')
2612 index++;
2614 ret = index;
2615 break;
2616 case WB_RIGHT:
2617 if (!count)
2618 break;
2619 if (index)
2620 index--;
2621 if (s[index] == ' ')
2622 while ((index < count) && (s[index] == ' ')) index++;
2623 else {
2624 while (s[index] && (s[index] != ' ') && (index < count))
2625 index++;
2626 while ((s[index] == ' ') && (index < count)) index++;
2628 ret = index;
2629 break;
2630 case WB_ISDELIMITER:
2631 ret = (s[index] == ' ');
2632 break;
2633 default:
2634 ERR("unknown action code, please report !\n");
2635 break;
2637 return ret;
2641 /*********************************************************************
2643 * EM_CHARFROMPOS
2645 * returns line number (not index) in high-order word of result.
2646 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2647 * to Richedit, not to the edit control. Original documentation is valid.
2648 * FIXME: do the specs mean to return -1 if outside client area or
2649 * if outside formatting rectangle ???
2652 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2654 POINT pt;
2655 RECT rc;
2656 INT index;
2658 pt.x = x;
2659 pt.y = y;
2660 GetClientRect(es->hwndSelf, &rc);
2661 if (!PtInRect(&rc, pt))
2662 return -1;
2664 index = EDIT_CharFromPos(es, x, y, NULL);
2665 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2669 /*********************************************************************
2671 * EM_FMTLINES
2673 * Enable or disable soft breaks.
2675 * This means: insert or remove the soft linebreak character (\r\r\n).
2676 * Take care to check if the text still fits the buffer after insertion.
2677 * If not, notify with EN_ERRSPACE.
2680 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2682 es->flags &= ~EF_USE_SOFTBRK;
2683 if (add_eol) {
2684 es->flags |= EF_USE_SOFTBRK;
2685 FIXME("soft break enabled, not implemented\n");
2687 return add_eol;
2691 /*********************************************************************
2693 * EM_GETHANDLE
2695 * Hopefully this won't fire back at us.
2696 * We always start with a fixed buffer in the local heap.
2697 * Despite of the documentation says that the local heap is used
2698 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2699 * buffer on the local heap.
2702 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2704 HLOCAL hLocal;
2706 if (!(es->style & ES_MULTILINE))
2707 return 0;
2709 if(es->is_unicode)
2710 hLocal = es->hloc32W;
2711 else
2713 if(!es->hloc32A)
2715 CHAR *textA;
2716 UINT countA, alloc_size;
2717 TRACE("Allocating 32-bit ANSI alias buffer\n");
2718 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2719 alloc_size = ROUND_TO_GROW(countA);
2720 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2722 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2723 return 0;
2725 textA = LocalLock(es->hloc32A);
2726 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2727 LocalUnlock(es->hloc32A);
2729 hLocal = es->hloc32A;
2732 es->flags |= EF_APP_HAS_HANDLE;
2733 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2734 return hLocal;
2738 /*********************************************************************
2740 * EM_GETHANDLE16
2742 * Hopefully this won't fire back at us.
2743 * We always start with a buffer in 32 bit linear memory.
2744 * However, with this message a 16 bit application requests
2745 * a handle of 16 bit local heap memory, where it expects to find
2746 * the text.
2747 * It's a pitty that from this moment on we have to use this
2748 * local heap, because applications may rely on the handle
2749 * in the future.
2751 * In this function we'll try to switch to local heap.
2753 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2755 CHAR *textA;
2756 UINT countA, alloc_size;
2757 STACK16FRAME* stack16;
2758 HANDLE16 oldDS;
2760 if (!(es->style & ES_MULTILINE))
2761 return 0;
2763 if (es->hloc16)
2764 return es->hloc16;
2766 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2767 oldDS = stack16->ds;
2768 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2770 if (!LocalHeapSize16()) {
2772 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2773 ERR("could not initialize local heap\n");
2774 goto done;
2776 TRACE("local heap initialized\n");
2779 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2780 alloc_size = ROUND_TO_GROW(countA);
2782 TRACE("Allocating 16-bit ANSI alias buffer\n");
2783 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2784 ERR("could not allocate new 16 bit buffer\n");
2785 goto done;
2788 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2789 ERR("could not lock new 16 bit buffer\n");
2790 LocalFree16(es->hloc16);
2791 es->hloc16 = 0;
2792 goto done;
2795 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2796 LocalUnlock16(es->hloc16);
2797 es->flags |= EF_APP_HAS_HANDLE;
2799 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2801 done:
2802 stack16->ds = oldDS;
2803 return es->hloc16;
2807 /*********************************************************************
2809 * EM_GETLINE
2812 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2814 LPWSTR src;
2815 INT line_len, dst_len;
2816 INT i;
2818 if (es->style & ES_MULTILINE) {
2819 if (line >= es->line_count)
2820 return 0;
2821 } else
2822 line = 0;
2823 i = EDIT_EM_LineIndex(es, line);
2824 src = es->text + i;
2825 line_len = EDIT_EM_LineLength(es, i);
2826 dst_len = *(WORD *)dst;
2827 if(unicode)
2829 if(dst_len <= line_len)
2831 memcpy(dst, src, dst_len * sizeof(WCHAR));
2832 return dst_len;
2834 else /* Append 0 if enough space */
2836 memcpy(dst, src, line_len * sizeof(WCHAR));
2837 dst[line_len] = 0;
2838 return line_len;
2841 else
2843 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2844 if(!ret && line_len) /* Insufficient buffer size */
2845 return dst_len;
2846 if(ret < dst_len) /* Append 0 if enough space */
2847 ((LPSTR)dst)[ret] = 0;
2848 return ret;
2853 /*********************************************************************
2855 * EM_GETSEL
2858 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2860 UINT s = es->selection_start;
2861 UINT e = es->selection_end;
2863 ORDER_UINT(s, e);
2864 if (start)
2865 *start = s;
2866 if (end)
2867 *end = e;
2868 return MAKELONG(s, e);
2872 /*********************************************************************
2874 * EM_GETTHUMB
2876 * FIXME: is this right ? (or should it be only VSCROLL)
2877 * (and maybe only for edit controls that really have their
2878 * own scrollbars) (and maybe only for multiline controls ?)
2879 * All in all: very poorly documented
2882 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2884 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2885 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2889 /*********************************************************************
2891 * EM_LINEFROMCHAR
2894 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2896 INT line;
2897 LINEDEF *line_def;
2899 if (!(es->style & ES_MULTILINE))
2900 return 0;
2901 if (index > (INT)get_text_length(es))
2902 return es->line_count - 1;
2903 if (index == -1)
2904 index = min(es->selection_start, es->selection_end);
2906 line = 0;
2907 line_def = es->first_line_def;
2908 index -= line_def->length;
2909 while ((index >= 0) && line_def->next) {
2910 line++;
2911 line_def = line_def->next;
2912 index -= line_def->length;
2914 return line;
2918 /*********************************************************************
2920 * EM_LINEINDEX
2923 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
2925 INT line_index;
2926 const LINEDEF *line_def;
2928 if (!(es->style & ES_MULTILINE))
2929 return 0;
2930 if (line >= es->line_count)
2931 return -1;
2933 line_index = 0;
2934 line_def = es->first_line_def;
2935 if (line == -1) {
2936 INT index = es->selection_end - line_def->length;
2937 while ((index >= 0) && line_def->next) {
2938 line_index += line_def->length;
2939 line_def = line_def->next;
2940 index -= line_def->length;
2942 } else {
2943 while (line > 0) {
2944 line_index += line_def->length;
2945 line_def = line_def->next;
2946 line--;
2949 return line_index;
2953 /*********************************************************************
2955 * EM_LINELENGTH
2958 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2960 LINEDEF *line_def;
2962 if (!(es->style & ES_MULTILINE))
2963 return get_text_length(es);
2965 if (index == -1) {
2966 /* get the number of remaining non-selected chars of selected lines */
2967 INT32 l; /* line number */
2968 INT32 li; /* index of first char in line */
2969 INT32 count;
2970 l = EDIT_EM_LineFromChar(es, es->selection_start);
2971 /* # chars before start of selection area */
2972 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2973 l = EDIT_EM_LineFromChar(es, es->selection_end);
2974 /* # chars after end of selection */
2975 li = EDIT_EM_LineIndex(es, l);
2976 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2977 return count;
2979 line_def = es->first_line_def;
2980 index -= line_def->length;
2981 while ((index >= 0) && line_def->next) {
2982 line_def = line_def->next;
2983 index -= line_def->length;
2985 return line_def->net_length;
2989 /*********************************************************************
2991 * EM_LINESCROLL
2993 * NOTE: dx is in average character widths, dy - in lines;
2996 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2998 if (!(es->style & ES_MULTILINE))
2999 return FALSE;
3001 dx *= es->char_width;
3002 return EDIT_EM_LineScroll_internal(es, dx, dy);
3005 /*********************************************************************
3007 * EDIT_EM_LineScroll_internal
3009 * Version of EDIT_EM_LineScroll for internal use.
3010 * It doesn't refuse if ES_MULTILINE is set and assumes that
3011 * dx is in pixels, dy - in lines.
3014 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3016 INT nyoff;
3017 INT x_offset_in_pixels;
3018 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3019 es->line_height;
3021 if (es->style & ES_MULTILINE)
3023 x_offset_in_pixels = es->x_offset;
3025 else
3027 dy = 0;
3028 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3031 if (-dx > x_offset_in_pixels)
3032 dx = -x_offset_in_pixels;
3033 if (dx > es->text_width - x_offset_in_pixels)
3034 dx = es->text_width - x_offset_in_pixels;
3035 nyoff = max(0, es->y_offset + dy);
3036 if (nyoff >= es->line_count - lines_per_page)
3037 nyoff = max(0, es->line_count - lines_per_page);
3038 dy = (es->y_offset - nyoff) * es->line_height;
3039 if (dx || dy) {
3040 RECT rc1;
3041 RECT rc;
3043 es->y_offset = nyoff;
3044 if(es->style & ES_MULTILINE)
3045 es->x_offset += dx;
3046 else
3047 es->x_offset += dx / es->char_width;
3049 GetClientRect(es->hwndSelf, &rc1);
3050 IntersectRect(&rc, &rc1, &es->format_rect);
3051 ScrollWindowEx(es->hwndSelf, -dx, dy,
3052 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3053 /* force scroll info update */
3054 EDIT_UpdateScrollInfo(es);
3056 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3057 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3058 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3059 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3060 return TRUE;
3064 /*********************************************************************
3066 * EM_POSFROMCHAR
3069 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3071 INT len = get_text_length(es);
3072 INT l;
3073 INT li;
3074 INT x;
3075 INT y = 0;
3076 INT w;
3077 INT lw = 0;
3078 INT ll = 0;
3079 HDC dc;
3080 HFONT old_font = 0;
3081 SIZE size;
3082 LINEDEF *line_def;
3084 index = min(index, len);
3085 dc = GetDC(es->hwndSelf);
3086 if (es->font)
3087 old_font = SelectObject(dc, es->font);
3088 if (es->style & ES_MULTILINE) {
3089 l = EDIT_EM_LineFromChar(es, index);
3090 y = (l - es->y_offset) * es->line_height;
3091 li = EDIT_EM_LineIndex(es, l);
3092 if (after_wrap && (li == index) && l) {
3093 INT l2 = l - 1;
3094 line_def = es->first_line_def;
3095 while (l2) {
3096 line_def = line_def->next;
3097 l2--;
3099 if (line_def->ending == END_WRAP) {
3100 l--;
3101 y -= es->line_height;
3102 li = EDIT_EM_LineIndex(es, l);
3106 line_def = es->first_line_def;
3107 while (line_def->index != li)
3108 line_def = line_def->next;
3110 ll = line_def->net_length;
3111 lw = line_def->width;
3113 w = es->format_rect.right - es->format_rect.left;
3114 if (es->style & ES_RIGHT)
3116 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3117 es->tabs_count, es->tabs)) - es->x_offset;
3118 x = w - x;
3120 else if (es->style & ES_CENTER)
3122 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3123 es->tabs_count, es->tabs)) - es->x_offset;
3124 x += (w - lw) / 2;
3126 else /* ES_LEFT */
3128 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3129 es->tabs_count, es->tabs)) - es->x_offset;
3131 } else {
3132 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3133 if (index < es->x_offset) {
3134 GetTextExtentPoint32W(dc, text + index,
3135 es->x_offset - index, &size);
3136 x = -size.cx;
3137 } else {
3138 GetTextExtentPoint32W(dc, text + es->x_offset,
3139 index - es->x_offset, &size);
3140 x = size.cx;
3142 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3144 w = es->format_rect.right - es->format_rect.left;
3145 if (w > es->text_width)
3147 if (es->style & ES_RIGHT)
3148 x += w - es->text_width;
3149 else if (es->style & ES_CENTER)
3150 x += (w - es->text_width) / 2;
3154 y = 0;
3155 if (es->style & ES_PASSWORD)
3156 HeapFree(GetProcessHeap(), 0, text);
3158 x += es->format_rect.left;
3159 y += es->format_rect.top;
3160 if (es->font)
3161 SelectObject(dc, old_font);
3162 ReleaseDC(es->hwndSelf, dc);
3163 return MAKELONG((INT16)x, (INT16)y);
3167 /*********************************************************************
3169 * EM_REPLACESEL
3171 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3174 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3176 UINT strl = strlenW(lpsz_replace);
3177 UINT tl = get_text_length(es);
3178 UINT utl;
3179 UINT s;
3180 UINT e;
3181 UINT i;
3182 UINT size;
3183 LPWSTR p;
3184 HRGN hrgn = 0;
3185 LPWSTR buf = NULL;
3186 UINT bufl = 0;
3188 TRACE("%s, can_undo %d, send_update %d\n",
3189 debugstr_w(lpsz_replace), can_undo, send_update);
3191 s = es->selection_start;
3192 e = es->selection_end;
3194 if ((s == e) && !strl)
3195 return;
3197 ORDER_UINT(s, e);
3199 size = tl - (e - s) + strl;
3200 if (!size)
3201 es->text_width = 0;
3203 /* Issue the EN_MAXTEXT notification and continue with replacing text
3204 * such that buffer limit is honored. */
3205 if ((honor_limit) && (size > es->buffer_limit)) {
3206 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3207 /* Buffer limit can be smaller than the actual length of text in combobox */
3208 if (es->buffer_limit < (tl - (e-s)))
3209 strl = 0;
3210 else
3211 strl = es->buffer_limit - (tl - (e-s));
3214 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3215 return;
3217 if (e != s) {
3218 /* there is something to be deleted */
3219 TRACE("deleting stuff.\n");
3220 bufl = e - s;
3221 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3222 if (!buf) return;
3223 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3224 buf[bufl] = 0; /* ensure 0 termination */
3225 /* now delete */
3226 strcpyW(es->text + s, es->text + e);
3227 text_buffer_changed(es);
3229 if (strl) {
3230 /* there is an insertion */
3231 tl = get_text_length(es);
3232 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));
3233 for (p = es->text + tl ; p >= es->text + s ; p--)
3234 p[strl] = p[0];
3235 for (i = 0 , p = es->text + s ; i < strl ; i++)
3236 p[i] = lpsz_replace[i];
3237 if(es->style & ES_UPPERCASE)
3238 CharUpperBuffW(p, strl);
3239 else if(es->style & ES_LOWERCASE)
3240 CharLowerBuffW(p, strl);
3241 text_buffer_changed(es);
3243 if (es->style & ES_MULTILINE)
3245 INT st = min(es->selection_start, es->selection_end);
3246 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3248 hrgn = CreateRectRgn(0, 0, 0, 0);
3249 EDIT_BuildLineDefs_ML(es, st, st + strl,
3250 strl - abs(es->selection_end - es->selection_start), hrgn);
3251 /* if text is too long undo all changes */
3252 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3253 if (strl)
3254 strcpyW(es->text + e, es->text + e + strl);
3255 if (e != s)
3256 for (i = 0 , p = es->text ; i < e - s ; i++)
3257 p[i + s] = buf[i];
3258 text_buffer_changed(es);
3259 EDIT_BuildLineDefs_ML(es, s, e,
3260 abs(es->selection_end - es->selection_start) - strl, hrgn);
3261 strl = 0;
3262 e = s;
3263 hrgn = CreateRectRgn(0, 0, 0, 0);
3264 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3267 else {
3268 INT fw = es->format_rect.right - es->format_rect.left;
3269 EDIT_CalcLineWidth_SL(es);
3270 /* remove chars that don't fit */
3271 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3272 while ((es->text_width > fw) && s + strl >= s) {
3273 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3274 strl--;
3275 EDIT_CalcLineWidth_SL(es);
3277 text_buffer_changed(es);
3278 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3282 if (e != s) {
3283 if (can_undo) {
3284 utl = strlenW(es->undo_text);
3285 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3286 /* undo-buffer is extended to the right */
3287 EDIT_MakeUndoFit(es, utl + e - s);
3288 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3289 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3290 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3291 /* undo-buffer is extended to the left */
3292 EDIT_MakeUndoFit(es, utl + e - s);
3293 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3294 p[e - s] = p[0];
3295 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3296 p[i] = buf[i];
3297 es->undo_position = s;
3298 } else {
3299 /* new undo-buffer */
3300 EDIT_MakeUndoFit(es, e - s);
3301 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3302 es->undo_text[e - s] = 0; /* ensure 0 termination */
3303 es->undo_position = s;
3305 /* any deletion makes the old insertion-undo invalid */
3306 es->undo_insert_count = 0;
3307 } else
3308 EDIT_EM_EmptyUndoBuffer(es);
3310 if (strl) {
3311 if (can_undo) {
3312 if ((s == es->undo_position) ||
3313 ((es->undo_insert_count) &&
3314 (s == es->undo_position + es->undo_insert_count)))
3316 * insertion is new and at delete position or
3317 * an extension to either left or right
3319 es->undo_insert_count += strl;
3320 else {
3321 /* new insertion undo */
3322 es->undo_position = s;
3323 es->undo_insert_count = strl;
3324 /* new insertion makes old delete-buffer invalid */
3325 *es->undo_text = '\0';
3327 } else
3328 EDIT_EM_EmptyUndoBuffer(es);
3331 if (bufl)
3332 HeapFree(GetProcessHeap(), 0, buf);
3334 s += strl;
3336 /* If text has been deleted and we're right or center aligned then scroll rightward */
3337 if (es->style & (ES_RIGHT | ES_CENTER))
3339 INT delta = strl - abs(es->selection_end - es->selection_start);
3341 if (delta < 0 && es->x_offset)
3343 if (abs(delta) > es->x_offset)
3344 es->x_offset = 0;
3345 else
3346 es->x_offset += delta;
3350 EDIT_EM_SetSel(es, s, s, FALSE);
3351 es->flags |= EF_MODIFIED;
3352 if (send_update) es->flags |= EF_UPDATE;
3353 if (hrgn)
3355 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3356 DeleteObject(hrgn);
3358 else
3359 EDIT_UpdateText(es, NULL, TRUE);
3361 EDIT_EM_ScrollCaret(es);
3363 /* force scroll info update */
3364 EDIT_UpdateScrollInfo(es);
3367 if(send_update || (es->flags & EF_UPDATE))
3369 es->flags &= ~EF_UPDATE;
3370 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3375 /*********************************************************************
3377 * EM_SCROLL
3380 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3382 INT dy;
3384 if (!(es->style & ES_MULTILINE))
3385 return (LRESULT)FALSE;
3387 dy = 0;
3389 switch (action) {
3390 case SB_LINEUP:
3391 if (es->y_offset)
3392 dy = -1;
3393 break;
3394 case SB_LINEDOWN:
3395 if (es->y_offset < es->line_count - 1)
3396 dy = 1;
3397 break;
3398 case SB_PAGEUP:
3399 if (es->y_offset)
3400 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3401 break;
3402 case SB_PAGEDOWN:
3403 if (es->y_offset < es->line_count - 1)
3404 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3405 break;
3406 default:
3407 return (LRESULT)FALSE;
3409 if (dy) {
3410 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3411 /* check if we are going to move too far */
3412 if(es->y_offset + dy > es->line_count - vlc)
3413 dy = es->line_count - vlc - es->y_offset;
3415 /* Notification is done in EDIT_EM_LineScroll */
3416 if(dy)
3417 EDIT_EM_LineScroll(es, 0, dy);
3419 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3423 /*********************************************************************
3425 * EM_SCROLLCARET
3428 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3430 if (es->style & ES_MULTILINE) {
3431 INT l;
3432 INT li;
3433 INT vlc;
3434 INT ww;
3435 INT cw = es->char_width;
3436 INT x;
3437 INT dy = 0;
3438 INT dx = 0;
3440 l = EDIT_EM_LineFromChar(es, es->selection_end);
3441 li = EDIT_EM_LineIndex(es, l);
3442 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3443 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3444 if (l >= es->y_offset + vlc)
3445 dy = l - vlc + 1 - es->y_offset;
3446 if (l < es->y_offset)
3447 dy = l - es->y_offset;
3448 ww = es->format_rect.right - es->format_rect.left;
3449 if (x < es->format_rect.left)
3450 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3451 if (x > es->format_rect.right)
3452 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3453 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3455 /* check if we are going to move too far */
3456 if(es->x_offset + dx + ww > es->text_width)
3457 dx = es->text_width - ww - es->x_offset;
3458 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3459 EDIT_EM_LineScroll_internal(es, dx, dy);
3461 } else {
3462 INT x;
3463 INT goal;
3464 INT format_width;
3466 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3467 format_width = es->format_rect.right - es->format_rect.left;
3468 if (x < es->format_rect.left) {
3469 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3470 do {
3471 es->x_offset--;
3472 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3473 } while ((x < goal) && es->x_offset);
3474 /* FIXME: use ScrollWindow() somehow to improve performance */
3475 EDIT_UpdateText(es, NULL, TRUE);
3476 } else if (x > es->format_rect.right) {
3477 INT x_last;
3478 INT len = get_text_length(es);
3479 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3480 do {
3481 es->x_offset++;
3482 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3483 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3484 } while ((x > goal) && (x_last > es->format_rect.right));
3485 /* FIXME: use ScrollWindow() somehow to improve performance */
3486 EDIT_UpdateText(es, NULL, TRUE);
3490 if(es->flags & EF_FOCUSED)
3491 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3495 /*********************************************************************
3497 * EM_SETHANDLE
3499 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3502 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3504 if (!(es->style & ES_MULTILINE))
3505 return;
3507 if (!hloc) {
3508 WARN("called with NULL handle\n");
3509 return;
3512 EDIT_UnlockBuffer(es, TRUE);
3514 if(es->hloc16)
3516 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3517 HANDLE16 oldDS = stack16->ds;
3519 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3520 LocalFree16(es->hloc16);
3521 stack16->ds = oldDS;
3522 es->hloc16 = 0;
3525 if(es->is_unicode)
3527 if(es->hloc32A)
3529 LocalFree(es->hloc32A);
3530 es->hloc32A = NULL;
3532 es->hloc32W = hloc;
3534 else
3536 INT countW, countA;
3537 HLOCAL hloc32W_new;
3538 WCHAR *textW;
3539 CHAR *textA;
3541 countA = LocalSize(hloc);
3542 textA = LocalLock(hloc);
3543 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3544 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3546 ERR("Could not allocate new unicode buffer\n");
3547 return;
3549 textW = LocalLock(hloc32W_new);
3550 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3551 LocalUnlock(hloc32W_new);
3552 LocalUnlock(hloc);
3554 if(es->hloc32W)
3555 LocalFree(es->hloc32W);
3557 es->hloc32W = hloc32W_new;
3558 es->hloc32A = hloc;
3561 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3563 es->flags |= EF_APP_HAS_HANDLE;
3564 EDIT_LockBuffer(es);
3566 es->x_offset = es->y_offset = 0;
3567 es->selection_start = es->selection_end = 0;
3568 EDIT_EM_EmptyUndoBuffer(es);
3569 es->flags &= ~EF_MODIFIED;
3570 es->flags &= ~EF_UPDATE;
3571 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3572 EDIT_UpdateText(es, NULL, TRUE);
3573 EDIT_EM_ScrollCaret(es);
3574 /* force scroll info update */
3575 EDIT_UpdateScrollInfo(es);
3579 /*********************************************************************
3581 * EM_SETHANDLE16
3583 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3586 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3588 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3589 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3590 HANDLE16 oldDS = stack16->ds;
3591 INT countW, countA;
3592 HLOCAL hloc32W_new;
3593 WCHAR *textW;
3594 CHAR *textA;
3596 if (!(es->style & ES_MULTILINE))
3597 return;
3599 if (!hloc) {
3600 WARN("called with NULL handle\n");
3601 return;
3604 EDIT_UnlockBuffer(es, TRUE);
3606 if(es->hloc32A)
3608 LocalFree(es->hloc32A);
3609 es->hloc32A = NULL;
3612 stack16->ds = hInstance;
3613 countA = LocalSize16(hloc);
3614 textA = MapSL(LocalLock16(hloc));
3615 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3616 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3618 ERR("Could not allocate new unicode buffer\n");
3619 return;
3621 textW = LocalLock(hloc32W_new);
3622 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3623 LocalUnlock(hloc32W_new);
3624 LocalUnlock16(hloc);
3625 stack16->ds = oldDS;
3627 if(es->hloc32W)
3628 LocalFree(es->hloc32W);
3630 es->hloc32W = hloc32W_new;
3631 es->hloc16 = hloc;
3633 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3635 es->flags |= EF_APP_HAS_HANDLE;
3636 EDIT_LockBuffer(es);
3638 es->x_offset = es->y_offset = 0;
3639 es->selection_start = es->selection_end = 0;
3640 EDIT_EM_EmptyUndoBuffer(es);
3641 es->flags &= ~EF_MODIFIED;
3642 es->flags &= ~EF_UPDATE;
3643 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3644 EDIT_UpdateText(es, NULL, TRUE);
3645 EDIT_EM_ScrollCaret(es);
3646 /* force scroll info update */
3647 EDIT_UpdateScrollInfo(es);
3651 /*********************************************************************
3653 * EM_SETLIMITTEXT
3655 * NOTE: this version currently implements WinNT limits
3658 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
3660 if (!limit) limit = ~0u;
3661 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
3662 es->buffer_limit = limit;
3666 /*********************************************************************
3668 * EM_SETMARGINS
3670 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3671 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3672 * margin according to the textmetrics of the current font.
3674 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3675 * of the char's width as the margin, but this is not how Windows handles this.
3676 * For all other fonts Windows sets the margins to zero.
3678 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3679 * edit control is equal to or larger than a certain size.
3680 * Interestingly if one subtracts both the left and right margins from
3681 * this size one always seems to get an even number. The extents of
3682 * the (four character) string "'**'" match this quite closely, so
3683 * we'll use this until we come up with a better idea.
3685 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3687 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3688 SIZE sz;
3690 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3691 return sz.cx + left + right;
3694 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3695 WORD left, WORD right, BOOL repaint)
3697 TEXTMETRICW tm;
3698 INT default_left_margin = 0; /* in pixels */
3699 INT default_right_margin = 0; /* in pixels */
3701 /* Set the default margins depending on the font */
3702 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3703 HDC dc = GetDC(es->hwndSelf);
3704 HFONT old_font = SelectObject(dc, es->font);
3705 GetTextMetricsW(dc, &tm);
3706 /* The default margins are only non zero for TrueType or Vector fonts */
3707 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3708 int min_size;
3709 RECT rc;
3710 /* This must be calculated more exactly! But how? */
3711 default_left_margin = tm.tmAveCharWidth / 2;
3712 default_right_margin = tm.tmAveCharWidth / 2;
3713 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3714 GetClientRect(es->hwndSelf, &rc);
3715 if(rc.right - rc.left < min_size) {
3716 default_left_margin = es->left_margin;
3717 default_right_margin = es->right_margin;
3720 SelectObject(dc, old_font);
3721 ReleaseDC(es->hwndSelf, dc);
3724 if (action & EC_LEFTMARGIN) {
3725 es->format_rect.left -= es->left_margin;
3726 if (left != EC_USEFONTINFO)
3727 es->left_margin = left;
3728 else
3729 es->left_margin = default_left_margin;
3730 es->format_rect.left += es->left_margin;
3733 if (action & EC_RIGHTMARGIN) {
3734 es->format_rect.right += es->right_margin;
3735 if (right != EC_USEFONTINFO)
3736 es->right_margin = right;
3737 else
3738 es->right_margin = default_right_margin;
3739 es->format_rect.right -= es->right_margin;
3742 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3743 EDIT_AdjustFormatRect(es);
3744 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3747 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3751 /*********************************************************************
3753 * EM_SETPASSWORDCHAR
3756 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3758 LONG style;
3760 if (es->style & ES_MULTILINE)
3761 return;
3763 if (es->password_char == c)
3764 return;
3766 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3767 es->password_char = c;
3768 if (c) {
3769 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3770 es->style |= ES_PASSWORD;
3771 } else {
3772 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3773 es->style &= ~ES_PASSWORD;
3775 EDIT_UpdateText(es, NULL, TRUE);
3779 /*********************************************************************
3781 * EDIT_EM_SetSel
3783 * note: unlike the specs say: the order of start and end
3784 * _is_ preserved in Windows. (i.e. start can be > end)
3785 * In other words: this handler is OK
3788 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3790 UINT old_start = es->selection_start;
3791 UINT old_end = es->selection_end;
3792 UINT len = get_text_length(es);
3794 if (start == (UINT)-1) {
3795 start = es->selection_end;
3796 end = es->selection_end;
3797 } else {
3798 start = min(start, len);
3799 end = min(end, len);
3801 es->selection_start = start;
3802 es->selection_end = end;
3803 if (after_wrap)
3804 es->flags |= EF_AFTER_WRAP;
3805 else
3806 es->flags &= ~EF_AFTER_WRAP;
3807 /* Compute the necessary invalidation region. */
3808 /* Note that we don't need to invalidate regions which have
3809 * "never" been selected, or those which are "still" selected.
3810 * In fact, every time we hit a selection boundary, we can
3811 * *toggle* whether we need to invalidate. Thus we can optimize by
3812 * *sorting* the interval endpoints. Let's assume that we sort them
3813 * in this order:
3814 * start <= end <= old_start <= old_end
3815 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3816 * in 5 comparisons; ie it's impossible to do better than the
3817 * following: */
3818 ORDER_UINT(end, old_end);
3819 ORDER_UINT(start, old_start);
3820 ORDER_UINT(old_start, old_end);
3821 ORDER_UINT(start, end);
3822 /* Note that at this point 'end' and 'old_start' are not in order, but
3823 * start is definitely the min. and old_end is definitely the max. */
3824 if (end != old_start)
3827 * One can also do
3828 * ORDER_UINT32(end, old_start);
3829 * EDIT_InvalidateText(es, start, end);
3830 * EDIT_InvalidateText(es, old_start, old_end);
3831 * in place of the following if statement.
3832 * (That would complete the optimal five-comparison four-element sort.)
3834 if (old_start > end )
3836 EDIT_InvalidateText(es, start, end);
3837 EDIT_InvalidateText(es, old_start, old_end);
3839 else
3841 EDIT_InvalidateText(es, start, old_start);
3842 EDIT_InvalidateText(es, end, old_end);
3845 else EDIT_InvalidateText(es, start, old_end);
3849 /*********************************************************************
3851 * EM_SETTABSTOPS
3854 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3856 if (!(es->style & ES_MULTILINE))
3857 return FALSE;
3858 HeapFree(GetProcessHeap(), 0, es->tabs);
3859 es->tabs_count = count;
3860 if (!count)
3861 es->tabs = NULL;
3862 else {
3863 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3864 memcpy(es->tabs, tabs, count * sizeof(INT));
3866 return TRUE;
3870 /*********************************************************************
3872 * EM_SETTABSTOPS16
3875 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs)
3877 if (!(es->style & ES_MULTILINE))
3878 return FALSE;
3879 HeapFree(GetProcessHeap(), 0, es->tabs);
3880 es->tabs_count = count;
3881 if (!count)
3882 es->tabs = NULL;
3883 else {
3884 INT i;
3885 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3886 for (i = 0 ; i < count ; i++)
3887 es->tabs[i] = *tabs++;
3889 return TRUE;
3893 /*********************************************************************
3895 * EM_SETWORDBREAKPROC
3898 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3900 if (es->word_break_proc == wbp)
3901 return;
3903 es->word_break_proc = wbp;
3904 es->word_break_proc16 = NULL;
3906 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3907 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3908 EDIT_UpdateText(es, NULL, TRUE);
3913 /*********************************************************************
3915 * EM_SETWORDBREAKPROC16
3918 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3920 if (es->word_break_proc16 == wbp)
3921 return;
3923 es->word_break_proc = NULL;
3924 es->word_break_proc16 = wbp;
3925 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3926 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3927 EDIT_UpdateText(es, NULL, TRUE);
3932 /*********************************************************************
3934 * EM_UNDO / WM_UNDO
3937 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3939 INT ulength;
3940 LPWSTR utext;
3942 /* As per MSDN spec, for a single-line edit control,
3943 the return value is always TRUE */
3944 if( es->style & ES_READONLY )
3945 return !(es->style & ES_MULTILINE);
3947 ulength = strlenW(es->undo_text);
3949 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3951 strcpyW(utext, es->undo_text);
3953 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3954 es->undo_insert_count, debugstr_w(utext));
3956 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3957 EDIT_EM_EmptyUndoBuffer(es);
3958 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3959 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3960 /* send the notification after the selection start and end are set */
3961 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3962 EDIT_EM_ScrollCaret(es);
3963 HeapFree(GetProcessHeap(), 0, utext);
3965 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3966 es->undo_insert_count, debugstr_w(es->undo_text));
3967 return TRUE;
3971 /*********************************************************************
3973 * WM_CHAR
3976 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3978 BOOL control;
3980 control = GetKeyState(VK_CONTROL) & 0x8000;
3982 switch (c) {
3983 case '\r':
3984 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3985 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3986 break;
3987 case '\n':
3988 if (es->style & ES_MULTILINE) {
3989 if (es->style & ES_READONLY) {
3990 EDIT_MoveHome(es, FALSE);
3991 EDIT_MoveDown_ML(es, FALSE);
3992 } else {
3993 static const WCHAR cr_lfW[] = {'\r','\n',0};
3994 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3997 break;
3998 case '\t':
3999 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4001 static const WCHAR tabW[] = {'\t',0};
4002 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4004 break;
4005 case VK_BACK:
4006 if (!(es->style & ES_READONLY) && !control) {
4007 if (es->selection_start != es->selection_end)
4008 EDIT_WM_Clear(es);
4009 else {
4010 /* delete character left of caret */
4011 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4012 EDIT_MoveBackward(es, TRUE);
4013 EDIT_WM_Clear(es);
4016 break;
4017 case 0x03: /* ^C */
4018 if (!(es->style & ES_PASSWORD))
4019 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4020 break;
4021 case 0x16: /* ^V */
4022 if (!(es->style & ES_READONLY))
4023 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4024 break;
4025 case 0x18: /* ^X */
4026 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
4027 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4028 break;
4029 case 0x1A: /* ^Z */
4030 if (!(es->style & ES_READONLY))
4031 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
4032 break;
4034 default:
4035 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4036 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4037 break;
4039 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4040 WCHAR str[2];
4041 str[0] = c;
4042 str[1] = '\0';
4043 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4045 break;
4050 /*********************************************************************
4052 * WM_COMMAND
4055 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4057 if (code || control)
4058 return;
4060 switch (id) {
4061 case EM_UNDO:
4062 EDIT_EM_Undo(es);
4063 break;
4064 case WM_CUT:
4065 EDIT_WM_Cut(es);
4066 break;
4067 case WM_COPY:
4068 EDIT_WM_Copy(es);
4069 break;
4070 case WM_PASTE:
4071 EDIT_WM_Paste(es);
4072 break;
4073 case WM_CLEAR:
4074 EDIT_WM_Clear(es);
4075 break;
4076 case EM_SETSEL:
4077 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4078 EDIT_EM_ScrollCaret(es);
4079 break;
4080 default:
4081 ERR("unknown menu item, please report\n");
4082 break;
4087 /*********************************************************************
4089 * WM_CONTEXTMENU
4091 * Note: the resource files resource/sysres_??.rc cannot define a
4092 * single popup menu. Hence we use a (dummy) menubar
4093 * containing the single popup menu as its first item.
4095 * FIXME: the message identifiers have been chosen arbitrarily,
4096 * hence we use MF_BYPOSITION.
4097 * We might as well use the "real" values (anybody knows ?)
4098 * The menu definition is in resources/sysres_??.rc.
4099 * Once these are OK, we better use MF_BYCOMMAND here
4100 * (as we do in EDIT_WM_Command()).
4103 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4105 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4106 HMENU popup = GetSubMenu(menu, 0);
4107 UINT start = es->selection_start;
4108 UINT end = es->selection_end;
4110 ORDER_UINT(start, end);
4112 /* undo */
4113 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4114 /* cut */
4115 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4116 /* copy */
4117 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4118 /* paste */
4119 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4120 /* delete */
4121 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4122 /* select all */
4123 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4125 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4127 RECT rc;
4128 /* Windows places the menu at the edit's center in this case */
4129 GetClientRect(es->hwndSelf, &rc);
4130 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4131 x = rc.left + (rc.right - rc.left) / 2;
4132 y = rc.top + (rc.bottom - rc.top) / 2;
4135 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4136 DestroyMenu(menu);
4140 /*********************************************************************
4142 * WM_COPY
4145 static void EDIT_WM_Copy(EDITSTATE *es)
4147 INT s = min(es->selection_start, es->selection_end);
4148 INT e = max(es->selection_start, es->selection_end);
4149 HGLOBAL hdst;
4150 LPWSTR dst;
4151 DWORD len;
4153 if (e == s) return;
4155 len = e - s;
4156 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4157 dst = GlobalLock(hdst);
4158 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4159 dst[len] = 0; /* ensure 0 termination */
4160 TRACE("%s\n", debugstr_w(dst));
4161 GlobalUnlock(hdst);
4162 OpenClipboard(es->hwndSelf);
4163 EmptyClipboard();
4164 SetClipboardData(CF_UNICODETEXT, hdst);
4165 CloseClipboard();
4169 /*********************************************************************
4171 * WM_CREATE
4174 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4176 RECT clientRect;
4178 TRACE("%s\n", debugstr_w(name));
4180 * To initialize some final structure members, we call some helper
4181 * functions. However, since the EDITSTATE is not consistent (i.e.
4182 * not fully initialized), we should be very careful which
4183 * functions can be called, and in what order.
4185 EDIT_WM_SetFont(es, 0, FALSE);
4186 EDIT_EM_EmptyUndoBuffer(es);
4188 /* We need to calculate the format rect
4189 (applications may send EM_SETMARGINS before the control gets visible) */
4190 GetClientRect(es->hwndSelf, &clientRect);
4191 EDIT_SetRectNP(es, &clientRect);
4193 if (name && *name) {
4194 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4195 /* if we insert text to the editline, the text scrolls out
4196 * of the window, as the caret is placed after the insert
4197 * pos normally; thus we reset es->selection... to 0 and
4198 * update caret
4200 es->selection_start = es->selection_end = 0;
4201 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4202 * Messages are only to be sent when the USER does something to
4203 * change the contents. So I am removing this EN_CHANGE
4205 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4207 EDIT_EM_ScrollCaret(es);
4209 /* force scroll info update */
4210 EDIT_UpdateScrollInfo(es);
4211 /* The rule seems to return 1 here for success */
4212 /* Power Builder masked edit controls will crash */
4213 /* if not. */
4214 /* FIXME: is that in all cases so ? */
4215 return 1;
4219 /*********************************************************************
4221 * WM_DESTROY
4224 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4226 LINEDEF *pc, *pp;
4228 if (es->hloc32W) {
4229 while (LocalUnlock(es->hloc32W)) ;
4230 LocalFree(es->hloc32W);
4232 if (es->hloc32A) {
4233 while (LocalUnlock(es->hloc32A)) ;
4234 LocalFree(es->hloc32A);
4236 if (es->hloc16) {
4237 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4238 HANDLE16 oldDS = stack16->ds;
4240 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4241 while (LocalUnlock16(es->hloc16)) ;
4242 LocalFree16(es->hloc16);
4243 stack16->ds = oldDS;
4246 pc = es->first_line_def;
4247 while (pc)
4249 pp = pc->next;
4250 HeapFree(GetProcessHeap(), 0, pc);
4251 pc = pp;
4254 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4255 HeapFree(GetProcessHeap(), 0, es);
4257 return 0;
4261 /*********************************************************************
4263 * WM_GETTEXT
4266 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4268 if(!count) return 0;
4270 if(unicode)
4272 lstrcpynW(dst, es->text, count);
4273 return strlenW(dst);
4275 else
4277 LPSTR textA = (LPSTR)dst;
4278 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4279 textA[count - 1] = 0; /* ensure 0 termination */
4280 return strlen(textA);
4284 /*********************************************************************
4286 * WM_HSCROLL
4289 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4291 INT dx;
4292 INT fw;
4294 if (!(es->style & ES_MULTILINE))
4295 return 0;
4297 if (!(es->style & ES_AUTOHSCROLL))
4298 return 0;
4300 dx = 0;
4301 fw = es->format_rect.right - es->format_rect.left;
4302 switch (action) {
4303 case SB_LINELEFT:
4304 TRACE("SB_LINELEFT\n");
4305 if (es->x_offset)
4306 dx = -es->char_width;
4307 break;
4308 case SB_LINERIGHT:
4309 TRACE("SB_LINERIGHT\n");
4310 if (es->x_offset < es->text_width)
4311 dx = es->char_width;
4312 break;
4313 case SB_PAGELEFT:
4314 TRACE("SB_PAGELEFT\n");
4315 if (es->x_offset)
4316 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4317 break;
4318 case SB_PAGERIGHT:
4319 TRACE("SB_PAGERIGHT\n");
4320 if (es->x_offset < es->text_width)
4321 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4322 break;
4323 case SB_LEFT:
4324 TRACE("SB_LEFT\n");
4325 if (es->x_offset)
4326 dx = -es->x_offset;
4327 break;
4328 case SB_RIGHT:
4329 TRACE("SB_RIGHT\n");
4330 if (es->x_offset < es->text_width)
4331 dx = es->text_width - es->x_offset;
4332 break;
4333 case SB_THUMBTRACK:
4334 TRACE("SB_THUMBTRACK %d\n", pos);
4335 es->flags |= EF_HSCROLL_TRACK;
4336 if(es->style & WS_HSCROLL)
4337 dx = pos - es->x_offset;
4338 else
4340 INT fw, new_x;
4341 /* Sanity check */
4342 if(pos < 0 || pos > 100) return 0;
4343 /* Assume default scroll range 0-100 */
4344 fw = es->format_rect.right - es->format_rect.left;
4345 new_x = pos * (es->text_width - fw) / 100;
4346 dx = es->text_width ? (new_x - es->x_offset) : 0;
4348 break;
4349 case SB_THUMBPOSITION:
4350 TRACE("SB_THUMBPOSITION %d\n", pos);
4351 es->flags &= ~EF_HSCROLL_TRACK;
4352 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4353 dx = pos - es->x_offset;
4354 else
4356 INT fw, new_x;
4357 /* Sanity check */
4358 if(pos < 0 || pos > 100) return 0;
4359 /* Assume default scroll range 0-100 */
4360 fw = es->format_rect.right - es->format_rect.left;
4361 new_x = pos * (es->text_width - fw) / 100;
4362 dx = es->text_width ? (new_x - es->x_offset) : 0;
4364 if (!dx) {
4365 /* force scroll info update */
4366 EDIT_UpdateScrollInfo(es);
4367 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4369 break;
4370 case SB_ENDSCROLL:
4371 TRACE("SB_ENDSCROLL\n");
4372 break;
4374 * FIXME : the next two are undocumented !
4375 * Are we doing the right thing ?
4376 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4377 * although it's also a regular control message.
4379 case EM_GETTHUMB: /* this one is used by NT notepad */
4380 case EM_GETTHUMB16:
4382 LRESULT ret;
4383 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4384 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4385 else
4387 /* Assume default scroll range 0-100 */
4388 INT fw = es->format_rect.right - es->format_rect.left;
4389 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4391 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4392 return ret;
4394 case EM_LINESCROLL16:
4395 TRACE("EM_LINESCROLL16\n");
4396 dx = pos;
4397 break;
4399 default:
4400 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4401 action, action);
4402 return 0;
4404 if (dx)
4406 INT fw = es->format_rect.right - es->format_rect.left;
4407 /* check if we are going to move too far */
4408 if(es->x_offset + dx + fw > es->text_width)
4409 dx = es->text_width - fw - es->x_offset;
4410 if(dx)
4411 EDIT_EM_LineScroll_internal(es, dx, 0);
4413 return 0;
4417 /*********************************************************************
4419 * EDIT_CheckCombo
4422 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4424 HWND hLBox = es->hwndListBox;
4425 HWND hCombo;
4426 BOOL bDropped;
4427 int nEUI;
4429 if (!hLBox)
4430 return FALSE;
4432 hCombo = GetParent(es->hwndSelf);
4433 bDropped = TRUE;
4434 nEUI = 0;
4436 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4438 if (key == VK_UP || key == VK_DOWN)
4440 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4441 nEUI = 1;
4443 if (msg == WM_KEYDOWN || nEUI)
4444 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4447 switch (msg)
4449 case WM_KEYDOWN:
4450 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4452 /* make sure ComboLBox pops up */
4453 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4454 key = VK_F4;
4455 nEUI = 2;
4458 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4459 break;
4461 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4462 if (nEUI)
4463 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4464 else
4465 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4466 break;
4469 if(nEUI == 2)
4470 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4472 return TRUE;
4476 /*********************************************************************
4478 * WM_KEYDOWN
4480 * Handling of special keys that don't produce a WM_CHAR
4481 * (i.e. non-printable keys) & Backspace & Delete
4484 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4486 BOOL shift;
4487 BOOL control;
4489 if (GetKeyState(VK_MENU) & 0x8000)
4490 return 0;
4492 shift = GetKeyState(VK_SHIFT) & 0x8000;
4493 control = GetKeyState(VK_CONTROL) & 0x8000;
4495 switch (key) {
4496 case VK_F4:
4497 case VK_UP:
4498 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4499 break;
4501 /* fall through */
4502 case VK_LEFT:
4503 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4504 EDIT_MoveUp_ML(es, shift);
4505 else
4506 if (control)
4507 EDIT_MoveWordBackward(es, shift);
4508 else
4509 EDIT_MoveBackward(es, shift);
4510 break;
4511 case VK_DOWN:
4512 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4513 break;
4514 /* fall through */
4515 case VK_RIGHT:
4516 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4517 EDIT_MoveDown_ML(es, shift);
4518 else if (control)
4519 EDIT_MoveWordForward(es, shift);
4520 else
4521 EDIT_MoveForward(es, shift);
4522 break;
4523 case VK_HOME:
4524 EDIT_MoveHome(es, shift);
4525 break;
4526 case VK_END:
4527 EDIT_MoveEnd(es, shift);
4528 break;
4529 case VK_PRIOR:
4530 if (es->style & ES_MULTILINE)
4531 EDIT_MovePageUp_ML(es, shift);
4532 else
4533 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4534 break;
4535 case VK_NEXT:
4536 if (es->style & ES_MULTILINE)
4537 EDIT_MovePageDown_ML(es, shift);
4538 else
4539 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4540 break;
4541 case VK_DELETE:
4542 if (!(es->style & ES_READONLY) && !(shift && control)) {
4543 if (es->selection_start != es->selection_end) {
4544 if (shift)
4545 EDIT_WM_Cut(es);
4546 else
4547 EDIT_WM_Clear(es);
4548 } else {
4549 if (shift) {
4550 /* delete character left of caret */
4551 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4552 EDIT_MoveBackward(es, TRUE);
4553 EDIT_WM_Clear(es);
4554 } else if (control) {
4555 /* delete to end of line */
4556 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4557 EDIT_MoveEnd(es, TRUE);
4558 EDIT_WM_Clear(es);
4559 } else {
4560 /* delete character right of caret */
4561 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4562 EDIT_MoveForward(es, TRUE);
4563 EDIT_WM_Clear(es);
4567 break;
4568 case VK_INSERT:
4569 if (shift) {
4570 if (!(es->style & ES_READONLY))
4571 EDIT_WM_Paste(es);
4572 } else if (control)
4573 EDIT_WM_Copy(es);
4574 break;
4575 case VK_RETURN:
4576 /* If the edit doesn't want the return send a message to the default object */
4577 if(!(es->style & ES_WANTRETURN))
4579 HWND hwndParent = GetParent(es->hwndSelf);
4580 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4581 if (HIWORD(dw) == DC_HASDEFID)
4583 SendMessageW( hwndParent, WM_COMMAND,
4584 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4585 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4588 break;
4590 return 0;
4594 /*********************************************************************
4596 * WM_KILLFOCUS
4599 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4601 es->flags &= ~EF_FOCUSED;
4602 DestroyCaret();
4603 if(!(es->style & ES_NOHIDESEL))
4604 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4605 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4606 return 0;
4610 /*********************************************************************
4612 * WM_LBUTTONDBLCLK
4614 * The caret position has been set on the WM_LBUTTONDOWN message
4617 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4619 INT s;
4620 INT e = es->selection_end;
4621 INT l;
4622 INT li;
4623 INT ll;
4625 es->bCaptureState = TRUE;
4626 SetCapture(es->hwndSelf);
4628 l = EDIT_EM_LineFromChar(es, e);
4629 li = EDIT_EM_LineIndex(es, l);
4630 ll = EDIT_EM_LineLength(es, e);
4631 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4632 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4633 EDIT_EM_SetSel(es, s, e, FALSE);
4634 EDIT_EM_ScrollCaret(es);
4635 es->region_posx = es->region_posy = 0;
4636 SetTimer(es->hwndSelf, 0, 100, NULL);
4637 return 0;
4641 /*********************************************************************
4643 * WM_LBUTTONDOWN
4646 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4648 INT e;
4649 BOOL after_wrap;
4651 es->bCaptureState = TRUE;
4652 SetCapture(es->hwndSelf);
4653 EDIT_ConfinePoint(es, &x, &y);
4654 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4655 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4656 EDIT_EM_ScrollCaret(es);
4657 es->region_posx = es->region_posy = 0;
4658 SetTimer(es->hwndSelf, 0, 100, NULL);
4660 if (!(es->flags & EF_FOCUSED))
4661 SetFocus(es->hwndSelf);
4663 return 0;
4667 /*********************************************************************
4669 * WM_LBUTTONUP
4672 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4674 if (es->bCaptureState) {
4675 KillTimer(es->hwndSelf, 0);
4676 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4678 es->bCaptureState = FALSE;
4679 return 0;
4683 /*********************************************************************
4685 * WM_MBUTTONDOWN
4688 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4690 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4691 return 0;
4695 /*********************************************************************
4697 * WM_MOUSEMOVE
4700 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4702 INT e;
4703 BOOL after_wrap;
4704 INT prex, prey;
4706 /* If the mouse has been captured by process other than the edit control itself,
4707 * the windows edit controls will not select the strings with mouse move.
4709 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4710 return 0;
4713 * FIXME: gotta do some scrolling if outside client
4714 * area. Maybe reset the timer ?
4716 prex = x; prey = y;
4717 EDIT_ConfinePoint(es, &x, &y);
4718 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4719 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4720 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4721 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4722 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4723 return 0;
4727 /*********************************************************************
4729 * WM_NCCREATE
4731 * See also EDIT_WM_StyleChanged
4733 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4735 EDITSTATE *es;
4736 UINT alloc_size;
4738 TRACE("Creating %s edit control, style = %08x\n",
4739 unicode ? "Unicode" : "ANSI", lpcs->style);
4741 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4742 return FALSE;
4743 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4746 * Note: since the EDITSTATE has not been fully initialized yet,
4747 * we can't use any API calls that may send
4748 * WM_XXX messages before WM_NCCREATE is completed.
4751 es->is_unicode = unicode;
4752 es->style = lpcs->style;
4754 es->bEnableState = !(es->style & WS_DISABLED);
4756 es->hwndSelf = hwnd;
4757 /* Save parent, which will be notified by EN_* messages */
4758 es->hwndParent = lpcs->hwndParent;
4760 if (es->style & ES_COMBO)
4761 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4763 /* Number overrides lowercase overrides uppercase (at least it
4764 * does in Win95). However I'll bet that ES_NUMBER would be
4765 * invalid under Win 3.1.
4767 if (es->style & ES_NUMBER) {
4768 ; /* do not override the ES_NUMBER */
4769 } else if (es->style & ES_LOWERCASE) {
4770 es->style &= ~ES_UPPERCASE;
4772 if (es->style & ES_MULTILINE) {
4773 es->buffer_limit = BUFLIMIT_INITIAL;
4774 if (es->style & WS_VSCROLL)
4775 es->style |= ES_AUTOVSCROLL;
4776 if (es->style & WS_HSCROLL)
4777 es->style |= ES_AUTOHSCROLL;
4778 es->style &= ~ES_PASSWORD;
4779 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4780 /* Confirmed - RIGHT overrides CENTER */
4781 if (es->style & ES_RIGHT)
4782 es->style &= ~ES_CENTER;
4783 es->style &= ~WS_HSCROLL;
4784 es->style &= ~ES_AUTOHSCROLL;
4786 } else {
4787 es->buffer_limit = BUFLIMIT_INITIAL;
4788 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4789 es->style &= ~ES_CENTER;
4790 es->style &= ~WS_HSCROLL;
4791 es->style &= ~WS_VSCROLL;
4792 if (es->style & ES_PASSWORD)
4793 es->password_char = '*';
4796 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4797 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4798 return FALSE;
4799 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4801 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4802 return FALSE;
4803 es->undo_buffer_size = es->buffer_size;
4805 if (es->style & ES_MULTILINE)
4806 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4807 return FALSE;
4808 es->line_count = 1;
4811 * In Win95 look and feel, the WS_BORDER style is replaced by the
4812 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4813 * control a nonclient area so we don't need to draw the border.
4814 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4815 * a nonclient area and we should handle painting the border ourselves.
4817 * When making modifications please ensure that the code still works
4818 * for edit controls created directly with style 0x50800000, exStyle 0
4819 * (which should have a single pixel border)
4821 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4822 es->style &= ~WS_BORDER;
4823 else if (es->style & WS_BORDER)
4824 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4826 return TRUE;
4829 /*********************************************************************
4831 * WM_PAINT
4834 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4836 PAINTSTRUCT ps;
4837 INT i;
4838 HDC dc;
4839 HFONT old_font = 0;
4840 RECT rc;
4841 RECT rcClient;
4842 RECT rcLine;
4843 RECT rcRgn;
4844 HBRUSH brush;
4845 HBRUSH old_brush;
4846 INT bw, bh;
4847 BOOL rev = es->bEnableState &&
4848 ((es->flags & EF_FOCUSED) ||
4849 (es->style & ES_NOHIDESEL));
4850 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4852 GetClientRect(es->hwndSelf, &rcClient);
4854 /* get the background brush */
4855 brush = EDIT_NotifyCtlColor(es, dc);
4857 /* paint the border and the background */
4858 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4860 if(es->style & WS_BORDER) {
4861 bw = GetSystemMetrics(SM_CXBORDER);
4862 bh = GetSystemMetrics(SM_CYBORDER);
4863 rc = rcClient;
4864 if(es->style & ES_MULTILINE) {
4865 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4866 if(es->style & WS_VSCROLL) rc.right+=bw;
4869 /* Draw the frame. Same code as in nonclient.c */
4870 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4871 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4872 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4873 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4874 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4875 SelectObject(dc, old_brush);
4877 /* Keep the border clean */
4878 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4879 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4882 GetClipBox(dc, &rc);
4883 FillRect(dc, &rc, brush);
4885 IntersectClipRect(dc, es->format_rect.left,
4886 es->format_rect.top,
4887 es->format_rect.right,
4888 es->format_rect.bottom);
4889 if (es->style & ES_MULTILINE) {
4890 rc = rcClient;
4891 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4893 if (es->font)
4894 old_font = SelectObject(dc, es->font);
4896 if (!es->bEnableState)
4897 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4898 GetClipBox(dc, &rcRgn);
4899 if (es->style & ES_MULTILINE) {
4900 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4901 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4902 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4903 if (IntersectRect(&rc, &rcRgn, &rcLine))
4904 EDIT_PaintLine(es, dc, i, rev);
4906 } else {
4907 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4908 if (IntersectRect(&rc, &rcRgn, &rcLine))
4909 EDIT_PaintLine(es, dc, 0, rev);
4911 if (es->font)
4912 SelectObject(dc, old_font);
4914 if (!hdc)
4915 EndPaint(es->hwndSelf, &ps);
4919 /*********************************************************************
4921 * WM_PASTE
4924 static void EDIT_WM_Paste(EDITSTATE *es)
4926 HGLOBAL hsrc;
4927 LPWSTR src;
4929 /* Protect read-only edit control from modification */
4930 if(es->style & ES_READONLY)
4931 return;
4933 OpenClipboard(es->hwndSelf);
4934 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4935 src = (LPWSTR)GlobalLock(hsrc);
4936 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4937 GlobalUnlock(hsrc);
4939 else if (es->style & ES_PASSWORD) {
4940 /* clear selected text in password edit box even with empty clipboard */
4941 const WCHAR empty_strW[] = { 0 };
4942 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
4944 CloseClipboard();
4948 /*********************************************************************
4950 * WM_SETFOCUS
4953 static void EDIT_WM_SetFocus(EDITSTATE *es)
4955 es->flags |= EF_FOCUSED;
4957 if (!(es->style & ES_NOHIDESEL))
4958 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4960 /* single line edit updates itself */
4961 if (!(es->style & ES_MULTILINE))
4963 HDC hdc = GetDC(es->hwndSelf);
4964 EDIT_WM_Paint(es, hdc);
4965 ReleaseDC(es->hwndSelf, hdc);
4968 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4969 EDIT_SetCaretPos(es, es->selection_end,
4970 es->flags & EF_AFTER_WRAP);
4971 ShowCaret(es->hwndSelf);
4972 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
4976 /*********************************************************************
4978 * WM_SETFONT
4980 * With Win95 look the margins are set to default font value unless
4981 * the system font (font == 0) is being set, in which case they are left
4982 * unchanged.
4985 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4987 TEXTMETRICW tm;
4988 HDC dc;
4989 HFONT old_font = 0;
4990 RECT clientRect;
4992 es->font = font;
4993 dc = GetDC(es->hwndSelf);
4994 if (font)
4995 old_font = SelectObject(dc, font);
4996 GetTextMetricsW(dc, &tm);
4997 es->line_height = tm.tmHeight;
4998 es->char_width = tm.tmAveCharWidth;
4999 if (font)
5000 SelectObject(dc, old_font);
5001 ReleaseDC(es->hwndSelf, dc);
5003 /* Reset the format rect and the margins */
5004 GetClientRect(es->hwndSelf, &clientRect);
5005 EDIT_SetRectNP(es, &clientRect);
5006 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5007 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5009 if (es->style & ES_MULTILINE)
5010 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5011 else
5012 EDIT_CalcLineWidth_SL(es);
5014 if (redraw)
5015 EDIT_UpdateText(es, NULL, TRUE);
5016 if (es->flags & EF_FOCUSED) {
5017 DestroyCaret();
5018 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5019 EDIT_SetCaretPos(es, es->selection_end,
5020 es->flags & EF_AFTER_WRAP);
5021 ShowCaret(es->hwndSelf);
5026 /*********************************************************************
5028 * WM_SETTEXT
5030 * NOTES
5031 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5032 * The modified flag is reset. No notifications are sent.
5034 * For single-line controls, reception of WM_SETTEXT triggers:
5035 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5038 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5040 LPWSTR textW = NULL;
5041 if (!unicode && text)
5043 LPCSTR textA = (LPCSTR)text;
5044 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5045 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5046 if (textW)
5047 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5048 text = textW;
5051 if (es->flags & EF_UPDATE)
5052 /* fixed this bug once; complain if we see it about to happen again. */
5053 ERR("SetSel may generate UPDATE message whose handler may reset "
5054 "selection.\n");
5056 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5057 if (text)
5059 TRACE("%s\n", debugstr_w(text));
5060 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5061 if(!unicode)
5062 HeapFree(GetProcessHeap(), 0, textW);
5064 else
5066 static const WCHAR empty_stringW[] = {0};
5067 TRACE("<NULL>\n");
5068 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5070 es->x_offset = 0;
5071 es->flags &= ~EF_MODIFIED;
5072 EDIT_EM_SetSel(es, 0, 0, FALSE);
5074 /* Send the notification after the selection start and end have been set
5075 * edit control doesn't send notification on WM_SETTEXT
5076 * if it is multiline, or it is part of combobox
5078 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5080 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5081 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5083 EDIT_EM_ScrollCaret(es);
5084 EDIT_UpdateScrollInfo(es);
5088 /*********************************************************************
5090 * WM_SIZE
5093 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5095 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5096 RECT rc;
5097 TRACE("width = %d, height = %d\n", width, height);
5098 SetRect(&rc, 0, 0, width, height);
5099 EDIT_SetRectNP(es, &rc);
5100 EDIT_UpdateText(es, NULL, TRUE);
5105 /*********************************************************************
5107 * WM_STYLECHANGED
5109 * This message is sent by SetWindowLong on having changed either the Style
5110 * or the extended style.
5112 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5114 * See also EDIT_WM_NCCreate
5116 * It appears that the Windows version of the edit control allows the style
5117 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5118 * style variable which will generally be different. In this function we
5119 * update the internal style based on what changed in the externally visible
5120 * style.
5122 * Much of this content as based upon the MSDN, especially:
5123 * Platform SDK Documentation -> User Interface Services ->
5124 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5125 * Edit Control Styles
5127 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5129 if (GWL_STYLE == which) {
5130 DWORD style_change_mask;
5131 DWORD new_style;
5132 /* Only a subset of changes can be applied after the control
5133 * has been created.
5135 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5136 ES_NUMBER;
5137 if (es->style & ES_MULTILINE)
5138 style_change_mask |= ES_WANTRETURN;
5140 new_style = style->styleNew & style_change_mask;
5142 /* Number overrides lowercase overrides uppercase (at least it
5143 * does in Win95). However I'll bet that ES_NUMBER would be
5144 * invalid under Win 3.1.
5146 if (new_style & ES_NUMBER) {
5147 ; /* do not override the ES_NUMBER */
5148 } else if (new_style & ES_LOWERCASE) {
5149 new_style &= ~ES_UPPERCASE;
5152 es->style = (es->style & ~style_change_mask) | new_style;
5153 } else if (GWL_EXSTYLE == which) {
5154 ; /* FIXME - what is needed here */
5155 } else {
5156 WARN ("Invalid style change %ld\n",which);
5159 return 0;
5162 /*********************************************************************
5164 * WM_SYSKEYDOWN
5167 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5169 if ((key == VK_BACK) && (key_data & 0x2000)) {
5170 if (EDIT_EM_CanUndo(es))
5171 EDIT_EM_Undo(es);
5172 return 0;
5173 } else if (key == VK_UP || key == VK_DOWN) {
5174 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5175 return 0;
5177 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5181 /*********************************************************************
5183 * WM_TIMER
5186 static void EDIT_WM_Timer(EDITSTATE *es)
5188 if (es->region_posx < 0) {
5189 EDIT_MoveBackward(es, TRUE);
5190 } else if (es->region_posx > 0) {
5191 EDIT_MoveForward(es, TRUE);
5194 * FIXME: gotta do some vertical scrolling here, like
5195 * EDIT_EM_LineScroll(hwnd, 0, 1);
5199 /*********************************************************************
5201 * WM_VSCROLL
5204 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5206 INT dy;
5208 if (!(es->style & ES_MULTILINE))
5209 return 0;
5211 if (!(es->style & ES_AUTOVSCROLL))
5212 return 0;
5214 dy = 0;
5215 switch (action) {
5216 case SB_LINEUP:
5217 case SB_LINEDOWN:
5218 case SB_PAGEUP:
5219 case SB_PAGEDOWN:
5220 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5221 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5222 (action == SB_PAGEUP ? "SB_PAGEUP" :
5223 "SB_PAGEDOWN"))));
5224 EDIT_EM_Scroll(es, action);
5225 return 0;
5226 case SB_TOP:
5227 TRACE("SB_TOP\n");
5228 dy = -es->y_offset;
5229 break;
5230 case SB_BOTTOM:
5231 TRACE("SB_BOTTOM\n");
5232 dy = es->line_count - 1 - es->y_offset;
5233 break;
5234 case SB_THUMBTRACK:
5235 TRACE("SB_THUMBTRACK %d\n", pos);
5236 es->flags |= EF_VSCROLL_TRACK;
5237 if(es->style & WS_VSCROLL)
5238 dy = pos - es->y_offset;
5239 else
5241 /* Assume default scroll range 0-100 */
5242 INT vlc, new_y;
5243 /* Sanity check */
5244 if(pos < 0 || pos > 100) return 0;
5245 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5246 new_y = pos * (es->line_count - vlc) / 100;
5247 dy = es->line_count ? (new_y - es->y_offset) : 0;
5248 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5249 es->line_count, es->y_offset, pos, dy);
5251 break;
5252 case SB_THUMBPOSITION:
5253 TRACE("SB_THUMBPOSITION %d\n", pos);
5254 es->flags &= ~EF_VSCROLL_TRACK;
5255 if(es->style & WS_VSCROLL)
5256 dy = pos - es->y_offset;
5257 else
5259 /* Assume default scroll range 0-100 */
5260 INT vlc, new_y;
5261 /* Sanity check */
5262 if(pos < 0 || pos > 100) return 0;
5263 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5264 new_y = pos * (es->line_count - vlc) / 100;
5265 dy = es->line_count ? (new_y - es->y_offset) : 0;
5266 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5267 es->line_count, es->y_offset, pos, dy);
5269 if (!dy)
5271 /* force scroll info update */
5272 EDIT_UpdateScrollInfo(es);
5273 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5275 break;
5276 case SB_ENDSCROLL:
5277 TRACE("SB_ENDSCROLL\n");
5278 break;
5280 * FIXME : the next two are undocumented !
5281 * Are we doing the right thing ?
5282 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5283 * although it's also a regular control message.
5285 case EM_GETTHUMB: /* this one is used by NT notepad */
5286 case EM_GETTHUMB16:
5288 LRESULT ret;
5289 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5290 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5291 else
5293 /* Assume default scroll range 0-100 */
5294 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5295 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5297 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5298 return ret;
5300 case EM_LINESCROLL16:
5301 TRACE("EM_LINESCROLL16 %d\n", pos);
5302 dy = pos;
5303 break;
5305 default:
5306 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5307 action, action);
5308 return 0;
5310 if (dy)
5311 EDIT_EM_LineScroll(es, 0, dy);
5312 return 0;
5315 /*********************************************************************
5317 * EDIT_UpdateText
5320 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5322 if (es->flags & EF_UPDATE) {
5323 es->flags &= ~EF_UPDATE;
5324 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5326 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5330 /*********************************************************************
5332 * EDIT_UpdateText
5335 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
5337 if (es->flags & EF_UPDATE) {
5338 es->flags &= ~EF_UPDATE;
5339 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5341 InvalidateRect(es->hwndSelf, rc, bErase);
5344 /********************************************************************
5346 * The Following code is to handle inline editing from IMEs
5349 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5351 DWORD dwBufLen;
5352 LPWSTR lpCompStr = NULL;
5353 HIMC hIMC;
5354 LPSTR lpCompStrAttr = NULL;
5355 DWORD dwBufLenAttr;
5357 if (!(hIMC = ImmGetContext(hwnd)))
5358 return;
5360 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5362 if (dwBufLen < 0)
5364 ImmReleaseContext(hwnd, hIMC);
5365 return;
5368 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
5369 if (!lpCompStr)
5371 ERR("Unable to allocate IME CompositionString\n");
5372 ImmReleaseContext(hwnd,hIMC);
5373 return;
5376 if (dwBufLen)
5377 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
5378 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
5380 if (CompFlag & GCS_COMPATTR)
5383 * We do not use the attributes yet. it would tell us what characters
5384 * are in transition and which are converted or decided upon
5386 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5387 if (dwBufLenAttr)
5389 dwBufLenAttr ++;
5390 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5391 if (!lpCompStrAttr)
5393 ERR("Unable to allocate IME Attribute String\n");
5394 HeapFree(GetProcessHeap(),0,lpCompStr);
5395 ImmReleaseContext(hwnd,hIMC);
5396 return;
5398 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5399 dwBufLenAttr);
5400 lpCompStrAttr[dwBufLenAttr] = 0;
5402 else
5403 lpCompStrAttr = NULL;
5406 /* check for change in composition start */
5407 if (es->selection_end < es->composition_start)
5408 es->composition_start = es->selection_end;
5410 /* replace existing selection string */
5411 es->selection_start = es->composition_start;
5413 if (es->composition_len > 0)
5414 es->selection_end = es->composition_start + es->composition_len;
5415 else
5416 es->selection_end = es->selection_start;
5418 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5419 es->composition_len = abs(es->composition_start - es->selection_end);
5421 es->selection_start = es->composition_start;
5422 es->selection_end = es->selection_start + es->composition_len;
5424 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5425 HeapFree(GetProcessHeap(),0,lpCompStr);
5426 ImmReleaseContext(hwnd,hIMC);
5429 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5431 DWORD dwBufLen;
5432 LPWSTR lpResultStr;
5433 HIMC hIMC;
5435 if ( !(hIMC = ImmGetContext(hwnd)))
5436 return;
5438 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5439 if (dwBufLen <= 0)
5441 ImmReleaseContext(hwnd, hIMC);
5442 return;
5445 lpResultStr = HeapAlloc(GetProcessHeap(),0, dwBufLen+sizeof(WCHAR));
5446 if (!lpResultStr)
5448 ERR("Unable to alloc buffer for IME string\n");
5449 ImmReleaseContext(hwnd, hIMC);
5450 return;
5453 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, dwBufLen);
5454 lpResultStr[dwBufLen/sizeof(WCHAR)] = 0;
5456 /* check for change in composition start */
5457 if (es->selection_end < es->composition_start)
5458 es->composition_start = es->selection_end;
5460 es->selection_start = es->composition_start;
5461 es->selection_end = es->composition_start + es->composition_len;
5462 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5463 es->composition_start = es->selection_end;
5464 es->composition_len = 0;
5466 HeapFree(GetProcessHeap(),0,lpResultStr);
5467 ImmReleaseContext(hwnd, hIMC);
5470 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5472 if (CompFlag & GCS_RESULTSTR)
5473 EDIT_GetResultStr(hwnd,es);
5474 if (CompFlag & GCS_COMPSTR)
5475 EDIT_GetCompositionStr(hwnd, CompFlag, es);