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