Register Shell Drag and Drop Helper.
[wine/wine64.git] / dlls / user / edit.c
blob3e71ea3d023cda68c243909ba7d18e61f8a2d92d
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "wine/winbase16.h"
57 #include "wine/winuser16.h"
58 #include "wine/unicode.h"
59 #include "controls.h"
60 #include "local.h"
61 #include "message.h"
62 #include "user_private.h"
63 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(edit);
66 WINE_DECLARE_DEBUG_CHANNEL(combo);
67 WINE_DECLARE_DEBUG_CHANNEL(relay);
69 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
70 FIXME: BTW, new specs say 65535 (do you dare ???) */
71 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
72 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
73 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
74 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
77 * extra flags for EDITSTATE.flags field
79 #define EF_MODIFIED 0x0001 /* text has been modified */
80 #define EF_FOCUSED 0x0002 /* we have input focus */
81 #define EF_UPDATE 0x0004 /* notify parent of changed state */
82 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
83 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
84 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
85 wrapped line, instead of in front of the next character */
86 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
88 typedef enum
90 END_0 = 0, /* line ends with terminating '\0' character */
91 END_WRAP, /* line is wrapped */
92 END_HARD, /* line ends with a hard return '\r\n' */
93 END_SOFT, /* line ends with a soft return '\r\r\n' */
94 END_RICH /* line ends with a single '\n' */
95 } LINE_END;
97 typedef struct tagLINEDEF {
98 INT length; /* bruto length of a line in bytes */
99 INT net_length; /* netto length of a line in visible characters */
100 LINE_END ending;
101 INT width; /* width of the line in pixels */
102 INT index; /* line index into the buffer */
103 struct tagLINEDEF *next;
104 } LINEDEF;
106 typedef struct
108 BOOL is_unicode; /* how the control was created */
109 LPWSTR text; /* the actual contents of the control */
110 UINT buffer_size; /* the size of the buffer in characters */
111 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
112 HFONT font; /* NULL means standard system font */
113 INT x_offset; /* scroll offset for multi lines this is in pixels
114 for single lines it's in characters */
115 INT line_height; /* height of a screen line in pixels */
116 INT char_width; /* average character width in pixels */
117 DWORD style; /* sane version of wnd->dwStyle */
118 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
119 INT undo_insert_count; /* number of characters inserted in sequence */
120 UINT undo_position; /* character index of the insertion and deletion */
121 LPWSTR undo_text; /* deleted text */
122 UINT undo_buffer_size; /* size of the deleted text buffer */
123 INT selection_start; /* == selection_end if no selection */
124 INT selection_end; /* == current caret position */
125 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
126 INT left_margin; /* in pixels */
127 INT right_margin; /* in pixels */
128 RECT format_rect;
129 INT text_width; /* width of the widest line in pixels for multi line controls
130 and just line width for single line controls */
131 INT region_posx; /* Position of cursor relative to region: */
132 INT region_posy; /* -1: to left, 0: within, 1: to right */
133 EDITWORDBREAKPROC16 word_break_proc16;
134 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
135 INT line_count; /* number of lines */
136 INT y_offset; /* scroll offset in number of lines */
137 BOOL bCaptureState; /* flag indicating whether mouse was captured */
138 BOOL bEnableState; /* flag keeping the enable state */
139 HWND hwndSelf; /* the our window handle */
140 HWND hwndParent; /* Handle of parent for sending EN_* messages.
141 Even if parent will change, EN_* messages
142 should be sent to the first parent. */
143 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
145 * only for multi line controls
147 INT lock_count; /* amount of re-entries in the EditWndProc */
148 INT tabs_count;
149 LPINT tabs;
150 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
151 HLOCAL hloc32W; /* our unicode local memory block */
152 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
153 or EM_SETHANDLE16 */
154 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
155 or EM_SETHANDLE */
156 } EDITSTATE;
159 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
160 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
162 /* used for disabled or read-only edit control */
163 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
164 do \
165 { /* Notify parent which has created this edit control */ \
166 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
167 SendMessageW(es->hwndParent, WM_COMMAND, \
168 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
169 (LPARAM)(es->hwndSelf)); \
170 } while(0)
172 /*********************************************************************
174 * Declarations
179 * These functions have trivial implementations
180 * We still like to call them internally
181 * "static inline" makes them more like macro's
183 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
184 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
185 static inline void EDIT_WM_Clear(EDITSTATE *es);
186 static inline void EDIT_WM_Cut(EDITSTATE *es);
189 * Helper functions only valid for one type of control
191 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
192 static void EDIT_CalcLineWidth_SL(EDITSTATE *es);
193 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
194 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend);
195 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend);
196 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend);
197 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend);
199 * Helper functions valid for both single line _and_ multi line controls
201 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
202 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
203 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
204 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
205 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end);
206 static void EDIT_LockBuffer(EDITSTATE *es);
207 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size);
208 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
209 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend);
210 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend);
211 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend);
212 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend);
213 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend);
214 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend);
215 static void EDIT_PaintLine(EDITSTATE *es, HDC hdc, INT line, BOOL rev);
216 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
217 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos, BOOL after_wrap);
218 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT lprc);
219 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force);
220 static void EDIT_UpdateScrollInfo(EDITSTATE *es);
221 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
223 * EM_XXX message handlers
225 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y);
226 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
227 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
228 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es);
229 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode);
230 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end);
231 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es);
232 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
233 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
234 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
235 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy);
236 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy);
237 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
238 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit);
239 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action);
240 static void EDIT_EM_ScrollCaret(EDITSTATE *es);
241 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc);
242 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc);
243 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
244 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
245 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c);
246 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
247 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
248 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
249 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp);
250 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
251 static BOOL EDIT_EM_Undo(EDITSTATE *es);
253 * WM_XXX message handlers
255 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c);
256 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND conrtol);
257 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y);
258 static void EDIT_WM_Copy(EDITSTATE *es);
259 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name);
260 static LRESULT EDIT_WM_Destroy(EDITSTATE *es);
261 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc);
262 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode);
263 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos);
264 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key);
265 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es);
266 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es);
267 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y);
268 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es);
269 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es);
270 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y);
271 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode);
272 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc);
273 static void EDIT_WM_Paste(EDITSTATE *es);
274 static void EDIT_WM_SetFocus(EDITSTATE *es);
275 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw);
276 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode);
277 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height);
278 static LRESULT EDIT_WM_StyleChanged(EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
279 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data);
280 static void EDIT_WM_Timer(EDITSTATE *es);
281 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos);
282 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase);
283 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase);
285 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
286 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
288 /*********************************************************************
289 * edit class descriptor
291 const struct builtin_class_descr EDIT_builtin_class =
293 "Edit", /* name */
294 CS_DBLCLKS | CS_PARENTDC, /* style */
295 EditWndProcA, /* procA */
296 EditWndProcW, /* procW */
297 sizeof(EDITSTATE *), /* extra */
298 IDC_IBEAM, /* cursor */
299 0 /* brush */
303 /*********************************************************************
305 * EM_CANUNDO
308 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
310 return (es->undo_insert_count || strlenW(es->undo_text));
314 /*********************************************************************
316 * EM_EMPTYUNDOBUFFER
319 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
321 es->undo_insert_count = 0;
322 *es->undo_text = '\0';
326 /*********************************************************************
328 * WM_CLEAR
331 static inline void EDIT_WM_Clear(EDITSTATE *es)
333 static const WCHAR empty_stringW[] = {0};
335 /* Protect read-only edit control from modification */
336 if(es->style & ES_READONLY)
337 return;
339 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
343 /*********************************************************************
345 * WM_CUT
348 static inline void EDIT_WM_Cut(EDITSTATE *es)
350 EDIT_WM_Copy(es);
351 EDIT_WM_Clear(es);
355 /**********************************************************************
356 * get_app_version
358 * Returns the window version in case Wine emulates a later version
359 * of windows than the application expects.
361 * In a number of cases when windows runs an application that was
362 * designed for an earlier windows version, windows reverts
363 * to "old" behaviour of that earlier version.
365 * An example is a disabled edit control that needs to be painted.
366 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
367 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
368 * applications with an expected version 0f 4.0 or higher.
371 static DWORD get_app_version(void)
373 static DWORD version;
374 if (!version)
376 DWORD dwEmulatedVersion;
377 OSVERSIONINFOW info;
378 DWORD dwProcVersion = GetProcessVersion(0);
380 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
381 GetVersionExW( &info );
382 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
383 /* FIXME: this may not be 100% correct; see discussion on the
384 * wine developer list in Nov 1999 */
385 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
387 return version;
391 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
393 UINT msg;
395 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
396 msg = WM_CTLCOLORSTATIC;
397 else
398 msg = WM_CTLCOLOREDIT;
400 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
401 return (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
404 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
406 if(unicode)
407 return DefWindowProcW(hwnd, msg, wParam, lParam);
408 else
409 return DefWindowProcA(hwnd, msg, wParam, lParam);
412 /*********************************************************************
414 * EditWndProc_common
416 * The messages are in the order of the actual integer values
417 * (which can be found in include/windows.h)
418 * Wherever possible the 16 bit versions are converted to
419 * the 32 bit ones, so that we can 'fall through' to the
420 * helper functions. These are mostly 32 bit (with a few
421 * exceptions, clearly indicated by a '16' extension to their
422 * names).
425 static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
426 WPARAM wParam, LPARAM lParam, BOOL unicode )
428 EDITSTATE *es = (EDITSTATE *)GetWindowLongW( hwnd, 0 );
429 LRESULT result = 0;
431 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
433 if (!es && msg != WM_NCCREATE)
434 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
435 else if (msg == WM_NCCREATE)
436 return EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
437 else if (msg == WM_DESTROY)
438 return EDIT_WM_Destroy(es);
441 if (es) EDIT_LockBuffer(es);
443 switch (msg) {
444 case EM_GETSEL16:
445 wParam = 0;
446 lParam = 0;
447 /* fall through */
448 case EM_GETSEL:
449 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
450 break;
452 case EM_SETSEL16:
453 if ((short)LOWORD(lParam) == -1)
454 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
455 else
456 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
457 if (!wParam)
458 EDIT_EM_ScrollCaret(es);
459 result = 1;
460 break;
461 case EM_SETSEL:
462 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
463 EDIT_EM_ScrollCaret(es);
464 result = 1;
465 break;
467 case EM_GETRECT16:
468 if (lParam)
470 RECT16 *r16 = MapSL(lParam);
471 r16->left = es->format_rect.left;
472 r16->top = es->format_rect.top;
473 r16->right = es->format_rect.right;
474 r16->bottom = es->format_rect.bottom;
476 break;
477 case EM_GETRECT:
478 if (lParam)
479 CopyRect((LPRECT)lParam, &es->format_rect);
480 break;
482 case EM_SETRECT16:
483 if ((es->style & ES_MULTILINE) && lParam) {
484 RECT rc;
485 RECT16 *r16 = MapSL(lParam);
486 rc.left = r16->left;
487 rc.top = r16->top;
488 rc.right = r16->right;
489 rc.bottom = r16->bottom;
490 EDIT_SetRectNP(es, &rc);
491 EDIT_UpdateText(es, NULL, TRUE);
493 break;
494 case EM_SETRECT:
495 if ((es->style & ES_MULTILINE) && lParam) {
496 EDIT_SetRectNP(es, (LPRECT)lParam);
497 EDIT_UpdateText(es, NULL, TRUE);
499 break;
501 case EM_SETRECTNP16:
502 if ((es->style & ES_MULTILINE) && lParam) {
503 RECT rc;
504 RECT16 *r16 = MapSL(lParam);
505 rc.left = r16->left;
506 rc.top = r16->top;
507 rc.right = r16->right;
508 rc.bottom = r16->bottom;
509 EDIT_SetRectNP(es, &rc);
511 break;
512 case EM_SETRECTNP:
513 if ((es->style & ES_MULTILINE) && lParam)
514 EDIT_SetRectNP(es, (LPRECT)lParam);
515 break;
517 case EM_SCROLL16:
518 case EM_SCROLL:
519 result = EDIT_EM_Scroll(es, (INT)wParam);
520 break;
522 case EM_LINESCROLL16:
523 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
524 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
525 /* fall through */
526 case EM_LINESCROLL:
527 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
528 break;
530 case EM_SCROLLCARET16:
531 case EM_SCROLLCARET:
532 EDIT_EM_ScrollCaret(es);
533 result = 1;
534 break;
536 case EM_GETMODIFY16:
537 case EM_GETMODIFY:
538 result = ((es->flags & EF_MODIFIED) != 0);
539 break;
541 case EM_SETMODIFY16:
542 case EM_SETMODIFY:
543 if (wParam)
544 es->flags |= EF_MODIFIED;
545 else
546 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
547 break;
549 case EM_GETLINECOUNT16:
550 case EM_GETLINECOUNT:
551 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
552 break;
554 case EM_LINEINDEX16:
555 if ((INT16)wParam == -1)
556 wParam = (WPARAM)-1;
557 /* fall through */
558 case EM_LINEINDEX:
559 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
560 break;
562 case EM_SETHANDLE16:
563 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
564 break;
565 case EM_SETHANDLE:
566 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
567 break;
569 case EM_GETHANDLE16:
570 result = (LRESULT)EDIT_EM_GetHandle16(es);
571 break;
572 case EM_GETHANDLE:
573 result = (LRESULT)EDIT_EM_GetHandle(es);
574 break;
576 case EM_GETTHUMB16:
577 case EM_GETTHUMB:
578 result = EDIT_EM_GetThumb(es);
579 break;
581 /* these messages missing from specs */
582 case WM_USER+15:
583 case 0x00bf:
584 case WM_USER+16:
585 case 0x00c0:
586 case WM_USER+19:
587 case 0x00c3:
588 case WM_USER+26:
589 case 0x00ca:
590 FIXME("undocumented message 0x%x, please report\n", msg);
591 result = DefWindowProcW(hwnd, msg, wParam, lParam);
592 break;
594 case EM_LINELENGTH16:
595 case EM_LINELENGTH:
596 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
597 break;
599 case EM_REPLACESEL16:
600 lParam = (LPARAM)MapSL(lParam);
601 unicode = FALSE; /* 16-bit message is always ascii */
602 /* fall through */
603 case EM_REPLACESEL:
605 LPWSTR textW;
607 if(unicode)
608 textW = (LPWSTR)lParam;
609 else
611 LPSTR textA = (LPSTR)lParam;
612 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
613 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
614 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
617 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
618 result = 1;
620 if(!unicode)
621 HeapFree(GetProcessHeap(), 0, textW);
622 break;
625 case EM_GETLINE16:
626 lParam = (LPARAM)MapSL(lParam);
627 unicode = FALSE; /* 16-bit message is always ascii */
628 /* fall through */
629 case EM_GETLINE:
630 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
631 break;
633 case EM_LIMITTEXT16:
634 case EM_SETLIMITTEXT:
635 EDIT_EM_SetLimitText(es, (INT)wParam);
636 break;
638 case EM_CANUNDO16:
639 case EM_CANUNDO:
640 result = (LRESULT)EDIT_EM_CanUndo(es);
641 break;
643 case EM_UNDO16:
644 case EM_UNDO:
645 case WM_UNDO:
646 result = (LRESULT)EDIT_EM_Undo(es);
647 break;
649 case EM_FMTLINES16:
650 case EM_FMTLINES:
651 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
652 break;
654 case EM_LINEFROMCHAR16:
655 case EM_LINEFROMCHAR:
656 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
657 break;
659 case EM_SETTABSTOPS16:
660 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
661 break;
662 case EM_SETTABSTOPS:
663 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
664 break;
666 case EM_SETPASSWORDCHAR16:
667 unicode = FALSE; /* 16-bit message is always ascii */
668 /* fall through */
669 case EM_SETPASSWORDCHAR:
671 WCHAR charW = 0;
673 if(unicode)
674 charW = (WCHAR)wParam;
675 else
677 CHAR charA = wParam;
678 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
681 EDIT_EM_SetPasswordChar(es, charW);
682 break;
685 case EM_EMPTYUNDOBUFFER16:
686 case EM_EMPTYUNDOBUFFER:
687 EDIT_EM_EmptyUndoBuffer(es);
688 break;
690 case EM_GETFIRSTVISIBLELINE16:
691 result = es->y_offset;
692 break;
693 case EM_GETFIRSTVISIBLELINE:
694 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
695 break;
697 case EM_SETREADONLY16:
698 case EM_SETREADONLY:
699 if (wParam) {
700 SetWindowLongW( hwnd, GWL_STYLE,
701 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
702 es->style |= ES_READONLY;
703 } else {
704 SetWindowLongW( hwnd, GWL_STYLE,
705 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
706 es->style &= ~ES_READONLY;
708 result = 1;
709 break;
711 case EM_SETWORDBREAKPROC16:
712 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
713 break;
714 case EM_SETWORDBREAKPROC:
715 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
716 break;
718 case EM_GETWORDBREAKPROC16:
719 result = (LRESULT)es->word_break_proc16;
720 break;
721 case EM_GETWORDBREAKPROC:
722 result = (LRESULT)es->word_break_proc;
723 break;
725 case EM_GETPASSWORDCHAR16:
726 unicode = FALSE; /* 16-bit message is always ascii */
727 /* fall through */
728 case EM_GETPASSWORDCHAR:
730 if(unicode)
731 result = es->password_char;
732 else
734 WCHAR charW = es->password_char;
735 CHAR charA = 0;
736 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
737 result = charA;
739 break;
742 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
744 case EM_SETMARGINS:
745 EDIT_EM_SetMargins(es, (INT)wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
746 break;
748 case EM_GETMARGINS:
749 result = MAKELONG(es->left_margin, es->right_margin);
750 break;
752 case EM_GETLIMITTEXT:
753 result = es->buffer_limit;
754 break;
756 case EM_POSFROMCHAR:
757 result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
758 break;
760 case EM_CHARFROMPOS:
761 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
762 break;
764 /* End of the EM_ messages which were in numerical order; what order
765 * are these in? vaguely alphabetical?
768 case WM_GETDLGCODE:
769 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
771 if (es->style & ES_MULTILINE)
773 result |= DLGC_WANTALLKEYS;
774 break;
777 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
779 int vk = (int)((LPMSG)lParam)->wParam;
781 if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
783 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
784 result |= DLGC_WANTMESSAGE;
787 break;
789 case WM_IME_CHAR:
790 if (!unicode)
792 WCHAR charW;
793 CHAR strng[2];
795 strng[0] = wParam >> 8;
796 strng[1] = wParam & 0xff;
797 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
798 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
799 EDIT_WM_Char(es, charW);
800 break;
802 /* fall through */
803 case WM_CHAR:
805 WCHAR charW;
807 if(unicode)
808 charW = wParam;
809 else
811 CHAR charA = wParam;
812 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
815 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
817 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
818 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
819 break;
821 EDIT_WM_Char(es, charW);
822 break;
825 case WM_CLEAR:
826 EDIT_WM_Clear(es);
827 break;
829 case WM_COMMAND:
830 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
831 break;
833 case WM_CONTEXTMENU:
834 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
835 break;
837 case WM_COPY:
838 EDIT_WM_Copy(es);
839 break;
841 case WM_CREATE:
842 if(unicode)
843 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
844 else
846 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
847 LPWSTR nameW = NULL;
848 if(nameA)
850 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
851 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
852 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
854 result = EDIT_WM_Create(es, nameW);
855 HeapFree(GetProcessHeap(), 0, nameW);
857 break;
859 case WM_CUT:
860 EDIT_WM_Cut(es);
861 break;
863 case WM_ENABLE:
864 es->bEnableState = (BOOL) wParam;
865 EDIT_UpdateText(es, NULL, TRUE);
866 break;
868 case WM_ERASEBKGND:
869 result = EDIT_WM_EraseBkGnd(es, (HDC)wParam);
870 break;
872 case WM_GETFONT:
873 result = (LRESULT)es->font;
874 break;
876 case WM_GETTEXT:
877 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
878 break;
880 case WM_GETTEXTLENGTH:
881 if (unicode) result = strlenW(es->text);
882 else result = WideCharToMultiByte( CP_ACP, 0, es->text, strlenW(es->text),
883 NULL, 0, NULL, NULL );
884 break;
886 case WM_HSCROLL:
887 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
888 break;
890 case WM_KEYDOWN:
891 result = EDIT_WM_KeyDown(es, (INT)wParam);
892 break;
894 case WM_KILLFOCUS:
895 result = EDIT_WM_KillFocus(es);
896 break;
898 case WM_LBUTTONDBLCLK:
899 result = EDIT_WM_LButtonDblClk(es);
900 break;
902 case WM_LBUTTONDOWN:
903 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
904 break;
906 case WM_LBUTTONUP:
907 result = EDIT_WM_LButtonUp(es);
908 break;
910 case WM_MBUTTONDOWN:
911 result = EDIT_WM_MButtonDown(es);
912 break;
914 case WM_MOUSEACTIVATE:
915 result = MA_ACTIVATE;
916 break;
918 case WM_MOUSEMOVE:
919 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
920 break;
922 case WM_PAINT:
923 EDIT_WM_Paint(es, (HDC)wParam);
924 break;
926 case WM_PASTE:
927 EDIT_WM_Paste(es);
928 break;
930 case WM_SETFOCUS:
931 EDIT_WM_SetFocus(es);
932 break;
934 case WM_SETFONT:
935 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
936 break;
938 case WM_SETREDRAW:
939 /* FIXME: actually set an internal flag and behave accordingly */
940 break;
942 case WM_SETTEXT:
943 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
944 result = TRUE;
945 break;
947 case WM_SIZE:
948 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
949 break;
951 case WM_STYLECHANGED:
952 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
953 break;
955 case WM_STYLECHANGING:
956 result = 0; /* See EDIT_WM_StyleChanged */
957 break;
959 case WM_SYSKEYDOWN:
960 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
961 break;
963 case WM_TIMER:
964 EDIT_WM_Timer(es);
965 break;
967 case WM_VSCROLL:
968 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
969 break;
971 case WM_MOUSEWHEEL:
973 int gcWheelDelta = 0;
974 UINT pulScrollLines = 3;
975 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
977 if (wParam & (MK_SHIFT | MK_CONTROL)) {
978 result = DefWindowProcW(hwnd, msg, wParam, lParam);
979 break;
981 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
982 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
984 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
985 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
986 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
989 break;
990 default:
991 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
992 break;
995 if (es) EDIT_UnlockBuffer(es, FALSE);
997 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
999 return result;
1002 /*********************************************************************
1004 * EditWndProcW (USER32.@)
1006 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1008 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1011 /*********************************************************************
1013 * EditWndProc (USER32.@)
1015 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1017 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1020 /*********************************************************************
1022 * EDIT_BuildLineDefs_ML
1024 * Build linked list of text lines.
1025 * Lines can end with '\0' (last line), a character (if it is wrapped),
1026 * a soft return '\r\r\n' or a hard return '\r\n'
1029 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1031 HDC dc;
1032 HFONT old_font = 0;
1033 LPWSTR current_position, cp;
1034 INT fw;
1035 LINEDEF *current_line;
1036 LINEDEF *previous_line;
1037 LINEDEF *start_line;
1038 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1039 INT line_count = es->line_count;
1040 INT orig_net_length;
1041 RECT rc;
1043 if (istart == iend && delta == 0)
1044 return;
1046 dc = GetDC(es->hwndSelf);
1047 if (es->font)
1048 old_font = SelectObject(dc, es->font);
1050 previous_line = NULL;
1051 current_line = es->first_line_def;
1053 /* Find starting line. istart must lie inside an existing line or
1054 * at the end of buffer */
1055 do {
1056 if (istart < current_line->index + current_line->length ||
1057 current_line->ending == END_0)
1058 break;
1060 previous_line = current_line;
1061 current_line = current_line->next;
1062 line_index++;
1063 } while (current_line);
1065 if (!current_line) /* Error occurred start is not inside previous buffer */
1067 FIXME(" modification occurred outside buffer\n");
1068 ReleaseDC(es->hwndSelf, dc);
1069 return;
1072 /* Remember start of modifications in order to calculate update region */
1073 nstart_line = line_index;
1074 nstart_index = current_line->index;
1076 /* We must start to reformat from the previous line since the modifications
1077 * may have caused the line to wrap upwards. */
1078 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1080 line_index--;
1081 current_line = previous_line;
1083 start_line = current_line;
1085 fw = es->format_rect.right - es->format_rect.left;
1086 current_position = es->text + current_line->index;
1087 do {
1088 if (current_line != start_line)
1090 if (!current_line || current_line->index + delta > current_position - es->text)
1092 /* The buffer has been expanded, create a new line and
1093 insert it into the link list */
1094 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1095 new_line->next = previous_line->next;
1096 previous_line->next = new_line;
1097 current_line = new_line;
1098 es->line_count++;
1100 else if (current_line->index + delta < current_position - es->text)
1102 /* The previous line merged with this line so we delete this extra entry */
1103 previous_line->next = current_line->next;
1104 HeapFree(GetProcessHeap(), 0, current_line);
1105 current_line = previous_line->next;
1106 es->line_count--;
1107 continue;
1109 else /* current_line->index + delta == current_position */
1111 if (current_position - es->text > iend)
1112 break; /* We reached end of line modifications */
1113 /* else recalulate this line */
1117 current_line->index = current_position - es->text;
1118 orig_net_length = current_line->net_length;
1120 /* Find end of line */
1121 cp = current_position;
1122 while (*cp) {
1123 if (*cp == '\n') break;
1124 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1125 break;
1126 cp++;
1129 /* Mark type of line termination */
1130 if (!(*cp)) {
1131 current_line->ending = END_0;
1132 current_line->net_length = strlenW(current_position);
1133 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1134 current_line->ending = END_SOFT;
1135 current_line->net_length = cp - current_position - 1;
1136 } else if (*cp == '\n') {
1137 current_line->ending = END_RICH;
1138 current_line->net_length = cp - current_position;
1139 } else {
1140 current_line->ending = END_HARD;
1141 current_line->net_length = cp - current_position;
1144 /* Calculate line width */
1145 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1146 current_position, current_line->net_length,
1147 es->tabs_count, es->tabs));
1149 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1150 if (!(es->style & ES_AUTOHSCROLL)) {
1151 if (current_line->width > fw) {
1152 INT next = 0;
1153 INT prev;
1154 do {
1155 prev = next;
1156 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1157 prev + 1, current_line->net_length, WB_RIGHT);
1158 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1159 current_position, next, es->tabs_count, es->tabs));
1160 } while (current_line->width <= fw);
1161 if (!prev) { /* Didn't find a line break so force a break */
1162 next = 0;
1163 do {
1164 prev = next;
1165 next++;
1166 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1167 current_position, next, es->tabs_count, es->tabs));
1168 } while (current_line->width <= fw);
1169 if (!prev)
1170 prev = 1;
1173 /* If the first line we are calculating, wrapped before istart, we must
1174 * adjust istart in order for this to be reflected in the update region. */
1175 if (current_line->index == nstart_index && istart > current_line->index + prev)
1176 istart = current_line->index + prev;
1177 /* else if we are updating the previous line before the first line we
1178 * are re-calculating and it expanded */
1179 else if (current_line == start_line &&
1180 current_line->index != nstart_index && orig_net_length < prev)
1182 /* Line expanded due to an upwards line wrap so we must partially include
1183 * previous line in update region */
1184 nstart_line = line_index;
1185 nstart_index = current_line->index;
1186 istart = current_line->index + orig_net_length;
1189 current_line->net_length = prev;
1190 current_line->ending = END_WRAP;
1191 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1192 current_line->net_length, es->tabs_count, es->tabs));
1194 else if (orig_net_length < current_line->net_length &&
1195 current_line == start_line &&
1196 current_line->index != nstart_index) {
1197 /* The previous line expanded but it's still not as wide as the client rect */
1198 /* The expansion is due to an upwards line wrap so we must partially include
1199 it in the update region */
1200 nstart_line = line_index;
1201 nstart_index = current_line->index;
1202 istart = current_line->index + orig_net_length;
1207 /* Adjust length to include line termination */
1208 switch (current_line->ending) {
1209 case END_SOFT:
1210 current_line->length = current_line->net_length + 3;
1211 break;
1212 case END_RICH:
1213 current_line->length = current_line->net_length + 1;
1214 break;
1215 case END_HARD:
1216 current_line->length = current_line->net_length + 2;
1217 break;
1218 case END_WRAP:
1219 case END_0:
1220 current_line->length = current_line->net_length;
1221 break;
1223 es->text_width = max(es->text_width, current_line->width);
1224 current_position += current_line->length;
1225 previous_line = current_line;
1226 current_line = current_line->next;
1227 line_index++;
1228 } while (previous_line->ending != END_0);
1230 /* Finish adjusting line indexes by delta or remove hanging lines */
1231 if (previous_line->ending == END_0)
1233 LINEDEF *pnext = NULL;
1235 previous_line->next = NULL;
1236 while (current_line)
1238 pnext = current_line->next;
1239 HeapFree(GetProcessHeap(), 0, current_line);
1240 current_line = pnext;
1241 es->line_count--;
1244 else if (delta != 0)
1246 while (current_line)
1248 current_line->index += delta;
1249 current_line = current_line->next;
1253 /* Calculate rest of modification rectangle */
1254 if (hrgn)
1256 HRGN tmphrgn;
1258 * We calculate two rectangles. One for the first line which may have
1259 * an indent with respect to the format rect. The other is a format-width
1260 * rectangle that spans the rest of the lines that changed or moved.
1262 rc.top = es->format_rect.top + nstart_line * es->line_height -
1263 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1264 rc.bottom = rc.top + es->line_height;
1265 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1266 rc.left = es->format_rect.left;
1267 else
1268 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1269 es->text + nstart_index, istart - nstart_index,
1270 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1271 rc.right = es->format_rect.right;
1272 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1274 rc.top = rc.bottom;
1275 rc.left = es->format_rect.left;
1276 rc.right = es->format_rect.right;
1278 * If lines were added or removed we must re-paint the remainder of the
1279 * lines since the remaining lines were either shifted up or down.
1281 if (line_count < es->line_count) /* We added lines */
1282 rc.bottom = es->line_count * es->line_height;
1283 else if (line_count > es->line_count) /* We removed lines */
1284 rc.bottom = line_count * es->line_height;
1285 else
1286 rc.bottom = line_index * es->line_height;
1287 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1288 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1289 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1290 DeleteObject(tmphrgn);
1293 if (es->font)
1294 SelectObject(dc, old_font);
1296 ReleaseDC(es->hwndSelf, dc);
1299 /*********************************************************************
1301 * EDIT_CalcLineWidth_SL
1304 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1306 SIZE size;
1307 LPWSTR text;
1308 HDC dc;
1309 HFONT old_font = 0;
1311 text = EDIT_GetPasswordPointer_SL(es);
1313 dc = GetDC(es->hwndSelf);
1314 if (es->font)
1315 old_font = SelectObject(dc, es->font);
1317 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1319 if (es->font)
1320 SelectObject(dc, old_font);
1321 ReleaseDC(es->hwndSelf, dc);
1323 if (es->style & ES_PASSWORD)
1324 HeapFree(GetProcessHeap(), 0, text);
1326 es->text_width = size.cx;
1329 /*********************************************************************
1331 * EDIT_CallWordBreakProc
1333 * Call appropriate WordBreakProc (internal or external).
1335 * Note: The "start" argument should always be an index referring
1336 * to es->text. The actual wordbreak proc might be
1337 * 16 bit, so we can't always pass any 32 bit LPSTR.
1338 * Hence we assume that es->text is the buffer that holds
1339 * the string under examination (we can decide this for ourselves).
1342 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1344 INT ret;
1346 if (es->word_break_proc16) {
1347 HGLOBAL16 hglob16;
1348 SEGPTR segptr;
1349 INT countA;
1350 WORD args[5];
1351 DWORD result;
1353 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1354 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1355 segptr = K32WOWGlobalLock16(hglob16);
1356 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1357 args[4] = SELECTOROF(segptr);
1358 args[3] = OFFSETOF(segptr);
1359 args[2] = index;
1360 args[1] = countA;
1361 args[0] = action;
1362 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1363 ret = LOWORD(result);
1364 GlobalUnlock16(hglob16);
1365 GlobalFree16(hglob16);
1367 else if (es->word_break_proc)
1369 if(es->is_unicode)
1371 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1373 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1374 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1375 ret = wbpW(es->text + start, index, count, action);
1377 else
1379 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1380 INT countA;
1381 CHAR *textA;
1383 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1384 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1385 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1386 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1387 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1388 ret = wbpA(textA, index, countA, action);
1389 HeapFree(GetProcessHeap(), 0, textA);
1392 else
1393 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1395 return ret;
1399 /*********************************************************************
1401 * EDIT_CharFromPos
1403 * Beware: This is not the function called on EM_CHARFROMPOS
1404 * The position _can_ be outside the formatting / client
1405 * rectangle
1406 * The return value is only the character index
1409 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1411 INT index;
1412 HDC dc;
1413 HFONT old_font = 0;
1415 if (es->style & ES_MULTILINE) {
1416 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1417 INT line_index = 0;
1418 LINEDEF *line_def = es->first_line_def;
1419 INT low, high;
1420 while ((line > 0) && line_def->next) {
1421 line_index += line_def->length;
1422 line_def = line_def->next;
1423 line--;
1425 x += es->x_offset - es->format_rect.left;
1426 if (es->style & ES_RIGHT)
1427 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1428 else if (es->style & ES_CENTER)
1429 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1430 if (x >= line_def->width) {
1431 if (after_wrap)
1432 *after_wrap = (line_def->ending == END_WRAP);
1433 return line_index + line_def->net_length;
1435 if (x <= 0) {
1436 if (after_wrap)
1437 *after_wrap = FALSE;
1438 return line_index;
1440 dc = GetDC(es->hwndSelf);
1441 if (es->font)
1442 old_font = SelectObject(dc, es->font);
1443 low = line_index + 1;
1444 high = line_index + line_def->net_length + 1;
1445 while (low < high - 1)
1447 INT mid = (low + high) / 2;
1448 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1449 else low = mid;
1451 index = low;
1453 if (after_wrap)
1454 *after_wrap = ((index == line_index + line_def->net_length) &&
1455 (line_def->ending == END_WRAP));
1456 } else {
1457 LPWSTR text;
1458 SIZE size;
1459 if (after_wrap)
1460 *after_wrap = FALSE;
1461 x -= es->format_rect.left;
1462 if (!x)
1463 return es->x_offset;
1465 if (!es->x_offset)
1467 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1468 if (es->style & ES_RIGHT)
1469 x -= indent;
1470 else if (es->style & ES_CENTER)
1471 x -= indent / 2;
1474 text = EDIT_GetPasswordPointer_SL(es);
1475 dc = GetDC(es->hwndSelf);
1476 if (es->font)
1477 old_font = SelectObject(dc, es->font);
1478 if (x < 0)
1480 INT low = 0;
1481 INT high = es->x_offset;
1482 while (low < high - 1)
1484 INT mid = (low + high) / 2;
1485 GetTextExtentPoint32W( dc, text + mid,
1486 es->x_offset - mid, &size );
1487 if (size.cx > -x) low = mid;
1488 else high = mid;
1490 index = low;
1492 else
1494 INT low = es->x_offset;
1495 INT high = strlenW(es->text) + 1;
1496 while (low < high - 1)
1498 INT mid = (low + high) / 2;
1499 GetTextExtentPoint32W( dc, text + es->x_offset,
1500 mid - es->x_offset, &size );
1501 if (size.cx > x) high = mid;
1502 else low = mid;
1504 index = low;
1506 if (es->style & ES_PASSWORD)
1507 HeapFree(GetProcessHeap(), 0, text);
1509 if (es->font)
1510 SelectObject(dc, old_font);
1511 ReleaseDC(es->hwndSelf, dc);
1512 return index;
1516 /*********************************************************************
1518 * EDIT_ConfinePoint
1520 * adjusts the point to be within the formatting rectangle
1521 * (so CharFromPos returns the nearest _visible_ character)
1524 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1526 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1527 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1531 /*********************************************************************
1533 * EDIT_GetLineRect
1535 * Calculates the bounding rectangle for a line from a starting
1536 * column to an ending column.
1539 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1541 INT line_index = EDIT_EM_LineIndex(es, line);
1543 if (es->style & ES_MULTILINE)
1544 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1545 else
1546 rc->top = es->format_rect.top;
1547 rc->bottom = rc->top + es->line_height;
1548 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1549 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1553 /*********************************************************************
1555 * EDIT_GetPasswordPointer_SL
1557 * note: caller should free the (optionally) allocated buffer
1560 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1562 if (es->style & ES_PASSWORD) {
1563 INT len = strlenW(es->text);
1564 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1565 text[len] = '\0';
1566 while(len) text[--len] = es->password_char;
1567 return text;
1568 } else
1569 return es->text;
1573 /*********************************************************************
1575 * EDIT_LockBuffer
1577 * This acts as a LOCAL_Lock(), but it locks only once. This way
1578 * you can call it whenever you like, without unlocking.
1580 * Initially the edit control allocates a HLOCAL32 buffer
1581 * (32 bit linear memory handler). However, 16 bit application
1582 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1583 * handler). From that moment on we have to keep using this 16 bit memory
1584 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1585 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1586 * conversion.
1589 static void EDIT_LockBuffer(EDITSTATE *es)
1591 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1592 if (!es->text) {
1593 CHAR *textA = NULL;
1594 UINT countA = 0;
1595 BOOL _16bit = FALSE;
1597 if(es->hloc32W)
1599 if(es->hloc32A)
1601 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1602 textA = LocalLock(es->hloc32A);
1603 countA = strlen(textA) + 1;
1605 else if(es->hloc16)
1607 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1608 textA = LOCAL_Lock(hInstance, es->hloc16);
1609 countA = strlen(textA) + 1;
1610 _16bit = TRUE;
1613 else {
1614 ERR("no buffer ... please report\n");
1615 return;
1618 if(textA)
1620 HLOCAL hloc32W_new;
1621 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1622 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1623 if(countW_new > es->buffer_size + 1)
1625 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1626 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1627 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1628 if(hloc32W_new)
1630 es->hloc32W = hloc32W_new;
1631 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1632 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1634 else
1635 WARN("FAILED! Will synchronize partially\n");
1639 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1640 es->text = LocalLock(es->hloc32W);
1642 if(textA)
1644 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1645 if(_16bit)
1646 LOCAL_Unlock(hInstance, es->hloc16);
1647 else
1648 LocalUnlock(es->hloc32A);
1651 es->lock_count++;
1655 /*********************************************************************
1657 * EDIT_SL_InvalidateText
1659 * Called from EDIT_InvalidateText().
1660 * Does the job for single-line controls only.
1663 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1665 RECT line_rect;
1666 RECT rc;
1668 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1669 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1670 EDIT_UpdateText(es, &rc, TRUE);
1674 /*********************************************************************
1676 * EDIT_ML_InvalidateText
1678 * Called from EDIT_InvalidateText().
1679 * Does the job for multi-line controls only.
1682 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1684 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1685 INT sl = EDIT_EM_LineFromChar(es, start);
1686 INT el = EDIT_EM_LineFromChar(es, end);
1687 INT sc;
1688 INT ec;
1689 RECT rc1;
1690 RECT rcWnd;
1691 RECT rcLine;
1692 RECT rcUpdate;
1693 INT l;
1695 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1696 return;
1698 sc = start - EDIT_EM_LineIndex(es, sl);
1699 ec = end - EDIT_EM_LineIndex(es, el);
1700 if (sl < es->y_offset) {
1701 sl = es->y_offset;
1702 sc = 0;
1704 if (el > es->y_offset + vlc) {
1705 el = es->y_offset + vlc;
1706 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1708 GetClientRect(es->hwndSelf, &rc1);
1709 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1710 if (sl == el) {
1711 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1712 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1713 EDIT_UpdateText(es, &rcUpdate, TRUE);
1714 } else {
1715 EDIT_GetLineRect(es, sl, sc,
1716 EDIT_EM_LineLength(es,
1717 EDIT_EM_LineIndex(es, sl)),
1718 &rcLine);
1719 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1720 EDIT_UpdateText(es, &rcUpdate, TRUE);
1721 for (l = sl + 1 ; l < el ; l++) {
1722 EDIT_GetLineRect(es, l, 0,
1723 EDIT_EM_LineLength(es,
1724 EDIT_EM_LineIndex(es, l)),
1725 &rcLine);
1726 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1727 EDIT_UpdateText(es, &rcUpdate, TRUE);
1729 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1730 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1731 EDIT_UpdateText(es, &rcUpdate, TRUE);
1736 /*********************************************************************
1738 * EDIT_InvalidateText
1740 * Invalidate the text from offset start upto, but not including,
1741 * offset end. Useful for (re)painting the selection.
1742 * Regions outside the linewidth are not invalidated.
1743 * end == -1 means end == TextLength.
1744 * start and end need not be ordered.
1747 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1749 if (end == start)
1750 return;
1752 if (end == -1)
1753 end = strlenW(es->text);
1755 if (end < start) {
1756 INT tmp = start;
1757 start = end;
1758 end = tmp;
1761 if (es->style & ES_MULTILINE)
1762 EDIT_ML_InvalidateText(es, start, end);
1763 else
1764 EDIT_SL_InvalidateText(es, start, end);
1768 /*********************************************************************
1770 * EDIT_MakeFit
1772 * Try to fit size + 1 characters in the buffer.
1774 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1776 HLOCAL hNew32W;
1778 if (size <= es->buffer_size)
1779 return TRUE;
1781 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1783 /* Force edit to unlock it's buffer. es->text now NULL */
1784 EDIT_UnlockBuffer(es, TRUE);
1786 if (es->hloc32W) {
1787 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1788 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1789 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1790 es->hloc32W = hNew32W;
1791 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1795 EDIT_LockBuffer(es);
1797 if (es->buffer_size < size) {
1798 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1799 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1800 return FALSE;
1801 } else {
1802 TRACE("We now have %d+1\n", es->buffer_size);
1803 return TRUE;
1808 /*********************************************************************
1810 * EDIT_MakeUndoFit
1812 * Try to fit size + 1 bytes in the undo buffer.
1815 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1817 UINT alloc_size;
1819 if (size <= es->undo_buffer_size)
1820 return TRUE;
1822 TRACE("trying to ReAlloc to %d+1\n", size);
1824 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1825 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1826 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1827 return TRUE;
1829 else
1831 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1832 return FALSE;
1837 /*********************************************************************
1839 * EDIT_MoveBackward
1842 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1844 INT e = es->selection_end;
1846 if (e) {
1847 e--;
1848 if ((es->style & ES_MULTILINE) && e &&
1849 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1850 e--;
1851 if (e && (es->text[e - 1] == '\r'))
1852 e--;
1855 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1856 EDIT_EM_ScrollCaret(es);
1860 /*********************************************************************
1862 * EDIT_MoveDown_ML
1864 * Only for multi line controls
1865 * Move the caret one line down, on a column with the nearest
1866 * x coordinate on the screen (might be a different column).
1869 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1871 INT s = es->selection_start;
1872 INT e = es->selection_end;
1873 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1874 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1875 INT x = (short)LOWORD(pos);
1876 INT y = (short)HIWORD(pos);
1878 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1879 if (!extend)
1880 s = e;
1881 EDIT_EM_SetSel(es, s, e, after_wrap);
1882 EDIT_EM_ScrollCaret(es);
1886 /*********************************************************************
1888 * EDIT_MoveEnd
1891 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
1893 BOOL after_wrap = FALSE;
1894 INT e;
1896 /* Pass a high value in x to make sure of receiving the end of the line */
1897 if (es->style & ES_MULTILINE)
1898 e = EDIT_CharFromPos(es, 0x3fffffff,
1899 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1900 else
1901 e = strlenW(es->text);
1902 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1903 EDIT_EM_ScrollCaret(es);
1907 /*********************************************************************
1909 * EDIT_MoveForward
1912 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1914 INT e = es->selection_end;
1916 if (es->text[e]) {
1917 e++;
1918 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1919 if (es->text[e] == '\n')
1920 e++;
1921 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1922 e += 2;
1925 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1926 EDIT_EM_ScrollCaret(es);
1930 /*********************************************************************
1932 * EDIT_MoveHome
1934 * Home key: move to beginning of line.
1937 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
1939 INT e;
1941 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1942 if (es->style & ES_MULTILINE)
1943 e = EDIT_CharFromPos(es, -es->x_offset,
1944 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1945 else
1946 e = 0;
1947 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1948 EDIT_EM_ScrollCaret(es);
1952 /*********************************************************************
1954 * EDIT_MovePageDown_ML
1956 * Only for multi line controls
1957 * Move the caret one page down, on a column with the nearest
1958 * x coordinate on the screen (might be a different column).
1961 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1963 INT s = es->selection_start;
1964 INT e = es->selection_end;
1965 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1966 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1967 INT x = (short)LOWORD(pos);
1968 INT y = (short)HIWORD(pos);
1970 e = EDIT_CharFromPos(es, x,
1971 y + (es->format_rect.bottom - es->format_rect.top),
1972 &after_wrap);
1973 if (!extend)
1974 s = e;
1975 EDIT_EM_SetSel(es, s, e, after_wrap);
1976 EDIT_EM_ScrollCaret(es);
1980 /*********************************************************************
1982 * EDIT_MovePageUp_ML
1984 * Only for multi line controls
1985 * Move the caret one page up, on a column with the nearest
1986 * x coordinate on the screen (might be a different column).
1989 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1991 INT s = es->selection_start;
1992 INT e = es->selection_end;
1993 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1994 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1995 INT x = (short)LOWORD(pos);
1996 INT y = (short)HIWORD(pos);
1998 e = EDIT_CharFromPos(es, x,
1999 y - (es->format_rect.bottom - es->format_rect.top),
2000 &after_wrap);
2001 if (!extend)
2002 s = e;
2003 EDIT_EM_SetSel(es, s, e, after_wrap);
2004 EDIT_EM_ScrollCaret(es);
2008 /*********************************************************************
2010 * EDIT_MoveUp_ML
2012 * Only for multi line controls
2013 * Move the caret one line up, on a column with the nearest
2014 * x coordinate on the screen (might be a different column).
2017 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2019 INT s = es->selection_start;
2020 INT e = es->selection_end;
2021 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2022 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2023 INT x = (short)LOWORD(pos);
2024 INT y = (short)HIWORD(pos);
2026 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2027 if (!extend)
2028 s = e;
2029 EDIT_EM_SetSel(es, s, e, after_wrap);
2030 EDIT_EM_ScrollCaret(es);
2034 /*********************************************************************
2036 * EDIT_MoveWordBackward
2039 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2041 INT s = es->selection_start;
2042 INT e = es->selection_end;
2043 INT l;
2044 INT ll;
2045 INT li;
2047 l = EDIT_EM_LineFromChar(es, e);
2048 ll = EDIT_EM_LineLength(es, e);
2049 li = EDIT_EM_LineIndex(es, l);
2050 if (e - li == 0) {
2051 if (l) {
2052 li = EDIT_EM_LineIndex(es, l - 1);
2053 e = li + EDIT_EM_LineLength(es, li);
2055 } else {
2056 e = li + (INT)EDIT_CallWordBreakProc(es,
2057 li, e - li, ll, WB_LEFT);
2059 if (!extend)
2060 s = e;
2061 EDIT_EM_SetSel(es, s, e, FALSE);
2062 EDIT_EM_ScrollCaret(es);
2066 /*********************************************************************
2068 * EDIT_MoveWordForward
2071 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2073 INT s = es->selection_start;
2074 INT e = es->selection_end;
2075 INT l;
2076 INT ll;
2077 INT li;
2079 l = EDIT_EM_LineFromChar(es, e);
2080 ll = EDIT_EM_LineLength(es, e);
2081 li = EDIT_EM_LineIndex(es, l);
2082 if (e - li == ll) {
2083 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2084 e = EDIT_EM_LineIndex(es, l + 1);
2085 } else {
2086 e = li + EDIT_CallWordBreakProc(es,
2087 li, e - li + 1, ll, WB_RIGHT);
2089 if (!extend)
2090 s = e;
2091 EDIT_EM_SetSel(es, s, e, FALSE);
2092 EDIT_EM_ScrollCaret(es);
2096 /*********************************************************************
2098 * EDIT_PaintLine
2101 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2103 INT s = es->selection_start;
2104 INT e = es->selection_end;
2105 INT li;
2106 INT ll;
2107 INT x;
2108 INT y;
2109 LRESULT pos;
2111 if (es->style & ES_MULTILINE) {
2112 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2113 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2114 return;
2115 } else if (line)
2116 return;
2118 TRACE("line=%d\n", line);
2120 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2121 x = (short)LOWORD(pos);
2122 y = (short)HIWORD(pos);
2123 li = EDIT_EM_LineIndex(es, line);
2124 ll = EDIT_EM_LineLength(es, li);
2125 s = min(es->selection_start, es->selection_end);
2126 e = max(es->selection_start, es->selection_end);
2127 s = min(li + ll, max(li, s));
2128 e = min(li + ll, max(li, e));
2129 if (rev && (s != e) &&
2130 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2131 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2132 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2133 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2134 } else
2135 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2139 /*********************************************************************
2141 * EDIT_PaintText
2144 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2146 COLORREF BkColor;
2147 COLORREF TextColor;
2148 INT ret;
2149 INT li;
2150 INT BkMode;
2151 SIZE size;
2153 if (!count)
2154 return 0;
2155 BkMode = GetBkMode(dc);
2156 BkColor = GetBkColor(dc);
2157 TextColor = GetTextColor(dc);
2158 if (rev) {
2159 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2160 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2161 SetBkMode( dc, OPAQUE);
2163 li = EDIT_EM_LineIndex(es, line);
2164 if (es->style & ES_MULTILINE) {
2165 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2166 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2167 } else {
2168 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2169 TextOutW(dc, x, y, text + li + col, count);
2170 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2171 ret = size.cx;
2172 if (es->style & ES_PASSWORD)
2173 HeapFree(GetProcessHeap(), 0, text);
2175 if (rev) {
2176 SetBkColor(dc, BkColor);
2177 SetTextColor(dc, TextColor);
2178 SetBkMode( dc, BkMode);
2180 return ret;
2184 /*********************************************************************
2186 * EDIT_SetCaretPos
2189 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2190 BOOL after_wrap)
2192 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2193 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2194 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2198 /*********************************************************************
2200 * EDIT_SetRectNP
2202 * note: this is not (exactly) the handler called on EM_SETRECTNP
2203 * it is also used to set the rect of a single line control
2206 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2208 RECT ClientRect;
2209 LONG_PTR ExStyle;
2211 CopyRect(&es->format_rect, rc);
2212 if (es->style & ES_MULTILINE)
2214 if (es->style & WS_BORDER) {
2215 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2216 es->format_rect.left += bw;
2217 es->format_rect.right -= bw;
2218 es->format_rect.top += bw;
2219 es->format_rect.bottom -= bw;
2222 else
2224 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2225 if (ExStyle & WS_EX_CLIENTEDGE) {
2226 if (es->line_height + 2 <=
2227 es->format_rect.bottom - es->format_rect.top) {
2228 es->format_rect.top++;
2229 es->format_rect.bottom--;
2231 } else if (es->style & WS_BORDER) {
2232 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2233 es->format_rect.left += bw;
2234 es->format_rect.right -= bw;
2235 if (es->line_height + 2 * bw <=
2236 es->format_rect.bottom - es->format_rect.top) {
2237 es->format_rect.top += bw;
2238 es->format_rect.bottom -= bw;
2242 es->format_rect.left += es->left_margin;
2243 es->format_rect.right -= es->right_margin;
2244 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2245 if (es->style & ES_MULTILINE)
2247 INT fw, vlc, max_x_offset, max_y_offset;
2249 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2250 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2252 /* correct es->x_offset */
2253 fw = es->format_rect.right - es->format_rect.left;
2254 max_x_offset = es->text_width - fw;
2255 if(max_x_offset < 0) max_x_offset = 0;
2256 if(es->x_offset > max_x_offset)
2257 es->x_offset = max_x_offset;
2259 /* correct es->y_offset */
2260 max_y_offset = es->line_count - vlc;
2261 if(max_y_offset < 0) max_y_offset = 0;
2262 if(es->y_offset > max_y_offset)
2263 es->y_offset = max_y_offset;
2265 /* force scroll info update */
2266 EDIT_UpdateScrollInfo(es);
2268 else
2269 /* Windows doesn't care to fix text placement for SL controls */
2270 es->format_rect.bottom = es->format_rect.top + es->line_height;
2272 /* Always stay within the client area */
2273 GetClientRect(es->hwndSelf, &ClientRect);
2274 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2276 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2277 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
2279 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2283 /*********************************************************************
2285 * EDIT_UnlockBuffer
2288 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2290 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2292 /* Edit window might be already destroyed */
2293 if(!IsWindow(es->hwndSelf))
2295 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2296 return;
2299 if (!es->lock_count) {
2300 ERR("lock_count == 0 ... please report\n");
2301 return;
2303 if (!es->text) {
2304 ERR("es->text == 0 ... please report\n");
2305 return;
2308 if (force || (es->lock_count == 1)) {
2309 if (es->hloc32W) {
2310 CHAR *textA = NULL;
2311 BOOL _16bit = FALSE;
2312 UINT countA = 0;
2313 UINT countW = strlenW(es->text) + 1;
2315 if(es->hloc32A)
2317 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2318 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2319 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2320 countA = LocalSize(es->hloc32A);
2321 if(countA_new > countA)
2323 HLOCAL hloc32A_new;
2324 UINT alloc_size = ROUND_TO_GROW(countA_new);
2325 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2326 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2327 if(hloc32A_new)
2329 es->hloc32A = hloc32A_new;
2330 countA = LocalSize(hloc32A_new);
2331 TRACE("Real new size %d bytes\n", countA);
2333 else
2334 WARN("FAILED! Will synchronize partially\n");
2336 textA = LocalLock(es->hloc32A);
2338 else if(es->hloc16)
2340 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2341 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2342 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2343 countA = LOCAL_Size(hInstance, es->hloc16);
2344 if(countA_new > countA)
2346 HLOCAL16 hloc16_new;
2347 UINT alloc_size = ROUND_TO_GROW(countA_new);
2348 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2349 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2350 if(hloc16_new)
2352 es->hloc16 = hloc16_new;
2353 countA = LOCAL_Size(hInstance, hloc16_new);
2354 TRACE("Real new size %d bytes\n", countA);
2356 else
2357 WARN("FAILED! Will synchronize partially\n");
2359 textA = LOCAL_Lock(hInstance, es->hloc16);
2360 _16bit = TRUE;
2363 if(textA)
2365 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2366 if(_16bit)
2367 LOCAL_Unlock(hInstance, es->hloc16);
2368 else
2369 LocalUnlock(es->hloc32A);
2372 LocalUnlock(es->hloc32W);
2373 es->text = NULL;
2375 else {
2376 ERR("no buffer ... please report\n");
2377 return;
2380 es->lock_count--;
2384 /*********************************************************************
2386 * EDIT_UpdateScrollInfo
2389 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2391 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2393 SCROLLINFO si;
2394 si.cbSize = sizeof(SCROLLINFO);
2395 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2396 si.nMin = 0;
2397 si.nMax = es->line_count - 1;
2398 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2399 si.nPos = es->y_offset;
2400 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2401 si.nMin, si.nMax, si.nPage, si.nPos);
2402 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2405 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2407 SCROLLINFO si;
2408 si.cbSize = sizeof(SCROLLINFO);
2409 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2410 si.nMin = 0;
2411 si.nMax = es->text_width - 1;
2412 si.nPage = es->format_rect.right - es->format_rect.left;
2413 si.nPos = es->x_offset;
2414 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2415 si.nMin, si.nMax, si.nPage, si.nPos);
2416 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2420 /*********************************************************************
2422 * EDIT_WordBreakProc
2424 * Find the beginning of words.
2425 * Note: unlike the specs for a WordBreakProc, this function only
2426 * allows to be called without linebreaks between s[0] upto
2427 * s[count - 1]. Remember it is only called
2428 * internally, so we can decide this for ourselves.
2431 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2433 INT ret = 0;
2435 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2437 if(!s) return 0;
2439 switch (action) {
2440 case WB_LEFT:
2441 if (!count)
2442 break;
2443 if (index)
2444 index--;
2445 if (s[index] == ' ') {
2446 while (index && (s[index] == ' '))
2447 index--;
2448 if (index) {
2449 while (index && (s[index] != ' '))
2450 index--;
2451 if (s[index] == ' ')
2452 index++;
2454 } else {
2455 while (index && (s[index] != ' '))
2456 index--;
2457 if (s[index] == ' ')
2458 index++;
2460 ret = index;
2461 break;
2462 case WB_RIGHT:
2463 if (!count)
2464 break;
2465 if (index)
2466 index--;
2467 if (s[index] == ' ')
2468 while ((index < count) && (s[index] == ' ')) index++;
2469 else {
2470 while (s[index] && (s[index] != ' ') && (index < count))
2471 index++;
2472 while ((s[index] == ' ') && (index < count)) index++;
2474 ret = index;
2475 break;
2476 case WB_ISDELIMITER:
2477 ret = (s[index] == ' ');
2478 break;
2479 default:
2480 ERR("unknown action code, please report !\n");
2481 break;
2483 return ret;
2487 /*********************************************************************
2489 * EM_CHARFROMPOS
2491 * returns line number (not index) in high-order word of result.
2492 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2493 * to Richedit, not to the edit control. Original documentation is valid.
2494 * FIXME: do the specs mean to return -1 if outside client area or
2495 * if outside formatting rectangle ???
2498 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2500 POINT pt;
2501 RECT rc;
2502 INT index;
2504 pt.x = x;
2505 pt.y = y;
2506 GetClientRect(es->hwndSelf, &rc);
2507 if (!PtInRect(&rc, pt))
2508 return -1;
2510 index = EDIT_CharFromPos(es, x, y, NULL);
2511 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2515 /*********************************************************************
2517 * EM_FMTLINES
2519 * Enable or disable soft breaks.
2521 * This means: insert or remove the soft linebreak character (\r\r\n).
2522 * Take care to check if the text still fits the buffer after insertion.
2523 * If not, notify with EN_ERRSPACE.
2526 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2528 es->flags &= ~EF_USE_SOFTBRK;
2529 if (add_eol) {
2530 es->flags |= EF_USE_SOFTBRK;
2531 FIXME("soft break enabled, not implemented\n");
2533 return add_eol;
2537 /*********************************************************************
2539 * EM_GETHANDLE
2541 * Hopefully this won't fire back at us.
2542 * We always start with a fixed buffer in the local heap.
2543 * Despite of the documentation says that the local heap is used
2544 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2545 * buffer on the local heap.
2548 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2550 HLOCAL hLocal;
2552 if (!(es->style & ES_MULTILINE))
2553 return 0;
2555 if(es->is_unicode)
2556 hLocal = es->hloc32W;
2557 else
2559 if(!es->hloc32A)
2561 CHAR *textA;
2562 UINT countA, alloc_size;
2563 TRACE("Allocating 32-bit ANSI alias buffer\n");
2564 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2565 alloc_size = ROUND_TO_GROW(countA);
2566 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2568 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2569 return 0;
2571 textA = LocalLock(es->hloc32A);
2572 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2573 LocalUnlock(es->hloc32A);
2575 hLocal = es->hloc32A;
2578 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2579 return hLocal;
2583 /*********************************************************************
2585 * EM_GETHANDLE16
2587 * Hopefully this won't fire back at us.
2588 * We always start with a buffer in 32 bit linear memory.
2589 * However, with this message a 16 bit application requests
2590 * a handle of 16 bit local heap memory, where it expects to find
2591 * the text.
2592 * It's a pitty that from this moment on we have to use this
2593 * local heap, because applications may rely on the handle
2594 * in the future.
2596 * In this function we'll try to switch to local heap.
2598 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2600 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2601 CHAR *textA;
2602 UINT countA, alloc_size;
2604 if (!(es->style & ES_MULTILINE))
2605 return 0;
2607 if (es->hloc16)
2608 return es->hloc16;
2610 if (!LOCAL_HeapSize(hInstance)) {
2611 if (!LocalInit16(hInstance, 0,
2612 GlobalSize16(hInstance))) {
2613 ERR("could not initialize local heap\n");
2614 return 0;
2616 TRACE("local heap initialized\n");
2619 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2620 alloc_size = ROUND_TO_GROW(countA);
2622 TRACE("Allocating 16-bit ANSI alias buffer\n");
2623 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2624 ERR("could not allocate new 16 bit buffer\n");
2625 return 0;
2628 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2629 ERR("could not lock new 16 bit buffer\n");
2630 LOCAL_Free(hInstance, es->hloc16);
2631 es->hloc16 = 0;
2632 return 0;
2635 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2636 LOCAL_Unlock(hInstance, es->hloc16);
2638 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2639 return es->hloc16;
2643 /*********************************************************************
2645 * EM_GETLINE
2648 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2650 LPWSTR src;
2651 INT line_len, dst_len;
2652 INT i;
2654 if (es->style & ES_MULTILINE) {
2655 if (line >= es->line_count)
2656 return 0;
2657 } else
2658 line = 0;
2659 i = EDIT_EM_LineIndex(es, line);
2660 src = es->text + i;
2661 line_len = EDIT_EM_LineLength(es, i);
2662 dst_len = *(WORD *)dst;
2663 if(unicode)
2665 if(dst_len <= line_len)
2667 memcpy(dst, src, dst_len * sizeof(WCHAR));
2668 return dst_len;
2670 else /* Append 0 if enough space */
2672 memcpy(dst, src, line_len * sizeof(WCHAR));
2673 dst[line_len] = 0;
2674 return line_len;
2677 else
2679 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2680 if(!ret) /* Insufficient buffer size */
2681 return dst_len;
2682 if(ret < dst_len) /* Append 0 if enough space */
2683 ((LPSTR)dst)[ret] = 0;
2684 return ret;
2689 /*********************************************************************
2691 * EM_GETSEL
2694 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2696 UINT s = es->selection_start;
2697 UINT e = es->selection_end;
2699 ORDER_UINT(s, e);
2700 if (start)
2701 *start = s;
2702 if (end)
2703 *end = e;
2704 return MAKELONG(s, e);
2708 /*********************************************************************
2710 * EM_GETTHUMB
2712 * FIXME: is this right ? (or should it be only VSCROLL)
2713 * (and maybe only for edit controls that really have their
2714 * own scrollbars) (and maybe only for multiline controls ?)
2715 * All in all: very poorly documented
2718 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2720 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2721 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2725 /*********************************************************************
2727 * EM_LINEFROMCHAR
2730 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2732 INT line;
2733 LINEDEF *line_def;
2735 if (!(es->style & ES_MULTILINE))
2736 return 0;
2737 if (index > (INT)strlenW(es->text))
2738 return es->line_count - 1;
2739 if (index == -1)
2740 index = min(es->selection_start, es->selection_end);
2742 line = 0;
2743 line_def = es->first_line_def;
2744 index -= line_def->length;
2745 while ((index >= 0) && line_def->next) {
2746 line++;
2747 line_def = line_def->next;
2748 index -= line_def->length;
2750 return line;
2754 /*********************************************************************
2756 * EM_LINEINDEX
2759 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2761 INT line_index;
2762 LINEDEF *line_def;
2764 if (!(es->style & ES_MULTILINE))
2765 return 0;
2766 if (line >= es->line_count)
2767 return -1;
2769 line_index = 0;
2770 line_def = es->first_line_def;
2771 if (line == -1) {
2772 INT index = es->selection_end - line_def->length;
2773 while ((index >= 0) && line_def->next) {
2774 line_index += line_def->length;
2775 line_def = line_def->next;
2776 index -= line_def->length;
2778 } else {
2779 while (line > 0) {
2780 line_index += line_def->length;
2781 line_def = line_def->next;
2782 line--;
2785 return line_index;
2789 /*********************************************************************
2791 * EM_LINELENGTH
2794 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2796 LINEDEF *line_def;
2798 if (!(es->style & ES_MULTILINE))
2799 return strlenW(es->text);
2801 if (index == -1) {
2802 /* get the number of remaining non-selected chars of selected lines */
2803 INT32 l; /* line number */
2804 INT32 li; /* index of first char in line */
2805 INT32 count;
2806 l = EDIT_EM_LineFromChar(es, es->selection_start);
2807 /* # chars before start of selection area */
2808 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2809 l = EDIT_EM_LineFromChar(es, es->selection_end);
2810 /* # chars after end of selection */
2811 li = EDIT_EM_LineIndex(es, l);
2812 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2813 return count;
2815 line_def = es->first_line_def;
2816 index -= line_def->length;
2817 while ((index >= 0) && line_def->next) {
2818 line_def = line_def->next;
2819 index -= line_def->length;
2821 return line_def->net_length;
2825 /*********************************************************************
2827 * EM_LINESCROLL
2829 * NOTE: dx is in average character widths, dy - in lines;
2832 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
2834 if (!(es->style & ES_MULTILINE))
2835 return FALSE;
2837 dx *= es->char_width;
2838 return EDIT_EM_LineScroll_internal(es, dx, dy);
2841 /*********************************************************************
2843 * EDIT_EM_LineScroll_internal
2845 * Version of EDIT_EM_LineScroll for internal use.
2846 * It doesn't refuse if ES_MULTILINE is set and assumes that
2847 * dx is in pixels, dy - in lines.
2850 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
2852 INT nyoff;
2853 INT x_offset_in_pixels;
2854 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
2855 es->line_height;
2857 if (es->style & ES_MULTILINE)
2859 x_offset_in_pixels = es->x_offset;
2861 else
2863 dy = 0;
2864 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
2867 if (-dx > x_offset_in_pixels)
2868 dx = -x_offset_in_pixels;
2869 if (dx > es->text_width - x_offset_in_pixels)
2870 dx = es->text_width - x_offset_in_pixels;
2871 nyoff = max(0, es->y_offset + dy);
2872 if (nyoff >= es->line_count - lines_per_page)
2873 nyoff = max(0, es->line_count - lines_per_page);
2874 dy = (es->y_offset - nyoff) * es->line_height;
2875 if (dx || dy) {
2876 RECT rc1;
2877 RECT rc;
2879 es->y_offset = nyoff;
2880 if(es->style & ES_MULTILINE)
2881 es->x_offset += dx;
2882 else
2883 es->x_offset += dx / es->char_width;
2885 GetClientRect(es->hwndSelf, &rc1);
2886 IntersectRect(&rc, &rc1, &es->format_rect);
2887 ScrollWindowEx(es->hwndSelf, -dx, dy,
2888 NULL, &rc, NULL, NULL, SW_INVALIDATE);
2889 /* force scroll info update */
2890 EDIT_UpdateScrollInfo(es);
2892 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2893 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2894 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2895 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2896 return TRUE;
2900 /*********************************************************************
2902 * EM_POSFROMCHAR
2905 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
2907 INT len = strlenW(es->text);
2908 INT l;
2909 INT li;
2910 INT x;
2911 INT y = 0;
2912 INT w;
2913 INT lw = 0;
2914 INT ll = 0;
2915 HDC dc;
2916 HFONT old_font = 0;
2917 SIZE size;
2918 LINEDEF *line_def;
2920 index = min(index, len);
2921 dc = GetDC(es->hwndSelf);
2922 if (es->font)
2923 old_font = SelectObject(dc, es->font);
2924 if (es->style & ES_MULTILINE) {
2925 l = EDIT_EM_LineFromChar(es, index);
2926 y = (l - es->y_offset) * es->line_height;
2927 li = EDIT_EM_LineIndex(es, l);
2928 if (after_wrap && (li == index) && l) {
2929 INT l2 = l - 1;
2930 line_def = es->first_line_def;
2931 while (l2) {
2932 line_def = line_def->next;
2933 l2--;
2935 if (line_def->ending == END_WRAP) {
2936 l--;
2937 y -= es->line_height;
2938 li = EDIT_EM_LineIndex(es, l);
2942 line_def = es->first_line_def;
2943 while (line_def->index != li)
2944 line_def = line_def->next;
2946 ll = line_def->net_length;
2947 lw = line_def->width;
2949 w = es->format_rect.right - es->format_rect.left;
2950 if (es->style & ES_RIGHT)
2952 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
2953 es->tabs_count, es->tabs)) - es->x_offset;
2954 x = w - x;
2956 else if (es->style & ES_CENTER)
2958 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2959 es->tabs_count, es->tabs)) - es->x_offset;
2960 x += (w - lw) / 2;
2962 else /* ES_LEFT */
2964 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2965 es->tabs_count, es->tabs)) - es->x_offset;
2967 } else {
2968 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2969 if (index < es->x_offset) {
2970 GetTextExtentPoint32W(dc, text + index,
2971 es->x_offset - index, &size);
2972 x = -size.cx;
2973 } else {
2974 GetTextExtentPoint32W(dc, text + es->x_offset,
2975 index - es->x_offset, &size);
2976 x = size.cx;
2978 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
2980 w = es->format_rect.right - es->format_rect.left;
2981 if (w > es->text_width)
2983 if (es->style & ES_RIGHT)
2984 x += w - es->text_width;
2985 else if (es->style & ES_CENTER)
2986 x += (w - es->text_width) / 2;
2990 y = 0;
2991 if (es->style & ES_PASSWORD)
2992 HeapFree(GetProcessHeap(), 0, text);
2994 x += es->format_rect.left;
2995 y += es->format_rect.top;
2996 if (es->font)
2997 SelectObject(dc, old_font);
2998 ReleaseDC(es->hwndSelf, dc);
2999 return MAKELONG((INT16)x, (INT16)y);
3003 /*********************************************************************
3005 * EM_REPLACESEL
3007 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3010 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3012 UINT strl = strlenW(lpsz_replace);
3013 UINT tl = strlenW(es->text);
3014 UINT utl;
3015 UINT s;
3016 UINT e;
3017 UINT i;
3018 UINT size;
3019 LPWSTR p;
3020 HRGN hrgn = 0;
3021 LPWSTR buf = NULL;
3022 UINT bufl = 0;
3024 TRACE("%s, can_undo %d, send_update %d\n",
3025 debugstr_w(lpsz_replace), can_undo, send_update);
3027 s = es->selection_start;
3028 e = es->selection_end;
3030 if ((s == e) && !strl)
3031 return;
3033 ORDER_UINT(s, e);
3035 size = tl - (e - s) + strl;
3036 if (!size)
3037 es->text_width = 0;
3039 /* Issue the EN_MAXTEXT notification and continue with replacing text
3040 * such that buffer limit is honored. */
3041 if ((honor_limit) && (es->buffer_limit > 0) && (size > es->buffer_limit)) {
3042 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3043 strl = es->buffer_limit - (tl - (e-s));
3046 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3047 return;
3049 if (e != s) {
3050 /* there is something to be deleted */
3051 TRACE("deleting stuff.\n");
3052 bufl = e - s;
3053 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3054 if (!buf) return;
3055 strncpyW(buf, es->text + s, bufl);
3056 buf[bufl] = 0; /* ensure 0 termination */
3057 /* now delete */
3058 strcpyW(es->text + s, es->text + e);
3060 if (strl) {
3061 /* there is an insertion */
3062 tl = strlenW(es->text);
3063 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));
3064 for (p = es->text + tl ; p >= es->text + s ; p--)
3065 p[strl] = p[0];
3066 for (i = 0 , p = es->text + s ; i < strl ; i++)
3067 p[i] = lpsz_replace[i];
3068 if(es->style & ES_UPPERCASE)
3069 CharUpperBuffW(p, strl);
3070 else if(es->style & ES_LOWERCASE)
3071 CharLowerBuffW(p, strl);
3073 if (es->style & ES_MULTILINE)
3075 INT st = min(es->selection_start, es->selection_end);
3076 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3078 hrgn = CreateRectRgn(0, 0, 0, 0);
3079 EDIT_BuildLineDefs_ML(es, st, st + strl,
3080 strl - abs(es->selection_end - es->selection_start), hrgn);
3081 /* if text is too long undo all changes */
3082 if (!(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3083 if (strl)
3084 strcpyW(es->text + e, es->text + e + strl);
3085 if (e != s)
3086 for (i = 0 , p = es->text ; i < e - s ; i++)
3087 p[i + s] = buf[i];
3088 EDIT_BuildLineDefs_ML(es, s, e,
3089 abs(es->selection_end - es->selection_start) - strl, hrgn);
3090 strl = 0;
3091 e = s;
3092 hrgn = CreateRectRgn(0, 0, 0, 0);
3093 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3096 else {
3097 INT fw = es->format_rect.right - es->format_rect.left;
3098 EDIT_CalcLineWidth_SL(es);
3099 /* remove chars that don't fit */
3100 if (!(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3101 while ((es->text_width > fw) && s + strl >= s) {
3102 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3103 strl--;
3104 EDIT_CalcLineWidth_SL(es);
3106 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
3110 if (e != s) {
3111 if (can_undo) {
3112 utl = strlenW(es->undo_text);
3113 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3114 /* undo-buffer is extended to the right */
3115 EDIT_MakeUndoFit(es, utl + e - s);
3116 strncpyW(es->undo_text + utl, buf, e - s + 1);
3117 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3118 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3119 /* undo-buffer is extended to the left */
3120 EDIT_MakeUndoFit(es, utl + e - s);
3121 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3122 p[e - s] = p[0];
3123 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3124 p[i] = buf[i];
3125 es->undo_position = s;
3126 } else {
3127 /* new undo-buffer */
3128 EDIT_MakeUndoFit(es, e - s);
3129 strncpyW(es->undo_text, buf, e - s + 1);
3130 es->undo_text[e - s] = 0; /* ensure 0 termination */
3131 es->undo_position = s;
3133 /* any deletion makes the old insertion-undo invalid */
3134 es->undo_insert_count = 0;
3135 } else
3136 EDIT_EM_EmptyUndoBuffer(es);
3138 if (strl) {
3139 if (can_undo) {
3140 if ((s == es->undo_position) ||
3141 ((es->undo_insert_count) &&
3142 (s == es->undo_position + es->undo_insert_count)))
3144 * insertion is new and at delete position or
3145 * an extension to either left or right
3147 es->undo_insert_count += strl;
3148 else {
3149 /* new insertion undo */
3150 es->undo_position = s;
3151 es->undo_insert_count = strl;
3152 /* new insertion makes old delete-buffer invalid */
3153 *es->undo_text = '\0';
3155 } else
3156 EDIT_EM_EmptyUndoBuffer(es);
3159 if (bufl)
3160 HeapFree(GetProcessHeap(), 0, buf);
3162 s += strl;
3164 /* If text has been deleted and we're right or center aligned then scroll rightward */
3165 if (es->style & (ES_RIGHT | ES_CENTER))
3167 INT delta = strl - abs(es->selection_end - es->selection_start);
3169 if (delta < 0 && es->x_offset)
3171 if (abs(delta) > es->x_offset)
3172 es->x_offset = 0;
3173 else
3174 es->x_offset += delta;
3178 EDIT_EM_SetSel(es, s, s, FALSE);
3179 es->flags |= EF_MODIFIED;
3180 if (send_update) es->flags |= EF_UPDATE;
3181 if (hrgn)
3183 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3184 DeleteObject(hrgn);
3186 else
3187 EDIT_UpdateText(es, NULL, TRUE);
3189 EDIT_EM_ScrollCaret(es);
3191 /* force scroll info update */
3192 EDIT_UpdateScrollInfo(es);
3195 if(send_update || (es->flags & EF_UPDATE))
3197 es->flags &= ~EF_UPDATE;
3198 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3203 /*********************************************************************
3205 * EM_SCROLL
3208 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3210 INT dy;
3212 if (!(es->style & ES_MULTILINE))
3213 return (LRESULT)FALSE;
3215 dy = 0;
3217 switch (action) {
3218 case SB_LINEUP:
3219 if (es->y_offset)
3220 dy = -1;
3221 break;
3222 case SB_LINEDOWN:
3223 if (es->y_offset < es->line_count - 1)
3224 dy = 1;
3225 break;
3226 case SB_PAGEUP:
3227 if (es->y_offset)
3228 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3229 break;
3230 case SB_PAGEDOWN:
3231 if (es->y_offset < es->line_count - 1)
3232 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3233 break;
3234 default:
3235 return (LRESULT)FALSE;
3237 if (dy) {
3238 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3239 /* check if we are going to move too far */
3240 if(es->y_offset + dy > es->line_count - vlc)
3241 dy = es->line_count - vlc - es->y_offset;
3243 /* Notification is done in EDIT_EM_LineScroll */
3244 if(dy)
3245 EDIT_EM_LineScroll(es, 0, dy);
3247 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3251 /*********************************************************************
3253 * EM_SCROLLCARET
3256 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3258 if (es->style & ES_MULTILINE) {
3259 INT l;
3260 INT li;
3261 INT vlc;
3262 INT ww;
3263 INT cw = es->char_width;
3264 INT x;
3265 INT dy = 0;
3266 INT dx = 0;
3268 l = EDIT_EM_LineFromChar(es, es->selection_end);
3269 li = EDIT_EM_LineIndex(es, l);
3270 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3271 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3272 if (l >= es->y_offset + vlc)
3273 dy = l - vlc + 1 - es->y_offset;
3274 if (l < es->y_offset)
3275 dy = l - es->y_offset;
3276 ww = es->format_rect.right - es->format_rect.left;
3277 if (x < es->format_rect.left)
3278 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3279 if (x > es->format_rect.right)
3280 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3281 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3283 /* check if we are going to move too far */
3284 if(es->x_offset + dx + ww > es->text_width)
3285 dx = es->text_width - ww - es->x_offset;
3286 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3287 EDIT_EM_LineScroll_internal(es, dx, dy);
3289 } else {
3290 INT x;
3291 INT goal;
3292 INT format_width;
3294 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3295 format_width = es->format_rect.right - es->format_rect.left;
3296 if (x < es->format_rect.left) {
3297 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3298 do {
3299 es->x_offset--;
3300 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3301 } while ((x < goal) && es->x_offset);
3302 /* FIXME: use ScrollWindow() somehow to improve performance */
3303 EDIT_UpdateText(es, NULL, TRUE);
3304 } else if (x > es->format_rect.right) {
3305 INT x_last;
3306 INT len = strlenW(es->text);
3307 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3308 do {
3309 es->x_offset++;
3310 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3311 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3312 } while ((x > goal) && (x_last > es->format_rect.right));
3313 /* FIXME: use ScrollWindow() somehow to improve performance */
3314 EDIT_UpdateText(es, NULL, TRUE);
3318 if(es->flags & EF_FOCUSED)
3319 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3323 /*********************************************************************
3325 * EM_SETHANDLE
3327 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3330 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3332 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3334 if (!(es->style & ES_MULTILINE))
3335 return;
3337 if (!hloc) {
3338 WARN("called with NULL handle\n");
3339 return;
3342 EDIT_UnlockBuffer(es, TRUE);
3344 if(es->hloc16)
3346 LOCAL_Free(hInstance, es->hloc16);
3347 es->hloc16 = (HLOCAL16)NULL;
3350 if(es->is_unicode)
3352 if(es->hloc32A)
3354 LocalFree(es->hloc32A);
3355 es->hloc32A = NULL;
3357 es->hloc32W = hloc;
3359 else
3361 INT countW, countA;
3362 HLOCAL hloc32W_new;
3363 WCHAR *textW;
3364 CHAR *textA;
3366 countA = LocalSize(hloc);
3367 textA = LocalLock(hloc);
3368 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3369 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3371 ERR("Could not allocate new unicode buffer\n");
3372 return;
3374 textW = LocalLock(hloc32W_new);
3375 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3376 LocalUnlock(hloc32W_new);
3377 LocalUnlock(hloc);
3379 if(es->hloc32W)
3380 LocalFree(es->hloc32W);
3382 es->hloc32W = hloc32W_new;
3383 es->hloc32A = hloc;
3386 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3388 EDIT_LockBuffer(es);
3390 es->x_offset = es->y_offset = 0;
3391 es->selection_start = es->selection_end = 0;
3392 EDIT_EM_EmptyUndoBuffer(es);
3393 es->flags &= ~EF_MODIFIED;
3394 es->flags &= ~EF_UPDATE;
3395 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3396 EDIT_UpdateText(es, NULL, TRUE);
3397 EDIT_EM_ScrollCaret(es);
3398 /* force scroll info update */
3399 EDIT_UpdateScrollInfo(es);
3403 /*********************************************************************
3405 * EM_SETHANDLE16
3407 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3410 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3412 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
3413 INT countW, countA;
3414 HLOCAL hloc32W_new;
3415 WCHAR *textW;
3416 CHAR *textA;
3418 if (!(es->style & ES_MULTILINE))
3419 return;
3421 if (!hloc) {
3422 WARN("called with NULL handle\n");
3423 return;
3426 EDIT_UnlockBuffer(es, TRUE);
3428 if(es->hloc32A)
3430 LocalFree(es->hloc32A);
3431 es->hloc32A = NULL;
3434 countA = LOCAL_Size(hInstance, hloc);
3435 textA = LOCAL_Lock(hInstance, hloc);
3436 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3437 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3439 ERR("Could not allocate new unicode buffer\n");
3440 return;
3442 textW = LocalLock(hloc32W_new);
3443 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3444 LocalUnlock(hloc32W_new);
3445 LOCAL_Unlock(hInstance, hloc);
3447 if(es->hloc32W)
3448 LocalFree(es->hloc32W);
3450 es->hloc32W = hloc32W_new;
3451 es->hloc16 = hloc;
3453 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3455 EDIT_LockBuffer(es);
3457 es->x_offset = es->y_offset = 0;
3458 es->selection_start = es->selection_end = 0;
3459 EDIT_EM_EmptyUndoBuffer(es);
3460 es->flags &= ~EF_MODIFIED;
3461 es->flags &= ~EF_UPDATE;
3462 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3463 EDIT_UpdateText(es, NULL, TRUE);
3464 EDIT_EM_ScrollCaret(es);
3465 /* force scroll info update */
3466 EDIT_UpdateScrollInfo(es);
3470 /*********************************************************************
3472 * EM_SETLIMITTEXT
3474 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3475 * However, the windows version is not complied to yet in all of edit.c
3477 * Additionally as the wrapper for RichEdit controls we need larger buffers
3478 * at present -1 will represent nolimit
3480 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3482 if (limit == 0xFFFFFFFF)
3483 es->buffer_limit = -1;
3484 else if (es->style & ES_MULTILINE) {
3485 if (limit)
3486 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3487 else
3488 es->buffer_limit = BUFLIMIT_MULTI;
3489 } else {
3490 if (limit)
3491 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3492 else
3493 es->buffer_limit = BUFLIMIT_SINGLE;
3498 /*********************************************************************
3500 * EM_SETMARGINS
3502 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3503 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3504 * margin according to the textmetrics of the current font.
3506 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3507 * of the char's width as the margin, but this is not how Windows handles this.
3508 * For all other fonts Windows sets the margins to zero.
3511 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3512 INT left, INT right)
3514 TEXTMETRICW tm;
3515 INT default_left_margin = 0; /* in pixels */
3516 INT default_right_margin = 0; /* in pixels */
3518 /* Set the default margins depending on the font */
3519 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3520 HDC dc = GetDC(es->hwndSelf);
3521 HFONT old_font = SelectObject(dc, es->font);
3522 GetTextMetricsW(dc, &tm);
3523 /* The default margins are only non zero for TrueType or Vector fonts */
3524 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3525 /* This must be calculated more exactly! But how? */
3526 default_left_margin = tm.tmAveCharWidth / 3;
3527 default_right_margin = tm.tmAveCharWidth / 3;
3529 SelectObject(dc, old_font);
3530 ReleaseDC(es->hwndSelf, dc);
3533 if (action & EC_LEFTMARGIN) {
3534 if (left != EC_USEFONTINFO)
3535 es->left_margin = left;
3536 else
3537 es->left_margin = default_left_margin;
3540 if (action & EC_RIGHTMARGIN) {
3541 if (right != EC_USEFONTINFO)
3542 es->right_margin = right;
3543 else
3544 es->right_margin = default_right_margin;
3546 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3550 /*********************************************************************
3552 * EM_SETPASSWORDCHAR
3555 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3557 LONG style;
3559 if (es->style & ES_MULTILINE)
3560 return;
3562 if (es->password_char == c)
3563 return;
3565 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3566 es->password_char = c;
3567 if (c) {
3568 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3569 es->style |= ES_PASSWORD;
3570 } else {
3571 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3572 es->style &= ~ES_PASSWORD;
3574 EDIT_UpdateText(es, NULL, TRUE);
3578 /*********************************************************************
3580 * EDIT_EM_SetSel
3582 * note: unlike the specs say: the order of start and end
3583 * _is_ preserved in Windows. (i.e. start can be > end)
3584 * In other words: this handler is OK
3587 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3589 UINT old_start = es->selection_start;
3590 UINT old_end = es->selection_end;
3591 UINT len = strlenW(es->text);
3593 if (start == (UINT)-1) {
3594 start = es->selection_end;
3595 end = es->selection_end;
3596 } else {
3597 start = min(start, len);
3598 end = min(end, len);
3600 es->selection_start = start;
3601 es->selection_end = end;
3602 if (after_wrap)
3603 es->flags |= EF_AFTER_WRAP;
3604 else
3605 es->flags &= ~EF_AFTER_WRAP;
3606 /* Compute the necessary invalidation region. */
3607 /* Note that we don't need to invalidate regions which have
3608 * "never" been selected, or those which are "still" selected.
3609 * In fact, every time we hit a selection boundary, we can
3610 * *toggle* whether we need to invalidate. Thus we can optimize by
3611 * *sorting* the interval endpoints. Let's assume that we sort them
3612 * in this order:
3613 * start <= end <= old_start <= old_end
3614 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3615 * in 5 comparisons; ie it's impossible to do better than the
3616 * following: */
3617 ORDER_UINT(end, old_end);
3618 ORDER_UINT(start, old_start);
3619 ORDER_UINT(old_start, old_end);
3620 ORDER_UINT(start, end);
3621 /* Note that at this point 'end' and 'old_start' are not in order, but
3622 * start is definitely the min. and old_end is definitely the max. */
3623 if (end != old_start)
3626 * One can also do
3627 * ORDER_UINT32(end, old_start);
3628 * EDIT_InvalidateText(es, start, end);
3629 * EDIT_InvalidateText(es, old_start, old_end);
3630 * in place of the following if statement.
3631 * (That would complete the optimal five-comparison four-element sort.)
3633 if (old_start > end )
3635 EDIT_InvalidateText(es, start, end);
3636 EDIT_InvalidateText(es, old_start, old_end);
3638 else
3640 EDIT_InvalidateText(es, start, old_start);
3641 EDIT_InvalidateText(es, end, old_end);
3644 else EDIT_InvalidateText(es, start, old_end);
3648 /*********************************************************************
3650 * EM_SETTABSTOPS
3653 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3655 if (!(es->style & ES_MULTILINE))
3656 return FALSE;
3657 HeapFree(GetProcessHeap(), 0, es->tabs);
3658 es->tabs_count = count;
3659 if (!count)
3660 es->tabs = NULL;
3661 else {
3662 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3663 memcpy(es->tabs, tabs, count * sizeof(INT));
3665 return TRUE;
3669 /*********************************************************************
3671 * EM_SETTABSTOPS16
3674 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3676 if (!(es->style & ES_MULTILINE))
3677 return FALSE;
3678 HeapFree(GetProcessHeap(), 0, es->tabs);
3679 es->tabs_count = count;
3680 if (!count)
3681 es->tabs = NULL;
3682 else {
3683 INT i;
3684 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3685 for (i = 0 ; i < count ; i++)
3686 es->tabs[i] = *tabs++;
3688 return TRUE;
3692 /*********************************************************************
3694 * EM_SETWORDBREAKPROC
3697 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3699 if (es->word_break_proc == wbp)
3700 return;
3702 es->word_break_proc = wbp;
3703 es->word_break_proc16 = NULL;
3705 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3706 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3707 EDIT_UpdateText(es, NULL, TRUE);
3712 /*********************************************************************
3714 * EM_SETWORDBREAKPROC16
3717 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3719 if (es->word_break_proc16 == wbp)
3720 return;
3722 es->word_break_proc = NULL;
3723 es->word_break_proc16 = wbp;
3724 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3725 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
3726 EDIT_UpdateText(es, NULL, TRUE);
3731 /*********************************************************************
3733 * EM_UNDO / WM_UNDO
3736 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3738 INT ulength;
3739 LPWSTR utext;
3741 /* As per MSDN spec, for a single-line edit control,
3742 the return value is always TRUE */
3743 if( es->style & ES_READONLY )
3744 return !(es->style & ES_MULTILINE);
3746 ulength = strlenW(es->undo_text);
3748 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3750 strcpyW(utext, es->undo_text);
3752 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3753 es->undo_insert_count, debugstr_w(utext));
3755 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3756 EDIT_EM_EmptyUndoBuffer(es);
3757 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3758 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3759 /* send the notification after the selection start and end are set */
3760 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3761 EDIT_EM_ScrollCaret(es);
3762 HeapFree(GetProcessHeap(), 0, utext);
3764 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3765 es->undo_insert_count, debugstr_w(es->undo_text));
3766 return TRUE;
3770 /*********************************************************************
3772 * WM_CHAR
3775 static void EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3777 BOOL control;
3779 control = GetKeyState(VK_CONTROL) & 0x8000;
3781 switch (c) {
3782 case '\r':
3783 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3784 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3785 break;
3786 case '\n':
3787 if (es->style & ES_MULTILINE) {
3788 if (es->style & ES_READONLY) {
3789 EDIT_MoveHome(es, FALSE);
3790 EDIT_MoveDown_ML(es, FALSE);
3791 } else {
3792 static const WCHAR cr_lfW[] = {'\r','\n',0};
3793 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3796 break;
3797 case '\t':
3798 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3800 static const WCHAR tabW[] = {'\t',0};
3801 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3803 break;
3804 case VK_BACK:
3805 if (!(es->style & ES_READONLY) && !control) {
3806 if (es->selection_start != es->selection_end)
3807 EDIT_WM_Clear(es);
3808 else {
3809 /* delete character left of caret */
3810 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3811 EDIT_MoveBackward(es, TRUE);
3812 EDIT_WM_Clear(es);
3815 break;
3816 case 0x03: /* ^C */
3817 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3818 break;
3819 case 0x16: /* ^V */
3820 if (!(es->style & ES_READONLY))
3821 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3822 break;
3823 case 0x18: /* ^X */
3824 if (!(es->style & ES_READONLY))
3825 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3826 break;
3828 default:
3829 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3830 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3831 break;
3833 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3834 WCHAR str[2];
3835 str[0] = c;
3836 str[1] = '\0';
3837 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3839 break;
3844 /*********************************************************************
3846 * WM_COMMAND
3849 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3851 if (code || control)
3852 return;
3854 switch (id) {
3855 case EM_UNDO:
3856 EDIT_EM_Undo(es);
3857 break;
3858 case WM_CUT:
3859 EDIT_WM_Cut(es);
3860 break;
3861 case WM_COPY:
3862 EDIT_WM_Copy(es);
3863 break;
3864 case WM_PASTE:
3865 EDIT_WM_Paste(es);
3866 break;
3867 case WM_CLEAR:
3868 EDIT_WM_Clear(es);
3869 break;
3870 case EM_SETSEL:
3871 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3872 EDIT_EM_ScrollCaret(es);
3873 break;
3874 default:
3875 ERR("unknown menu item, please report\n");
3876 break;
3881 /*********************************************************************
3883 * WM_CONTEXTMENU
3885 * Note: the resource files resource/sysres_??.rc cannot define a
3886 * single popup menu. Hence we use a (dummy) menubar
3887 * containing the single popup menu as its first item.
3889 * FIXME: the message identifiers have been chosen arbitrarily,
3890 * hence we use MF_BYPOSITION.
3891 * We might as well use the "real" values (anybody knows ?)
3892 * The menu definition is in resources/sysres_??.rc.
3893 * Once these are OK, we better use MF_BYCOMMAND here
3894 * (as we do in EDIT_WM_Command()).
3897 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3899 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3900 HMENU popup = GetSubMenu(menu, 0);
3901 UINT start = es->selection_start;
3902 UINT end = es->selection_end;
3904 ORDER_UINT(start, end);
3906 /* undo */
3907 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3908 /* cut */
3909 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3910 /* copy */
3911 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3912 /* paste */
3913 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3914 /* delete */
3915 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3916 /* select all */
3917 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3919 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3920 DestroyMenu(menu);
3924 /*********************************************************************
3926 * WM_COPY
3929 static void EDIT_WM_Copy(EDITSTATE *es)
3931 INT s = min(es->selection_start, es->selection_end);
3932 INT e = max(es->selection_start, es->selection_end);
3933 HGLOBAL hdst;
3934 LPWSTR dst;
3936 if (e == s) return;
3938 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3939 dst = GlobalLock(hdst);
3940 strncpyW(dst, es->text + s, e - s);
3941 dst[e - s] = 0; /* ensure 0 termination */
3942 TRACE("%s\n", debugstr_w(dst));
3943 GlobalUnlock(hdst);
3944 OpenClipboard(es->hwndSelf);
3945 EmptyClipboard();
3946 SetClipboardData(CF_UNICODETEXT, hdst);
3947 CloseClipboard();
3951 /*********************************************************************
3953 * WM_CREATE
3956 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
3958 TRACE("%s\n", debugstr_w(name));
3960 * To initialize some final structure members, we call some helper
3961 * functions. However, since the EDITSTATE is not consistent (i.e.
3962 * not fully initialized), we should be very careful which
3963 * functions can be called, and in what order.
3965 EDIT_WM_SetFont(es, 0, FALSE);
3966 EDIT_EM_EmptyUndoBuffer(es);
3968 if (name && *name) {
3969 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, TRUE);
3970 /* if we insert text to the editline, the text scrolls out
3971 * of the window, as the caret is placed after the insert
3972 * pos normally; thus we reset es->selection... to 0 and
3973 * update caret
3975 es->selection_start = es->selection_end = 0;
3976 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3977 * Messages are only to be sent when the USER does something to
3978 * change the contents. So I am removing this EN_CHANGE
3980 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3982 EDIT_EM_ScrollCaret(es);
3984 /* force scroll info update */
3985 EDIT_UpdateScrollInfo(es);
3986 /* The rule seems to return 1 here for success */
3987 /* Power Builder masked edit controls will crash */
3988 /* if not. */
3989 /* FIXME: is that in all cases so ? */
3990 return 1;
3994 /*********************************************************************
3996 * WM_DESTROY
3999 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4001 LINEDEF *pc, *pp;
4003 if (es->hloc32W) {
4004 while (LocalUnlock(es->hloc32W)) ;
4005 LocalFree(es->hloc32W);
4007 if (es->hloc32A) {
4008 while (LocalUnlock(es->hloc32A)) ;
4009 LocalFree(es->hloc32A);
4011 if (es->hloc16) {
4012 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4013 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
4014 LOCAL_Free(hInstance, es->hloc16);
4017 pc = es->first_line_def;
4018 while (pc)
4020 pp = pc->next;
4021 HeapFree(GetProcessHeap(), 0, pc);
4022 pc = pp;
4025 SetWindowLongW( es->hwndSelf, 0, 0 );
4026 HeapFree(GetProcessHeap(), 0, es);
4028 return 0;
4032 /*********************************************************************
4034 * WM_ERASEBKGND
4037 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4039 /* we do the proper erase in EDIT_WM_Paint */
4040 return -1;
4044 /*********************************************************************
4046 * WM_GETTEXT
4049 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4051 if(!count) return 0;
4053 if(unicode)
4055 lstrcpynW(dst, es->text, count);
4056 return strlenW(dst);
4058 else
4060 LPSTR textA = (LPSTR)dst;
4061 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4062 textA[count - 1] = 0; /* ensure 0 termination */
4063 return strlen(textA);
4067 /*********************************************************************
4069 * WM_HSCROLL
4072 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4074 INT dx;
4075 INT fw;
4077 if (!(es->style & ES_MULTILINE))
4078 return 0;
4080 if (!(es->style & ES_AUTOHSCROLL))
4081 return 0;
4083 dx = 0;
4084 fw = es->format_rect.right - es->format_rect.left;
4085 switch (action) {
4086 case SB_LINELEFT:
4087 TRACE("SB_LINELEFT\n");
4088 if (es->x_offset)
4089 dx = -es->char_width;
4090 break;
4091 case SB_LINERIGHT:
4092 TRACE("SB_LINERIGHT\n");
4093 if (es->x_offset < es->text_width)
4094 dx = es->char_width;
4095 break;
4096 case SB_PAGELEFT:
4097 TRACE("SB_PAGELEFT\n");
4098 if (es->x_offset)
4099 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4100 break;
4101 case SB_PAGERIGHT:
4102 TRACE("SB_PAGERIGHT\n");
4103 if (es->x_offset < es->text_width)
4104 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4105 break;
4106 case SB_LEFT:
4107 TRACE("SB_LEFT\n");
4108 if (es->x_offset)
4109 dx = -es->x_offset;
4110 break;
4111 case SB_RIGHT:
4112 TRACE("SB_RIGHT\n");
4113 if (es->x_offset < es->text_width)
4114 dx = es->text_width - es->x_offset;
4115 break;
4116 case SB_THUMBTRACK:
4117 TRACE("SB_THUMBTRACK %d\n", pos);
4118 es->flags |= EF_HSCROLL_TRACK;
4119 if(es->style & WS_HSCROLL)
4120 dx = pos - es->x_offset;
4121 else
4123 INT fw, new_x;
4124 /* Sanity check */
4125 if(pos < 0 || pos > 100) return 0;
4126 /* Assume default scroll range 0-100 */
4127 fw = es->format_rect.right - es->format_rect.left;
4128 new_x = pos * (es->text_width - fw) / 100;
4129 dx = es->text_width ? (new_x - es->x_offset) : 0;
4131 break;
4132 case SB_THUMBPOSITION:
4133 TRACE("SB_THUMBPOSITION %d\n", pos);
4134 es->flags &= ~EF_HSCROLL_TRACK;
4135 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4136 dx = pos - es->x_offset;
4137 else
4139 INT fw, new_x;
4140 /* Sanity check */
4141 if(pos < 0 || pos > 100) return 0;
4142 /* Assume default scroll range 0-100 */
4143 fw = es->format_rect.right - es->format_rect.left;
4144 new_x = pos * (es->text_width - fw) / 100;
4145 dx = es->text_width ? (new_x - es->x_offset) : 0;
4147 if (!dx) {
4148 /* force scroll info update */
4149 EDIT_UpdateScrollInfo(es);
4150 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
4152 break;
4153 case SB_ENDSCROLL:
4154 TRACE("SB_ENDSCROLL\n");
4155 break;
4157 * FIXME : the next two are undocumented !
4158 * Are we doing the right thing ?
4159 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4160 * although it's also a regular control message.
4162 case EM_GETTHUMB: /* this one is used by NT notepad */
4163 case EM_GETTHUMB16:
4165 LRESULT ret;
4166 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4167 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4168 else
4170 /* Assume default scroll range 0-100 */
4171 INT fw = es->format_rect.right - es->format_rect.left;
4172 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4174 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4175 return ret;
4177 case EM_LINESCROLL16:
4178 TRACE("EM_LINESCROLL16\n");
4179 dx = pos;
4180 break;
4182 default:
4183 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4184 action, action);
4185 return 0;
4187 if (dx)
4189 INT fw = es->format_rect.right - es->format_rect.left;
4190 /* check if we are going to move too far */
4191 if(es->x_offset + dx + fw > es->text_width)
4192 dx = es->text_width - fw - es->x_offset;
4193 if(dx)
4194 EDIT_EM_LineScroll_internal(es, dx, 0);
4196 return 0;
4200 /*********************************************************************
4202 * EDIT_CheckCombo
4205 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4207 HWND hLBox = es->hwndListBox;
4208 HWND hCombo;
4209 BOOL bDropped;
4210 int nEUI;
4212 if (!hLBox)
4213 return FALSE;
4215 hCombo = GetParent(es->hwndSelf);
4216 bDropped = TRUE;
4217 nEUI = 0;
4219 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4221 if (key == VK_UP || key == VK_DOWN)
4223 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4224 nEUI = 1;
4226 if (msg == WM_KEYDOWN || nEUI)
4227 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4230 switch (msg)
4232 case WM_KEYDOWN:
4233 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4235 /* make sure ComboLBox pops up */
4236 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4237 key = VK_F4;
4238 nEUI = 2;
4241 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4242 break;
4244 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4245 if (nEUI)
4246 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4247 else
4248 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4249 break;
4252 if(nEUI == 2)
4253 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4255 return TRUE;
4259 /*********************************************************************
4261 * WM_KEYDOWN
4263 * Handling of special keys that don't produce a WM_CHAR
4264 * (i.e. non-printable keys) & Backspace & Delete
4267 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4269 BOOL shift;
4270 BOOL control;
4272 if (GetKeyState(VK_MENU) & 0x8000)
4273 return 0;
4275 shift = GetKeyState(VK_SHIFT) & 0x8000;
4276 control = GetKeyState(VK_CONTROL) & 0x8000;
4278 switch (key) {
4279 case VK_F4:
4280 case VK_UP:
4281 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4282 break;
4284 /* fall through */
4285 case VK_LEFT:
4286 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4287 EDIT_MoveUp_ML(es, shift);
4288 else
4289 if (control)
4290 EDIT_MoveWordBackward(es, shift);
4291 else
4292 EDIT_MoveBackward(es, shift);
4293 break;
4294 case VK_DOWN:
4295 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4296 break;
4297 /* fall through */
4298 case VK_RIGHT:
4299 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4300 EDIT_MoveDown_ML(es, shift);
4301 else if (control)
4302 EDIT_MoveWordForward(es, shift);
4303 else
4304 EDIT_MoveForward(es, shift);
4305 break;
4306 case VK_HOME:
4307 EDIT_MoveHome(es, shift);
4308 break;
4309 case VK_END:
4310 EDIT_MoveEnd(es, shift);
4311 break;
4312 case VK_PRIOR:
4313 if (es->style & ES_MULTILINE)
4314 EDIT_MovePageUp_ML(es, shift);
4315 else
4316 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4317 break;
4318 case VK_NEXT:
4319 if (es->style & ES_MULTILINE)
4320 EDIT_MovePageDown_ML(es, shift);
4321 else
4322 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4323 break;
4324 case VK_DELETE:
4325 if (!(es->style & ES_READONLY) && !(shift && control)) {
4326 if (es->selection_start != es->selection_end) {
4327 if (shift)
4328 EDIT_WM_Cut(es);
4329 else
4330 EDIT_WM_Clear(es);
4331 } else {
4332 if (shift) {
4333 /* delete character left of caret */
4334 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4335 EDIT_MoveBackward(es, TRUE);
4336 EDIT_WM_Clear(es);
4337 } else if (control) {
4338 /* delete to end of line */
4339 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4340 EDIT_MoveEnd(es, TRUE);
4341 EDIT_WM_Clear(es);
4342 } else {
4343 /* delete character right of caret */
4344 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4345 EDIT_MoveForward(es, TRUE);
4346 EDIT_WM_Clear(es);
4350 break;
4351 case VK_INSERT:
4352 if (shift) {
4353 if (!(es->style & ES_READONLY))
4354 EDIT_WM_Paste(es);
4355 } else if (control)
4356 EDIT_WM_Copy(es);
4357 break;
4358 case VK_RETURN:
4359 /* If the edit doesn't want the return send a message to the default object */
4360 if(!(es->style & ES_WANTRETURN))
4362 HWND hwndParent = GetParent(es->hwndSelf);
4363 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4364 if (HIWORD(dw) == DC_HASDEFID)
4366 SendMessageW( hwndParent, WM_COMMAND,
4367 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4368 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4371 break;
4373 return 0;
4377 /*********************************************************************
4379 * WM_KILLFOCUS
4382 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4384 es->flags &= ~EF_FOCUSED;
4385 DestroyCaret();
4386 if(!(es->style & ES_NOHIDESEL))
4387 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4388 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4389 return 0;
4393 /*********************************************************************
4395 * WM_LBUTTONDBLCLK
4397 * The caret position has been set on the WM_LBUTTONDOWN message
4400 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4402 INT s;
4403 INT e = es->selection_end;
4404 INT l;
4405 INT li;
4406 INT ll;
4408 if (!(es->flags & EF_FOCUSED))
4409 return 0;
4411 l = EDIT_EM_LineFromChar(es, e);
4412 li = EDIT_EM_LineIndex(es, l);
4413 ll = EDIT_EM_LineLength(es, e);
4414 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4415 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4416 EDIT_EM_SetSel(es, s, e, FALSE);
4417 EDIT_EM_ScrollCaret(es);
4418 return 0;
4422 /*********************************************************************
4424 * WM_LBUTTONDOWN
4427 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4429 INT e;
4430 BOOL after_wrap;
4432 SetFocus(es->hwndSelf);
4433 if (!(es->flags & EF_FOCUSED))
4434 return 0;
4436 es->bCaptureState = TRUE;
4437 SetCapture(es->hwndSelf);
4438 EDIT_ConfinePoint(es, &x, &y);
4439 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4440 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4441 EDIT_EM_ScrollCaret(es);
4442 es->region_posx = es->region_posy = 0;
4443 SetTimer(es->hwndSelf, 0, 100, NULL);
4444 return 0;
4448 /*********************************************************************
4450 * WM_LBUTTONUP
4453 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4455 if (es->bCaptureState) {
4456 KillTimer(es->hwndSelf, 0);
4457 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4459 es->bCaptureState = FALSE;
4460 return 0;
4464 /*********************************************************************
4466 * WM_MBUTTONDOWN
4469 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4471 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4472 return 0;
4476 /*********************************************************************
4478 * WM_MOUSEMOVE
4481 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4483 INT e;
4484 BOOL after_wrap;
4485 INT prex, prey;
4487 if (GetCapture() != es->hwndSelf)
4488 return 0;
4491 * FIXME: gotta do some scrolling if outside client
4492 * area. Maybe reset the timer ?
4494 prex = x; prey = y;
4495 EDIT_ConfinePoint(es, &x, &y);
4496 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4497 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4498 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4499 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4500 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4501 return 0;
4505 /*********************************************************************
4507 * WM_NCCREATE
4509 * See also EDIT_WM_StyleChanged
4511 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4513 EDITSTATE *es;
4514 UINT alloc_size;
4516 TRACE("Creating %s edit control, style = %08lx\n",
4517 unicode ? "Unicode" : "ANSI", lpcs->style);
4519 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4520 return FALSE;
4521 SetWindowLongW( hwnd, 0, (LONG)es );
4524 * Note: since the EDITSTATE has not been fully initialized yet,
4525 * we can't use any API calls that may send
4526 * WM_XXX messages before WM_NCCREATE is completed.
4529 es->is_unicode = unicode;
4530 es->style = lpcs->style;
4532 es->bEnableState = !(es->style & WS_DISABLED);
4534 es->hwndSelf = hwnd;
4535 /* Save parent, which will be notified by EN_* messages */
4536 es->hwndParent = lpcs->hwndParent;
4538 if (es->style & ES_COMBO)
4539 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4541 /* Number overrides lowercase overrides uppercase (at least it
4542 * does in Win95). However I'll bet that ES_NUMBER would be
4543 * invalid under Win 3.1.
4545 if (es->style & ES_NUMBER) {
4546 ; /* do not override the ES_NUMBER */
4547 } else if (es->style & ES_LOWERCASE) {
4548 es->style &= ~ES_UPPERCASE;
4550 if (es->style & ES_MULTILINE) {
4551 es->buffer_limit = BUFLIMIT_MULTI;
4552 if (es->style & WS_VSCROLL)
4553 es->style |= ES_AUTOVSCROLL;
4554 if (es->style & WS_HSCROLL)
4555 es->style |= ES_AUTOHSCROLL;
4556 es->style &= ~ES_PASSWORD;
4557 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4558 /* Confirmed - RIGHT overrides CENTER */
4559 if (es->style & ES_RIGHT)
4560 es->style &= ~ES_CENTER;
4561 es->style &= ~WS_HSCROLL;
4562 es->style &= ~ES_AUTOHSCROLL;
4564 } else {
4565 es->buffer_limit = BUFLIMIT_SINGLE;
4566 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4567 es->style &= ~ES_CENTER;
4568 es->style &= ~WS_HSCROLL;
4569 es->style &= ~WS_VSCROLL;
4570 if (es->style & ES_PASSWORD)
4571 es->password_char = '*';
4574 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4575 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4576 return FALSE;
4577 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4579 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4580 return FALSE;
4581 es->undo_buffer_size = es->buffer_size;
4583 if (es->style & ES_MULTILINE)
4584 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4585 return FALSE;
4586 es->line_count = 1;
4589 * In Win95 look and feel, the WS_BORDER style is replaced by the
4590 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4591 * control a nonclient area so we don't need to draw the border.
4592 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4593 * a nonclient area and we should handle painting the border ourselves.
4595 * When making modifications please ensure that the code still works
4596 * for edit controls created directly with style 0x50800000, exStyle 0
4597 * (which should have a single pixel border)
4599 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4600 es->style &= ~WS_BORDER;
4601 else if (es->style & WS_BORDER)
4602 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4604 return TRUE;
4607 /*********************************************************************
4609 * WM_PAINT
4612 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
4614 PAINTSTRUCT ps;
4615 INT i;
4616 HDC dc;
4617 HFONT old_font = 0;
4618 RECT rc;
4619 RECT rcClient;
4620 RECT rcLine;
4621 RECT rcRgn;
4622 HBRUSH brush;
4623 BOOL rev = es->bEnableState &&
4624 ((es->flags & EF_FOCUSED) ||
4625 (es->style & ES_NOHIDESEL));
4626 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
4628 GetClientRect(es->hwndSelf, &rcClient);
4630 /* paint the background */
4631 if (!(brush = EDIT_NotifyCtlColor(es, dc)))
4632 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
4633 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
4634 GetClipBox(dc, &rc);
4635 FillRect(dc, &rc, brush);
4637 /* draw the border */
4638 if(es->style & WS_BORDER) {
4639 rc = rcClient;
4640 if(es->style & ES_MULTILINE) {
4641 if(es->style & WS_HSCROLL) rc.bottom++;
4642 if(es->style & WS_VSCROLL) rc.right++;
4644 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4646 IntersectClipRect(dc, es->format_rect.left,
4647 es->format_rect.top,
4648 es->format_rect.right,
4649 es->format_rect.bottom);
4650 if (es->style & ES_MULTILINE) {
4651 rc = rcClient;
4652 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4654 if (es->font)
4655 old_font = SelectObject(dc, es->font);
4656 EDIT_NotifyCtlColor(es, dc);
4658 if (!es->bEnableState)
4659 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4660 GetClipBox(dc, &rcRgn);
4661 if (es->style & ES_MULTILINE) {
4662 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4663 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4664 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
4665 if (IntersectRect(&rc, &rcRgn, &rcLine))
4666 EDIT_PaintLine(es, dc, i, rev);
4668 } else {
4669 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
4670 if (IntersectRect(&rc, &rcRgn, &rcLine))
4671 EDIT_PaintLine(es, dc, 0, rev);
4673 if (es->font)
4674 SelectObject(dc, old_font);
4676 if (!hdc)
4677 EndPaint(es->hwndSelf, &ps);
4681 /*********************************************************************
4683 * WM_PASTE
4686 static void EDIT_WM_Paste(EDITSTATE *es)
4688 HGLOBAL hsrc;
4689 LPWSTR src;
4691 /* Protect read-only edit control from modification */
4692 if(es->style & ES_READONLY)
4693 return;
4695 OpenClipboard(es->hwndSelf);
4696 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4697 src = (LPWSTR)GlobalLock(hsrc);
4698 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
4699 GlobalUnlock(hsrc);
4701 CloseClipboard();
4705 /*********************************************************************
4707 * WM_SETFOCUS
4710 static void EDIT_WM_SetFocus(EDITSTATE *es)
4712 es->flags |= EF_FOCUSED;
4713 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4714 EDIT_SetCaretPos(es, es->selection_end,
4715 es->flags & EF_AFTER_WRAP);
4716 if(!(es->style & ES_NOHIDESEL))
4717 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4718 ShowCaret(es->hwndSelf);
4719 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4723 /*********************************************************************
4725 * WM_SETFONT
4727 * With Win95 look the margins are set to default font value unless
4728 * the system font (font == 0) is being set, in which case they are left
4729 * unchanged.
4732 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
4734 TEXTMETRICW tm;
4735 HDC dc;
4736 HFONT old_font = 0;
4737 RECT r;
4739 es->font = font;
4740 dc = GetDC(es->hwndSelf);
4741 if (font)
4742 old_font = SelectObject(dc, font);
4743 GetTextMetricsW(dc, &tm);
4744 es->line_height = tm.tmHeight;
4745 es->char_width = tm.tmAveCharWidth;
4746 if (font)
4747 SelectObject(dc, old_font);
4748 ReleaseDC(es->hwndSelf, dc);
4749 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4750 EC_USEFONTINFO, EC_USEFONTINFO);
4752 /* Force the recalculation of the format rect for each font change */
4753 GetClientRect(es->hwndSelf, &r);
4754 EDIT_SetRectNP(es, &r);
4756 if (es->style & ES_MULTILINE)
4757 EDIT_BuildLineDefs_ML(es, 0, strlenW(es->text), 0, NULL);
4758 else
4759 EDIT_CalcLineWidth_SL(es);
4761 if (redraw)
4762 EDIT_UpdateText(es, NULL, TRUE);
4763 if (es->flags & EF_FOCUSED) {
4764 DestroyCaret();
4765 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
4766 EDIT_SetCaretPos(es, es->selection_end,
4767 es->flags & EF_AFTER_WRAP);
4768 ShowCaret(es->hwndSelf);
4773 /*********************************************************************
4775 * WM_SETTEXT
4777 * NOTES
4778 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4779 * The modified flag is reset. No notifications are sent.
4781 * For single-line controls, reception of WM_SETTEXT triggers:
4782 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4785 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
4787 if (!unicode && text)
4789 LPCSTR textA = (LPCSTR)text;
4790 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4791 LPWSTR textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
4792 if (textW)
4793 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4794 text = textW;
4797 if (es->flags & EF_UPDATE)
4798 /* fixed this bug once; complain if we see it about to happen again. */
4799 ERR("SetSel may generate UPDATE message whose handler may reset "
4800 "selection.\n");
4802 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4803 if (text)
4805 TRACE("%s\n", debugstr_w(text));
4806 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
4807 if(!unicode)
4808 HeapFree(GetProcessHeap(), 0, (LPWSTR)text);
4810 else
4812 static const WCHAR empty_stringW[] = {0};
4813 TRACE("<NULL>\n");
4814 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4816 es->x_offset = 0;
4817 es->flags &= ~EF_MODIFIED;
4818 EDIT_EM_SetSel(es, 0, 0, FALSE);
4820 /* Send the notification after the selection start and end have been set
4821 * edit control doesn't send notification on WM_SETTEXT
4822 * if it is multiline, or it is part of combobox
4824 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4826 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4827 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4829 EDIT_EM_ScrollCaret(es);
4830 EDIT_UpdateScrollInfo(es);
4834 /*********************************************************************
4836 * WM_SIZE
4839 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4841 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4842 RECT rc;
4843 TRACE("width = %d, height = %d\n", width, height);
4844 SetRect(&rc, 0, 0, width, height);
4845 EDIT_SetRectNP(es, &rc);
4846 EDIT_UpdateText(es, NULL, TRUE);
4851 /*********************************************************************
4853 * WM_STYLECHANGED
4855 * This message is sent by SetWindowLong on having changed either the Style
4856 * or the extended style.
4858 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4860 * See also EDIT_WM_NCCreate
4862 * It appears that the Windows version of the edit control allows the style
4863 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4864 * style variable which will generally be different. In this function we
4865 * update the internal style based on what changed in the externally visible
4866 * style.
4868 * Much of this content as based upon the MSDN, especially:
4869 * Platform SDK Documentation -> User Interface Services ->
4870 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4871 * Edit Control Styles
4873 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4875 if (GWL_STYLE == which) {
4876 DWORD style_change_mask;
4877 DWORD new_style;
4878 /* Only a subset of changes can be applied after the control
4879 * has been created.
4881 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4882 ES_NUMBER;
4883 if (es->style & ES_MULTILINE)
4884 style_change_mask |= ES_WANTRETURN;
4886 new_style = style->styleNew & style_change_mask;
4888 /* Number overrides lowercase overrides uppercase (at least it
4889 * does in Win95). However I'll bet that ES_NUMBER would be
4890 * invalid under Win 3.1.
4892 if (new_style & ES_NUMBER) {
4893 ; /* do not override the ES_NUMBER */
4894 } else if (new_style & ES_LOWERCASE) {
4895 new_style &= ~ES_UPPERCASE;
4898 es->style = (es->style & ~style_change_mask) | new_style;
4899 } else if (GWL_EXSTYLE == which) {
4900 ; /* FIXME - what is needed here */
4901 } else {
4902 WARN ("Invalid style change %d\n",which);
4905 return 0;
4908 /*********************************************************************
4910 * WM_SYSKEYDOWN
4913 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4915 if ((key == VK_BACK) && (key_data & 0x2000)) {
4916 if (EDIT_EM_CanUndo(es))
4917 EDIT_EM_Undo(es);
4918 return 0;
4919 } else if (key == VK_UP || key == VK_DOWN) {
4920 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4921 return 0;
4923 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4927 /*********************************************************************
4929 * WM_TIMER
4932 static void EDIT_WM_Timer(EDITSTATE *es)
4934 if (es->region_posx < 0) {
4935 EDIT_MoveBackward(es, TRUE);
4936 } else if (es->region_posx > 0) {
4937 EDIT_MoveForward(es, TRUE);
4940 * FIXME: gotta do some vertical scrolling here, like
4941 * EDIT_EM_LineScroll(hwnd, 0, 1);
4945 /*********************************************************************
4947 * WM_VSCROLL
4950 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4952 INT dy;
4954 if (!(es->style & ES_MULTILINE))
4955 return 0;
4957 if (!(es->style & ES_AUTOVSCROLL))
4958 return 0;
4960 dy = 0;
4961 switch (action) {
4962 case SB_LINEUP:
4963 case SB_LINEDOWN:
4964 case SB_PAGEUP:
4965 case SB_PAGEDOWN:
4966 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4967 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4968 (action == SB_PAGEUP ? "SB_PAGEUP" :
4969 "SB_PAGEDOWN"))));
4970 EDIT_EM_Scroll(es, action);
4971 return 0;
4972 case SB_TOP:
4973 TRACE("SB_TOP\n");
4974 dy = -es->y_offset;
4975 break;
4976 case SB_BOTTOM:
4977 TRACE("SB_BOTTOM\n");
4978 dy = es->line_count - 1 - es->y_offset;
4979 break;
4980 case SB_THUMBTRACK:
4981 TRACE("SB_THUMBTRACK %d\n", pos);
4982 es->flags |= EF_VSCROLL_TRACK;
4983 if(es->style & WS_VSCROLL)
4984 dy = pos - es->y_offset;
4985 else
4987 /* Assume default scroll range 0-100 */
4988 INT vlc, new_y;
4989 /* Sanity check */
4990 if(pos < 0 || pos > 100) return 0;
4991 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4992 new_y = pos * (es->line_count - vlc) / 100;
4993 dy = es->line_count ? (new_y - es->y_offset) : 0;
4994 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4995 es->line_count, es->y_offset, pos, dy);
4997 break;
4998 case SB_THUMBPOSITION:
4999 TRACE("SB_THUMBPOSITION %d\n", pos);
5000 es->flags &= ~EF_VSCROLL_TRACK;
5001 if(es->style & WS_VSCROLL)
5002 dy = pos - es->y_offset;
5003 else
5005 /* Assume default scroll range 0-100 */
5006 INT vlc, new_y;
5007 /* Sanity check */
5008 if(pos < 0 || pos > 100) return 0;
5009 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5010 new_y = pos * (es->line_count - vlc) / 100;
5011 dy = es->line_count ? (new_y - es->y_offset) : 0;
5012 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5013 es->line_count, es->y_offset, pos, dy);
5015 if (!dy)
5017 /* force scroll info update */
5018 EDIT_UpdateScrollInfo(es);
5019 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
5021 break;
5022 case SB_ENDSCROLL:
5023 TRACE("SB_ENDSCROLL\n");
5024 break;
5026 * FIXME : the next two are undocumented !
5027 * Are we doing the right thing ?
5028 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5029 * although it's also a regular control message.
5031 case EM_GETTHUMB: /* this one is used by NT notepad */
5032 case EM_GETTHUMB16:
5034 LRESULT ret;
5035 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5036 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5037 else
5039 /* Assume default scroll range 0-100 */
5040 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5041 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5043 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5044 return ret;
5046 case EM_LINESCROLL16:
5047 TRACE("EM_LINESCROLL16 %d\n", pos);
5048 dy = pos;
5049 break;
5051 default:
5052 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5053 action, action);
5054 return 0;
5056 if (dy)
5057 EDIT_EM_LineScroll(es, 0, dy);
5058 return 0;
5061 /*********************************************************************
5063 * EDIT_UpdateText
5066 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5068 if (es->flags & EF_UPDATE) {
5069 es->flags &= ~EF_UPDATE;
5070 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5072 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5076 /*********************************************************************
5078 * EDIT_UpdateText
5081 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5083 if (es->flags & EF_UPDATE) {
5084 es->flags &= ~EF_UPDATE;
5085 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
5087 InvalidateRect(es->hwndSelf, rc, bErase);