user32: Add edit control check to see if its in a dialog on WM_CHAR/VK_TAB.
[wine/gsoc_dplay.git] / dlls / user32 / edit.c
blob7fd62ccc2ba3f16281f85472ed96fe2a47b177e1
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 LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c);
261 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
262 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
263 static void EDIT_WM_Copy(EDITSTATE *es);
264 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
265 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
266 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
267 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
268 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
269 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
270 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
271 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
272 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
273 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
274 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
275 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
276 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
277 static void EDIT_WM_Paste(EDITSTATE *es);
278 static void EDIT_WM_SetFocus(EDITSTATE *es);
279 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
280 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
281 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
282 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
283 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
284 static void EDIT_WM_Timer(EDITSTATE *es);
285 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
286 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase);
287 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
288 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
290 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
291 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
293 /*********************************************************************
294 * edit class descriptor
296 static const WCHAR editW[] = {'E','d','i','t',0};
297 const struct builtin_class_descr EDIT_builtin_class =
299 editW, /* name */
300 CS_DBLCLKS | CS_PARENTDC, /* style */
301 EditWndProcA, /* procA */
302 EditWndProcW, /* procW */
303 sizeof(EDITSTATE *), /* extra */
304 IDC_IBEAM, /* cursor */
305 0 /* brush */
309 /*********************************************************************
311 * EM_CANUNDO
314 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
316 return (es->undo_insert_count || strlenW(es->undo_text));
320 /*********************************************************************
322 * EM_EMPTYUNDOBUFFER
325 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
327 es->undo_insert_count = 0;
328 *es->undo_text = '\0';
332 /*********************************************************************
334 * WM_CLEAR
337 static inline void EDIT_WM_Clear(EDITSTATE *es)
339 static const WCHAR empty_stringW[] = {0};
341 /* Protect read-only edit control from modification */
342 if(es->style & ES_READONLY)
343 return;
345 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
349 /*********************************************************************
351 * WM_CUT
354 static inline void EDIT_WM_Cut(EDITSTATE *es)
356 EDIT_WM_Copy(es);
357 EDIT_WM_Clear(es);
361 /**********************************************************************
362 * get_app_version
364 * Returns the window version in case Wine emulates a later version
365 * of windows than the application expects.
367 * In a number of cases when windows runs an application that was
368 * designed for an earlier windows version, windows reverts
369 * to "old" behaviour of that earlier version.
371 * An example is a disabled edit control that needs to be painted.
372 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
373 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
374 * applications with an expected version 0f 4.0 or higher.
377 static DWORD get_app_version(void)
379 static DWORD version;
380 if (!version)
382 DWORD dwEmulatedVersion;
383 OSVERSIONINFOW info;
384 DWORD dwProcVersion = GetProcessVersion(0);
386 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
387 GetVersionExW( &info );
388 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
389 /* FIXME: this may not be 100% correct; see discussion on the
390 * wine developer list in Nov 1999 */
391 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
393 return version;
396 static inline UINT get_text_length(EDITSTATE *es)
398 if(es->text_length == (UINT)-1)
399 es->text_length = strlenW(es->text);
400 return es->text_length;
403 static inline void text_buffer_changed(EDITSTATE *es)
405 es->text_length = (UINT)-1;
408 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
410 HBRUSH hbrush;
411 UINT msg;
413 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
414 msg = WM_CTLCOLORSTATIC;
415 else
416 msg = WM_CTLCOLOREDIT;
418 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
419 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
420 if (!hbrush)
421 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
422 return hbrush;
425 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
427 if(unicode)
428 return DefWindowProcW(hwnd, msg, wParam, lParam);
429 else
430 return DefWindowProcA(hwnd, msg, wParam, lParam);
433 /*********************************************************************
435 * EditWndProc_common
437 * The messages are in the order of the actual integer values
438 * (which can be found in include/windows.h)
439 * Wherever possible the 16 bit versions are converted to
440 * the 32 bit ones, so that we can 'fall through' to the
441 * helper functions. These are mostly 32 bit (with a few
442 * exceptions, clearly indicated by a '16' extension to their
443 * names).
446 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
447 WPARAM wParam, LPARAM lParam, BOOL unicode )
449 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
450 LRESULT result = 0;
452 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
454 if (!es && msg != WM_NCCREATE)
455 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
457 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
459 switch (msg) {
460 case EM_GETSEL16:
461 wParam = 0;
462 lParam = 0;
463 /* fall through */
464 case EM_GETSEL:
465 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
466 break;
468 case EM_SETSEL16:
469 if ((short)LOWORD(lParam) == -1)
470 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
471 else
472 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
473 if (!wParam)
474 EDIT_EM_ScrollCaret(es);
475 result = 1;
476 break;
477 case EM_SETSEL:
478 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
479 EDIT_EM_ScrollCaret(es);
480 result = 1;
481 break;
483 case EM_GETRECT16:
484 if (lParam)
486 RECT16 *r16 = MapSL(lParam);
487 r16->left = es->format_rect.left;
488 r16->top = es->format_rect.top;
489 r16->right = es->format_rect.right;
490 r16->bottom = es->format_rect.bottom;
492 break;
493 case EM_GETRECT:
494 if (lParam)
495 CopyRect((LPRECT)lParam, &es->format_rect);
496 break;
498 case EM_SETRECT16:
499 if ((es->style & ES_MULTILINE) && lParam) {
500 RECT rc;
501 RECT16 *r16 = MapSL(lParam);
502 rc.left = r16->left;
503 rc.top = r16->top;
504 rc.right = r16->right;
505 rc.bottom = r16->bottom;
506 EDIT_SetRectNP(es, &rc);
507 EDIT_UpdateText(es, NULL, TRUE);
509 break;
510 case EM_SETRECT:
511 if ((es->style & ES_MULTILINE) && lParam) {
512 EDIT_SetRectNP(es, (LPRECT)lParam);
513 EDIT_UpdateText(es, NULL, TRUE);
515 break;
517 case EM_SETRECTNP16:
518 if ((es->style & ES_MULTILINE) && lParam) {
519 RECT rc;
520 RECT16 *r16 = MapSL(lParam);
521 rc.left = r16->left;
522 rc.top = r16->top;
523 rc.right = r16->right;
524 rc.bottom = r16->bottom;
525 EDIT_SetRectNP(es, &rc);
527 break;
528 case EM_SETRECTNP:
529 if ((es->style & ES_MULTILINE) && lParam)
530 EDIT_SetRectNP(es, (LPRECT)lParam);
531 break;
533 case EM_SCROLL16:
534 case EM_SCROLL:
535 result = EDIT_EM_Scroll(es, (INT)wParam);
536 break;
538 case EM_LINESCROLL16:
539 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
540 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
541 /* fall through */
542 case EM_LINESCROLL:
543 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
544 break;
546 case EM_SCROLLCARET16:
547 case EM_SCROLLCARET:
548 EDIT_EM_ScrollCaret(es);
549 result = 1;
550 break;
552 case EM_GETMODIFY16:
553 case EM_GETMODIFY:
554 result = ((es->flags & EF_MODIFIED) != 0);
555 break;
557 case EM_SETMODIFY16:
558 case EM_SETMODIFY:
559 if (wParam)
560 es->flags |= EF_MODIFIED;
561 else
562 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
563 break;
565 case EM_GETLINECOUNT16:
566 case EM_GETLINECOUNT:
567 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
568 break;
570 case EM_LINEINDEX16:
571 if ((INT16)wParam == -1)
572 wParam = (WPARAM)-1;
573 /* fall through */
574 case EM_LINEINDEX:
575 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
576 break;
578 case EM_SETHANDLE16:
579 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
580 break;
581 case EM_SETHANDLE:
582 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
583 break;
585 case EM_GETHANDLE16:
586 result = (LRESULT)EDIT_EM_GetHandle16(es);
587 break;
588 case EM_GETHANDLE:
589 result = (LRESULT)EDIT_EM_GetHandle(es);
590 break;
592 case EM_GETTHUMB16:
593 case EM_GETTHUMB:
594 result = EDIT_EM_GetThumb(es);
595 break;
597 /* these messages missing from specs */
598 case WM_USER+15:
599 case 0x00bf:
600 case WM_USER+16:
601 case 0x00c0:
602 case WM_USER+19:
603 case 0x00c3:
604 case WM_USER+26:
605 case 0x00ca:
606 FIXME("undocumented message 0x%x, please report\n", msg);
607 result = DefWindowProcW(hwnd, msg, wParam, lParam);
608 break;
610 case EM_LINELENGTH16:
611 case EM_LINELENGTH:
612 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
613 break;
615 case EM_REPLACESEL16:
616 lParam = (LPARAM)MapSL(lParam);
617 unicode = FALSE; /* 16-bit message is always ascii */
618 /* fall through */
619 case EM_REPLACESEL:
621 LPWSTR textW;
623 if(unicode)
624 textW = (LPWSTR)lParam;
625 else
627 LPSTR textA = (LPSTR)lParam;
628 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
629 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
630 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
633 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
634 result = 1;
636 if(!unicode)
637 HeapFree(GetProcessHeap(), 0, textW);
638 break;
641 case EM_GETLINE16:
642 lParam = (LPARAM)MapSL(lParam);
643 unicode = FALSE; /* 16-bit message is always ascii */
644 /* fall through */
645 case EM_GETLINE:
646 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
647 break;
649 case EM_LIMITTEXT16:
650 case EM_SETLIMITTEXT:
651 EDIT_EM_SetLimitText(es, wParam);
652 break;
654 case EM_CANUNDO16:
655 case EM_CANUNDO:
656 result = (LRESULT)EDIT_EM_CanUndo(es);
657 break;
659 case EM_UNDO16:
660 case EM_UNDO:
661 case WM_UNDO:
662 result = (LRESULT)EDIT_EM_Undo(es);
663 break;
665 case EM_FMTLINES16:
666 case EM_FMTLINES:
667 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
668 break;
670 case EM_LINEFROMCHAR16:
671 case EM_LINEFROMCHAR:
672 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
673 break;
675 case EM_SETTABSTOPS16:
676 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
677 break;
678 case EM_SETTABSTOPS:
679 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
680 break;
682 case EM_SETPASSWORDCHAR16:
683 unicode = FALSE; /* 16-bit message is always ascii */
684 /* fall through */
685 case EM_SETPASSWORDCHAR:
687 WCHAR charW = 0;
689 if(unicode)
690 charW = (WCHAR)wParam;
691 else
693 CHAR charA = wParam;
694 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
697 EDIT_EM_SetPasswordChar(es, charW);
698 break;
701 case EM_EMPTYUNDOBUFFER16:
702 case EM_EMPTYUNDOBUFFER:
703 EDIT_EM_EmptyUndoBuffer(es);
704 break;
706 case EM_GETFIRSTVISIBLELINE16:
707 result = es->y_offset;
708 break;
709 case EM_GETFIRSTVISIBLELINE:
710 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
711 break;
713 case EM_SETREADONLY16:
714 case EM_SETREADONLY:
715 if (wParam) {
716 SetWindowLongW( hwnd, GWL_STYLE,
717 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
718 es->style |= ES_READONLY;
719 } else {
720 SetWindowLongW( hwnd, GWL_STYLE,
721 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
722 es->style &= ~ES_READONLY;
724 result = 1;
725 break;
727 case EM_SETWORDBREAKPROC16:
728 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
729 break;
730 case EM_SETWORDBREAKPROC:
731 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
732 break;
734 case EM_GETWORDBREAKPROC16:
735 result = (LRESULT)es->word_break_proc16;
736 break;
737 case EM_GETWORDBREAKPROC:
738 result = (LRESULT)es->word_break_proc;
739 break;
741 case EM_GETPASSWORDCHAR16:
742 unicode = FALSE; /* 16-bit message is always ascii */
743 /* fall through */
744 case EM_GETPASSWORDCHAR:
746 if(unicode)
747 result = es->password_char;
748 else
750 WCHAR charW = es->password_char;
751 CHAR charA = 0;
752 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
753 result = charA;
755 break;
758 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
760 case EM_SETMARGINS:
761 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
762 break;
764 case EM_GETMARGINS:
765 result = MAKELONG(es->left_margin, es->right_margin);
766 break;
768 case EM_GETLIMITTEXT:
769 result = es->buffer_limit;
770 break;
772 case EM_POSFROMCHAR:
773 if ((INT)wParam >= get_text_length(es)) result = -1;
774 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
775 break;
777 case EM_CHARFROMPOS:
778 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
779 break;
781 /* End of the EM_ messages which were in numerical order; what order
782 * are these in? vaguely alphabetical?
785 case WM_NCCREATE:
786 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
787 break;
789 case WM_DESTROY:
790 result = EDIT_WM_Destroy(es);
791 es = NULL;
792 break;
794 case WM_GETDLGCODE:
795 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
797 if (es->style & ES_MULTILINE)
798 result |= DLGC_WANTALLKEYS;
800 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
802 int vk = (int)((LPMSG)lParam)->wParam;
804 if (es->hwndListBox)
806 if (vk == VK_RETURN || vk == VK_ESCAPE)
807 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
808 result |= DLGC_WANTMESSAGE;
810 else
812 switch (vk)
814 case VK_ESCAPE:
815 SendMessageW(GetParent(hwnd), WM_CLOSE, 0, 0);
816 break;
817 case VK_TAB:
818 SendMessageW(GetParent(hwnd), WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0);
819 break;
820 default:
821 break;
825 break;
827 case WM_IME_CHAR:
828 if (!unicode)
830 WCHAR charW;
831 CHAR strng[2];
833 strng[0] = wParam >> 8;
834 strng[1] = wParam & 0xff;
835 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
836 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
837 result = EDIT_WM_Char(es, charW);
838 break;
840 /* fall through */
841 case WM_CHAR:
843 WCHAR charW;
845 if(unicode)
846 charW = wParam;
847 else
849 CHAR charA = wParam;
850 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
853 if (es->hwndListBox)
855 if (charW == VK_RETURN || charW == VK_ESCAPE)
857 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
859 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
860 break;
864 result = EDIT_WM_Char(es, charW);
865 break;
868 case WM_CLEAR:
869 EDIT_WM_Clear(es);
870 break;
872 case WM_COMMAND:
873 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
874 break;
876 case WM_CONTEXTMENU:
877 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
878 break;
880 case WM_COPY:
881 EDIT_WM_Copy(es);
882 break;
884 case WM_CREATE:
885 if(unicode)
886 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
887 else
889 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
890 LPWSTR nameW = NULL;
891 if(nameA)
893 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
894 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
895 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
897 result = EDIT_WM_Create(es, nameW);
898 HeapFree(GetProcessHeap(), 0, nameW);
900 break;
902 case WM_CUT:
903 EDIT_WM_Cut(es);
904 break;
906 case WM_ENABLE:
907 es->bEnableState = (BOOL) wParam;
908 EDIT_UpdateText(es, NULL, TRUE);
909 break;
911 case WM_ERASEBKGND:
912 /* we do the proper erase in EDIT_WM_Paint */
913 result = 1;
914 break;
916 case WM_GETFONT:
917 result = (LRESULT)es->font;
918 break;
920 case WM_GETTEXT:
921 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
922 break;
924 case WM_GETTEXTLENGTH:
925 if (unicode) result = get_text_length(es);
926 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
927 NULL, 0, NULL, NULL );
928 break;
930 case WM_HSCROLL:
931 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
932 break;
934 case WM_KEYDOWN:
935 result = EDIT_WM_KeyDown(es, (INT)wParam);
936 break;
938 case WM_KILLFOCUS:
939 result = EDIT_WM_KillFocus(es);
940 break;
942 case WM_LBUTTONDBLCLK:
943 result = EDIT_WM_LButtonDblClk(es);
944 break;
946 case WM_LBUTTONDOWN:
947 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
948 break;
950 case WM_LBUTTONUP:
951 result = EDIT_WM_LButtonUp(es);
952 break;
954 case WM_MBUTTONDOWN:
955 result = EDIT_WM_MButtonDown(es);
956 break;
958 case WM_MOUSEMOVE:
959 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
960 break;
962 case WM_PRINTCLIENT:
963 case WM_PAINT:
964 EDIT_WM_Paint(es, (HDC)wParam);
965 break;
967 case WM_PASTE:
968 EDIT_WM_Paste(es);
969 break;
971 case WM_SETFOCUS:
972 EDIT_WM_SetFocus(es);
973 break;
975 case WM_SETFONT:
976 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
977 break;
979 case WM_SETREDRAW:
980 /* FIXME: actually set an internal flag and behave accordingly */
981 break;
983 case WM_SETTEXT:
984 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
985 result = TRUE;
986 break;
988 case WM_SIZE:
989 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
990 break;
992 case WM_STYLECHANGED:
993 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
994 break;
996 case WM_STYLECHANGING:
997 result = 0; /* See EDIT_WM_StyleChanged */
998 break;
1000 case WM_SYSKEYDOWN:
1001 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
1002 break;
1004 case WM_TIMER:
1005 EDIT_WM_Timer(es);
1006 break;
1008 case WM_VSCROLL:
1009 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
1010 break;
1012 case WM_MOUSEWHEEL:
1014 int gcWheelDelta = 0;
1015 UINT pulScrollLines = 3;
1016 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1018 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1019 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1020 break;
1022 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1023 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1025 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1026 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1027 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1030 break;
1033 /* IME messages to make the edit control IME aware */
1034 case WM_IME_SETCONTEXT:
1035 break;
1037 case WM_IME_STARTCOMPOSITION:
1039 * FIXME in IME: This message is not always sent like it should be
1041 if (es->selection_start != es->selection_end)
1043 static const WCHAR empty_stringW[] = {0};
1044 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1046 es->composition_start = es->selection_end;
1047 es->composition_len = 0;
1048 break;
1050 case WM_IME_COMPOSITION:
1052 int caret_pos = es->selection_end;
1053 if (es->composition_len == 0)
1055 if (es->selection_start != es->selection_end)
1057 static const WCHAR empty_stringW[] = {0};
1058 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1061 es->composition_start = es->selection_end;
1063 EDIT_ImeComposition(hwnd,lParam,es);
1064 EDIT_SetCaretPos(es, caret_pos, es->flags & EF_AFTER_WRAP);
1065 break;
1068 case WM_IME_ENDCOMPOSITION:
1069 es->composition_len= 0;
1070 break;
1072 case WM_IME_COMPOSITIONFULL:
1073 break;
1075 case WM_IME_SELECT:
1076 break;
1078 case WM_IME_CONTROL:
1079 break;
1081 default:
1082 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1083 break;
1086 if (es) EDIT_UnlockBuffer(es, FALSE);
1088 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1090 return result;
1093 /*********************************************************************
1095 * EditWndProcW (USER32.@)
1097 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1099 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1102 /*********************************************************************
1104 * EditWndProc (USER32.@)
1106 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1108 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1111 /*********************************************************************
1113 * EDIT_BuildLineDefs_ML
1115 * Build linked list of text lines.
1116 * Lines can end with '\0' (last line), a character (if it is wrapped),
1117 * a soft return '\r\r\n' or a hard return '\r\n'
1120 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1122 HDC dc;
1123 HFONT old_font = 0;
1124 LPWSTR current_position, cp;
1125 INT fw;
1126 LINEDEF *current_line;
1127 LINEDEF *previous_line;
1128 LINEDEF *start_line;
1129 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1130 INT line_count = es->line_count;
1131 INT orig_net_length;
1132 RECT rc;
1134 if (istart == iend && delta == 0)
1135 return;
1137 dc = GetDC(es->hwndSelf);
1138 if (es->font)
1139 old_font = SelectObject(dc, es->font);
1141 previous_line = NULL;
1142 current_line = es->first_line_def;
1144 /* Find starting line. istart must lie inside an existing line or
1145 * at the end of buffer */
1146 do {
1147 if (istart < current_line->index + current_line->length ||
1148 current_line->ending == END_0)
1149 break;
1151 previous_line = current_line;
1152 current_line = current_line->next;
1153 line_index++;
1154 } while (current_line);
1156 if (!current_line) /* Error occurred start is not inside previous buffer */
1158 FIXME(" modification occurred outside buffer\n");
1159 ReleaseDC(es->hwndSelf, dc);
1160 return;
1163 /* Remember start of modifications in order to calculate update region */
1164 nstart_line = line_index;
1165 nstart_index = current_line->index;
1167 /* We must start to reformat from the previous line since the modifications
1168 * may have caused the line to wrap upwards. */
1169 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1171 line_index--;
1172 current_line = previous_line;
1174 start_line = current_line;
1176 fw = es->format_rect.right - es->format_rect.left;
1177 current_position = es->text + current_line->index;
1178 do {
1179 if (current_line != start_line)
1181 if (!current_line || current_line->index + delta > current_position - es->text)
1183 /* The buffer has been expanded, create a new line and
1184 insert it into the link list */
1185 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1186 new_line->next = previous_line->next;
1187 previous_line->next = new_line;
1188 current_line = new_line;
1189 es->line_count++;
1191 else if (current_line->index + delta < current_position - es->text)
1193 /* The previous line merged with this line so we delete this extra entry */
1194 previous_line->next = current_line->next;
1195 HeapFree(GetProcessHeap(), 0, current_line);
1196 current_line = previous_line->next;
1197 es->line_count--;
1198 continue;
1200 else /* current_line->index + delta == current_position */
1202 if (current_position - es->text > iend)
1203 break; /* We reached end of line modifications */
1204 /* else recalulate this line */
1208 current_line->index = current_position - es->text;
1209 orig_net_length = current_line->net_length;
1211 /* Find end of line */
1212 cp = current_position;
1213 while (*cp) {
1214 if (*cp == '\n') break;
1215 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1216 break;
1217 cp++;
1220 /* Mark type of line termination */
1221 if (!(*cp)) {
1222 current_line->ending = END_0;
1223 current_line->net_length = strlenW(current_position);
1224 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1225 current_line->ending = END_SOFT;
1226 current_line->net_length = cp - current_position - 1;
1227 } else if (*cp == '\n') {
1228 current_line->ending = END_RICH;
1229 current_line->net_length = cp - current_position;
1230 } else {
1231 current_line->ending = END_HARD;
1232 current_line->net_length = cp - current_position;
1235 /* Calculate line width */
1236 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1237 current_position, current_line->net_length,
1238 es->tabs_count, es->tabs));
1240 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1241 if (!(es->style & ES_AUTOHSCROLL)) {
1242 if (current_line->width > fw) {
1243 INT next = 0;
1244 INT prev;
1245 do {
1246 prev = next;
1247 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1248 prev + 1, current_line->net_length, WB_RIGHT);
1249 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1250 current_position, next, es->tabs_count, es->tabs));
1251 } while (current_line->width <= fw);
1252 if (!prev) { /* Didn't find a line break so force a break */
1253 next = 0;
1254 do {
1255 prev = next;
1256 next++;
1257 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1258 current_position, next, es->tabs_count, es->tabs));
1259 } while (current_line->width <= fw);
1260 if (!prev)
1261 prev = 1;
1264 /* If the first line we are calculating, wrapped before istart, we must
1265 * adjust istart in order for this to be reflected in the update region. */
1266 if (current_line->index == nstart_index && istart > current_line->index + prev)
1267 istart = current_line->index + prev;
1268 /* else if we are updating the previous line before the first line we
1269 * are re-calculating and it expanded */
1270 else if (current_line == start_line &&
1271 current_line->index != nstart_index && orig_net_length < prev)
1273 /* Line expanded due to an upwards line wrap so we must partially include
1274 * previous line in update region */
1275 nstart_line = line_index;
1276 nstart_index = current_line->index;
1277 istart = current_line->index + orig_net_length;
1280 current_line->net_length = prev;
1281 current_line->ending = END_WRAP;
1282 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1283 current_line->net_length, es->tabs_count, es->tabs));
1285 else if (orig_net_length < current_line->net_length &&
1286 current_line == start_line &&
1287 current_line->index != nstart_index) {
1288 /* The previous line expanded but it's still not as wide as the client rect */
1289 /* The expansion is due to an upwards line wrap so we must partially include
1290 it in the update region */
1291 nstart_line = line_index;
1292 nstart_index = current_line->index;
1293 istart = current_line->index + orig_net_length;
1298 /* Adjust length to include line termination */
1299 switch (current_line->ending) {
1300 case END_SOFT:
1301 current_line->length = current_line->net_length + 3;
1302 break;
1303 case END_RICH:
1304 current_line->length = current_line->net_length + 1;
1305 break;
1306 case END_HARD:
1307 current_line->length = current_line->net_length + 2;
1308 break;
1309 case END_WRAP:
1310 case END_0:
1311 current_line->length = current_line->net_length;
1312 break;
1314 es->text_width = max(es->text_width, current_line->width);
1315 current_position += current_line->length;
1316 previous_line = current_line;
1317 current_line = current_line->next;
1318 line_index++;
1319 } while (previous_line->ending != END_0);
1321 /* Finish adjusting line indexes by delta or remove hanging lines */
1322 if (previous_line->ending == END_0)
1324 LINEDEF *pnext = NULL;
1326 previous_line->next = NULL;
1327 while (current_line)
1329 pnext = current_line->next;
1330 HeapFree(GetProcessHeap(), 0, current_line);
1331 current_line = pnext;
1332 es->line_count--;
1335 else if (delta != 0)
1337 while (current_line)
1339 current_line->index += delta;
1340 current_line = current_line->next;
1344 /* Calculate rest of modification rectangle */
1345 if (hrgn)
1347 HRGN tmphrgn;
1349 * We calculate two rectangles. One for the first line which may have
1350 * an indent with respect to the format rect. The other is a format-width
1351 * rectangle that spans the rest of the lines that changed or moved.
1353 rc.top = es->format_rect.top + nstart_line * es->line_height -
1354 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1355 rc.bottom = rc.top + es->line_height;
1356 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1357 rc.left = es->format_rect.left;
1358 else
1359 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1360 es->text + nstart_index, istart - nstart_index,
1361 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1362 rc.right = es->format_rect.right;
1363 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1365 rc.top = rc.bottom;
1366 rc.left = es->format_rect.left;
1367 rc.right = es->format_rect.right;
1369 * If lines were added or removed we must re-paint the remainder of the
1370 * lines since the remaining lines were either shifted up or down.
1372 if (line_count < es->line_count) /* We added lines */
1373 rc.bottom = es->line_count * es->line_height;
1374 else if (line_count > es->line_count) /* We removed lines */
1375 rc.bottom = line_count * es->line_height;
1376 else
1377 rc.bottom = line_index * es->line_height;
1378 rc.bottom += es->format_rect.top;
1379 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1380 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1381 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1382 DeleteObject(tmphrgn);
1385 if (es->font)
1386 SelectObject(dc, old_font);
1388 ReleaseDC(es->hwndSelf, dc);
1391 /*********************************************************************
1393 * EDIT_CalcLineWidth_SL
1396 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1398 SIZE size;
1399 LPWSTR text;
1400 HDC dc;
1401 HFONT old_font = 0;
1403 text = EDIT_GetPasswordPointer_SL(es);
1405 dc = GetDC(es->hwndSelf);
1406 if (es->font)
1407 old_font = SelectObject(dc, es->font);
1409 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1411 if (es->font)
1412 SelectObject(dc, old_font);
1413 ReleaseDC(es->hwndSelf, dc);
1415 if (es->style & ES_PASSWORD)
1416 HeapFree(GetProcessHeap(), 0, text);
1418 es->text_width = size.cx;
1421 /*********************************************************************
1423 * EDIT_CallWordBreakProc
1425 * Call appropriate WordBreakProc (internal or external).
1427 * Note: The "start" argument should always be an index referring
1428 * to es->text. The actual wordbreak proc might be
1429 * 16 bit, so we can't always pass any 32 bit LPSTR.
1430 * Hence we assume that es->text is the buffer that holds
1431 * the string under examination (we can decide this for ourselves).
1434 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1436 INT ret;
1438 if (es->word_break_proc16) {
1439 HGLOBAL16 hglob16;
1440 SEGPTR segptr;
1441 INT countA;
1442 WORD args[5];
1443 DWORD result;
1445 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1446 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1447 segptr = WOWGlobalLock16(hglob16);
1448 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1449 args[4] = SELECTOROF(segptr);
1450 args[3] = OFFSETOF(segptr);
1451 args[2] = index;
1452 args[1] = countA;
1453 args[0] = action;
1454 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1455 ret = LOWORD(result);
1456 GlobalUnlock16(hglob16);
1457 GlobalFree16(hglob16);
1459 else if (es->word_break_proc)
1461 if(es->is_unicode)
1463 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1465 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1466 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1467 ret = wbpW(es->text + start, index, count, action);
1469 else
1471 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1472 INT countA;
1473 CHAR *textA;
1475 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1476 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1477 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1478 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1479 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1480 ret = wbpA(textA, index, countA, action);
1481 HeapFree(GetProcessHeap(), 0, textA);
1484 else
1485 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1487 return ret;
1491 /*********************************************************************
1493 * EDIT_CharFromPos
1495 * Beware: This is not the function called on EM_CHARFROMPOS
1496 * The position _can_ be outside the formatting / client
1497 * rectangle
1498 * The return value is only the character index
1501 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1503 INT index;
1504 HDC dc;
1505 HFONT old_font = 0;
1506 INT x_high = 0, x_low = 0;
1508 if (es->style & ES_MULTILINE) {
1509 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1510 INT line_index = 0;
1511 LINEDEF *line_def = es->first_line_def;
1512 INT low, high;
1513 while ((line > 0) && line_def->next) {
1514 line_index += line_def->length;
1515 line_def = line_def->next;
1516 line--;
1518 x += es->x_offset - es->format_rect.left;
1519 if (es->style & ES_RIGHT)
1520 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1521 else if (es->style & ES_CENTER)
1522 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1523 if (x >= line_def->width) {
1524 if (after_wrap)
1525 *after_wrap = (line_def->ending == END_WRAP);
1526 return line_index + line_def->net_length;
1528 if (x <= 0) {
1529 if (after_wrap)
1530 *after_wrap = FALSE;
1531 return line_index;
1533 dc = GetDC(es->hwndSelf);
1534 if (es->font)
1535 old_font = SelectObject(dc, es->font);
1536 low = line_index;
1537 high = line_index + line_def->net_length + 1;
1538 while (low < high - 1)
1540 INT mid = (low + high) / 2;
1541 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1542 if (x_now > x) {
1543 high = mid;
1544 x_high = x_now;
1545 } else {
1546 low = mid;
1547 x_low = x_now;
1550 if (abs(x_high - x) + 1 <= abs(x_low - x))
1551 index = high;
1552 else
1553 index = low;
1555 if (after_wrap)
1556 *after_wrap = ((index == line_index + line_def->net_length) &&
1557 (line_def->ending == END_WRAP));
1558 } else {
1559 LPWSTR text;
1560 SIZE size;
1561 if (after_wrap)
1562 *after_wrap = FALSE;
1563 x -= es->format_rect.left;
1564 if (!x)
1565 return es->x_offset;
1567 if (!es->x_offset)
1569 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1570 if (es->style & ES_RIGHT)
1571 x -= indent;
1572 else if (es->style & ES_CENTER)
1573 x -= indent / 2;
1576 text = EDIT_GetPasswordPointer_SL(es);
1577 dc = GetDC(es->hwndSelf);
1578 if (es->font)
1579 old_font = SelectObject(dc, es->font);
1580 if (x < 0)
1582 INT low = 0;
1583 INT high = es->x_offset;
1584 while (low < high - 1)
1586 INT mid = (low + high) / 2;
1587 GetTextExtentPoint32W( dc, text + mid,
1588 es->x_offset - mid, &size );
1589 if (size.cx > -x) {
1590 low = mid;
1591 x_low = size.cx;
1592 } else {
1593 high = mid;
1594 x_high = size.cx;
1597 if (abs(x_high + x) <= abs(x_low + x) + 1)
1598 index = high;
1599 else
1600 index = low;
1602 else
1604 INT low = es->x_offset;
1605 INT high = get_text_length(es) + 1;
1606 while (low < high - 1)
1608 INT mid = (low + high) / 2;
1609 GetTextExtentPoint32W( dc, text + es->x_offset,
1610 mid - es->x_offset, &size );
1611 if (size.cx > x) {
1612 high = mid;
1613 x_high = size.cx;
1614 } else {
1615 low = mid;
1616 x_low = size.cx;
1619 if (abs(x_high - x) <= abs(x_low - x) + 1)
1620 index = high;
1621 else
1622 index = low;
1624 if (es->style & ES_PASSWORD)
1625 HeapFree(GetProcessHeap(), 0, text);
1627 if (es->font)
1628 SelectObject(dc, old_font);
1629 ReleaseDC(es->hwndSelf, dc);
1630 return index;
1634 /*********************************************************************
1636 * EDIT_ConfinePoint
1638 * adjusts the point to be within the formatting rectangle
1639 * (so CharFromPos returns the nearest _visible_ character)
1642 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
1644 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1645 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1649 /*********************************************************************
1651 * EDIT_GetLineRect
1653 * Calculates the bounding rectangle for a line from a starting
1654 * column to an ending column.
1657 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1659 INT line_index = EDIT_EM_LineIndex(es, line);
1661 if (es->style & ES_MULTILINE)
1662 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1663 else
1664 rc->top = es->format_rect.top;
1665 rc->bottom = rc->top + es->line_height;
1666 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1667 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1671 /*********************************************************************
1673 * EDIT_GetPasswordPointer_SL
1675 * note: caller should free the (optionally) allocated buffer
1678 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1680 if (es->style & ES_PASSWORD) {
1681 INT len = get_text_length(es);
1682 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1683 text[len] = '\0';
1684 while(len) text[--len] = es->password_char;
1685 return text;
1686 } else
1687 return es->text;
1691 /*********************************************************************
1693 * EDIT_LockBuffer
1695 * This acts as a LocalLock16(), but it locks only once. This way
1696 * you can call it whenever you like, without unlocking.
1698 * Initially the edit control allocates a HLOCAL32 buffer
1699 * (32 bit linear memory handler). However, 16 bit application
1700 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1701 * handler). From that moment on we have to keep using this 16 bit memory
1702 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1703 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1704 * conversion.
1707 static void EDIT_LockBuffer(EDITSTATE *es)
1709 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1710 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1712 if (!es->text) {
1713 CHAR *textA = NULL;
1714 UINT countA = 0;
1715 BOOL _16bit = FALSE;
1717 if(es->hloc32W)
1719 if(es->hloc32A)
1721 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1722 textA = LocalLock(es->hloc32A);
1723 countA = strlen(textA) + 1;
1725 else if(es->hloc16)
1727 HANDLE16 oldDS = stack16->ds;
1728 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1729 stack16->ds = hInstance;
1730 textA = MapSL(LocalLock16(es->hloc16));
1731 stack16->ds = oldDS;
1732 countA = strlen(textA) + 1;
1733 _16bit = TRUE;
1736 else {
1737 ERR("no buffer ... please report\n");
1738 return;
1741 if(textA)
1743 HLOCAL hloc32W_new;
1744 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1745 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1746 if(countW_new > es->buffer_size + 1)
1748 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1749 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1750 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1751 if(hloc32W_new)
1753 es->hloc32W = hloc32W_new;
1754 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1755 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1757 else
1758 WARN("FAILED! Will synchronize partially\n");
1762 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1763 es->text = LocalLock(es->hloc32W);
1765 if(textA)
1767 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1768 if(_16bit)
1770 HANDLE16 oldDS = stack16->ds;
1771 stack16->ds = hInstance;
1772 LocalUnlock16(es->hloc16);
1773 stack16->ds = oldDS;
1775 else
1776 LocalUnlock(es->hloc32A);
1779 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1780 es->lock_count++;
1784 /*********************************************************************
1786 * EDIT_SL_InvalidateText
1788 * Called from EDIT_InvalidateText().
1789 * Does the job for single-line controls only.
1792 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1794 RECT line_rect;
1795 RECT rc;
1797 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1798 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1799 EDIT_UpdateText(es, &rc, TRUE);
1803 /*********************************************************************
1805 * EDIT_ML_InvalidateText
1807 * Called from EDIT_InvalidateText().
1808 * Does the job for multi-line controls only.
1811 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1813 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1814 INT sl = EDIT_EM_LineFromChar(es, start);
1815 INT el = EDIT_EM_LineFromChar(es, end);
1816 INT sc;
1817 INT ec;
1818 RECT rc1;
1819 RECT rcWnd;
1820 RECT rcLine;
1821 RECT rcUpdate;
1822 INT l;
1824 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1825 return;
1827 sc = start - EDIT_EM_LineIndex(es, sl);
1828 ec = end - EDIT_EM_LineIndex(es, el);
1829 if (sl < es->y_offset) {
1830 sl = es->y_offset;
1831 sc = 0;
1833 if (el > es->y_offset + vlc) {
1834 el = es->y_offset + vlc;
1835 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1837 GetClientRect(es->hwndSelf, &rc1);
1838 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1839 if (sl == el) {
1840 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1841 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1842 EDIT_UpdateText(es, &rcUpdate, TRUE);
1843 } else {
1844 EDIT_GetLineRect(es, sl, sc,
1845 EDIT_EM_LineLength(es,
1846 EDIT_EM_LineIndex(es, sl)),
1847 &rcLine);
1848 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1849 EDIT_UpdateText(es, &rcUpdate, TRUE);
1850 for (l = sl + 1 ; l < el ; l++) {
1851 EDIT_GetLineRect(es, l, 0,
1852 EDIT_EM_LineLength(es,
1853 EDIT_EM_LineIndex(es, l)),
1854 &rcLine);
1855 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1856 EDIT_UpdateText(es, &rcUpdate, TRUE);
1858 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1859 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1860 EDIT_UpdateText(es, &rcUpdate, TRUE);
1865 /*********************************************************************
1867 * EDIT_InvalidateText
1869 * Invalidate the text from offset start up to, but not including,
1870 * offset end. Useful for (re)painting the selection.
1871 * Regions outside the linewidth are not invalidated.
1872 * end == -1 means end == TextLength.
1873 * start and end need not be ordered.
1876 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1878 if (end == start)
1879 return;
1881 if (end == -1)
1882 end = get_text_length(es);
1884 if (end < start) {
1885 INT tmp = start;
1886 start = end;
1887 end = tmp;
1890 if (es->style & ES_MULTILINE)
1891 EDIT_ML_InvalidateText(es, start, end);
1892 else
1893 EDIT_SL_InvalidateText(es, start, end);
1897 /*********************************************************************
1899 * EDIT_MakeFit
1901 * Try to fit size + 1 characters in the buffer.
1903 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1905 HLOCAL hNew32W;
1907 if (size <= es->buffer_size)
1908 return TRUE;
1910 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1912 /* Force edit to unlock it's buffer. es->text now NULL */
1913 EDIT_UnlockBuffer(es, TRUE);
1915 if (es->hloc32W) {
1916 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1917 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1918 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1919 es->hloc32W = hNew32W;
1920 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1924 EDIT_LockBuffer(es);
1926 if (es->buffer_size < size) {
1927 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1928 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1929 return FALSE;
1930 } else {
1931 TRACE("We now have %d+1\n", es->buffer_size);
1932 return TRUE;
1937 /*********************************************************************
1939 * EDIT_MakeUndoFit
1941 * Try to fit size + 1 bytes in the undo buffer.
1944 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1946 UINT alloc_size;
1948 if (size <= es->undo_buffer_size)
1949 return TRUE;
1951 TRACE("trying to ReAlloc to %d+1\n", size);
1953 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1954 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1955 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1956 return TRUE;
1958 else
1960 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1961 return FALSE;
1966 /*********************************************************************
1968 * EDIT_MoveBackward
1971 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1973 INT e = es->selection_end;
1975 if (e) {
1976 e--;
1977 if ((es->style & ES_MULTILINE) && e &&
1978 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1979 e--;
1980 if (e && (es->text[e - 1] == '\r'))
1981 e--;
1984 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1985 EDIT_EM_ScrollCaret(es);
1989 /*********************************************************************
1991 * EDIT_MoveDown_ML
1993 * Only for multi line controls
1994 * Move the caret one line down, on a column with the nearest
1995 * x coordinate on the screen (might be a different column).
1998 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
2000 INT s = es->selection_start;
2001 INT e = es->selection_end;
2002 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2003 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2004 INT x = (short)LOWORD(pos);
2005 INT y = (short)HIWORD(pos);
2007 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
2008 if (!extend)
2009 s = e;
2010 EDIT_EM_SetSel(es, s, e, after_wrap);
2011 EDIT_EM_ScrollCaret(es);
2015 /*********************************************************************
2017 * EDIT_MoveEnd
2020 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
2022 BOOL after_wrap = FALSE;
2023 INT e;
2025 /* Pass a high value in x to make sure of receiving the end of the line */
2026 if (es->style & ES_MULTILINE)
2027 e = EDIT_CharFromPos(es, 0x3fffffff,
2028 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2029 else
2030 e = get_text_length(es);
2031 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2032 EDIT_EM_ScrollCaret(es);
2036 /*********************************************************************
2038 * EDIT_MoveForward
2041 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2043 INT e = es->selection_end;
2045 if (es->text[e]) {
2046 e++;
2047 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2048 if (es->text[e] == '\n')
2049 e++;
2050 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2051 e += 2;
2054 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2055 EDIT_EM_ScrollCaret(es);
2059 /*********************************************************************
2061 * EDIT_MoveHome
2063 * Home key: move to beginning of line.
2066 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2068 INT e;
2070 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2071 if (es->style & ES_MULTILINE)
2072 e = EDIT_CharFromPos(es, -es->x_offset,
2073 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2074 else
2075 e = 0;
2076 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2077 EDIT_EM_ScrollCaret(es);
2081 /*********************************************************************
2083 * EDIT_MovePageDown_ML
2085 * Only for multi line controls
2086 * Move the caret one page down, on a column with the nearest
2087 * x coordinate on the screen (might be a different column).
2090 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2092 INT s = es->selection_start;
2093 INT e = es->selection_end;
2094 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2095 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2096 INT x = (short)LOWORD(pos);
2097 INT y = (short)HIWORD(pos);
2099 e = EDIT_CharFromPos(es, x,
2100 y + (es->format_rect.bottom - es->format_rect.top),
2101 &after_wrap);
2102 if (!extend)
2103 s = e;
2104 EDIT_EM_SetSel(es, s, e, after_wrap);
2105 EDIT_EM_ScrollCaret(es);
2109 /*********************************************************************
2111 * EDIT_MovePageUp_ML
2113 * Only for multi line controls
2114 * Move the caret one page up, on a column with the nearest
2115 * x coordinate on the screen (might be a different column).
2118 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2120 INT s = es->selection_start;
2121 INT e = es->selection_end;
2122 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2123 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2124 INT x = (short)LOWORD(pos);
2125 INT y = (short)HIWORD(pos);
2127 e = EDIT_CharFromPos(es, x,
2128 y - (es->format_rect.bottom - es->format_rect.top),
2129 &after_wrap);
2130 if (!extend)
2131 s = e;
2132 EDIT_EM_SetSel(es, s, e, after_wrap);
2133 EDIT_EM_ScrollCaret(es);
2137 /*********************************************************************
2139 * EDIT_MoveUp_ML
2141 * Only for multi line controls
2142 * Move the caret one line up, on a column with the nearest
2143 * x coordinate on the screen (might be a different column).
2146 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2148 INT s = es->selection_start;
2149 INT e = es->selection_end;
2150 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2151 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2152 INT x = (short)LOWORD(pos);
2153 INT y = (short)HIWORD(pos);
2155 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2156 if (!extend)
2157 s = e;
2158 EDIT_EM_SetSel(es, s, e, after_wrap);
2159 EDIT_EM_ScrollCaret(es);
2163 /*********************************************************************
2165 * EDIT_MoveWordBackward
2168 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2170 INT s = es->selection_start;
2171 INT e = es->selection_end;
2172 INT l;
2173 INT ll;
2174 INT li;
2176 l = EDIT_EM_LineFromChar(es, e);
2177 ll = EDIT_EM_LineLength(es, e);
2178 li = EDIT_EM_LineIndex(es, l);
2179 if (e - li == 0) {
2180 if (l) {
2181 li = EDIT_EM_LineIndex(es, l - 1);
2182 e = li + EDIT_EM_LineLength(es, li);
2184 } else {
2185 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2187 if (!extend)
2188 s = e;
2189 EDIT_EM_SetSel(es, s, e, FALSE);
2190 EDIT_EM_ScrollCaret(es);
2194 /*********************************************************************
2196 * EDIT_MoveWordForward
2199 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2201 INT s = es->selection_start;
2202 INT e = es->selection_end;
2203 INT l;
2204 INT ll;
2205 INT li;
2207 l = EDIT_EM_LineFromChar(es, e);
2208 ll = EDIT_EM_LineLength(es, e);
2209 li = EDIT_EM_LineIndex(es, l);
2210 if (e - li == ll) {
2211 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2212 e = EDIT_EM_LineIndex(es, l + 1);
2213 } else {
2214 e = li + EDIT_CallWordBreakProc(es,
2215 li, e - li + 1, ll, WB_RIGHT);
2217 if (!extend)
2218 s = e;
2219 EDIT_EM_SetSel(es, s, e, FALSE);
2220 EDIT_EM_ScrollCaret(es);
2224 /*********************************************************************
2226 * EDIT_PaintLine
2229 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2231 INT s = es->selection_start;
2232 INT e = es->selection_end;
2233 INT li;
2234 INT ll;
2235 INT x;
2236 INT y;
2237 LRESULT pos;
2239 if (es->style & ES_MULTILINE) {
2240 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2241 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2242 return;
2243 } else if (line)
2244 return;
2246 TRACE("line=%d\n", line);
2248 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2249 x = (short)LOWORD(pos);
2250 y = (short)HIWORD(pos);
2251 li = EDIT_EM_LineIndex(es, line);
2252 ll = EDIT_EM_LineLength(es, li);
2253 s = min(es->selection_start, es->selection_end);
2254 e = max(es->selection_start, es->selection_end);
2255 s = min(li + ll, max(li, s));
2256 e = min(li + ll, max(li, e));
2257 if (rev && (s != e) &&
2258 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2259 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2260 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2261 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2262 } else
2263 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2267 /*********************************************************************
2269 * EDIT_PaintText
2272 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2274 COLORREF BkColor;
2275 COLORREF TextColor;
2276 LOGFONTW underline_font;
2277 HFONT hUnderline = 0;
2278 HFONT old_font = 0;
2279 INT ret;
2280 INT li;
2281 INT BkMode;
2282 SIZE size;
2284 if (!count)
2285 return 0;
2286 BkMode = GetBkMode(dc);
2287 BkColor = GetBkColor(dc);
2288 TextColor = GetTextColor(dc);
2289 if (rev) {
2290 if (es->composition_len == 0)
2292 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2293 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2294 SetBkMode( dc, OPAQUE);
2296 else
2298 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2299 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2300 underline_font.lfUnderline = TRUE;
2301 hUnderline = CreateFontIndirectW(&underline_font);
2302 old_font = SelectObject(dc,hUnderline);
2305 li = EDIT_EM_LineIndex(es, line);
2306 if (es->style & ES_MULTILINE) {
2307 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2308 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2309 } else {
2310 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2311 TextOutW(dc, x, y, text + li + col, count);
2312 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2313 ret = size.cx;
2314 if (es->style & ES_PASSWORD)
2315 HeapFree(GetProcessHeap(), 0, text);
2317 if (rev) {
2318 if (es->composition_len == 0)
2320 SetBkColor(dc, BkColor);
2321 SetTextColor(dc, TextColor);
2322 SetBkMode( dc, BkMode);
2324 else
2326 if (old_font)
2327 SelectObject(dc,old_font);
2328 if (hUnderline)
2329 DeleteObject(hUnderline);
2332 return ret;
2336 /*********************************************************************
2338 * EDIT_SetCaretPos
2341 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2342 BOOL after_wrap)
2344 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2345 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2346 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2350 /*********************************************************************
2352 * EDIT_AdjustFormatRect
2354 * Adjusts the format rectangle for the current font and the
2355 * current client rectangle.
2358 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2360 RECT ClientRect;
2362 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2363 if (es->style & ES_MULTILINE)
2365 INT fw, vlc, max_x_offset, max_y_offset;
2367 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2368 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2370 /* correct es->x_offset */
2371 fw = es->format_rect.right - es->format_rect.left;
2372 max_x_offset = es->text_width - fw;
2373 if(max_x_offset < 0) max_x_offset = 0;
2374 if(es->x_offset > max_x_offset)
2375 es->x_offset = max_x_offset;
2377 /* correct es->y_offset */
2378 max_y_offset = es->line_count - vlc;
2379 if(max_y_offset < 0) max_y_offset = 0;
2380 if(es->y_offset > max_y_offset)
2381 es->y_offset = max_y_offset;
2383 /* force scroll info update */
2384 EDIT_UpdateScrollInfo(es);
2386 else
2387 /* Windows doesn't care to fix text placement for SL controls */
2388 es->format_rect.bottom = es->format_rect.top + es->line_height;
2390 /* Always stay within the client area */
2391 GetClientRect(es->hwndSelf, &ClientRect);
2392 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2394 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2395 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2397 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2401 /*********************************************************************
2403 * EDIT_SetRectNP
2405 * note: this is not (exactly) the handler called on EM_SETRECTNP
2406 * it is also used to set the rect of a single line control
2409 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2411 LONG_PTR ExStyle;
2412 INT bw, bh;
2413 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2415 CopyRect(&es->format_rect, rc);
2417 if (ExStyle & WS_EX_CLIENTEDGE) {
2418 es->format_rect.left++;
2419 es->format_rect.right--;
2421 if (es->format_rect.bottom - es->format_rect.top
2422 >= es->line_height + 2)
2424 es->format_rect.top++;
2425 es->format_rect.bottom--;
2428 else if (es->style & WS_BORDER) {
2429 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2430 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2431 es->format_rect.left += bw;
2432 es->format_rect.right -= bw;
2433 if (es->format_rect.bottom - es->format_rect.top
2434 >= es->line_height + 2 * bh)
2436 es->format_rect.top += bh;
2437 es->format_rect.bottom -= bh;
2441 es->format_rect.left += es->left_margin;
2442 es->format_rect.right -= es->right_margin;
2443 EDIT_AdjustFormatRect(es);
2447 /*********************************************************************
2449 * EDIT_UnlockBuffer
2452 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2455 /* Edit window might be already destroyed */
2456 if(!IsWindow(es->hwndSelf))
2458 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2459 return;
2462 if (!es->lock_count) {
2463 ERR("lock_count == 0 ... please report\n");
2464 return;
2466 if (!es->text) {
2467 ERR("es->text == 0 ... please report\n");
2468 return;
2471 if (force || (es->lock_count == 1)) {
2472 if (es->hloc32W) {
2473 CHAR *textA = NULL;
2474 UINT countA = 0;
2475 UINT countW = get_text_length(es) + 1;
2476 STACK16FRAME* stack16 = NULL;
2477 HANDLE16 oldDS = 0;
2479 if(es->hloc32A)
2481 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2482 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2483 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2484 countA = LocalSize(es->hloc32A);
2485 if(countA_new > countA)
2487 HLOCAL hloc32A_new;
2488 UINT alloc_size = ROUND_TO_GROW(countA_new);
2489 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2490 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2491 if(hloc32A_new)
2493 es->hloc32A = hloc32A_new;
2494 countA = LocalSize(hloc32A_new);
2495 TRACE("Real new size %d bytes\n", countA);
2497 else
2498 WARN("FAILED! Will synchronize partially\n");
2500 textA = LocalLock(es->hloc32A);
2502 else if(es->hloc16)
2504 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2506 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2507 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2509 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2510 oldDS = stack16->ds;
2511 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2513 countA = LocalSize16(es->hloc16);
2514 if(countA_new > countA)
2516 HLOCAL16 hloc16_new;
2517 UINT alloc_size = ROUND_TO_GROW(countA_new);
2518 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2519 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2520 if(hloc16_new)
2522 es->hloc16 = hloc16_new;
2523 countA = LocalSize16(hloc16_new);
2524 TRACE("Real new size %d bytes\n", countA);
2526 else
2527 WARN("FAILED! Will synchronize partially\n");
2529 textA = MapSL(LocalLock16(es->hloc16));
2532 if(textA)
2534 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2535 if(stack16)
2536 LocalUnlock16(es->hloc16);
2537 else
2538 LocalUnlock(es->hloc32A);
2541 if (stack16) stack16->ds = oldDS;
2542 LocalUnlock(es->hloc32W);
2543 es->text = NULL;
2545 else {
2546 ERR("no buffer ... please report\n");
2547 return;
2550 es->lock_count--;
2554 /*********************************************************************
2556 * EDIT_UpdateScrollInfo
2559 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2561 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2563 SCROLLINFO si;
2564 si.cbSize = sizeof(SCROLLINFO);
2565 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2566 si.nMin = 0;
2567 si.nMax = es->line_count - 1;
2568 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2569 si.nPos = es->y_offset;
2570 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2571 si.nMin, si.nMax, si.nPage, si.nPos);
2572 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2575 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2577 SCROLLINFO si;
2578 si.cbSize = sizeof(SCROLLINFO);
2579 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2580 si.nMin = 0;
2581 si.nMax = es->text_width - 1;
2582 si.nPage = es->format_rect.right - es->format_rect.left;
2583 si.nPos = es->x_offset;
2584 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2585 si.nMin, si.nMax, si.nPage, si.nPos);
2586 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2590 /*********************************************************************
2592 * EDIT_WordBreakProc
2594 * Find the beginning of words.
2595 * Note: unlike the specs for a WordBreakProc, this function only
2596 * allows to be called without linebreaks between s[0] up to
2597 * s[count - 1]. Remember it is only called
2598 * internally, so we can decide this for ourselves.
2601 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2603 INT ret = 0;
2605 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2607 if(!s) return 0;
2609 switch (action) {
2610 case WB_LEFT:
2611 if (!count)
2612 break;
2613 if (index)
2614 index--;
2615 if (s[index] == ' ') {
2616 while (index && (s[index] == ' '))
2617 index--;
2618 if (index) {
2619 while (index && (s[index] != ' '))
2620 index--;
2621 if (s[index] == ' ')
2622 index++;
2624 } else {
2625 while (index && (s[index] != ' '))
2626 index--;
2627 if (s[index] == ' ')
2628 index++;
2630 ret = index;
2631 break;
2632 case WB_RIGHT:
2633 if (!count)
2634 break;
2635 if (index)
2636 index--;
2637 if (s[index] == ' ')
2638 while ((index < count) && (s[index] == ' ')) index++;
2639 else {
2640 while (s[index] && (s[index] != ' ') && (index < count))
2641 index++;
2642 while ((s[index] == ' ') && (index < count)) index++;
2644 ret = index;
2645 break;
2646 case WB_ISDELIMITER:
2647 ret = (s[index] == ' ');
2648 break;
2649 default:
2650 ERR("unknown action code, please report !\n");
2651 break;
2653 return ret;
2657 /*********************************************************************
2659 * EM_CHARFROMPOS
2661 * returns line number (not index) in high-order word of result.
2662 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2663 * to Richedit, not to the edit control. Original documentation is valid.
2664 * FIXME: do the specs mean to return -1 if outside client area or
2665 * if outside formatting rectangle ???
2668 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2670 POINT pt;
2671 RECT rc;
2672 INT index;
2674 pt.x = x;
2675 pt.y = y;
2676 GetClientRect(es->hwndSelf, &rc);
2677 if (!PtInRect(&rc, pt))
2678 return -1;
2680 index = EDIT_CharFromPos(es, x, y, NULL);
2681 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2685 /*********************************************************************
2687 * EM_FMTLINES
2689 * Enable or disable soft breaks.
2691 * This means: insert or remove the soft linebreak character (\r\r\n).
2692 * Take care to check if the text still fits the buffer after insertion.
2693 * If not, notify with EN_ERRSPACE.
2696 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2698 es->flags &= ~EF_USE_SOFTBRK;
2699 if (add_eol) {
2700 es->flags |= EF_USE_SOFTBRK;
2701 FIXME("soft break enabled, not implemented\n");
2703 return add_eol;
2707 /*********************************************************************
2709 * EM_GETHANDLE
2711 * Hopefully this won't fire back at us.
2712 * We always start with a fixed buffer in the local heap.
2713 * Despite of the documentation says that the local heap is used
2714 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2715 * buffer on the local heap.
2718 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2720 HLOCAL hLocal;
2722 if (!(es->style & ES_MULTILINE))
2723 return 0;
2725 if(es->is_unicode)
2726 hLocal = es->hloc32W;
2727 else
2729 if(!es->hloc32A)
2731 CHAR *textA;
2732 UINT countA, alloc_size;
2733 TRACE("Allocating 32-bit ANSI alias buffer\n");
2734 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2735 alloc_size = ROUND_TO_GROW(countA);
2736 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2738 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2739 return 0;
2741 textA = LocalLock(es->hloc32A);
2742 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2743 LocalUnlock(es->hloc32A);
2745 hLocal = es->hloc32A;
2748 es->flags |= EF_APP_HAS_HANDLE;
2749 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2750 return hLocal;
2754 /*********************************************************************
2756 * EM_GETHANDLE16
2758 * Hopefully this won't fire back at us.
2759 * We always start with a buffer in 32 bit linear memory.
2760 * However, with this message a 16 bit application requests
2761 * a handle of 16 bit local heap memory, where it expects to find
2762 * the text.
2763 * It's a pitty that from this moment on we have to use this
2764 * local heap, because applications may rely on the handle
2765 * in the future.
2767 * In this function we'll try to switch to local heap.
2769 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2771 CHAR *textA;
2772 UINT countA, alloc_size;
2773 STACK16FRAME* stack16;
2774 HANDLE16 oldDS;
2776 if (!(es->style & ES_MULTILINE))
2777 return 0;
2779 if (es->hloc16)
2780 return es->hloc16;
2782 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2783 oldDS = stack16->ds;
2784 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2786 if (!LocalHeapSize16()) {
2788 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2789 ERR("could not initialize local heap\n");
2790 goto done;
2792 TRACE("local heap initialized\n");
2795 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2796 alloc_size = ROUND_TO_GROW(countA);
2798 TRACE("Allocating 16-bit ANSI alias buffer\n");
2799 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2800 ERR("could not allocate new 16 bit buffer\n");
2801 goto done;
2804 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2805 ERR("could not lock new 16 bit buffer\n");
2806 LocalFree16(es->hloc16);
2807 es->hloc16 = 0;
2808 goto done;
2811 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2812 LocalUnlock16(es->hloc16);
2813 es->flags |= EF_APP_HAS_HANDLE;
2815 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2817 done:
2818 stack16->ds = oldDS;
2819 return es->hloc16;
2823 /*********************************************************************
2825 * EM_GETLINE
2828 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2830 LPWSTR src;
2831 INT line_len, dst_len;
2832 INT i;
2834 if (es->style & ES_MULTILINE) {
2835 if (line >= es->line_count)
2836 return 0;
2837 } else
2838 line = 0;
2839 i = EDIT_EM_LineIndex(es, line);
2840 src = es->text + i;
2841 line_len = EDIT_EM_LineLength(es, i);
2842 dst_len = *(WORD *)dst;
2843 if(unicode)
2845 if(dst_len <= line_len)
2847 memcpy(dst, src, dst_len * sizeof(WCHAR));
2848 return dst_len;
2850 else /* Append 0 if enough space */
2852 memcpy(dst, src, line_len * sizeof(WCHAR));
2853 dst[line_len] = 0;
2854 return line_len;
2857 else
2859 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2860 if(!ret && line_len) /* Insufficient buffer size */
2861 return dst_len;
2862 if(ret < dst_len) /* Append 0 if enough space */
2863 ((LPSTR)dst)[ret] = 0;
2864 return ret;
2869 /*********************************************************************
2871 * EM_GETSEL
2874 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2876 UINT s = es->selection_start;
2877 UINT e = es->selection_end;
2879 ORDER_UINT(s, e);
2880 if (start)
2881 *start = s;
2882 if (end)
2883 *end = e;
2884 return MAKELONG(s, e);
2888 /*********************************************************************
2890 * EM_GETTHUMB
2892 * FIXME: is this right ? (or should it be only VSCROLL)
2893 * (and maybe only for edit controls that really have their
2894 * own scrollbars) (and maybe only for multiline controls ?)
2895 * All in all: very poorly documented
2898 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2900 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2901 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2905 /*********************************************************************
2907 * EM_LINEFROMCHAR
2910 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2912 INT line;
2913 LINEDEF *line_def;
2915 if (!(es->style & ES_MULTILINE))
2916 return 0;
2917 if (index > (INT)get_text_length(es))
2918 return es->line_count - 1;
2919 if (index == -1)
2920 index = min(es->selection_start, es->selection_end);
2922 line = 0;
2923 line_def = es->first_line_def;
2924 index -= line_def->length;
2925 while ((index >= 0) && line_def->next) {
2926 line++;
2927 line_def = line_def->next;
2928 index -= line_def->length;
2930 return line;
2934 /*********************************************************************
2936 * EM_LINEINDEX
2939 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
2941 INT line_index;
2942 const LINEDEF *line_def;
2944 if (!(es->style & ES_MULTILINE))
2945 return 0;
2946 if (line >= es->line_count)
2947 return -1;
2949 line_index = 0;
2950 line_def = es->first_line_def;
2951 if (line == -1) {
2952 INT index = es->selection_end - line_def->length;
2953 while ((index >= 0) && line_def->next) {
2954 line_index += line_def->length;
2955 line_def = line_def->next;
2956 index -= line_def->length;
2958 } else {
2959 while (line > 0) {
2960 line_index += line_def->length;
2961 line_def = line_def->next;
2962 line--;
2965 return line_index;
2969 /*********************************************************************
2971 * EM_LINELENGTH
2974 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2976 LINEDEF *line_def;
2978 if (!(es->style & ES_MULTILINE))
2979 return get_text_length(es);
2981 if (index == -1) {
2982 /* get the number of remaining non-selected chars of selected lines */
2983 INT32 l; /* line number */
2984 INT32 li; /* index of first char in line */
2985 INT32 count;
2986 l = EDIT_EM_LineFromChar(es, es->selection_start);
2987 /* # chars before start of selection area */
2988 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2989 l = EDIT_EM_LineFromChar(es, es->selection_end);
2990 /* # chars after end of selection */
2991 li = EDIT_EM_LineIndex(es, l);
2992 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2993 return count;
2995 line_def = es->first_line_def;
2996 index -= line_def->length;
2997 while ((index >= 0) && line_def->next) {
2998 line_def = line_def->next;
2999 index -= line_def->length;
3001 return line_def->net_length;
3005 /*********************************************************************
3007 * EM_LINESCROLL
3009 * NOTE: dx is in average character widths, dy - in lines;
3012 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
3014 if (!(es->style & ES_MULTILINE))
3015 return FALSE;
3017 dx *= es->char_width;
3018 return EDIT_EM_LineScroll_internal(es, dx, dy);
3021 /*********************************************************************
3023 * EDIT_EM_LineScroll_internal
3025 * Version of EDIT_EM_LineScroll for internal use.
3026 * It doesn't refuse if ES_MULTILINE is set and assumes that
3027 * dx is in pixels, dy - in lines.
3030 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3032 INT nyoff;
3033 INT x_offset_in_pixels;
3034 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3035 es->line_height;
3037 if (es->style & ES_MULTILINE)
3039 x_offset_in_pixels = es->x_offset;
3041 else
3043 dy = 0;
3044 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3047 if (-dx > x_offset_in_pixels)
3048 dx = -x_offset_in_pixels;
3049 if (dx > es->text_width - x_offset_in_pixels)
3050 dx = es->text_width - x_offset_in_pixels;
3051 nyoff = max(0, es->y_offset + dy);
3052 if (nyoff >= es->line_count - lines_per_page)
3053 nyoff = max(0, es->line_count - lines_per_page);
3054 dy = (es->y_offset - nyoff) * es->line_height;
3055 if (dx || dy) {
3056 RECT rc1;
3057 RECT rc;
3059 es->y_offset = nyoff;
3060 if(es->style & ES_MULTILINE)
3061 es->x_offset += dx;
3062 else
3063 es->x_offset += dx / es->char_width;
3065 GetClientRect(es->hwndSelf, &rc1);
3066 IntersectRect(&rc, &rc1, &es->format_rect);
3067 ScrollWindowEx(es->hwndSelf, -dx, dy,
3068 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3069 /* force scroll info update */
3070 EDIT_UpdateScrollInfo(es);
3072 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3073 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3074 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3075 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3076 return TRUE;
3080 /*********************************************************************
3082 * EM_POSFROMCHAR
3085 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3087 INT len = get_text_length(es);
3088 INT l;
3089 INT li;
3090 INT x;
3091 INT y = 0;
3092 INT w;
3093 INT lw = 0;
3094 INT ll = 0;
3095 HDC dc;
3096 HFONT old_font = 0;
3097 SIZE size;
3098 LINEDEF *line_def;
3100 index = min(index, len);
3101 dc = GetDC(es->hwndSelf);
3102 if (es->font)
3103 old_font = SelectObject(dc, es->font);
3104 if (es->style & ES_MULTILINE) {
3105 l = EDIT_EM_LineFromChar(es, index);
3106 y = (l - es->y_offset) * es->line_height;
3107 li = EDIT_EM_LineIndex(es, l);
3108 if (after_wrap && (li == index) && l) {
3109 INT l2 = l - 1;
3110 line_def = es->first_line_def;
3111 while (l2) {
3112 line_def = line_def->next;
3113 l2--;
3115 if (line_def->ending == END_WRAP) {
3116 l--;
3117 y -= es->line_height;
3118 li = EDIT_EM_LineIndex(es, l);
3122 line_def = es->first_line_def;
3123 while (line_def->index != li)
3124 line_def = line_def->next;
3126 ll = line_def->net_length;
3127 lw = line_def->width;
3129 w = es->format_rect.right - es->format_rect.left;
3130 if (es->style & ES_RIGHT)
3132 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3133 es->tabs_count, es->tabs)) - es->x_offset;
3134 x = w - x;
3136 else if (es->style & ES_CENTER)
3138 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3139 es->tabs_count, es->tabs)) - es->x_offset;
3140 x += (w - lw) / 2;
3142 else /* ES_LEFT */
3144 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3145 es->tabs_count, es->tabs)) - es->x_offset;
3147 } else {
3148 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3149 if (index < es->x_offset) {
3150 GetTextExtentPoint32W(dc, text + index,
3151 es->x_offset - index, &size);
3152 x = -size.cx;
3153 } else {
3154 GetTextExtentPoint32W(dc, text + es->x_offset,
3155 index - es->x_offset, &size);
3156 x = size.cx;
3158 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3160 w = es->format_rect.right - es->format_rect.left;
3161 if (w > es->text_width)
3163 if (es->style & ES_RIGHT)
3164 x += w - es->text_width;
3165 else if (es->style & ES_CENTER)
3166 x += (w - es->text_width) / 2;
3170 y = 0;
3171 if (es->style & ES_PASSWORD)
3172 HeapFree(GetProcessHeap(), 0, text);
3174 x += es->format_rect.left;
3175 y += es->format_rect.top;
3176 if (es->font)
3177 SelectObject(dc, old_font);
3178 ReleaseDC(es->hwndSelf, dc);
3179 return MAKELONG((INT16)x, (INT16)y);
3183 /*********************************************************************
3185 * EM_REPLACESEL
3187 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3190 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3192 UINT strl = strlenW(lpsz_replace);
3193 UINT tl = get_text_length(es);
3194 UINT utl;
3195 UINT s;
3196 UINT e;
3197 UINT i;
3198 UINT size;
3199 LPWSTR p;
3200 HRGN hrgn = 0;
3201 LPWSTR buf = NULL;
3202 UINT bufl = 0;
3204 TRACE("%s, can_undo %d, send_update %d\n",
3205 debugstr_w(lpsz_replace), can_undo, send_update);
3207 s = es->selection_start;
3208 e = es->selection_end;
3210 if ((s == e) && !strl)
3211 return;
3213 ORDER_UINT(s, e);
3215 size = tl - (e - s) + strl;
3216 if (!size)
3217 es->text_width = 0;
3219 /* Issue the EN_MAXTEXT notification and continue with replacing text
3220 * such that buffer limit is honored. */
3221 if ((honor_limit) && (size > es->buffer_limit)) {
3222 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3223 /* Buffer limit can be smaller than the actual length of text in combobox */
3224 if (es->buffer_limit < (tl - (e-s)))
3225 strl = 0;
3226 else
3227 strl = es->buffer_limit - (tl - (e-s));
3230 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3231 return;
3233 if (e != s) {
3234 /* there is something to be deleted */
3235 TRACE("deleting stuff.\n");
3236 bufl = e - s;
3237 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3238 if (!buf) return;
3239 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3240 buf[bufl] = 0; /* ensure 0 termination */
3241 /* now delete */
3242 strcpyW(es->text + s, es->text + e);
3243 text_buffer_changed(es);
3245 if (strl) {
3246 /* there is an insertion */
3247 tl = get_text_length(es);
3248 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));
3249 for (p = es->text + tl ; p >= es->text + s ; p--)
3250 p[strl] = p[0];
3251 for (i = 0 , p = es->text + s ; i < strl ; i++)
3252 p[i] = lpsz_replace[i];
3253 if(es->style & ES_UPPERCASE)
3254 CharUpperBuffW(p, strl);
3255 else if(es->style & ES_LOWERCASE)
3256 CharLowerBuffW(p, strl);
3257 text_buffer_changed(es);
3259 if (es->style & ES_MULTILINE)
3261 INT st = min(es->selection_start, es->selection_end);
3262 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3264 hrgn = CreateRectRgn(0, 0, 0, 0);
3265 EDIT_BuildLineDefs_ML(es, st, st + strl,
3266 strl - abs(es->selection_end - es->selection_start), hrgn);
3267 /* if text is too long undo all changes */
3268 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3269 if (strl)
3270 strcpyW(es->text + e, es->text + e + strl);
3271 if (e != s)
3272 for (i = 0 , p = es->text ; i < e - s ; i++)
3273 p[i + s] = buf[i];
3274 text_buffer_changed(es);
3275 EDIT_BuildLineDefs_ML(es, s, e,
3276 abs(es->selection_end - es->selection_start) - strl, hrgn);
3277 strl = 0;
3278 e = s;
3279 hrgn = CreateRectRgn(0, 0, 0, 0);
3280 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3283 else {
3284 INT fw = es->format_rect.right - es->format_rect.left;
3285 EDIT_CalcLineWidth_SL(es);
3286 /* remove chars that don't fit */
3287 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3288 while ((es->text_width > fw) && s + strl >= s) {
3289 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3290 strl--;
3291 EDIT_CalcLineWidth_SL(es);
3293 text_buffer_changed(es);
3294 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3298 if (e != s) {
3299 if (can_undo) {
3300 utl = strlenW(es->undo_text);
3301 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3302 /* undo-buffer is extended to the right */
3303 EDIT_MakeUndoFit(es, utl + e - s);
3304 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3305 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3306 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3307 /* undo-buffer is extended to the left */
3308 EDIT_MakeUndoFit(es, utl + e - s);
3309 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3310 p[e - s] = p[0];
3311 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3312 p[i] = buf[i];
3313 es->undo_position = s;
3314 } else {
3315 /* new undo-buffer */
3316 EDIT_MakeUndoFit(es, e - s);
3317 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3318 es->undo_text[e - s] = 0; /* ensure 0 termination */
3319 es->undo_position = s;
3321 /* any deletion makes the old insertion-undo invalid */
3322 es->undo_insert_count = 0;
3323 } else
3324 EDIT_EM_EmptyUndoBuffer(es);
3326 if (strl) {
3327 if (can_undo) {
3328 if ((s == es->undo_position) ||
3329 ((es->undo_insert_count) &&
3330 (s == es->undo_position + es->undo_insert_count)))
3332 * insertion is new and at delete position or
3333 * an extension to either left or right
3335 es->undo_insert_count += strl;
3336 else {
3337 /* new insertion undo */
3338 es->undo_position = s;
3339 es->undo_insert_count = strl;
3340 /* new insertion makes old delete-buffer invalid */
3341 *es->undo_text = '\0';
3343 } else
3344 EDIT_EM_EmptyUndoBuffer(es);
3347 if (bufl)
3348 HeapFree(GetProcessHeap(), 0, buf);
3350 s += strl;
3352 /* If text has been deleted and we're right or center aligned then scroll rightward */
3353 if (es->style & (ES_RIGHT | ES_CENTER))
3355 INT delta = strl - abs(es->selection_end - es->selection_start);
3357 if (delta < 0 && es->x_offset)
3359 if (abs(delta) > es->x_offset)
3360 es->x_offset = 0;
3361 else
3362 es->x_offset += delta;
3366 EDIT_EM_SetSel(es, s, s, FALSE);
3367 es->flags |= EF_MODIFIED;
3368 if (send_update) es->flags |= EF_UPDATE;
3369 if (hrgn)
3371 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3372 DeleteObject(hrgn);
3374 else
3375 EDIT_UpdateText(es, NULL, TRUE);
3377 EDIT_EM_ScrollCaret(es);
3379 /* force scroll info update */
3380 EDIT_UpdateScrollInfo(es);
3383 if(send_update || (es->flags & EF_UPDATE))
3385 es->flags &= ~EF_UPDATE;
3386 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3391 /*********************************************************************
3393 * EM_SCROLL
3396 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3398 INT dy;
3400 if (!(es->style & ES_MULTILINE))
3401 return (LRESULT)FALSE;
3403 dy = 0;
3405 switch (action) {
3406 case SB_LINEUP:
3407 if (es->y_offset)
3408 dy = -1;
3409 break;
3410 case SB_LINEDOWN:
3411 if (es->y_offset < es->line_count - 1)
3412 dy = 1;
3413 break;
3414 case SB_PAGEUP:
3415 if (es->y_offset)
3416 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3417 break;
3418 case SB_PAGEDOWN:
3419 if (es->y_offset < es->line_count - 1)
3420 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3421 break;
3422 default:
3423 return (LRESULT)FALSE;
3425 if (dy) {
3426 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3427 /* check if we are going to move too far */
3428 if(es->y_offset + dy > es->line_count - vlc)
3429 dy = es->line_count - vlc - es->y_offset;
3431 /* Notification is done in EDIT_EM_LineScroll */
3432 if(dy)
3433 EDIT_EM_LineScroll(es, 0, dy);
3435 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3439 /*********************************************************************
3441 * EM_SCROLLCARET
3444 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3446 if (es->style & ES_MULTILINE) {
3447 INT l;
3448 INT li;
3449 INT vlc;
3450 INT ww;
3451 INT cw = es->char_width;
3452 INT x;
3453 INT dy = 0;
3454 INT dx = 0;
3456 l = EDIT_EM_LineFromChar(es, es->selection_end);
3457 li = EDIT_EM_LineIndex(es, l);
3458 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3459 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3460 if (l >= es->y_offset + vlc)
3461 dy = l - vlc + 1 - es->y_offset;
3462 if (l < es->y_offset)
3463 dy = l - es->y_offset;
3464 ww = es->format_rect.right - es->format_rect.left;
3465 if (x < es->format_rect.left)
3466 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3467 if (x > es->format_rect.right)
3468 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3469 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3471 /* check if we are going to move too far */
3472 if(es->x_offset + dx + ww > es->text_width)
3473 dx = es->text_width - ww - es->x_offset;
3474 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3475 EDIT_EM_LineScroll_internal(es, dx, dy);
3477 } else {
3478 INT x;
3479 INT goal;
3480 INT format_width;
3482 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3483 format_width = es->format_rect.right - es->format_rect.left;
3484 if (x < es->format_rect.left) {
3485 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3486 do {
3487 es->x_offset--;
3488 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3489 } while ((x < goal) && es->x_offset);
3490 /* FIXME: use ScrollWindow() somehow to improve performance */
3491 EDIT_UpdateText(es, NULL, TRUE);
3492 } else if (x > es->format_rect.right) {
3493 INT x_last;
3494 INT len = get_text_length(es);
3495 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3496 do {
3497 es->x_offset++;
3498 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3499 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3500 } while ((x > goal) && (x_last > es->format_rect.right));
3501 /* FIXME: use ScrollWindow() somehow to improve performance */
3502 EDIT_UpdateText(es, NULL, TRUE);
3506 if(es->flags & EF_FOCUSED)
3507 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3511 /*********************************************************************
3513 * EM_SETHANDLE
3515 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3518 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3520 if (!(es->style & ES_MULTILINE))
3521 return;
3523 if (!hloc) {
3524 WARN("called with NULL handle\n");
3525 return;
3528 EDIT_UnlockBuffer(es, TRUE);
3530 if(es->hloc16)
3532 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3533 HANDLE16 oldDS = stack16->ds;
3535 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3536 LocalFree16(es->hloc16);
3537 stack16->ds = oldDS;
3538 es->hloc16 = 0;
3541 if(es->is_unicode)
3543 if(es->hloc32A)
3545 LocalFree(es->hloc32A);
3546 es->hloc32A = NULL;
3548 es->hloc32W = hloc;
3550 else
3552 INT countW, countA;
3553 HLOCAL hloc32W_new;
3554 WCHAR *textW;
3555 CHAR *textA;
3557 countA = LocalSize(hloc);
3558 textA = LocalLock(hloc);
3559 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3560 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3562 ERR("Could not allocate new unicode buffer\n");
3563 return;
3565 textW = LocalLock(hloc32W_new);
3566 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3567 LocalUnlock(hloc32W_new);
3568 LocalUnlock(hloc);
3570 if(es->hloc32W)
3571 LocalFree(es->hloc32W);
3573 es->hloc32W = hloc32W_new;
3574 es->hloc32A = hloc;
3577 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3579 es->flags |= EF_APP_HAS_HANDLE;
3580 EDIT_LockBuffer(es);
3582 es->x_offset = es->y_offset = 0;
3583 es->selection_start = es->selection_end = 0;
3584 EDIT_EM_EmptyUndoBuffer(es);
3585 es->flags &= ~EF_MODIFIED;
3586 es->flags &= ~EF_UPDATE;
3587 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3588 EDIT_UpdateText(es, NULL, TRUE);
3589 EDIT_EM_ScrollCaret(es);
3590 /* force scroll info update */
3591 EDIT_UpdateScrollInfo(es);
3595 /*********************************************************************
3597 * EM_SETHANDLE16
3599 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3602 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3604 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3605 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3606 HANDLE16 oldDS = stack16->ds;
3607 INT countW, countA;
3608 HLOCAL hloc32W_new;
3609 WCHAR *textW;
3610 CHAR *textA;
3612 if (!(es->style & ES_MULTILINE))
3613 return;
3615 if (!hloc) {
3616 WARN("called with NULL handle\n");
3617 return;
3620 EDIT_UnlockBuffer(es, TRUE);
3622 if(es->hloc32A)
3624 LocalFree(es->hloc32A);
3625 es->hloc32A = NULL;
3628 stack16->ds = hInstance;
3629 countA = LocalSize16(hloc);
3630 textA = MapSL(LocalLock16(hloc));
3631 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3632 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3634 ERR("Could not allocate new unicode buffer\n");
3635 return;
3637 textW = LocalLock(hloc32W_new);
3638 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3639 LocalUnlock(hloc32W_new);
3640 LocalUnlock16(hloc);
3641 stack16->ds = oldDS;
3643 if(es->hloc32W)
3644 LocalFree(es->hloc32W);
3646 es->hloc32W = hloc32W_new;
3647 es->hloc16 = hloc;
3649 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3651 es->flags |= EF_APP_HAS_HANDLE;
3652 EDIT_LockBuffer(es);
3654 es->x_offset = es->y_offset = 0;
3655 es->selection_start = es->selection_end = 0;
3656 EDIT_EM_EmptyUndoBuffer(es);
3657 es->flags &= ~EF_MODIFIED;
3658 es->flags &= ~EF_UPDATE;
3659 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3660 EDIT_UpdateText(es, NULL, TRUE);
3661 EDIT_EM_ScrollCaret(es);
3662 /* force scroll info update */
3663 EDIT_UpdateScrollInfo(es);
3667 /*********************************************************************
3669 * EM_SETLIMITTEXT
3671 * NOTE: this version currently implements WinNT limits
3674 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
3676 if (!limit) limit = ~0u;
3677 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
3678 es->buffer_limit = limit;
3682 /*********************************************************************
3684 * EM_SETMARGINS
3686 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3687 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3688 * margin according to the textmetrics of the current font.
3690 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3691 * of the char's width as the margin, but this is not how Windows handles this.
3692 * For all other fonts Windows sets the margins to zero.
3694 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3695 * edit control is equal to or larger than a certain size.
3696 * Interestingly if one subtracts both the left and right margins from
3697 * this size one always seems to get an even number. The extents of
3698 * the (four character) string "'**'" match this quite closely, so
3699 * we'll use this until we come up with a better idea.
3701 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3703 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3704 SIZE sz;
3706 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3707 return sz.cx + left + right;
3710 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3711 WORD left, WORD right, BOOL repaint)
3713 TEXTMETRICW tm;
3714 INT default_left_margin = 0; /* in pixels */
3715 INT default_right_margin = 0; /* in pixels */
3717 /* Set the default margins depending on the font */
3718 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3719 HDC dc = GetDC(es->hwndSelf);
3720 HFONT old_font = SelectObject(dc, es->font);
3721 GetTextMetricsW(dc, &tm);
3722 /* The default margins are only non zero for TrueType or Vector fonts */
3723 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3724 int min_size;
3725 RECT rc;
3726 /* This must be calculated more exactly! But how? */
3727 default_left_margin = tm.tmAveCharWidth / 2;
3728 default_right_margin = tm.tmAveCharWidth / 2;
3729 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3730 GetClientRect(es->hwndSelf, &rc);
3731 if(rc.right - rc.left < min_size) {
3732 default_left_margin = es->left_margin;
3733 default_right_margin = es->right_margin;
3736 SelectObject(dc, old_font);
3737 ReleaseDC(es->hwndSelf, dc);
3740 if (action & EC_LEFTMARGIN) {
3741 es->format_rect.left -= es->left_margin;
3742 if (left != EC_USEFONTINFO)
3743 es->left_margin = left;
3744 else
3745 es->left_margin = default_left_margin;
3746 es->format_rect.left += es->left_margin;
3749 if (action & EC_RIGHTMARGIN) {
3750 es->format_rect.right += es->right_margin;
3751 if (right != EC_USEFONTINFO)
3752 es->right_margin = right;
3753 else
3754 es->right_margin = default_right_margin;
3755 es->format_rect.right -= es->right_margin;
3758 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3759 EDIT_AdjustFormatRect(es);
3760 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3763 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3767 /*********************************************************************
3769 * EM_SETPASSWORDCHAR
3772 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3774 LONG style;
3776 if (es->style & ES_MULTILINE)
3777 return;
3779 if (es->password_char == c)
3780 return;
3782 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3783 es->password_char = c;
3784 if (c) {
3785 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3786 es->style |= ES_PASSWORD;
3787 } else {
3788 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3789 es->style &= ~ES_PASSWORD;
3791 EDIT_UpdateText(es, NULL, TRUE);
3795 /*********************************************************************
3797 * EDIT_EM_SetSel
3799 * note: unlike the specs say: the order of start and end
3800 * _is_ preserved in Windows. (i.e. start can be > end)
3801 * In other words: this handler is OK
3804 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3806 UINT old_start = es->selection_start;
3807 UINT old_end = es->selection_end;
3808 UINT len = get_text_length(es);
3810 if (start == (UINT)-1) {
3811 start = es->selection_end;
3812 end = es->selection_end;
3813 } else {
3814 start = min(start, len);
3815 end = min(end, len);
3817 es->selection_start = start;
3818 es->selection_end = end;
3819 if (after_wrap)
3820 es->flags |= EF_AFTER_WRAP;
3821 else
3822 es->flags &= ~EF_AFTER_WRAP;
3823 /* Compute the necessary invalidation region. */
3824 /* Note that we don't need to invalidate regions which have
3825 * "never" been selected, or those which are "still" selected.
3826 * In fact, every time we hit a selection boundary, we can
3827 * *toggle* whether we need to invalidate. Thus we can optimize by
3828 * *sorting* the interval endpoints. Let's assume that we sort them
3829 * in this order:
3830 * start <= end <= old_start <= old_end
3831 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3832 * in 5 comparisons; ie it's impossible to do better than the
3833 * following: */
3834 ORDER_UINT(end, old_end);
3835 ORDER_UINT(start, old_start);
3836 ORDER_UINT(old_start, old_end);
3837 ORDER_UINT(start, end);
3838 /* Note that at this point 'end' and 'old_start' are not in order, but
3839 * start is definitely the min. and old_end is definitely the max. */
3840 if (end != old_start)
3843 * One can also do
3844 * ORDER_UINT32(end, old_start);
3845 * EDIT_InvalidateText(es, start, end);
3846 * EDIT_InvalidateText(es, old_start, old_end);
3847 * in place of the following if statement.
3848 * (That would complete the optimal five-comparison four-element sort.)
3850 if (old_start > end )
3852 EDIT_InvalidateText(es, start, end);
3853 EDIT_InvalidateText(es, old_start, old_end);
3855 else
3857 EDIT_InvalidateText(es, start, old_start);
3858 EDIT_InvalidateText(es, end, old_end);
3861 else EDIT_InvalidateText(es, start, old_end);
3865 /*********************************************************************
3867 * EM_SETTABSTOPS
3870 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3872 if (!(es->style & ES_MULTILINE))
3873 return FALSE;
3874 HeapFree(GetProcessHeap(), 0, es->tabs);
3875 es->tabs_count = count;
3876 if (!count)
3877 es->tabs = NULL;
3878 else {
3879 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3880 memcpy(es->tabs, tabs, count * sizeof(INT));
3882 return TRUE;
3886 /*********************************************************************
3888 * EM_SETTABSTOPS16
3891 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs)
3893 if (!(es->style & ES_MULTILINE))
3894 return FALSE;
3895 HeapFree(GetProcessHeap(), 0, es->tabs);
3896 es->tabs_count = count;
3897 if (!count)
3898 es->tabs = NULL;
3899 else {
3900 INT i;
3901 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3902 for (i = 0 ; i < count ; i++)
3903 es->tabs[i] = *tabs++;
3905 return TRUE;
3909 /*********************************************************************
3911 * EM_SETWORDBREAKPROC
3914 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3916 if (es->word_break_proc == wbp)
3917 return;
3919 es->word_break_proc = wbp;
3920 es->word_break_proc16 = NULL;
3922 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3923 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3924 EDIT_UpdateText(es, NULL, TRUE);
3929 /*********************************************************************
3931 * EM_SETWORDBREAKPROC16
3934 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3936 if (es->word_break_proc16 == wbp)
3937 return;
3939 es->word_break_proc = NULL;
3940 es->word_break_proc16 = wbp;
3941 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3942 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3943 EDIT_UpdateText(es, NULL, TRUE);
3948 /*********************************************************************
3950 * EM_UNDO / WM_UNDO
3953 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3955 INT ulength;
3956 LPWSTR utext;
3958 /* As per MSDN spec, for a single-line edit control,
3959 the return value is always TRUE */
3960 if( es->style & ES_READONLY )
3961 return !(es->style & ES_MULTILINE);
3963 ulength = strlenW(es->undo_text);
3965 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3967 strcpyW(utext, es->undo_text);
3969 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3970 es->undo_insert_count, debugstr_w(utext));
3972 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3973 EDIT_EM_EmptyUndoBuffer(es);
3974 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3975 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3976 /* send the notification after the selection start and end are set */
3977 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3978 EDIT_EM_ScrollCaret(es);
3979 HeapFree(GetProcessHeap(), 0, utext);
3981 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3982 es->undo_insert_count, debugstr_w(es->undo_text));
3983 return TRUE;
3987 /* Helper function for WM_CHAR
3989 * According to an MSDN blog article titled "Just because you're a control
3990 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3991 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3992 * a dialog box or not.
3994 static BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3996 WND *pParent;
3997 BOOL r = FALSE;
3999 if (es->hwndParent)
4001 pParent = WIN_GetPtr(es->hwndParent);
4002 if (pParent && pParent != WND_OTHER_PROCESS && pParent != WND_DESKTOP)
4004 if (pParent->flags & WIN_ISDIALOG)
4005 r = TRUE;
4006 WIN_ReleasePtr(pParent);
4009 return r;
4013 /*********************************************************************
4015 * WM_CHAR
4018 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
4020 BOOL control;
4022 control = GetKeyState(VK_CONTROL) & 0x8000;
4024 switch (c) {
4025 case '\r':
4026 /* If it's not a multiline edit box, it would be ignored below.
4027 * For multiline edit without ES_WANTRETURN, we have to make a
4028 * special case.
4030 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
4031 if (EDIT_IsInsideDialog(es))
4032 break;
4033 case '\n':
4034 if (es->style & ES_MULTILINE) {
4035 if (es->style & ES_READONLY) {
4036 EDIT_MoveHome(es, FALSE);
4037 EDIT_MoveDown_ML(es, FALSE);
4038 } else {
4039 static const WCHAR cr_lfW[] = {'\r','\n',0};
4040 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
4043 break;
4044 case '\t':
4045 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4047 static const WCHAR tabW[] = {'\t',0};
4048 if (EDIT_IsInsideDialog(es))
4049 break;
4050 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4052 break;
4053 case VK_BACK:
4054 if (!(es->style & ES_READONLY) && !control) {
4055 if (es->selection_start != es->selection_end)
4056 EDIT_WM_Clear(es);
4057 else {
4058 /* delete character left of caret */
4059 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4060 EDIT_MoveBackward(es, TRUE);
4061 EDIT_WM_Clear(es);
4064 break;
4065 case 0x03: /* ^C */
4066 if (!(es->style & ES_PASSWORD))
4067 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4068 break;
4069 case 0x16: /* ^V */
4070 if (!(es->style & ES_READONLY))
4071 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4072 break;
4073 case 0x18: /* ^X */
4074 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
4075 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4076 break;
4077 case 0x1A: /* ^Z */
4078 if (!(es->style & ES_READONLY))
4079 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
4080 break;
4082 default:
4083 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4084 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4085 break;
4087 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4088 WCHAR str[2];
4089 str[0] = c;
4090 str[1] = '\0';
4091 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4093 break;
4095 return 1;
4099 /*********************************************************************
4101 * WM_COMMAND
4104 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4106 if (code || control)
4107 return;
4109 switch (id) {
4110 case EM_UNDO:
4111 EDIT_EM_Undo(es);
4112 break;
4113 case WM_CUT:
4114 EDIT_WM_Cut(es);
4115 break;
4116 case WM_COPY:
4117 EDIT_WM_Copy(es);
4118 break;
4119 case WM_PASTE:
4120 EDIT_WM_Paste(es);
4121 break;
4122 case WM_CLEAR:
4123 EDIT_WM_Clear(es);
4124 break;
4125 case EM_SETSEL:
4126 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4127 EDIT_EM_ScrollCaret(es);
4128 break;
4129 default:
4130 ERR("unknown menu item, please report\n");
4131 break;
4136 /*********************************************************************
4138 * WM_CONTEXTMENU
4140 * Note: the resource files resource/sysres_??.rc cannot define a
4141 * single popup menu. Hence we use a (dummy) menubar
4142 * containing the single popup menu as its first item.
4144 * FIXME: the message identifiers have been chosen arbitrarily,
4145 * hence we use MF_BYPOSITION.
4146 * We might as well use the "real" values (anybody knows ?)
4147 * The menu definition is in resources/sysres_??.rc.
4148 * Once these are OK, we better use MF_BYCOMMAND here
4149 * (as we do in EDIT_WM_Command()).
4152 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4154 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4155 HMENU popup = GetSubMenu(menu, 0);
4156 UINT start = es->selection_start;
4157 UINT end = es->selection_end;
4159 ORDER_UINT(start, end);
4161 /* undo */
4162 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4163 /* cut */
4164 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4165 /* copy */
4166 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4167 /* paste */
4168 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4169 /* delete */
4170 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4171 /* select all */
4172 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4174 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4176 RECT rc;
4177 /* Windows places the menu at the edit's center in this case */
4178 GetClientRect(es->hwndSelf, &rc);
4179 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4180 x = rc.left + (rc.right - rc.left) / 2;
4181 y = rc.top + (rc.bottom - rc.top) / 2;
4184 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4185 DestroyMenu(menu);
4189 /*********************************************************************
4191 * WM_COPY
4194 static void EDIT_WM_Copy(EDITSTATE *es)
4196 INT s = min(es->selection_start, es->selection_end);
4197 INT e = max(es->selection_start, es->selection_end);
4198 HGLOBAL hdst;
4199 LPWSTR dst;
4200 DWORD len;
4202 if (e == s) return;
4204 len = e - s;
4205 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4206 dst = GlobalLock(hdst);
4207 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4208 dst[len] = 0; /* ensure 0 termination */
4209 TRACE("%s\n", debugstr_w(dst));
4210 GlobalUnlock(hdst);
4211 OpenClipboard(es->hwndSelf);
4212 EmptyClipboard();
4213 SetClipboardData(CF_UNICODETEXT, hdst);
4214 CloseClipboard();
4218 /*********************************************************************
4220 * WM_CREATE
4223 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4225 RECT clientRect;
4227 TRACE("%s\n", debugstr_w(name));
4229 * To initialize some final structure members, we call some helper
4230 * functions. However, since the EDITSTATE is not consistent (i.e.
4231 * not fully initialized), we should be very careful which
4232 * functions can be called, and in what order.
4234 EDIT_WM_SetFont(es, 0, FALSE);
4235 EDIT_EM_EmptyUndoBuffer(es);
4237 /* We need to calculate the format rect
4238 (applications may send EM_SETMARGINS before the control gets visible) */
4239 GetClientRect(es->hwndSelf, &clientRect);
4240 EDIT_SetRectNP(es, &clientRect);
4242 if (name && *name) {
4243 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4244 /* if we insert text to the editline, the text scrolls out
4245 * of the window, as the caret is placed after the insert
4246 * pos normally; thus we reset es->selection... to 0 and
4247 * update caret
4249 es->selection_start = es->selection_end = 0;
4250 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4251 * Messages are only to be sent when the USER does something to
4252 * change the contents. So I am removing this EN_CHANGE
4254 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4256 EDIT_EM_ScrollCaret(es);
4258 /* force scroll info update */
4259 EDIT_UpdateScrollInfo(es);
4260 /* The rule seems to return 1 here for success */
4261 /* Power Builder masked edit controls will crash */
4262 /* if not. */
4263 /* FIXME: is that in all cases so ? */
4264 return 1;
4268 /*********************************************************************
4270 * WM_DESTROY
4273 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4275 LINEDEF *pc, *pp;
4277 if (es->hloc32W) {
4278 while (LocalUnlock(es->hloc32W)) ;
4279 LocalFree(es->hloc32W);
4281 if (es->hloc32A) {
4282 while (LocalUnlock(es->hloc32A)) ;
4283 LocalFree(es->hloc32A);
4285 if (es->hloc16) {
4286 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4287 HANDLE16 oldDS = stack16->ds;
4289 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4290 while (LocalUnlock16(es->hloc16)) ;
4291 LocalFree16(es->hloc16);
4292 stack16->ds = oldDS;
4295 pc = es->first_line_def;
4296 while (pc)
4298 pp = pc->next;
4299 HeapFree(GetProcessHeap(), 0, pc);
4300 pc = pp;
4303 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4304 HeapFree(GetProcessHeap(), 0, es);
4306 return 0;
4310 /*********************************************************************
4312 * WM_GETTEXT
4315 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4317 if(!count) return 0;
4319 if(unicode)
4321 lstrcpynW(dst, es->text, count);
4322 return strlenW(dst);
4324 else
4326 LPSTR textA = (LPSTR)dst;
4327 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4328 textA[count - 1] = 0; /* ensure 0 termination */
4329 return strlen(textA);
4333 /*********************************************************************
4335 * WM_HSCROLL
4338 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4340 INT dx;
4341 INT fw;
4343 if (!(es->style & ES_MULTILINE))
4344 return 0;
4346 if (!(es->style & ES_AUTOHSCROLL))
4347 return 0;
4349 dx = 0;
4350 fw = es->format_rect.right - es->format_rect.left;
4351 switch (action) {
4352 case SB_LINELEFT:
4353 TRACE("SB_LINELEFT\n");
4354 if (es->x_offset)
4355 dx = -es->char_width;
4356 break;
4357 case SB_LINERIGHT:
4358 TRACE("SB_LINERIGHT\n");
4359 if (es->x_offset < es->text_width)
4360 dx = es->char_width;
4361 break;
4362 case SB_PAGELEFT:
4363 TRACE("SB_PAGELEFT\n");
4364 if (es->x_offset)
4365 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4366 break;
4367 case SB_PAGERIGHT:
4368 TRACE("SB_PAGERIGHT\n");
4369 if (es->x_offset < es->text_width)
4370 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4371 break;
4372 case SB_LEFT:
4373 TRACE("SB_LEFT\n");
4374 if (es->x_offset)
4375 dx = -es->x_offset;
4376 break;
4377 case SB_RIGHT:
4378 TRACE("SB_RIGHT\n");
4379 if (es->x_offset < es->text_width)
4380 dx = es->text_width - es->x_offset;
4381 break;
4382 case SB_THUMBTRACK:
4383 TRACE("SB_THUMBTRACK %d\n", pos);
4384 es->flags |= EF_HSCROLL_TRACK;
4385 if(es->style & WS_HSCROLL)
4386 dx = pos - es->x_offset;
4387 else
4389 INT fw, new_x;
4390 /* Sanity check */
4391 if(pos < 0 || pos > 100) return 0;
4392 /* Assume default scroll range 0-100 */
4393 fw = es->format_rect.right - es->format_rect.left;
4394 new_x = pos * (es->text_width - fw) / 100;
4395 dx = es->text_width ? (new_x - es->x_offset) : 0;
4397 break;
4398 case SB_THUMBPOSITION:
4399 TRACE("SB_THUMBPOSITION %d\n", pos);
4400 es->flags &= ~EF_HSCROLL_TRACK;
4401 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4402 dx = pos - es->x_offset;
4403 else
4405 INT fw, new_x;
4406 /* Sanity check */
4407 if(pos < 0 || pos > 100) return 0;
4408 /* Assume default scroll range 0-100 */
4409 fw = es->format_rect.right - es->format_rect.left;
4410 new_x = pos * (es->text_width - fw) / 100;
4411 dx = es->text_width ? (new_x - es->x_offset) : 0;
4413 if (!dx) {
4414 /* force scroll info update */
4415 EDIT_UpdateScrollInfo(es);
4416 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4418 break;
4419 case SB_ENDSCROLL:
4420 TRACE("SB_ENDSCROLL\n");
4421 break;
4423 * FIXME : the next two are undocumented !
4424 * Are we doing the right thing ?
4425 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4426 * although it's also a regular control message.
4428 case EM_GETTHUMB: /* this one is used by NT notepad */
4429 case EM_GETTHUMB16:
4431 LRESULT ret;
4432 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4433 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4434 else
4436 /* Assume default scroll range 0-100 */
4437 INT fw = es->format_rect.right - es->format_rect.left;
4438 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4440 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4441 return ret;
4443 case EM_LINESCROLL16:
4444 TRACE("EM_LINESCROLL16\n");
4445 dx = pos;
4446 break;
4448 default:
4449 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4450 action, action);
4451 return 0;
4453 if (dx)
4455 INT fw = es->format_rect.right - es->format_rect.left;
4456 /* check if we are going to move too far */
4457 if(es->x_offset + dx + fw > es->text_width)
4458 dx = es->text_width - fw - es->x_offset;
4459 if(dx)
4460 EDIT_EM_LineScroll_internal(es, dx, 0);
4462 return 0;
4466 /*********************************************************************
4468 * EDIT_CheckCombo
4471 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4473 HWND hLBox = es->hwndListBox;
4474 HWND hCombo;
4475 BOOL bDropped;
4476 int nEUI;
4478 if (!hLBox)
4479 return FALSE;
4481 hCombo = GetParent(es->hwndSelf);
4482 bDropped = TRUE;
4483 nEUI = 0;
4485 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4487 if (key == VK_UP || key == VK_DOWN)
4489 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4490 nEUI = 1;
4492 if (msg == WM_KEYDOWN || nEUI)
4493 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4496 switch (msg)
4498 case WM_KEYDOWN:
4499 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4501 /* make sure ComboLBox pops up */
4502 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4503 key = VK_F4;
4504 nEUI = 2;
4507 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4508 break;
4510 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4511 if (nEUI)
4512 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4513 else
4514 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4515 break;
4518 if(nEUI == 2)
4519 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4521 return TRUE;
4525 /*********************************************************************
4527 * WM_KEYDOWN
4529 * Handling of special keys that don't produce a WM_CHAR
4530 * (i.e. non-printable keys) & Backspace & Delete
4533 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4535 BOOL shift;
4536 BOOL control;
4538 if (GetKeyState(VK_MENU) & 0x8000)
4539 return 0;
4541 shift = GetKeyState(VK_SHIFT) & 0x8000;
4542 control = GetKeyState(VK_CONTROL) & 0x8000;
4544 switch (key) {
4545 case VK_F4:
4546 case VK_UP:
4547 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4548 break;
4550 /* fall through */
4551 case VK_LEFT:
4552 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4553 EDIT_MoveUp_ML(es, shift);
4554 else
4555 if (control)
4556 EDIT_MoveWordBackward(es, shift);
4557 else
4558 EDIT_MoveBackward(es, shift);
4559 break;
4560 case VK_DOWN:
4561 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4562 break;
4563 /* fall through */
4564 case VK_RIGHT:
4565 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4566 EDIT_MoveDown_ML(es, shift);
4567 else if (control)
4568 EDIT_MoveWordForward(es, shift);
4569 else
4570 EDIT_MoveForward(es, shift);
4571 break;
4572 case VK_HOME:
4573 EDIT_MoveHome(es, shift);
4574 break;
4575 case VK_END:
4576 EDIT_MoveEnd(es, shift);
4577 break;
4578 case VK_PRIOR:
4579 if (es->style & ES_MULTILINE)
4580 EDIT_MovePageUp_ML(es, shift);
4581 else
4582 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4583 break;
4584 case VK_NEXT:
4585 if (es->style & ES_MULTILINE)
4586 EDIT_MovePageDown_ML(es, shift);
4587 else
4588 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4589 break;
4590 case VK_DELETE:
4591 if (!(es->style & ES_READONLY) && !(shift && control)) {
4592 if (es->selection_start != es->selection_end) {
4593 if (shift)
4594 EDIT_WM_Cut(es);
4595 else
4596 EDIT_WM_Clear(es);
4597 } else {
4598 if (shift) {
4599 /* delete character left of caret */
4600 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4601 EDIT_MoveBackward(es, TRUE);
4602 EDIT_WM_Clear(es);
4603 } else if (control) {
4604 /* delete to end of line */
4605 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4606 EDIT_MoveEnd(es, TRUE);
4607 EDIT_WM_Clear(es);
4608 } else {
4609 /* delete character right of caret */
4610 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4611 EDIT_MoveForward(es, TRUE);
4612 EDIT_WM_Clear(es);
4616 break;
4617 case VK_INSERT:
4618 if (shift) {
4619 if (!(es->style & ES_READONLY))
4620 EDIT_WM_Paste(es);
4621 } else if (control)
4622 EDIT_WM_Copy(es);
4623 break;
4624 case VK_RETURN:
4625 /* If the edit doesn't want the return send a message to the default object */
4626 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
4628 HWND hwndParent = GetParent(es->hwndSelf);
4629 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4630 if (HIWORD(dw) == DC_HASDEFID)
4632 SendMessageW( hwndParent, WM_COMMAND,
4633 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4634 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4636 else
4637 SendMessageW( hwndParent, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndParent, IDOK ) );
4639 break;
4640 case VK_ESCAPE:
4641 if (!(es->style & ES_MULTILINE))
4642 SendMessageW(GetParent(es->hwndSelf), WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( GetParent(es->hwndSelf), IDCANCEL ) );
4643 break;
4645 return 0;
4649 /*********************************************************************
4651 * WM_KILLFOCUS
4654 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4656 es->flags &= ~EF_FOCUSED;
4657 DestroyCaret();
4658 if(!(es->style & ES_NOHIDESEL))
4659 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4660 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4661 return 0;
4665 /*********************************************************************
4667 * WM_LBUTTONDBLCLK
4669 * The caret position has been set on the WM_LBUTTONDOWN message
4672 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4674 INT s;
4675 INT e = es->selection_end;
4676 INT l;
4677 INT li;
4678 INT ll;
4680 es->bCaptureState = TRUE;
4681 SetCapture(es->hwndSelf);
4683 l = EDIT_EM_LineFromChar(es, e);
4684 li = EDIT_EM_LineIndex(es, l);
4685 ll = EDIT_EM_LineLength(es, e);
4686 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4687 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4688 EDIT_EM_SetSel(es, s, e, FALSE);
4689 EDIT_EM_ScrollCaret(es);
4690 es->region_posx = es->region_posy = 0;
4691 SetTimer(es->hwndSelf, 0, 100, NULL);
4692 return 0;
4696 /*********************************************************************
4698 * WM_LBUTTONDOWN
4701 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4703 INT e;
4704 BOOL after_wrap;
4706 es->bCaptureState = TRUE;
4707 SetCapture(es->hwndSelf);
4708 EDIT_ConfinePoint(es, &x, &y);
4709 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4710 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4711 EDIT_EM_ScrollCaret(es);
4712 es->region_posx = es->region_posy = 0;
4713 SetTimer(es->hwndSelf, 0, 100, NULL);
4715 if (!(es->flags & EF_FOCUSED))
4716 SetFocus(es->hwndSelf);
4718 return 0;
4722 /*********************************************************************
4724 * WM_LBUTTONUP
4727 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4729 if (es->bCaptureState) {
4730 KillTimer(es->hwndSelf, 0);
4731 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4733 es->bCaptureState = FALSE;
4734 return 0;
4738 /*********************************************************************
4740 * WM_MBUTTONDOWN
4743 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4745 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4746 return 0;
4750 /*********************************************************************
4752 * WM_MOUSEMOVE
4755 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4757 INT e;
4758 BOOL after_wrap;
4759 INT prex, prey;
4761 /* If the mouse has been captured by process other than the edit control itself,
4762 * the windows edit controls will not select the strings with mouse move.
4764 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4765 return 0;
4768 * FIXME: gotta do some scrolling if outside client
4769 * area. Maybe reset the timer ?
4771 prex = x; prey = y;
4772 EDIT_ConfinePoint(es, &x, &y);
4773 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4774 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4775 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4776 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4777 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4778 return 0;
4782 /*********************************************************************
4784 * WM_NCCREATE
4786 * See also EDIT_WM_StyleChanged
4788 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4790 EDITSTATE *es;
4791 UINT alloc_size;
4793 TRACE("Creating %s edit control, style = %08x\n",
4794 unicode ? "Unicode" : "ANSI", lpcs->style);
4796 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4797 return FALSE;
4798 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4801 * Note: since the EDITSTATE has not been fully initialized yet,
4802 * we can't use any API calls that may send
4803 * WM_XXX messages before WM_NCCREATE is completed.
4806 es->is_unicode = unicode;
4807 es->style = lpcs->style;
4809 es->bEnableState = !(es->style & WS_DISABLED);
4811 es->hwndSelf = hwnd;
4812 /* Save parent, which will be notified by EN_* messages */
4813 es->hwndParent = lpcs->hwndParent;
4815 if (es->style & ES_COMBO)
4816 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4818 /* Number overrides lowercase overrides uppercase (at least it
4819 * does in Win95). However I'll bet that ES_NUMBER would be
4820 * invalid under Win 3.1.
4822 if (es->style & ES_NUMBER) {
4823 ; /* do not override the ES_NUMBER */
4824 } else if (es->style & ES_LOWERCASE) {
4825 es->style &= ~ES_UPPERCASE;
4827 if (es->style & ES_MULTILINE) {
4828 es->buffer_limit = BUFLIMIT_INITIAL;
4829 if (es->style & WS_VSCROLL)
4830 es->style |= ES_AUTOVSCROLL;
4831 if (es->style & WS_HSCROLL)
4832 es->style |= ES_AUTOHSCROLL;
4833 es->style &= ~ES_PASSWORD;
4834 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4835 /* Confirmed - RIGHT overrides CENTER */
4836 if (es->style & ES_RIGHT)
4837 es->style &= ~ES_CENTER;
4838 es->style &= ~WS_HSCROLL;
4839 es->style &= ~ES_AUTOHSCROLL;
4841 } else {
4842 es->buffer_limit = BUFLIMIT_INITIAL;
4843 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4844 es->style &= ~ES_CENTER;
4845 es->style &= ~WS_HSCROLL;
4846 es->style &= ~WS_VSCROLL;
4847 if (es->style & ES_PASSWORD)
4848 es->password_char = '*';
4851 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4852 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4853 return FALSE;
4854 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4856 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4857 return FALSE;
4858 es->undo_buffer_size = es->buffer_size;
4860 if (es->style & ES_MULTILINE)
4861 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4862 return FALSE;
4863 es->line_count = 1;
4866 * In Win95 look and feel, the WS_BORDER style is replaced by the
4867 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4868 * control a nonclient area so we don't need to draw the border.
4869 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4870 * a nonclient area and we should handle painting the border ourselves.
4872 * When making modifications please ensure that the code still works
4873 * for edit controls created directly with style 0x50800000, exStyle 0
4874 * (which should have a single pixel border)
4876 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4877 es->style &= ~WS_BORDER;
4878 else if (es->style & WS_BORDER)
4879 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4881 return TRUE;
4884 /*********************************************************************
4886 * WM_PAINT
4889 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4891 PAINTSTRUCT ps;
4892 INT i;
4893 HDC dc;
4894 HFONT old_font = 0;
4895 RECT rc;
4896 RECT rcClient;
4897 RECT rcLine;
4898 RECT rcRgn;
4899 HBRUSH brush;
4900 HBRUSH old_brush;
4901 INT bw, bh;
4902 BOOL rev = es->bEnableState &&
4903 ((es->flags & EF_FOCUSED) ||
4904 (es->style & ES_NOHIDESEL));
4905 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4907 GetClientRect(es->hwndSelf, &rcClient);
4909 /* get the background brush */
4910 brush = EDIT_NotifyCtlColor(es, dc);
4912 /* paint the border and the background */
4913 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4915 if(es->style & WS_BORDER) {
4916 bw = GetSystemMetrics(SM_CXBORDER);
4917 bh = GetSystemMetrics(SM_CYBORDER);
4918 rc = rcClient;
4919 if(es->style & ES_MULTILINE) {
4920 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4921 if(es->style & WS_VSCROLL) rc.right+=bw;
4924 /* Draw the frame. Same code as in nonclient.c */
4925 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4926 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4927 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4928 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4929 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4930 SelectObject(dc, old_brush);
4932 /* Keep the border clean */
4933 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4934 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4937 GetClipBox(dc, &rc);
4938 FillRect(dc, &rc, brush);
4940 IntersectClipRect(dc, es->format_rect.left,
4941 es->format_rect.top,
4942 es->format_rect.right,
4943 es->format_rect.bottom);
4944 if (es->style & ES_MULTILINE) {
4945 rc = rcClient;
4946 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4948 if (es->font)
4949 old_font = SelectObject(dc, es->font);
4951 if (!es->bEnableState)
4952 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4953 GetClipBox(dc, &rcRgn);
4954 if (es->style & ES_MULTILINE) {
4955 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4956 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4957 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4958 if (IntersectRect(&rc, &rcRgn, &rcLine))
4959 EDIT_PaintLine(es, dc, i, rev);
4961 } else {
4962 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4963 if (IntersectRect(&rc, &rcRgn, &rcLine))
4964 EDIT_PaintLine(es, dc, 0, rev);
4966 if (es->font)
4967 SelectObject(dc, old_font);
4969 if (!hdc)
4970 EndPaint(es->hwndSelf, &ps);
4974 /*********************************************************************
4976 * WM_PASTE
4979 static void EDIT_WM_Paste(EDITSTATE *es)
4981 HGLOBAL hsrc;
4982 LPWSTR src;
4984 /* Protect read-only edit control from modification */
4985 if(es->style & ES_READONLY)
4986 return;
4988 OpenClipboard(es->hwndSelf);
4989 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4990 src = (LPWSTR)GlobalLock(hsrc);
4991 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4992 GlobalUnlock(hsrc);
4994 else if (es->style & ES_PASSWORD) {
4995 /* clear selected text in password edit box even with empty clipboard */
4996 const WCHAR empty_strW[] = { 0 };
4997 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
4999 CloseClipboard();
5003 /*********************************************************************
5005 * WM_SETFOCUS
5008 static void EDIT_WM_SetFocus(EDITSTATE *es)
5010 es->flags |= EF_FOCUSED;
5012 if (!(es->style & ES_NOHIDESEL))
5013 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
5015 /* single line edit updates itself */
5016 if (!(es->style & ES_MULTILINE))
5018 HDC hdc = GetDC(es->hwndSelf);
5019 EDIT_WM_Paint(es, hdc);
5020 ReleaseDC(es->hwndSelf, hdc);
5023 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5024 EDIT_SetCaretPos(es, es->selection_end,
5025 es->flags & EF_AFTER_WRAP);
5026 ShowCaret(es->hwndSelf);
5027 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
5031 /*********************************************************************
5033 * WM_SETFONT
5035 * With Win95 look the margins are set to default font value unless
5036 * the system font (font == 0) is being set, in which case they are left
5037 * unchanged.
5040 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
5042 TEXTMETRICW tm;
5043 HDC dc;
5044 HFONT old_font = 0;
5045 RECT clientRect;
5047 es->font = font;
5048 dc = GetDC(es->hwndSelf);
5049 if (font)
5050 old_font = SelectObject(dc, font);
5051 GetTextMetricsW(dc, &tm);
5052 es->line_height = tm.tmHeight;
5053 es->char_width = tm.tmAveCharWidth;
5054 if (font)
5055 SelectObject(dc, old_font);
5056 ReleaseDC(es->hwndSelf, dc);
5058 /* Reset the format rect and the margins */
5059 GetClientRect(es->hwndSelf, &clientRect);
5060 EDIT_SetRectNP(es, &clientRect);
5061 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5062 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5064 if (es->style & ES_MULTILINE)
5065 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5066 else
5067 EDIT_CalcLineWidth_SL(es);
5069 if (redraw)
5070 EDIT_UpdateText(es, NULL, TRUE);
5071 if (es->flags & EF_FOCUSED) {
5072 DestroyCaret();
5073 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5074 EDIT_SetCaretPos(es, es->selection_end,
5075 es->flags & EF_AFTER_WRAP);
5076 ShowCaret(es->hwndSelf);
5081 /*********************************************************************
5083 * WM_SETTEXT
5085 * NOTES
5086 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5087 * The modified flag is reset. No notifications are sent.
5089 * For single-line controls, reception of WM_SETTEXT triggers:
5090 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5093 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5095 LPWSTR textW = NULL;
5096 if (!unicode && text)
5098 LPCSTR textA = (LPCSTR)text;
5099 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5100 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5101 if (textW)
5102 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5103 text = textW;
5106 if (es->flags & EF_UPDATE)
5107 /* fixed this bug once; complain if we see it about to happen again. */
5108 ERR("SetSel may generate UPDATE message whose handler may reset "
5109 "selection.\n");
5111 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5112 if (text)
5114 TRACE("%s\n", debugstr_w(text));
5115 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5116 if(!unicode)
5117 HeapFree(GetProcessHeap(), 0, textW);
5119 else
5121 static const WCHAR empty_stringW[] = {0};
5122 TRACE("<NULL>\n");
5123 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5125 es->x_offset = 0;
5126 es->flags &= ~EF_MODIFIED;
5127 EDIT_EM_SetSel(es, 0, 0, FALSE);
5129 /* Send the notification after the selection start and end have been set
5130 * edit control doesn't send notification on WM_SETTEXT
5131 * if it is multiline, or it is part of combobox
5133 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5135 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5136 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5138 EDIT_EM_ScrollCaret(es);
5139 EDIT_UpdateScrollInfo(es);
5143 /*********************************************************************
5145 * WM_SIZE
5148 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5150 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5151 RECT rc;
5152 TRACE("width = %d, height = %d\n", width, height);
5153 SetRect(&rc, 0, 0, width, height);
5154 EDIT_SetRectNP(es, &rc);
5155 EDIT_UpdateText(es, NULL, TRUE);
5160 /*********************************************************************
5162 * WM_STYLECHANGED
5164 * This message is sent by SetWindowLong on having changed either the Style
5165 * or the extended style.
5167 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5169 * See also EDIT_WM_NCCreate
5171 * It appears that the Windows version of the edit control allows the style
5172 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5173 * style variable which will generally be different. In this function we
5174 * update the internal style based on what changed in the externally visible
5175 * style.
5177 * Much of this content as based upon the MSDN, especially:
5178 * Platform SDK Documentation -> User Interface Services ->
5179 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5180 * Edit Control Styles
5182 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5184 if (GWL_STYLE == which) {
5185 DWORD style_change_mask;
5186 DWORD new_style;
5187 /* Only a subset of changes can be applied after the control
5188 * has been created.
5190 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5191 ES_NUMBER;
5192 if (es->style & ES_MULTILINE)
5193 style_change_mask |= ES_WANTRETURN;
5195 new_style = style->styleNew & style_change_mask;
5197 /* Number overrides lowercase overrides uppercase (at least it
5198 * does in Win95). However I'll bet that ES_NUMBER would be
5199 * invalid under Win 3.1.
5201 if (new_style & ES_NUMBER) {
5202 ; /* do not override the ES_NUMBER */
5203 } else if (new_style & ES_LOWERCASE) {
5204 new_style &= ~ES_UPPERCASE;
5207 es->style = (es->style & ~style_change_mask) | new_style;
5208 } else if (GWL_EXSTYLE == which) {
5209 ; /* FIXME - what is needed here */
5210 } else {
5211 WARN ("Invalid style change %ld\n",which);
5214 return 0;
5217 /*********************************************************************
5219 * WM_SYSKEYDOWN
5222 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5224 if ((key == VK_BACK) && (key_data & 0x2000)) {
5225 if (EDIT_EM_CanUndo(es))
5226 EDIT_EM_Undo(es);
5227 return 0;
5228 } else if (key == VK_UP || key == VK_DOWN) {
5229 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5230 return 0;
5232 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5236 /*********************************************************************
5238 * WM_TIMER
5241 static void EDIT_WM_Timer(EDITSTATE *es)
5243 if (es->region_posx < 0) {
5244 EDIT_MoveBackward(es, TRUE);
5245 } else if (es->region_posx > 0) {
5246 EDIT_MoveForward(es, TRUE);
5249 * FIXME: gotta do some vertical scrolling here, like
5250 * EDIT_EM_LineScroll(hwnd, 0, 1);
5254 /*********************************************************************
5256 * WM_VSCROLL
5259 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5261 INT dy;
5263 if (!(es->style & ES_MULTILINE))
5264 return 0;
5266 if (!(es->style & ES_AUTOVSCROLL))
5267 return 0;
5269 dy = 0;
5270 switch (action) {
5271 case SB_LINEUP:
5272 case SB_LINEDOWN:
5273 case SB_PAGEUP:
5274 case SB_PAGEDOWN:
5275 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5276 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5277 (action == SB_PAGEUP ? "SB_PAGEUP" :
5278 "SB_PAGEDOWN"))));
5279 EDIT_EM_Scroll(es, action);
5280 return 0;
5281 case SB_TOP:
5282 TRACE("SB_TOP\n");
5283 dy = -es->y_offset;
5284 break;
5285 case SB_BOTTOM:
5286 TRACE("SB_BOTTOM\n");
5287 dy = es->line_count - 1 - es->y_offset;
5288 break;
5289 case SB_THUMBTRACK:
5290 TRACE("SB_THUMBTRACK %d\n", pos);
5291 es->flags |= EF_VSCROLL_TRACK;
5292 if(es->style & WS_VSCROLL)
5293 dy = pos - es->y_offset;
5294 else
5296 /* Assume default scroll range 0-100 */
5297 INT vlc, new_y;
5298 /* Sanity check */
5299 if(pos < 0 || pos > 100) return 0;
5300 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5301 new_y = pos * (es->line_count - vlc) / 100;
5302 dy = es->line_count ? (new_y - es->y_offset) : 0;
5303 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5304 es->line_count, es->y_offset, pos, dy);
5306 break;
5307 case SB_THUMBPOSITION:
5308 TRACE("SB_THUMBPOSITION %d\n", pos);
5309 es->flags &= ~EF_VSCROLL_TRACK;
5310 if(es->style & WS_VSCROLL)
5311 dy = pos - es->y_offset;
5312 else
5314 /* Assume default scroll range 0-100 */
5315 INT vlc, new_y;
5316 /* Sanity check */
5317 if(pos < 0 || pos > 100) return 0;
5318 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5319 new_y = pos * (es->line_count - vlc) / 100;
5320 dy = es->line_count ? (new_y - es->y_offset) : 0;
5321 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5322 es->line_count, es->y_offset, pos, dy);
5324 if (!dy)
5326 /* force scroll info update */
5327 EDIT_UpdateScrollInfo(es);
5328 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5330 break;
5331 case SB_ENDSCROLL:
5332 TRACE("SB_ENDSCROLL\n");
5333 break;
5335 * FIXME : the next two are undocumented !
5336 * Are we doing the right thing ?
5337 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5338 * although it's also a regular control message.
5340 case EM_GETTHUMB: /* this one is used by NT notepad */
5341 case EM_GETTHUMB16:
5343 LRESULT ret;
5344 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5345 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5346 else
5348 /* Assume default scroll range 0-100 */
5349 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5350 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5352 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5353 return ret;
5355 case EM_LINESCROLL16:
5356 TRACE("EM_LINESCROLL16 %d\n", pos);
5357 dy = pos;
5358 break;
5360 default:
5361 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5362 action, action);
5363 return 0;
5365 if (dy)
5366 EDIT_EM_LineScroll(es, 0, dy);
5367 return 0;
5370 /*********************************************************************
5372 * EDIT_UpdateText
5375 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5377 if (es->flags & EF_UPDATE) {
5378 es->flags &= ~EF_UPDATE;
5379 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5381 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5385 /*********************************************************************
5387 * EDIT_UpdateText
5390 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
5392 if (es->flags & EF_UPDATE) {
5393 es->flags &= ~EF_UPDATE;
5394 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5396 InvalidateRect(es->hwndSelf, rc, bErase);
5399 /********************************************************************
5401 * The Following code is to handle inline editing from IMEs
5404 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5406 LONG buflen;
5407 LPWSTR lpCompStr = NULL;
5408 HIMC hIMC;
5409 LPSTR lpCompStrAttr = NULL;
5410 DWORD dwBufLenAttr;
5412 if (!(hIMC = ImmGetContext(hwnd)))
5413 return;
5415 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5417 if (buflen < 0)
5419 ImmReleaseContext(hwnd, hIMC);
5420 return;
5423 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
5424 if (!lpCompStr)
5426 ERR("Unable to allocate IME CompositionString\n");
5427 ImmReleaseContext(hwnd,hIMC);
5428 return;
5431 if (buflen)
5432 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
5433 lpCompStr[buflen/sizeof(WCHAR)] = 0;
5435 if (CompFlag & GCS_COMPATTR)
5438 * We do not use the attributes yet. it would tell us what characters
5439 * are in transition and which are converted or decided upon
5441 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5442 if (dwBufLenAttr)
5444 dwBufLenAttr ++;
5445 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5446 if (!lpCompStrAttr)
5448 ERR("Unable to allocate IME Attribute String\n");
5449 HeapFree(GetProcessHeap(),0,lpCompStr);
5450 ImmReleaseContext(hwnd,hIMC);
5451 return;
5453 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5454 dwBufLenAttr);
5455 lpCompStrAttr[dwBufLenAttr] = 0;
5457 else
5458 lpCompStrAttr = NULL;
5461 /* check for change in composition start */
5462 if (es->selection_end < es->composition_start)
5463 es->composition_start = es->selection_end;
5465 /* replace existing selection string */
5466 es->selection_start = es->composition_start;
5468 if (es->composition_len > 0)
5469 es->selection_end = es->composition_start + es->composition_len;
5470 else
5471 es->selection_end = es->selection_start;
5473 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5474 es->composition_len = abs(es->composition_start - es->selection_end);
5476 es->selection_start = es->composition_start;
5477 es->selection_end = es->selection_start + es->composition_len;
5479 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5480 HeapFree(GetProcessHeap(),0,lpCompStr);
5481 ImmReleaseContext(hwnd,hIMC);
5484 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5486 LONG buflen;
5487 LPWSTR lpResultStr;
5488 HIMC hIMC;
5490 if ( !(hIMC = ImmGetContext(hwnd)))
5491 return;
5493 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5494 if (buflen <= 0)
5496 ImmReleaseContext(hwnd, hIMC);
5497 return;
5500 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
5501 if (!lpResultStr)
5503 ERR("Unable to alloc buffer for IME string\n");
5504 ImmReleaseContext(hwnd, hIMC);
5505 return;
5508 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
5509 lpResultStr[buflen/sizeof(WCHAR)] = 0;
5511 /* check for change in composition start */
5512 if (es->selection_end < es->composition_start)
5513 es->composition_start = es->selection_end;
5515 es->selection_start = es->composition_start;
5516 es->selection_end = es->composition_start + es->composition_len;
5517 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5518 es->composition_start = es->selection_end;
5519 es->composition_len = 0;
5521 HeapFree(GetProcessHeap(),0,lpResultStr);
5522 ImmReleaseContext(hwnd, hIMC);
5525 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5527 if (CompFlag & GCS_RESULTSTR)
5528 EDIT_GetResultStr(hwnd,es);
5529 if (CompFlag & GCS_COMPSTR)
5530 EDIT_GetCompositionStr(hwnd, CompFlag, es);