user32: Tweak how the edit control handles WM_GETDLGCODE.
[wine/gsoc_dplay.git] / dlls / user32 / edit.c
blob0ff4e7cf960e9896dd50459b92adb5d12a0544b5
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTES
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_OEMCONVERT
45 #include "config.h"
47 #include <stdarg.h>
48 #include <string.h>
49 #include <stdlib.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnt.h"
54 #include "wownt32.h"
55 #include "win.h"
56 #include "imm.h"
57 #include "wine/winbase16.h"
58 #include "wine/winuser16.h"
59 #include "wine/unicode.h"
60 #include "controls.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
69 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
70 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
71 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
74 * extra flags for EDITSTATE.flags field
76 #define EF_MODIFIED 0x0001 /* text has been modified */
77 #define EF_FOCUSED 0x0002 /* we have input focus */
78 #define EF_UPDATE 0x0004 /* notify parent of changed state */
79 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
80 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
82 wrapped line, instead of in front of the next character */
83 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
84 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
85 the text buffer if this is clear. */
86 typedef enum
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
93 } LINE_END;
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
98 LINE_END ending;
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
102 } LINEDEF;
104 typedef struct
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
109 UINT buffer_size; /* the size of the buffer in characters */
110 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
111 HFONT font; /* NULL means standard system font */
112 INT x_offset; /* scroll offset for multi lines this is in pixels
113 for single lines it's in characters */
114 INT line_height; /* height of a screen line in pixels */
115 INT char_width; /* average character width in pixels */
116 DWORD style; /* sane version of wnd->dwStyle */
117 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
118 INT undo_insert_count; /* number of characters inserted in sequence */
119 UINT undo_position; /* character index of the insertion and deletion */
120 LPWSTR undo_text; /* deleted text */
121 UINT undo_buffer_size; /* size of the deleted text buffer */
122 INT selection_start; /* == selection_end if no selection */
123 INT selection_end; /* == current caret position */
124 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
125 INT left_margin; /* in pixels */
126 INT right_margin; /* in pixels */
127 RECT format_rect;
128 INT text_width; /* width of the widest line in pixels for multi line controls
129 and just line width for single line controls */
130 INT region_posx; /* Position of cursor relative to region: */
131 INT region_posy; /* -1: to left, 0: within, 1: to right */
132 EDITWORDBREAKPROC16 word_break_proc16;
133 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
134 INT line_count; /* number of lines */
135 INT y_offset; /* scroll offset in number of lines */
136 BOOL bCaptureState; /* flag indicating whether mouse was captured */
137 BOOL bEnableState; /* flag keeping the enable state */
138 HWND hwndSelf; /* the our window handle */
139 HWND hwndParent; /* Handle of parent for sending EN_* messages.
140 Even if parent will change, EN_* messages
141 should be sent to the first parent. */
142 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
144 * only for multi line controls
146 INT lock_count; /* amount of re-entries in the EditWndProc */
147 INT tabs_count;
148 LPINT tabs;
149 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
150 HLOCAL hloc32W; /* our unicode local memory block */
151 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
152 or EM_SETHANDLE16 */
153 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
154 or EM_SETHANDLE */
156 * IME Data
158 UINT composition_len; /* length of composition, 0 == no composition */
159 int composition_start; /* the character position for the composition */
160 } EDITSTATE;
163 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
164 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
166 /* used for disabled or read-only edit control */
167 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
168 do \
169 { /* Notify parent which has created this edit control */ \
170 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
171 SendMessageW(es->hwndParent, WM_COMMAND, \
172 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
173 (LPARAM)(es->hwndSelf)); \
174 } while(0)
176 /*********************************************************************
178 * Declarations
183 * These functions have trivial implementations
184 * We still like to call them internally
185 * "static inline" makes them more like macro's
187 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es);
188 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
189 static inline void EDIT_WM_Clear(EDITSTATE *es);
190 static inline void EDIT_WM_Cut(EDITSTATE *es);
193 * Helper functions only valid for one type of control
195 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
196 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
197 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
198 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
199 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
200 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
201 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
203 * Helper functions valid for both single line _and_ multi line controls
205 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
206 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
207 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y);
208 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
209 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
210 static void EDIT_LockBuffer(EDITSTATE *es);
211 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
212 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
213 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
214 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
215 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
216 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
217 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
218 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
219 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
220 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
221 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
222 static void EDIT_AdjustFormatRect(EDITSTATE *es);
223 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *lprc);
224 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
225 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
226 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
228 * EM_XXX message handlers
230 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
231 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
232 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
233 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
234 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
235 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end);
236 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
237 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
238 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line);
239 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
240 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
241 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
242 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
243 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
244 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
245 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
246 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
247 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
248 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit);
249 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, WORD left, WORD right, BOOL repaint);
250 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
251 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
252 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs);
253 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs);
254 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
255 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
256 static BOOL EDIT_EM_Undo(EDITSTATE *es);
258 * WM_XXX message handlers
260 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
261 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
262 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
263 static void EDIT_WM_Copy(EDITSTATE *es);
264 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
265 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
266 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
267 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
268 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
269 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
270 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
271 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
272 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
273 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
274 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
275 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
276 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
277 static void EDIT_WM_Paste(EDITSTATE *es);
278 static void EDIT_WM_SetFocus(EDITSTATE *es);
279 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
280 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
281 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
282 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
283 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
284 static void EDIT_WM_Timer(EDITSTATE *es);
285 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
286 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase);
287 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
288 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es);
290 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
291 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
293 /*********************************************************************
294 * edit class descriptor
296 static const WCHAR editW[] = {'E','d','i','t',0};
297 const struct builtin_class_descr EDIT_builtin_class =
299 editW, /* name */
300 CS_DBLCLKS | CS_PARENTDC, /* style */
301 EditWndProcA, /* procA */
302 EditWndProcW, /* procW */
303 sizeof(EDITSTATE *), /* extra */
304 IDC_IBEAM, /* cursor */
305 0 /* brush */
309 /*********************************************************************
311 * EM_CANUNDO
314 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
316 return (es->undo_insert_count || strlenW(es->undo_text));
320 /*********************************************************************
322 * EM_EMPTYUNDOBUFFER
325 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
327 es->undo_insert_count = 0;
328 *es->undo_text = '\0';
332 /*********************************************************************
334 * WM_CLEAR
337 static inline void EDIT_WM_Clear(EDITSTATE *es)
339 static const WCHAR empty_stringW[] = {0};
341 /* Protect read-only edit control from modification */
342 if(es->style & ES_READONLY)
343 return;
345 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
349 /*********************************************************************
351 * WM_CUT
354 static inline void EDIT_WM_Cut(EDITSTATE *es)
356 EDIT_WM_Copy(es);
357 EDIT_WM_Clear(es);
361 /**********************************************************************
362 * get_app_version
364 * Returns the window version in case Wine emulates a later version
365 * of windows than the application expects.
367 * In a number of cases when windows runs an application that was
368 * designed for an earlier windows version, windows reverts
369 * to "old" behaviour of that earlier version.
371 * An example is a disabled edit control that needs to be painted.
372 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
373 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
374 * applications with an expected version 0f 4.0 or higher.
377 static DWORD get_app_version(void)
379 static DWORD version;
380 if (!version)
382 DWORD dwEmulatedVersion;
383 OSVERSIONINFOW info;
384 DWORD dwProcVersion = GetProcessVersion(0);
386 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
387 GetVersionExW( &info );
388 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
389 /* FIXME: this may not be 100% correct; see discussion on the
390 * wine developer list in Nov 1999 */
391 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
393 return version;
396 static inline UINT get_text_length(EDITSTATE *es)
398 if(es->text_length == (UINT)-1)
399 es->text_length = strlenW(es->text);
400 return es->text_length;
403 static inline void text_buffer_changed(EDITSTATE *es)
405 es->text_length = (UINT)-1;
408 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
410 HBRUSH hbrush;
411 UINT msg;
413 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
414 msg = WM_CTLCOLORSTATIC;
415 else
416 msg = WM_CTLCOLOREDIT;
418 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
419 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
420 if (!hbrush)
421 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
422 return hbrush;
425 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
427 if(unicode)
428 return DefWindowProcW(hwnd, msg, wParam, lParam);
429 else
430 return DefWindowProcA(hwnd, msg, wParam, lParam);
433 /*********************************************************************
435 * EditWndProc_common
437 * The messages are in the order of the actual integer values
438 * (which can be found in include/windows.h)
439 * Wherever possible the 16 bit versions are converted to
440 * the 32 bit ones, so that we can 'fall through' to the
441 * helper functions. These are mostly 32 bit (with a few
442 * exceptions, clearly indicated by a '16' extension to their
443 * names).
446 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
447 WPARAM wParam, LPARAM lParam, BOOL unicode )
449 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
450 LRESULT result = 0;
452 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
454 if (!es && msg != WM_NCCREATE)
455 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
457 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
459 switch (msg) {
460 case EM_GETSEL16:
461 wParam = 0;
462 lParam = 0;
463 /* fall through */
464 case EM_GETSEL:
465 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
466 break;
468 case EM_SETSEL16:
469 if ((short)LOWORD(lParam) == -1)
470 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
471 else
472 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
473 if (!wParam)
474 EDIT_EM_ScrollCaret(es);
475 result = 1;
476 break;
477 case EM_SETSEL:
478 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
479 EDIT_EM_ScrollCaret(es);
480 result = 1;
481 break;
483 case EM_GETRECT16:
484 if (lParam)
486 RECT16 *r16 = MapSL(lParam);
487 r16->left = es->format_rect.left;
488 r16->top = es->format_rect.top;
489 r16->right = es->format_rect.right;
490 r16->bottom = es->format_rect.bottom;
492 break;
493 case EM_GETRECT:
494 if (lParam)
495 CopyRect((LPRECT)lParam, &es->format_rect);
496 break;
498 case EM_SETRECT16:
499 if ((es->style & ES_MULTILINE) && lParam) {
500 RECT rc;
501 RECT16 *r16 = MapSL(lParam);
502 rc.left = r16->left;
503 rc.top = r16->top;
504 rc.right = r16->right;
505 rc.bottom = r16->bottom;
506 EDIT_SetRectNP(es, &rc);
507 EDIT_UpdateText(es, NULL, TRUE);
509 break;
510 case EM_SETRECT:
511 if ((es->style & ES_MULTILINE) && lParam) {
512 EDIT_SetRectNP(es, (LPRECT)lParam);
513 EDIT_UpdateText(es, NULL, TRUE);
515 break;
517 case EM_SETRECTNP16:
518 if ((es->style & ES_MULTILINE) && lParam) {
519 RECT rc;
520 RECT16 *r16 = MapSL(lParam);
521 rc.left = r16->left;
522 rc.top = r16->top;
523 rc.right = r16->right;
524 rc.bottom = r16->bottom;
525 EDIT_SetRectNP(es, &rc);
527 break;
528 case EM_SETRECTNP:
529 if ((es->style & ES_MULTILINE) && lParam)
530 EDIT_SetRectNP(es, (LPRECT)lParam);
531 break;
533 case EM_SCROLL16:
534 case EM_SCROLL:
535 result = EDIT_EM_Scroll(es, (INT)wParam);
536 break;
538 case EM_LINESCROLL16:
539 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
540 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
541 /* fall through */
542 case EM_LINESCROLL:
543 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
544 break;
546 case EM_SCROLLCARET16:
547 case EM_SCROLLCARET:
548 EDIT_EM_ScrollCaret(es);
549 result = 1;
550 break;
552 case EM_GETMODIFY16:
553 case EM_GETMODIFY:
554 result = ((es->flags & EF_MODIFIED) != 0);
555 break;
557 case EM_SETMODIFY16:
558 case EM_SETMODIFY:
559 if (wParam)
560 es->flags |= EF_MODIFIED;
561 else
562 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
563 break;
565 case EM_GETLINECOUNT16:
566 case EM_GETLINECOUNT:
567 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
568 break;
570 case EM_LINEINDEX16:
571 if ((INT16)wParam == -1)
572 wParam = (WPARAM)-1;
573 /* fall through */
574 case EM_LINEINDEX:
575 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
576 break;
578 case EM_SETHANDLE16:
579 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
580 break;
581 case EM_SETHANDLE:
582 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
583 break;
585 case EM_GETHANDLE16:
586 result = (LRESULT)EDIT_EM_GetHandle16(es);
587 break;
588 case EM_GETHANDLE:
589 result = (LRESULT)EDIT_EM_GetHandle(es);
590 break;
592 case EM_GETTHUMB16:
593 case EM_GETTHUMB:
594 result = EDIT_EM_GetThumb(es);
595 break;
597 /* these messages missing from specs */
598 case WM_USER+15:
599 case 0x00bf:
600 case WM_USER+16:
601 case 0x00c0:
602 case WM_USER+19:
603 case 0x00c3:
604 case WM_USER+26:
605 case 0x00ca:
606 FIXME("undocumented message 0x%x, please report\n", msg);
607 result = DefWindowProcW(hwnd, msg, wParam, lParam);
608 break;
610 case EM_LINELENGTH16:
611 case EM_LINELENGTH:
612 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
613 break;
615 case EM_REPLACESEL16:
616 lParam = (LPARAM)MapSL(lParam);
617 unicode = FALSE; /* 16-bit message is always ascii */
618 /* fall through */
619 case EM_REPLACESEL:
621 LPWSTR textW;
623 if(unicode)
624 textW = (LPWSTR)lParam;
625 else
627 LPSTR textA = (LPSTR)lParam;
628 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
629 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
630 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
633 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
634 result = 1;
636 if(!unicode)
637 HeapFree(GetProcessHeap(), 0, textW);
638 break;
641 case EM_GETLINE16:
642 lParam = (LPARAM)MapSL(lParam);
643 unicode = FALSE; /* 16-bit message is always ascii */
644 /* fall through */
645 case EM_GETLINE:
646 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
647 break;
649 case EM_LIMITTEXT16:
650 case EM_SETLIMITTEXT:
651 EDIT_EM_SetLimitText(es, wParam);
652 break;
654 case EM_CANUNDO16:
655 case EM_CANUNDO:
656 result = (LRESULT)EDIT_EM_CanUndo(es);
657 break;
659 case EM_UNDO16:
660 case EM_UNDO:
661 case WM_UNDO:
662 result = (LRESULT)EDIT_EM_Undo(es);
663 break;
665 case EM_FMTLINES16:
666 case EM_FMTLINES:
667 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
668 break;
670 case EM_LINEFROMCHAR16:
671 case EM_LINEFROMCHAR:
672 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
673 break;
675 case EM_SETTABSTOPS16:
676 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
677 break;
678 case EM_SETTABSTOPS:
679 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
680 break;
682 case EM_SETPASSWORDCHAR16:
683 unicode = FALSE; /* 16-bit message is always ascii */
684 /* fall through */
685 case EM_SETPASSWORDCHAR:
687 WCHAR charW = 0;
689 if(unicode)
690 charW = (WCHAR)wParam;
691 else
693 CHAR charA = wParam;
694 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
697 EDIT_EM_SetPasswordChar(es, charW);
698 break;
701 case EM_EMPTYUNDOBUFFER16:
702 case EM_EMPTYUNDOBUFFER:
703 EDIT_EM_EmptyUndoBuffer(es);
704 break;
706 case EM_GETFIRSTVISIBLELINE16:
707 result = es->y_offset;
708 break;
709 case EM_GETFIRSTVISIBLELINE:
710 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
711 break;
713 case EM_SETREADONLY16:
714 case EM_SETREADONLY:
715 if (wParam) {
716 SetWindowLongW( hwnd, GWL_STYLE,
717 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
718 es->style |= ES_READONLY;
719 } else {
720 SetWindowLongW( hwnd, GWL_STYLE,
721 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
722 es->style &= ~ES_READONLY;
724 result = 1;
725 break;
727 case EM_SETWORDBREAKPROC16:
728 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
729 break;
730 case EM_SETWORDBREAKPROC:
731 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
732 break;
734 case EM_GETWORDBREAKPROC16:
735 result = (LRESULT)es->word_break_proc16;
736 break;
737 case EM_GETWORDBREAKPROC:
738 result = (LRESULT)es->word_break_proc;
739 break;
741 case EM_GETPASSWORDCHAR16:
742 unicode = FALSE; /* 16-bit message is always ascii */
743 /* fall through */
744 case EM_GETPASSWORDCHAR:
746 if(unicode)
747 result = es->password_char;
748 else
750 WCHAR charW = es->password_char;
751 CHAR charA = 0;
752 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
753 result = charA;
755 break;
758 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
760 case EM_SETMARGINS:
761 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
762 break;
764 case EM_GETMARGINS:
765 result = MAKELONG(es->left_margin, es->right_margin);
766 break;
768 case EM_GETLIMITTEXT:
769 result = es->buffer_limit;
770 break;
772 case EM_POSFROMCHAR:
773 if ((INT)wParam >= get_text_length(es)) result = -1;
774 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
775 break;
777 case EM_CHARFROMPOS:
778 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
779 break;
781 /* End of the EM_ messages which were in numerical order; what order
782 * are these in? vaguely alphabetical?
785 case WM_NCCREATE:
786 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
787 break;
789 case WM_DESTROY:
790 result = EDIT_WM_Destroy(es);
791 es = NULL;
792 break;
794 case WM_GETDLGCODE:
795 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
797 if (es->style & ES_MULTILINE)
798 result |= DLGC_WANTALLKEYS;
800 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
802 int vk = (int)((LPMSG)lParam)->wParam;
804 if (es->hwndListBox)
806 if (vk == VK_RETURN || vk == VK_ESCAPE)
807 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
808 result |= DLGC_WANTMESSAGE;
810 else
812 switch (vk)
814 case VK_RETURN:
815 SendMessageW(GetParent(hwnd), WM_COMMAND, IDOK, (LPARAM)GetDlgItem(GetParent(hwnd), IDOK));
816 break;
817 case VK_ESCAPE:
818 SendMessageW(GetParent(hwnd), WM_CLOSE, 0, 0);
819 break;
820 case VK_TAB:
821 SendMessageW(GetParent(hwnd), WM_NEXTDLGCTL, (GetKeyState(VK_SHIFT) & 0x8000), 0);
822 break;
823 default:
824 break;
828 break;
830 case WM_IME_CHAR:
831 if (!unicode)
833 WCHAR charW;
834 CHAR strng[2];
836 strng[0] = wParam >> 8;
837 strng[1] = wParam & 0xff;
838 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
839 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
840 EDIT_WM_Char(es, charW);
841 break;
843 /* fall through */
844 case WM_CHAR:
846 WCHAR charW;
848 if(unicode)
849 charW = wParam;
850 else
852 CHAR charA = wParam;
853 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
856 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
858 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
859 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
860 break;
862 EDIT_WM_Char(es, charW);
863 break;
866 case WM_CLEAR:
867 EDIT_WM_Clear(es);
868 break;
870 case WM_COMMAND:
871 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
872 break;
874 case WM_CONTEXTMENU:
875 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
876 break;
878 case WM_COPY:
879 EDIT_WM_Copy(es);
880 break;
882 case WM_CREATE:
883 if(unicode)
884 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
885 else
887 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
888 LPWSTR nameW = NULL;
889 if(nameA)
891 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
892 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
893 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
895 result = EDIT_WM_Create(es, nameW);
896 HeapFree(GetProcessHeap(), 0, nameW);
898 break;
900 case WM_CUT:
901 EDIT_WM_Cut(es);
902 break;
904 case WM_ENABLE:
905 es->bEnableState = (BOOL) wParam;
906 EDIT_UpdateText(es, NULL, TRUE);
907 break;
909 case WM_ERASEBKGND:
910 /* we do the proper erase in EDIT_WM_Paint */
911 result = 1;
912 break;
914 case WM_GETFONT:
915 result = (LRESULT)es->font;
916 break;
918 case WM_GETTEXT:
919 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
920 break;
922 case WM_GETTEXTLENGTH:
923 if (unicode) result = get_text_length(es);
924 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
925 NULL, 0, NULL, NULL );
926 break;
928 case WM_HSCROLL:
929 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
930 break;
932 case WM_KEYDOWN:
933 result = EDIT_WM_KeyDown(es, (INT)wParam);
934 break;
936 case WM_KILLFOCUS:
937 result = EDIT_WM_KillFocus(es);
938 break;
940 case WM_LBUTTONDBLCLK:
941 result = EDIT_WM_LButtonDblClk(es);
942 break;
944 case WM_LBUTTONDOWN:
945 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
946 break;
948 case WM_LBUTTONUP:
949 result = EDIT_WM_LButtonUp(es);
950 break;
952 case WM_MBUTTONDOWN:
953 result = EDIT_WM_MButtonDown(es);
954 break;
956 case WM_MOUSEMOVE:
957 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
958 break;
960 case WM_PRINTCLIENT:
961 case WM_PAINT:
962 EDIT_WM_Paint(es, (HDC)wParam);
963 break;
965 case WM_PASTE:
966 EDIT_WM_Paste(es);
967 break;
969 case WM_SETFOCUS:
970 EDIT_WM_SetFocus(es);
971 break;
973 case WM_SETFONT:
974 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
975 break;
977 case WM_SETREDRAW:
978 /* FIXME: actually set an internal flag and behave accordingly */
979 break;
981 case WM_SETTEXT:
982 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
983 result = TRUE;
984 break;
986 case WM_SIZE:
987 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
988 break;
990 case WM_STYLECHANGED:
991 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
992 break;
994 case WM_STYLECHANGING:
995 result = 0; /* See EDIT_WM_StyleChanged */
996 break;
998 case WM_SYSKEYDOWN:
999 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
1000 break;
1002 case WM_TIMER:
1003 EDIT_WM_Timer(es);
1004 break;
1006 case WM_VSCROLL:
1007 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
1008 break;
1010 case WM_MOUSEWHEEL:
1012 int gcWheelDelta = 0;
1013 UINT pulScrollLines = 3;
1014 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1016 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1017 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1018 break;
1020 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1021 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1023 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1024 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1025 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
1028 break;
1031 /* IME messages to make the edit control IME aware */
1032 case WM_IME_SETCONTEXT:
1033 break;
1035 case WM_IME_STARTCOMPOSITION:
1037 * FIXME in IME: This message is not always sent like it should be
1039 if (es->selection_start != es->selection_end)
1041 static const WCHAR empty_stringW[] = {0};
1042 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1044 es->composition_start = es->selection_end;
1045 es->composition_len = 0;
1046 break;
1048 case WM_IME_COMPOSITION:
1050 int caret_pos = es->selection_end;
1051 if (es->composition_len == 0)
1053 if (es->selection_start != es->selection_end)
1055 static const WCHAR empty_stringW[] = {0};
1056 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
1059 es->composition_start = es->selection_end;
1061 EDIT_ImeComposition(hwnd,lParam,es);
1062 EDIT_SetCaretPos(es, caret_pos, es->flags & EF_AFTER_WRAP);
1063 break;
1066 case WM_IME_ENDCOMPOSITION:
1067 es->composition_len= 0;
1068 break;
1070 case WM_IME_COMPOSITIONFULL:
1071 break;
1073 case WM_IME_SELECT:
1074 break;
1076 case WM_IME_CONTROL:
1077 break;
1079 default:
1080 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
1081 break;
1084 if (es) EDIT_UnlockBuffer(es, FALSE);
1086 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
1088 return result;
1091 /*********************************************************************
1093 * EditWndProcW (USER32.@)
1095 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1097 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1100 /*********************************************************************
1102 * EditWndProc (USER32.@)
1104 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1106 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1109 /*********************************************************************
1111 * EDIT_BuildLineDefs_ML
1113 * Build linked list of text lines.
1114 * Lines can end with '\0' (last line), a character (if it is wrapped),
1115 * a soft return '\r\r\n' or a hard return '\r\n'
1118 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1120 HDC dc;
1121 HFONT old_font = 0;
1122 LPWSTR current_position, cp;
1123 INT fw;
1124 LINEDEF *current_line;
1125 LINEDEF *previous_line;
1126 LINEDEF *start_line;
1127 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1128 INT line_count = es->line_count;
1129 INT orig_net_length;
1130 RECT rc;
1132 if (istart == iend && delta == 0)
1133 return;
1135 dc = GetDC(es->hwndSelf);
1136 if (es->font)
1137 old_font = SelectObject(dc, es->font);
1139 previous_line = NULL;
1140 current_line = es->first_line_def;
1142 /* Find starting line. istart must lie inside an existing line or
1143 * at the end of buffer */
1144 do {
1145 if (istart < current_line->index + current_line->length ||
1146 current_line->ending == END_0)
1147 break;
1149 previous_line = current_line;
1150 current_line = current_line->next;
1151 line_index++;
1152 } while (current_line);
1154 if (!current_line) /* Error occurred start is not inside previous buffer */
1156 FIXME(" modification occurred outside buffer\n");
1157 ReleaseDC(es->hwndSelf, dc);
1158 return;
1161 /* Remember start of modifications in order to calculate update region */
1162 nstart_line = line_index;
1163 nstart_index = current_line->index;
1165 /* We must start to reformat from the previous line since the modifications
1166 * may have caused the line to wrap upwards. */
1167 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1169 line_index--;
1170 current_line = previous_line;
1172 start_line = current_line;
1174 fw = es->format_rect.right - es->format_rect.left;
1175 current_position = es->text + current_line->index;
1176 do {
1177 if (current_line != start_line)
1179 if (!current_line || current_line->index + delta > current_position - es->text)
1181 /* The buffer has been expanded, create a new line and
1182 insert it into the link list */
1183 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1184 new_line->next = previous_line->next;
1185 previous_line->next = new_line;
1186 current_line = new_line;
1187 es->line_count++;
1189 else if (current_line->index + delta < current_position - es->text)
1191 /* The previous line merged with this line so we delete this extra entry */
1192 previous_line->next = current_line->next;
1193 HeapFree(GetProcessHeap(), 0, current_line);
1194 current_line = previous_line->next;
1195 es->line_count--;
1196 continue;
1198 else /* current_line->index + delta == current_position */
1200 if (current_position - es->text > iend)
1201 break; /* We reached end of line modifications */
1202 /* else recalulate this line */
1206 current_line->index = current_position - es->text;
1207 orig_net_length = current_line->net_length;
1209 /* Find end of line */
1210 cp = current_position;
1211 while (*cp) {
1212 if (*cp == '\n') break;
1213 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1214 break;
1215 cp++;
1218 /* Mark type of line termination */
1219 if (!(*cp)) {
1220 current_line->ending = END_0;
1221 current_line->net_length = strlenW(current_position);
1222 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1223 current_line->ending = END_SOFT;
1224 current_line->net_length = cp - current_position - 1;
1225 } else if (*cp == '\n') {
1226 current_line->ending = END_RICH;
1227 current_line->net_length = cp - current_position;
1228 } else {
1229 current_line->ending = END_HARD;
1230 current_line->net_length = cp - current_position;
1233 /* Calculate line width */
1234 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1235 current_position, current_line->net_length,
1236 es->tabs_count, es->tabs));
1238 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1239 if (!(es->style & ES_AUTOHSCROLL)) {
1240 if (current_line->width > fw) {
1241 INT next = 0;
1242 INT prev;
1243 do {
1244 prev = next;
1245 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1246 prev + 1, current_line->net_length, WB_RIGHT);
1247 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1248 current_position, next, es->tabs_count, es->tabs));
1249 } while (current_line->width <= fw);
1250 if (!prev) { /* Didn't find a line break so force a break */
1251 next = 0;
1252 do {
1253 prev = next;
1254 next++;
1255 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1256 current_position, next, es->tabs_count, es->tabs));
1257 } while (current_line->width <= fw);
1258 if (!prev)
1259 prev = 1;
1262 /* If the first line we are calculating, wrapped before istart, we must
1263 * adjust istart in order for this to be reflected in the update region. */
1264 if (current_line->index == nstart_index && istart > current_line->index + prev)
1265 istart = current_line->index + prev;
1266 /* else if we are updating the previous line before the first line we
1267 * are re-calculating and it expanded */
1268 else if (current_line == start_line &&
1269 current_line->index != nstart_index && orig_net_length < prev)
1271 /* Line expanded due to an upwards line wrap so we must partially include
1272 * previous line in update region */
1273 nstart_line = line_index;
1274 nstart_index = current_line->index;
1275 istart = current_line->index + orig_net_length;
1278 current_line->net_length = prev;
1279 current_line->ending = END_WRAP;
1280 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1281 current_line->net_length, es->tabs_count, es->tabs));
1283 else if (orig_net_length < current_line->net_length &&
1284 current_line == start_line &&
1285 current_line->index != nstart_index) {
1286 /* The previous line expanded but it's still not as wide as the client rect */
1287 /* The expansion is due to an upwards line wrap so we must partially include
1288 it in the update region */
1289 nstart_line = line_index;
1290 nstart_index = current_line->index;
1291 istart = current_line->index + orig_net_length;
1296 /* Adjust length to include line termination */
1297 switch (current_line->ending) {
1298 case END_SOFT:
1299 current_line->length = current_line->net_length + 3;
1300 break;
1301 case END_RICH:
1302 current_line->length = current_line->net_length + 1;
1303 break;
1304 case END_HARD:
1305 current_line->length = current_line->net_length + 2;
1306 break;
1307 case END_WRAP:
1308 case END_0:
1309 current_line->length = current_line->net_length;
1310 break;
1312 es->text_width = max(es->text_width, current_line->width);
1313 current_position += current_line->length;
1314 previous_line = current_line;
1315 current_line = current_line->next;
1316 line_index++;
1317 } while (previous_line->ending != END_0);
1319 /* Finish adjusting line indexes by delta or remove hanging lines */
1320 if (previous_line->ending == END_0)
1322 LINEDEF *pnext = NULL;
1324 previous_line->next = NULL;
1325 while (current_line)
1327 pnext = current_line->next;
1328 HeapFree(GetProcessHeap(), 0, current_line);
1329 current_line = pnext;
1330 es->line_count--;
1333 else if (delta != 0)
1335 while (current_line)
1337 current_line->index += delta;
1338 current_line = current_line->next;
1342 /* Calculate rest of modification rectangle */
1343 if (hrgn)
1345 HRGN tmphrgn;
1347 * We calculate two rectangles. One for the first line which may have
1348 * an indent with respect to the format rect. The other is a format-width
1349 * rectangle that spans the rest of the lines that changed or moved.
1351 rc.top = es->format_rect.top + nstart_line * es->line_height -
1352 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1353 rc.bottom = rc.top + es->line_height;
1354 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1355 rc.left = es->format_rect.left;
1356 else
1357 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1358 es->text + nstart_index, istart - nstart_index,
1359 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1360 rc.right = es->format_rect.right;
1361 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1363 rc.top = rc.bottom;
1364 rc.left = es->format_rect.left;
1365 rc.right = es->format_rect.right;
1367 * If lines were added or removed we must re-paint the remainder of the
1368 * lines since the remaining lines were either shifted up or down.
1370 if (line_count < es->line_count) /* We added lines */
1371 rc.bottom = es->line_count * es->line_height;
1372 else if (line_count > es->line_count) /* We removed lines */
1373 rc.bottom = line_count * es->line_height;
1374 else
1375 rc.bottom = line_index * es->line_height;
1376 rc.bottom += es->format_rect.top;
1377 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1378 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1379 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1380 DeleteObject(tmphrgn);
1383 if (es->font)
1384 SelectObject(dc, old_font);
1386 ReleaseDC(es->hwndSelf, dc);
1389 /*********************************************************************
1391 * EDIT_CalcLineWidth_SL
1394 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1396 SIZE size;
1397 LPWSTR text;
1398 HDC dc;
1399 HFONT old_font = 0;
1401 text = EDIT_GetPasswordPointer_SL(es);
1403 dc = GetDC(es->hwndSelf);
1404 if (es->font)
1405 old_font = SelectObject(dc, es->font);
1407 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1409 if (es->font)
1410 SelectObject(dc, old_font);
1411 ReleaseDC(es->hwndSelf, dc);
1413 if (es->style & ES_PASSWORD)
1414 HeapFree(GetProcessHeap(), 0, text);
1416 es->text_width = size.cx;
1419 /*********************************************************************
1421 * EDIT_CallWordBreakProc
1423 * Call appropriate WordBreakProc (internal or external).
1425 * Note: The "start" argument should always be an index referring
1426 * to es->text. The actual wordbreak proc might be
1427 * 16 bit, so we can't always pass any 32 bit LPSTR.
1428 * Hence we assume that es->text is the buffer that holds
1429 * the string under examination (we can decide this for ourselves).
1432 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1434 INT ret;
1436 if (es->word_break_proc16) {
1437 HGLOBAL16 hglob16;
1438 SEGPTR segptr;
1439 INT countA;
1440 WORD args[5];
1441 DWORD result;
1443 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1444 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1445 segptr = WOWGlobalLock16(hglob16);
1446 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1447 args[4] = SELECTOROF(segptr);
1448 args[3] = OFFSETOF(segptr);
1449 args[2] = index;
1450 args[1] = countA;
1451 args[0] = action;
1452 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1453 ret = LOWORD(result);
1454 GlobalUnlock16(hglob16);
1455 GlobalFree16(hglob16);
1457 else if (es->word_break_proc)
1459 if(es->is_unicode)
1461 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1463 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1464 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1465 ret = wbpW(es->text + start, index, count, action);
1467 else
1469 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1470 INT countA;
1471 CHAR *textA;
1473 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1474 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1475 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1476 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1477 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1478 ret = wbpA(textA, index, countA, action);
1479 HeapFree(GetProcessHeap(), 0, textA);
1482 else
1483 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1485 return ret;
1489 /*********************************************************************
1491 * EDIT_CharFromPos
1493 * Beware: This is not the function called on EM_CHARFROMPOS
1494 * The position _can_ be outside the formatting / client
1495 * rectangle
1496 * The return value is only the character index
1499 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1501 INT index;
1502 HDC dc;
1503 HFONT old_font = 0;
1504 INT x_high = 0, x_low = 0;
1506 if (es->style & ES_MULTILINE) {
1507 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1508 INT line_index = 0;
1509 LINEDEF *line_def = es->first_line_def;
1510 INT low, high;
1511 while ((line > 0) && line_def->next) {
1512 line_index += line_def->length;
1513 line_def = line_def->next;
1514 line--;
1516 x += es->x_offset - es->format_rect.left;
1517 if (es->style & ES_RIGHT)
1518 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1519 else if (es->style & ES_CENTER)
1520 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1521 if (x >= line_def->width) {
1522 if (after_wrap)
1523 *after_wrap = (line_def->ending == END_WRAP);
1524 return line_index + line_def->net_length;
1526 if (x <= 0) {
1527 if (after_wrap)
1528 *after_wrap = FALSE;
1529 return line_index;
1531 dc = GetDC(es->hwndSelf);
1532 if (es->font)
1533 old_font = SelectObject(dc, es->font);
1534 low = line_index;
1535 high = line_index + line_def->net_length + 1;
1536 while (low < high - 1)
1538 INT mid = (low + high) / 2;
1539 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1540 if (x_now > x) {
1541 high = mid;
1542 x_high = x_now;
1543 } else {
1544 low = mid;
1545 x_low = x_now;
1548 if (abs(x_high - x) + 1 <= abs(x_low - x))
1549 index = high;
1550 else
1551 index = low;
1553 if (after_wrap)
1554 *after_wrap = ((index == line_index + line_def->net_length) &&
1555 (line_def->ending == END_WRAP));
1556 } else {
1557 LPWSTR text;
1558 SIZE size;
1559 if (after_wrap)
1560 *after_wrap = FALSE;
1561 x -= es->format_rect.left;
1562 if (!x)
1563 return es->x_offset;
1565 if (!es->x_offset)
1567 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1568 if (es->style & ES_RIGHT)
1569 x -= indent;
1570 else if (es->style & ES_CENTER)
1571 x -= indent / 2;
1574 text = EDIT_GetPasswordPointer_SL(es);
1575 dc = GetDC(es->hwndSelf);
1576 if (es->font)
1577 old_font = SelectObject(dc, es->font);
1578 if (x < 0)
1580 INT low = 0;
1581 INT high = es->x_offset;
1582 while (low < high - 1)
1584 INT mid = (low + high) / 2;
1585 GetTextExtentPoint32W( dc, text + mid,
1586 es->x_offset - mid, &size );
1587 if (size.cx > -x) {
1588 low = mid;
1589 x_low = size.cx;
1590 } else {
1591 high = mid;
1592 x_high = size.cx;
1595 if (abs(x_high + x) <= abs(x_low + x) + 1)
1596 index = high;
1597 else
1598 index = low;
1600 else
1602 INT low = es->x_offset;
1603 INT high = get_text_length(es) + 1;
1604 while (low < high - 1)
1606 INT mid = (low + high) / 2;
1607 GetTextExtentPoint32W( dc, text + es->x_offset,
1608 mid - es->x_offset, &size );
1609 if (size.cx > x) {
1610 high = mid;
1611 x_high = size.cx;
1612 } else {
1613 low = mid;
1614 x_low = size.cx;
1617 if (abs(x_high - x) <= abs(x_low - x) + 1)
1618 index = high;
1619 else
1620 index = low;
1622 if (es->style & ES_PASSWORD)
1623 HeapFree(GetProcessHeap(), 0, text);
1625 if (es->font)
1626 SelectObject(dc, old_font);
1627 ReleaseDC(es->hwndSelf, dc);
1628 return index;
1632 /*********************************************************************
1634 * EDIT_ConfinePoint
1636 * adjusts the point to be within the formatting rectangle
1637 * (so CharFromPos returns the nearest _visible_ character)
1640 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
1642 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1643 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1647 /*********************************************************************
1649 * EDIT_GetLineRect
1651 * Calculates the bounding rectangle for a line from a starting
1652 * column to an ending column.
1655 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1657 INT line_index = EDIT_EM_LineIndex(es, line);
1659 if (es->style & ES_MULTILINE)
1660 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1661 else
1662 rc->top = es->format_rect.top;
1663 rc->bottom = rc->top + es->line_height;
1664 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1665 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1669 /*********************************************************************
1671 * EDIT_GetPasswordPointer_SL
1673 * note: caller should free the (optionally) allocated buffer
1676 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1678 if (es->style & ES_PASSWORD) {
1679 INT len = get_text_length(es);
1680 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1681 text[len] = '\0';
1682 while(len) text[--len] = es->password_char;
1683 return text;
1684 } else
1685 return es->text;
1689 /*********************************************************************
1691 * EDIT_LockBuffer
1693 * This acts as a LocalLock16(), but it locks only once. This way
1694 * you can call it whenever you like, without unlocking.
1696 * Initially the edit control allocates a HLOCAL32 buffer
1697 * (32 bit linear memory handler). However, 16 bit application
1698 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1699 * handler). From that moment on we have to keep using this 16 bit memory
1700 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1701 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1702 * conversion.
1705 static void EDIT_LockBuffer(EDITSTATE *es)
1707 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1708 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1710 if (!es->text) {
1711 CHAR *textA = NULL;
1712 UINT countA = 0;
1713 BOOL _16bit = FALSE;
1715 if(es->hloc32W)
1717 if(es->hloc32A)
1719 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1720 textA = LocalLock(es->hloc32A);
1721 countA = strlen(textA) + 1;
1723 else if(es->hloc16)
1725 HANDLE16 oldDS = stack16->ds;
1726 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1727 stack16->ds = hInstance;
1728 textA = MapSL(LocalLock16(es->hloc16));
1729 stack16->ds = oldDS;
1730 countA = strlen(textA) + 1;
1731 _16bit = TRUE;
1734 else {
1735 ERR("no buffer ... please report\n");
1736 return;
1739 if(textA)
1741 HLOCAL hloc32W_new;
1742 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1743 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1744 if(countW_new > es->buffer_size + 1)
1746 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1747 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1748 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1749 if(hloc32W_new)
1751 es->hloc32W = hloc32W_new;
1752 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1753 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1755 else
1756 WARN("FAILED! Will synchronize partially\n");
1760 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1761 es->text = LocalLock(es->hloc32W);
1763 if(textA)
1765 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1766 if(_16bit)
1768 HANDLE16 oldDS = stack16->ds;
1769 stack16->ds = hInstance;
1770 LocalUnlock16(es->hloc16);
1771 stack16->ds = oldDS;
1773 else
1774 LocalUnlock(es->hloc32A);
1777 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1778 es->lock_count++;
1782 /*********************************************************************
1784 * EDIT_SL_InvalidateText
1786 * Called from EDIT_InvalidateText().
1787 * Does the job for single-line controls only.
1790 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1792 RECT line_rect;
1793 RECT rc;
1795 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1796 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1797 EDIT_UpdateText(es, &rc, TRUE);
1801 /*********************************************************************
1803 * EDIT_ML_InvalidateText
1805 * Called from EDIT_InvalidateText().
1806 * Does the job for multi-line controls only.
1809 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1811 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1812 INT sl = EDIT_EM_LineFromChar(es, start);
1813 INT el = EDIT_EM_LineFromChar(es, end);
1814 INT sc;
1815 INT ec;
1816 RECT rc1;
1817 RECT rcWnd;
1818 RECT rcLine;
1819 RECT rcUpdate;
1820 INT l;
1822 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1823 return;
1825 sc = start - EDIT_EM_LineIndex(es, sl);
1826 ec = end - EDIT_EM_LineIndex(es, el);
1827 if (sl < es->y_offset) {
1828 sl = es->y_offset;
1829 sc = 0;
1831 if (el > es->y_offset + vlc) {
1832 el = es->y_offset + vlc;
1833 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1835 GetClientRect(es->hwndSelf, &rc1);
1836 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1837 if (sl == el) {
1838 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1839 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1840 EDIT_UpdateText(es, &rcUpdate, TRUE);
1841 } else {
1842 EDIT_GetLineRect(es, sl, sc,
1843 EDIT_EM_LineLength(es,
1844 EDIT_EM_LineIndex(es, sl)),
1845 &rcLine);
1846 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1847 EDIT_UpdateText(es, &rcUpdate, TRUE);
1848 for (l = sl + 1 ; l < el ; l++) {
1849 EDIT_GetLineRect(es, l, 0,
1850 EDIT_EM_LineLength(es,
1851 EDIT_EM_LineIndex(es, l)),
1852 &rcLine);
1853 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1854 EDIT_UpdateText(es, &rcUpdate, TRUE);
1856 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1857 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1858 EDIT_UpdateText(es, &rcUpdate, TRUE);
1863 /*********************************************************************
1865 * EDIT_InvalidateText
1867 * Invalidate the text from offset start up to, but not including,
1868 * offset end. Useful for (re)painting the selection.
1869 * Regions outside the linewidth are not invalidated.
1870 * end == -1 means end == TextLength.
1871 * start and end need not be ordered.
1874 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1876 if (end == start)
1877 return;
1879 if (end == -1)
1880 end = get_text_length(es);
1882 if (end < start) {
1883 INT tmp = start;
1884 start = end;
1885 end = tmp;
1888 if (es->style & ES_MULTILINE)
1889 EDIT_ML_InvalidateText(es, start, end);
1890 else
1891 EDIT_SL_InvalidateText(es, start, end);
1895 /*********************************************************************
1897 * EDIT_MakeFit
1899 * Try to fit size + 1 characters in the buffer.
1901 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1903 HLOCAL hNew32W;
1905 if (size <= es->buffer_size)
1906 return TRUE;
1908 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1910 /* Force edit to unlock it's buffer. es->text now NULL */
1911 EDIT_UnlockBuffer(es, TRUE);
1913 if (es->hloc32W) {
1914 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1915 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1916 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1917 es->hloc32W = hNew32W;
1918 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1922 EDIT_LockBuffer(es);
1924 if (es->buffer_size < size) {
1925 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1926 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1927 return FALSE;
1928 } else {
1929 TRACE("We now have %d+1\n", es->buffer_size);
1930 return TRUE;
1935 /*********************************************************************
1937 * EDIT_MakeUndoFit
1939 * Try to fit size + 1 bytes in the undo buffer.
1942 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1944 UINT alloc_size;
1946 if (size <= es->undo_buffer_size)
1947 return TRUE;
1949 TRACE("trying to ReAlloc to %d+1\n", size);
1951 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1952 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1953 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1954 return TRUE;
1956 else
1958 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1959 return FALSE;
1964 /*********************************************************************
1966 * EDIT_MoveBackward
1969 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1971 INT e = es->selection_end;
1973 if (e) {
1974 e--;
1975 if ((es->style & ES_MULTILINE) && e &&
1976 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1977 e--;
1978 if (e && (es->text[e - 1] == '\r'))
1979 e--;
1982 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1983 EDIT_EM_ScrollCaret(es);
1987 /*********************************************************************
1989 * EDIT_MoveDown_ML
1991 * Only for multi line controls
1992 * Move the caret one line down, on a column with the nearest
1993 * x coordinate on the screen (might be a different column).
1996 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1998 INT s = es->selection_start;
1999 INT e = es->selection_end;
2000 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2001 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2002 INT x = (short)LOWORD(pos);
2003 INT y = (short)HIWORD(pos);
2005 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
2006 if (!extend)
2007 s = e;
2008 EDIT_EM_SetSel(es, s, e, after_wrap);
2009 EDIT_EM_ScrollCaret(es);
2013 /*********************************************************************
2015 * EDIT_MoveEnd
2018 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
2020 BOOL after_wrap = FALSE;
2021 INT e;
2023 /* Pass a high value in x to make sure of receiving the end of the line */
2024 if (es->style & ES_MULTILINE)
2025 e = EDIT_CharFromPos(es, 0x3fffffff,
2026 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2027 else
2028 e = get_text_length(es);
2029 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2030 EDIT_EM_ScrollCaret(es);
2034 /*********************************************************************
2036 * EDIT_MoveForward
2039 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2041 INT e = es->selection_end;
2043 if (es->text[e]) {
2044 e++;
2045 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2046 if (es->text[e] == '\n')
2047 e++;
2048 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2049 e += 2;
2052 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2053 EDIT_EM_ScrollCaret(es);
2057 /*********************************************************************
2059 * EDIT_MoveHome
2061 * Home key: move to beginning of line.
2064 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2066 INT e;
2068 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2069 if (es->style & ES_MULTILINE)
2070 e = EDIT_CharFromPos(es, -es->x_offset,
2071 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2072 else
2073 e = 0;
2074 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2075 EDIT_EM_ScrollCaret(es);
2079 /*********************************************************************
2081 * EDIT_MovePageDown_ML
2083 * Only for multi line controls
2084 * Move the caret one page down, on a column with the nearest
2085 * x coordinate on the screen (might be a different column).
2088 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2090 INT s = es->selection_start;
2091 INT e = es->selection_end;
2092 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2093 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2094 INT x = (short)LOWORD(pos);
2095 INT y = (short)HIWORD(pos);
2097 e = EDIT_CharFromPos(es, x,
2098 y + (es->format_rect.bottom - es->format_rect.top),
2099 &after_wrap);
2100 if (!extend)
2101 s = e;
2102 EDIT_EM_SetSel(es, s, e, after_wrap);
2103 EDIT_EM_ScrollCaret(es);
2107 /*********************************************************************
2109 * EDIT_MovePageUp_ML
2111 * Only for multi line controls
2112 * Move the caret one page up, on a column with the nearest
2113 * x coordinate on the screen (might be a different column).
2116 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2118 INT s = es->selection_start;
2119 INT e = es->selection_end;
2120 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2121 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2122 INT x = (short)LOWORD(pos);
2123 INT y = (short)HIWORD(pos);
2125 e = EDIT_CharFromPos(es, x,
2126 y - (es->format_rect.bottom - es->format_rect.top),
2127 &after_wrap);
2128 if (!extend)
2129 s = e;
2130 EDIT_EM_SetSel(es, s, e, after_wrap);
2131 EDIT_EM_ScrollCaret(es);
2135 /*********************************************************************
2137 * EDIT_MoveUp_ML
2139 * Only for multi line controls
2140 * Move the caret one line up, on a column with the nearest
2141 * x coordinate on the screen (might be a different column).
2144 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2146 INT s = es->selection_start;
2147 INT e = es->selection_end;
2148 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2149 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2150 INT x = (short)LOWORD(pos);
2151 INT y = (short)HIWORD(pos);
2153 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2154 if (!extend)
2155 s = e;
2156 EDIT_EM_SetSel(es, s, e, after_wrap);
2157 EDIT_EM_ScrollCaret(es);
2161 /*********************************************************************
2163 * EDIT_MoveWordBackward
2166 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2168 INT s = es->selection_start;
2169 INT e = es->selection_end;
2170 INT l;
2171 INT ll;
2172 INT li;
2174 l = EDIT_EM_LineFromChar(es, e);
2175 ll = EDIT_EM_LineLength(es, e);
2176 li = EDIT_EM_LineIndex(es, l);
2177 if (e - li == 0) {
2178 if (l) {
2179 li = EDIT_EM_LineIndex(es, l - 1);
2180 e = li + EDIT_EM_LineLength(es, li);
2182 } else {
2183 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2185 if (!extend)
2186 s = e;
2187 EDIT_EM_SetSel(es, s, e, FALSE);
2188 EDIT_EM_ScrollCaret(es);
2192 /*********************************************************************
2194 * EDIT_MoveWordForward
2197 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2199 INT s = es->selection_start;
2200 INT e = es->selection_end;
2201 INT l;
2202 INT ll;
2203 INT li;
2205 l = EDIT_EM_LineFromChar(es, e);
2206 ll = EDIT_EM_LineLength(es, e);
2207 li = EDIT_EM_LineIndex(es, l);
2208 if (e - li == ll) {
2209 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2210 e = EDIT_EM_LineIndex(es, l + 1);
2211 } else {
2212 e = li + EDIT_CallWordBreakProc(es,
2213 li, e - li + 1, ll, WB_RIGHT);
2215 if (!extend)
2216 s = e;
2217 EDIT_EM_SetSel(es, s, e, FALSE);
2218 EDIT_EM_ScrollCaret(es);
2222 /*********************************************************************
2224 * EDIT_PaintLine
2227 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2229 INT s = es->selection_start;
2230 INT e = es->selection_end;
2231 INT li;
2232 INT ll;
2233 INT x;
2234 INT y;
2235 LRESULT pos;
2237 if (es->style & ES_MULTILINE) {
2238 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2239 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2240 return;
2241 } else if (line)
2242 return;
2244 TRACE("line=%d\n", line);
2246 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2247 x = (short)LOWORD(pos);
2248 y = (short)HIWORD(pos);
2249 li = EDIT_EM_LineIndex(es, line);
2250 ll = EDIT_EM_LineLength(es, li);
2251 s = min(es->selection_start, es->selection_end);
2252 e = max(es->selection_start, es->selection_end);
2253 s = min(li + ll, max(li, s));
2254 e = min(li + ll, max(li, e));
2255 if (rev && (s != e) &&
2256 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2257 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2258 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2259 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2260 } else
2261 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2265 /*********************************************************************
2267 * EDIT_PaintText
2270 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2272 COLORREF BkColor;
2273 COLORREF TextColor;
2274 LOGFONTW underline_font;
2275 HFONT hUnderline = 0;
2276 HFONT old_font = 0;
2277 INT ret;
2278 INT li;
2279 INT BkMode;
2280 SIZE size;
2282 if (!count)
2283 return 0;
2284 BkMode = GetBkMode(dc);
2285 BkColor = GetBkColor(dc);
2286 TextColor = GetTextColor(dc);
2287 if (rev) {
2288 if (es->composition_len == 0)
2290 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2291 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2292 SetBkMode( dc, OPAQUE);
2294 else
2296 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2297 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2298 underline_font.lfUnderline = TRUE;
2299 hUnderline = CreateFontIndirectW(&underline_font);
2300 old_font = SelectObject(dc,hUnderline);
2303 li = EDIT_EM_LineIndex(es, line);
2304 if (es->style & ES_MULTILINE) {
2305 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2306 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2307 } else {
2308 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2309 TextOutW(dc, x, y, text + li + col, count);
2310 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2311 ret = size.cx;
2312 if (es->style & ES_PASSWORD)
2313 HeapFree(GetProcessHeap(), 0, text);
2315 if (rev) {
2316 if (es->composition_len == 0)
2318 SetBkColor(dc, BkColor);
2319 SetTextColor(dc, TextColor);
2320 SetBkMode( dc, BkMode);
2322 else
2324 if (old_font)
2325 SelectObject(dc,old_font);
2326 if (hUnderline)
2327 DeleteObject(hUnderline);
2330 return ret;
2334 /*********************************************************************
2336 * EDIT_SetCaretPos
2339 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2340 BOOL after_wrap)
2342 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2343 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2344 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2348 /*********************************************************************
2350 * EDIT_AdjustFormatRect
2352 * Adjusts the format rectangle for the current font and the
2353 * current client rectangle.
2356 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2358 RECT ClientRect;
2360 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2361 if (es->style & ES_MULTILINE)
2363 INT fw, vlc, max_x_offset, max_y_offset;
2365 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2366 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2368 /* correct es->x_offset */
2369 fw = es->format_rect.right - es->format_rect.left;
2370 max_x_offset = es->text_width - fw;
2371 if(max_x_offset < 0) max_x_offset = 0;
2372 if(es->x_offset > max_x_offset)
2373 es->x_offset = max_x_offset;
2375 /* correct es->y_offset */
2376 max_y_offset = es->line_count - vlc;
2377 if(max_y_offset < 0) max_y_offset = 0;
2378 if(es->y_offset > max_y_offset)
2379 es->y_offset = max_y_offset;
2381 /* force scroll info update */
2382 EDIT_UpdateScrollInfo(es);
2384 else
2385 /* Windows doesn't care to fix text placement for SL controls */
2386 es->format_rect.bottom = es->format_rect.top + es->line_height;
2388 /* Always stay within the client area */
2389 GetClientRect(es->hwndSelf, &ClientRect);
2390 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2392 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2393 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2395 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2399 /*********************************************************************
2401 * EDIT_SetRectNP
2403 * note: this is not (exactly) the handler called on EM_SETRECTNP
2404 * it is also used to set the rect of a single line control
2407 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2409 LONG_PTR ExStyle;
2410 INT bw, bh;
2411 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2413 CopyRect(&es->format_rect, rc);
2415 if (ExStyle & WS_EX_CLIENTEDGE) {
2416 es->format_rect.left++;
2417 es->format_rect.right--;
2419 if (es->format_rect.bottom - es->format_rect.top
2420 >= es->line_height + 2)
2422 es->format_rect.top++;
2423 es->format_rect.bottom--;
2426 else if (es->style & WS_BORDER) {
2427 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2428 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2429 es->format_rect.left += bw;
2430 es->format_rect.right -= bw;
2431 if (es->format_rect.bottom - es->format_rect.top
2432 >= es->line_height + 2 * bh)
2434 es->format_rect.top += bh;
2435 es->format_rect.bottom -= bh;
2439 es->format_rect.left += es->left_margin;
2440 es->format_rect.right -= es->right_margin;
2441 EDIT_AdjustFormatRect(es);
2445 /*********************************************************************
2447 * EDIT_UnlockBuffer
2450 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2453 /* Edit window might be already destroyed */
2454 if(!IsWindow(es->hwndSelf))
2456 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2457 return;
2460 if (!es->lock_count) {
2461 ERR("lock_count == 0 ... please report\n");
2462 return;
2464 if (!es->text) {
2465 ERR("es->text == 0 ... please report\n");
2466 return;
2469 if (force || (es->lock_count == 1)) {
2470 if (es->hloc32W) {
2471 CHAR *textA = NULL;
2472 UINT countA = 0;
2473 UINT countW = get_text_length(es) + 1;
2474 STACK16FRAME* stack16 = NULL;
2475 HANDLE16 oldDS = 0;
2477 if(es->hloc32A)
2479 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2480 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2481 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2482 countA = LocalSize(es->hloc32A);
2483 if(countA_new > countA)
2485 HLOCAL hloc32A_new;
2486 UINT alloc_size = ROUND_TO_GROW(countA_new);
2487 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2488 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2489 if(hloc32A_new)
2491 es->hloc32A = hloc32A_new;
2492 countA = LocalSize(hloc32A_new);
2493 TRACE("Real new size %d bytes\n", countA);
2495 else
2496 WARN("FAILED! Will synchronize partially\n");
2498 textA = LocalLock(es->hloc32A);
2500 else if(es->hloc16)
2502 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2504 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2505 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2507 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2508 oldDS = stack16->ds;
2509 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2511 countA = LocalSize16(es->hloc16);
2512 if(countA_new > countA)
2514 HLOCAL16 hloc16_new;
2515 UINT alloc_size = ROUND_TO_GROW(countA_new);
2516 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2517 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2518 if(hloc16_new)
2520 es->hloc16 = hloc16_new;
2521 countA = LocalSize16(hloc16_new);
2522 TRACE("Real new size %d bytes\n", countA);
2524 else
2525 WARN("FAILED! Will synchronize partially\n");
2527 textA = MapSL(LocalLock16(es->hloc16));
2530 if(textA)
2532 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2533 if(stack16)
2534 LocalUnlock16(es->hloc16);
2535 else
2536 LocalUnlock(es->hloc32A);
2539 if (stack16) stack16->ds = oldDS;
2540 LocalUnlock(es->hloc32W);
2541 es->text = NULL;
2543 else {
2544 ERR("no buffer ... please report\n");
2545 return;
2548 es->lock_count--;
2552 /*********************************************************************
2554 * EDIT_UpdateScrollInfo
2557 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2559 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_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->line_count - 1;
2566 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2567 si.nPos = es->y_offset;
2568 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2569 si.nMin, si.nMax, si.nPage, si.nPos);
2570 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2573 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2575 SCROLLINFO si;
2576 si.cbSize = sizeof(SCROLLINFO);
2577 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2578 si.nMin = 0;
2579 si.nMax = es->text_width - 1;
2580 si.nPage = es->format_rect.right - es->format_rect.left;
2581 si.nPos = es->x_offset;
2582 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2583 si.nMin, si.nMax, si.nPage, si.nPos);
2584 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2588 /*********************************************************************
2590 * EDIT_WordBreakProc
2592 * Find the beginning of words.
2593 * Note: unlike the specs for a WordBreakProc, this function only
2594 * allows to be called without linebreaks between s[0] up to
2595 * s[count - 1]. Remember it is only called
2596 * internally, so we can decide this for ourselves.
2599 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2601 INT ret = 0;
2603 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2605 if(!s) return 0;
2607 switch (action) {
2608 case WB_LEFT:
2609 if (!count)
2610 break;
2611 if (index)
2612 index--;
2613 if (s[index] == ' ') {
2614 while (index && (s[index] == ' '))
2615 index--;
2616 if (index) {
2617 while (index && (s[index] != ' '))
2618 index--;
2619 if (s[index] == ' ')
2620 index++;
2622 } else {
2623 while (index && (s[index] != ' '))
2624 index--;
2625 if (s[index] == ' ')
2626 index++;
2628 ret = index;
2629 break;
2630 case WB_RIGHT:
2631 if (!count)
2632 break;
2633 if (index)
2634 index--;
2635 if (s[index] == ' ')
2636 while ((index < count) && (s[index] == ' ')) index++;
2637 else {
2638 while (s[index] && (s[index] != ' ') && (index < count))
2639 index++;
2640 while ((s[index] == ' ') && (index < count)) index++;
2642 ret = index;
2643 break;
2644 case WB_ISDELIMITER:
2645 ret = (s[index] == ' ');
2646 break;
2647 default:
2648 ERR("unknown action code, please report !\n");
2649 break;
2651 return ret;
2655 /*********************************************************************
2657 * EM_CHARFROMPOS
2659 * returns line number (not index) in high-order word of result.
2660 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2661 * to Richedit, not to the edit control. Original documentation is valid.
2662 * FIXME: do the specs mean to return -1 if outside client area or
2663 * if outside formatting rectangle ???
2666 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2668 POINT pt;
2669 RECT rc;
2670 INT index;
2672 pt.x = x;
2673 pt.y = y;
2674 GetClientRect(es->hwndSelf, &rc);
2675 if (!PtInRect(&rc, pt))
2676 return -1;
2678 index = EDIT_CharFromPos(es, x, y, NULL);
2679 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2683 /*********************************************************************
2685 * EM_FMTLINES
2687 * Enable or disable soft breaks.
2689 * This means: insert or remove the soft linebreak character (\r\r\n).
2690 * Take care to check if the text still fits the buffer after insertion.
2691 * If not, notify with EN_ERRSPACE.
2694 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2696 es->flags &= ~EF_USE_SOFTBRK;
2697 if (add_eol) {
2698 es->flags |= EF_USE_SOFTBRK;
2699 FIXME("soft break enabled, not implemented\n");
2701 return add_eol;
2705 /*********************************************************************
2707 * EM_GETHANDLE
2709 * Hopefully this won't fire back at us.
2710 * We always start with a fixed buffer in the local heap.
2711 * Despite of the documentation says that the local heap is used
2712 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2713 * buffer on the local heap.
2716 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2718 HLOCAL hLocal;
2720 if (!(es->style & ES_MULTILINE))
2721 return 0;
2723 if(es->is_unicode)
2724 hLocal = es->hloc32W;
2725 else
2727 if(!es->hloc32A)
2729 CHAR *textA;
2730 UINT countA, alloc_size;
2731 TRACE("Allocating 32-bit ANSI alias buffer\n");
2732 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2733 alloc_size = ROUND_TO_GROW(countA);
2734 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2736 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2737 return 0;
2739 textA = LocalLock(es->hloc32A);
2740 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2741 LocalUnlock(es->hloc32A);
2743 hLocal = es->hloc32A;
2746 es->flags |= EF_APP_HAS_HANDLE;
2747 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2748 return hLocal;
2752 /*********************************************************************
2754 * EM_GETHANDLE16
2756 * Hopefully this won't fire back at us.
2757 * We always start with a buffer in 32 bit linear memory.
2758 * However, with this message a 16 bit application requests
2759 * a handle of 16 bit local heap memory, where it expects to find
2760 * the text.
2761 * It's a pitty that from this moment on we have to use this
2762 * local heap, because applications may rely on the handle
2763 * in the future.
2765 * In this function we'll try to switch to local heap.
2767 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2769 CHAR *textA;
2770 UINT countA, alloc_size;
2771 STACK16FRAME* stack16;
2772 HANDLE16 oldDS;
2774 if (!(es->style & ES_MULTILINE))
2775 return 0;
2777 if (es->hloc16)
2778 return es->hloc16;
2780 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2781 oldDS = stack16->ds;
2782 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2784 if (!LocalHeapSize16()) {
2786 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2787 ERR("could not initialize local heap\n");
2788 goto done;
2790 TRACE("local heap initialized\n");
2793 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2794 alloc_size = ROUND_TO_GROW(countA);
2796 TRACE("Allocating 16-bit ANSI alias buffer\n");
2797 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2798 ERR("could not allocate new 16 bit buffer\n");
2799 goto done;
2802 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2803 ERR("could not lock new 16 bit buffer\n");
2804 LocalFree16(es->hloc16);
2805 es->hloc16 = 0;
2806 goto done;
2809 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2810 LocalUnlock16(es->hloc16);
2811 es->flags |= EF_APP_HAS_HANDLE;
2813 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2815 done:
2816 stack16->ds = oldDS;
2817 return es->hloc16;
2821 /*********************************************************************
2823 * EM_GETLINE
2826 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2828 LPWSTR src;
2829 INT line_len, dst_len;
2830 INT i;
2832 if (es->style & ES_MULTILINE) {
2833 if (line >= es->line_count)
2834 return 0;
2835 } else
2836 line = 0;
2837 i = EDIT_EM_LineIndex(es, line);
2838 src = es->text + i;
2839 line_len = EDIT_EM_LineLength(es, i);
2840 dst_len = *(WORD *)dst;
2841 if(unicode)
2843 if(dst_len <= line_len)
2845 memcpy(dst, src, dst_len * sizeof(WCHAR));
2846 return dst_len;
2848 else /* Append 0 if enough space */
2850 memcpy(dst, src, line_len * sizeof(WCHAR));
2851 dst[line_len] = 0;
2852 return line_len;
2855 else
2857 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2858 if(!ret && line_len) /* Insufficient buffer size */
2859 return dst_len;
2860 if(ret < dst_len) /* Append 0 if enough space */
2861 ((LPSTR)dst)[ret] = 0;
2862 return ret;
2867 /*********************************************************************
2869 * EM_GETSEL
2872 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2874 UINT s = es->selection_start;
2875 UINT e = es->selection_end;
2877 ORDER_UINT(s, e);
2878 if (start)
2879 *start = s;
2880 if (end)
2881 *end = e;
2882 return MAKELONG(s, e);
2886 /*********************************************************************
2888 * EM_GETTHUMB
2890 * FIXME: is this right ? (or should it be only VSCROLL)
2891 * (and maybe only for edit controls that really have their
2892 * own scrollbars) (and maybe only for multiline controls ?)
2893 * All in all: very poorly documented
2896 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2898 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2899 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2903 /*********************************************************************
2905 * EM_LINEFROMCHAR
2908 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2910 INT line;
2911 LINEDEF *line_def;
2913 if (!(es->style & ES_MULTILINE))
2914 return 0;
2915 if (index > (INT)get_text_length(es))
2916 return es->line_count - 1;
2917 if (index == -1)
2918 index = min(es->selection_start, es->selection_end);
2920 line = 0;
2921 line_def = es->first_line_def;
2922 index -= line_def->length;
2923 while ((index >= 0) && line_def->next) {
2924 line++;
2925 line_def = line_def->next;
2926 index -= line_def->length;
2928 return line;
2932 /*********************************************************************
2934 * EM_LINEINDEX
2937 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
2939 INT line_index;
2940 const LINEDEF *line_def;
2942 if (!(es->style & ES_MULTILINE))
2943 return 0;
2944 if (line >= es->line_count)
2945 return -1;
2947 line_index = 0;
2948 line_def = es->first_line_def;
2949 if (line == -1) {
2950 INT index = es->selection_end - line_def->length;
2951 while ((index >= 0) && line_def->next) {
2952 line_index += line_def->length;
2953 line_def = line_def->next;
2954 index -= line_def->length;
2956 } else {
2957 while (line > 0) {
2958 line_index += line_def->length;
2959 line_def = line_def->next;
2960 line--;
2963 return line_index;
2967 /*********************************************************************
2969 * EM_LINELENGTH
2972 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2974 LINEDEF *line_def;
2976 if (!(es->style & ES_MULTILINE))
2977 return get_text_length(es);
2979 if (index == -1) {
2980 /* get the number of remaining non-selected chars of selected lines */
2981 INT32 l; /* line number */
2982 INT32 li; /* index of first char in line */
2983 INT32 count;
2984 l = EDIT_EM_LineFromChar(es, es->selection_start);
2985 /* # chars before start of selection area */
2986 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2987 l = EDIT_EM_LineFromChar(es, es->selection_end);
2988 /* # chars after end of selection */
2989 li = EDIT_EM_LineIndex(es, l);
2990 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2991 return count;
2993 line_def = es->first_line_def;
2994 index -= line_def->length;
2995 while ((index >= 0) && line_def->next) {
2996 line_def = line_def->next;
2997 index -= line_def->length;
2999 return line_def->net_length;
3003 /*********************************************************************
3005 * EM_LINESCROLL
3007 * NOTE: dx is in average character widths, dy - in lines;
3010 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
3012 if (!(es->style & ES_MULTILINE))
3013 return FALSE;
3015 dx *= es->char_width;
3016 return EDIT_EM_LineScroll_internal(es, dx, dy);
3019 /*********************************************************************
3021 * EDIT_EM_LineScroll_internal
3023 * Version of EDIT_EM_LineScroll for internal use.
3024 * It doesn't refuse if ES_MULTILINE is set and assumes that
3025 * dx is in pixels, dy - in lines.
3028 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3030 INT nyoff;
3031 INT x_offset_in_pixels;
3032 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3033 es->line_height;
3035 if (es->style & ES_MULTILINE)
3037 x_offset_in_pixels = es->x_offset;
3039 else
3041 dy = 0;
3042 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3045 if (-dx > x_offset_in_pixels)
3046 dx = -x_offset_in_pixels;
3047 if (dx > es->text_width - x_offset_in_pixels)
3048 dx = es->text_width - x_offset_in_pixels;
3049 nyoff = max(0, es->y_offset + dy);
3050 if (nyoff >= es->line_count - lines_per_page)
3051 nyoff = max(0, es->line_count - lines_per_page);
3052 dy = (es->y_offset - nyoff) * es->line_height;
3053 if (dx || dy) {
3054 RECT rc1;
3055 RECT rc;
3057 es->y_offset = nyoff;
3058 if(es->style & ES_MULTILINE)
3059 es->x_offset += dx;
3060 else
3061 es->x_offset += dx / es->char_width;
3063 GetClientRect(es->hwndSelf, &rc1);
3064 IntersectRect(&rc, &rc1, &es->format_rect);
3065 ScrollWindowEx(es->hwndSelf, -dx, dy,
3066 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3067 /* force scroll info update */
3068 EDIT_UpdateScrollInfo(es);
3070 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3071 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3072 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3073 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3074 return TRUE;
3078 /*********************************************************************
3080 * EM_POSFROMCHAR
3083 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3085 INT len = get_text_length(es);
3086 INT l;
3087 INT li;
3088 INT x;
3089 INT y = 0;
3090 INT w;
3091 INT lw = 0;
3092 INT ll = 0;
3093 HDC dc;
3094 HFONT old_font = 0;
3095 SIZE size;
3096 LINEDEF *line_def;
3098 index = min(index, len);
3099 dc = GetDC(es->hwndSelf);
3100 if (es->font)
3101 old_font = SelectObject(dc, es->font);
3102 if (es->style & ES_MULTILINE) {
3103 l = EDIT_EM_LineFromChar(es, index);
3104 y = (l - es->y_offset) * es->line_height;
3105 li = EDIT_EM_LineIndex(es, l);
3106 if (after_wrap && (li == index) && l) {
3107 INT l2 = l - 1;
3108 line_def = es->first_line_def;
3109 while (l2) {
3110 line_def = line_def->next;
3111 l2--;
3113 if (line_def->ending == END_WRAP) {
3114 l--;
3115 y -= es->line_height;
3116 li = EDIT_EM_LineIndex(es, l);
3120 line_def = es->first_line_def;
3121 while (line_def->index != li)
3122 line_def = line_def->next;
3124 ll = line_def->net_length;
3125 lw = line_def->width;
3127 w = es->format_rect.right - es->format_rect.left;
3128 if (es->style & ES_RIGHT)
3130 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3131 es->tabs_count, es->tabs)) - es->x_offset;
3132 x = w - x;
3134 else if (es->style & ES_CENTER)
3136 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3137 es->tabs_count, es->tabs)) - es->x_offset;
3138 x += (w - lw) / 2;
3140 else /* ES_LEFT */
3142 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3143 es->tabs_count, es->tabs)) - es->x_offset;
3145 } else {
3146 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3147 if (index < es->x_offset) {
3148 GetTextExtentPoint32W(dc, text + index,
3149 es->x_offset - index, &size);
3150 x = -size.cx;
3151 } else {
3152 GetTextExtentPoint32W(dc, text + es->x_offset,
3153 index - es->x_offset, &size);
3154 x = size.cx;
3156 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3158 w = es->format_rect.right - es->format_rect.left;
3159 if (w > es->text_width)
3161 if (es->style & ES_RIGHT)
3162 x += w - es->text_width;
3163 else if (es->style & ES_CENTER)
3164 x += (w - es->text_width) / 2;
3168 y = 0;
3169 if (es->style & ES_PASSWORD)
3170 HeapFree(GetProcessHeap(), 0, text);
3172 x += es->format_rect.left;
3173 y += es->format_rect.top;
3174 if (es->font)
3175 SelectObject(dc, old_font);
3176 ReleaseDC(es->hwndSelf, dc);
3177 return MAKELONG((INT16)x, (INT16)y);
3181 /*********************************************************************
3183 * EM_REPLACESEL
3185 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3188 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3190 UINT strl = strlenW(lpsz_replace);
3191 UINT tl = get_text_length(es);
3192 UINT utl;
3193 UINT s;
3194 UINT e;
3195 UINT i;
3196 UINT size;
3197 LPWSTR p;
3198 HRGN hrgn = 0;
3199 LPWSTR buf = NULL;
3200 UINT bufl = 0;
3202 TRACE("%s, can_undo %d, send_update %d\n",
3203 debugstr_w(lpsz_replace), can_undo, send_update);
3205 s = es->selection_start;
3206 e = es->selection_end;
3208 if ((s == e) && !strl)
3209 return;
3211 ORDER_UINT(s, e);
3213 size = tl - (e - s) + strl;
3214 if (!size)
3215 es->text_width = 0;
3217 /* Issue the EN_MAXTEXT notification and continue with replacing text
3218 * such that buffer limit is honored. */
3219 if ((honor_limit) && (size > es->buffer_limit)) {
3220 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3221 /* Buffer limit can be smaller than the actual length of text in combobox */
3222 if (es->buffer_limit < (tl - (e-s)))
3223 strl = 0;
3224 else
3225 strl = es->buffer_limit - (tl - (e-s));
3228 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3229 return;
3231 if (e != s) {
3232 /* there is something to be deleted */
3233 TRACE("deleting stuff.\n");
3234 bufl = e - s;
3235 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3236 if (!buf) return;
3237 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3238 buf[bufl] = 0; /* ensure 0 termination */
3239 /* now delete */
3240 strcpyW(es->text + s, es->text + e);
3241 text_buffer_changed(es);
3243 if (strl) {
3244 /* there is an insertion */
3245 tl = get_text_length(es);
3246 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));
3247 for (p = es->text + tl ; p >= es->text + s ; p--)
3248 p[strl] = p[0];
3249 for (i = 0 , p = es->text + s ; i < strl ; i++)
3250 p[i] = lpsz_replace[i];
3251 if(es->style & ES_UPPERCASE)
3252 CharUpperBuffW(p, strl);
3253 else if(es->style & ES_LOWERCASE)
3254 CharLowerBuffW(p, strl);
3255 text_buffer_changed(es);
3257 if (es->style & ES_MULTILINE)
3259 INT st = min(es->selection_start, es->selection_end);
3260 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3262 hrgn = CreateRectRgn(0, 0, 0, 0);
3263 EDIT_BuildLineDefs_ML(es, st, st + strl,
3264 strl - abs(es->selection_end - es->selection_start), hrgn);
3265 /* if text is too long undo all changes */
3266 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3267 if (strl)
3268 strcpyW(es->text + e, es->text + e + strl);
3269 if (e != s)
3270 for (i = 0 , p = es->text ; i < e - s ; i++)
3271 p[i + s] = buf[i];
3272 text_buffer_changed(es);
3273 EDIT_BuildLineDefs_ML(es, s, e,
3274 abs(es->selection_end - es->selection_start) - strl, hrgn);
3275 strl = 0;
3276 e = s;
3277 hrgn = CreateRectRgn(0, 0, 0, 0);
3278 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3281 else {
3282 INT fw = es->format_rect.right - es->format_rect.left;
3283 EDIT_CalcLineWidth_SL(es);
3284 /* remove chars that don't fit */
3285 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3286 while ((es->text_width > fw) && s + strl >= s) {
3287 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3288 strl--;
3289 EDIT_CalcLineWidth_SL(es);
3291 text_buffer_changed(es);
3292 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3296 if (e != s) {
3297 if (can_undo) {
3298 utl = strlenW(es->undo_text);
3299 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3300 /* undo-buffer is extended to the right */
3301 EDIT_MakeUndoFit(es, utl + e - s);
3302 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3303 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3304 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3305 /* undo-buffer is extended to the left */
3306 EDIT_MakeUndoFit(es, utl + e - s);
3307 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3308 p[e - s] = p[0];
3309 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3310 p[i] = buf[i];
3311 es->undo_position = s;
3312 } else {
3313 /* new undo-buffer */
3314 EDIT_MakeUndoFit(es, e - s);
3315 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3316 es->undo_text[e - s] = 0; /* ensure 0 termination */
3317 es->undo_position = s;
3319 /* any deletion makes the old insertion-undo invalid */
3320 es->undo_insert_count = 0;
3321 } else
3322 EDIT_EM_EmptyUndoBuffer(es);
3324 if (strl) {
3325 if (can_undo) {
3326 if ((s == es->undo_position) ||
3327 ((es->undo_insert_count) &&
3328 (s == es->undo_position + es->undo_insert_count)))
3330 * insertion is new and at delete position or
3331 * an extension to either left or right
3333 es->undo_insert_count += strl;
3334 else {
3335 /* new insertion undo */
3336 es->undo_position = s;
3337 es->undo_insert_count = strl;
3338 /* new insertion makes old delete-buffer invalid */
3339 *es->undo_text = '\0';
3341 } else
3342 EDIT_EM_EmptyUndoBuffer(es);
3345 if (bufl)
3346 HeapFree(GetProcessHeap(), 0, buf);
3348 s += strl;
3350 /* If text has been deleted and we're right or center aligned then scroll rightward */
3351 if (es->style & (ES_RIGHT | ES_CENTER))
3353 INT delta = strl - abs(es->selection_end - es->selection_start);
3355 if (delta < 0 && es->x_offset)
3357 if (abs(delta) > es->x_offset)
3358 es->x_offset = 0;
3359 else
3360 es->x_offset += delta;
3364 EDIT_EM_SetSel(es, s, s, FALSE);
3365 es->flags |= EF_MODIFIED;
3366 if (send_update) es->flags |= EF_UPDATE;
3367 if (hrgn)
3369 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3370 DeleteObject(hrgn);
3372 else
3373 EDIT_UpdateText(es, NULL, TRUE);
3375 EDIT_EM_ScrollCaret(es);
3377 /* force scroll info update */
3378 EDIT_UpdateScrollInfo(es);
3381 if(send_update || (es->flags & EF_UPDATE))
3383 es->flags &= ~EF_UPDATE;
3384 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3389 /*********************************************************************
3391 * EM_SCROLL
3394 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3396 INT dy;
3398 if (!(es->style & ES_MULTILINE))
3399 return (LRESULT)FALSE;
3401 dy = 0;
3403 switch (action) {
3404 case SB_LINEUP:
3405 if (es->y_offset)
3406 dy = -1;
3407 break;
3408 case SB_LINEDOWN:
3409 if (es->y_offset < es->line_count - 1)
3410 dy = 1;
3411 break;
3412 case SB_PAGEUP:
3413 if (es->y_offset)
3414 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3415 break;
3416 case SB_PAGEDOWN:
3417 if (es->y_offset < es->line_count - 1)
3418 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3419 break;
3420 default:
3421 return (LRESULT)FALSE;
3423 if (dy) {
3424 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3425 /* check if we are going to move too far */
3426 if(es->y_offset + dy > es->line_count - vlc)
3427 dy = es->line_count - vlc - es->y_offset;
3429 /* Notification is done in EDIT_EM_LineScroll */
3430 if(dy)
3431 EDIT_EM_LineScroll(es, 0, dy);
3433 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3437 /*********************************************************************
3439 * EM_SCROLLCARET
3442 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3444 if (es->style & ES_MULTILINE) {
3445 INT l;
3446 INT li;
3447 INT vlc;
3448 INT ww;
3449 INT cw = es->char_width;
3450 INT x;
3451 INT dy = 0;
3452 INT dx = 0;
3454 l = EDIT_EM_LineFromChar(es, es->selection_end);
3455 li = EDIT_EM_LineIndex(es, l);
3456 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3457 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3458 if (l >= es->y_offset + vlc)
3459 dy = l - vlc + 1 - es->y_offset;
3460 if (l < es->y_offset)
3461 dy = l - es->y_offset;
3462 ww = es->format_rect.right - es->format_rect.left;
3463 if (x < es->format_rect.left)
3464 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3465 if (x > es->format_rect.right)
3466 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3467 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3469 /* check if we are going to move too far */
3470 if(es->x_offset + dx + ww > es->text_width)
3471 dx = es->text_width - ww - es->x_offset;
3472 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3473 EDIT_EM_LineScroll_internal(es, dx, dy);
3475 } else {
3476 INT x;
3477 INT goal;
3478 INT format_width;
3480 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3481 format_width = es->format_rect.right - es->format_rect.left;
3482 if (x < es->format_rect.left) {
3483 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3484 do {
3485 es->x_offset--;
3486 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3487 } while ((x < goal) && es->x_offset);
3488 /* FIXME: use ScrollWindow() somehow to improve performance */
3489 EDIT_UpdateText(es, NULL, TRUE);
3490 } else if (x > es->format_rect.right) {
3491 INT x_last;
3492 INT len = get_text_length(es);
3493 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3494 do {
3495 es->x_offset++;
3496 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3497 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3498 } while ((x > goal) && (x_last > es->format_rect.right));
3499 /* FIXME: use ScrollWindow() somehow to improve performance */
3500 EDIT_UpdateText(es, NULL, TRUE);
3504 if(es->flags & EF_FOCUSED)
3505 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3509 /*********************************************************************
3511 * EM_SETHANDLE
3513 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3516 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3518 if (!(es->style & ES_MULTILINE))
3519 return;
3521 if (!hloc) {
3522 WARN("called with NULL handle\n");
3523 return;
3526 EDIT_UnlockBuffer(es, TRUE);
3528 if(es->hloc16)
3530 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3531 HANDLE16 oldDS = stack16->ds;
3533 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3534 LocalFree16(es->hloc16);
3535 stack16->ds = oldDS;
3536 es->hloc16 = 0;
3539 if(es->is_unicode)
3541 if(es->hloc32A)
3543 LocalFree(es->hloc32A);
3544 es->hloc32A = NULL;
3546 es->hloc32W = hloc;
3548 else
3550 INT countW, countA;
3551 HLOCAL hloc32W_new;
3552 WCHAR *textW;
3553 CHAR *textA;
3555 countA = LocalSize(hloc);
3556 textA = LocalLock(hloc);
3557 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3558 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3560 ERR("Could not allocate new unicode buffer\n");
3561 return;
3563 textW = LocalLock(hloc32W_new);
3564 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3565 LocalUnlock(hloc32W_new);
3566 LocalUnlock(hloc);
3568 if(es->hloc32W)
3569 LocalFree(es->hloc32W);
3571 es->hloc32W = hloc32W_new;
3572 es->hloc32A = hloc;
3575 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3577 es->flags |= EF_APP_HAS_HANDLE;
3578 EDIT_LockBuffer(es);
3580 es->x_offset = es->y_offset = 0;
3581 es->selection_start = es->selection_end = 0;
3582 EDIT_EM_EmptyUndoBuffer(es);
3583 es->flags &= ~EF_MODIFIED;
3584 es->flags &= ~EF_UPDATE;
3585 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3586 EDIT_UpdateText(es, NULL, TRUE);
3587 EDIT_EM_ScrollCaret(es);
3588 /* force scroll info update */
3589 EDIT_UpdateScrollInfo(es);
3593 /*********************************************************************
3595 * EM_SETHANDLE16
3597 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3600 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3602 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
3603 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3604 HANDLE16 oldDS = stack16->ds;
3605 INT countW, countA;
3606 HLOCAL hloc32W_new;
3607 WCHAR *textW;
3608 CHAR *textA;
3610 if (!(es->style & ES_MULTILINE))
3611 return;
3613 if (!hloc) {
3614 WARN("called with NULL handle\n");
3615 return;
3618 EDIT_UnlockBuffer(es, TRUE);
3620 if(es->hloc32A)
3622 LocalFree(es->hloc32A);
3623 es->hloc32A = NULL;
3626 stack16->ds = hInstance;
3627 countA = LocalSize16(hloc);
3628 textA = MapSL(LocalLock16(hloc));
3629 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3630 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3632 ERR("Could not allocate new unicode buffer\n");
3633 return;
3635 textW = LocalLock(hloc32W_new);
3636 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3637 LocalUnlock(hloc32W_new);
3638 LocalUnlock16(hloc);
3639 stack16->ds = oldDS;
3641 if(es->hloc32W)
3642 LocalFree(es->hloc32W);
3644 es->hloc32W = hloc32W_new;
3645 es->hloc16 = hloc;
3647 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3649 es->flags |= EF_APP_HAS_HANDLE;
3650 EDIT_LockBuffer(es);
3652 es->x_offset = es->y_offset = 0;
3653 es->selection_start = es->selection_end = 0;
3654 EDIT_EM_EmptyUndoBuffer(es);
3655 es->flags &= ~EF_MODIFIED;
3656 es->flags &= ~EF_UPDATE;
3657 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3658 EDIT_UpdateText(es, NULL, TRUE);
3659 EDIT_EM_ScrollCaret(es);
3660 /* force scroll info update */
3661 EDIT_UpdateScrollInfo(es);
3665 /*********************************************************************
3667 * EM_SETLIMITTEXT
3669 * NOTE: this version currently implements WinNT limits
3672 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
3674 if (!limit) limit = ~0u;
3675 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
3676 es->buffer_limit = limit;
3680 /*********************************************************************
3682 * EM_SETMARGINS
3684 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3685 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3686 * margin according to the textmetrics of the current font.
3688 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3689 * of the char's width as the margin, but this is not how Windows handles this.
3690 * For all other fonts Windows sets the margins to zero.
3692 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3693 * edit control is equal to or larger than a certain size.
3694 * Interestingly if one subtracts both the left and right margins from
3695 * this size one always seems to get an even number. The extents of
3696 * the (four character) string "'**'" match this quite closely, so
3697 * we'll use this until we come up with a better idea.
3699 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3701 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3702 SIZE sz;
3704 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3705 return sz.cx + left + right;
3708 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3709 WORD left, WORD right, BOOL repaint)
3711 TEXTMETRICW tm;
3712 INT default_left_margin = 0; /* in pixels */
3713 INT default_right_margin = 0; /* in pixels */
3715 /* Set the default margins depending on the font */
3716 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3717 HDC dc = GetDC(es->hwndSelf);
3718 HFONT old_font = SelectObject(dc, es->font);
3719 GetTextMetricsW(dc, &tm);
3720 /* The default margins are only non zero for TrueType or Vector fonts */
3721 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3722 int min_size;
3723 RECT rc;
3724 /* This must be calculated more exactly! But how? */
3725 default_left_margin = tm.tmAveCharWidth / 2;
3726 default_right_margin = tm.tmAveCharWidth / 2;
3727 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3728 GetClientRect(es->hwndSelf, &rc);
3729 if(rc.right - rc.left < min_size) {
3730 default_left_margin = es->left_margin;
3731 default_right_margin = es->right_margin;
3734 SelectObject(dc, old_font);
3735 ReleaseDC(es->hwndSelf, dc);
3738 if (action & EC_LEFTMARGIN) {
3739 es->format_rect.left -= es->left_margin;
3740 if (left != EC_USEFONTINFO)
3741 es->left_margin = left;
3742 else
3743 es->left_margin = default_left_margin;
3744 es->format_rect.left += es->left_margin;
3747 if (action & EC_RIGHTMARGIN) {
3748 es->format_rect.right += es->right_margin;
3749 if (right != EC_USEFONTINFO)
3750 es->right_margin = right;
3751 else
3752 es->right_margin = default_right_margin;
3753 es->format_rect.right -= es->right_margin;
3756 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3757 EDIT_AdjustFormatRect(es);
3758 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3761 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3765 /*********************************************************************
3767 * EM_SETPASSWORDCHAR
3770 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3772 LONG style;
3774 if (es->style & ES_MULTILINE)
3775 return;
3777 if (es->password_char == c)
3778 return;
3780 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3781 es->password_char = c;
3782 if (c) {
3783 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3784 es->style |= ES_PASSWORD;
3785 } else {
3786 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3787 es->style &= ~ES_PASSWORD;
3789 EDIT_UpdateText(es, NULL, TRUE);
3793 /*********************************************************************
3795 * EDIT_EM_SetSel
3797 * note: unlike the specs say: the order of start and end
3798 * _is_ preserved in Windows. (i.e. start can be > end)
3799 * In other words: this handler is OK
3802 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3804 UINT old_start = es->selection_start;
3805 UINT old_end = es->selection_end;
3806 UINT len = get_text_length(es);
3808 if (start == (UINT)-1) {
3809 start = es->selection_end;
3810 end = es->selection_end;
3811 } else {
3812 start = min(start, len);
3813 end = min(end, len);
3815 es->selection_start = start;
3816 es->selection_end = end;
3817 if (after_wrap)
3818 es->flags |= EF_AFTER_WRAP;
3819 else
3820 es->flags &= ~EF_AFTER_WRAP;
3821 /* Compute the necessary invalidation region. */
3822 /* Note that we don't need to invalidate regions which have
3823 * "never" been selected, or those which are "still" selected.
3824 * In fact, every time we hit a selection boundary, we can
3825 * *toggle* whether we need to invalidate. Thus we can optimize by
3826 * *sorting* the interval endpoints. Let's assume that we sort them
3827 * in this order:
3828 * start <= end <= old_start <= old_end
3829 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3830 * in 5 comparisons; ie it's impossible to do better than the
3831 * following: */
3832 ORDER_UINT(end, old_end);
3833 ORDER_UINT(start, old_start);
3834 ORDER_UINT(old_start, old_end);
3835 ORDER_UINT(start, end);
3836 /* Note that at this point 'end' and 'old_start' are not in order, but
3837 * start is definitely the min. and old_end is definitely the max. */
3838 if (end != old_start)
3841 * One can also do
3842 * ORDER_UINT32(end, old_start);
3843 * EDIT_InvalidateText(es, start, end);
3844 * EDIT_InvalidateText(es, old_start, old_end);
3845 * in place of the following if statement.
3846 * (That would complete the optimal five-comparison four-element sort.)
3848 if (old_start > end )
3850 EDIT_InvalidateText(es, start, end);
3851 EDIT_InvalidateText(es, old_start, old_end);
3853 else
3855 EDIT_InvalidateText(es, start, old_start);
3856 EDIT_InvalidateText(es, end, old_end);
3859 else EDIT_InvalidateText(es, start, old_end);
3863 /*********************************************************************
3865 * EM_SETTABSTOPS
3868 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3870 if (!(es->style & ES_MULTILINE))
3871 return FALSE;
3872 HeapFree(GetProcessHeap(), 0, es->tabs);
3873 es->tabs_count = count;
3874 if (!count)
3875 es->tabs = NULL;
3876 else {
3877 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3878 memcpy(es->tabs, tabs, count * sizeof(INT));
3880 return TRUE;
3884 /*********************************************************************
3886 * EM_SETTABSTOPS16
3889 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs)
3891 if (!(es->style & ES_MULTILINE))
3892 return FALSE;
3893 HeapFree(GetProcessHeap(), 0, es->tabs);
3894 es->tabs_count = count;
3895 if (!count)
3896 es->tabs = NULL;
3897 else {
3898 INT i;
3899 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3900 for (i = 0 ; i < count ; i++)
3901 es->tabs[i] = *tabs++;
3903 return TRUE;
3907 /*********************************************************************
3909 * EM_SETWORDBREAKPROC
3912 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3914 if (es->word_break_proc == wbp)
3915 return;
3917 es->word_break_proc = wbp;
3918 es->word_break_proc16 = NULL;
3920 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3921 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3922 EDIT_UpdateText(es, NULL, TRUE);
3927 /*********************************************************************
3929 * EM_SETWORDBREAKPROC16
3932 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3934 if (es->word_break_proc16 == wbp)
3935 return;
3937 es->word_break_proc = NULL;
3938 es->word_break_proc16 = wbp;
3939 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3940 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3941 EDIT_UpdateText(es, NULL, TRUE);
3946 /*********************************************************************
3948 * EM_UNDO / WM_UNDO
3951 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3953 INT ulength;
3954 LPWSTR utext;
3956 /* As per MSDN spec, for a single-line edit control,
3957 the return value is always TRUE */
3958 if( es->style & ES_READONLY )
3959 return !(es->style & ES_MULTILINE);
3961 ulength = strlenW(es->undo_text);
3963 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3965 strcpyW(utext, es->undo_text);
3967 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3968 es->undo_insert_count, debugstr_w(utext));
3970 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3971 EDIT_EM_EmptyUndoBuffer(es);
3972 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3973 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3974 /* send the notification after the selection start and end are set */
3975 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3976 EDIT_EM_ScrollCaret(es);
3977 HeapFree(GetProcessHeap(), 0, utext);
3979 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3980 es->undo_insert_count, debugstr_w(es->undo_text));
3981 return TRUE;
3985 /*********************************************************************
3987 * WM_CHAR
3990 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3992 BOOL control;
3994 control = GetKeyState(VK_CONTROL) & 0x8000;
3996 switch (c) {
3997 case '\r':
3998 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3999 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
4000 break;
4001 case '\n':
4002 if (es->style & ES_MULTILINE) {
4003 if (es->style & ES_READONLY) {
4004 EDIT_MoveHome(es, FALSE);
4005 EDIT_MoveDown_ML(es, FALSE);
4006 } else {
4007 static const WCHAR cr_lfW[] = {'\r','\n',0};
4008 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
4011 break;
4012 case '\t':
4013 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4015 static const WCHAR tabW[] = {'\t',0};
4016 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4018 break;
4019 case VK_BACK:
4020 if (!(es->style & ES_READONLY) && !control) {
4021 if (es->selection_start != es->selection_end)
4022 EDIT_WM_Clear(es);
4023 else {
4024 /* delete character left of caret */
4025 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4026 EDIT_MoveBackward(es, TRUE);
4027 EDIT_WM_Clear(es);
4030 break;
4031 case 0x03: /* ^C */
4032 if (!(es->style & ES_PASSWORD))
4033 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4034 break;
4035 case 0x16: /* ^V */
4036 if (!(es->style & ES_READONLY))
4037 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4038 break;
4039 case 0x18: /* ^X */
4040 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
4041 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4042 break;
4043 case 0x1A: /* ^Z */
4044 if (!(es->style & ES_READONLY))
4045 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
4046 break;
4048 default:
4049 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4050 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4051 break;
4053 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4054 WCHAR str[2];
4055 str[0] = c;
4056 str[1] = '\0';
4057 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4059 break;
4064 /*********************************************************************
4066 * WM_COMMAND
4069 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4071 if (code || control)
4072 return;
4074 switch (id) {
4075 case EM_UNDO:
4076 EDIT_EM_Undo(es);
4077 break;
4078 case WM_CUT:
4079 EDIT_WM_Cut(es);
4080 break;
4081 case WM_COPY:
4082 EDIT_WM_Copy(es);
4083 break;
4084 case WM_PASTE:
4085 EDIT_WM_Paste(es);
4086 break;
4087 case WM_CLEAR:
4088 EDIT_WM_Clear(es);
4089 break;
4090 case EM_SETSEL:
4091 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4092 EDIT_EM_ScrollCaret(es);
4093 break;
4094 default:
4095 ERR("unknown menu item, please report\n");
4096 break;
4101 /*********************************************************************
4103 * WM_CONTEXTMENU
4105 * Note: the resource files resource/sysres_??.rc cannot define a
4106 * single popup menu. Hence we use a (dummy) menubar
4107 * containing the single popup menu as its first item.
4109 * FIXME: the message identifiers have been chosen arbitrarily,
4110 * hence we use MF_BYPOSITION.
4111 * We might as well use the "real" values (anybody knows ?)
4112 * The menu definition is in resources/sysres_??.rc.
4113 * Once these are OK, we better use MF_BYCOMMAND here
4114 * (as we do in EDIT_WM_Command()).
4117 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4119 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4120 HMENU popup = GetSubMenu(menu, 0);
4121 UINT start = es->selection_start;
4122 UINT end = es->selection_end;
4124 ORDER_UINT(start, end);
4126 /* undo */
4127 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4128 /* cut */
4129 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4130 /* copy */
4131 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4132 /* paste */
4133 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4134 /* delete */
4135 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4136 /* select all */
4137 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4139 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4141 RECT rc;
4142 /* Windows places the menu at the edit's center in this case */
4143 GetClientRect(es->hwndSelf, &rc);
4144 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4145 x = rc.left + (rc.right - rc.left) / 2;
4146 y = rc.top + (rc.bottom - rc.top) / 2;
4149 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4150 DestroyMenu(menu);
4154 /*********************************************************************
4156 * WM_COPY
4159 static void EDIT_WM_Copy(EDITSTATE *es)
4161 INT s = min(es->selection_start, es->selection_end);
4162 INT e = max(es->selection_start, es->selection_end);
4163 HGLOBAL hdst;
4164 LPWSTR dst;
4165 DWORD len;
4167 if (e == s) return;
4169 len = e - s;
4170 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4171 dst = GlobalLock(hdst);
4172 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4173 dst[len] = 0; /* ensure 0 termination */
4174 TRACE("%s\n", debugstr_w(dst));
4175 GlobalUnlock(hdst);
4176 OpenClipboard(es->hwndSelf);
4177 EmptyClipboard();
4178 SetClipboardData(CF_UNICODETEXT, hdst);
4179 CloseClipboard();
4183 /*********************************************************************
4185 * WM_CREATE
4188 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4190 RECT clientRect;
4192 TRACE("%s\n", debugstr_w(name));
4194 * To initialize some final structure members, we call some helper
4195 * functions. However, since the EDITSTATE is not consistent (i.e.
4196 * not fully initialized), we should be very careful which
4197 * functions can be called, and in what order.
4199 EDIT_WM_SetFont(es, 0, FALSE);
4200 EDIT_EM_EmptyUndoBuffer(es);
4202 /* We need to calculate the format rect
4203 (applications may send EM_SETMARGINS before the control gets visible) */
4204 GetClientRect(es->hwndSelf, &clientRect);
4205 EDIT_SetRectNP(es, &clientRect);
4207 if (name && *name) {
4208 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4209 /* if we insert text to the editline, the text scrolls out
4210 * of the window, as the caret is placed after the insert
4211 * pos normally; thus we reset es->selection... to 0 and
4212 * update caret
4214 es->selection_start = es->selection_end = 0;
4215 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4216 * Messages are only to be sent when the USER does something to
4217 * change the contents. So I am removing this EN_CHANGE
4219 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4221 EDIT_EM_ScrollCaret(es);
4223 /* force scroll info update */
4224 EDIT_UpdateScrollInfo(es);
4225 /* The rule seems to return 1 here for success */
4226 /* Power Builder masked edit controls will crash */
4227 /* if not. */
4228 /* FIXME: is that in all cases so ? */
4229 return 1;
4233 /*********************************************************************
4235 * WM_DESTROY
4238 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4240 LINEDEF *pc, *pp;
4242 if (es->hloc32W) {
4243 while (LocalUnlock(es->hloc32W)) ;
4244 LocalFree(es->hloc32W);
4246 if (es->hloc32A) {
4247 while (LocalUnlock(es->hloc32A)) ;
4248 LocalFree(es->hloc32A);
4250 if (es->hloc16) {
4251 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4252 HANDLE16 oldDS = stack16->ds;
4254 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4255 while (LocalUnlock16(es->hloc16)) ;
4256 LocalFree16(es->hloc16);
4257 stack16->ds = oldDS;
4260 pc = es->first_line_def;
4261 while (pc)
4263 pp = pc->next;
4264 HeapFree(GetProcessHeap(), 0, pc);
4265 pc = pp;
4268 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4269 HeapFree(GetProcessHeap(), 0, es);
4271 return 0;
4275 /*********************************************************************
4277 * WM_GETTEXT
4280 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4282 if(!count) return 0;
4284 if(unicode)
4286 lstrcpynW(dst, es->text, count);
4287 return strlenW(dst);
4289 else
4291 LPSTR textA = (LPSTR)dst;
4292 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4293 textA[count - 1] = 0; /* ensure 0 termination */
4294 return strlen(textA);
4298 /*********************************************************************
4300 * WM_HSCROLL
4303 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4305 INT dx;
4306 INT fw;
4308 if (!(es->style & ES_MULTILINE))
4309 return 0;
4311 if (!(es->style & ES_AUTOHSCROLL))
4312 return 0;
4314 dx = 0;
4315 fw = es->format_rect.right - es->format_rect.left;
4316 switch (action) {
4317 case SB_LINELEFT:
4318 TRACE("SB_LINELEFT\n");
4319 if (es->x_offset)
4320 dx = -es->char_width;
4321 break;
4322 case SB_LINERIGHT:
4323 TRACE("SB_LINERIGHT\n");
4324 if (es->x_offset < es->text_width)
4325 dx = es->char_width;
4326 break;
4327 case SB_PAGELEFT:
4328 TRACE("SB_PAGELEFT\n");
4329 if (es->x_offset)
4330 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4331 break;
4332 case SB_PAGERIGHT:
4333 TRACE("SB_PAGERIGHT\n");
4334 if (es->x_offset < es->text_width)
4335 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4336 break;
4337 case SB_LEFT:
4338 TRACE("SB_LEFT\n");
4339 if (es->x_offset)
4340 dx = -es->x_offset;
4341 break;
4342 case SB_RIGHT:
4343 TRACE("SB_RIGHT\n");
4344 if (es->x_offset < es->text_width)
4345 dx = es->text_width - es->x_offset;
4346 break;
4347 case SB_THUMBTRACK:
4348 TRACE("SB_THUMBTRACK %d\n", pos);
4349 es->flags |= EF_HSCROLL_TRACK;
4350 if(es->style & WS_HSCROLL)
4351 dx = pos - es->x_offset;
4352 else
4354 INT fw, new_x;
4355 /* Sanity check */
4356 if(pos < 0 || pos > 100) return 0;
4357 /* Assume default scroll range 0-100 */
4358 fw = es->format_rect.right - es->format_rect.left;
4359 new_x = pos * (es->text_width - fw) / 100;
4360 dx = es->text_width ? (new_x - es->x_offset) : 0;
4362 break;
4363 case SB_THUMBPOSITION:
4364 TRACE("SB_THUMBPOSITION %d\n", pos);
4365 es->flags &= ~EF_HSCROLL_TRACK;
4366 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4367 dx = pos - es->x_offset;
4368 else
4370 INT fw, new_x;
4371 /* Sanity check */
4372 if(pos < 0 || pos > 100) return 0;
4373 /* Assume default scroll range 0-100 */
4374 fw = es->format_rect.right - es->format_rect.left;
4375 new_x = pos * (es->text_width - fw) / 100;
4376 dx = es->text_width ? (new_x - es->x_offset) : 0;
4378 if (!dx) {
4379 /* force scroll info update */
4380 EDIT_UpdateScrollInfo(es);
4381 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4383 break;
4384 case SB_ENDSCROLL:
4385 TRACE("SB_ENDSCROLL\n");
4386 break;
4388 * FIXME : the next two are undocumented !
4389 * Are we doing the right thing ?
4390 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4391 * although it's also a regular control message.
4393 case EM_GETTHUMB: /* this one is used by NT notepad */
4394 case EM_GETTHUMB16:
4396 LRESULT ret;
4397 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4398 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4399 else
4401 /* Assume default scroll range 0-100 */
4402 INT fw = es->format_rect.right - es->format_rect.left;
4403 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4405 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4406 return ret;
4408 case EM_LINESCROLL16:
4409 TRACE("EM_LINESCROLL16\n");
4410 dx = pos;
4411 break;
4413 default:
4414 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4415 action, action);
4416 return 0;
4418 if (dx)
4420 INT fw = es->format_rect.right - es->format_rect.left;
4421 /* check if we are going to move too far */
4422 if(es->x_offset + dx + fw > es->text_width)
4423 dx = es->text_width - fw - es->x_offset;
4424 if(dx)
4425 EDIT_EM_LineScroll_internal(es, dx, 0);
4427 return 0;
4431 /*********************************************************************
4433 * EDIT_CheckCombo
4436 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4438 HWND hLBox = es->hwndListBox;
4439 HWND hCombo;
4440 BOOL bDropped;
4441 int nEUI;
4443 if (!hLBox)
4444 return FALSE;
4446 hCombo = GetParent(es->hwndSelf);
4447 bDropped = TRUE;
4448 nEUI = 0;
4450 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4452 if (key == VK_UP || key == VK_DOWN)
4454 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4455 nEUI = 1;
4457 if (msg == WM_KEYDOWN || nEUI)
4458 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4461 switch (msg)
4463 case WM_KEYDOWN:
4464 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4466 /* make sure ComboLBox pops up */
4467 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4468 key = VK_F4;
4469 nEUI = 2;
4472 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4473 break;
4475 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4476 if (nEUI)
4477 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4478 else
4479 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4480 break;
4483 if(nEUI == 2)
4484 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4486 return TRUE;
4490 /*********************************************************************
4492 * WM_KEYDOWN
4494 * Handling of special keys that don't produce a WM_CHAR
4495 * (i.e. non-printable keys) & Backspace & Delete
4498 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4500 BOOL shift;
4501 BOOL control;
4503 if (GetKeyState(VK_MENU) & 0x8000)
4504 return 0;
4506 shift = GetKeyState(VK_SHIFT) & 0x8000;
4507 control = GetKeyState(VK_CONTROL) & 0x8000;
4509 switch (key) {
4510 case VK_F4:
4511 case VK_UP:
4512 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4513 break;
4515 /* fall through */
4516 case VK_LEFT:
4517 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4518 EDIT_MoveUp_ML(es, shift);
4519 else
4520 if (control)
4521 EDIT_MoveWordBackward(es, shift);
4522 else
4523 EDIT_MoveBackward(es, shift);
4524 break;
4525 case VK_DOWN:
4526 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4527 break;
4528 /* fall through */
4529 case VK_RIGHT:
4530 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4531 EDIT_MoveDown_ML(es, shift);
4532 else if (control)
4533 EDIT_MoveWordForward(es, shift);
4534 else
4535 EDIT_MoveForward(es, shift);
4536 break;
4537 case VK_HOME:
4538 EDIT_MoveHome(es, shift);
4539 break;
4540 case VK_END:
4541 EDIT_MoveEnd(es, shift);
4542 break;
4543 case VK_PRIOR:
4544 if (es->style & ES_MULTILINE)
4545 EDIT_MovePageUp_ML(es, shift);
4546 else
4547 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4548 break;
4549 case VK_NEXT:
4550 if (es->style & ES_MULTILINE)
4551 EDIT_MovePageDown_ML(es, shift);
4552 else
4553 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4554 break;
4555 case VK_DELETE:
4556 if (!(es->style & ES_READONLY) && !(shift && control)) {
4557 if (es->selection_start != es->selection_end) {
4558 if (shift)
4559 EDIT_WM_Cut(es);
4560 else
4561 EDIT_WM_Clear(es);
4562 } else {
4563 if (shift) {
4564 /* delete character left of caret */
4565 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4566 EDIT_MoveBackward(es, TRUE);
4567 EDIT_WM_Clear(es);
4568 } else if (control) {
4569 /* delete to end of line */
4570 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4571 EDIT_MoveEnd(es, TRUE);
4572 EDIT_WM_Clear(es);
4573 } else {
4574 /* delete character right of caret */
4575 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4576 EDIT_MoveForward(es, TRUE);
4577 EDIT_WM_Clear(es);
4581 break;
4582 case VK_INSERT:
4583 if (shift) {
4584 if (!(es->style & ES_READONLY))
4585 EDIT_WM_Paste(es);
4586 } else if (control)
4587 EDIT_WM_Copy(es);
4588 break;
4589 case VK_RETURN:
4590 /* If the edit doesn't want the return send a message to the default object */
4591 if(!(es->style & ES_WANTRETURN))
4593 HWND hwndParent = GetParent(es->hwndSelf);
4594 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4595 if (HIWORD(dw) == DC_HASDEFID)
4597 SendMessageW( hwndParent, WM_COMMAND,
4598 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4599 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4602 break;
4604 return 0;
4608 /*********************************************************************
4610 * WM_KILLFOCUS
4613 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4615 es->flags &= ~EF_FOCUSED;
4616 DestroyCaret();
4617 if(!(es->style & ES_NOHIDESEL))
4618 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4619 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4620 return 0;
4624 /*********************************************************************
4626 * WM_LBUTTONDBLCLK
4628 * The caret position has been set on the WM_LBUTTONDOWN message
4631 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4633 INT s;
4634 INT e = es->selection_end;
4635 INT l;
4636 INT li;
4637 INT ll;
4639 es->bCaptureState = TRUE;
4640 SetCapture(es->hwndSelf);
4642 l = EDIT_EM_LineFromChar(es, e);
4643 li = EDIT_EM_LineIndex(es, l);
4644 ll = EDIT_EM_LineLength(es, e);
4645 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4646 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4647 EDIT_EM_SetSel(es, s, e, FALSE);
4648 EDIT_EM_ScrollCaret(es);
4649 es->region_posx = es->region_posy = 0;
4650 SetTimer(es->hwndSelf, 0, 100, NULL);
4651 return 0;
4655 /*********************************************************************
4657 * WM_LBUTTONDOWN
4660 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4662 INT e;
4663 BOOL after_wrap;
4665 es->bCaptureState = TRUE;
4666 SetCapture(es->hwndSelf);
4667 EDIT_ConfinePoint(es, &x, &y);
4668 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4669 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4670 EDIT_EM_ScrollCaret(es);
4671 es->region_posx = es->region_posy = 0;
4672 SetTimer(es->hwndSelf, 0, 100, NULL);
4674 if (!(es->flags & EF_FOCUSED))
4675 SetFocus(es->hwndSelf);
4677 return 0;
4681 /*********************************************************************
4683 * WM_LBUTTONUP
4686 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4688 if (es->bCaptureState) {
4689 KillTimer(es->hwndSelf, 0);
4690 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4692 es->bCaptureState = FALSE;
4693 return 0;
4697 /*********************************************************************
4699 * WM_MBUTTONDOWN
4702 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4704 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4705 return 0;
4709 /*********************************************************************
4711 * WM_MOUSEMOVE
4714 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4716 INT e;
4717 BOOL after_wrap;
4718 INT prex, prey;
4720 /* If the mouse has been captured by process other than the edit control itself,
4721 * the windows edit controls will not select the strings with mouse move.
4723 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4724 return 0;
4727 * FIXME: gotta do some scrolling if outside client
4728 * area. Maybe reset the timer ?
4730 prex = x; prey = y;
4731 EDIT_ConfinePoint(es, &x, &y);
4732 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4733 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4734 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4735 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4736 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4737 return 0;
4741 /*********************************************************************
4743 * WM_NCCREATE
4745 * See also EDIT_WM_StyleChanged
4747 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4749 EDITSTATE *es;
4750 UINT alloc_size;
4752 TRACE("Creating %s edit control, style = %08x\n",
4753 unicode ? "Unicode" : "ANSI", lpcs->style);
4755 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4756 return FALSE;
4757 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4760 * Note: since the EDITSTATE has not been fully initialized yet,
4761 * we can't use any API calls that may send
4762 * WM_XXX messages before WM_NCCREATE is completed.
4765 es->is_unicode = unicode;
4766 es->style = lpcs->style;
4768 es->bEnableState = !(es->style & WS_DISABLED);
4770 es->hwndSelf = hwnd;
4771 /* Save parent, which will be notified by EN_* messages */
4772 es->hwndParent = lpcs->hwndParent;
4774 if (es->style & ES_COMBO)
4775 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4777 /* Number overrides lowercase overrides uppercase (at least it
4778 * does in Win95). However I'll bet that ES_NUMBER would be
4779 * invalid under Win 3.1.
4781 if (es->style & ES_NUMBER) {
4782 ; /* do not override the ES_NUMBER */
4783 } else if (es->style & ES_LOWERCASE) {
4784 es->style &= ~ES_UPPERCASE;
4786 if (es->style & ES_MULTILINE) {
4787 es->buffer_limit = BUFLIMIT_INITIAL;
4788 if (es->style & WS_VSCROLL)
4789 es->style |= ES_AUTOVSCROLL;
4790 if (es->style & WS_HSCROLL)
4791 es->style |= ES_AUTOHSCROLL;
4792 es->style &= ~ES_PASSWORD;
4793 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4794 /* Confirmed - RIGHT overrides CENTER */
4795 if (es->style & ES_RIGHT)
4796 es->style &= ~ES_CENTER;
4797 es->style &= ~WS_HSCROLL;
4798 es->style &= ~ES_AUTOHSCROLL;
4800 } else {
4801 es->buffer_limit = BUFLIMIT_INITIAL;
4802 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4803 es->style &= ~ES_CENTER;
4804 es->style &= ~WS_HSCROLL;
4805 es->style &= ~WS_VSCROLL;
4806 if (es->style & ES_PASSWORD)
4807 es->password_char = '*';
4810 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4811 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4812 return FALSE;
4813 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4815 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4816 return FALSE;
4817 es->undo_buffer_size = es->buffer_size;
4819 if (es->style & ES_MULTILINE)
4820 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4821 return FALSE;
4822 es->line_count = 1;
4825 * In Win95 look and feel, the WS_BORDER style is replaced by the
4826 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4827 * control a nonclient area so we don't need to draw the border.
4828 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4829 * a nonclient area and we should handle painting the border ourselves.
4831 * When making modifications please ensure that the code still works
4832 * for edit controls created directly with style 0x50800000, exStyle 0
4833 * (which should have a single pixel border)
4835 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4836 es->style &= ~WS_BORDER;
4837 else if (es->style & WS_BORDER)
4838 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4840 return TRUE;
4843 /*********************************************************************
4845 * WM_PAINT
4848 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4850 PAINTSTRUCT ps;
4851 INT i;
4852 HDC dc;
4853 HFONT old_font = 0;
4854 RECT rc;
4855 RECT rcClient;
4856 RECT rcLine;
4857 RECT rcRgn;
4858 HBRUSH brush;
4859 HBRUSH old_brush;
4860 INT bw, bh;
4861 BOOL rev = es->bEnableState &&
4862 ((es->flags & EF_FOCUSED) ||
4863 (es->style & ES_NOHIDESEL));
4864 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4866 GetClientRect(es->hwndSelf, &rcClient);
4868 /* get the background brush */
4869 brush = EDIT_NotifyCtlColor(es, dc);
4871 /* paint the border and the background */
4872 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4874 if(es->style & WS_BORDER) {
4875 bw = GetSystemMetrics(SM_CXBORDER);
4876 bh = GetSystemMetrics(SM_CYBORDER);
4877 rc = rcClient;
4878 if(es->style & ES_MULTILINE) {
4879 if(es->style & WS_HSCROLL) rc.bottom+=bh;
4880 if(es->style & WS_VSCROLL) rc.right+=bw;
4883 /* Draw the frame. Same code as in nonclient.c */
4884 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
4885 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
4886 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
4887 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
4888 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
4889 SelectObject(dc, old_brush);
4891 /* Keep the border clean */
4892 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
4893 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
4896 GetClipBox(dc, &rc);
4897 FillRect(dc, &rc, brush);
4899 IntersectClipRect(dc, es->format_rect.left,
4900 es->format_rect.top,
4901 es->format_rect.right,
4902 es->format_rect.bottom);
4903 if (es->style & ES_MULTILINE) {
4904 rc = rcClient;
4905 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4907 if (es->font)
4908 old_font = SelectObject(dc, es->font);
4910 if (!es->bEnableState)
4911 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4912 GetClipBox(dc, &rcRgn);
4913 if (es->style & ES_MULTILINE) {
4914 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4915 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4916 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4917 if (IntersectRect(&rc, &rcRgn, &rcLine))
4918 EDIT_PaintLine(es, dc, i, rev);
4920 } else {
4921 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4922 if (IntersectRect(&rc, &rcRgn, &rcLine))
4923 EDIT_PaintLine(es, dc, 0, rev);
4925 if (es->font)
4926 SelectObject(dc, old_font);
4928 if (!hdc)
4929 EndPaint(es->hwndSelf, &ps);
4933 /*********************************************************************
4935 * WM_PASTE
4938 static void EDIT_WM_Paste(EDITSTATE *es)
4940 HGLOBAL hsrc;
4941 LPWSTR src;
4943 /* Protect read-only edit control from modification */
4944 if(es->style & ES_READONLY)
4945 return;
4947 OpenClipboard(es->hwndSelf);
4948 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4949 src = (LPWSTR)GlobalLock(hsrc);
4950 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4951 GlobalUnlock(hsrc);
4953 else if (es->style & ES_PASSWORD) {
4954 /* clear selected text in password edit box even with empty clipboard */
4955 const WCHAR empty_strW[] = { 0 };
4956 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
4958 CloseClipboard();
4962 /*********************************************************************
4964 * WM_SETFOCUS
4967 static void EDIT_WM_SetFocus(EDITSTATE *es)
4969 es->flags |= EF_FOCUSED;
4971 if (!(es->style & ES_NOHIDESEL))
4972 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4974 /* single line edit updates itself */
4975 if (!(es->style & ES_MULTILINE))
4977 HDC hdc = GetDC(es->hwndSelf);
4978 EDIT_WM_Paint(es, hdc);
4979 ReleaseDC(es->hwndSelf, hdc);
4982 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4983 EDIT_SetCaretPos(es, es->selection_end,
4984 es->flags & EF_AFTER_WRAP);
4985 ShowCaret(es->hwndSelf);
4986 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
4990 /*********************************************************************
4992 * WM_SETFONT
4994 * With Win95 look the margins are set to default font value unless
4995 * the system font (font == 0) is being set, in which case they are left
4996 * unchanged.
4999 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
5001 TEXTMETRICW tm;
5002 HDC dc;
5003 HFONT old_font = 0;
5004 RECT clientRect;
5006 es->font = font;
5007 dc = GetDC(es->hwndSelf);
5008 if (font)
5009 old_font = SelectObject(dc, font);
5010 GetTextMetricsW(dc, &tm);
5011 es->line_height = tm.tmHeight;
5012 es->char_width = tm.tmAveCharWidth;
5013 if (font)
5014 SelectObject(dc, old_font);
5015 ReleaseDC(es->hwndSelf, dc);
5017 /* Reset the format rect and the margins */
5018 GetClientRect(es->hwndSelf, &clientRect);
5019 EDIT_SetRectNP(es, &clientRect);
5020 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5021 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5023 if (es->style & ES_MULTILINE)
5024 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5025 else
5026 EDIT_CalcLineWidth_SL(es);
5028 if (redraw)
5029 EDIT_UpdateText(es, NULL, TRUE);
5030 if (es->flags & EF_FOCUSED) {
5031 DestroyCaret();
5032 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5033 EDIT_SetCaretPos(es, es->selection_end,
5034 es->flags & EF_AFTER_WRAP);
5035 ShowCaret(es->hwndSelf);
5040 /*********************************************************************
5042 * WM_SETTEXT
5044 * NOTES
5045 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5046 * The modified flag is reset. No notifications are sent.
5048 * For single-line controls, reception of WM_SETTEXT triggers:
5049 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5052 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5054 LPWSTR textW = NULL;
5055 if (!unicode && text)
5057 LPCSTR textA = (LPCSTR)text;
5058 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5059 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5060 if (textW)
5061 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5062 text = textW;
5065 if (es->flags & EF_UPDATE)
5066 /* fixed this bug once; complain if we see it about to happen again. */
5067 ERR("SetSel may generate UPDATE message whose handler may reset "
5068 "selection.\n");
5070 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5071 if (text)
5073 TRACE("%s\n", debugstr_w(text));
5074 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5075 if(!unicode)
5076 HeapFree(GetProcessHeap(), 0, textW);
5078 else
5080 static const WCHAR empty_stringW[] = {0};
5081 TRACE("<NULL>\n");
5082 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5084 es->x_offset = 0;
5085 es->flags &= ~EF_MODIFIED;
5086 EDIT_EM_SetSel(es, 0, 0, FALSE);
5088 /* Send the notification after the selection start and end have been set
5089 * edit control doesn't send notification on WM_SETTEXT
5090 * if it is multiline, or it is part of combobox
5092 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5094 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5095 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5097 EDIT_EM_ScrollCaret(es);
5098 EDIT_UpdateScrollInfo(es);
5102 /*********************************************************************
5104 * WM_SIZE
5107 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5109 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5110 RECT rc;
5111 TRACE("width = %d, height = %d\n", width, height);
5112 SetRect(&rc, 0, 0, width, height);
5113 EDIT_SetRectNP(es, &rc);
5114 EDIT_UpdateText(es, NULL, TRUE);
5119 /*********************************************************************
5121 * WM_STYLECHANGED
5123 * This message is sent by SetWindowLong on having changed either the Style
5124 * or the extended style.
5126 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5128 * See also EDIT_WM_NCCreate
5130 * It appears that the Windows version of the edit control allows the style
5131 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5132 * style variable which will generally be different. In this function we
5133 * update the internal style based on what changed in the externally visible
5134 * style.
5136 * Much of this content as based upon the MSDN, especially:
5137 * Platform SDK Documentation -> User Interface Services ->
5138 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5139 * Edit Control Styles
5141 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5143 if (GWL_STYLE == which) {
5144 DWORD style_change_mask;
5145 DWORD new_style;
5146 /* Only a subset of changes can be applied after the control
5147 * has been created.
5149 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5150 ES_NUMBER;
5151 if (es->style & ES_MULTILINE)
5152 style_change_mask |= ES_WANTRETURN;
5154 new_style = style->styleNew & style_change_mask;
5156 /* Number overrides lowercase overrides uppercase (at least it
5157 * does in Win95). However I'll bet that ES_NUMBER would be
5158 * invalid under Win 3.1.
5160 if (new_style & ES_NUMBER) {
5161 ; /* do not override the ES_NUMBER */
5162 } else if (new_style & ES_LOWERCASE) {
5163 new_style &= ~ES_UPPERCASE;
5166 es->style = (es->style & ~style_change_mask) | new_style;
5167 } else if (GWL_EXSTYLE == which) {
5168 ; /* FIXME - what is needed here */
5169 } else {
5170 WARN ("Invalid style change %ld\n",which);
5173 return 0;
5176 /*********************************************************************
5178 * WM_SYSKEYDOWN
5181 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5183 if ((key == VK_BACK) && (key_data & 0x2000)) {
5184 if (EDIT_EM_CanUndo(es))
5185 EDIT_EM_Undo(es);
5186 return 0;
5187 } else if (key == VK_UP || key == VK_DOWN) {
5188 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5189 return 0;
5191 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5195 /*********************************************************************
5197 * WM_TIMER
5200 static void EDIT_WM_Timer(EDITSTATE *es)
5202 if (es->region_posx < 0) {
5203 EDIT_MoveBackward(es, TRUE);
5204 } else if (es->region_posx > 0) {
5205 EDIT_MoveForward(es, TRUE);
5208 * FIXME: gotta do some vertical scrolling here, like
5209 * EDIT_EM_LineScroll(hwnd, 0, 1);
5213 /*********************************************************************
5215 * WM_VSCROLL
5218 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5220 INT dy;
5222 if (!(es->style & ES_MULTILINE))
5223 return 0;
5225 if (!(es->style & ES_AUTOVSCROLL))
5226 return 0;
5228 dy = 0;
5229 switch (action) {
5230 case SB_LINEUP:
5231 case SB_LINEDOWN:
5232 case SB_PAGEUP:
5233 case SB_PAGEDOWN:
5234 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5235 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5236 (action == SB_PAGEUP ? "SB_PAGEUP" :
5237 "SB_PAGEDOWN"))));
5238 EDIT_EM_Scroll(es, action);
5239 return 0;
5240 case SB_TOP:
5241 TRACE("SB_TOP\n");
5242 dy = -es->y_offset;
5243 break;
5244 case SB_BOTTOM:
5245 TRACE("SB_BOTTOM\n");
5246 dy = es->line_count - 1 - es->y_offset;
5247 break;
5248 case SB_THUMBTRACK:
5249 TRACE("SB_THUMBTRACK %d\n", pos);
5250 es->flags |= EF_VSCROLL_TRACK;
5251 if(es->style & WS_VSCROLL)
5252 dy = pos - es->y_offset;
5253 else
5255 /* Assume default scroll range 0-100 */
5256 INT vlc, new_y;
5257 /* Sanity check */
5258 if(pos < 0 || pos > 100) return 0;
5259 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5260 new_y = pos * (es->line_count - vlc) / 100;
5261 dy = es->line_count ? (new_y - es->y_offset) : 0;
5262 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5263 es->line_count, es->y_offset, pos, dy);
5265 break;
5266 case SB_THUMBPOSITION:
5267 TRACE("SB_THUMBPOSITION %d\n", pos);
5268 es->flags &= ~EF_VSCROLL_TRACK;
5269 if(es->style & WS_VSCROLL)
5270 dy = pos - es->y_offset;
5271 else
5273 /* Assume default scroll range 0-100 */
5274 INT vlc, new_y;
5275 /* Sanity check */
5276 if(pos < 0 || pos > 100) return 0;
5277 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5278 new_y = pos * (es->line_count - vlc) / 100;
5279 dy = es->line_count ? (new_y - es->y_offset) : 0;
5280 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5281 es->line_count, es->y_offset, pos, dy);
5283 if (!dy)
5285 /* force scroll info update */
5286 EDIT_UpdateScrollInfo(es);
5287 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5289 break;
5290 case SB_ENDSCROLL:
5291 TRACE("SB_ENDSCROLL\n");
5292 break;
5294 * FIXME : the next two are undocumented !
5295 * Are we doing the right thing ?
5296 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5297 * although it's also a regular control message.
5299 case EM_GETTHUMB: /* this one is used by NT notepad */
5300 case EM_GETTHUMB16:
5302 LRESULT ret;
5303 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5304 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5305 else
5307 /* Assume default scroll range 0-100 */
5308 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5309 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5311 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5312 return ret;
5314 case EM_LINESCROLL16:
5315 TRACE("EM_LINESCROLL16 %d\n", pos);
5316 dy = pos;
5317 break;
5319 default:
5320 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5321 action, action);
5322 return 0;
5324 if (dy)
5325 EDIT_EM_LineScroll(es, 0, dy);
5326 return 0;
5329 /*********************************************************************
5331 * EDIT_UpdateText
5334 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5336 if (es->flags & EF_UPDATE) {
5337 es->flags &= ~EF_UPDATE;
5338 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5340 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5344 /*********************************************************************
5346 * EDIT_UpdateText
5349 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
5351 if (es->flags & EF_UPDATE) {
5352 es->flags &= ~EF_UPDATE;
5353 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5355 InvalidateRect(es->hwndSelf, rc, bErase);
5358 /********************************************************************
5360 * The Following code is to handle inline editing from IMEs
5363 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5365 LONG buflen;
5366 LPWSTR lpCompStr = NULL;
5367 HIMC hIMC;
5368 LPSTR lpCompStrAttr = NULL;
5369 DWORD dwBufLenAttr;
5371 if (!(hIMC = ImmGetContext(hwnd)))
5372 return;
5374 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5376 if (buflen < 0)
5378 ImmReleaseContext(hwnd, hIMC);
5379 return;
5382 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
5383 if (!lpCompStr)
5385 ERR("Unable to allocate IME CompositionString\n");
5386 ImmReleaseContext(hwnd,hIMC);
5387 return;
5390 if (buflen)
5391 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
5392 lpCompStr[buflen/sizeof(WCHAR)] = 0;
5394 if (CompFlag & GCS_COMPATTR)
5397 * We do not use the attributes yet. it would tell us what characters
5398 * are in transition and which are converted or decided upon
5400 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5401 if (dwBufLenAttr)
5403 dwBufLenAttr ++;
5404 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
5405 if (!lpCompStrAttr)
5407 ERR("Unable to allocate IME Attribute String\n");
5408 HeapFree(GetProcessHeap(),0,lpCompStr);
5409 ImmReleaseContext(hwnd,hIMC);
5410 return;
5412 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5413 dwBufLenAttr);
5414 lpCompStrAttr[dwBufLenAttr] = 0;
5416 else
5417 lpCompStrAttr = NULL;
5420 /* check for change in composition start */
5421 if (es->selection_end < es->composition_start)
5422 es->composition_start = es->selection_end;
5424 /* replace existing selection string */
5425 es->selection_start = es->composition_start;
5427 if (es->composition_len > 0)
5428 es->selection_end = es->composition_start + es->composition_len;
5429 else
5430 es->selection_end = es->selection_start;
5432 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5433 es->composition_len = abs(es->composition_start - es->selection_end);
5435 es->selection_start = es->composition_start;
5436 es->selection_end = es->selection_start + es->composition_len;
5438 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5439 HeapFree(GetProcessHeap(),0,lpCompStr);
5440 ImmReleaseContext(hwnd,hIMC);
5443 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5445 LONG buflen;
5446 LPWSTR lpResultStr;
5447 HIMC hIMC;
5449 if ( !(hIMC = ImmGetContext(hwnd)))
5450 return;
5452 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5453 if (buflen <= 0)
5455 ImmReleaseContext(hwnd, hIMC);
5456 return;
5459 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
5460 if (!lpResultStr)
5462 ERR("Unable to alloc buffer for IME string\n");
5463 ImmReleaseContext(hwnd, hIMC);
5464 return;
5467 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
5468 lpResultStr[buflen/sizeof(WCHAR)] = 0;
5470 /* check for change in composition start */
5471 if (es->selection_end < es->composition_start)
5472 es->composition_start = es->selection_end;
5474 es->selection_start = es->composition_start;
5475 es->selection_end = es->composition_start + es->composition_len;
5476 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5477 es->composition_start = es->selection_end;
5478 es->composition_len = 0;
5480 HeapFree(GetProcessHeap(),0,lpResultStr);
5481 ImmReleaseContext(hwnd, hIMC);
5484 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5486 if (CompFlag & GCS_RESULTSTR)
5487 EDIT_GetResultStr(hwnd,es);
5488 if (CompFlag & GCS_COMPSTR)
5489 EDIT_GetCompositionStr(hwnd, CompFlag, es);