Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / user32 / edit.c
blob92f7e28acd4f2404d2d91c3586aee6d1b4ae1b53
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 "winreg.h"
63 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(edit);
66 WINE_DECLARE_DEBUG_CHANNEL(combo);
67 WINE_DECLARE_DEBUG_CHANNEL(relay);
69 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
70 FIXME: BTW, new specs say 65535 (do you dare ???) */
71 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
72 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
73 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
74 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
77 * extra flags for EDITSTATE.flags field
79 #define EF_MODIFIED 0x0001 /* text has been modified */
80 #define EF_FOCUSED 0x0002 /* we have input focus */
81 #define EF_UPDATE 0x0004 /* notify parent of changed state */
82 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
83 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
84 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
85 wrapped line, instead of in front of the next character */
86 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
88 typedef enum
90 END_0 = 0, /* line ends with terminating '\0' character */
91 END_WRAP, /* line is wrapped */
92 END_HARD, /* line ends with a hard return '\r\n' */
93 END_SOFT, /* line ends with a soft return '\r\r\n' */
94 END_RICH /* line ends with a single '\n' */
95 } LINE_END;
97 typedef struct tagLINEDEF {
98 INT length; /* bruto length of a line in bytes */
99 INT net_length; /* netto length of a line in visible characters */
100 LINE_END ending;
101 INT width; /* width of the line in pixels */
102 INT index; /* line index into the buffer */
103 struct tagLINEDEF *next;
104 } LINEDEF;
106 typedef struct
108 BOOL is_unicode; /* how the control was created */
109 LPWSTR text; /* the actual contents of the control */
110 UINT buffer_size; /* the size of the buffer in characters */
111 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
112 HFONT font; /* NULL means standard system font */
113 INT x_offset; /* scroll offset for multi lines this is in pixels
114 for single lines it's in characters */
115 INT line_height; /* height of a screen line in pixels */
116 INT char_width; /* average character width in pixels */
117 DWORD style; /* sane version of wnd->dwStyle */
118 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
119 INT undo_insert_count; /* number of characters inserted in sequence */
120 UINT undo_position; /* character index of the insertion and deletion */
121 LPWSTR undo_text; /* deleted text */
122 UINT undo_buffer_size; /* size of the deleted text buffer */
123 INT selection_start; /* == selection_end if no selection */
124 INT selection_end; /* == current caret position */
125 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
126 INT left_margin; /* in pixels */
127 INT right_margin; /* in pixels */
128 RECT format_rect;
129 INT text_width; /* width of the widest line in pixels for multi line controls
130 and just line width for single line controls */
131 INT region_posx; /* Position of cursor relative to region: */
132 INT region_posy; /* -1: to left, 0: within, 1: to right */
133 EDITWORDBREAKPROC16 word_break_proc16;
134 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
135 INT line_count; /* number of lines */
136 INT y_offset; /* scroll offset in number of lines */
137 BOOL bCaptureState; /* flag indicating whether mouse was captured */
138 BOOL bEnableState; /* flag keeping the enable state */
139 HWND hwndSelf; /* the our window handle */
140 HWND hwndParent; /* Handle of parent for sending EN_* messages.
141 Even if parent will change, EN_* messages
142 should be sent to the first parent. */
143 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
145 * only for multi line controls
147 INT lock_count; /* amount of re-entries in the EditWndProc */
148 INT tabs_count;
149 LPINT tabs;
150 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
151 HLOCAL hloc32W; /* our unicode local memory block */
152 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
153 or EM_SETHANDLE16 */
154 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
155 or EM_SETHANDLE */
157 * IME Data
159 UINT composition_len; /* length of composition, 0 == no composition */
160 int composition_start; /* the character position for the composition */
161 } EDITSTATE;
164 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
165 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
167 /* used for disabled or read-only edit control */
168 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
169 do \
170 { /* Notify parent which has created this edit control */ \
171 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
172 SendMessageW(es->hwndParent, WM_COMMAND, \
173 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
174 (LPARAM)(es->hwndSelf)); \
175 } while(0)
177 /*********************************************************************
179 * Declarations
184 * These functions have trivial implementations
185 * We still like to call them internally
186 * "static inline" makes them more like macro's
188 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
189 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
190 static inline void EDIT_WM_Clear(EDITSTATE *es);
191 static inline void EDIT_WM_Cut(EDITSTATE *es);
194 * Helper functions only valid for one type of control
196 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
197 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
198 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
199 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
200 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
201 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
202 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
204 * Helper functions valid for both single line _and_ multi line controls
206 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
207 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
208 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
209 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
210 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
211 static void EDIT_LockBuffer(EDITSTATE *es);
212 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
213 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
214 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
215 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
216 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
217 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
218 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
219 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
220 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
221 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
222 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
223 static void EDIT_AdjustFormatRect(EDITSTATE *es);
224 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
225 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
226 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
227 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
229 * EM_XXX message handlers
231 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
232 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
233 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
234 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
235 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
236 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
237 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
238 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
239 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
240 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
241 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
242 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
243 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
244 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
245 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
246 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
247 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
248 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
249 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
250 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, WORD left, WORD right, BOOL repaint);
251 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
252 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
253 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
254 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
255 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
256 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
257 static BOOL EDIT_EM_Undo(EDITSTATE *es);
259 * WM_XXX message handlers
261 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
262 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
263 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
264 static void EDIT_WM_Copy(EDITSTATE *es);
265 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
266 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
267 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
268 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
269 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
270 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
271 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
272 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
273 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
274 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
275 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
276 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
277 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
278 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
279 static void EDIT_WM_Paste(EDITSTATE *es);
280 static void EDIT_WM_SetFocus(EDITSTATE *es);
281 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
282 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
283 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
284 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
285 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
286 static void EDIT_WM_Timer(EDITSTATE *es);
287 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
288 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
289 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
290 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
292 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
293 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
295 /***********************************************************************
296 * USER32_DisplayUnixPaths
298 * Check if unix paths should be displayed.
300 static int bDisplayUnixPath = -1;
302 int USER32_DisplayUnixPaths(EDITSTATE *es)
304 if (bDisplayUnixPath < 0)
306 HKEY hkey;
308 bDisplayUnixPath = 0;
310 /* @@ Wine registry key: HKCU\Software\Wine */
311 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine", &hkey))
313 char buffer[20];
314 DWORD type, count = sizeof(buffer);
316 if(!RegQueryValueExA(hkey, "DisplayUnixPaths", 0, &type, (LPBYTE)buffer, &count))
317 bDisplayUnixPath = atoi(buffer);
319 RegCloseKey(hkey);
323 return bDisplayUnixPath && !(es->style & ES_MULTILINE) && strchrW(es->text, '/');
327 /*********************************************************************
328 * edit class descriptor
330 const struct builtin_class_descr EDIT_builtin_class =
332 "Edit", /* name */
333 CS_DBLCLKS | CS_PARENTDC, /* style */
334 EditWndProcA, /* procA */
335 EditWndProcW, /* procW */
336 sizeof(EDITSTATE *), /* extra */
337 IDC_IBEAM, /* cursor */
338 0 /* brush */
342 /*********************************************************************
344 * EM_CANUNDO
347 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
349 return (es->undo_insert_count || strlenW(es->undo_text));
353 /*********************************************************************
355 * EM_EMPTYUNDOBUFFER
358 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
360 es->undo_insert_count = 0;
361 *es->undo_text = '\0';
365 /*********************************************************************
367 * WM_CLEAR
370 static inline void EDIT_WM_Clear(EDITSTATE *es)
372 static const WCHAR empty_stringW[] = {0};
374 /* Protect read-only edit control from modification */
375 if(es->style & ES_READONLY)
376 return;
378 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
382 /*********************************************************************
384 * WM_CUT
387 static inline void EDIT_WM_Cut(EDITSTATE *es)
389 EDIT_WM_Copy(es);
390 EDIT_WM_Clear(es);
394 /**********************************************************************
395 * get_app_version
397 * Returns the window version in case Wine emulates a later version
398 * of windows than the application expects.
400 * In a number of cases when windows runs an application that was
401 * designed for an earlier windows version, windows reverts
402 * to "old" behaviour of that earlier version.
404 * An example is a disabled edit control that needs to be painted.
405 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
406 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
407 * applications with an expected version 0f 4.0 or higher.
410 static DWORD get_app_version(void)
412 static DWORD version;
413 if (!version)
415 DWORD dwEmulatedVersion;
416 OSVERSIONINFOW info;
417 DWORD dwProcVersion = GetProcessVersion(0);
419 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
420 GetVersionExW( &info );
421 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
422 /* FIXME: this may not be 100% correct; see discussion on the
423 * wine developer list in Nov 1999 */
424 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
426 return version;
430 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
432 HBRUSH hbrush;
433 UINT msg;
435 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
436 msg = WM_CTLCOLORSTATIC;
437 else
438 msg = WM_CTLCOLOREDIT;
440 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
441 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
442 if (!hbrush)
443 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
444 return hbrush;
447 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
449 if(unicode)
450 return DefWindowProcW(hwnd, msg, wParam, lParam);
451 else
452 return DefWindowProcA(hwnd, msg, wParam, lParam);
455 /*********************************************************************
457 * EditWndProc_common
459 * The messages are in the order of the actual integer values
460 * (which can be found in include/windows.h)
461 * Wherever possible the 16 bit versions are converted to
462 * the 32 bit ones, so that we can 'fall through' to the
463 * helper functions. These are mostly 32 bit (with a few
464 * exceptions, clearly indicated by a '16' extension to their
465 * names).
468 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
469 WPARAM wParam, LPARAM lParam, BOOL unicode )
471 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
472 LRESULT result = 0;
474 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, msg, wParam, lParam);
476 if (!es && msg != WM_NCCREATE)
477 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
479 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
481 switch (msg) {
482 case EM_GETSEL16:
483 wParam = 0;
484 lParam = 0;
485 /* fall through */
486 case EM_GETSEL:
487 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
488 break;
490 case EM_SETSEL16:
491 if ((short)LOWORD(lParam) == -1)
492 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
493 else
494 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
495 if (!wParam)
496 EDIT_EM_ScrollCaret(es);
497 result = 1;
498 break;
499 case EM_SETSEL:
500 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
501 EDIT_EM_ScrollCaret(es);
502 result = 1;
503 break;
505 case EM_GETRECT16:
506 if (lParam)
508 RECT16 *r16 = MapSL(lParam);
509 r16->left = es->format_rect.left;
510 r16->top = es->format_rect.top;
511 r16->right = es->format_rect.right;
512 r16->bottom = es->format_rect.bottom;
514 break;
515 case EM_GETRECT:
516 if (lParam)
517 CopyRect((LPRECT)lParam, &es->format_rect);
518 break;
520 case EM_SETRECT16:
521 if ((es->style & ES_MULTILINE) && lParam) {
522 RECT rc;
523 RECT16 *r16 = MapSL(lParam);
524 rc.left = r16->left;
525 rc.top = r16->top;
526 rc.right = r16->right;
527 rc.bottom = r16->bottom;
528 EDIT_SetRectNP(es, &rc);
529 EDIT_UpdateText(es, NULL, TRUE);
531 break;
532 case EM_SETRECT:
533 if ((es->style & ES_MULTILINE) && lParam) {
534 EDIT_SetRectNP(es, (LPRECT)lParam);
535 EDIT_UpdateText(es, NULL, TRUE);
537 break;
539 case EM_SETRECTNP16:
540 if ((es->style & ES_MULTILINE) && lParam) {
541 RECT rc;
542 RECT16 *r16 = MapSL(lParam);
543 rc.left = r16->left;
544 rc.top = r16->top;
545 rc.right = r16->right;
546 rc.bottom = r16->bottom;
547 EDIT_SetRectNP(es, &rc);
549 break;
550 case EM_SETRECTNP:
551 if ((es->style & ES_MULTILINE) && lParam)
552 EDIT_SetRectNP(es, (LPRECT)lParam);
553 break;
555 case EM_SCROLL16:
556 case EM_SCROLL:
557 result = EDIT_EM_Scroll(es, (INT)wParam);
558 break;
560 case EM_LINESCROLL16:
561 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
562 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
563 /* fall through */
564 case EM_LINESCROLL:
565 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
566 break;
568 case EM_SCROLLCARET16:
569 case EM_SCROLLCARET:
570 EDIT_EM_ScrollCaret(es);
571 result = 1;
572 break;
574 case EM_GETMODIFY16:
575 case EM_GETMODIFY:
576 result = ((es->flags & EF_MODIFIED) != 0);
577 break;
579 case EM_SETMODIFY16:
580 case EM_SETMODIFY:
581 if (wParam)
582 es->flags |= EF_MODIFIED;
583 else
584 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
585 break;
587 case EM_GETLINECOUNT16:
588 case EM_GETLINECOUNT:
589 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
590 break;
592 case EM_LINEINDEX16:
593 if ((INT16)wParam == -1)
594 wParam = (WPARAM)-1;
595 /* fall through */
596 case EM_LINEINDEX:
597 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
598 break;
600 case EM_SETHANDLE16:
601 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
602 break;
603 case EM_SETHANDLE:
604 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
605 break;
607 case EM_GETHANDLE16:
608 result = (LRESULT)EDIT_EM_GetHandle16(es);
609 break;
610 case EM_GETHANDLE:
611 result = (LRESULT)EDIT_EM_GetHandle(es);
612 break;
614 case EM_GETTHUMB16:
615 case EM_GETTHUMB:
616 result = EDIT_EM_GetThumb(es);
617 break;
619 /* these messages missing from specs */
620 case WM_USER+15:
621 case 0x00bf:
622 case WM_USER+16:
623 case 0x00c0:
624 case WM_USER+19:
625 case 0x00c3:
626 case WM_USER+26:
627 case 0x00ca:
628 FIXME("undocumented message 0x%x, please report\n", msg);
629 result = DefWindowProcW(hwnd, msg, wParam, lParam);
630 break;
632 case EM_LINELENGTH16:
633 case EM_LINELENGTH:
634 if (USER32_DisplayUnixPaths(es))
636 WCHAR textW[MAX_PATH];
637 return EDIT_WM_GetText(es, MAX_PATH, textW, TRUE);
639 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
640 break;
642 case EM_REPLACESEL16:
643 lParam = (LPARAM)MapSL(lParam);
644 unicode = FALSE; /* 16-bit message is always ascii */
645 /* fall through */
646 case EM_REPLACESEL:
648 LPWSTR textW;
650 if(unicode)
651 textW = (LPWSTR)lParam;
652 else
654 LPSTR textA = (LPSTR)lParam;
655 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
656 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
657 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
660 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
661 result = 1;
663 if(!unicode)
664 HeapFree(GetProcessHeap(), 0, textW);
665 break;
668 case EM_GETLINE16:
669 lParam = (LPARAM)MapSL(lParam);
670 unicode = FALSE; /* 16-bit message is always ascii */
671 /* fall through */
672 case EM_GETLINE:
673 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
674 break;
676 case EM_LIMITTEXT16:
677 case EM_SETLIMITTEXT:
678 EDIT_EM_SetLimitText(es, (INT)wParam);
679 break;
681 case EM_CANUNDO16:
682 case EM_CANUNDO:
683 result = (LRESULT)EDIT_EM_CanUndo(es);
684 break;
686 case EM_UNDO16:
687 case EM_UNDO:
688 case WM_UNDO:
689 result = (LRESULT)EDIT_EM_Undo(es);
690 break;
692 case EM_FMTLINES16:
693 case EM_FMTLINES:
694 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
695 break;
697 case EM_LINEFROMCHAR16:
698 case EM_LINEFROMCHAR:
699 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
700 break;
702 case EM_SETTABSTOPS16:
703 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
704 break;
705 case EM_SETTABSTOPS:
706 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
707 break;
709 case EM_SETPASSWORDCHAR16:
710 unicode = FALSE; /* 16-bit message is always ascii */
711 /* fall through */
712 case EM_SETPASSWORDCHAR:
714 WCHAR charW = 0;
716 if(unicode)
717 charW = (WCHAR)wParam;
718 else
720 CHAR charA = wParam;
721 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
724 EDIT_EM_SetPasswordChar(es, charW);
725 break;
728 case EM_EMPTYUNDOBUFFER16:
729 case EM_EMPTYUNDOBUFFER:
730 EDIT_EM_EmptyUndoBuffer(es);
731 break;
733 case EM_GETFIRSTVISIBLELINE16:
734 result = es->y_offset;
735 break;
736 case EM_GETFIRSTVISIBLELINE:
737 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
738 break;
740 case EM_SETREADONLY16:
741 case EM_SETREADONLY:
742 if (wParam) {
743 SetWindowLongW( hwnd, GWL_STYLE,
744 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
745 es->style |= ES_READONLY;
746 } else {
747 SetWindowLongW( hwnd, GWL_STYLE,
748 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
749 es->style &= ~ES_READONLY;
751 result = 1;
752 break;
754 case EM_SETWORDBREAKPROC16:
755 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
756 break;
757 case EM_SETWORDBREAKPROC:
758 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
759 break;
761 case EM_GETWORDBREAKPROC16:
762 result = (LRESULT)es->word_break_proc16;
763 break;
764 case EM_GETWORDBREAKPROC:
765 result = (LRESULT)es->word_break_proc;
766 break;
768 case EM_GETPASSWORDCHAR16:
769 unicode = FALSE; /* 16-bit message is always ascii */
770 /* fall through */
771 case EM_GETPASSWORDCHAR:
773 if(unicode)
774 result = es->password_char;
775 else
777 WCHAR charW = es->password_char;
778 CHAR charA = 0;
779 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
780 result = charA;
782 break;
785 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
787 case EM_SETMARGINS:
788 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
789 break;
791 case EM_GETMARGINS:
792 result = MAKELONG(es->left_margin, es->right_margin);
793 break;
795 case EM_GETLIMITTEXT:
796 result = es->buffer_limit;
797 break;
799 case EM_POSFROMCHAR:
800 result = strlenW(es->text);
801 if ((INT)wParam >= result) result = -1;
802 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
803 break;
805 case EM_CHARFROMPOS:
806 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
807 break;
809 /* End of the EM_ messages which were in numerical order; what order
810 * are these in? vaguely alphabetical?
813 case WM_NCCREATE:
814 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
815 break;
817 case WM_DESTROY:
818 result = EDIT_WM_Destroy(es);
819 es = NULL;
820 break;
822 case WM_GETDLGCODE:
823 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
825 if (es->style & ES_MULTILINE)
827 result |= DLGC_WANTALLKEYS;
828 break;
831 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
833 int vk = (int)((LPMSG)lParam)->wParam;
835 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
837 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
838 result |= DLGC_WANTMESSAGE;
841 break;
843 case WM_IME_CHAR:
844 if (!unicode)
846 WCHAR charW;
847 CHAR strng[2];
849 strng[0] = wParam >> 8;
850 strng[1] = wParam & 0xff;
851 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
852 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
853 EDIT_WM_Char(es, charW);
854 break;
856 /* fall through */
857 case WM_CHAR:
859 WCHAR charW;
861 if(unicode)
862 charW = wParam;
863 else
865 CHAR charA = wParam;
866 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
869 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
871 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
872 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
873 break;
875 EDIT_WM_Char(es, charW);
876 break;
879 case WM_CLEAR:
880 EDIT_WM_Clear(es);
881 break;
883 case WM_COMMAND:
884 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
885 break;
887 case WM_CONTEXTMENU:
888 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
889 break;
891 case WM_COPY:
892 EDIT_WM_Copy(es);
893 break;
895 case WM_CREATE:
896 if(unicode)
897 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
898 else
900 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
901 LPWSTR nameW = NULL;
902 if(nameA)
904 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
905 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
906 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
908 result = EDIT_WM_Create(es, nameW);
909 HeapFree(GetProcessHeap(), 0, nameW);
911 break;
913 case WM_CUT:
914 EDIT_WM_Cut(es);
915 break;
917 case WM_ENABLE:
918 es->bEnableState = (BOOL) wParam;
919 EDIT_UpdateText(es, NULL, TRUE);
920 break;
922 case WM_ERASEBKGND:
923 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
924 break;
926 case WM_GETFONT:
927 result = (LRESULT)es->font;
928 break;
930 case WM_GETTEXT:
931 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
932 break;
934 case WM_GETTEXTLENGTH:
935 if (USER32_DisplayUnixPaths(es))
937 WCHAR textW[MAX_PATH];
938 return EDIT_WM_GetText(es, MAX_PATH, textW, TRUE);
940 if (unicode) result = strlenW(es->text);
941 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
942 NULL, 0, NULL, NULL );
943 break;
945 case WM_HSCROLL:
946 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
947 break;
949 case WM_KEYDOWN:
950 result = EDIT_WM_KeyDown(es, (INT)wParam);
951 break;
953 case WM_KILLFOCUS:
954 result = EDIT_WM_KillFocus(es);
955 break;
957 case WM_LBUTTONDBLCLK:
958 result = EDIT_WM_LButtonDblClk(es);
959 break;
961 case WM_LBUTTONDOWN:
962 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
963 break;
965 case WM_LBUTTONUP:
966 result = EDIT_WM_LButtonUp(es);
967 break;
969 case WM_MBUTTONDOWN:
970 result = EDIT_WM_MButtonDown(es);
971 break;
973 case WM_MOUSEMOVE:
974 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
975 break;
977 case WM_PRINTCLIENT:
978 case WM_PAINT:
979 EDIT_WM_Paint(es, (HDC)wParam);
980 break;
982 case WM_PASTE:
983 EDIT_WM_Paste(es);
984 break;
986 case WM_SETFOCUS:
987 EDIT_WM_SetFocus(es);
988 break;
990 case WM_SETFONT:
991 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
992 break;
994 case WM_SETREDRAW:
995 /* FIXME: actually set an internal flag and behave accordingly */
996 break;
998 case WM_SETTEXT:
999 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
1000 result = TRUE;
1001 break;
1003 case WM_SIZE:
1004 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
1005 break;
1007 case WM_STYLECHANGED:
1008 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
1009 break;
1011 case WM_STYLECHANGING:
1012 result = 0; /* See EDIT_WM_StyleChanged */
1013 break;
1015 case WM_SYSKEYDOWN:
1016 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
1017 break;
1019 case WM_TIMER:
1020 EDIT_WM_Timer(es);
1021 break;
1023 case WM_VSCROLL:
1024 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
1025 break;
1027 case WM_MOUSEWHEEL:
1029 int gcWheelDelta = 0;
1030 UINT pulScrollLines = 3;
1031 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1033 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1034 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1035 break;
1037 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1038 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1040 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1041 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1042 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1045 break;
1048 /* IME messages to make the edit control IME aware */
1049 case WM_IME_SETCONTEXT:
1050 break;
1052 case WM_IME_STARTCOMPOSITION:
1054 * FIXME in IME: This message is not always sent like it should be
1056 if (es->selection_start != es->selection_end)
1058 static const WCHAR empty_stringW[] = {0};
1059 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1061 es->composition_start = es->selection_end;
1062 es->composition_len = 0;
1063 break;
1065 case WM_IME_COMPOSITION:
1066 if (es->composition_len == 0)
1068 if (es->selection_start != es->selection_end)
1070 static const WCHAR empty_stringW[] = {0};
1071 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1074 es->composition_start = es->selection_end;
1076 EDIT_ImeComposition(hwnd,lParam,es);
1077 break;
1079 case WM_IME_ENDCOMPOSITION:
1080 es->composition_len= 0;
1081 break;
1083 case WM_IME_COMPOSITIONFULL:
1084 break;
1086 case WM_IME_SELECT:
1087 break;
1089 case WM_IME_CONTROL:
1090 break;
1092 default:
1093 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1094 break;
1097 if (es) EDIT_UnlockBuffer(es, FALSE);
1099 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1101 return result;
1104 /*********************************************************************
1106 * EditWndProcW (USER32.@)
1108 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1110 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1113 /*********************************************************************
1115 * EditWndProc (USER32.@)
1117 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1119 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1122 /*********************************************************************
1124 * EDIT_BuildLineDefs_ML
1126 * Build linked list of text lines.
1127 * Lines can end with '\0' (last line), a character (if it is wrapped),
1128 * a soft return '\r\r\n' or a hard return '\r\n'
1131 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1133 HDC dc;
1134 HFONT old_font = 0;
1135 LPWSTR current_position, cp;
1136 INT fw;
1137 LINEDEF *current_line;
1138 LINEDEF *previous_line;
1139 LINEDEF *start_line;
1140 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1141 INT line_count = es->line_count;
1142 INT orig_net_length;
1143 RECT rc;
1145 if (istart == iend && delta == 0)
1146 return;
1148 dc = GetDC(es->hwndSelf);
1149 if (es->font)
1150 old_font = SelectObject(dc, es->font);
1152 previous_line = NULL;
1153 current_line = es->first_line_def;
1155 /* Find starting line. istart must lie inside an existing line or
1156 * at the end of buffer */
1157 do {
1158 if (istart < current_line->index + current_line->length ||
1159 current_line->ending == END_0)
1160 break;
1162 previous_line = current_line;
1163 current_line = current_line->next;
1164 line_index++;
1165 } while (current_line);
1167 if (!current_line) /* Error occurred start is not inside previous buffer */
1169 FIXME(" modification occurred outside buffer\n");
1170 ReleaseDC(es->hwndSelf, dc);
1171 return;
1174 /* Remember start of modifications in order to calculate update region */
1175 nstart_line = line_index;
1176 nstart_index = current_line->index;
1178 /* We must start to reformat from the previous line since the modifications
1179 * may have caused the line to wrap upwards. */
1180 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1182 line_index--;
1183 current_line = previous_line;
1185 start_line = current_line;
1187 fw = es->format_rect.right - es->format_rect.left;
1188 current_position = es->text + current_line->index;
1189 do {
1190 if (current_line != start_line)
1192 if (!current_line || current_line->index + delta > current_position - es->text)
1194 /* The buffer has been expanded, create a new line and
1195 insert it into the link list */
1196 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1197 new_line->next = previous_line->next;
1198 previous_line->next = new_line;
1199 current_line = new_line;
1200 es->line_count++;
1202 else if (current_line->index + delta < current_position - es->text)
1204 /* The previous line merged with this line so we delete this extra entry */
1205 previous_line->next = current_line->next;
1206 HeapFree(GetProcessHeap(), 0, current_line);
1207 current_line = previous_line->next;
1208 es->line_count--;
1209 continue;
1211 else /* current_line->index + delta == current_position */
1213 if (current_position - es->text > iend)
1214 break; /* We reached end of line modifications */
1215 /* else recalulate this line */
1219 current_line->index = current_position - es->text;
1220 orig_net_length = current_line->net_length;
1222 /* Find end of line */
1223 cp = current_position;
1224 while (*cp) {
1225 if (*cp == '\n') break;
1226 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1227 break;
1228 cp++;
1231 /* Mark type of line termination */
1232 if (!(*cp)) {
1233 current_line->ending = END_0;
1234 current_line->net_length = strlenW(current_position);
1235 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1236 current_line->ending = END_SOFT;
1237 current_line->net_length = cp - current_position - 1;
1238 } else if (*cp == '\n') {
1239 current_line->ending = END_RICH;
1240 current_line->net_length = cp - current_position;
1241 } else {
1242 current_line->ending = END_HARD;
1243 current_line->net_length = cp - current_position;
1246 /* Calculate line width */
1247 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1248 current_position, current_line->net_length,
1249 es->tabs_count, es->tabs));
1251 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1252 if (!(es->style & ES_AUTOHSCROLL)) {
1253 if (current_line->width > fw) {
1254 INT next = 0;
1255 INT prev;
1256 do {
1257 prev = next;
1258 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1259 prev + 1, current_line->net_length, WB_RIGHT);
1260 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1261 current_position, next, es->tabs_count, es->tabs));
1262 } while (current_line->width <= fw);
1263 if (!prev) { /* Didn't find a line break so force a break */
1264 next = 0;
1265 do {
1266 prev = next;
1267 next++;
1268 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1269 current_position, next, es->tabs_count, es->tabs));
1270 } while (current_line->width <= fw);
1271 if (!prev)
1272 prev = 1;
1275 /* If the first line we are calculating, wrapped before istart, we must
1276 * adjust istart in order for this to be reflected in the update region. */
1277 if (current_line->index == nstart_index && istart > current_line->index + prev)
1278 istart = current_line->index + prev;
1279 /* else if we are updating the previous line before the first line we
1280 * are re-calculating and it expanded */
1281 else if (current_line == start_line &&
1282 current_line->index != nstart_index && orig_net_length < prev)
1284 /* Line expanded due to an upwards line wrap so we must partially include
1285 * previous line in update region */
1286 nstart_line = line_index;
1287 nstart_index = current_line->index;
1288 istart = current_line->index + orig_net_length;
1291 current_line->net_length = prev;
1292 current_line->ending = END_WRAP;
1293 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1294 current_line->net_length, es->tabs_count, es->tabs));
1296 else if (orig_net_length < current_line->net_length &&
1297 current_line == start_line &&
1298 current_line->index != nstart_index) {
1299 /* The previous line expanded but it's still not as wide as the client rect */
1300 /* The expansion is due to an upwards line wrap so we must partially include
1301 it in the update region */
1302 nstart_line = line_index;
1303 nstart_index = current_line->index;
1304 istart = current_line->index + orig_net_length;
1309 /* Adjust length to include line termination */
1310 switch (current_line->ending) {
1311 case END_SOFT:
1312 current_line->length = current_line->net_length + 3;
1313 break;
1314 case END_RICH:
1315 current_line->length = current_line->net_length + 1;
1316 break;
1317 case END_HARD:
1318 current_line->length = current_line->net_length + 2;
1319 break;
1320 case END_WRAP:
1321 case END_0:
1322 current_line->length = current_line->net_length;
1323 break;
1325 es->text_width = max(es->text_width, current_line->width);
1326 current_position += current_line->length;
1327 previous_line = current_line;
1328 current_line = current_line->next;
1329 line_index++;
1330 } while (previous_line->ending != END_0);
1332 /* Finish adjusting line indexes by delta or remove hanging lines */
1333 if (previous_line->ending == END_0)
1335 LINEDEF *pnext = NULL;
1337 previous_line->next = NULL;
1338 while (current_line)
1340 pnext = current_line->next;
1341 HeapFree(GetProcessHeap(), 0, current_line);
1342 current_line = pnext;
1343 es->line_count--;
1346 else if (delta != 0)
1348 while (current_line)
1350 current_line->index += delta;
1351 current_line = current_line->next;
1355 /* Calculate rest of modification rectangle */
1356 if (hrgn)
1358 HRGN tmphrgn;
1360 * We calculate two rectangles. One for the first line which may have
1361 * an indent with respect to the format rect. The other is a format-width
1362 * rectangle that spans the rest of the lines that changed or moved.
1364 rc.top = es->format_rect.top + nstart_line * es->line_height -
1365 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1366 rc.bottom = rc.top + es->line_height;
1367 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1368 rc.left = es->format_rect.left;
1369 else
1370 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1371 es->text + nstart_index, istart - nstart_index,
1372 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1373 rc.right = es->format_rect.right;
1374 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1376 rc.top = rc.bottom;
1377 rc.left = es->format_rect.left;
1378 rc.right = es->format_rect.right;
1380 * If lines were added or removed we must re-paint the remainder of the
1381 * lines since the remaining lines were either shifted up or down.
1383 if (line_count < es->line_count) /* We added lines */
1384 rc.bottom = es->line_count * es->line_height;
1385 else if (line_count > es->line_count) /* We removed lines */
1386 rc.bottom = line_count * es->line_height;
1387 else
1388 rc.bottom = line_index * es->line_height;
1389 rc.bottom += es->format_rect.top;
1390 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1391 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1392 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1393 DeleteObject(tmphrgn);
1396 if (es->font)
1397 SelectObject(dc, old_font);
1399 ReleaseDC(es->hwndSelf, dc);
1402 /*********************************************************************
1404 * EDIT_CalcLineWidth_SL
1407 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1409 SIZE size;
1410 LPWSTR text;
1411 HDC dc;
1412 HFONT old_font = 0;
1414 text = EDIT_GetPasswordPointer_SL(es);
1416 dc = GetDC(es->hwndSelf);
1417 if (es->font)
1418 old_font = SelectObject(dc, es->font);
1420 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1422 if (es->font)
1423 SelectObject(dc, old_font);
1424 ReleaseDC(es->hwndSelf, dc);
1426 if (es->style & ES_PASSWORD)
1427 HeapFree(GetProcessHeap(), 0, text);
1429 es->text_width = size.cx;
1432 /*********************************************************************
1434 * EDIT_CallWordBreakProc
1436 * Call appropriate WordBreakProc (internal or external).
1438 * Note: The "start" argument should always be an index referring
1439 * to es->text. The actual wordbreak proc might be
1440 * 16 bit, so we can't always pass any 32 bit LPSTR.
1441 * Hence we assume that es->text is the buffer that holds
1442 * the string under examination (we can decide this for ourselves).
1445 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1447 INT ret;
1449 if (es->word_break_proc16) {
1450 HGLOBAL16 hglob16;
1451 SEGPTR segptr;
1452 INT countA;
1453 WORD args[5];
1454 DWORD result;
1456 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1457 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1458 segptr = WOWGlobalLock16(hglob16);
1459 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1460 args[4] = SELECTOROF(segptr);
1461 args[3] = OFFSETOF(segptr);
1462 args[2] = index;
1463 args[1] = countA;
1464 args[0] = action;
1465 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1466 ret = LOWORD(result);
1467 GlobalUnlock16(hglob16);
1468 GlobalFree16(hglob16);
1470 else if (es->word_break_proc)
1472 if(es->is_unicode)
1474 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1476 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1477 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1478 ret = wbpW(es->text + start, index, count, action);
1480 else
1482 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1483 INT countA;
1484 CHAR *textA;
1486 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1487 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1488 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1489 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1490 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1491 ret = wbpA(textA, index, countA, action);
1492 HeapFree(GetProcessHeap(), 0, textA);
1495 else
1496 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1498 return ret;
1502 /*********************************************************************
1504 * EDIT_CharFromPos
1506 * Beware: This is not the function called on EM_CHARFROMPOS
1507 * The position _can_ be outside the formatting / client
1508 * rectangle
1509 * The return value is only the character index
1512 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1514 INT index;
1515 HDC dc;
1516 HFONT old_font = 0;
1517 INT x_high = 0, x_low = 0;
1519 if (es->style & ES_MULTILINE) {
1520 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1521 INT line_index = 0;
1522 LINEDEF *line_def = es->first_line_def;
1523 INT low, high;
1524 while ((line > 0) && line_def->next) {
1525 line_index += line_def->length;
1526 line_def = line_def->next;
1527 line--;
1529 x += es->x_offset - es->format_rect.left;
1530 if (es->style & ES_RIGHT)
1531 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1532 else if (es->style & ES_CENTER)
1533 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1534 if (x >= line_def->width) {
1535 if (after_wrap)
1536 *after_wrap = (line_def->ending == END_WRAP);
1537 return line_index + line_def->net_length;
1539 if (x <= 0) {
1540 if (after_wrap)
1541 *after_wrap = FALSE;
1542 return line_index;
1544 dc = GetDC(es->hwndSelf);
1545 if (es->font)
1546 old_font = SelectObject(dc, es->font);
1547 low = line_index;
1548 high = line_index + line_def->net_length + 1;
1549 while (low < high - 1)
1551 INT mid = (low + high) / 2;
1552 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1553 if (x_now > x) {
1554 high = mid;
1555 x_high = x_now;
1556 } else {
1557 low = mid;
1558 x_low = x_now;
1561 if (abs(x_high - x) + 1 <= abs(x_low - x))
1562 index = high;
1563 else
1564 index = low;
1566 if (after_wrap)
1567 *after_wrap = ((index == line_index + line_def->net_length) &&
1568 (line_def->ending == END_WRAP));
1569 } else {
1570 LPWSTR text;
1571 SIZE size;
1572 if (after_wrap)
1573 *after_wrap = FALSE;
1574 x -= es->format_rect.left;
1575 if (!x)
1576 return es->x_offset;
1578 if (!es->x_offset)
1580 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1581 if (es->style & ES_RIGHT)
1582 x -= indent;
1583 else if (es->style & ES_CENTER)
1584 x -= indent / 2;
1587 text = EDIT_GetPasswordPointer_SL(es);
1588 dc = GetDC(es->hwndSelf);
1589 if (es->font)
1590 old_font = SelectObject(dc, es->font);
1591 if (x < 0)
1593 INT low = 0;
1594 INT high = es->x_offset;
1595 while (low < high - 1)
1597 INT mid = (low + high) / 2;
1598 GetTextExtentPoint32W( dc, text + mid,
1599 es->x_offset - mid, &size );
1600 if (size.cx > -x) {
1601 low = mid;
1602 x_low = size.cx;
1603 } else {
1604 high = mid;
1605 x_high = size.cx;
1608 if (abs(x_high + x) <= abs(x_low + x) + 1)
1609 index = high;
1610 else
1611 index = low;
1613 else
1615 INT low = es->x_offset;
1616 INT high = strlenW(es->text) + 1;
1617 while (low < high - 1)
1619 INT mid = (low + high) / 2;
1620 GetTextExtentPoint32W( dc, text + es->x_offset,
1621 mid - es->x_offset, &size );
1622 if (size.cx > x) {
1623 high = mid;
1624 x_high = size.cx;
1625 } else {
1626 low = mid;
1627 x_low = size.cx;
1630 if (abs(x_high - x) <= abs(x_low - x) + 1)
1631 index = high;
1632 else
1633 index = low;
1635 if (es->style & ES_PASSWORD)
1636 HeapFree(GetProcessHeap(), 0, text);
1638 if (es->font)
1639 SelectObject(dc, old_font);
1640 ReleaseDC(es->hwndSelf, dc);
1641 return index;
1645 /*********************************************************************
1647 * EDIT_ConfinePoint
1649 * adjusts the point to be within the formatting rectangle
1650 * (so CharFromPos returns the nearest _visible_ character)
1653 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1655 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1656 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1660 /*********************************************************************
1662 * EDIT_GetLineRect
1664 * Calculates the bounding rectangle for a line from a starting
1665 * column to an ending column.
1668 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1670 INT line_index = EDIT_EM_LineIndex(es, line);
1672 if (es->style & ES_MULTILINE)
1673 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1674 else
1675 rc->top = es->format_rect.top;
1676 rc->bottom = rc->top + es->line_height;
1677 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1678 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1682 /*********************************************************************
1684 * EDIT_GetPasswordPointer_SL
1686 * note: caller should free the (optionally) allocated buffer
1689 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1691 if (es->style & ES_PASSWORD) {
1692 INT len = strlenW(es->text);
1693 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1694 text[len] = '\0';
1695 while(len) text[--len] = es->password_char;
1696 return text;
1697 } else
1698 return es->text;
1702 /*********************************************************************
1704 * EDIT_LockBuffer
1706 * This acts as a LocalLock16(), but it locks only once. This way
1707 * you can call it whenever you like, without unlocking.
1709 * Initially the edit control allocates a HLOCAL32 buffer
1710 * (32 bit linear memory handler). However, 16 bit application
1711 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1712 * handler). From that moment on we have to keep using this 16 bit memory
1713 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1714 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1715 * conversion.
1718 static void EDIT_LockBuffer(EDITSTATE *es)
1720 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1721 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1723 if (!es->text) {
1724 CHAR *textA = NULL;
1725 UINT countA = 0;
1726 BOOL _16bit = FALSE;
1728 if(es->hloc32W)
1730 if(es->hloc32A)
1732 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1733 textA = LocalLock(es->hloc32A);
1734 countA = strlen(textA) + 1;
1736 else if(es->hloc16)
1738 HANDLE16 oldDS = stack16->ds;
1739 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1740 stack16->ds = hInstance;
1741 textA = MapSL(LocalLock16(es->hloc16));
1742 stack16->ds = oldDS;
1743 countA = strlen(textA) + 1;
1744 _16bit = TRUE;
1747 else {
1748 ERR("no buffer ... please report\n");
1749 return;
1752 if(textA)
1754 HLOCAL hloc32W_new;
1755 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1756 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1757 if(countW_new > es->buffer_size + 1)
1759 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1760 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1761 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1762 if(hloc32W_new)
1764 es->hloc32W = hloc32W_new;
1765 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1766 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1768 else
1769 WARN("FAILED! Will synchronize partially\n");
1773 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1774 es->text = LocalLock(es->hloc32W);
1776 if(textA)
1778 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1779 if(_16bit)
1781 HANDLE16 oldDS = stack16->ds;
1782 stack16->ds = hInstance;
1783 LocalUnlock16(es->hloc16);
1784 stack16->ds = oldDS;
1786 else
1787 LocalUnlock(es->hloc32A);
1790 es->lock_count++;
1794 /*********************************************************************
1796 * EDIT_SL_InvalidateText
1798 * Called from EDIT_InvalidateText().
1799 * Does the job for single-line controls only.
1802 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1804 RECT line_rect;
1805 RECT rc;
1807 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1808 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1809 EDIT_UpdateText(es, &rc, TRUE);
1813 /*********************************************************************
1815 * EDIT_ML_InvalidateText
1817 * Called from EDIT_InvalidateText().
1818 * Does the job for multi-line controls only.
1821 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1823 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1824 INT sl = EDIT_EM_LineFromChar(es, start);
1825 INT el = EDIT_EM_LineFromChar(es, end);
1826 INT sc;
1827 INT ec;
1828 RECT rc1;
1829 RECT rcWnd;
1830 RECT rcLine;
1831 RECT rcUpdate;
1832 INT l;
1834 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1835 return;
1837 sc = start - EDIT_EM_LineIndex(es, sl);
1838 ec = end - EDIT_EM_LineIndex(es, el);
1839 if (sl < es->y_offset) {
1840 sl = es->y_offset;
1841 sc = 0;
1843 if (el > es->y_offset + vlc) {
1844 el = es->y_offset + vlc;
1845 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1847 GetClientRect(es->hwndSelf, &rc1);
1848 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1849 if (sl == el) {
1850 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1851 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1852 EDIT_UpdateText(es, &rcUpdate, TRUE);
1853 } else {
1854 EDIT_GetLineRect(es, sl, sc,
1855 EDIT_EM_LineLength(es,
1856 EDIT_EM_LineIndex(es, sl)),
1857 &rcLine);
1858 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1859 EDIT_UpdateText(es, &rcUpdate, TRUE);
1860 for (l = sl + 1 ; l < el ; l++) {
1861 EDIT_GetLineRect(es, l, 0,
1862 EDIT_EM_LineLength(es,
1863 EDIT_EM_LineIndex(es, l)),
1864 &rcLine);
1865 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1866 EDIT_UpdateText(es, &rcUpdate, TRUE);
1868 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1869 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1870 EDIT_UpdateText(es, &rcUpdate, TRUE);
1875 /*********************************************************************
1877 * EDIT_InvalidateText
1879 * Invalidate the text from offset start up to, but not including,
1880 * offset end. Useful for (re)painting the selection.
1881 * Regions outside the linewidth are not invalidated.
1882 * end == -1 means end == TextLength.
1883 * start and end need not be ordered.
1886 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1888 if (end == start)
1889 return;
1891 if (end == -1)
1892 end = strlenW(es->text);
1894 if (end < start) {
1895 INT tmp = start;
1896 start = end;
1897 end = tmp;
1900 if (es->style & ES_MULTILINE)
1901 EDIT_ML_InvalidateText(es, start, end);
1902 else
1903 EDIT_SL_InvalidateText(es, start, end);
1907 /*********************************************************************
1909 * EDIT_MakeFit
1911 * Try to fit size + 1 characters in the buffer.
1913 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1915 HLOCAL hNew32W;
1917 if (size <= es->buffer_size)
1918 return TRUE;
1920 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1922 /* Force edit to unlock it's buffer. es->text now NULL */
1923 EDIT_UnlockBuffer(es, TRUE);
1925 if (es->hloc32W) {
1926 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1927 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1928 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1929 es->hloc32W = hNew32W;
1930 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1934 EDIT_LockBuffer(es);
1936 if (es->buffer_size < size) {
1937 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1938 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1939 return FALSE;
1940 } else {
1941 TRACE("We now have %d+1\n", es->buffer_size);
1942 return TRUE;
1947 /*********************************************************************
1949 * EDIT_MakeUndoFit
1951 * Try to fit size + 1 bytes in the undo buffer.
1954 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1956 UINT alloc_size;
1958 if (size <= es->undo_buffer_size)
1959 return TRUE;
1961 TRACE("trying to ReAlloc to %d+1\n", size);
1963 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1964 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1965 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1966 return TRUE;
1968 else
1970 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1971 return FALSE;
1976 /*********************************************************************
1978 * EDIT_MoveBackward
1981 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1983 INT e = es->selection_end;
1985 if (e) {
1986 e--;
1987 if ((es->style & ES_MULTILINE) && e &&
1988 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1989 e--;
1990 if (e && (es->text[e - 1] == '\r'))
1991 e--;
1994 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1995 EDIT_EM_ScrollCaret(es);
1999 /*********************************************************************
2001 * EDIT_MoveDown_ML
2003 * Only for multi line controls
2004 * Move the caret one line down, on a column with the nearest
2005 * x coordinate on the screen (might be a different column).
2008 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
2010 INT s = es->selection_start;
2011 INT e = es->selection_end;
2012 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2013 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2014 INT x = (short)LOWORD(pos);
2015 INT y = (short)HIWORD(pos);
2017 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
2018 if (!extend)
2019 s = e;
2020 EDIT_EM_SetSel(es, s, e, after_wrap);
2021 EDIT_EM_ScrollCaret(es);
2025 /*********************************************************************
2027 * EDIT_MoveEnd
2030 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
2032 BOOL after_wrap = FALSE;
2033 INT e;
2035 /* Pass a high value in x to make sure of receiving the end of the line */
2036 if (es->style & ES_MULTILINE)
2037 e = EDIT_CharFromPos(es, 0x3fffffff,
2038 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2039 else
2040 e = strlenW(es->text);
2041 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2042 EDIT_EM_ScrollCaret(es);
2046 /*********************************************************************
2048 * EDIT_MoveForward
2051 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2053 INT e = es->selection_end;
2055 if (es->text[e]) {
2056 e++;
2057 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2058 if (es->text[e] == '\n')
2059 e++;
2060 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2061 e += 2;
2064 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2065 EDIT_EM_ScrollCaret(es);
2069 /*********************************************************************
2071 * EDIT_MoveHome
2073 * Home key: move to beginning of line.
2076 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2078 INT e;
2080 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2081 if (es->style & ES_MULTILINE)
2082 e = EDIT_CharFromPos(es, -es->x_offset,
2083 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2084 else
2085 e = 0;
2086 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2087 EDIT_EM_ScrollCaret(es);
2091 /*********************************************************************
2093 * EDIT_MovePageDown_ML
2095 * Only for multi line controls
2096 * Move the caret one page down, on a column with the nearest
2097 * x coordinate on the screen (might be a different column).
2100 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2102 INT s = es->selection_start;
2103 INT e = es->selection_end;
2104 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2105 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2106 INT x = (short)LOWORD(pos);
2107 INT y = (short)HIWORD(pos);
2109 e = EDIT_CharFromPos(es, x,
2110 y + (es->format_rect.bottom - es->format_rect.top),
2111 &after_wrap);
2112 if (!extend)
2113 s = e;
2114 EDIT_EM_SetSel(es, s, e, after_wrap);
2115 EDIT_EM_ScrollCaret(es);
2119 /*********************************************************************
2121 * EDIT_MovePageUp_ML
2123 * Only for multi line controls
2124 * Move the caret one page up, on a column with the nearest
2125 * x coordinate on the screen (might be a different column).
2128 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2130 INT s = es->selection_start;
2131 INT e = es->selection_end;
2132 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2133 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2134 INT x = (short)LOWORD(pos);
2135 INT y = (short)HIWORD(pos);
2137 e = EDIT_CharFromPos(es, x,
2138 y - (es->format_rect.bottom - es->format_rect.top),
2139 &after_wrap);
2140 if (!extend)
2141 s = e;
2142 EDIT_EM_SetSel(es, s, e, after_wrap);
2143 EDIT_EM_ScrollCaret(es);
2147 /*********************************************************************
2149 * EDIT_MoveUp_ML
2151 * Only for multi line controls
2152 * Move the caret one line up, on a column with the nearest
2153 * x coordinate on the screen (might be a different column).
2156 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2158 INT s = es->selection_start;
2159 INT e = es->selection_end;
2160 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2161 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2162 INT x = (short)LOWORD(pos);
2163 INT y = (short)HIWORD(pos);
2165 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2166 if (!extend)
2167 s = e;
2168 EDIT_EM_SetSel(es, s, e, after_wrap);
2169 EDIT_EM_ScrollCaret(es);
2173 /*********************************************************************
2175 * EDIT_MoveWordBackward
2178 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2180 INT s = es->selection_start;
2181 INT e = es->selection_end;
2182 INT l;
2183 INT ll;
2184 INT li;
2186 l = EDIT_EM_LineFromChar(es, e);
2187 ll = EDIT_EM_LineLength(es, e);
2188 li = EDIT_EM_LineIndex(es, l);
2189 if (e - li == 0) {
2190 if (l) {
2191 li = EDIT_EM_LineIndex(es, l - 1);
2192 e = li + EDIT_EM_LineLength(es, li);
2194 } else {
2195 e = li + (INT)EDIT_CallWordBreakProc(es,
2196 li, e - li, ll, WB_LEFT);
2198 if (!extend)
2199 s = e;
2200 EDIT_EM_SetSel(es, s, e, FALSE);
2201 EDIT_EM_ScrollCaret(es);
2205 /*********************************************************************
2207 * EDIT_MoveWordForward
2210 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2212 INT s = es->selection_start;
2213 INT e = es->selection_end;
2214 INT l;
2215 INT ll;
2216 INT li;
2218 l = EDIT_EM_LineFromChar(es, e);
2219 ll = EDIT_EM_LineLength(es, e);
2220 li = EDIT_EM_LineIndex(es, l);
2221 if (e - li == ll) {
2222 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2223 e = EDIT_EM_LineIndex(es, l + 1);
2224 } else {
2225 e = li + EDIT_CallWordBreakProc(es,
2226 li, e - li + 1, ll, WB_RIGHT);
2228 if (!extend)
2229 s = e;
2230 EDIT_EM_SetSel(es, s, e, FALSE);
2231 EDIT_EM_ScrollCaret(es);
2235 /*********************************************************************
2237 * EDIT_PaintLine
2240 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2242 INT s = es->selection_start;
2243 INT e = es->selection_end;
2244 INT li;
2245 INT ll;
2246 INT x;
2247 INT y;
2248 LRESULT pos;
2250 if (es->style & ES_MULTILINE) {
2251 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2252 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2253 return;
2254 } else if (line)
2255 return;
2257 TRACE("line=%d\n", line);
2259 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2260 x = (short)LOWORD(pos);
2261 y = (short)HIWORD(pos);
2262 li = EDIT_EM_LineIndex(es, line);
2263 ll = EDIT_EM_LineLength(es, li);
2264 s = min(es->selection_start, es->selection_end);
2265 e = max(es->selection_start, es->selection_end);
2266 s = min(li + ll, max(li, s));
2267 e = min(li + ll, max(li, e));
2268 if (rev && (s != e) &&
2269 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2270 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2271 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2272 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2273 } else
2274 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2278 /*********************************************************************
2280 * EDIT_PaintText
2283 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2285 COLORREF BkColor;
2286 COLORREF TextColor;
2287 LOGFONTW underline_font;
2288 HFONT hUnderline = 0;
2289 HFONT old_font = 0;
2290 INT ret;
2291 INT li;
2292 INT BkMode;
2293 SIZE size;
2295 if (!count)
2296 return 0;
2297 BkMode = GetBkMode(dc);
2298 BkColor = GetBkColor(dc);
2299 TextColor = GetTextColor(dc);
2300 if (rev) {
2301 if (es->composition_len == 0)
2303 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2304 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2305 SetBkMode( dc, OPAQUE);
2307 else
2309 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2310 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2311 underline_font.lfUnderline = TRUE;
2312 hUnderline = CreateFontIndirectW(&underline_font);
2313 old_font = SelectObject(dc,hUnderline);
2316 li = EDIT_EM_LineIndex(es, line);
2317 if (es->style & ES_MULTILINE) {
2318 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2319 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2320 } else {
2321 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2322 TextOutW(dc, x, y, text + li + col, count);
2323 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2324 ret = size.cx;
2325 if (es->style & ES_PASSWORD)
2326 HeapFree(GetProcessHeap(), 0, text);
2328 if (rev) {
2329 if (es->composition_len == 0)
2331 SetBkColor(dc, BkColor);
2332 SetTextColor(dc, TextColor);
2333 SetBkMode( dc, BkMode);
2335 else
2337 if (old_font)
2338 SelectObject(dc,old_font);
2339 if (hUnderline)
2340 DeleteObject(hUnderline);
2343 return ret;
2347 /*********************************************************************
2349 * EDIT_SetCaretPos
2352 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2353 BOOL after_wrap)
2355 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2356 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2357 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2361 /*********************************************************************
2363 * EDIT_AdjustFormatRect
2365 * Adjusts the format rectangle for the current font and the
2366 * current client rectangle.
2369 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2371 RECT ClientRect;
2373 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2374 if (es->style & ES_MULTILINE)
2376 INT fw, vlc, max_x_offset, max_y_offset;
2378 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2379 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2381 /* correct es->x_offset */
2382 fw = es->format_rect.right - es->format_rect.left;
2383 max_x_offset = es->text_width - fw;
2384 if(max_x_offset < 0) max_x_offset = 0;
2385 if(es->x_offset > max_x_offset)
2386 es->x_offset = max_x_offset;
2388 /* correct es->y_offset */
2389 max_y_offset = es->line_count - vlc;
2390 if(max_y_offset < 0) max_y_offset = 0;
2391 if(es->y_offset > max_y_offset)
2392 es->y_offset = max_y_offset;
2394 /* force scroll info update */
2395 EDIT_UpdateScrollInfo(es);
2397 else
2398 /* Windows doesn't care to fix text placement for SL controls */
2399 es->format_rect.bottom = es->format_rect.top + es->line_height;
2401 /* Always stay within the client area */
2402 GetClientRect(es->hwndSelf, &ClientRect);
2403 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2405 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2406 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2408 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2412 /*********************************************************************
2414 * EDIT_SetRectNP
2416 * note: this is not (exactly) the handler called on EM_SETRECTNP
2417 * it is also used to set the rect of a single line control
2420 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2422 LONG_PTR ExStyle;
2423 INT bw, bh;
2424 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2426 CopyRect(&es->format_rect, rc);
2428 if (ExStyle & WS_EX_CLIENTEDGE) {
2429 es->format_rect.left++;
2430 es->format_rect.right--;
2432 if (es->format_rect.bottom - es->format_rect.top
2433 >= es->line_height + 2)
2435 es->format_rect.top++;
2436 es->format_rect.bottom--;
2439 else if (es->style & WS_BORDER) {
2440 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2441 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2442 es->format_rect.left += bw;
2443 es->format_rect.right -= bw;
2444 if (es->format_rect.bottom - es->format_rect.top
2445 >= es->line_height + 2 * bh)
2447 es->format_rect.top += bh;
2448 es->format_rect.bottom -= bh;
2452 es->format_rect.left += es->left_margin;
2453 es->format_rect.right -= es->right_margin;
2454 EDIT_AdjustFormatRect(es);
2458 /*********************************************************************
2460 * EDIT_UnlockBuffer
2463 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2466 /* Edit window might be already destroyed */
2467 if(!IsWindow(es->hwndSelf))
2469 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2470 return;
2473 if (!es->lock_count) {
2474 ERR("lock_count == 0 ... please report\n");
2475 return;
2477 if (!es->text) {
2478 ERR("es->text == 0 ... please report\n");
2479 return;
2482 if (force || (es->lock_count == 1)) {
2483 if (es->hloc32W) {
2484 CHAR *textA = NULL;
2485 UINT countA = 0;
2486 UINT countW = strlenW(es->text) + 1;
2487 STACK16FRAME* stack16 = NULL;
2488 HANDLE16 oldDS = 0;
2490 if(es->hloc32A)
2492 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2493 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2494 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2495 countA = LocalSize(es->hloc32A);
2496 if(countA_new > countA)
2498 HLOCAL hloc32A_new;
2499 UINT alloc_size = ROUND_TO_GROW(countA_new);
2500 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2501 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2502 if(hloc32A_new)
2504 es->hloc32A = hloc32A_new;
2505 countA = LocalSize(hloc32A_new);
2506 TRACE("Real new size %d bytes\n", countA);
2508 else
2509 WARN("FAILED! Will synchronize partially\n");
2511 textA = LocalLock(es->hloc32A);
2513 else if(es->hloc16)
2515 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2517 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2518 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2520 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2521 oldDS = stack16->ds;
2522 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2524 countA = LocalSize16(es->hloc16);
2525 if(countA_new > countA)
2527 HLOCAL16 hloc16_new;
2528 UINT alloc_size = ROUND_TO_GROW(countA_new);
2529 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2530 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2531 if(hloc16_new)
2533 es->hloc16 = hloc16_new;
2534 countA = LocalSize16(hloc16_new);
2535 TRACE("Real new size %d bytes\n", countA);
2537 else
2538 WARN("FAILED! Will synchronize partially\n");
2540 textA = MapSL(LocalLock16(es->hloc16));
2543 if(textA)
2545 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2546 if(stack16)
2547 LocalUnlock16(es->hloc16);
2548 else
2549 LocalUnlock(es->hloc32A);
2552 if (stack16) stack16->ds = oldDS;
2553 LocalUnlock(es->hloc32W);
2554 es->text = NULL;
2556 else {
2557 ERR("no buffer ... please report\n");
2558 return;
2561 es->lock_count--;
2565 /*********************************************************************
2567 * EDIT_UpdateScrollInfo
2570 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2572 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2574 SCROLLINFO si;
2575 si.cbSize = sizeof(SCROLLINFO);
2576 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2577 si.nMin = 0;
2578 si.nMax = es->line_count - 1;
2579 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2580 si.nPos = es->y_offset;
2581 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2582 si.nMin, si.nMax, si.nPage, si.nPos);
2583 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2586 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2588 SCROLLINFO si;
2589 si.cbSize = sizeof(SCROLLINFO);
2590 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2591 si.nMin = 0;
2592 si.nMax = es->text_width - 1;
2593 si.nPage = es->format_rect.right - es->format_rect.left;
2594 si.nPos = es->x_offset;
2595 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2596 si.nMin, si.nMax, si.nPage, si.nPos);
2597 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2601 /*********************************************************************
2603 * EDIT_WordBreakProc
2605 * Find the beginning of words.
2606 * Note: unlike the specs for a WordBreakProc, this function only
2607 * allows to be called without linebreaks between s[0] up to
2608 * s[count - 1]. Remember it is only called
2609 * internally, so we can decide this for ourselves.
2612 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2614 INT ret = 0;
2616 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2618 if(!s) return 0;
2620 switch (action) {
2621 case WB_LEFT:
2622 if (!count)
2623 break;
2624 if (index)
2625 index--;
2626 if (s[index] == ' ') {
2627 while (index && (s[index] == ' '))
2628 index--;
2629 if (index) {
2630 while (index && (s[index] != ' '))
2631 index--;
2632 if (s[index] == ' ')
2633 index++;
2635 } else {
2636 while (index && (s[index] != ' '))
2637 index--;
2638 if (s[index] == ' ')
2639 index++;
2641 ret = index;
2642 break;
2643 case WB_RIGHT:
2644 if (!count)
2645 break;
2646 if (index)
2647 index--;
2648 if (s[index] == ' ')
2649 while ((index < count) && (s[index] == ' ')) index++;
2650 else {
2651 while (s[index] && (s[index] != ' ') && (index < count))
2652 index++;
2653 while ((s[index] == ' ') && (index < count)) index++;
2655 ret = index;
2656 break;
2657 case WB_ISDELIMITER:
2658 ret = (s[index] == ' ');
2659 break;
2660 default:
2661 ERR("unknown action code, please report !\n");
2662 break;
2664 return ret;
2668 /*********************************************************************
2670 * EM_CHARFROMPOS
2672 * returns line number (not index) in high-order word of result.
2673 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2674 * to Richedit, not to the edit control. Original documentation is valid.
2675 * FIXME: do the specs mean to return -1 if outside client area or
2676 * if outside formatting rectangle ???
2679 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2681 POINT pt;
2682 RECT rc;
2683 INT index;
2685 pt.x = x;
2686 pt.y = y;
2687 GetClientRect(es->hwndSelf, &rc);
2688 if (!PtInRect(&rc, pt))
2689 return -1;
2691 index = EDIT_CharFromPos(es, x, y, NULL);
2692 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2696 /*********************************************************************
2698 * EM_FMTLINES
2700 * Enable or disable soft breaks.
2702 * This means: insert or remove the soft linebreak character (\r\r\n).
2703 * Take care to check if the text still fits the buffer after insertion.
2704 * If not, notify with EN_ERRSPACE.
2707 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2709 es->flags &= ~EF_USE_SOFTBRK;
2710 if (add_eol) {
2711 es->flags |= EF_USE_SOFTBRK;
2712 FIXME("soft break enabled, not implemented\n");
2714 return add_eol;
2718 /*********************************************************************
2720 * EM_GETHANDLE
2722 * Hopefully this won't fire back at us.
2723 * We always start with a fixed buffer in the local heap.
2724 * Despite of the documentation says that the local heap is used
2725 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2726 * buffer on the local heap.
2729 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2731 HLOCAL hLocal;
2733 if (!(es->style & ES_MULTILINE))
2734 return 0;
2736 if(es->is_unicode)
2737 hLocal = es->hloc32W;
2738 else
2740 if(!es->hloc32A)
2742 CHAR *textA;
2743 UINT countA, alloc_size;
2744 TRACE("Allocating 32-bit ANSI alias buffer\n");
2745 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2746 alloc_size = ROUND_TO_GROW(countA);
2747 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2749 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2750 return 0;
2752 textA = LocalLock(es->hloc32A);
2753 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2754 LocalUnlock(es->hloc32A);
2756 hLocal = es->hloc32A;
2759 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2760 return hLocal;
2764 /*********************************************************************
2766 * EM_GETHANDLE16
2768 * Hopefully this won't fire back at us.
2769 * We always start with a buffer in 32 bit linear memory.
2770 * However, with this message a 16 bit application requests
2771 * a handle of 16 bit local heap memory, where it expects to find
2772 * the text.
2773 * It's a pitty that from this moment on we have to use this
2774 * local heap, because applications may rely on the handle
2775 * in the future.
2777 * In this function we'll try to switch to local heap.
2779 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2781 CHAR *textA;
2782 UINT countA, alloc_size;
2783 STACK16FRAME* stack16;
2784 HANDLE16 oldDS;
2786 if (!(es->style & ES_MULTILINE))
2787 return 0;
2789 if (es->hloc16)
2790 return es->hloc16;
2792 stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
2793 oldDS = stack16->ds;
2794 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2796 if (!LocalHeapSize16()) {
2798 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2799 ERR("could not initialize local heap\n");
2800 goto done;
2802 TRACE("local heap initialized\n");
2805 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2806 alloc_size = ROUND_TO_GROW(countA);
2808 TRACE("Allocating 16-bit ANSI alias buffer\n");
2809 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2810 ERR("could not allocate new 16 bit buffer\n");
2811 goto done;
2814 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2815 ERR("could not lock new 16 bit buffer\n");
2816 LocalFree16(es->hloc16);
2817 es->hloc16 = 0;
2818 goto done;
2821 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2822 LocalUnlock16(es->hloc16);
2824 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2826 done:
2827 stack16->ds = oldDS;
2828 return es->hloc16;
2832 /*********************************************************************
2834 * EM_GETLINE
2837 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2839 LPWSTR src;
2840 INT line_len, dst_len;
2841 INT i;
2843 if (es->style & ES_MULTILINE) {
2844 if (line >= es->line_count)
2845 return 0;
2846 } else
2847 line = 0;
2848 i = EDIT_EM_LineIndex(es, line);
2849 src = es->text + i;
2850 line_len = EDIT_EM_LineLength(es, i);
2851 dst_len = *(WORD *)dst;
2852 if(unicode)
2854 if(dst_len <= line_len)
2856 memcpy(dst, src, dst_len * sizeof(WCHAR));
2857 return dst_len;
2859 else /* Append 0 if enough space */
2861 memcpy(dst, src, line_len * sizeof(WCHAR));
2862 dst[line_len] = 0;
2863 return line_len;
2866 else
2868 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2869 if(!ret && line_len) /* Insufficient buffer size */
2870 return dst_len;
2871 if(ret < dst_len) /* Append 0 if enough space */
2872 ((LPSTR)dst)[ret] = 0;
2873 return ret;
2878 /*********************************************************************
2880 * EM_GETSEL
2883 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2885 UINT s = es->selection_start;
2886 UINT e = es->selection_end;
2888 ORDER_UINT(s, e);
2889 if (start)
2890 *start = s;
2891 if (end)
2892 *end = e;
2893 return MAKELONG(s, e);
2897 /*********************************************************************
2899 * EM_GETTHUMB
2901 * FIXME: is this right ? (or should it be only VSCROLL)
2902 * (and maybe only for edit controls that really have their
2903 * own scrollbars) (and maybe only for multiline controls ?)
2904 * All in all: very poorly documented
2907 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2909 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2910 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2914 /*********************************************************************
2916 * EM_LINEFROMCHAR
2919 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2921 INT line;
2922 LINEDEF *line_def;
2924 if (!(es->style & ES_MULTILINE))
2925 return 0;
2926 if (index > (INT)strlenW(es->text))
2927 return es->line_count - 1;
2928 if (index == -1)
2929 index = min(es->selection_start, es->selection_end);
2931 line = 0;
2932 line_def = es->first_line_def;
2933 index -= line_def->length;
2934 while ((index >= 0) && line_def->next) {
2935 line++;
2936 line_def = line_def->next;
2937 index -= line_def->length;
2939 return line;
2943 /*********************************************************************
2945 * EM_LINEINDEX
2948 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2950 INT line_index;
2951 LINEDEF *line_def;
2953 if (!(es->style & ES_MULTILINE))
2954 return 0;
2955 if (line >= es->line_count)
2956 return -1;
2958 line_index = 0;
2959 line_def = es->first_line_def;
2960 if (line == -1) {
2961 INT index = es->selection_end - line_def->length;
2962 while ((index >= 0) && line_def->next) {
2963 line_index += line_def->length;
2964 line_def = line_def->next;
2965 index -= line_def->length;
2967 } else {
2968 while (line > 0) {
2969 line_index += line_def->length;
2970 line_def = line_def->next;
2971 line--;
2974 return line_index;
2978 /*********************************************************************
2980 * EM_LINELENGTH
2983 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2985 LINEDEF *line_def;
2987 if (!(es->style & ES_MULTILINE))
2988 return strlenW(es->text);
2990 if (index == -1) {
2991 /* get the number of remaining non-selected chars of selected lines */
2992 INT32 l; /* line number */
2993 INT32 li; /* index of first char in line */
2994 INT32 count;
2995 l = EDIT_EM_LineFromChar(es, es->selection_start);
2996 /* # chars before start of selection area */
2997 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2998 l = EDIT_EM_LineFromChar(es, es->selection_end);
2999 /* # chars after end of selection */
3000 li = EDIT_EM_LineIndex(es, l);
3001 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
3002 return count;
3004 line_def = es->first_line_def;
3005 index -= line_def->length;
3006 while ((index >= 0) && line_def->next) {
3007 line_def = line_def->next;
3008 index -= line_def->length;
3010 return line_def->net_length;
3014 /*********************************************************************
3016 * EM_LINESCROLL
3018 * NOTE: dx is in average character widths, dy - in lines;
3021 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
3023 if (!(es->style & ES_MULTILINE))
3024 return FALSE;
3026 dx *= es->char_width;
3027 return EDIT_EM_LineScroll_internal(es, dx, dy);
3030 /*********************************************************************
3032 * EDIT_EM_LineScroll_internal
3034 * Version of EDIT_EM_LineScroll for internal use.
3035 * It doesn't refuse if ES_MULTILINE is set and assumes that
3036 * dx is in pixels, dy - in lines.
3039 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3041 INT nyoff;
3042 INT x_offset_in_pixels;
3043 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3044 es->line_height;
3046 if (es->style & ES_MULTILINE)
3048 x_offset_in_pixels = es->x_offset;
3050 else
3052 dy = 0;
3053 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3056 if (-dx > x_offset_in_pixels)
3057 dx = -x_offset_in_pixels;
3058 if (dx > es->text_width - x_offset_in_pixels)
3059 dx = es->text_width - x_offset_in_pixels;
3060 nyoff = max(0, es->y_offset + dy);
3061 if (nyoff >= es->line_count - lines_per_page)
3062 nyoff = max(0, es->line_count - lines_per_page);
3063 dy = (es->y_offset - nyoff) * es->line_height;
3064 if (dx || dy) {
3065 RECT rc1;
3066 RECT rc;
3068 es->y_offset = nyoff;
3069 if(es->style & ES_MULTILINE)
3070 es->x_offset += dx;
3071 else
3072 es->x_offset += dx / es->char_width;
3074 GetClientRect(es->hwndSelf, &rc1);
3075 IntersectRect(&rc, &rc1, &es->format_rect);
3076 ScrollWindowEx(es->hwndSelf, -dx, dy,
3077 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3078 /* force scroll info update */
3079 EDIT_UpdateScrollInfo(es);
3081 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3082 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3083 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3084 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3085 return TRUE;
3089 /*********************************************************************
3091 * EM_POSFROMCHAR
3094 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3096 INT len = strlenW(es->text);
3097 INT l;
3098 INT li;
3099 INT x;
3100 INT y = 0;
3101 INT w;
3102 INT lw = 0;
3103 INT ll = 0;
3104 HDC dc;
3105 HFONT old_font = 0;
3106 SIZE size;
3107 LINEDEF *line_def;
3109 index = min(index, len);
3110 dc = GetDC(es->hwndSelf);
3111 if (es->font)
3112 old_font = SelectObject(dc, es->font);
3113 if (es->style & ES_MULTILINE) {
3114 l = EDIT_EM_LineFromChar(es, index);
3115 y = (l - es->y_offset) * es->line_height;
3116 li = EDIT_EM_LineIndex(es, l);
3117 if (after_wrap && (li == index) && l) {
3118 INT l2 = l - 1;
3119 line_def = es->first_line_def;
3120 while (l2) {
3121 line_def = line_def->next;
3122 l2--;
3124 if (line_def->ending == END_WRAP) {
3125 l--;
3126 y -= es->line_height;
3127 li = EDIT_EM_LineIndex(es, l);
3131 line_def = es->first_line_def;
3132 while (line_def->index != li)
3133 line_def = line_def->next;
3135 ll = line_def->net_length;
3136 lw = line_def->width;
3138 w = es->format_rect.right - es->format_rect.left;
3139 if (es->style & ES_RIGHT)
3141 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3142 es->tabs_count, es->tabs)) - es->x_offset;
3143 x = w - x;
3145 else if (es->style & ES_CENTER)
3147 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3148 es->tabs_count, es->tabs)) - es->x_offset;
3149 x += (w - lw) / 2;
3151 else /* ES_LEFT */
3153 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3154 es->tabs_count, es->tabs)) - es->x_offset;
3156 } else {
3157 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3158 if (index < es->x_offset) {
3159 GetTextExtentPoint32W(dc, text + index,
3160 es->x_offset - index, &size);
3161 x = -size.cx;
3162 } else {
3163 GetTextExtentPoint32W(dc, text + es->x_offset,
3164 index - es->x_offset, &size);
3165 x = size.cx;
3167 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3169 w = es->format_rect.right - es->format_rect.left;
3170 if (w > es->text_width)
3172 if (es->style & ES_RIGHT)
3173 x += w - es->text_width;
3174 else if (es->style & ES_CENTER)
3175 x += (w - es->text_width) / 2;
3179 y = 0;
3180 if (es->style & ES_PASSWORD)
3181 HeapFree(GetProcessHeap(), 0, text);
3183 x += es->format_rect.left;
3184 y += es->format_rect.top;
3185 if (es->font)
3186 SelectObject(dc, old_font);
3187 ReleaseDC(es->hwndSelf, dc);
3188 return MAKELONG((INT16)x, (INT16)y);
3192 /*********************************************************************
3194 * EM_REPLACESEL
3196 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3199 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3201 UINT strl = strlenW(lpsz_replace);
3202 UINT tl = strlenW(es->text);
3203 UINT utl;
3204 UINT s;
3205 UINT e;
3206 UINT i;
3207 UINT size;
3208 LPWSTR p;
3209 HRGN hrgn = 0;
3210 LPWSTR buf = NULL;
3211 UINT bufl = 0;
3213 TRACE("%s, can_undo %d, send_update %d\n",
3214 debugstr_w(lpsz_replace), can_undo, send_update);
3216 s = es->selection_start;
3217 e = es->selection_end;
3219 if ((s == e) && !strl)
3220 return;
3222 ORDER_UINT(s, e);
3224 size = tl - (e - s) + strl;
3225 if (!size)
3226 es->text_width = 0;
3228 /* Issue the EN_MAXTEXT notification and continue with replacing text
3229 * such that buffer limit is honored. */
3230 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3231 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3232 strl = es->buffer_limit - (tl - (e-s));
3235 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3236 return;
3238 if (e != s) {
3239 /* there is something to be deleted */
3240 TRACE("deleting stuff.\n");
3241 bufl = e - s;
3242 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3243 if (!buf) return;
3244 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3245 buf[bufl] = 0; /* ensure 0 termination */
3246 /* now delete */
3247 strcpyW(es->text + s, es->text + e);
3249 if (strl) {
3250 /* there is an insertion */
3251 tl = strlenW(es->text);
3252 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));
3253 for (p = es->text + tl ; p >= es->text + s ; p--)
3254 p[strl] = p[0];
3255 for (i = 0 , p = es->text + s ; i < strl ; i++)
3256 p[i] = lpsz_replace[i];
3257 if(es->style & ES_UPPERCASE)
3258 CharUpperBuffW(p, strl);
3259 else if(es->style & ES_LOWERCASE)
3260 CharLowerBuffW(p, strl);
3262 if (es->style & ES_MULTILINE)
3264 INT st = min(es->selection_start, es->selection_end);
3265 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3267 hrgn = CreateRectRgn(0, 0, 0, 0);
3268 EDIT_BuildLineDefs_ML(es, st, st + strl,
3269 strl - abs(es->selection_end - es->selection_start), hrgn);
3270 /* if text is too long undo all changes */
3271 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3272 if (strl)
3273 strcpyW(es->text + e, es->text + e + strl);
3274 if (e != s)
3275 for (i = 0 , p = es->text ; i < e - s ; i++)
3276 p[i + s] = buf[i];
3277 EDIT_BuildLineDefs_ML(es, s, e,
3278 abs(es->selection_end - es->selection_start) - strl, hrgn);
3279 strl = 0;
3280 e = s;
3281 hrgn = CreateRectRgn(0, 0, 0, 0);
3282 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3285 else {
3286 INT fw = es->format_rect.right - es->format_rect.left;
3287 EDIT_CalcLineWidth_SL(es);
3288 /* remove chars that don't fit */
3289 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3290 while ((es->text_width > fw) && s + strl >= s) {
3291 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3292 strl--;
3293 EDIT_CalcLineWidth_SL(es);
3295 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3299 if (e != s) {
3300 if (can_undo) {
3301 utl = strlenW(es->undo_text);
3302 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3303 /* undo-buffer is extended to the right */
3304 EDIT_MakeUndoFit(es, utl + e - s);
3305 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3306 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3307 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3308 /* undo-buffer is extended to the left */
3309 EDIT_MakeUndoFit(es, utl + e - s);
3310 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3311 p[e - s] = p[0];
3312 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3313 p[i] = buf[i];
3314 es->undo_position = s;
3315 } else {
3316 /* new undo-buffer */
3317 EDIT_MakeUndoFit(es, e - s);
3318 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3319 es->undo_text[e - s] = 0; /* ensure 0 termination */
3320 es->undo_position = s;
3322 /* any deletion makes the old insertion-undo invalid */
3323 es->undo_insert_count = 0;
3324 } else
3325 EDIT_EM_EmptyUndoBuffer(es);
3327 if (strl) {
3328 if (can_undo) {
3329 if ((s == es->undo_position) ||
3330 ((es->undo_insert_count) &&
3331 (s == es->undo_position + es->undo_insert_count)))
3333 * insertion is new and at delete position or
3334 * an extension to either left or right
3336 es->undo_insert_count += strl;
3337 else {
3338 /* new insertion undo */
3339 es->undo_position = s;
3340 es->undo_insert_count = strl;
3341 /* new insertion makes old delete-buffer invalid */
3342 *es->undo_text = '\0';
3344 } else
3345 EDIT_EM_EmptyUndoBuffer(es);
3348 if (bufl)
3349 HeapFree(GetProcessHeap(), 0, buf);
3351 s += strl;
3353 /* If text has been deleted and we're right or center aligned then scroll rightward */
3354 if (es->style & (ES_RIGHT | ES_CENTER))
3356 INT delta = strl - abs(es->selection_end - es->selection_start);
3358 if (delta < 0 && es->x_offset)
3360 if (abs(delta) > es->x_offset)
3361 es->x_offset = 0;
3362 else
3363 es->x_offset += delta;
3367 EDIT_EM_SetSel(es, s, s, FALSE);
3368 es->flags |= EF_MODIFIED;
3369 if (send_update) es->flags |= EF_UPDATE;
3370 if (hrgn)
3372 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3373 DeleteObject(hrgn);
3375 else
3376 EDIT_UpdateText(es, NULL, TRUE);
3378 EDIT_EM_ScrollCaret(es);
3380 /* force scroll info update */
3381 EDIT_UpdateScrollInfo(es);
3384 if(send_update || (es->flags & EF_UPDATE))
3386 es->flags &= ~EF_UPDATE;
3387 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3392 /*********************************************************************
3394 * EM_SCROLL
3397 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3399 INT dy;
3401 if (!(es->style & ES_MULTILINE))
3402 return (LRESULT)FALSE;
3404 dy = 0;
3406 switch (action) {
3407 case SB_LINEUP:
3408 if (es->y_offset)
3409 dy = -1;
3410 break;
3411 case SB_LINEDOWN:
3412 if (es->y_offset < es->line_count - 1)
3413 dy = 1;
3414 break;
3415 case SB_PAGEUP:
3416 if (es->y_offset)
3417 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3418 break;
3419 case SB_PAGEDOWN:
3420 if (es->y_offset < es->line_count - 1)
3421 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3422 break;
3423 default:
3424 return (LRESULT)FALSE;
3426 if (dy) {
3427 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3428 /* check if we are going to move too far */
3429 if(es->y_offset + dy > es->line_count - vlc)
3430 dy = es->line_count - vlc - es->y_offset;
3432 /* Notification is done in EDIT_EM_LineScroll */
3433 if(dy)
3434 EDIT_EM_LineScroll(es, 0, dy);
3436 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3440 /*********************************************************************
3442 * EM_SCROLLCARET
3445 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3447 if (es->style & ES_MULTILINE) {
3448 INT l;
3449 INT li;
3450 INT vlc;
3451 INT ww;
3452 INT cw = es->char_width;
3453 INT x;
3454 INT dy = 0;
3455 INT dx = 0;
3457 l = EDIT_EM_LineFromChar(es, es->selection_end);
3458 li = EDIT_EM_LineIndex(es, l);
3459 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3460 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3461 if (l >= es->y_offset + vlc)
3462 dy = l - vlc + 1 - es->y_offset;
3463 if (l < es->y_offset)
3464 dy = l - es->y_offset;
3465 ww = es->format_rect.right - es->format_rect.left;
3466 if (x < es->format_rect.left)
3467 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3468 if (x > es->format_rect.right)
3469 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3470 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3472 /* check if we are going to move too far */
3473 if(es->x_offset + dx + ww > es->text_width)
3474 dx = es->text_width - ww - es->x_offset;
3475 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3476 EDIT_EM_LineScroll_internal(es, dx, dy);
3478 } else {
3479 INT x;
3480 INT goal;
3481 INT format_width;
3483 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3484 format_width = es->format_rect.right - es->format_rect.left;
3485 if (x < es->format_rect.left) {
3486 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3487 do {
3488 es->x_offset--;
3489 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3490 } while ((x < goal) && es->x_offset);
3491 /* FIXME: use ScrollWindow() somehow to improve performance */
3492 EDIT_UpdateText(es, NULL, TRUE);
3493 } else if (x > es->format_rect.right) {
3494 INT x_last;
3495 INT len = strlenW(es->text);
3496 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3497 do {
3498 es->x_offset++;
3499 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3500 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3501 } while ((x > goal) && (x_last > es->format_rect.right));
3502 /* FIXME: use ScrollWindow() somehow to improve performance */
3503 EDIT_UpdateText(es, NULL, TRUE);
3507 if(es->flags & EF_FOCUSED)
3508 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3512 /*********************************************************************
3514 * EM_SETHANDLE
3516 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3519 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3521 if (!(es->style & ES_MULTILINE))
3522 return;
3524 if (!hloc) {
3525 WARN("called with NULL handle\n");
3526 return;
3529 EDIT_UnlockBuffer(es, TRUE);
3531 if(es->hloc16)
3533 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
3534 HANDLE16 oldDS = stack16->ds;
3536 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3537 LocalFree16(es->hloc16);
3538 stack16->ds = oldDS;
3539 es->hloc16 = 0;
3542 if(es->is_unicode)
3544 if(es->hloc32A)
3546 LocalFree(es->hloc32A);
3547 es->hloc32A = NULL;
3549 es->hloc32W = hloc;
3551 else
3553 INT countW, countA;
3554 HLOCAL hloc32W_new;
3555 WCHAR *textW;
3556 CHAR *textA;
3558 countA = LocalSize(hloc);
3559 textA = LocalLock(hloc);
3560 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3561 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3563 ERR("Could not allocate new unicode buffer\n");
3564 return;
3566 textW = LocalLock(hloc32W_new);
3567 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3568 LocalUnlock(hloc32W_new);
3569 LocalUnlock(hloc);
3571 if(es->hloc32W)
3572 LocalFree(es->hloc32W);
3574 es->hloc32W = hloc32W_new;
3575 es->hloc32A = hloc;
3578 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
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, strlenW(es->text), 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((SEGPTR)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 EDIT_LockBuffer(es);
3653 es->x_offset = es->y_offset = 0;
3654 es->selection_start = es->selection_end = 0;
3655 EDIT_EM_EmptyUndoBuffer(es);
3656 es->flags &= ~EF_MODIFIED;
3657 es->flags &= ~EF_UPDATE;
3658 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3659 EDIT_UpdateText(es, NULL, TRUE);
3660 EDIT_EM_ScrollCaret(es);
3661 /* force scroll info update */
3662 EDIT_UpdateScrollInfo(es);
3666 /*********************************************************************
3668 * EM_SETLIMITTEXT
3670 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3671 * However, the windows version is not complied to yet in all of edit.c
3673 * Additionally as the wrapper for RichEdit controls we need larger buffers
3674 * at present -1 will represent nolimit
3676 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3678 if (limit == 0xFFFFFFFF)
3679 es->buffer_limit = -1;
3680 else if (es->style & ES_MULTILINE) {
3681 if (limit)
3682 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3683 else
3684 es->buffer_limit = BUFLIMIT_MULTI;
3685 } else {
3686 if (limit)
3687 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3688 else
3689 es->buffer_limit = BUFLIMIT_SINGLE;
3694 /*********************************************************************
3696 * EM_SETMARGINS
3698 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3699 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3700 * margin according to the textmetrics of the current font.
3702 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3703 * of the char's width as the margin, but this is not how Windows handles this.
3704 * For all other fonts Windows sets the margins to zero.
3706 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3707 * edit control is equal to or larger than a certain size.
3708 * Interestingly if one subtracts both the left and right margins from
3709 * this size one always seems to get an even number. The extents of
3710 * the (four character) string "'**'" match this quite closely, so
3711 * we'll use this until we come up with a better idea.
3713 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3715 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3716 SIZE sz;
3718 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3719 return sz.cx + left + right;
3722 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3723 WORD left, WORD right, BOOL repaint)
3725 TEXTMETRICW tm;
3726 INT default_left_margin = 0; /* in pixels */
3727 INT default_right_margin = 0; /* in pixels */
3729 /* Set the default margins depending on the font */
3730 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3731 HDC dc = GetDC(es->hwndSelf);
3732 HFONT old_font = SelectObject(dc, es->font);
3733 GetTextMetricsW(dc, &tm);
3734 /* The default margins are only non zero for TrueType or Vector fonts */
3735 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3736 int min_size;
3737 RECT rc;
3738 /* This must be calculated more exactly! But how? */
3739 default_left_margin = tm.tmAveCharWidth / 2;
3740 default_right_margin = tm.tmAveCharWidth / 2;
3741 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3742 GetClientRect(es->hwndSelf, &rc);
3743 if(rc.right - rc.left < min_size) {
3744 default_left_margin = es->left_margin;
3745 default_right_margin = es->right_margin;
3748 SelectObject(dc, old_font);
3749 ReleaseDC(es->hwndSelf, dc);
3752 if (action & EC_LEFTMARGIN) {
3753 es->format_rect.left -= es->left_margin;
3754 if (left != EC_USEFONTINFO)
3755 es->left_margin = left;
3756 else
3757 es->left_margin = default_left_margin;
3758 es->format_rect.left += es->left_margin;
3761 if (action & EC_RIGHTMARGIN) {
3762 es->format_rect.right += es->right_margin;
3763 if (right != EC_USEFONTINFO)
3764 es->right_margin = right;
3765 else
3766 es->right_margin = default_right_margin;
3767 es->format_rect.right -= es->right_margin;
3770 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3771 EDIT_AdjustFormatRect(es);
3772 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3775 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3779 /*********************************************************************
3781 * EM_SETPASSWORDCHAR
3784 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3786 LONG style;
3788 if (es->style & ES_MULTILINE)
3789 return;
3791 if (es->password_char == c)
3792 return;
3794 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3795 es->password_char = c;
3796 if (c) {
3797 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3798 es->style |= ES_PASSWORD;
3799 } else {
3800 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3801 es->style &= ~ES_PASSWORD;
3803 EDIT_UpdateText(es, NULL, TRUE);
3807 /*********************************************************************
3809 * EDIT_EM_SetSel
3811 * note: unlike the specs say: the order of start and end
3812 * _is_ preserved in Windows. (i.e. start can be > end)
3813 * In other words: this handler is OK
3816 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3818 UINT old_start = es->selection_start;
3819 UINT old_end = es->selection_end;
3820 UINT len = strlenW(es->text);
3822 if (start == (UINT)-1) {
3823 start = es->selection_end;
3824 end = es->selection_end;
3825 } else {
3826 start = min(start, len);
3827 end = min(end, len);
3829 es->selection_start = start;
3830 es->selection_end = end;
3831 if (after_wrap)
3832 es->flags |= EF_AFTER_WRAP;
3833 else
3834 es->flags &= ~EF_AFTER_WRAP;
3835 /* Compute the necessary invalidation region. */
3836 /* Note that we don't need to invalidate regions which have
3837 * "never" been selected, or those which are "still" selected.
3838 * In fact, every time we hit a selection boundary, we can
3839 * *toggle* whether we need to invalidate. Thus we can optimize by
3840 * *sorting* the interval endpoints. Let's assume that we sort them
3841 * in this order:
3842 * start <= end <= old_start <= old_end
3843 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3844 * in 5 comparisons; ie it's impossible to do better than the
3845 * following: */
3846 ORDER_UINT(end, old_end);
3847 ORDER_UINT(start, old_start);
3848 ORDER_UINT(old_start, old_end);
3849 ORDER_UINT(start, end);
3850 /* Note that at this point 'end' and 'old_start' are not in order, but
3851 * start is definitely the min. and old_end is definitely the max. */
3852 if (end != old_start)
3855 * One can also do
3856 * ORDER_UINT32(end, old_start);
3857 * EDIT_InvalidateText(es, start, end);
3858 * EDIT_InvalidateText(es, old_start, old_end);
3859 * in place of the following if statement.
3860 * (That would complete the optimal five-comparison four-element sort.)
3862 if (old_start > end )
3864 EDIT_InvalidateText(es, start, end);
3865 EDIT_InvalidateText(es, old_start, old_end);
3867 else
3869 EDIT_InvalidateText(es, start, old_start);
3870 EDIT_InvalidateText(es, end, old_end);
3873 else EDIT_InvalidateText(es, start, old_end);
3877 /*********************************************************************
3879 * EM_SETTABSTOPS
3882 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3884 if (!(es->style & ES_MULTILINE))
3885 return FALSE;
3886 HeapFree(GetProcessHeap(), 0, es->tabs);
3887 es->tabs_count = count;
3888 if (!count)
3889 es->tabs = NULL;
3890 else {
3891 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3892 memcpy(es->tabs, tabs, count * sizeof(INT));
3894 return TRUE;
3898 /*********************************************************************
3900 * EM_SETTABSTOPS16
3903 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3905 if (!(es->style & ES_MULTILINE))
3906 return FALSE;
3907 HeapFree(GetProcessHeap(), 0, es->tabs);
3908 es->tabs_count = count;
3909 if (!count)
3910 es->tabs = NULL;
3911 else {
3912 INT i;
3913 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3914 for (i = 0 ; i < count ; i++)
3915 es->tabs[i] = *tabs++;
3917 return TRUE;
3921 /*********************************************************************
3923 * EM_SETWORDBREAKPROC
3926 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3928 if (es->word_break_proc == wbp)
3929 return;
3931 es->word_break_proc = wbp;
3932 es->word_break_proc16 = NULL;
3934 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3935 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3936 EDIT_UpdateText(es, NULL, TRUE);
3941 /*********************************************************************
3943 * EM_SETWORDBREAKPROC16
3946 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3948 if (es->word_break_proc16 == wbp)
3949 return;
3951 es->word_break_proc = NULL;
3952 es->word_break_proc16 = wbp;
3953 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3954 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3955 EDIT_UpdateText(es, NULL, TRUE);
3960 /*********************************************************************
3962 * EM_UNDO / WM_UNDO
3965 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3967 INT ulength;
3968 LPWSTR utext;
3970 /* As per MSDN spec, for a single-line edit control,
3971 the return value is always TRUE */
3972 if( es->style & ES_READONLY )
3973 return !(es->style & ES_MULTILINE);
3975 ulength = strlenW(es->undo_text);
3977 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3979 strcpyW(utext, es->undo_text);
3981 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3982 es->undo_insert_count, debugstr_w(utext));
3984 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3985 EDIT_EM_EmptyUndoBuffer(es);
3986 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3987 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3988 /* send the notification after the selection start and end are set */
3989 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3990 EDIT_EM_ScrollCaret(es);
3991 HeapFree(GetProcessHeap(), 0, utext);
3993 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3994 es->undo_insert_count, debugstr_w(es->undo_text));
3995 return TRUE;
3999 /*********************************************************************
4001 * WM_CHAR
4004 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
4006 BOOL control;
4008 control = GetKeyState(VK_CONTROL) & 0x8000;
4010 switch (c) {
4011 case '\r':
4012 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
4013 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
4014 break;
4015 case '\n':
4016 if (es->style & ES_MULTILINE) {
4017 if (es->style & ES_READONLY) {
4018 EDIT_MoveHome(es, FALSE);
4019 EDIT_MoveDown_ML(es, FALSE);
4020 } else {
4021 static const WCHAR cr_lfW[] = {'\r','\n',0};
4022 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
4025 break;
4026 case '\t':
4027 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4029 static const WCHAR tabW[] = {'\t',0};
4030 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4032 break;
4033 case VK_BACK:
4034 if (!(es->style & ES_READONLY) && !control) {
4035 if (es->selection_start != es->selection_end)
4036 EDIT_WM_Clear(es);
4037 else {
4038 /* delete character left of caret */
4039 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4040 EDIT_MoveBackward(es, TRUE);
4041 EDIT_WM_Clear(es);
4044 break;
4045 case 0x03: /* ^C */
4046 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4047 break;
4048 case 0x16: /* ^V */
4049 if (!(es->style & ES_READONLY))
4050 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4051 break;
4052 case 0x18: /* ^X */
4053 if (!(es->style & ES_READONLY))
4054 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4055 break;
4057 default:
4058 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4059 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4060 break;
4062 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4063 WCHAR str[2];
4064 str[0] = c;
4065 str[1] = '\0';
4066 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4068 break;
4073 /*********************************************************************
4075 * WM_COMMAND
4078 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4080 if (code || control)
4081 return;
4083 switch (id) {
4084 case EM_UNDO:
4085 EDIT_EM_Undo(es);
4086 break;
4087 case WM_CUT:
4088 EDIT_WM_Cut(es);
4089 break;
4090 case WM_COPY:
4091 EDIT_WM_Copy(es);
4092 break;
4093 case WM_PASTE:
4094 EDIT_WM_Paste(es);
4095 break;
4096 case WM_CLEAR:
4097 EDIT_WM_Clear(es);
4098 break;
4099 case EM_SETSEL:
4100 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4101 EDIT_EM_ScrollCaret(es);
4102 break;
4103 default:
4104 ERR("unknown menu item, please report\n");
4105 break;
4110 /*********************************************************************
4112 * WM_CONTEXTMENU
4114 * Note: the resource files resource/sysres_??.rc cannot define a
4115 * single popup menu. Hence we use a (dummy) menubar
4116 * containing the single popup menu as its first item.
4118 * FIXME: the message identifiers have been chosen arbitrarily,
4119 * hence we use MF_BYPOSITION.
4120 * We might as well use the "real" values (anybody knows ?)
4121 * The menu definition is in resources/sysres_??.rc.
4122 * Once these are OK, we better use MF_BYCOMMAND here
4123 * (as we do in EDIT_WM_Command()).
4126 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4128 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4129 HMENU popup = GetSubMenu(menu, 0);
4130 UINT start = es->selection_start;
4131 UINT end = es->selection_end;
4133 ORDER_UINT(start, end);
4135 /* undo */
4136 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4137 /* cut */
4138 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4139 /* copy */
4140 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4141 /* paste */
4142 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4143 /* delete */
4144 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4145 /* select all */
4146 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
4148 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4150 RECT rc;
4151 /* Windows places the menu at the edit's center in this case */
4152 GetClientRect(es->hwndSelf, &rc);
4153 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4154 x = rc.left + (rc.right - rc.left) / 2;
4155 y = rc.top + (rc.bottom - rc.top) / 2;
4158 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4159 DestroyMenu(menu);
4163 /*********************************************************************
4165 * WM_COPY
4168 static void EDIT_WM_Copy(EDITSTATE *es)
4170 INT s = min(es->selection_start, es->selection_end);
4171 INT e = max(es->selection_start, es->selection_end);
4172 HGLOBAL hdst;
4173 LPWSTR dst;
4174 DWORD len;
4176 if (e == s) return;
4178 len = e - s;
4179 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4180 dst = GlobalLock(hdst);
4181 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4182 dst[len] = 0; /* ensure 0 termination */
4183 TRACE("%s\n", debugstr_w(dst));
4184 GlobalUnlock(hdst);
4185 OpenClipboard(es->hwndSelf);
4186 EmptyClipboard();
4187 SetClipboardData(CF_UNICODETEXT, hdst);
4188 CloseClipboard();
4192 /*********************************************************************
4194 * WM_CREATE
4197 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4199 RECT clientRect;
4201 TRACE("%s\n", debugstr_w(name));
4203 * To initialize some final structure members, we call some helper
4204 * functions. However, since the EDITSTATE is not consistent (i.e.
4205 * not fully initialized), we should be very careful which
4206 * functions can be called, and in what order.
4208 EDIT_WM_SetFont(es, 0, FALSE);
4209 EDIT_EM_EmptyUndoBuffer(es);
4211 /* We need to calculate the format rect
4212 (applications may send EM_SETMARGINS before the control gets visible) */
4213 GetClientRect(es->hwndSelf, &clientRect);
4214 EDIT_SetRectNP(es, &clientRect);
4216 if (name && *name) {
4217 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4218 /* if we insert text to the editline, the text scrolls out
4219 * of the window, as the caret is placed after the insert
4220 * pos normally; thus we reset es->selection... to 0 and
4221 * update caret
4223 es->selection_start = es->selection_end = 0;
4224 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4225 * Messages are only to be sent when the USER does something to
4226 * change the contents. So I am removing this EN_CHANGE
4228 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4230 EDIT_EM_ScrollCaret(es);
4232 /* force scroll info update */
4233 EDIT_UpdateScrollInfo(es);
4234 /* The rule seems to return 1 here for success */
4235 /* Power Builder masked edit controls will crash */
4236 /* if not. */
4237 /* FIXME: is that in all cases so ? */
4238 return 1;
4242 /*********************************************************************
4244 * WM_DESTROY
4247 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4249 LINEDEF *pc, *pp;
4251 if (es->hloc32W) {
4252 while (LocalUnlock(es->hloc32W)) ;
4253 LocalFree(es->hloc32W);
4255 if (es->hloc32A) {
4256 while (LocalUnlock(es->hloc32A)) ;
4257 LocalFree(es->hloc32A);
4259 if (es->hloc16) {
4260 STACK16FRAME* stack16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
4261 HANDLE16 oldDS = stack16->ds;
4263 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4264 while (LocalUnlock16(es->hloc16)) ;
4265 LocalFree16(es->hloc16);
4266 stack16->ds = oldDS;
4269 pc = es->first_line_def;
4270 while (pc)
4272 pp = pc->next;
4273 HeapFree(GetProcessHeap(), 0, pc);
4274 pc = pp;
4277 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4278 HeapFree(GetProcessHeap(), 0, es);
4280 return 0;
4284 /*********************************************************************
4286 * WM_ERASEBKGND
4289 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4291 /* we do the proper erase in EDIT_WM_Paint */
4292 return 1;
4296 /*********************************************************************
4298 * WM_GETTEXT
4301 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4303 WCHAR text[MAX_PATH];
4304 LPWSTR lptext = es->text;
4306 if(!count) return 0;
4308 if (USER32_DisplayUnixPaths(es))
4310 INT len = GetShortPathNameW(es->text, text, MAX_PATH);
4311 if (len && len < MAX_PATH)
4312 lptext = text;
4315 if (unicode)
4317 lstrcpynW(dst, es->text, count);
4318 return strlenW(dst);
4320 else
4322 LPSTR textA = (LPSTR)dst;
4323 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4324 textA[count - 1] = 0; /* ensure 0 termination */
4325 return strlen(textA);
4329 /*********************************************************************
4331 * WM_HSCROLL
4334 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4336 INT dx;
4337 INT fw;
4339 if (!(es->style & ES_MULTILINE))
4340 return 0;
4342 if (!(es->style & ES_AUTOHSCROLL))
4343 return 0;
4345 dx = 0;
4346 fw = es->format_rect.right - es->format_rect.left;
4347 switch (action) {
4348 case SB_LINELEFT:
4349 TRACE("SB_LINELEFT\n");
4350 if (es->x_offset)
4351 dx = -es->char_width;
4352 break;
4353 case SB_LINERIGHT:
4354 TRACE("SB_LINERIGHT\n");
4355 if (es->x_offset < es->text_width)
4356 dx = es->char_width;
4357 break;
4358 case SB_PAGELEFT:
4359 TRACE("SB_PAGELEFT\n");
4360 if (es->x_offset)
4361 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4362 break;
4363 case SB_PAGERIGHT:
4364 TRACE("SB_PAGERIGHT\n");
4365 if (es->x_offset < es->text_width)
4366 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4367 break;
4368 case SB_LEFT:
4369 TRACE("SB_LEFT\n");
4370 if (es->x_offset)
4371 dx = -es->x_offset;
4372 break;
4373 case SB_RIGHT:
4374 TRACE("SB_RIGHT\n");
4375 if (es->x_offset < es->text_width)
4376 dx = es->text_width - es->x_offset;
4377 break;
4378 case SB_THUMBTRACK:
4379 TRACE("SB_THUMBTRACK %d\n", pos);
4380 es->flags |= EF_HSCROLL_TRACK;
4381 if(es->style & WS_HSCROLL)
4382 dx = pos - es->x_offset;
4383 else
4385 INT fw, new_x;
4386 /* Sanity check */
4387 if(pos < 0 || pos > 100) return 0;
4388 /* Assume default scroll range 0-100 */
4389 fw = es->format_rect.right - es->format_rect.left;
4390 new_x = pos * (es->text_width - fw) / 100;
4391 dx = es->text_width ? (new_x - es->x_offset) : 0;
4393 break;
4394 case SB_THUMBPOSITION:
4395 TRACE("SB_THUMBPOSITION %d\n", pos);
4396 es->flags &= ~EF_HSCROLL_TRACK;
4397 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4398 dx = pos - es->x_offset;
4399 else
4401 INT fw, new_x;
4402 /* Sanity check */
4403 if(pos < 0 || pos > 100) return 0;
4404 /* Assume default scroll range 0-100 */
4405 fw = es->format_rect.right - es->format_rect.left;
4406 new_x = pos * (es->text_width - fw) / 100;
4407 dx = es->text_width ? (new_x - es->x_offset) : 0;
4409 if (!dx) {
4410 /* force scroll info update */
4411 EDIT_UpdateScrollInfo(es);
4412 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4414 break;
4415 case SB_ENDSCROLL:
4416 TRACE("SB_ENDSCROLL\n");
4417 break;
4419 * FIXME : the next two are undocumented !
4420 * Are we doing the right thing ?
4421 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4422 * although it's also a regular control message.
4424 case EM_GETTHUMB: /* this one is used by NT notepad */
4425 case EM_GETTHUMB16:
4427 LRESULT ret;
4428 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4429 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4430 else
4432 /* Assume default scroll range 0-100 */
4433 INT fw = es->format_rect.right - es->format_rect.left;
4434 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4436 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4437 return ret;
4439 case EM_LINESCROLL16:
4440 TRACE("EM_LINESCROLL16\n");
4441 dx = pos;
4442 break;
4444 default:
4445 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4446 action, action);
4447 return 0;
4449 if (dx)
4451 INT fw = es->format_rect.right - es->format_rect.left;
4452 /* check if we are going to move too far */
4453 if(es->x_offset + dx + fw > es->text_width)
4454 dx = es->text_width - fw - es->x_offset;
4455 if(dx)
4456 EDIT_EM_LineScroll_internal(es, dx, 0);
4458 return 0;
4462 /*********************************************************************
4464 * EDIT_CheckCombo
4467 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4469 HWND hLBox = es->hwndListBox;
4470 HWND hCombo;
4471 BOOL bDropped;
4472 int nEUI;
4474 if (!hLBox)
4475 return FALSE;
4477 hCombo = GetParent(es->hwndSelf);
4478 bDropped = TRUE;
4479 nEUI = 0;
4481 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4483 if (key == VK_UP || key == VK_DOWN)
4485 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4486 nEUI = 1;
4488 if (msg == WM_KEYDOWN || nEUI)
4489 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4492 switch (msg)
4494 case WM_KEYDOWN:
4495 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4497 /* make sure ComboLBox pops up */
4498 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4499 key = VK_F4;
4500 nEUI = 2;
4503 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4504 break;
4506 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4507 if (nEUI)
4508 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4509 else
4510 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4511 break;
4514 if(nEUI == 2)
4515 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4517 return TRUE;
4521 /*********************************************************************
4523 * WM_KEYDOWN
4525 * Handling of special keys that don't produce a WM_CHAR
4526 * (i.e. non-printable keys) & Backspace & Delete
4529 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4531 BOOL shift;
4532 BOOL control;
4534 if (GetKeyState(VK_MENU) & 0x8000)
4535 return 0;
4537 shift = GetKeyState(VK_SHIFT) & 0x8000;
4538 control = GetKeyState(VK_CONTROL) & 0x8000;
4540 switch (key) {
4541 case VK_F4:
4542 case VK_UP:
4543 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4544 break;
4546 /* fall through */
4547 case VK_LEFT:
4548 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4549 EDIT_MoveUp_ML(es, shift);
4550 else
4551 if (control)
4552 EDIT_MoveWordBackward(es, shift);
4553 else
4554 EDIT_MoveBackward(es, shift);
4555 break;
4556 case VK_DOWN:
4557 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4558 break;
4559 /* fall through */
4560 case VK_RIGHT:
4561 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4562 EDIT_MoveDown_ML(es, shift);
4563 else if (control)
4564 EDIT_MoveWordForward(es, shift);
4565 else
4566 EDIT_MoveForward(es, shift);
4567 break;
4568 case VK_HOME:
4569 EDIT_MoveHome(es, shift);
4570 break;
4571 case VK_END:
4572 EDIT_MoveEnd(es, shift);
4573 break;
4574 case VK_PRIOR:
4575 if (es->style & ES_MULTILINE)
4576 EDIT_MovePageUp_ML(es, shift);
4577 else
4578 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4579 break;
4580 case VK_NEXT:
4581 if (es->style & ES_MULTILINE)
4582 EDIT_MovePageDown_ML(es, shift);
4583 else
4584 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4585 break;
4586 case VK_DELETE:
4587 if (!(es->style & ES_READONLY) && !(shift && control)) {
4588 if (es->selection_start != es->selection_end) {
4589 if (shift)
4590 EDIT_WM_Cut(es);
4591 else
4592 EDIT_WM_Clear(es);
4593 } else {
4594 if (shift) {
4595 /* delete character left of caret */
4596 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4597 EDIT_MoveBackward(es, TRUE);
4598 EDIT_WM_Clear(es);
4599 } else if (control) {
4600 /* delete to end of line */
4601 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4602 EDIT_MoveEnd(es, TRUE);
4603 EDIT_WM_Clear(es);
4604 } else {
4605 /* delete character right of caret */
4606 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4607 EDIT_MoveForward(es, TRUE);
4608 EDIT_WM_Clear(es);
4612 break;
4613 case VK_INSERT:
4614 if (shift) {
4615 if (!(es->style & ES_READONLY))
4616 EDIT_WM_Paste(es);
4617 } else if (control)
4618 EDIT_WM_Copy(es);
4619 break;
4620 case VK_RETURN:
4621 /* If the edit doesn't want the return send a message to the default object */
4622 if(!(es->style & ES_WANTRETURN))
4624 HWND hwndParent = GetParent(es->hwndSelf);
4625 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4626 if (HIWORD(dw) == DC_HASDEFID)
4628 SendMessageW( hwndParent, WM_COMMAND,
4629 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4630 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4633 break;
4635 return 0;
4639 /*********************************************************************
4641 * WM_KILLFOCUS
4644 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4646 es->flags &= ~EF_FOCUSED;
4647 DestroyCaret();
4648 if(!(es->style & ES_NOHIDESEL))
4649 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4650 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4651 return 0;
4655 /*********************************************************************
4657 * WM_LBUTTONDBLCLK
4659 * The caret position has been set on the WM_LBUTTONDOWN message
4662 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4664 INT s;
4665 INT e = es->selection_end;
4666 INT l;
4667 INT li;
4668 INT ll;
4670 es->bCaptureState = TRUE;
4671 SetCapture(es->hwndSelf);
4673 l = EDIT_EM_LineFromChar(es, e);
4674 li = EDIT_EM_LineIndex(es, l);
4675 ll = EDIT_EM_LineLength(es, e);
4676 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4677 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4678 EDIT_EM_SetSel(es, s, e, FALSE);
4679 EDIT_EM_ScrollCaret(es);
4680 es->region_posx = es->region_posy = 0;
4681 SetTimer(es->hwndSelf, 0, 100, NULL);
4682 return 0;
4686 /*********************************************************************
4688 * WM_LBUTTONDOWN
4691 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4693 INT e;
4694 BOOL after_wrap;
4696 es->bCaptureState = TRUE;
4697 SetCapture(es->hwndSelf);
4698 EDIT_ConfinePoint(es, &x, &y);
4699 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4700 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4701 EDIT_EM_ScrollCaret(es);
4702 es->region_posx = es->region_posy = 0;
4703 SetTimer(es->hwndSelf, 0, 100, NULL);
4705 if (!(es->flags & EF_FOCUSED))
4706 SetFocus(es->hwndSelf);
4708 return 0;
4712 /*********************************************************************
4714 * WM_LBUTTONUP
4717 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4719 if (es->bCaptureState) {
4720 KillTimer(es->hwndSelf, 0);
4721 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4723 es->bCaptureState = FALSE;
4724 return 0;
4728 /*********************************************************************
4730 * WM_MBUTTONDOWN
4733 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4735 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4736 return 0;
4740 /*********************************************************************
4742 * WM_MOUSEMOVE
4745 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4747 INT e;
4748 BOOL after_wrap;
4749 INT prex, prey;
4751 /* If the mouse has been captured by process other than the edit control itself,
4752 * the windows edit controls will not select the strings with mouse move.
4754 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4755 return 0;
4758 * FIXME: gotta do some scrolling if outside client
4759 * area. Maybe reset the timer ?
4761 prex = x; prey = y;
4762 EDIT_ConfinePoint(es, &x, &y);
4763 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4764 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4765 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4766 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4767 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4768 return 0;
4772 /*********************************************************************
4774 * WM_NCCREATE
4776 * See also EDIT_WM_StyleChanged
4778 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4780 EDITSTATE *es;
4781 UINT alloc_size;
4783 TRACE("Creating %s edit control, style = %08x\n",
4784 unicode ? "Unicode" : "ANSI", lpcs->style);
4786 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4787 return FALSE;
4788 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4791 * Note: since the EDITSTATE has not been fully initialized yet,
4792 * we can't use any API calls that may send
4793 * WM_XXX messages before WM_NCCREATE is completed.
4796 es->is_unicode = unicode;
4797 es->style = lpcs->style;
4799 es->bEnableState = !(es->style & WS_DISABLED);
4801 es->hwndSelf = hwnd;
4802 /* Save parent, which will be notified by EN_* messages */
4803 es->hwndParent = lpcs->hwndParent;
4805 if (es->style & ES_COMBO)
4806 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4808 /* Number overrides lowercase overrides uppercase (at least it
4809 * does in Win95). However I'll bet that ES_NUMBER would be
4810 * invalid under Win 3.1.
4812 if (es->style & ES_NUMBER) {
4813 ; /* do not override the ES_NUMBER */
4814 } else if (es->style & ES_LOWERCASE) {
4815 es->style &= ~ES_UPPERCASE;
4817 if (es->style & ES_MULTILINE) {
4818 es->buffer_limit = BUFLIMIT_MULTI;
4819 if (es->style & WS_VSCROLL)
4820 es->style |= ES_AUTOVSCROLL;
4821 if (es->style & WS_HSCROLL)
4822 es->style |= ES_AUTOHSCROLL;
4823 es->style &= ~ES_PASSWORD;
4824 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4825 /* Confirmed - RIGHT overrides CENTER */
4826 if (es->style & ES_RIGHT)
4827 es->style &= ~ES_CENTER;
4828 es->style &= ~WS_HSCROLL;
4829 es->style &= ~ES_AUTOHSCROLL;
4831 } else {
4832 es->buffer_limit = BUFLIMIT_SINGLE;
4833 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4834 es->style &= ~ES_CENTER;
4835 es->style &= ~WS_HSCROLL;
4836 es->style &= ~WS_VSCROLL;
4837 if (es->style & ES_PASSWORD)
4838 es->password_char = '*';
4841 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4842 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4843 return FALSE;
4844 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4846 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4847 return FALSE;
4848 es->undo_buffer_size = es->buffer_size;
4850 if (es->style & ES_MULTILINE)
4851 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4852 return FALSE;
4853 es->line_count = 1;
4856 * In Win95 look and feel, the WS_BORDER style is replaced by the
4857 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4858 * control a nonclient area so we don't need to draw the border.
4859 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4860 * a nonclient area and we should handle painting the border ourselves.
4862 * When making modifications please ensure that the code still works
4863 * for edit controls created directly with style 0x50800000, exStyle 0
4864 * (which should have a single pixel border)
4866 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4867 es->style &= ~WS_BORDER;
4868 else if (es->style & WS_BORDER)
4869 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4871 return TRUE;
4874 /*********************************************************************
4876 * WM_PAINT
4879 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4881 PAINTSTRUCT ps;
4882 INT i;
4883 HDC dc;
4884 HFONT old_font = 0;
4885 RECT rc;
4886 RECT rcClient;
4887 RECT rcLine;
4888 RECT rcRgn;
4889 HBRUSH brush;
4890 HBRUSH old_brush;
4891 INT bw, bh;
4892 BOOL rev = es->bEnableState &&
4893 ((es->flags & EF_FOCUSED) ||
4894 (es->style & ES_NOHIDESEL));
4895 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4897 GetClientRect(es->hwndSelf, &rcClient);
4899 /* get the background brush */
4900 brush = EDIT_NotifyCtlColor(es, dc);
4902 /* paint the border and the background */
4903 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4905 if(es->style & WS_BORDER) {
4906 bw = GetSystemMetrics(SM_CXBORDER);
4907 bh = GetSystemMetrics(SM_CYBORDER);
4908 rc = rcClient;
4909 if(es->style & ES_MULTILINE) {
4910 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4911 if(es->style & WS_VSCROLL) rc.right+=bw;
4914 /* Draw the frame. Same code as in nonclient.c */
4915 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4916 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4917 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4918 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4919 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4920 SelectObject(dc, old_brush);
4922 /* Keep the border clean */
4923 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4924 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4927 GetClipBox(dc, &rc);
4928 FillRect(dc, &rc, brush);
4930 IntersectClipRect(dc, es->format_rect.left,
4931 es->format_rect.top,
4932 es->format_rect.right,
4933 es->format_rect.bottom);
4934 if (es->style & ES_MULTILINE) {
4935 rc = rcClient;
4936 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4938 if (es->font)
4939 old_font = SelectObject(dc, es->font);
4941 if (!es->bEnableState)
4942 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4943 GetClipBox(dc, &rcRgn);
4944 if (es->style & ES_MULTILINE) {
4945 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4946 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4947 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4948 if (IntersectRect(&rc, &rcRgn, &rcLine))
4949 EDIT_PaintLine(es, dc, i, rev);
4951 } else {
4952 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4953 if (IntersectRect(&rc, &rcRgn, &rcLine))
4954 EDIT_PaintLine(es, dc, 0, rev);
4956 if (es->font)
4957 SelectObject(dc, old_font);
4959 if (!hdc)
4960 EndPaint(es->hwndSelf, &ps);
4964 /*********************************************************************
4966 * WM_PASTE
4969 static void EDIT_WM_Paste(EDITSTATE *es)
4971 HGLOBAL hsrc;
4972 LPWSTR src;
4974 /* Protect read-only edit control from modification */
4975 if(es->style & ES_READONLY)
4976 return;
4978 OpenClipboard(es->hwndSelf);
4979 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4980 src = (LPWSTR)GlobalLock(hsrc);
4981 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4982 GlobalUnlock(hsrc);
4984 CloseClipboard();
4988 /*********************************************************************
4990 * WM_SETFOCUS
4993 static void EDIT_WM_SetFocus(EDITSTATE *es)
4995 es->flags |= EF_FOCUSED;
4997 if (!(es->style & ES_NOHIDESEL))
4998 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
5000 /* single line edit updates itself */
5001 if (!(es->style & ES_MULTILINE))
5003 HDC hdc = GetDC(es->hwndSelf);
5004 EDIT_WM_Paint(es, hdc);
5005 ReleaseDC(es->hwndSelf, hdc);
5008 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5009 EDIT_SetCaretPos(es, es->selection_end,
5010 es->flags & EF_AFTER_WRAP);
5011 ShowCaret(es->hwndSelf);
5012 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
5016 /*********************************************************************
5018 * WM_SETFONT
5020 * With Win95 look the margins are set to default font value unless
5021 * the system font (font == 0) is being set, in which case they are left
5022 * unchanged.
5025 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
5027 TEXTMETRICW tm;
5028 HDC dc;
5029 HFONT old_font = 0;
5030 RECT clientRect;
5032 es->font = font;
5033 dc = GetDC(es->hwndSelf);
5034 if (font)
5035 old_font = SelectObject(dc, font);
5036 GetTextMetricsW(dc, &tm);
5037 es->line_height = tm.tmHeight;
5038 es->char_width = tm.tmAveCharWidth;
5039 if (font)
5040 SelectObject(dc, old_font);
5041 ReleaseDC(es->hwndSelf, dc);
5043 /* Reset the format rect and the margins */
5044 GetClientRect(es->hwndSelf, &clientRect);
5045 EDIT_SetRectNP(es, &clientRect);
5046 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5047 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5049 if (es->style & ES_MULTILINE)
5050 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
5051 else
5052 EDIT_CalcLineWidth_SL(es);
5054 if (redraw)
5055 EDIT_UpdateText(es, NULL, TRUE);
5056 if (es->flags & EF_FOCUSED) {
5057 DestroyCaret();
5058 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5059 EDIT_SetCaretPos(es, es->selection_end,
5060 es->flags & EF_AFTER_WRAP);
5061 ShowCaret(es->hwndSelf);
5066 /*********************************************************************
5068 * WM_SETTEXT
5070 * NOTES
5071 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5072 * The modified flag is reset. No notifications are sent.
5074 * For single-line controls, reception of WM_SETTEXT triggers:
5075 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5078 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5080 INT countW;
5081 LPWSTR textW = NULL;
5083 if (!unicode && text)
5085 LPCSTR textA = (LPCSTR)text;
5086 countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5087 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5088 if (textW)
5089 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5090 text = textW;
5093 if (es->flags & EF_UPDATE)
5094 /* fixed this bug once; complain if we see it about to happen again. */
5095 ERR("SetSel may generate UPDATE message whose handler may reset "
5096 "selection.\n");
5098 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5099 if (text)
5101 if (USER32_DisplayUnixPaths(es))
5103 char *textA = wine_get_unix_file_name( text );
5105 if (!unicode)
5106 HeapFree(GetProcessHeap(), 0, textW);
5108 countW = MultiByteToWideChar(CP_UNIXCP, 0, textA, -1, NULL, 0);
5109 if ((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5111 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5112 unicode = FALSE; /* Force text to be freed */
5113 text = textW;
5115 HeapFree( GetProcessHeap(), 0, textA );
5118 TRACE("%s\n", debugstr_w(text));
5119 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5120 if(!unicode)
5121 HeapFree(GetProcessHeap(), 0, textW);
5123 else
5125 static const WCHAR empty_stringW[] = {0};
5126 TRACE("<NULL>\n");
5127 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5129 es->x_offset = 0;
5130 es->flags &= ~EF_MODIFIED;
5131 EDIT_EM_SetSel(es, 0, 0, FALSE);
5133 /* Send the notification after the selection start and end have been set
5134 * edit control doesn't send notification on WM_SETTEXT
5135 * if it is multiline, or it is part of combobox
5137 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5139 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5140 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5142 EDIT_EM_ScrollCaret(es);
5143 EDIT_UpdateScrollInfo(es);
5147 /*********************************************************************
5149 * WM_SIZE
5152 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5154 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5155 RECT rc;
5156 TRACE("width = %d, height = %d\n", width, height);
5157 SetRect(&rc, 0, 0, width, height);
5158 EDIT_SetRectNP(es, &rc);
5159 EDIT_UpdateText(es, NULL, TRUE);
5164 /*********************************************************************
5166 * WM_STYLECHANGED
5168 * This message is sent by SetWindowLong on having changed either the Style
5169 * or the extended style.
5171 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5173 * See also EDIT_WM_NCCreate
5175 * It appears that the Windows version of the edit control allows the style
5176 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5177 * style variable which will generally be different. In this function we
5178 * update the internal style based on what changed in the externally visible
5179 * style.
5181 * Much of this content as based upon the MSDN, especially:
5182 * Platform SDK Documentation -> User Interface Services ->
5183 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5184 * Edit Control Styles
5186 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5188 if (GWL_STYLE == which) {
5189 DWORD style_change_mask;
5190 DWORD new_style;
5191 /* Only a subset of changes can be applied after the control
5192 * has been created.
5194 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5195 ES_NUMBER;
5196 if (es->style & ES_MULTILINE)
5197 style_change_mask |= ES_WANTRETURN;
5199 new_style = style->styleNew & style_change_mask;
5201 /* Number overrides lowercase overrides uppercase (at least it
5202 * does in Win95). However I'll bet that ES_NUMBER would be
5203 * invalid under Win 3.1.
5205 if (new_style & ES_NUMBER) {
5206 ; /* do not override the ES_NUMBER */
5207 } else if (new_style & ES_LOWERCASE) {
5208 new_style &= ~ES_UPPERCASE;
5211 es->style = (es->style & ~style_change_mask) | new_style;
5212 } else if (GWL_EXSTYLE == which) {
5213 ; /* FIXME - what is needed here */
5214 } else {
5215 WARN ("Invalid style change %d\n",which);
5218 return 0;
5221 /*********************************************************************
5223 * WM_SYSKEYDOWN
5226 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5228 if ((key == VK_BACK) && (key_data & 0x2000)) {
5229 if (EDIT_EM_CanUndo(es))
5230 EDIT_EM_Undo(es);
5231 return 0;
5232 } else if (key == VK_UP || key == VK_DOWN) {
5233 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5234 return 0;
5236 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5240 /*********************************************************************
5242 * WM_TIMER
5245 static void EDIT_WM_Timer(EDITSTATE *es)
5247 if (es->region_posx < 0) {
5248 EDIT_MoveBackward(es, TRUE);
5249 } else if (es->region_posx > 0) {
5250 EDIT_MoveForward(es, TRUE);
5253 * FIXME: gotta do some vertical scrolling here, like
5254 * EDIT_EM_LineScroll(hwnd, 0, 1);
5258 /*********************************************************************
5260 * WM_VSCROLL
5263 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5265 INT dy;
5267 if (!(es->style & ES_MULTILINE))
5268 return 0;
5270 if (!(es->style & ES_AUTOVSCROLL))
5271 return 0;
5273 dy = 0;
5274 switch (action) {
5275 case SB_LINEUP:
5276 case SB_LINEDOWN:
5277 case SB_PAGEUP:
5278 case SB_PAGEDOWN:
5279 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5280 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5281 (action == SB_PAGEUP ? "SB_PAGEUP" :
5282 "SB_PAGEDOWN"))));
5283 EDIT_EM_Scroll(es, action);
5284 return 0;
5285 case SB_TOP:
5286 TRACE("SB_TOP\n");
5287 dy = -es->y_offset;
5288 break;
5289 case SB_BOTTOM:
5290 TRACE("SB_BOTTOM\n");
5291 dy = es->line_count - 1 - es->y_offset;
5292 break;
5293 case SB_THUMBTRACK:
5294 TRACE("SB_THUMBTRACK %d\n", pos);
5295 es->flags |= EF_VSCROLL_TRACK;
5296 if(es->style & WS_VSCROLL)
5297 dy = pos - es->y_offset;
5298 else
5300 /* Assume default scroll range 0-100 */
5301 INT vlc, new_y;
5302 /* Sanity check */
5303 if(pos < 0 || pos > 100) return 0;
5304 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5305 new_y = pos * (es->line_count - vlc) / 100;
5306 dy = es->line_count ? (new_y - es->y_offset) : 0;
5307 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5308 es->line_count, es->y_offset, pos, dy);
5310 break;
5311 case SB_THUMBPOSITION:
5312 TRACE("SB_THUMBPOSITION %d\n", pos);
5313 es->flags &= ~EF_VSCROLL_TRACK;
5314 if(es->style & WS_VSCROLL)
5315 dy = pos - es->y_offset;
5316 else
5318 /* Assume default scroll range 0-100 */
5319 INT vlc, new_y;
5320 /* Sanity check */
5321 if(pos < 0 || pos > 100) return 0;
5322 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5323 new_y = pos * (es->line_count - vlc) / 100;
5324 dy = es->line_count ? (new_y - es->y_offset) : 0;
5325 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5326 es->line_count, es->y_offset, pos, dy);
5328 if (!dy)
5330 /* force scroll info update */
5331 EDIT_UpdateScrollInfo(es);
5332 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5334 break;
5335 case SB_ENDSCROLL:
5336 TRACE("SB_ENDSCROLL\n");
5337 break;
5339 * FIXME : the next two are undocumented !
5340 * Are we doing the right thing ?
5341 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5342 * although it's also a regular control message.
5344 case EM_GETTHUMB: /* this one is used by NT notepad */
5345 case EM_GETTHUMB16:
5347 LRESULT ret;
5348 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5349 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5350 else
5352 /* Assume default scroll range 0-100 */
5353 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5354 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5356 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5357 return ret;
5359 case EM_LINESCROLL16:
5360 TRACE("EM_LINESCROLL16 %d\n", pos);
5361 dy = pos;
5362 break;
5364 default:
5365 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5366 action, action);
5367 return 0;
5369 if (dy)
5370 EDIT_EM_LineScroll(es, 0, dy);
5371 return 0;
5374 /*********************************************************************
5376 * EDIT_UpdateText
5379 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5381 if (es->flags & EF_UPDATE) {
5382 es->flags &= ~EF_UPDATE;
5383 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5385 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5389 /*********************************************************************
5391 * EDIT_UpdateText
5394 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5396 if (es->flags & EF_UPDATE) {
5397 es->flags &= ~EF_UPDATE;
5398 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5400 InvalidateRect(es->hwndSelf, rc, bErase);
5403 /********************************************************************
5405 * The Following code is to handle inline editing from IMEs
5408 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5410 DWORD dwBufLen;
5411 LPWSTR lpCompStr = NULL;
5412 HIMC hIMC;
5413 LPSTR lpCompStrAttr = NULL;
5414 DWORD dwBufLenAttr;
5416 if (!(hIMC = ImmGetContext(hwnd)))
5417 return;
5419 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5421 if (dwBufLen < 0)
5423 ImmReleaseContext(hwnd, hIMC);
5424 return;
5427 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
5428 if (!lpCompStr)
5430 ERR("Unable to allocate IME CompositionString\n");
5431 ImmReleaseContext(hwnd,hIMC);
5432 return;
5435 if (dwBufLen)
5436 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
5437 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
5439 if (CompFlag & GCS_COMPATTR)
5442 * We do not use the attributes yet. it would tell us what characters
5443 * are in transition and which are converted or decided upon
5445 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5446 if (dwBufLenAttr)
5448 dwBufLenAttr ++;
5449 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5450 if (!lpCompStrAttr)
5452 ERR("Unable to allocate IME Attribute String\n");
5453 HeapFree(GetProcessHeap(),0,lpCompStr);
5454 ImmReleaseContext(hwnd,hIMC);
5455 return;
5457 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5458 dwBufLenAttr);
5459 lpCompStrAttr[dwBufLenAttr] = 0;
5461 else
5462 lpCompStrAttr = NULL;
5465 /* check for change in composition start */
5466 if (es->selection_end < es->composition_start)
5467 es->composition_start = es->selection_end;
5469 /* replace existing selection string */
5470 es->selection_start = es->composition_start;
5472 if (es->composition_len > 0)
5473 es->selection_end = es->composition_start + es->composition_len;
5474 else
5475 es->selection_end = es->selection_start;
5477 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5478 es->composition_len = abs(es->composition_start - es->selection_end);
5480 es->selection_start = es->composition_start;
5481 es->selection_end = es->selection_start + es->composition_len;
5483 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5484 HeapFree(GetProcessHeap(),0,lpCompStr);
5485 ImmReleaseContext(hwnd,hIMC);
5488 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5490 DWORD dwBufLen;
5491 LPWSTR lpResultStr;
5492 HIMC hIMC;
5494 if ( !(hIMC = ImmGetContext(hwnd)))
5495 return;
5497 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5498 if (dwBufLen <= 0)
5500 ImmReleaseContext(hwnd, hIMC);
5501 return;
5504 lpResultStr = HeapAlloc(GetProcessHeap(),0, dwBufLen+sizeof(WCHAR));
5505 if (!lpResultStr)
5507 ERR("Unable to alloc buffer for IME string\n");
5508 ImmReleaseContext(hwnd, hIMC);
5509 return;
5512 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, dwBufLen);
5513 lpResultStr[dwBufLen/sizeof(WCHAR)] = 0;
5515 /* check for change in composition start */
5516 if (es->selection_end < es->composition_start)
5517 es->composition_start = es->selection_end;
5519 es->selection_start = es->composition_start;
5520 es->selection_end = es->composition_start + es->composition_len;
5521 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5522 es->composition_start = es->selection_end;
5523 es->composition_len = 0;
5525 HeapFree(GetProcessHeap(),0,lpResultStr);
5526 ImmReleaseContext(hwnd, hIMC);
5529 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5531 if (CompFlag & GCS_RESULTSTR)
5532 EDIT_GetResultStr(hwnd,es);
5533 if (CompFlag & GCS_COMPSTR)
5534 EDIT_GetCompositionStr(hwnd, CompFlag, es);