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
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.
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
49 WINE_DEFAULT_DEBUG_CHANNEL(edit
);
50 WINE_DECLARE_DEBUG_CHANNEL(combo
);
51 WINE_DECLARE_DEBUG_CHANNEL(relay
);
53 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
54 FIXME: BTW, new specs say 65535 (do you dare ???) */
55 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
56 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
57 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
58 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
61 * extra flags for EDITSTATE.flags field
63 #define EF_MODIFIED 0x0001 /* text has been modified */
64 #define EF_FOCUSED 0x0002 /* we have input focus */
65 #define EF_UPDATE 0x0004 /* notify parent of changed state */
66 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
67 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
68 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
69 wrapped line, instead of in front of the next character */
70 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
74 END_0
= 0, /* line ends with terminating '\0' character */
75 END_WRAP
, /* line is wrapped */
76 END_HARD
, /* line ends with a hard return '\r\n' */
77 END_SOFT
, /* line ends with a soft return '\r\r\n' */
78 END_RICH
/* line ends with a single '\n' */
81 typedef struct tagLINEDEF
{
82 INT length
; /* bruto length of a line in bytes */
83 INT net_length
; /* netto length of a line in visible characters */
85 INT width
; /* width of the line in pixels */
86 INT index
; /* line index into the buffer */
87 struct tagLINEDEF
*next
;
92 BOOL is_unicode
; /* how the control was created */
93 LPWSTR text
; /* the actual contents of the control */
94 UINT buffer_size
; /* the size of the buffer in characters */
95 UINT buffer_limit
; /* the maximum size to which the buffer may grow in characters */
96 HFONT font
; /* NULL means standard system font */
97 INT x_offset
; /* scroll offset for multi lines this is in pixels
98 for single lines it's in characters */
99 INT line_height
; /* height of a screen line in pixels */
100 INT char_width
; /* average character width in pixels */
101 DWORD style
; /* sane version of wnd->dwStyle */
102 WORD flags
; /* flags that are not in es->style or wnd->flags (EF_XXX) */
103 INT undo_insert_count
; /* number of characters inserted in sequence */
104 UINT undo_position
; /* character index of the insertion and deletion */
105 LPWSTR undo_text
; /* deleted text */
106 UINT undo_buffer_size
; /* size of the deleted text buffer */
107 INT selection_start
; /* == selection_end if no selection */
108 INT selection_end
; /* == current caret position */
109 WCHAR password_char
; /* == 0 if no password char, and for multi line controls */
110 INT left_margin
; /* in pixels */
111 INT right_margin
; /* in pixels */
113 INT text_width
; /* width of the widest line in pixels for multi line controls
114 and just line width for single line controls */
115 INT region_posx
; /* Position of cursor relative to region: */
116 INT region_posy
; /* -1: to left, 0: within, 1: to right */
118 EDITWORDBREAKPROC16 word_break_proc16
;
120 void *word_break_proc
; /* 32-bit word break proc: ANSI or Unicode */
121 INT line_count
; /* number of lines */
122 INT y_offset
; /* scroll offset in number of lines */
123 BOOL bCaptureState
; /* flag indicating whether mouse was captured */
124 BOOL bEnableState
; /* flag keeping the enable state */
125 HWND hwndSelf
; /* the our window handle */
126 HWND hwndParent
; /* Handle of parent for sending EN_* messages.
127 Even if parent will change, EN_* messages
128 should be sent to the first parent. */
129 HWND hwndListBox
; /* handle of ComboBox's listbox or NULL */
131 * only for multi line controls
133 INT lock_count
; /* amount of re-entries in the EditWndProc */
136 LINEDEF
*first_line_def
; /* linked list of (soft) linebreaks */
137 HLOCAL hloc32W
; /* our unicode local memory block */
139 HLOCAL16 hloc16
; /* alias for 16-bit control receiving EM_GETHANDLE16
142 HLOCAL hloc32A
; /* alias for ANSI control receiving EM_GETHANDLE
147 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
148 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
150 /* used for disabled or read-only edit control */
151 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
153 { /* Notify parent which has created this edit control */ \
154 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
155 SendMessageW(es->hwndParent, WM_COMMAND, \
156 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
157 (LPARAM)(es->hwndSelf)); \
160 /*********************************************************************
167 * These functions have trivial implementations
168 * We still like to call them internally
169 * "static inline" makes them more like macro's
171 static __inline BOOL
EDIT_EM_CanUndo(EDITSTATE
*es
);
172 static __inline
void EDIT_EM_EmptyUndoBuffer(EDITSTATE
*es
);
173 static __inline
void EDIT_WM_Clear(EDITSTATE
*es
);
174 static __inline
void EDIT_WM_Cut(EDITSTATE
*es
);
177 * Helper functions only valid for one type of control
179 static void EDIT_BuildLineDefs_ML(EDITSTATE
*es
, INT iStart
, INT iEnd
, INT delta
, HRGN hrgn
);
180 static void EDIT_CalcLineWidth_SL(EDITSTATE
*es
);
181 static LPWSTR
EDIT_GetPasswordPointer_SL(EDITSTATE
*es
);
182 static void EDIT_MoveDown_ML(EDITSTATE
*es
, BOOL extend
);
183 static void EDIT_MovePageDown_ML(EDITSTATE
*es
, BOOL extend
);
184 static void EDIT_MovePageUp_ML(EDITSTATE
*es
, BOOL extend
);
185 static void EDIT_MoveUp_ML(EDITSTATE
*es
, BOOL extend
);
187 * Helper functions valid for both single line _and_ multi line controls
189 static INT
EDIT_CallWordBreakProc(EDITSTATE
*es
, INT start
, INT index
, INT count
, INT action
);
190 static INT
EDIT_CharFromPos(EDITSTATE
*es
, INT x
, INT y
, LPBOOL after_wrap
);
191 static void EDIT_ConfinePoint(EDITSTATE
*es
, LPINT x
, LPINT y
);
192 static void EDIT_GetLineRect(EDITSTATE
*es
, INT line
, INT scol
, INT ecol
, LPRECT rc
);
193 static void EDIT_InvalidateText(EDITSTATE
*es
, INT start
, INT end
);
194 static void EDIT_LockBuffer(EDITSTATE
*es
);
195 static BOOL
EDIT_MakeFit(EDITSTATE
*es
, UINT size
);
196 static BOOL
EDIT_MakeUndoFit(EDITSTATE
*es
, UINT size
);
197 static void EDIT_MoveBackward(EDITSTATE
*es
, BOOL extend
);
198 static void EDIT_MoveEnd(EDITSTATE
*es
, BOOL extend
);
199 static void EDIT_MoveForward(EDITSTATE
*es
, BOOL extend
);
200 static void EDIT_MoveHome(EDITSTATE
*es
, BOOL extend
);
201 static void EDIT_MoveWordBackward(EDITSTATE
*es
, BOOL extend
);
202 static void EDIT_MoveWordForward(EDITSTATE
*es
, BOOL extend
);
203 static void EDIT_PaintLine(EDITSTATE
*es
, HDC hdc
, INT line
, BOOL rev
);
204 static INT
EDIT_PaintText(EDITSTATE
*es
, HDC hdc
, INT x
, INT y
, INT line
, INT col
, INT count
, BOOL rev
);
205 static void EDIT_SetCaretPos(EDITSTATE
*es
, INT pos
, BOOL after_wrap
);
206 static void EDIT_SetRectNP(EDITSTATE
*es
, LPRECT lprc
);
207 static void EDIT_UnlockBuffer(EDITSTATE
*es
, BOOL force
);
208 static void EDIT_UpdateScrollInfo(EDITSTATE
*es
);
209 static INT CALLBACK
EDIT_WordBreakProc(LPWSTR s
, INT index
, INT count
, INT action
);
211 * EM_XXX message handlers
213 static LRESULT
EDIT_EM_CharFromPos(EDITSTATE
*es
, INT x
, INT y
);
214 static BOOL
EDIT_EM_FmtLines(EDITSTATE
*es
, BOOL add_eol
);
215 static HLOCAL
EDIT_EM_GetHandle(EDITSTATE
*es
);
217 static HLOCAL16
EDIT_EM_GetHandle16(EDITSTATE
*es
);
219 static INT
EDIT_EM_GetLine(EDITSTATE
*es
, INT line
, LPWSTR dst
, BOOL unicode
);
220 static LRESULT
EDIT_EM_GetSel(EDITSTATE
*es
, PUINT start
, PUINT end
);
221 static LRESULT
EDIT_EM_GetThumb(EDITSTATE
*es
);
222 static INT
EDIT_EM_LineFromChar(EDITSTATE
*es
, INT index
);
223 static INT
EDIT_EM_LineIndex(EDITSTATE
*es
, INT line
);
224 static INT
EDIT_EM_LineLength(EDITSTATE
*es
, INT index
);
225 static BOOL
EDIT_EM_LineScroll(EDITSTATE
*es
, INT dx
, INT dy
);
226 static BOOL
EDIT_EM_LineScroll_internal(EDITSTATE
*es
, INT dx
, INT dy
);
227 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
);
228 static void EDIT_EM_ReplaceSel(EDITSTATE
*es
, BOOL can_undo
, LPCWSTR lpsz_replace
, BOOL send_update
, BOOL honor_limit
);
229 static LRESULT
EDIT_EM_Scroll(EDITSTATE
*es
, INT action
);
230 static void EDIT_EM_ScrollCaret(EDITSTATE
*es
);
231 static void EDIT_EM_SetHandle(EDITSTATE
*es
, HLOCAL hloc
);
233 static void EDIT_EM_SetHandle16(EDITSTATE
*es
, HLOCAL16 hloc
);
235 static void EDIT_EM_SetLimitText(EDITSTATE
*es
, INT limit
);
236 static void EDIT_EM_SetMargins(EDITSTATE
*es
, INT action
, INT left
, INT right
, BOOL repaint
);
237 static void EDIT_EM_SetPasswordChar(EDITSTATE
*es
, WCHAR c
);
238 static void EDIT_EM_SetSel(EDITSTATE
*es
, UINT start
, UINT end
, BOOL after_wrap
);
239 static BOOL
EDIT_EM_SetTabStops(EDITSTATE
*es
, INT count
, LPINT tabs
);
241 static BOOL
EDIT_EM_SetTabStops16(EDITSTATE
*es
, INT count
, LPINT16 tabs
);
243 static void EDIT_EM_SetWordBreakProc(EDITSTATE
*es
, void *wbp
);
245 static void EDIT_EM_SetWordBreakProc16(EDITSTATE
*es
, EDITWORDBREAKPROC16 wbp
);
247 static BOOL
EDIT_EM_Undo(EDITSTATE
*es
);
249 * WM_XXX message handlers
251 static void EDIT_WM_Char(EDITSTATE
*es
, WCHAR c
);
252 static void EDIT_WM_Command(EDITSTATE
*es
, INT code
, INT id
, HWND conrtol
);
253 static void EDIT_WM_ContextMenu(EDITSTATE
*es
, INT x
, INT y
);
254 static void EDIT_WM_Copy(EDITSTATE
*es
);
255 static LRESULT
EDIT_WM_Create(EDITSTATE
*es
, LPCWSTR name
);
256 static LRESULT
EDIT_WM_Destroy(EDITSTATE
*es
);
257 static LRESULT
EDIT_WM_EraseBkGnd(EDITSTATE
*es
, HDC dc
);
258 static INT
EDIT_WM_GetText(EDITSTATE
*es
, INT count
, LPWSTR dst
, BOOL unicode
);
259 static LRESULT
EDIT_WM_HScroll(EDITSTATE
*es
, INT action
, INT pos
);
260 static LRESULT
EDIT_WM_KeyDown(EDITSTATE
*es
, INT key
);
261 static LRESULT
EDIT_WM_KillFocus(EDITSTATE
*es
);
262 static LRESULT
EDIT_WM_LButtonDblClk(EDITSTATE
*es
);
263 static LRESULT
EDIT_WM_LButtonDown(EDITSTATE
*es
, DWORD keys
, INT x
, INT y
);
264 static LRESULT
EDIT_WM_LButtonUp(EDITSTATE
*es
);
265 static LRESULT
EDIT_WM_MButtonDown(EDITSTATE
*es
);
266 static LRESULT
EDIT_WM_MouseMove(EDITSTATE
*es
, INT x
, INT y
);
267 static LRESULT
EDIT_WM_NCCreate(HWND hwnd
, LPCREATESTRUCTW lpcs
, BOOL unicode
);
268 static void EDIT_WM_Paint(EDITSTATE
*es
, HDC hdc
);
269 static void EDIT_WM_Paste(EDITSTATE
*es
);
270 static void EDIT_WM_SetFocus(EDITSTATE
*es
);
271 static void EDIT_WM_SetFont(EDITSTATE
*es
, HFONT font
, BOOL redraw
);
272 static void EDIT_WM_SetText(EDITSTATE
*es
, LPCWSTR text
, BOOL unicode
);
273 static void EDIT_WM_Size(EDITSTATE
*es
, UINT action
, INT width
, INT height
);
274 static LRESULT
EDIT_WM_StyleChanged(EDITSTATE
*es
, WPARAM which
, const STYLESTRUCT
*style
);
275 static LRESULT
EDIT_WM_SysKeyDown(EDITSTATE
*es
, INT key
, DWORD key_data
);
276 static void EDIT_WM_Timer(EDITSTATE
*es
);
277 static LRESULT
EDIT_WM_VScroll(EDITSTATE
*es
, INT action
, INT pos
);
278 static void EDIT_UpdateText(EDITSTATE
*es
, LPRECT rc
, BOOL bErase
);
279 static void EDIT_UpdateTextRegion(EDITSTATE
*es
, HRGN hrgn
, BOOL bErase
);
281 LRESULT WINAPI
EditWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
282 LRESULT WINAPI
EditWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
284 /*********************************************************************
285 * edit class descriptor
287 const struct builtin_class_descr EDIT_builtin_class
=
291 CS_DBLCLKS
| CS_PARENTDC
, /* style */
292 (WNDPROC
)EditWndProcW
, /* procW */
293 (WNDPROC
)EditWndProcA
, /* procA */
294 sizeof(EDITSTATE
*), /* extra */
295 (LPWSTR
)IDC_IBEAM
, /* cursor */
299 CS_DBLCLKS
| CS_PARENTDC
, /* style */
300 EditWndProcA
, /* procA */
301 EditWndProcW
, /* procW */
302 sizeof(EDITSTATE
*), /* extra */
303 IDC_IBEAM
, /* cursor */
309 /*********************************************************************
314 static __inline BOOL
EDIT_EM_CanUndo(EDITSTATE
*es
)
316 return (es
->undo_insert_count
|| strlenW(es
->undo_text
));
320 /*********************************************************************
325 static __inline
void EDIT_EM_EmptyUndoBuffer(EDITSTATE
*es
)
327 es
->undo_insert_count
= 0;
328 *es
->undo_text
= '\0';
332 /*********************************************************************
337 static __inline
void EDIT_WM_Clear(EDITSTATE
*es
)
339 static const WCHAR empty_stringW
[] = {0};
341 /* Protect read-only edit control from modification */
342 if(es
->style
& ES_READONLY
)
345 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
349 /*********************************************************************
354 static __inline
void EDIT_WM_Cut(EDITSTATE
*es
)
361 /**********************************************************************
364 * Returns the window version in case Wine emulates a later version
365 * of windows than the application expects.
367 * In a number of cases when windows runs an application that was
368 * designed for an earlier windows version, windows reverts
369 * to "old" behaviour of that earlier version.
371 * An example is a disabled edit control that needs to be painted.
372 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
373 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
374 * applications with an expected version 0f 4.0 or higher.
377 static DWORD
get_app_version(void)
379 static DWORD version
;
382 DWORD dwEmulatedVersion
;
384 DWORD dwProcVersion
= GetProcessVersion(0);
386 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOW
);
387 GetVersionExW( &info
);
388 dwEmulatedVersion
= MAKELONG( info
.dwMinorVersion
, info
.dwMajorVersion
);
389 /* FIXME: this may not be 100% correct; see discussion on the
390 * wine developer list in Nov 1999 */
391 version
= dwProcVersion
< dwEmulatedVersion
? dwProcVersion
: dwEmulatedVersion
;
397 static HBRUSH
EDIT_NotifyCtlColor(EDITSTATE
*es
, HDC hdc
)
402 if ( get_app_version() >= 0x40000 && (!es
->bEnableState
|| (es
->style
& ES_READONLY
)))
403 msg
= WM_CTLCOLORSTATIC
;
405 msg
= WM_CTLCOLOREDIT
;
407 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
408 hbrush
= (HBRUSH
)SendMessageW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
410 hbrush
= (HBRUSH
)DefWindowProcW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
414 static __inline LRESULT
DefWindowProcT(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
417 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
419 return DefWindowProcA(hwnd
, msg
, wParam
, lParam
);
422 /*********************************************************************
426 * The messages are in the order of the actual integer values
427 * (which can be found in include/windows.h)
428 * Wherever possible the 16 bit versions are converted to
429 * the 32 bit ones, so that we can 'fall through' to the
430 * helper functions. These are mostly 32 bit (with a few
431 * exceptions, clearly indicated by a '16' extension to their
435 static LRESULT WINAPI
EditWndProc_common( HWND hwnd
, UINT msg
,
436 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
438 EDITSTATE
*es
= (EDITSTATE
*)GetWindowLongPtrW( hwnd
, 0 );
441 //TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
443 if (!es
&& msg
!= WM_NCCREATE
)
444 return DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
446 if (es
&& (msg
!= WM_DESTROY
)) EDIT_LockBuffer(es
);
456 result
= EDIT_EM_GetSel(es
, (PUINT
)wParam
, (PUINT
)lParam
);
461 if ((short)LOWORD(lParam
) == -1)
462 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
464 EDIT_EM_SetSel(es
, LOWORD(lParam
), HIWORD(lParam
), FALSE
);
466 EDIT_EM_ScrollCaret(es
);
471 EDIT_EM_SetSel(es
, wParam
, lParam
, FALSE
);
472 EDIT_EM_ScrollCaret(es
);
479 CONV_RECT32TO16(&es
->format_rect
, MapSL(lParam
));
484 CopyRect((LPRECT
)lParam
, &es
->format_rect
);
489 if ((es
->style
& ES_MULTILINE
) && lParam
) {
491 CONV_RECT16TO32(MapSL(lParam
), &rc
);
492 EDIT_SetRectNP(es
, &rc
);
493 EDIT_UpdateText(es
, NULL
, TRUE
);
498 if ((es
->style
& ES_MULTILINE
) && lParam
) {
499 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
500 EDIT_UpdateText(es
, NULL
, TRUE
);
506 if ((es
->style
& ES_MULTILINE
) && lParam
) {
508 CONV_RECT16TO32(MapSL(lParam
), &rc
);
509 EDIT_SetRectNP(es
, &rc
);
514 if ((es
->style
& ES_MULTILINE
) && lParam
)
515 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
522 result
= EDIT_EM_Scroll(es
, (INT
)wParam
);
526 case EM_LINESCROLL16
:
527 wParam
= (WPARAM
)(INT
)(SHORT
)HIWORD(lParam
);
528 lParam
= (LPARAM
)(INT
)(SHORT
)LOWORD(lParam
);
532 result
= (LRESULT
)EDIT_EM_LineScroll(es
, (INT
)wParam
, (INT
)lParam
);
536 case EM_SCROLLCARET16
:
539 EDIT_EM_ScrollCaret(es
);
547 result
= ((es
->flags
& EF_MODIFIED
) != 0);
555 es
->flags
|= EF_MODIFIED
;
557 es
->flags
&= ~(EF_MODIFIED
| EF_UPDATE
); /* reset pending updates */
561 case EM_GETLINECOUNT16
:
563 case EM_GETLINECOUNT
:
564 result
= (es
->style
& ES_MULTILINE
) ? es
->line_count
: 1;
569 if ((INT16
)wParam
== -1)
574 result
= (LRESULT
)EDIT_EM_LineIndex(es
, (INT
)wParam
);
579 EDIT_EM_SetHandle16(es
, (HLOCAL16
)wParam
);
583 EDIT_EM_SetHandle(es
, (HLOCAL
)wParam
);
588 result
= (LRESULT
)EDIT_EM_GetHandle16(es
);
592 result
= (LRESULT
)EDIT_EM_GetHandle(es
);
599 result
= EDIT_EM_GetThumb(es
);
602 /* these messages missing from specs */
611 FIXME("undocumented message 0x%x, please report\n", msg
);
612 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
616 case EM_LINELENGTH16
:
619 result
= (LRESULT
)EDIT_EM_LineLength(es
, (INT
)wParam
);
623 case EM_REPLACESEL16
:
624 lParam
= (LPARAM
)MapSL(lParam
);
625 unicode
= FALSE
; /* 16-bit message is always ascii */
633 textW
= (LPWSTR
)lParam
;
636 LPSTR textA
= (LPSTR
)lParam
;
637 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
638 if((textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
))))
639 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
642 EDIT_EM_ReplaceSel(es
, (BOOL
)wParam
, textW
, TRUE
, TRUE
);
646 HeapFree(GetProcessHeap(), 0, textW
);
652 lParam
= (LPARAM
)MapSL(lParam
);
653 unicode
= FALSE
; /* 16-bit message is always ascii */
657 result
= (LRESULT
)EDIT_EM_GetLine(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
663 case EM_SETLIMITTEXT
:
664 EDIT_EM_SetLimitText(es
, (INT
)wParam
);
671 result
= (LRESULT
)EDIT_EM_CanUndo(es
);
679 result
= (LRESULT
)EDIT_EM_Undo(es
);
686 result
= (LRESULT
)EDIT_EM_FmtLines(es
, (BOOL
)wParam
);
690 case EM_LINEFROMCHAR16
:
692 case EM_LINEFROMCHAR
:
693 result
= (LRESULT
)EDIT_EM_LineFromChar(es
, (INT
)wParam
);
697 case EM_SETTABSTOPS16
:
698 result
= (LRESULT
)EDIT_EM_SetTabStops16(es
, (INT
)wParam
, MapSL(lParam
));
702 result
= (LRESULT
)EDIT_EM_SetTabStops(es
, (INT
)wParam
, (LPINT
)lParam
);
706 case EM_SETPASSWORDCHAR16
:
707 unicode
= FALSE
; /* 16-bit message is always ascii */
710 case EM_SETPASSWORDCHAR
:
715 charW
= (WCHAR
)wParam
;
719 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
722 EDIT_EM_SetPasswordChar(es
, charW
);
727 case EM_EMPTYUNDOBUFFER16
:
729 case EM_EMPTYUNDOBUFFER
:
730 EDIT_EM_EmptyUndoBuffer(es
);
734 case EM_GETFIRSTVISIBLELINE16
:
735 result
= es
->y_offset
;
738 case EM_GETFIRSTVISIBLELINE
:
739 result
= (es
->style
& ES_MULTILINE
) ? es
->y_offset
: es
->x_offset
;
743 case EM_SETREADONLY16
:
747 SetWindowLongW( hwnd
, GWL_STYLE
,
748 GetWindowLongW( hwnd
, GWL_STYLE
) | ES_READONLY
);
749 es
->style
|= ES_READONLY
;
751 SetWindowLongW( hwnd
, GWL_STYLE
,
752 GetWindowLongW( hwnd
, GWL_STYLE
) & ~ES_READONLY
);
753 es
->style
&= ~ES_READONLY
;
759 case EM_SETWORDBREAKPROC16
:
760 EDIT_EM_SetWordBreakProc16(es
, (EDITWORDBREAKPROC16
)lParam
);
763 case EM_SETWORDBREAKPROC
:
764 EDIT_EM_SetWordBreakProc(es
, (void *)lParam
);
768 case EM_GETWORDBREAKPROC16
:
769 result
= (LRESULT
)es
->word_break_proc16
;
772 case EM_GETWORDBREAKPROC
:
773 result
= (LRESULT
)es
->word_break_proc
;
777 case EM_GETPASSWORDCHAR16
:
778 unicode
= FALSE
; /* 16-bit message is always ascii */
781 case EM_GETPASSWORDCHAR
:
784 result
= es
->password_char
;
787 WCHAR charW
= es
->password_char
;
789 WideCharToMultiByte(CP_ACP
, 0, &charW
, 1, &charA
, 1, NULL
, NULL
);
795 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
798 EDIT_EM_SetMargins(es
, (INT
)wParam
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), TRUE
);
802 result
= MAKELONG(es
->left_margin
, es
->right_margin
);
805 case EM_GETLIMITTEXT
:
806 result
= es
->buffer_limit
;
810 result
= strlenW(es
->text
);
811 if ((INT
)wParam
>= result
) result
= -1;
812 else result
= EDIT_EM_PosFromChar(es
, (INT
)wParam
, FALSE
);
816 result
= EDIT_EM_CharFromPos(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
819 /* End of the EM_ messages which were in numerical order; what order
820 * are these in? vaguely alphabetical?
824 result
= EDIT_WM_NCCreate(hwnd
, (LPCREATESTRUCTW
)lParam
, unicode
);
828 result
= EDIT_WM_Destroy(es
);
833 result
= DLGC_HASSETSEL
| DLGC_WANTCHARS
| DLGC_WANTARROWS
;
835 if (es
->style
& ES_MULTILINE
)
837 result
|= DLGC_WANTALLKEYS
;
841 if (lParam
&& (((LPMSG
)lParam
)->message
== WM_KEYDOWN
))
843 int vk
= (int)((LPMSG
)lParam
)->wParam
;
845 if (es
->hwndListBox
&& (vk
== VK_RETURN
|| vk
== VK_ESCAPE
))
847 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
848 result
|= DLGC_WANTMESSAGE
;
859 strng
[0] = wParam
>> 8;
860 strng
[1] = wParam
& 0xff;
861 if (strng
[0]) MultiByteToWideChar(CP_ACP
, 0, strng
, 2, &charW
, 1);
862 else MultiByteToWideChar(CP_ACP
, 0, &strng
[1], 1, &charW
, 1);
863 EDIT_WM_Char(es
, charW
);
876 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
879 if ((charW
== VK_RETURN
|| charW
== VK_ESCAPE
) && es
->hwndListBox
)
881 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
882 SendMessageW(GetParent(hwnd
), WM_KEYDOWN
, charW
, 0);
885 EDIT_WM_Char(es
, charW
);
894 EDIT_WM_Command(es
, HIWORD(wParam
), LOWORD(wParam
), (HWND
)lParam
);
898 EDIT_WM_ContextMenu(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
907 result
= EDIT_WM_Create(es
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
910 LPCSTR nameA
= ((LPCREATESTRUCTA
)lParam
)->lpszName
;
914 INT countW
= MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, NULL
, 0);
915 if((nameW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
))))
916 MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, nameW
, countW
);
918 result
= EDIT_WM_Create(es
, nameW
);
919 HeapFree(GetProcessHeap(), 0, nameW
);
928 es
->bEnableState
= (BOOL
) wParam
;
929 EDIT_UpdateText(es
, NULL
, TRUE
);
933 result
= EDIT_WM_EraseBkGnd(es
, (HDC
)wParam
);
937 result
= (LRESULT
)es
->font
;
941 result
= (LRESULT
)EDIT_WM_GetText(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
944 case WM_GETTEXTLENGTH
:
945 if (unicode
) result
= strlenW(es
->text
);
946 else result
= WideCharToMultiByte( CP_ACP
, 0, es
->text
, strlenW(es
->text
),
947 NULL
, 0, NULL
, NULL
);
951 result
= EDIT_WM_HScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
955 result
= EDIT_WM_KeyDown(es
, (INT
)wParam
);
959 result
= EDIT_WM_KillFocus(es
);
962 case WM_LBUTTONDBLCLK
:
963 result
= EDIT_WM_LButtonDblClk(es
);
967 result
= EDIT_WM_LButtonDown(es
, wParam
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
971 result
= EDIT_WM_LButtonUp(es
);
975 result
= EDIT_WM_MButtonDown(es
);
979 result
= EDIT_WM_MouseMove(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
984 EDIT_WM_Paint(es
, (HDC
)wParam
);
992 EDIT_WM_SetFocus(es
);
996 EDIT_WM_SetFont(es
, (HFONT
)wParam
, LOWORD(lParam
) != 0);
1000 /* FIXME: actually set an internal flag and behave accordingly */
1004 EDIT_WM_SetText(es
, (LPCWSTR
)lParam
, unicode
);
1009 EDIT_WM_Size(es
, (UINT
)wParam
, LOWORD(lParam
), HIWORD(lParam
));
1012 case WM_STYLECHANGED
:
1013 result
= EDIT_WM_StyleChanged(es
, wParam
, (const STYLESTRUCT
*)lParam
);
1016 case WM_STYLECHANGING
:
1017 result
= 0; /* See EDIT_WM_StyleChanged */
1021 result
= EDIT_WM_SysKeyDown(es
, (INT
)wParam
, (DWORD
)lParam
);
1029 result
= EDIT_WM_VScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
1034 int gcWheelDelta
= 0;
1035 UINT pulScrollLines
= 3;
1036 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
1038 if (wParam
& (MK_SHIFT
| MK_CONTROL
)) {
1039 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
1042 gcWheelDelta
-= GET_WHEEL_DELTA_WPARAM(wParam
);
1043 if (abs(gcWheelDelta
) >= WHEEL_DELTA
&& pulScrollLines
)
1045 int cLineScroll
= (int) min((UINT
) es
->line_count
, pulScrollLines
);
1046 cLineScroll
*= (gcWheelDelta
/ WHEEL_DELTA
);
1047 result
= EDIT_EM_LineScroll(es
, 0, cLineScroll
);
1052 result
= DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
1056 if (es
) EDIT_UnlockBuffer(es
, FALSE
);
1061 /*********************************************************************
1063 * EditWndProcW (USER32.@)
1065 LRESULT WINAPI
EditWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1067 return EditWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);
1070 /*********************************************************************
1072 * EditWndProc (USER32.@)
1074 LRESULT WINAPI
EditWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1076 return EditWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
1079 /*********************************************************************
1081 * EDIT_BuildLineDefs_ML
1083 * Build linked list of text lines.
1084 * Lines can end with '\0' (last line), a character (if it is wrapped),
1085 * a soft return '\r\r\n' or a hard return '\r\n'
1088 static void EDIT_BuildLineDefs_ML(EDITSTATE
*es
, INT istart
, INT iend
, INT delta
, HRGN hrgn
)
1092 LPWSTR current_position
, cp
;
1094 LINEDEF
*current_line
;
1095 LINEDEF
*previous_line
;
1096 LINEDEF
*start_line
;
1097 INT line_index
= 0, nstart_line
= 0, nstart_index
= 0;
1098 INT line_count
= es
->line_count
;
1099 INT orig_net_length
;
1102 if (istart
== iend
&& delta
== 0)
1105 dc
= GetDC(es
->hwndSelf
);
1107 old_font
= SelectObject(dc
, es
->font
);
1109 previous_line
= NULL
;
1110 current_line
= es
->first_line_def
;
1112 /* Find starting line. istart must lie inside an existing line or
1113 * at the end of buffer */
1115 if (istart
< current_line
->index
+ current_line
->length
||
1116 current_line
->ending
== END_0
)
1119 previous_line
= current_line
;
1120 current_line
= current_line
->next
;
1122 } while (current_line
);
1124 if (!current_line
) /* Error occurred start is not inside previous buffer */
1126 FIXME(" modification occurred outside buffer\n");
1127 ReleaseDC(es
->hwndSelf
, dc
);
1131 /* Remember start of modifications in order to calculate update region */
1132 nstart_line
= line_index
;
1133 nstart_index
= current_line
->index
;
1135 /* We must start to reformat from the previous line since the modifications
1136 * may have caused the line to wrap upwards. */
1137 if (!(es
->style
& ES_AUTOHSCROLL
) && line_index
> 0)
1140 current_line
= previous_line
;
1142 start_line
= current_line
;
1144 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
1145 current_position
= es
->text
+ current_line
->index
;
1147 if (current_line
!= start_line
)
1149 if (!current_line
|| current_line
->index
+ delta
> current_position
- es
->text
)
1151 /* The buffer has been expanded, create a new line and
1152 insert it into the link list */
1153 LINEDEF
*new_line
= HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF
));
1154 new_line
->next
= previous_line
->next
;
1155 previous_line
->next
= new_line
;
1156 current_line
= new_line
;
1159 else if (current_line
->index
+ delta
< current_position
- es
->text
)
1161 /* The previous line merged with this line so we delete this extra entry */
1162 previous_line
->next
= current_line
->next
;
1163 HeapFree(GetProcessHeap(), 0, current_line
);
1164 current_line
= previous_line
->next
;
1168 else /* current_line->index + delta == current_position */
1170 if (current_position
- es
->text
> iend
)
1171 break; /* We reached end of line modifications */
1172 /* else recalulate this line */
1176 current_line
->index
= current_position
- es
->text
;
1177 orig_net_length
= current_line
->net_length
;
1179 /* Find end of line */
1180 cp
= current_position
;
1182 if (*cp
== '\n') break;
1183 if ((*cp
== '\r') && (*(cp
+ 1) == '\n'))
1188 /* Mark type of line termination */
1190 current_line
->ending
= END_0
;
1191 current_line
->net_length
= strlenW(current_position
);
1192 } else if ((cp
> current_position
) && (*(cp
- 1) == '\r')) {
1193 current_line
->ending
= END_SOFT
;
1194 current_line
->net_length
= cp
- current_position
- 1;
1195 } else if (*cp
== '\n') {
1196 current_line
->ending
= END_RICH
;
1197 current_line
->net_length
= cp
- current_position
;
1199 current_line
->ending
= END_HARD
;
1200 current_line
->net_length
= cp
- current_position
;
1203 /* Calculate line width */
1204 current_line
->width
= (INT
)LOWORD(GetTabbedTextExtentW(dc
,
1205 current_position
, current_line
->net_length
,
1206 es
->tabs_count
, es
->tabs
));
1208 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1209 if (!(es
->style
& ES_AUTOHSCROLL
)) {
1210 if (current_line
->width
> fw
) {
1215 next
= EDIT_CallWordBreakProc(es
, current_position
- es
->text
,
1216 prev
+ 1, current_line
->net_length
, WB_RIGHT
);
1217 current_line
->width
= (INT
)LOWORD(GetTabbedTextExtentW(dc
,
1218 current_position
, next
, es
->tabs_count
, es
->tabs
));
1219 } while (current_line
->width
<= fw
);
1220 if (!prev
) { /* Didn't find a line break so force a break */
1225 current_line
->width
= (INT
)LOWORD(GetTabbedTextExtentW(dc
,
1226 current_position
, next
, es
->tabs_count
, es
->tabs
));
1227 } while (current_line
->width
<= fw
);
1232 /* If the first line we are calculating, wrapped before istart, we must
1233 * adjust istart in order for this to be reflected in the update region. */
1234 if (current_line
->index
== nstart_index
&& istart
> current_line
->index
+ prev
)
1235 istart
= current_line
->index
+ prev
;
1236 /* else if we are updating the previous line before the first line we
1237 * are re-calculating and it expanded */
1238 else if (current_line
== start_line
&&
1239 current_line
->index
!= nstart_index
&& orig_net_length
< prev
)
1241 /* Line expanded due to an upwards line wrap so we must partially include
1242 * previous line in update region */
1243 nstart_line
= line_index
;
1244 nstart_index
= current_line
->index
;
1245 istart
= current_line
->index
+ orig_net_length
;
1248 current_line
->net_length
= prev
;
1249 current_line
->ending
= END_WRAP
;
1250 current_line
->width
= (INT
)LOWORD(GetTabbedTextExtentW(dc
, current_position
,
1251 current_line
->net_length
, es
->tabs_count
, es
->tabs
));
1253 else if (orig_net_length
< current_line
->net_length
&&
1254 current_line
== start_line
&&
1255 current_line
->index
!= nstart_index
) {
1256 /* The previous line expanded but it's still not as wide as the client rect */
1257 /* The expansion is due to an upwards line wrap so we must partially include
1258 it in the update region */
1259 nstart_line
= line_index
;
1260 nstart_index
= current_line
->index
;
1261 istart
= current_line
->index
+ orig_net_length
;
1266 /* Adjust length to include line termination */
1267 switch (current_line
->ending
) {
1269 current_line
->length
= current_line
->net_length
+ 3;
1272 current_line
->length
= current_line
->net_length
+ 1;
1275 current_line
->length
= current_line
->net_length
+ 2;
1279 current_line
->length
= current_line
->net_length
;
1282 es
->text_width
= max(es
->text_width
, current_line
->width
);
1283 current_position
+= current_line
->length
;
1284 previous_line
= current_line
;
1285 current_line
= current_line
->next
;
1287 } while (previous_line
->ending
!= END_0
);
1289 /* Finish adjusting line indexes by delta or remove hanging lines */
1290 if (previous_line
->ending
== END_0
)
1292 LINEDEF
*pnext
= NULL
;
1294 previous_line
->next
= NULL
;
1295 while (current_line
)
1297 pnext
= current_line
->next
;
1298 HeapFree(GetProcessHeap(), 0, current_line
);
1299 current_line
= pnext
;
1305 while (current_line
)
1307 current_line
->index
+= delta
;
1308 current_line
= current_line
->next
;
1312 /* Calculate rest of modification rectangle */
1317 * We calculate two rectangles. One for the first line which may have
1318 * an indent with respect to the format rect. The other is a format-width
1319 * rectangle that spans the rest of the lines that changed or moved.
1321 rc
.top
= es
->format_rect
.top
+ nstart_line
* es
->line_height
-
1322 (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
1323 rc
.bottom
= rc
.top
+ es
->line_height
;
1324 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
))
1325 rc
.left
= es
->format_rect
.left
;
1327 rc
.left
= es
->format_rect
.left
+ (INT
)LOWORD(GetTabbedTextExtentW(dc
,
1328 es
->text
+ nstart_index
, istart
- nstart_index
,
1329 es
->tabs_count
, es
->tabs
)) - es
->x_offset
; /* Adjust for horz scroll */
1330 rc
.right
= es
->format_rect
.right
;
1331 SetRectRgn(hrgn
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
1334 rc
.left
= es
->format_rect
.left
;
1335 rc
.right
= es
->format_rect
.right
;
1337 * If lines were added or removed we must re-paint the remainder of the
1338 * lines since the remaining lines were either shifted up or down.
1340 if (line_count
< es
->line_count
) /* We added lines */
1341 rc
.bottom
= es
->line_count
* es
->line_height
;
1342 else if (line_count
> es
->line_count
) /* We removed lines */
1343 rc
.bottom
= line_count
* es
->line_height
;
1345 rc
.bottom
= line_index
* es
->line_height
;
1346 rc
.bottom
+= es
->format_rect
.top
;
1347 rc
.bottom
-= (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
1348 tmphrgn
= CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
1349 CombineRgn(hrgn
, hrgn
, tmphrgn
, RGN_OR
);
1350 DeleteObject(tmphrgn
);
1354 SelectObject(dc
, old_font
);
1356 ReleaseDC(es
->hwndSelf
, dc
);
1359 /*********************************************************************
1361 * EDIT_CalcLineWidth_SL
1364 static void EDIT_CalcLineWidth_SL(EDITSTATE
*es
)
1371 text
= EDIT_GetPasswordPointer_SL(es
);
1373 dc
= GetDC(es
->hwndSelf
);
1375 old_font
= SelectObject(dc
, es
->font
);
1377 GetTextExtentPoint32W(dc
, text
, strlenW(text
), &size
);
1380 SelectObject(dc
, old_font
);
1381 ReleaseDC(es
->hwndSelf
, dc
);
1383 if (es
->style
& ES_PASSWORD
)
1384 HeapFree(GetProcessHeap(), 0, text
);
1386 es
->text_width
= size
.cx
;
1389 /*********************************************************************
1391 * EDIT_CallWordBreakProc
1393 * Call appropriate WordBreakProc (internal or external).
1395 * Note: The "start" argument should always be an index referring
1396 * to es->text. The actual wordbreak proc might be
1397 * 16 bit, so we can't always pass any 32 bit LPSTR.
1398 * Hence we assume that es->text is the buffer that holds
1399 * the string under examination (we can decide this for ourselves).
1402 static INT
EDIT_CallWordBreakProc(EDITSTATE
*es
, INT start
, INT index
, INT count
, INT action
)
1407 INT ret
, iWndsLocks
;
1410 /* To avoid any deadlocks, all the locks on the window structures
1411 must be suspended before the control is passed to the application */
1413 iWndsLocks
= WIN_SuspendWndsLock();
1415 if (es
->word_break_proc16
) {
1422 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, NULL
, 0, NULL
, NULL
);
1423 hglob16
= GlobalAlloc16(GMEM_MOVEABLE
| GMEM_ZEROINIT
, countA
);
1424 segptr
= K32WOWGlobalLock16(hglob16
);
1425 WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, MapSL(segptr
), countA
, NULL
, NULL
);
1426 args
[4] = SELECTOROF(segptr
);
1427 args
[3] = OFFSETOF(segptr
);
1431 WOWCallback16Ex((DWORD
)es
->word_break_proc16
, WCB16_PASCAL
, sizeof(args
), args
, &result
);
1432 ret
= LOWORD(result
);
1433 GlobalUnlock16(hglob16
);
1434 GlobalFree16(hglob16
);
1436 else if (es
->word_break_proc
)
1438 if (es
->word_break_proc
)
1443 EDITWORDBREAKPROCW wbpW
= (EDITWORDBREAKPROCW
)es
->word_break_proc
;
1445 TRACE_(relay
)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1446 es
->word_break_proc
, debugstr_wn(es
->text
+ start
, count
), index
, count
, action
);
1447 ret
= wbpW(es
->text
+ start
, index
, count
, action
);
1451 EDITWORDBREAKPROCA wbpA
= (EDITWORDBREAKPROCA
)es
->word_break_proc
;
1455 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, NULL
, 0, NULL
, NULL
);
1456 textA
= HeapAlloc(GetProcessHeap(), 0, countA
);
1457 WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, textA
, countA
, NULL
, NULL
);
1458 TRACE_(relay
)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1459 es
->word_break_proc
, debugstr_an(textA
, countA
), index
, countA
, action
);
1460 ret
= wbpA(textA
, index
, countA
, action
);
1461 HeapFree(GetProcessHeap(), 0, textA
);
1465 ret
= EDIT_WordBreakProc(es
->text
+ start
, index
, count
, action
);
1468 WIN_RestoreWndsLock(iWndsLocks
);
1474 /*********************************************************************
1478 * Beware: This is not the function called on EM_CHARFROMPOS
1479 * The position _can_ be outside the formatting / client
1481 * The return value is only the character index
1484 static INT
EDIT_CharFromPos(EDITSTATE
*es
, INT x
, INT y
, LPBOOL after_wrap
)
1489 INT x_high
= 0, x_low
= 0;
1491 if (es
->style
& ES_MULTILINE
) {
1492 INT line
= (y
- es
->format_rect
.top
) / es
->line_height
+ es
->y_offset
;
1494 LINEDEF
*line_def
= es
->first_line_def
;
1496 while ((line
> 0) && line_def
->next
) {
1497 line_index
+= line_def
->length
;
1498 line_def
= line_def
->next
;
1501 x
+= es
->x_offset
- es
->format_rect
.left
;
1502 if (es
->style
& ES_RIGHT
)
1503 x
-= (es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
;
1504 else if (es
->style
& ES_CENTER
)
1505 x
-= ((es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
) / 2;
1506 if (x
>= line_def
->width
) {
1508 *after_wrap
= (line_def
->ending
== END_WRAP
);
1509 return line_index
+ line_def
->net_length
;
1513 *after_wrap
= FALSE
;
1516 dc
= GetDC(es
->hwndSelf
);
1518 old_font
= SelectObject(dc
, es
->font
);
1520 high
= line_index
+ line_def
->net_length
+ 1;
1521 while (low
< high
- 1)
1523 INT mid
= (low
+ high
) / 2;
1524 INT x_now
= LOWORD(GetTabbedTextExtentW(dc
, es
->text
+ line_index
, mid
- line_index
, es
->tabs_count
, es
->tabs
));
1533 if (abs(x_high
- x
) + 1 <= abs(x_low
- x
))
1539 *after_wrap
= ((index
== line_index
+ line_def
->net_length
) &&
1540 (line_def
->ending
== END_WRAP
));
1545 *after_wrap
= FALSE
;
1546 x
-= es
->format_rect
.left
;
1548 return es
->x_offset
;
1552 INT indent
= (es
->format_rect
.right
- es
->format_rect
.left
) - es
->text_width
;
1553 if (es
->style
& ES_RIGHT
)
1555 else if (es
->style
& ES_CENTER
)
1559 text
= EDIT_GetPasswordPointer_SL(es
);
1560 dc
= GetDC(es
->hwndSelf
);
1562 old_font
= SelectObject(dc
, es
->font
);
1566 INT high
= es
->x_offset
;
1567 while (low
< high
- 1)
1569 INT mid
= (low
+ high
) / 2;
1570 GetTextExtentPoint32W( dc
, text
+ mid
,
1571 es
->x_offset
- mid
, &size
);
1580 if (abs(x_high
+ x
) <= abs(x_low
+ x
) + 1)
1587 INT low
= es
->x_offset
;
1588 INT high
= strlenW(es
->text
) + 1;
1589 while (low
< high
- 1)
1591 INT mid
= (low
+ high
) / 2;
1592 GetTextExtentPoint32W( dc
, text
+ es
->x_offset
,
1593 mid
- es
->x_offset
, &size
);
1602 if (abs(x_high
- x
) <= abs(x_low
- x
) + 1)
1607 if (es
->style
& ES_PASSWORD
)
1608 HeapFree(GetProcessHeap(), 0, text
);
1611 SelectObject(dc
, old_font
);
1612 ReleaseDC(es
->hwndSelf
, dc
);
1617 /*********************************************************************
1621 * adjusts the point to be within the formatting rectangle
1622 * (so CharFromPos returns the nearest _visible_ character)
1625 static void EDIT_ConfinePoint(EDITSTATE
*es
, LPINT x
, LPINT y
)
1627 *x
= min(max(*x
, es
->format_rect
.left
), es
->format_rect
.right
- 1);
1628 *y
= min(max(*y
, es
->format_rect
.top
), es
->format_rect
.bottom
- 1);
1632 /*********************************************************************
1636 * Calculates the bounding rectangle for a line from a starting
1637 * column to an ending column.
1640 static void EDIT_GetLineRect(EDITSTATE
*es
, INT line
, INT scol
, INT ecol
, LPRECT rc
)
1642 INT line_index
= EDIT_EM_LineIndex(es
, line
);
1644 if (es
->style
& ES_MULTILINE
)
1645 rc
->top
= es
->format_rect
.top
+ (line
- es
->y_offset
) * es
->line_height
;
1647 rc
->top
= es
->format_rect
.top
;
1648 rc
->bottom
= rc
->top
+ es
->line_height
;
1649 rc
->left
= (scol
== 0) ? es
->format_rect
.left
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ scol
, TRUE
));
1650 rc
->right
= (ecol
== -1) ? es
->format_rect
.right
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ ecol
, TRUE
));
1654 /*********************************************************************
1656 * EDIT_GetPasswordPointer_SL
1658 * note: caller should free the (optionally) allocated buffer
1661 static LPWSTR
EDIT_GetPasswordPointer_SL(EDITSTATE
*es
)
1663 if (es
->style
& ES_PASSWORD
) {
1664 INT len
= strlenW(es
->text
);
1665 LPWSTR text
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1667 while(len
) text
[--len
] = es
->password_char
;
1674 /*********************************************************************
1678 * This acts as a LOCAL_Lock(), but it locks only once. This way
1679 * you can call it whenever you like, without unlocking.
1681 * Initially the edit control allocates a HLOCAL32 buffer
1682 * (32 bit linear memory handler). However, 16 bit application
1683 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1684 * handler). From that moment on we have to keep using this 16 bit memory
1685 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1686 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1690 static void EDIT_LockBuffer(EDITSTATE
*es
)
1693 HINSTANCE16 hInstance
= GetWindowLongW( es
->hwndSelf
, GWL_HINSTANCE
);
1699 BOOL _16bit
= FALSE
;
1706 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1707 textA
= LocalLock(es
->hloc32A
);
1708 countA
= strlen(textA
) + 1;
1713 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1714 textA
= LOCAL_Lock(hInstance
, es
->hloc16
);
1715 countA
= strlen(textA
) + 1;
1721 ERR("no buffer ... please report\n");
1728 UINT countW_new
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
1729 TRACE("%d bytes translated to %d WCHARs\n", countA
, countW_new
);
1730 if(countW_new
> es
->buffer_size
+ 1)
1732 UINT alloc_size
= ROUND_TO_GROW(countW_new
* sizeof(WCHAR
));
1733 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es
->buffer_size
, countW_new
);
1734 hloc32W_new
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1737 es
->hloc32W
= hloc32W_new
;
1738 es
->buffer_size
= LocalSize(hloc32W_new
)/sizeof(WCHAR
) - 1;
1739 TRACE("Real new size %d+1 WCHARs\n", es
->buffer_size
);
1742 WARN("FAILED! Will synchronize partially\n");
1746 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1747 es
->text
= LocalLock(es
->hloc32W
);
1751 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, es
->text
, es
->buffer_size
+ 1);
1754 LOCAL_Unlock(hInstance
, es
->hloc16
);
1757 LocalUnlock(es
->hloc32A
);
1764 /*********************************************************************
1766 * EDIT_SL_InvalidateText
1768 * Called from EDIT_InvalidateText().
1769 * Does the job for single-line controls only.
1772 static void EDIT_SL_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1777 EDIT_GetLineRect(es
, 0, start
, end
, &line_rect
);
1778 if (IntersectRect(&rc
, &line_rect
, &es
->format_rect
))
1779 EDIT_UpdateText(es
, &rc
, TRUE
);
1783 /*********************************************************************
1785 * EDIT_ML_InvalidateText
1787 * Called from EDIT_InvalidateText().
1788 * Does the job for multi-line controls only.
1791 static void EDIT_ML_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1793 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1794 INT sl
= EDIT_EM_LineFromChar(es
, start
);
1795 INT el
= EDIT_EM_LineFromChar(es
, end
);
1804 if ((el
< es
->y_offset
) || (sl
> es
->y_offset
+ vlc
))
1807 sc
= start
- EDIT_EM_LineIndex(es
, sl
);
1808 ec
= end
- EDIT_EM_LineIndex(es
, el
);
1809 if (sl
< es
->y_offset
) {
1813 if (el
> es
->y_offset
+ vlc
) {
1814 el
= es
->y_offset
+ vlc
;
1815 ec
= EDIT_EM_LineLength(es
, EDIT_EM_LineIndex(es
, el
));
1817 GetClientRect(es
->hwndSelf
, &rc1
);
1818 IntersectRect(&rcWnd
, &rc1
, &es
->format_rect
);
1820 EDIT_GetLineRect(es
, sl
, sc
, ec
, &rcLine
);
1821 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1822 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1824 EDIT_GetLineRect(es
, sl
, sc
,
1825 EDIT_EM_LineLength(es
,
1826 EDIT_EM_LineIndex(es
, sl
)),
1828 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1829 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1830 for (l
= sl
+ 1 ; l
< el
; l
++) {
1831 EDIT_GetLineRect(es
, l
, 0,
1832 EDIT_EM_LineLength(es
,
1833 EDIT_EM_LineIndex(es
, l
)),
1835 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1836 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1838 EDIT_GetLineRect(es
, el
, 0, ec
, &rcLine
);
1839 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1840 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1845 /*********************************************************************
1847 * EDIT_InvalidateText
1849 * Invalidate the text from offset start upto, but not including,
1850 * offset end. Useful for (re)painting the selection.
1851 * Regions outside the linewidth are not invalidated.
1852 * end == -1 means end == TextLength.
1853 * start and end need not be ordered.
1856 static void EDIT_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1862 end
= strlenW(es
->text
);
1870 if (es
->style
& ES_MULTILINE
)
1871 EDIT_ML_InvalidateText(es
, start
, end
);
1873 EDIT_SL_InvalidateText(es
, start
, end
);
1877 /*********************************************************************
1881 * Try to fit size + 1 characters in the buffer.
1883 static BOOL
EDIT_MakeFit(EDITSTATE
*es
, UINT size
)
1887 if (size
<= es
->buffer_size
)
1890 TRACE("trying to ReAlloc to %d+1 characters\n", size
);
1892 /* Force edit to unlock it's buffer. es->text now NULL */
1893 EDIT_UnlockBuffer(es
, TRUE
);
1896 UINT alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1897 if ((hNew32W
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
))) {
1898 TRACE("Old 32 bit handle %p, new handle %p\n", es
->hloc32W
, hNew32W
);
1899 es
->hloc32W
= hNew32W
;
1900 es
->buffer_size
= LocalSize(hNew32W
)/sizeof(WCHAR
) - 1;
1904 EDIT_LockBuffer(es
);
1906 if (es
->buffer_size
< size
) {
1907 WARN("FAILED ! We now have %d+1\n", es
->buffer_size
);
1908 EDIT_NOTIFY_PARENT(es
, EN_ERRSPACE
);
1911 TRACE("We now have %d+1\n", es
->buffer_size
);
1917 /*********************************************************************
1921 * Try to fit size + 1 bytes in the undo buffer.
1924 static BOOL
EDIT_MakeUndoFit(EDITSTATE
*es
, UINT size
)
1928 if (size
<= es
->undo_buffer_size
)
1931 TRACE("trying to ReAlloc to %d+1\n", size
);
1933 alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1934 if ((es
->undo_text
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, es
->undo_text
, alloc_size
))) {
1935 es
->undo_buffer_size
= alloc_size
/sizeof(WCHAR
) - 1;
1940 WARN("FAILED ! We now have %d+1\n", es
->undo_buffer_size
);
1946 /*********************************************************************
1951 static void EDIT_MoveBackward(EDITSTATE
*es
, BOOL extend
)
1953 INT e
= es
->selection_end
;
1957 if ((es
->style
& ES_MULTILINE
) && e
&&
1958 (es
->text
[e
- 1] == '\r') && (es
->text
[e
] == '\n')) {
1960 if (e
&& (es
->text
[e
- 1] == '\r'))
1964 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1965 EDIT_EM_ScrollCaret(es
);
1969 /*********************************************************************
1973 * Only for multi line controls
1974 * Move the caret one line down, on a column with the nearest
1975 * x coordinate on the screen (might be a different column).
1978 static void EDIT_MoveDown_ML(EDITSTATE
*es
, BOOL extend
)
1980 INT s
= es
->selection_start
;
1981 INT e
= es
->selection_end
;
1982 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
1983 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
1984 INT x
= (short)LOWORD(pos
);
1985 INT y
= (short)HIWORD(pos
);
1987 e
= EDIT_CharFromPos(es
, x
, y
+ es
->line_height
, &after_wrap
);
1990 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
1991 EDIT_EM_ScrollCaret(es
);
1995 /*********************************************************************
2000 static void EDIT_MoveEnd(EDITSTATE
*es
, BOOL extend
)
2002 BOOL after_wrap
= FALSE
;
2005 /* Pass a high value in x to make sure of receiving the end of the line */
2006 if (es
->style
& ES_MULTILINE
)
2007 e
= EDIT_CharFromPos(es
, 0x3fffffff,
2008 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), &after_wrap
);
2010 e
= strlenW(es
->text
);
2011 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, after_wrap
);
2012 EDIT_EM_ScrollCaret(es
);
2016 /*********************************************************************
2021 static void EDIT_MoveForward(EDITSTATE
*es
, BOOL extend
)
2023 INT e
= es
->selection_end
;
2027 if ((es
->style
& ES_MULTILINE
) && (es
->text
[e
- 1] == '\r')) {
2028 if (es
->text
[e
] == '\n')
2030 else if ((es
->text
[e
] == '\r') && (es
->text
[e
+ 1] == '\n'))
2034 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
2035 EDIT_EM_ScrollCaret(es
);
2039 /*********************************************************************
2043 * Home key: move to beginning of line.
2046 static void EDIT_MoveHome(EDITSTATE
*es
, BOOL extend
)
2050 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2051 if (es
->style
& ES_MULTILINE
)
2052 e
= EDIT_CharFromPos(es
, -es
->x_offset
,
2053 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), NULL
);
2056 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
2057 EDIT_EM_ScrollCaret(es
);
2061 /*********************************************************************
2063 * EDIT_MovePageDown_ML
2065 * Only for multi line controls
2066 * Move the caret one page down, on a column with the nearest
2067 * x coordinate on the screen (might be a different column).
2070 static void EDIT_MovePageDown_ML(EDITSTATE
*es
, BOOL extend
)
2072 INT s
= es
->selection_start
;
2073 INT e
= es
->selection_end
;
2074 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2075 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2076 INT x
= (short)LOWORD(pos
);
2077 INT y
= (short)HIWORD(pos
);
2079 e
= EDIT_CharFromPos(es
, x
,
2080 y
+ (es
->format_rect
.bottom
- es
->format_rect
.top
),
2084 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2085 EDIT_EM_ScrollCaret(es
);
2089 /*********************************************************************
2091 * EDIT_MovePageUp_ML
2093 * Only for multi line controls
2094 * Move the caret one page up, on a column with the nearest
2095 * x coordinate on the screen (might be a different column).
2098 static void EDIT_MovePageUp_ML(EDITSTATE
*es
, BOOL extend
)
2100 INT s
= es
->selection_start
;
2101 INT e
= es
->selection_end
;
2102 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2103 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2104 INT x
= (short)LOWORD(pos
);
2105 INT y
= (short)HIWORD(pos
);
2107 e
= EDIT_CharFromPos(es
, x
,
2108 y
- (es
->format_rect
.bottom
- es
->format_rect
.top
),
2112 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2113 EDIT_EM_ScrollCaret(es
);
2117 /*********************************************************************
2121 * Only for multi line controls
2122 * Move the caret one line up, on a column with the nearest
2123 * x coordinate on the screen (might be a different column).
2126 static void EDIT_MoveUp_ML(EDITSTATE
*es
, BOOL extend
)
2128 INT s
= es
->selection_start
;
2129 INT e
= es
->selection_end
;
2130 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2131 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2132 INT x
= (short)LOWORD(pos
);
2133 INT y
= (short)HIWORD(pos
);
2135 e
= EDIT_CharFromPos(es
, x
, y
- es
->line_height
, &after_wrap
);
2138 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2139 EDIT_EM_ScrollCaret(es
);
2143 /*********************************************************************
2145 * EDIT_MoveWordBackward
2148 static void EDIT_MoveWordBackward(EDITSTATE
*es
, BOOL extend
)
2150 INT s
= es
->selection_start
;
2151 INT e
= es
->selection_end
;
2156 l
= EDIT_EM_LineFromChar(es
, e
);
2157 ll
= EDIT_EM_LineLength(es
, e
);
2158 li
= EDIT_EM_LineIndex(es
, l
);
2161 li
= EDIT_EM_LineIndex(es
, l
- 1);
2162 e
= li
+ EDIT_EM_LineLength(es
, li
);
2165 e
= li
+ (INT
)EDIT_CallWordBreakProc(es
,
2166 li
, e
- li
, ll
, WB_LEFT
);
2170 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2171 EDIT_EM_ScrollCaret(es
);
2175 /*********************************************************************
2177 * EDIT_MoveWordForward
2180 static void EDIT_MoveWordForward(EDITSTATE
*es
, BOOL extend
)
2182 INT s
= es
->selection_start
;
2183 INT e
= es
->selection_end
;
2188 l
= EDIT_EM_LineFromChar(es
, e
);
2189 ll
= EDIT_EM_LineLength(es
, e
);
2190 li
= EDIT_EM_LineIndex(es
, l
);
2192 if ((es
->style
& ES_MULTILINE
) && (l
!= es
->line_count
- 1))
2193 e
= EDIT_EM_LineIndex(es
, l
+ 1);
2195 e
= li
+ EDIT_CallWordBreakProc(es
,
2196 li
, e
- li
+ 1, ll
, WB_RIGHT
);
2200 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2201 EDIT_EM_ScrollCaret(es
);
2205 /*********************************************************************
2210 static void EDIT_PaintLine(EDITSTATE
*es
, HDC dc
, INT line
, BOOL rev
)
2212 INT s
= es
->selection_start
;
2213 INT e
= es
->selection_end
;
2220 if (es
->style
& ES_MULTILINE
) {
2221 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
2222 if ((line
< es
->y_offset
) || (line
> es
->y_offset
+ vlc
) || (line
>= es
->line_count
))
2227 TRACE("line=%d\n", line
);
2229 pos
= EDIT_EM_PosFromChar(es
, EDIT_EM_LineIndex(es
, line
), FALSE
);
2230 x
= (short)LOWORD(pos
);
2231 y
= (short)HIWORD(pos
);
2232 li
= EDIT_EM_LineIndex(es
, line
);
2233 ll
= EDIT_EM_LineLength(es
, li
);
2234 s
= min(es
->selection_start
, es
->selection_end
);
2235 e
= max(es
->selection_start
, es
->selection_end
);
2236 s
= min(li
+ ll
, max(li
, s
));
2237 e
= min(li
+ ll
, max(li
, e
));
2238 if (rev
&& (s
!= e
) &&
2239 ((es
->flags
& EF_FOCUSED
) || (es
->style
& ES_NOHIDESEL
))) {
2240 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, s
- li
, FALSE
);
2241 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, s
- li
, e
- s
, TRUE
);
2242 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, e
- li
, li
+ ll
- e
, FALSE
);
2244 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, ll
, FALSE
);
2248 /*********************************************************************
2253 static INT
EDIT_PaintText(EDITSTATE
*es
, HDC dc
, INT x
, INT y
, INT line
, INT col
, INT count
, BOOL rev
)
2264 BkMode
= GetBkMode(dc
);
2265 BkColor
= GetBkColor(dc
);
2266 TextColor
= GetTextColor(dc
);
2268 SetBkColor(dc
, GetSysColor(COLOR_HIGHLIGHT
));
2269 SetTextColor(dc
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
2270 SetBkMode( dc
, OPAQUE
);
2272 li
= EDIT_EM_LineIndex(es
, line
);
2273 if (es
->style
& ES_MULTILINE
) {
2274 ret
= (INT
)LOWORD(TabbedTextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
,
2275 es
->tabs_count
, es
->tabs
, es
->format_rect
.left
- es
->x_offset
));
2277 LPWSTR text
= EDIT_GetPasswordPointer_SL(es
);
2278 TextOutW(dc
, x
, y
, text
+ li
+ col
, count
);
2279 GetTextExtentPoint32W(dc
, text
+ li
+ col
, count
, &size
);
2281 if (es
->style
& ES_PASSWORD
)
2282 HeapFree(GetProcessHeap(), 0, text
);
2285 SetBkColor(dc
, BkColor
);
2286 SetTextColor(dc
, TextColor
);
2287 SetBkMode( dc
, BkMode
);
2293 /*********************************************************************
2298 static void EDIT_SetCaretPos(EDITSTATE
*es
, INT pos
,
2301 LRESULT res
= EDIT_EM_PosFromChar(es
, pos
, after_wrap
);
2302 TRACE("%d - %dx%d\n", pos
, (short)LOWORD(res
), (short)HIWORD(res
));
2303 SetCaretPos((short)LOWORD(res
), (short)HIWORD(res
));
2307 /*********************************************************************
2309 * EDIT_AdjustFormatRect
2311 * Adjusts the format rectangle for the current font and the
2312 * current client rectangle.
2315 static void EDIT_AdjustFormatRect(EDITSTATE
*es
)
2319 es
->format_rect
.right
= max(es
->format_rect
.right
, es
->format_rect
.left
+ es
->char_width
);
2320 if (es
->style
& ES_MULTILINE
)
2322 INT fw
, vlc
, max_x_offset
, max_y_offset
;
2324 vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
2325 es
->format_rect
.bottom
= es
->format_rect
.top
+ max(1, vlc
) * es
->line_height
;
2327 /* correct es->x_offset */
2328 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2329 max_x_offset
= es
->text_width
- fw
;
2330 if(max_x_offset
< 0) max_x_offset
= 0;
2331 if(es
->x_offset
> max_x_offset
)
2332 es
->x_offset
= max_x_offset
;
2334 /* correct es->y_offset */
2335 max_y_offset
= es
->line_count
- vlc
;
2336 if(max_y_offset
< 0) max_y_offset
= 0;
2337 if(es
->y_offset
> max_y_offset
)
2338 es
->y_offset
= max_y_offset
;
2340 /* force scroll info update */
2341 EDIT_UpdateScrollInfo(es
);
2344 /* Windows doesn't care to fix text placement for SL controls */
2345 es
->format_rect
.bottom
= es
->format_rect
.top
+ es
->line_height
;
2347 /* Always stay within the client area */
2348 GetClientRect(es
->hwndSelf
, &ClientRect
);
2349 es
->format_rect
.bottom
= min(es
->format_rect
.bottom
, ClientRect
.bottom
);
2351 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
))
2352 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
2354 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
2358 /*********************************************************************
2362 * note: this is not (exactly) the handler called on EM_SETRECTNP
2363 * it is also used to set the rect of a single line control
2366 static void EDIT_SetRectNP(EDITSTATE
*es
, LPRECT rc
)
2370 ExStyle
= GetWindowLongPtrW(es
->hwndSelf
, GWL_EXSTYLE
);
2372 CopyRect(&es
->format_rect
, rc
);
2374 if (ExStyle
& WS_EX_CLIENTEDGE
) {
2375 es
->format_rect
.left
++;
2376 es
->format_rect
.right
--;
2378 if (es
->format_rect
.bottom
- es
->format_rect
.top
2379 >= es
->line_height
+ 2)
2381 es
->format_rect
.top
++;
2382 es
->format_rect
.bottom
--;
2385 else if (es
->style
& WS_BORDER
) {
2386 bw
= GetSystemMetrics(SM_CXBORDER
) + 1;
2387 bh
= GetSystemMetrics(SM_CYBORDER
) + 1;
2388 es
->format_rect
.left
+= bw
;
2389 es
->format_rect
.right
-= bw
;
2390 if (es
->format_rect
.bottom
- es
->format_rect
.top
2391 >= es
->line_height
+ 2 * bh
)
2393 es
->format_rect
.top
+= bh
;
2394 es
->format_rect
.bottom
-= bh
;
2398 es
->format_rect
.left
+= es
->left_margin
;
2399 es
->format_rect
.right
-= es
->right_margin
;
2400 EDIT_AdjustFormatRect(es
);
2404 /*********************************************************************
2409 static void EDIT_UnlockBuffer(EDITSTATE
*es
, BOOL force
)
2412 HINSTANCE16 hInstance
= GetWindowLongW( es
->hwndSelf
, GWL_HINSTANCE
);
2415 /* Edit window might be already destroyed */
2416 if(!IsWindow(es
->hwndSelf
))
2418 WARN("edit hwnd %p already destroyed\n", es
->hwndSelf
);
2422 if (!es
->lock_count
) {
2423 ERR("lock_count == 0 ... please report\n");
2427 ERR("es->text == 0 ... please report\n");
2431 if (force
|| (es
->lock_count
== 1)) {
2435 BOOL _16bit
= FALSE
;
2438 UINT countW
= strlenW(es
->text
) + 1;
2442 UINT countA_new
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, NULL
, 0, NULL
, NULL
);
2443 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2444 TRACE("%d WCHARs translated to %d bytes\n", countW
, countA_new
);
2445 countA
= LocalSize(es
->hloc32A
);
2446 if(countA_new
> countA
)
2449 UINT alloc_size
= ROUND_TO_GROW(countA_new
);
2450 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA
, alloc_size
);
2451 hloc32A_new
= LocalReAlloc(es
->hloc32A
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
2454 es
->hloc32A
= hloc32A_new
;
2455 countA
= LocalSize(hloc32A_new
);
2456 TRACE("Real new size %d bytes\n", countA
);
2459 WARN("FAILED! Will synchronize partially\n");
2461 textA
= LocalLock(es
->hloc32A
);
2466 UINT countA_new
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, NULL
, 0, NULL
, NULL
);
2467 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2468 TRACE("%d WCHARs translated to %d bytes\n", countW
, countA_new
);
2469 countA
= LOCAL_Size(hInstance
, es
->hloc16
);
2470 if(countA_new
> countA
)
2472 HLOCAL16 hloc16_new
;
2473 UINT alloc_size
= ROUND_TO_GROW(countA_new
);
2474 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA
, alloc_size
);
2475 hloc16_new
= LOCAL_ReAlloc(hInstance
, es
->hloc16
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
2478 es
->hloc16
= hloc16_new
;
2479 countA
= LOCAL_Size(hInstance
, hloc16_new
);
2480 TRACE("Real new size %d bytes\n", countA
);
2483 WARN("FAILED! Will synchronize partially\n");
2485 textA
= LOCAL_Lock(hInstance
, es
->hloc16
);
2492 WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, textA
, countA
, NULL
, NULL
);
2495 LOCAL_Unlock(hInstance
, es
->hloc16
);
2498 LocalUnlock(es
->hloc32A
);
2501 LocalUnlock(es
->hloc32W
);
2505 ERR("no buffer ... please report\n");
2513 /*********************************************************************
2515 * EDIT_UpdateScrollInfo
2518 static void EDIT_UpdateScrollInfo(EDITSTATE
*es
)
2520 if ((es
->style
& WS_VSCROLL
) && !(es
->flags
& EF_VSCROLL_TRACK
))
2523 si
.cbSize
= sizeof(SCROLLINFO
);
2524 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
2526 si
.nMax
= es
->line_count
- 1;
2527 si
.nPage
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
2528 si
.nPos
= es
->y_offset
;
2529 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2530 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
2531 SetScrollInfo(es
->hwndSelf
, SB_VERT
, &si
, TRUE
);
2534 if ((es
->style
& WS_HSCROLL
) && !(es
->flags
& EF_HSCROLL_TRACK
))
2537 si
.cbSize
= sizeof(SCROLLINFO
);
2538 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
2540 si
.nMax
= es
->text_width
- 1;
2541 si
.nPage
= es
->format_rect
.right
- es
->format_rect
.left
;
2542 si
.nPos
= es
->x_offset
;
2543 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2544 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
2545 SetScrollInfo(es
->hwndSelf
, SB_HORZ
, &si
, TRUE
);
2549 /*********************************************************************
2551 * EDIT_WordBreakProc
2553 * Find the beginning of words.
2554 * Note: unlike the specs for a WordBreakProc, this function only
2555 * allows to be called without linebreaks between s[0] upto
2556 * s[count - 1]. Remember it is only called
2557 * internally, so we can decide this for ourselves.
2560 static INT CALLBACK
EDIT_WordBreakProc(LPWSTR s
, INT index
, INT count
, INT action
)
2564 TRACE("s=%p, index=%d, count=%d, action=%d\n", s
, index
, count
, action
);
2574 if (s
[index
] == ' ') {
2575 while (index
&& (s
[index
] == ' '))
2578 while (index
&& (s
[index
] != ' '))
2580 if (s
[index
] == ' ')
2584 while (index
&& (s
[index
] != ' '))
2586 if (s
[index
] == ' ')
2596 if (s
[index
] == ' ')
2597 while ((index
< count
) && (s
[index
] == ' ')) index
++;
2599 while (s
[index
] && (s
[index
] != ' ') && (index
< count
))
2601 while ((s
[index
] == ' ') && (index
< count
)) index
++;
2605 case WB_ISDELIMITER
:
2606 ret
= (s
[index
] == ' ');
2609 ERR("unknown action code, please report !\n");
2616 /*********************************************************************
2620 * returns line number (not index) in high-order word of result.
2621 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2622 * to Richedit, not to the edit control. Original documentation is valid.
2623 * FIXME: do the specs mean to return -1 if outside client area or
2624 * if outside formatting rectangle ???
2627 static LRESULT
EDIT_EM_CharFromPos(EDITSTATE
*es
, INT x
, INT y
)
2635 GetClientRect(es
->hwndSelf
, &rc
);
2636 if (!PtInRect(&rc
, pt
))
2639 index
= EDIT_CharFromPos(es
, x
, y
, NULL
);
2640 return MAKELONG(index
, EDIT_EM_LineFromChar(es
, index
));
2644 /*********************************************************************
2648 * Enable or disable soft breaks.
2650 * This means: insert or remove the soft linebreak character (\r\r\n).
2651 * Take care to check if the text still fits the buffer after insertion.
2652 * If not, notify with EN_ERRSPACE.
2655 static BOOL
EDIT_EM_FmtLines(EDITSTATE
*es
, BOOL add_eol
)
2657 es
->flags
&= ~EF_USE_SOFTBRK
;
2659 es
->flags
|= EF_USE_SOFTBRK
;
2660 FIXME("soft break enabled, not implemented\n");
2666 /*********************************************************************
2670 * Hopefully this won't fire back at us.
2671 * We always start with a fixed buffer in the local heap.
2672 * Despite of the documentation says that the local heap is used
2673 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2674 * buffer on the local heap.
2677 static HLOCAL
EDIT_EM_GetHandle(EDITSTATE
*es
)
2681 if (!(es
->style
& ES_MULTILINE
))
2685 hLocal
= es
->hloc32W
;
2691 UINT countA
, alloc_size
;
2692 TRACE("Allocating 32-bit ANSI alias buffer\n");
2693 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, NULL
, 0, NULL
, NULL
);
2694 alloc_size
= ROUND_TO_GROW(countA
);
2695 if(!(es
->hloc32A
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
2697 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size
);
2700 textA
= LocalLock(es
->hloc32A
);
2701 WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, countA
, NULL
, NULL
);
2702 LocalUnlock(es
->hloc32A
);
2704 hLocal
= es
->hloc32A
;
2707 TRACE("Returning %p, LocalSize() = %ld\n", hLocal
, LocalSize(hLocal
));
2713 /*********************************************************************
2717 * Hopefully this won't fire back at us.
2718 * We always start with a buffer in 32 bit linear memory.
2719 * However, with this message a 16 bit application requests
2720 * a handle of 16 bit local heap memory, where it expects to find
2722 * It's a pitty that from this moment on we have to use this
2723 * local heap, because applications may rely on the handle
2726 * In this function we'll try to switch to local heap.
2728 static HLOCAL16
EDIT_EM_GetHandle16(EDITSTATE
*es
)
2730 HINSTANCE16 hInstance
= GetWindowLongW( es
->hwndSelf
, GWL_HINSTANCE
);
2732 UINT countA
, alloc_size
;
2734 if (!(es
->style
& ES_MULTILINE
))
2740 if (!LOCAL_HeapSize(hInstance
)) {
2741 if (!LocalInit16(hInstance
, 0,
2742 GlobalSize16(hInstance
))) {
2743 ERR("could not initialize local heap\n");
2746 TRACE("local heap initialized\n");
2749 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, NULL
, 0, NULL
, NULL
);
2750 alloc_size
= ROUND_TO_GROW(countA
);
2752 TRACE("Allocating 16-bit ANSI alias buffer\n");
2753 if (!(es
->hloc16
= LOCAL_Alloc(hInstance
, LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
))) {
2754 ERR("could not allocate new 16 bit buffer\n");
2758 if (!(textA
= (LPSTR
)LOCAL_Lock(hInstance
, es
->hloc16
))) {
2759 ERR("could not lock new 16 bit buffer\n");
2760 LOCAL_Free(hInstance
, es
->hloc16
);
2765 WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, countA
, NULL
, NULL
);
2766 LOCAL_Unlock(hInstance
, es
->hloc16
);
2768 TRACE("Returning %04X, LocalSize() = %d\n", es
->hloc16
, LOCAL_Size(hInstance
, es
->hloc16
));
2774 /*********************************************************************
2779 static INT
EDIT_EM_GetLine(EDITSTATE
*es
, INT line
, LPWSTR dst
, BOOL unicode
)
2782 INT line_len
, dst_len
;
2785 if (es
->style
& ES_MULTILINE
) {
2786 if (line
>= es
->line_count
)
2790 i
= EDIT_EM_LineIndex(es
, line
);
2792 line_len
= EDIT_EM_LineLength(es
, i
);
2793 dst_len
= *(WORD
*)dst
;
2796 if(dst_len
<= line_len
)
2798 memcpy(dst
, src
, dst_len
* sizeof(WCHAR
));
2801 else /* Append 0 if enough space */
2803 memcpy(dst
, src
, line_len
* sizeof(WCHAR
));
2810 INT ret
= WideCharToMultiByte(CP_ACP
, 0, src
, line_len
, (LPSTR
)dst
, dst_len
, NULL
, NULL
);
2811 if(!ret
&& line_len
) /* Insufficient buffer size */
2813 if(ret
< dst_len
) /* Append 0 if enough space */
2814 ((LPSTR
)dst
)[ret
] = 0;
2820 /*********************************************************************
2825 static LRESULT
EDIT_EM_GetSel(EDITSTATE
*es
, PUINT start
, PUINT end
)
2827 UINT s
= es
->selection_start
;
2828 UINT e
= es
->selection_end
;
2835 return MAKELONG(s
, e
);
2839 /*********************************************************************
2843 * FIXME: is this right ? (or should it be only VSCROLL)
2844 * (and maybe only for edit controls that really have their
2845 * own scrollbars) (and maybe only for multiline controls ?)
2846 * All in all: very poorly documented
2849 static LRESULT
EDIT_EM_GetThumb(EDITSTATE
*es
)
2852 return MAKELONG(EDIT_WM_VScroll(es
, EM_GETTHUMB
, 0),
2853 EDIT_WM_HScroll(es
, EM_GETTHUMB
, 0));
2855 return MAKELONG(EDIT_WM_VScroll(es
, EM_GETTHUMB16
, 0),
2856 EDIT_WM_HScroll(es
, EM_GETTHUMB16
, 0));
2861 /*********************************************************************
2866 static INT
EDIT_EM_LineFromChar(EDITSTATE
*es
, INT index
)
2871 if (!(es
->style
& ES_MULTILINE
))
2873 if (index
> (INT
)strlenW(es
->text
))
2874 return es
->line_count
- 1;
2876 index
= min(es
->selection_start
, es
->selection_end
);
2879 line_def
= es
->first_line_def
;
2880 index
-= line_def
->length
;
2881 while ((index
>= 0) && line_def
->next
) {
2883 line_def
= line_def
->next
;
2884 index
-= line_def
->length
;
2890 /*********************************************************************
2895 static INT
EDIT_EM_LineIndex(EDITSTATE
*es
, INT line
)
2900 if (!(es
->style
& ES_MULTILINE
))
2902 if (line
>= es
->line_count
)
2906 line_def
= es
->first_line_def
;
2908 INT index
= es
->selection_end
- line_def
->length
;
2909 while ((index
>= 0) && line_def
->next
) {
2910 line_index
+= line_def
->length
;
2911 line_def
= line_def
->next
;
2912 index
-= line_def
->length
;
2916 line_index
+= line_def
->length
;
2917 line_def
= line_def
->next
;
2925 /*********************************************************************
2930 static INT
EDIT_EM_LineLength(EDITSTATE
*es
, INT index
)
2934 if (!(es
->style
& ES_MULTILINE
))
2935 return strlenW(es
->text
);
2938 /* get the number of remaining non-selected chars of selected lines */
2939 INT32 l
; /* line number */
2940 INT32 li
; /* index of first char in line */
2942 l
= EDIT_EM_LineFromChar(es
, es
->selection_start
);
2943 /* # chars before start of selection area */
2944 count
= es
->selection_start
- EDIT_EM_LineIndex(es
, l
);
2945 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
2946 /* # chars after end of selection */
2947 li
= EDIT_EM_LineIndex(es
, l
);
2948 count
+= li
+ EDIT_EM_LineLength(es
, li
) - es
->selection_end
;
2951 line_def
= es
->first_line_def
;
2952 index
-= line_def
->length
;
2953 while ((index
>= 0) && line_def
->next
) {
2954 line_def
= line_def
->next
;
2955 index
-= line_def
->length
;
2957 return line_def
->net_length
;
2961 /*********************************************************************
2965 * NOTE: dx is in average character widths, dy - in lines;
2968 static BOOL
EDIT_EM_LineScroll(EDITSTATE
*es
, INT dx
, INT dy
)
2970 if (!(es
->style
& ES_MULTILINE
))
2973 dx
*= es
->char_width
;
2974 return EDIT_EM_LineScroll_internal(es
, dx
, dy
);
2977 /*********************************************************************
2979 * EDIT_EM_LineScroll_internal
2981 * Version of EDIT_EM_LineScroll for internal use.
2982 * It doesn't refuse if ES_MULTILINE is set and assumes that
2983 * dx is in pixels, dy - in lines.
2986 static BOOL
EDIT_EM_LineScroll_internal(EDITSTATE
*es
, INT dx
, INT dy
)
2989 INT x_offset_in_pixels
;
2990 INT lines_per_page
= (es
->format_rect
.bottom
- es
->format_rect
.top
) /
2993 if (es
->style
& ES_MULTILINE
)
2995 x_offset_in_pixels
= es
->x_offset
;
3000 x_offset_in_pixels
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->x_offset
, FALSE
));
3003 if (-dx
> x_offset_in_pixels
)
3004 dx
= -x_offset_in_pixels
;
3005 if (dx
> es
->text_width
- x_offset_in_pixels
)
3006 dx
= es
->text_width
- x_offset_in_pixels
;
3007 nyoff
= max(0, es
->y_offset
+ dy
);
3008 if (nyoff
>= es
->line_count
- lines_per_page
)
3009 nyoff
= max(0,es
->line_count
- lines_per_page
);
3010 dy
= (es
->y_offset
- nyoff
) * es
->line_height
;
3015 es
->y_offset
= nyoff
;
3016 if(es
->style
& ES_MULTILINE
)
3019 es
->x_offset
+= dx
/ es
->char_width
;
3021 GetClientRect(es
->hwndSelf
, &rc1
);
3022 IntersectRect(&rc
, &rc1
, &es
->format_rect
);
3023 ScrollWindowEx(es
->hwndSelf
, -dx
, dy
,
3024 NULL
, &rc
, NULL
, NULL
, SW_INVALIDATE
);
3025 /* force scroll info update */
3026 EDIT_UpdateScrollInfo(es
);
3028 if (dx
&& !(es
->flags
& EF_HSCROLL_TRACK
))
3029 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
3030 if (dy
&& !(es
->flags
& EF_VSCROLL_TRACK
))
3031 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
3036 /*********************************************************************
3041 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
)
3043 INT len
= strlenW(es
->text
);
3056 index
= min(index
, len
);
3057 dc
= GetDC(es
->hwndSelf
);
3059 old_font
= SelectObject(dc
, es
->font
);
3060 if (es
->style
& ES_MULTILINE
) {
3061 l
= EDIT_EM_LineFromChar(es
, index
);
3062 y
= (l
- es
->y_offset
) * es
->line_height
;
3063 li
= EDIT_EM_LineIndex(es
, l
);
3064 if (after_wrap
&& (li
== index
) && l
) {
3066 line_def
= es
->first_line_def
;
3068 line_def
= line_def
->next
;
3071 if (line_def
->ending
== END_WRAP
) {
3073 y
-= es
->line_height
;
3074 li
= EDIT_EM_LineIndex(es
, l
);
3078 line_def
= es
->first_line_def
;
3079 while (line_def
->index
!= li
)
3080 line_def
= line_def
->next
;
3082 ll
= line_def
->net_length
;
3083 lw
= line_def
->width
;
3085 w
= es
->format_rect
.right
- es
->format_rect
.left
;
3086 if (es
->style
& ES_RIGHT
)
3088 x
= LOWORD(GetTabbedTextExtentW(dc
, es
->text
+ li
+ (index
- li
), ll
- (index
- li
),
3089 es
->tabs_count
, es
->tabs
)) - es
->x_offset
;
3092 else if (es
->style
& ES_CENTER
)
3094 x
= LOWORD(GetTabbedTextExtentW(dc
, es
->text
+ li
, index
- li
,
3095 es
->tabs_count
, es
->tabs
)) - es
->x_offset
;
3100 x
= LOWORD(GetTabbedTextExtentW(dc
, es
->text
+ li
, index
- li
,
3101 es
->tabs_count
, es
->tabs
)) - es
->x_offset
;
3104 LPWSTR text
= EDIT_GetPasswordPointer_SL(es
);
3105 if (index
< es
->x_offset
) {
3106 GetTextExtentPoint32W(dc
, text
+ index
,
3107 es
->x_offset
- index
, &size
);
3110 GetTextExtentPoint32W(dc
, text
+ es
->x_offset
,
3111 index
- es
->x_offset
, &size
);
3114 if (!es
->x_offset
&& (es
->style
& (ES_RIGHT
| ES_CENTER
)))
3116 w
= es
->format_rect
.right
- es
->format_rect
.left
;
3117 if (w
> es
->text_width
)
3119 if (es
->style
& ES_RIGHT
)
3120 x
+= w
- es
->text_width
;
3121 else if (es
->style
& ES_CENTER
)
3122 x
+= (w
- es
->text_width
) / 2;
3127 if (es
->style
& ES_PASSWORD
)
3128 HeapFree(GetProcessHeap(), 0, text
);
3130 x
+= es
->format_rect
.left
;
3131 y
+= es
->format_rect
.top
;
3133 SelectObject(dc
, old_font
);
3134 ReleaseDC(es
->hwndSelf
, dc
);
3135 return MAKELONG((INT16
)x
, (INT16
)y
);
3139 /*********************************************************************
3143 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3146 static void EDIT_EM_ReplaceSel(EDITSTATE
*es
, BOOL can_undo
, LPCWSTR lpsz_replace
, BOOL send_update
, BOOL honor_limit
)
3148 UINT strl
= strlenW(lpsz_replace
);
3149 UINT tl
= strlenW(es
->text
);
3160 TRACE("%s, can_undo %d, send_update %d\n",
3161 debugstr_w(lpsz_replace
), can_undo
, send_update
);
3163 s
= es
->selection_start
;
3164 e
= es
->selection_end
;
3166 if ((s
== e
) && !strl
)
3171 size
= tl
- (e
- s
) + strl
;
3175 /* Issue the EN_MAXTEXT notification and continue with replacing text
3176 * such that buffer limit is honored. */
3177 if ((honor_limit
) && (es
->buffer_limit
> 0) && (size
> es
->buffer_limit
)) {
3178 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
3179 strl
= es
->buffer_limit
- (tl
- (e
-s
));
3182 if (!EDIT_MakeFit(es
, tl
- (e
- s
) + strl
))
3186 /* there is something to be deleted */
3187 TRACE("deleting stuff.\n");
3189 buf
= HeapAlloc(GetProcessHeap(), 0, (bufl
+ 1) * sizeof(WCHAR
));
3191 memcpy(buf
, es
->text
+ s
, bufl
* sizeof(WCHAR
));
3192 buf
[bufl
] = 0; /* ensure 0 termination */
3194 strcpyW(es
->text
+ s
, es
->text
+ e
);
3197 /* there is an insertion */
3198 tl
= strlenW(es
->text
);
3199 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
));
3200 for (p
= es
->text
+ tl
; p
>= es
->text
+ s
; p
--)
3202 for (i
= 0 , p
= es
->text
+ s
; i
< strl
; i
++)
3203 p
[i
] = lpsz_replace
[i
];
3204 if(es
->style
& ES_UPPERCASE
)
3205 CharUpperBuffW(p
, strl
);
3206 else if(es
->style
& ES_LOWERCASE
)
3207 CharLowerBuffW(p
, strl
);
3209 if (es
->style
& ES_MULTILINE
)
3211 INT st
= min(es
->selection_start
, es
->selection_end
);
3212 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
3214 hrgn
= CreateRectRgn(0, 0, 0, 0);
3215 EDIT_BuildLineDefs_ML(es
, st
, st
+ strl
,
3216 strl
- abs(es
->selection_end
- es
->selection_start
), hrgn
);
3217 /* if text is too long undo all changes */
3218 if (honor_limit
&& !(es
->style
& ES_AUTOVSCROLL
) && (es
->line_count
> vlc
)) {
3220 strcpyW(es
->text
+ e
, es
->text
+ e
+ strl
);
3222 for (i
= 0 , p
= es
->text
; i
< e
- s
; i
++)
3224 EDIT_BuildLineDefs_ML(es
, s
, e
,
3225 abs(es
->selection_end
- es
->selection_start
) - strl
, hrgn
);
3228 hrgn
= CreateRectRgn(0, 0, 0, 0);
3229 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
3233 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
3234 EDIT_CalcLineWidth_SL(es
);
3235 /* remove chars that don't fit */
3236 if (honor_limit
&& !(es
->style
& ES_AUTOHSCROLL
) && (es
->text_width
> fw
)) {
3237 while ((es
->text_width
> fw
) && s
+ strl
>= s
) {
3238 strcpyW(es
->text
+ s
+ strl
- 1, es
->text
+ s
+ strl
);
3240 EDIT_CalcLineWidth_SL(es
);
3242 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
3248 utl
= strlenW(es
->undo_text
);
3249 if (!es
->undo_insert_count
&& (*es
->undo_text
&& (s
== es
->undo_position
))) {
3250 /* undo-buffer is extended to the right */
3251 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
3252 memcpy(es
->undo_text
+ utl
, buf
, (e
- s
)*sizeof(WCHAR
));
3253 (es
->undo_text
+ utl
)[e
- s
] = 0; /* ensure 0 termination */
3254 } else if (!es
->undo_insert_count
&& (*es
->undo_text
&& (e
== es
->undo_position
))) {
3255 /* undo-buffer is extended to the left */
3256 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
3257 for (p
= es
->undo_text
+ utl
; p
>= es
->undo_text
; p
--)
3259 for (i
= 0 , p
= es
->undo_text
; i
< e
- s
; i
++)
3261 es
->undo_position
= s
;
3263 /* new undo-buffer */
3264 EDIT_MakeUndoFit(es
, e
- s
);
3265 memcpy(es
->undo_text
, buf
, (e
- s
)*sizeof(WCHAR
));
3266 es
->undo_text
[e
- s
] = 0; /* ensure 0 termination */
3267 es
->undo_position
= s
;
3269 /* any deletion makes the old insertion-undo invalid */
3270 es
->undo_insert_count
= 0;
3272 EDIT_EM_EmptyUndoBuffer(es
);
3276 if ((s
== es
->undo_position
) ||
3277 ((es
->undo_insert_count
) &&
3278 (s
== es
->undo_position
+ es
->undo_insert_count
)))
3280 * insertion is new and at delete position or
3281 * an extension to either left or right
3283 es
->undo_insert_count
+= strl
;
3285 /* new insertion undo */
3286 es
->undo_position
= s
;
3287 es
->undo_insert_count
= strl
;
3288 /* new insertion makes old delete-buffer invalid */
3289 *es
->undo_text
= '\0';
3292 EDIT_EM_EmptyUndoBuffer(es
);
3296 HeapFree(GetProcessHeap(), 0, buf
);
3300 /* If text has been deleted and we're right or center aligned then scroll rightward */
3301 if (es
->style
& (ES_RIGHT
| ES_CENTER
))
3303 INT delta
= strl
- abs(es
->selection_end
- es
->selection_start
);
3305 if (delta
< 0 && es
->x_offset
)
3307 if (abs(delta
) > es
->x_offset
)
3310 es
->x_offset
+= delta
;
3314 EDIT_EM_SetSel(es
, s
, s
, FALSE
);
3315 es
->flags
|= EF_MODIFIED
;
3316 if (send_update
) es
->flags
|= EF_UPDATE
;
3319 EDIT_UpdateTextRegion(es
, hrgn
, TRUE
);
3323 EDIT_UpdateText(es
, NULL
, TRUE
);
3325 EDIT_EM_ScrollCaret(es
);
3327 /* force scroll info update */
3328 EDIT_UpdateScrollInfo(es
);
3331 if(send_update
|| (es
->flags
& EF_UPDATE
))
3333 es
->flags
&= ~EF_UPDATE
;
3334 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
3339 /*********************************************************************
3344 static LRESULT
EDIT_EM_Scroll(EDITSTATE
*es
, INT action
)
3348 if (!(es
->style
& ES_MULTILINE
))
3349 return (LRESULT
)FALSE
;
3359 if (es
->y_offset
< es
->line_count
- 1)
3364 dy
= -(es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
3367 if (es
->y_offset
< es
->line_count
- 1)
3368 dy
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
3371 return (LRESULT
)FALSE
;
3374 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
3375 /* check if we are going to move too far */
3376 if(es
->y_offset
+ dy
> es
->line_count
- vlc
)
3377 dy
= es
->line_count
- vlc
- es
->y_offset
;
3379 /* Notification is done in EDIT_EM_LineScroll */
3381 EDIT_EM_LineScroll(es
, 0, dy
);
3384 return MAKELONG((SHORT
)dy
, (BOOL
)TRUE
);
3386 return MAKELONG((INT16
)dy
, (BOOL16
)TRUE
);
3391 /*********************************************************************
3396 static void EDIT_EM_ScrollCaret(EDITSTATE
*es
)
3398 if (es
->style
& ES_MULTILINE
) {
3403 INT cw
= es
->char_width
;
3408 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
3409 li
= EDIT_EM_LineIndex(es
, l
);
3410 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
));
3411 vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
3412 if (l
>= es
->y_offset
+ vlc
)
3413 dy
= l
- vlc
+ 1 - es
->y_offset
;
3414 if (l
< es
->y_offset
)
3415 dy
= l
- es
->y_offset
;
3416 ww
= es
->format_rect
.right
- es
->format_rect
.left
;
3417 if (x
< es
->format_rect
.left
)
3418 dx
= x
- es
->format_rect
.left
- ww
/ HSCROLL_FRACTION
/ cw
* cw
;
3419 if (x
> es
->format_rect
.right
)
3420 dx
= x
- es
->format_rect
.left
- (HSCROLL_FRACTION
- 1) * ww
/ HSCROLL_FRACTION
/ cw
* cw
;
3421 if (dy
|| dx
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
3423 /* check if we are going to move too far */
3424 if(es
->x_offset
+ dx
+ ww
> es
->text_width
)
3425 dx
= es
->text_width
- ww
- es
->x_offset
;
3426 if(dx
|| dy
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
3427 EDIT_EM_LineScroll_internal(es
, dx
, dy
);
3434 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
3435 format_width
= es
->format_rect
.right
- es
->format_rect
.left
;
3436 if (x
< es
->format_rect
.left
) {
3437 goal
= es
->format_rect
.left
+ format_width
/ HSCROLL_FRACTION
;
3440 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
3441 } while ((x
< goal
) && es
->x_offset
);
3442 /* FIXME: use ScrollWindow() somehow to improve performance */
3443 EDIT_UpdateText(es
, NULL
, TRUE
);
3444 } else if (x
> es
->format_rect
.right
) {
3446 INT len
= strlenW(es
->text
);
3447 goal
= es
->format_rect
.right
- format_width
/ HSCROLL_FRACTION
;
3450 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
3451 x_last
= (short)LOWORD(EDIT_EM_PosFromChar(es
, len
, FALSE
));
3452 } while ((x
> goal
) && (x_last
> es
->format_rect
.right
));
3453 /* FIXME: use ScrollWindow() somehow to improve performance */
3454 EDIT_UpdateText(es
, NULL
, TRUE
);
3458 if(es
->flags
& EF_FOCUSED
)
3459 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
3463 /*********************************************************************
3467 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3470 static void EDIT_EM_SetHandle(EDITSTATE
*es
, HLOCAL hloc
)
3473 HINSTANCE16 hInstance
= GetWindowLongW( es
->hwndSelf
, GWL_HINSTANCE
);
3476 if (!(es
->style
& ES_MULTILINE
))
3480 WARN("called with NULL handle\n");
3484 EDIT_UnlockBuffer(es
, TRUE
);
3489 LOCAL_Free(hInstance
, es
->hloc16
);
3490 es
->hloc16
= (HLOCAL16
)NULL
;
3498 LocalFree(es
->hloc32A
);
3510 countA
= LocalSize(hloc
);
3511 textA
= LocalLock(hloc
);
3512 countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
3513 if(!(hloc32W_new
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, countW
* sizeof(WCHAR
))))
3515 ERR("Could not allocate new unicode buffer\n");
3518 textW
= LocalLock(hloc32W_new
);
3519 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, textW
, countW
);
3520 LocalUnlock(hloc32W_new
);
3524 LocalFree(es
->hloc32W
);
3526 es
->hloc32W
= hloc32W_new
;
3530 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
3532 EDIT_LockBuffer(es
);
3534 es
->x_offset
= es
->y_offset
= 0;
3535 es
->selection_start
= es
->selection_end
= 0;
3536 EDIT_EM_EmptyUndoBuffer(es
);
3537 es
->flags
&= ~EF_MODIFIED
;
3538 es
->flags
&= ~EF_UPDATE
;
3539 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
3540 EDIT_UpdateText(es
, NULL
, TRUE
);
3541 EDIT_EM_ScrollCaret(es
);
3542 /* force scroll info update */
3543 EDIT_UpdateScrollInfo(es
);
3548 /*********************************************************************
3552 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3555 static void EDIT_EM_SetHandle16(EDITSTATE
*es
, HLOCAL16 hloc
)
3557 HINSTANCE16 hInstance
= GetWindowLongW( es
->hwndSelf
, GWL_HINSTANCE
);
3563 if (!(es
->style
& ES_MULTILINE
))
3567 WARN("called with NULL handle\n");
3571 EDIT_UnlockBuffer(es
, TRUE
);
3575 LocalFree(es
->hloc32A
);
3579 countA
= LOCAL_Size(hInstance
, hloc
);
3580 textA
= LOCAL_Lock(hInstance
, hloc
);
3581 countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
3582 if(!(hloc32W_new
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, countW
* sizeof(WCHAR
))))
3584 ERR("Could not allocate new unicode buffer\n");
3587 textW
= LocalLock(hloc32W_new
);
3588 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, textW
, countW
);
3589 LocalUnlock(hloc32W_new
);
3590 LOCAL_Unlock(hInstance
, hloc
);
3593 LocalFree(es
->hloc32W
);
3595 es
->hloc32W
= hloc32W_new
;
3598 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
3600 EDIT_LockBuffer(es
);
3602 es
->x_offset
= es
->y_offset
= 0;
3603 es
->selection_start
= es
->selection_end
= 0;
3604 EDIT_EM_EmptyUndoBuffer(es
);
3605 es
->flags
&= ~EF_MODIFIED
;
3606 es
->flags
&= ~EF_UPDATE
;
3607 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
3608 EDIT_UpdateText(es
, NULL
, TRUE
);
3609 EDIT_EM_ScrollCaret(es
);
3610 /* force scroll info update */
3611 EDIT_UpdateScrollInfo(es
);
3616 /*********************************************************************
3620 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3621 * However, the windows version is not complied to yet in all of edit.c
3623 * Additionally as the wrapper for RichEdit controls we need larger buffers
3624 * at present -1 will represent nolimit
3626 static void EDIT_EM_SetLimitText(EDITSTATE
*es
, INT limit
)
3628 if (limit
== 0xFFFFFFFF)
3629 es
->buffer_limit
= -1;
3630 else if (es
->style
& ES_MULTILINE
) {
3632 es
->buffer_limit
= min(limit
, BUFLIMIT_MULTI
);
3634 es
->buffer_limit
= BUFLIMIT_MULTI
;
3637 es
->buffer_limit
= min(limit
, BUFLIMIT_SINGLE
);
3639 es
->buffer_limit
= BUFLIMIT_SINGLE
;
3644 /*********************************************************************
3648 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3649 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3650 * margin according to the textmetrics of the current font.
3652 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3653 * of the char's width as the margin, but this is not how Windows handles this.
3654 * For all other fonts Windows sets the margins to zero.
3657 static void EDIT_EM_SetMargins(EDITSTATE
*es
, INT action
,
3658 INT left
, INT right
, BOOL repaint
)
3661 INT default_left_margin
= 0; /* in pixels */
3662 INT default_right_margin
= 0; /* in pixels */
3664 /* Set the default margins depending on the font */
3665 if (es
->font
&& (left
== EC_USEFONTINFO
|| right
== EC_USEFONTINFO
)) {
3666 HDC dc
= GetDC(es
->hwndSelf
);
3667 HFONT old_font
= SelectObject(dc
, es
->font
);
3668 GetTextMetricsW(dc
, &tm
);
3669 /* The default margins are only non zero for TrueType or Vector fonts */
3670 if (tm
.tmPitchAndFamily
& ( TMPF_VECTOR
| TMPF_TRUETYPE
)) {
3671 /* This must be calculated more exactly! But how? */
3672 default_left_margin
= tm
.tmAveCharWidth
/ 3;
3673 default_right_margin
= tm
.tmAveCharWidth
/ 3;
3675 SelectObject(dc
, old_font
);
3676 ReleaseDC(es
->hwndSelf
, dc
);
3679 if (action
& EC_LEFTMARGIN
) {
3680 es
->format_rect
.left
-= es
->left_margin
;
3681 if (left
!= EC_USEFONTINFO
)
3682 es
->left_margin
= left
;
3684 es
->left_margin
= default_left_margin
;
3685 es
->format_rect
.left
+= es
->left_margin
;
3688 if (action
& EC_RIGHTMARGIN
) {
3689 es
->format_rect
.right
+= es
->right_margin
;
3690 if (right
!= EC_USEFONTINFO
)
3691 es
->right_margin
= right
;
3693 es
->right_margin
= default_right_margin
;
3694 es
->format_rect
.right
-= es
->right_margin
;
3697 if (action
& (EC_LEFTMARGIN
| EC_RIGHTMARGIN
)) {
3698 EDIT_AdjustFormatRect(es
);
3699 if (repaint
) EDIT_UpdateText(es
, NULL
, TRUE
);
3702 TRACE("left=%d, right=%d\n", es
->left_margin
, es
->right_margin
);
3706 /*********************************************************************
3708 * EM_SETPASSWORDCHAR
3711 static void EDIT_EM_SetPasswordChar(EDITSTATE
*es
, WCHAR c
)
3715 if (es
->style
& ES_MULTILINE
)
3718 if (es
->password_char
== c
)
3721 style
= GetWindowLongW( es
->hwndSelf
, GWL_STYLE
);
3722 es
->password_char
= c
;
3724 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
| ES_PASSWORD
);
3725 es
->style
|= ES_PASSWORD
;
3727 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
& ~ES_PASSWORD
);
3728 es
->style
&= ~ES_PASSWORD
;
3730 EDIT_UpdateText(es
, NULL
, TRUE
);
3734 /*********************************************************************
3738 * note: unlike the specs say: the order of start and end
3739 * _is_ preserved in Windows. (i.e. start can be > end)
3740 * In other words: this handler is OK
3743 static void EDIT_EM_SetSel(EDITSTATE
*es
, UINT start
, UINT end
, BOOL after_wrap
)
3745 UINT old_start
= es
->selection_start
;
3746 UINT old_end
= es
->selection_end
;
3747 UINT len
= strlenW(es
->text
);
3749 if (start
== (UINT
)-1) {
3750 start
= es
->selection_end
;
3751 end
= es
->selection_end
;
3753 start
= min(start
, len
);
3754 end
= min(end
, len
);
3756 es
->selection_start
= start
;
3757 es
->selection_end
= end
;
3759 es
->flags
|= EF_AFTER_WRAP
;
3761 es
->flags
&= ~EF_AFTER_WRAP
;
3762 /* Compute the necessary invalidation region. */
3763 /* Note that we don't need to invalidate regions which have
3764 * "never" been selected, or those which are "still" selected.
3765 * In fact, every time we hit a selection boundary, we can
3766 * *toggle* whether we need to invalidate. Thus we can optimize by
3767 * *sorting* the interval endpoints. Let's assume that we sort them
3769 * start <= end <= old_start <= old_end
3770 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3771 * in 5 comparisons; ie it's impossible to do better than the
3773 ORDER_UINT(end
, old_end
);
3774 ORDER_UINT(start
, old_start
);
3775 ORDER_UINT(old_start
, old_end
);
3776 ORDER_UINT(start
, end
);
3777 /* Note that at this point 'end' and 'old_start' are not in order, but
3778 * start is definitely the min. and old_end is definitely the max. */
3779 if (end
!= old_start
)
3783 * ORDER_UINT32(end, old_start);
3784 * EDIT_InvalidateText(es, start, end);
3785 * EDIT_InvalidateText(es, old_start, old_end);
3786 * in place of the following if statement.
3787 * (That would complete the optimal five-comparison four-element sort.)
3789 if (old_start
> end
)
3791 EDIT_InvalidateText(es
, start
, end
);
3792 EDIT_InvalidateText(es
, old_start
, old_end
);
3796 EDIT_InvalidateText(es
, start
, old_start
);
3797 EDIT_InvalidateText(es
, end
, old_end
);
3800 else EDIT_InvalidateText(es
, start
, old_end
);
3804 /*********************************************************************
3809 static BOOL
EDIT_EM_SetTabStops(EDITSTATE
*es
, INT count
, LPINT tabs
)
3811 if (!(es
->style
& ES_MULTILINE
))
3813 HeapFree(GetProcessHeap(), 0, es
->tabs
);
3814 es
->tabs_count
= count
;
3818 es
->tabs
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(INT
));
3819 memcpy(es
->tabs
, tabs
, count
* sizeof(INT
));
3826 /*********************************************************************
3831 static BOOL
EDIT_EM_SetTabStops16(EDITSTATE
*es
, INT count
, LPINT16 tabs
)
3833 if (!(es
->style
& ES_MULTILINE
))
3836 HeapFree(GetProcessHeap(), 0, es
->tabs
);
3837 es
->tabs_count
= count
;
3842 es
->tabs
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(INT
));
3843 for (i
= 0 ; i
< count
; i
++)
3844 es
->tabs
[i
] = *tabs
++;
3851 /*********************************************************************
3853 * EM_SETWORDBREAKPROC
3856 static void EDIT_EM_SetWordBreakProc(EDITSTATE
*es
, void *wbp
)
3858 if (es
->word_break_proc
== wbp
)
3861 es
->word_break_proc
= wbp
;
3863 es
->word_break_proc16
= NULL
;
3866 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
)) {
3867 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
3868 EDIT_UpdateText(es
, NULL
, TRUE
);
3874 /*********************************************************************
3876 * EM_SETWORDBREAKPROC16
3879 static void EDIT_EM_SetWordBreakProc16(EDITSTATE
*es
, EDITWORDBREAKPROC16 wbp
)
3881 if (es
->word_break_proc16
== wbp
)
3884 es
->word_break_proc
= NULL
;
3885 es
->word_break_proc16
= wbp
;
3886 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
)) {
3887 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
3888 EDIT_UpdateText(es
, NULL
, TRUE
);
3894 /*********************************************************************
3899 static BOOL
EDIT_EM_Undo(EDITSTATE
*es
)
3904 /* As per MSDN spec, for a single-line edit control,
3905 the return value is always TRUE */
3906 if( es
->style
& ES_READONLY
)
3907 return !(es
->style
& ES_MULTILINE
);
3909 ulength
= strlenW(es
->undo_text
);
3911 utext
= HeapAlloc(GetProcessHeap(), 0, (ulength
+ 1) * sizeof(WCHAR
));
3913 strcpyW(utext
, es
->undo_text
);
3915 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3916 es
->undo_insert_count
, debugstr_w(utext
));
3918 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3919 EDIT_EM_EmptyUndoBuffer(es
);
3920 EDIT_EM_ReplaceSel(es
, TRUE
, utext
, TRUE
, TRUE
);
3921 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3922 /* send the notification after the selection start and end are set */
3923 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
3924 EDIT_EM_ScrollCaret(es
);
3925 HeapFree(GetProcessHeap(), 0, utext
);
3927 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3928 es
->undo_insert_count
, debugstr_w(es
->undo_text
));
3933 /*********************************************************************
3938 static void EDIT_WM_Char(EDITSTATE
*es
, WCHAR c
)
3942 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3946 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3947 if(!(es
->style
& ES_MULTILINE
) && !(es
->style
& ES_WANTRETURN
))
3950 if (es
->style
& ES_MULTILINE
) {
3951 if (es
->style
& ES_READONLY
) {
3952 EDIT_MoveHome(es
, FALSE
);
3953 EDIT_MoveDown_ML(es
, FALSE
);
3955 static const WCHAR cr_lfW
[] = {'\r','\n',0};
3956 EDIT_EM_ReplaceSel(es
, TRUE
, cr_lfW
, TRUE
, TRUE
);
3961 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_READONLY
))
3963 static const WCHAR tabW
[] = {'\t',0};
3964 EDIT_EM_ReplaceSel(es
, TRUE
, tabW
, TRUE
, TRUE
);
3968 if (!(es
->style
& ES_READONLY
) && !control
) {
3969 if (es
->selection_start
!= es
->selection_end
)
3972 /* delete character left of caret */
3973 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3974 EDIT_MoveBackward(es
, TRUE
);
3980 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3983 if (!(es
->style
& ES_READONLY
))
3984 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3987 if (!(es
->style
& ES_READONLY
))
3988 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3992 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3993 if( (es
->style
& ES_NUMBER
) && !( c
>= '0' && c
<= '9') )
3996 if (!(es
->style
& ES_READONLY
) && (c
>= ' ') && (c
!= 127)) {
4000 EDIT_EM_ReplaceSel(es
, TRUE
, str
, TRUE
, TRUE
);
4007 /*********************************************************************
4012 static void EDIT_WM_Command(EDITSTATE
*es
, INT code
, INT id
, HWND control
)
4014 if (code
|| control
)
4034 EDIT_EM_SetSel(es
, 0, (UINT
)-1, FALSE
);
4035 EDIT_EM_ScrollCaret(es
);
4038 ERR("unknown menu item, please report\n");
4044 /*********************************************************************
4048 * Note: the resource files resource/sysres_??.rc cannot define a
4049 * single popup menu. Hence we use a (dummy) menubar
4050 * containing the single popup menu as its first item.
4052 * FIXME: the message identifiers have been chosen arbitrarily,
4053 * hence we use MF_BYPOSITION.
4054 * We might as well use the "real" values (anybody knows ?)
4055 * The menu definition is in resources/sysres_??.rc.
4056 * Once these are OK, we better use MF_BYCOMMAND here
4057 * (as we do in EDIT_WM_Command()).
4060 static void EDIT_WM_ContextMenu(EDITSTATE
*es
, INT x
, INT y
)
4063 HMENU menu
= LoadMenuA(User32Instance
, "EDITMENU");
4065 HMENU menu
= LoadMenuA(user32_module
, "EDITMENU");
4067 HMENU popup
= GetSubMenu(menu
, 0);
4068 UINT start
= es
->selection_start
;
4069 UINT end
= es
->selection_end
;
4071 ORDER_UINT(start
, end
);
4074 EnableMenuItem(popup
, 0, MF_BYPOSITION
| (EDIT_EM_CanUndo(es
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
4076 EnableMenuItem(popup
, 2, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
4078 EnableMenuItem(popup
, 3, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) ? MF_ENABLED
: MF_GRAYED
));
4080 EnableMenuItem(popup
, 4, MF_BYPOSITION
| (IsClipboardFormatAvailable(CF_UNICODETEXT
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
4082 EnableMenuItem(popup
, 5, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
4084 EnableMenuItem(popup
, 7, MF_BYPOSITION
| (start
|| (end
!= strlenW(es
->text
)) ? MF_ENABLED
: MF_GRAYED
));
4086 TrackPopupMenu(popup
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, es
->hwndSelf
, NULL
);
4091 /*********************************************************************
4096 static void EDIT_WM_Copy(EDITSTATE
*es
)
4098 INT s
= min(es
->selection_start
, es
->selection_end
);
4099 INT e
= max(es
->selection_start
, es
->selection_end
);
4107 hdst
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (len
+ 1) * sizeof(WCHAR
));
4108 dst
= GlobalLock(hdst
);
4109 memcpy(dst
, es
->text
+ s
, len
* sizeof(WCHAR
));
4110 dst
[len
] = 0; /* ensure 0 termination */
4111 TRACE("%s\n", debugstr_w(dst
));
4113 OpenClipboard(es
->hwndSelf
);
4115 SetClipboardData(CF_UNICODETEXT
, hdst
);
4120 /*********************************************************************
4125 static LRESULT
EDIT_WM_Create(EDITSTATE
*es
, LPCWSTR name
)
4129 TRACE("%s\n", debugstr_w(name
));
4131 * To initialize some final structure members, we call some helper
4132 * functions. However, since the EDITSTATE is not consistent (i.e.
4133 * not fully initialized), we should be very careful which
4134 * functions can be called, and in what order.
4136 EDIT_WM_SetFont(es
, 0, FALSE
);
4137 EDIT_EM_EmptyUndoBuffer(es
);
4139 /* We need to calculate the format rect
4140 (applications may send EM_SETMARGINS before the control gets visible) */
4141 GetClientRect(es
->hwndSelf
, &clientRect
);
4142 EDIT_SetRectNP(es
, &clientRect
);
4144 if (name
&& *name
) {
4145 EDIT_EM_ReplaceSel(es
, FALSE
, name
, FALSE
, TRUE
);
4146 /* if we insert text to the editline, the text scrolls out
4147 * of the window, as the caret is placed after the insert
4148 * pos normally; thus we reset es->selection... to 0 and
4151 es
->selection_start
= es
->selection_end
= 0;
4152 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4153 * Messages are only to be sent when the USER does something to
4154 * change the contents. So I am removing this EN_CHANGE
4156 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4158 EDIT_EM_ScrollCaret(es
);
4160 /* force scroll info update */
4161 EDIT_UpdateScrollInfo(es
);
4162 /* The rule seems to return 1 here for success */
4163 /* Power Builder masked edit controls will crash */
4165 /* FIXME: is that in all cases so ? */
4170 /*********************************************************************
4175 static LRESULT
EDIT_WM_Destroy(EDITSTATE
*es
)
4180 while (LocalUnlock(es
->hloc32W
)) ;
4181 LocalFree(es
->hloc32W
);
4184 while (LocalUnlock(es
->hloc32A
)) ;
4185 LocalFree(es
->hloc32A
);
4189 HINSTANCE16 hInstance
= GetWindowWord( es
->hwndSelf
, GWL_HINSTANCE
);
4190 while (LOCAL_Unlock(hInstance
, es
->hloc16
)) ;
4191 LOCAL_Free(hInstance
, es
->hloc16
);
4195 pc
= es
->first_line_def
;
4199 HeapFree(GetProcessHeap(), 0, pc
);
4203 SetWindowLongPtrW( es
->hwndSelf
, 0, 0 );
4204 HeapFree(GetProcessHeap(), 0, es
);
4210 /*********************************************************************
4215 static LRESULT
EDIT_WM_EraseBkGnd(EDITSTATE
*es
, HDC dc
)
4217 /* we do the proper erase in EDIT_WM_Paint */
4222 /*********************************************************************
4227 static INT
EDIT_WM_GetText(EDITSTATE
*es
, INT count
, LPWSTR dst
, BOOL unicode
)
4229 if(!count
) return 0;
4233 lstrcpynW(dst
, es
->text
, count
);
4234 return strlenW(dst
);
4238 LPSTR textA
= (LPSTR
)dst
;
4239 if (!WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, count
, NULL
, NULL
))
4240 textA
[count
- 1] = 0; /* ensure 0 termination */
4241 return strlen(textA
);
4246 /*********************************************************************
4251 static LRESULT
EDIT_WM_HScroll(EDITSTATE
*es
, INT action
, INT pos
)
4256 if (!(es
->style
& ES_MULTILINE
))
4259 if (!(es
->style
& ES_AUTOHSCROLL
))
4263 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4266 TRACE("SB_LINELEFT\n");
4268 dx
= -es
->char_width
;
4271 TRACE("SB_LINERIGHT\n");
4272 if (es
->x_offset
< es
->text_width
)
4273 dx
= es
->char_width
;
4276 TRACE("SB_PAGELEFT\n");
4278 dx
= -fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4281 TRACE("SB_PAGERIGHT\n");
4282 if (es
->x_offset
< es
->text_width
)
4283 dx
= fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4291 TRACE("SB_RIGHT\n");
4292 if (es
->x_offset
< es
->text_width
)
4293 dx
= es
->text_width
- es
->x_offset
;
4296 TRACE("SB_THUMBTRACK %d\n", pos
);
4297 es
->flags
|= EF_HSCROLL_TRACK
;
4298 if(es
->style
& WS_HSCROLL
)
4299 dx
= pos
- es
->x_offset
;
4304 if(pos
< 0 || pos
> 100) return 0;
4305 /* Assume default scroll range 0-100 */
4306 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4307 new_x
= pos
* (es
->text_width
- fw
) / 100;
4308 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4311 case SB_THUMBPOSITION
:
4312 TRACE("SB_THUMBPOSITION %d\n", pos
);
4313 es
->flags
&= ~EF_HSCROLL_TRACK
;
4314 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4315 dx
= pos
- es
->x_offset
;
4320 if(pos
< 0 || pos
> 100) return 0;
4321 /* Assume default scroll range 0-100 */
4322 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4323 new_x
= pos
* (es
->text_width
- fw
) / 100;
4324 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4327 /* force scroll info update */
4328 EDIT_UpdateScrollInfo(es
);
4329 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
4333 TRACE("SB_ENDSCROLL\n");
4336 * FIXME : the next two are undocumented !
4337 * Are we doing the right thing ?
4338 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4339 * although it's also a regular control message.
4341 case EM_GETTHUMB
: /* this one is used by NT notepad */
4347 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4348 ret
= GetScrollPos(es
->hwndSelf
, SB_HORZ
);
4351 /* Assume default scroll range 0-100 */
4352 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4353 ret
= es
->text_width
? es
->x_offset
* 100 / (es
->text_width
- fw
) : 0;
4355 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4359 case EM_LINESCROLL16
:
4360 TRACE("EM_LINESCROLL16\n");
4366 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4372 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4373 /* check if we are going to move too far */
4374 if(es
->x_offset
+ dx
+ fw
> es
->text_width
)
4375 dx
= es
->text_width
- fw
- es
->x_offset
;
4377 EDIT_EM_LineScroll_internal(es
, dx
, 0);
4383 /*********************************************************************
4388 static BOOL
EDIT_CheckCombo(EDITSTATE
*es
, UINT msg
, INT key
)
4390 HWND hLBox
= es
->hwndListBox
;
4398 hCombo
= GetParent(es
->hwndSelf
);
4402 TRACE_(combo
)("[%p]: handling msg %x (%x)\n", es
->hwndSelf
, msg
, key
);
4404 if (key
== VK_UP
|| key
== VK_DOWN
)
4406 if (SendMessageW(hCombo
, CB_GETEXTENDEDUI
, 0, 0))
4409 if (msg
== WM_KEYDOWN
|| nEUI
)
4410 bDropped
= (BOOL
)SendMessageW(hCombo
, CB_GETDROPPEDSTATE
, 0, 0);
4416 if (!bDropped
&& nEUI
&& (key
== VK_UP
|| key
== VK_DOWN
))
4418 /* make sure ComboLBox pops up */
4419 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, FALSE
, 0);
4424 SendMessageW(hLBox
, WM_KEYDOWN
, (WPARAM
)key
, 0);
4427 case WM_SYSKEYDOWN
: /* Handle Alt+up/down arrows */
4429 SendMessageW(hCombo
, CB_SHOWDROPDOWN
, bDropped
? FALSE
: TRUE
, 0);
4431 SendMessageW(hLBox
, WM_KEYDOWN
, (WPARAM
)VK_F4
, 0);
4436 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, TRUE
, 0);
4442 /*********************************************************************
4446 * Handling of special keys that don't produce a WM_CHAR
4447 * (i.e. non-printable keys) & Backspace & Delete
4450 static LRESULT
EDIT_WM_KeyDown(EDITSTATE
*es
, INT key
)
4455 if (GetKeyState(VK_MENU
) & 0x8000)
4458 shift
= GetKeyState(VK_SHIFT
) & 0x8000;
4459 control
= GetKeyState(VK_CONTROL
) & 0x8000;
4464 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
) || key
== VK_F4
)
4469 if ((es
->style
& ES_MULTILINE
) && (key
== VK_UP
))
4470 EDIT_MoveUp_ML(es
, shift
);
4473 EDIT_MoveWordBackward(es
, shift
);
4475 EDIT_MoveBackward(es
, shift
);
4478 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
))
4482 if ((es
->style
& ES_MULTILINE
) && (key
== VK_DOWN
))
4483 EDIT_MoveDown_ML(es
, shift
);
4485 EDIT_MoveWordForward(es
, shift
);
4487 EDIT_MoveForward(es
, shift
);
4490 EDIT_MoveHome(es
, shift
);
4493 EDIT_MoveEnd(es
, shift
);
4496 if (es
->style
& ES_MULTILINE
)
4497 EDIT_MovePageUp_ML(es
, shift
);
4499 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
4502 if (es
->style
& ES_MULTILINE
)
4503 EDIT_MovePageDown_ML(es
, shift
);
4505 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
4508 if (!(es
->style
& ES_READONLY
) && !(shift
&& control
)) {
4509 if (es
->selection_start
!= es
->selection_end
) {
4516 /* delete character left of caret */
4517 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
4518 EDIT_MoveBackward(es
, TRUE
);
4520 } else if (control
) {
4521 /* delete to end of line */
4522 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
4523 EDIT_MoveEnd(es
, TRUE
);
4526 /* delete character right of caret */
4527 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
4528 EDIT_MoveForward(es
, TRUE
);
4536 if (!(es
->style
& ES_READONLY
))
4542 /* If the edit doesn't want the return send a message to the default object */
4543 if(!(es
->style
& ES_WANTRETURN
))
4545 HWND hwndParent
= GetParent(es
->hwndSelf
);
4546 DWORD dw
= SendMessageW( hwndParent
, DM_GETDEFID
, 0, 0 );
4547 if (HIWORD(dw
) == DC_HASDEFID
)
4549 SendMessageW( hwndParent
, WM_COMMAND
,
4550 MAKEWPARAM( LOWORD(dw
), BN_CLICKED
),
4551 (LPARAM
)GetDlgItem( hwndParent
, LOWORD(dw
) ) );
4560 /*********************************************************************
4565 static LRESULT
EDIT_WM_KillFocus(EDITSTATE
*es
)
4567 es
->flags
&= ~EF_FOCUSED
;
4569 if(!(es
->style
& ES_NOHIDESEL
))
4570 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
4571 EDIT_NOTIFY_PARENT(es
, EN_KILLFOCUS
);
4576 /*********************************************************************
4580 * The caret position has been set on the WM_LBUTTONDOWN message
4583 static LRESULT
EDIT_WM_LButtonDblClk(EDITSTATE
*es
)
4586 INT e
= es
->selection_end
;
4591 es
->bCaptureState
= TRUE
;
4592 SetCapture(es
->hwndSelf
);
4594 l
= EDIT_EM_LineFromChar(es
, e
);
4595 li
= EDIT_EM_LineIndex(es
, l
);
4596 ll
= EDIT_EM_LineLength(es
, e
);
4597 s
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
4598 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_RIGHT
);
4599 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
4600 EDIT_EM_ScrollCaret(es
);
4601 es
->region_posx
= es
->region_posy
= 0;
4602 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
4607 /*********************************************************************
4612 static LRESULT
EDIT_WM_LButtonDown(EDITSTATE
*es
, DWORD keys
, INT x
, INT y
)
4617 es
->bCaptureState
= TRUE
;
4618 SetCapture(es
->hwndSelf
);
4619 EDIT_ConfinePoint(es
, &x
, &y
);
4620 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
4621 EDIT_EM_SetSel(es
, (keys
& MK_SHIFT
) ? es
->selection_start
: e
, e
, after_wrap
);
4622 EDIT_EM_ScrollCaret(es
);
4623 es
->region_posx
= es
->region_posy
= 0;
4624 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
4626 if (!(es
->flags
& EF_FOCUSED
))
4627 SetFocus(es
->hwndSelf
);
4633 /*********************************************************************
4638 static LRESULT
EDIT_WM_LButtonUp(EDITSTATE
*es
)
4640 if (es
->bCaptureState
) {
4641 KillTimer(es
->hwndSelf
, 0);
4642 if (GetCapture() == es
->hwndSelf
) ReleaseCapture();
4644 es
->bCaptureState
= FALSE
;
4649 /*********************************************************************
4654 static LRESULT
EDIT_WM_MButtonDown(EDITSTATE
*es
)
4656 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
4661 /*********************************************************************
4666 static LRESULT
EDIT_WM_MouseMove(EDITSTATE
*es
, INT x
, INT y
)
4672 /* If the mouse has been captured by process other than the edit control itself,
4673 * the windows edit controls will not select the strings with mouse move.
4675 if (!es
->bCaptureState
|| GetCapture() != es
->hwndSelf
)
4679 * FIXME: gotta do some scrolling if outside client
4680 * area. Maybe reset the timer ?
4683 EDIT_ConfinePoint(es
, &x
, &y
);
4684 es
->region_posx
= (prex
< x
) ? -1 : ((prex
> x
) ? 1 : 0);
4685 es
->region_posy
= (prey
< y
) ? -1 : ((prey
> y
) ? 1 : 0);
4686 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
4687 EDIT_EM_SetSel(es
, es
->selection_start
, e
, after_wrap
);
4688 EDIT_SetCaretPos(es
,es
->selection_end
,es
->flags
& EF_AFTER_WRAP
);
4693 /*********************************************************************
4697 * See also EDIT_WM_StyleChanged
4699 static LRESULT
EDIT_WM_NCCreate(HWND hwnd
, LPCREATESTRUCTW lpcs
, BOOL unicode
)
4704 TRACE("Creating %s edit control, style = %08lx\n",
4705 unicode
? "Unicode" : "ANSI", lpcs
->style
);
4707 if (!(es
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*es
))))
4709 SetWindowLongPtrW( hwnd
, 0, (LONG
)es
);
4712 * Note: since the EDITSTATE has not been fully initialized yet,
4713 * we can't use any API calls that may send
4714 * WM_XXX messages before WM_NCCREATE is completed.
4717 es
->is_unicode
= unicode
;
4718 es
->style
= lpcs
->style
;
4720 es
->bEnableState
= !(es
->style
& WS_DISABLED
);
4722 es
->hwndSelf
= hwnd
;
4723 /* Save parent, which will be notified by EN_* messages */
4724 es
->hwndParent
= lpcs
->hwndParent
;
4726 if (es
->style
& ES_COMBO
)
4727 es
->hwndListBox
= GetDlgItem(es
->hwndParent
, ID_CB_LISTBOX
);
4729 /* Number overrides lowercase overrides uppercase (at least it
4730 * does in Win95). However I'll bet that ES_NUMBER would be
4731 * invalid under Win 3.1.
4733 if (es
->style
& ES_NUMBER
) {
4734 ; /* do not override the ES_NUMBER */
4735 } else if (es
->style
& ES_LOWERCASE
) {
4736 es
->style
&= ~ES_UPPERCASE
;
4738 if (es
->style
& ES_MULTILINE
) {
4739 es
->buffer_limit
= BUFLIMIT_MULTI
;
4740 if (es
->style
& WS_VSCROLL
)
4741 es
->style
|= ES_AUTOVSCROLL
;
4742 if (es
->style
& WS_HSCROLL
)
4743 es
->style
|= ES_AUTOHSCROLL
;
4744 es
->style
&= ~ES_PASSWORD
;
4745 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
)) {
4746 /* Confirmed - RIGHT overrides CENTER */
4747 if (es
->style
& ES_RIGHT
)
4748 es
->style
&= ~ES_CENTER
;
4749 es
->style
&= ~WS_HSCROLL
;
4750 es
->style
&= ~ES_AUTOHSCROLL
;
4753 es
->buffer_limit
= BUFLIMIT_SINGLE
;
4754 if ((es
->style
& ES_RIGHT
) && (es
->style
& ES_CENTER
))
4755 es
->style
&= ~ES_CENTER
;
4756 es
->style
&= ~WS_HSCROLL
;
4757 es
->style
&= ~WS_VSCROLL
;
4758 if (es
->style
& ES_PASSWORD
)
4759 es
->password_char
= '*';
4762 alloc_size
= ROUND_TO_GROW((es
->buffer_size
+ 1) * sizeof(WCHAR
));
4763 if(!(es
->hloc32W
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
4765 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
4767 if (!(es
->undo_text
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (es
->buffer_size
+ 1) * sizeof(WCHAR
))))
4769 es
->undo_buffer_size
= es
->buffer_size
;
4771 if (es
->style
& ES_MULTILINE
)
4772 if (!(es
->first_line_def
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
))))
4777 * In Win95 look and feel, the WS_BORDER style is replaced by the
4778 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4779 * control a nonclient area so we don't need to draw the border.
4780 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4781 * a nonclient area and we should handle painting the border ourselves.
4783 * When making modifications please ensure that the code still works
4784 * for edit controls created directly with style 0x50800000, exStyle 0
4785 * (which should have a single pixel border)
4787 if (lpcs
->dwExStyle
& WS_EX_CLIENTEDGE
)
4788 es
->style
&= ~WS_BORDER
;
4789 else if (es
->style
& WS_BORDER
)
4790 SetWindowLongW(hwnd
, GWL_STYLE
, es
->style
& ~WS_BORDER
);
4795 /*********************************************************************
4800 static void EDIT_WM_Paint(EDITSTATE
*es
, HDC hdc
)
4813 BOOL rev
= es
->bEnableState
&&
4814 ((es
->flags
& EF_FOCUSED
) ||
4815 (es
->style
& ES_NOHIDESEL
));
4816 dc
= hdc
? hdc
: BeginPaint(es
->hwndSelf
, &ps
);
4818 GetClientRect(es
->hwndSelf
, &rcClient
);
4820 /* get the background brush */
4821 brush
= EDIT_NotifyCtlColor(es
, dc
);
4823 /* paint the border and the background */
4824 IntersectClipRect(dc
, rcClient
.left
, rcClient
.top
, rcClient
.right
, rcClient
.bottom
);
4826 if(es
->style
& WS_BORDER
) {
4827 bw
= GetSystemMetrics(SM_CXBORDER
);
4828 bh
= GetSystemMetrics(SM_CYBORDER
);
4830 if(es
->style
& ES_MULTILINE
) {
4831 if(es
->style
& WS_HSCROLL
) rc
.bottom
+=bh
;
4832 if(es
->style
& WS_VSCROLL
) rc
.right
+=bw
;
4835 /* Draw the frame. Same code as in nonclient.c */
4836 old_brush
= SelectObject(dc
, GetSysColorBrush(COLOR_WINDOWFRAME
));
4837 PatBlt(dc
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, bh
, PATCOPY
);
4838 PatBlt(dc
, rc
.left
, rc
.top
, bw
, rc
.bottom
- rc
.top
, PATCOPY
);
4839 PatBlt(dc
, rc
.left
, rc
.bottom
- 1, rc
.right
- rc
.left
, -bw
, PATCOPY
);
4840 PatBlt(dc
, rc
.right
- 1, rc
.top
, -bw
, rc
.bottom
- rc
.top
, PATCOPY
);
4841 SelectObject(dc
, old_brush
);
4843 /* Keep the border clean */
4844 IntersectClipRect(dc
, rc
.left
+bw
, rc
.top
+bh
,
4845 max(rc
.right
-bw
, rc
.left
+bw
), max(rc
.bottom
-bh
, rc
.top
+bh
));
4848 GetClipBox(dc
, &rc
);
4849 FillRect(dc
, &rc
, brush
);
4851 IntersectClipRect(dc
, es
->format_rect
.left
,
4852 es
->format_rect
.top
,
4853 es
->format_rect
.right
,
4854 es
->format_rect
.bottom
);
4855 if (es
->style
& ES_MULTILINE
) {
4857 IntersectClipRect(dc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
4860 old_font
= SelectObject(dc
, es
->font
);
4862 if (!es
->bEnableState
)
4863 SetTextColor(dc
, GetSysColor(COLOR_GRAYTEXT
));
4864 GetClipBox(dc
, &rcRgn
);
4865 if (es
->style
& ES_MULTILINE
) {
4866 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
4867 for (i
= es
->y_offset
; i
<= min(es
->y_offset
+ vlc
, es
->y_offset
+ es
->line_count
- 1) ; i
++) {
4868 EDIT_GetLineRect(es
, i
, 0, -1, &rcLine
);
4869 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
4870 EDIT_PaintLine(es
, dc
, i
, rev
);
4873 EDIT_GetLineRect(es
, 0, 0, -1, &rcLine
);
4874 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
4875 EDIT_PaintLine(es
, dc
, 0, rev
);
4878 SelectObject(dc
, old_font
);
4881 EndPaint(es
->hwndSelf
, &ps
);
4885 /*********************************************************************
4890 static void EDIT_WM_Paste(EDITSTATE
*es
)
4895 /* Protect read-only edit control from modification */
4896 if(es
->style
& ES_READONLY
)
4899 OpenClipboard(es
->hwndSelf
);
4900 if ((hsrc
= GetClipboardData(CF_UNICODETEXT
))) {
4901 src
= (LPWSTR
)GlobalLock(hsrc
);
4902 EDIT_EM_ReplaceSel(es
, TRUE
, src
, TRUE
, TRUE
);
4909 /*********************************************************************
4914 static void EDIT_WM_SetFocus(EDITSTATE
*es
)
4916 es
->flags
|= EF_FOCUSED
;
4918 if (!(es
->style
& ES_NOHIDESEL
))
4919 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
4921 /* single line edit updates itself */
4922 if (!(es
->style
& ES_MULTILINE
))
4924 HDC hdc
= GetDC(es
->hwndSelf
);
4925 EDIT_WM_Paint(es
, hdc
);
4926 ReleaseDC(es
->hwndSelf
, hdc
);
4929 CreateCaret(es
->hwndSelf
, 0, 2, es
->line_height
);
4930 EDIT_SetCaretPos(es
, es
->selection_end
,
4931 es
->flags
& EF_AFTER_WRAP
);
4932 ShowCaret(es
->hwndSelf
);
4933 EDIT_NOTIFY_PARENT(es
, EN_SETFOCUS
);
4937 /*********************************************************************
4941 * With Win95 look the margins are set to default font value unless
4942 * the system font (font == 0) is being set, in which case they are left
4946 static void EDIT_WM_SetFont(EDITSTATE
*es
, HFONT font
, BOOL redraw
)
4954 dc
= GetDC(es
->hwndSelf
);
4956 old_font
= SelectObject(dc
, font
);
4957 GetTextMetricsW(dc
, &tm
);
4958 es
->line_height
= tm
.tmHeight
;
4959 es
->char_width
= tm
.tmAveCharWidth
;
4961 SelectObject(dc
, old_font
);
4962 ReleaseDC(es
->hwndSelf
, dc
);
4964 /* Reset the format rect and the margins */
4965 GetClientRect(es
->hwndSelf
, &clientRect
);
4966 EDIT_SetRectNP(es
, &clientRect
);
4967 EDIT_EM_SetMargins(es
, EC_LEFTMARGIN
| EC_RIGHTMARGIN
,
4968 EC_USEFONTINFO
, EC_USEFONTINFO
, FALSE
);
4970 if (es
->style
& ES_MULTILINE
)
4971 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
4973 EDIT_CalcLineWidth_SL(es
);
4976 EDIT_UpdateText(es
, NULL
, TRUE
);
4977 if (es
->flags
& EF_FOCUSED
) {
4979 CreateCaret(es
->hwndSelf
, 0, 2, es
->line_height
);
4980 EDIT_SetCaretPos(es
, es
->selection_end
,
4981 es
->flags
& EF_AFTER_WRAP
);
4982 ShowCaret(es
->hwndSelf
);
4987 /*********************************************************************
4992 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4993 * The modified flag is reset. No notifications are sent.
4995 * For single-line controls, reception of WM_SETTEXT triggers:
4996 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4999 static void EDIT_WM_SetText(EDITSTATE
*es
, LPCWSTR text
, BOOL unicode
)
5001 if (!unicode
&& text
)
5003 LPCSTR textA
= (LPCSTR
)text
;
5004 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
5005 LPWSTR textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
));
5007 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
5011 if (es
->flags
& EF_UPDATE
)
5012 /* fixed this bug once; complain if we see it about to happen again. */
5013 ERR("SetSel may generate UPDATE message whose handler may reset "
5016 EDIT_EM_SetSel(es
, 0, (UINT
)-1, FALSE
);
5019 TRACE("%s\n", debugstr_w(text
));
5020 EDIT_EM_ReplaceSel(es
, FALSE
, text
, FALSE
, FALSE
);
5022 HeapFree(GetProcessHeap(), 0, (LPWSTR
)text
);
5026 static const WCHAR empty_stringW
[] = {0};
5028 EDIT_EM_ReplaceSel(es
, FALSE
, empty_stringW
, FALSE
, FALSE
);
5031 es
->flags
&= ~EF_MODIFIED
;
5032 EDIT_EM_SetSel(es
, 0, 0, FALSE
);
5034 /* Send the notification after the selection start and end have been set
5035 * edit control doesn't send notification on WM_SETTEXT
5036 * if it is multiline, or it is part of combobox
5038 if( !((es
->style
& ES_MULTILINE
) || es
->hwndListBox
))
5040 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
5041 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
5043 EDIT_EM_ScrollCaret(es
);
5044 EDIT_UpdateScrollInfo(es
);
5048 /*********************************************************************
5053 static void EDIT_WM_Size(EDITSTATE
*es
, UINT action
, INT width
, INT height
)
5055 if ((action
== SIZE_MAXIMIZED
) || (action
== SIZE_RESTORED
)) {
5057 TRACE("width = %d, height = %d\n", width
, height
);
5058 SetRect(&rc
, 0, 0, width
, height
);
5059 EDIT_SetRectNP(es
, &rc
);
5060 EDIT_UpdateText(es
, NULL
, TRUE
);
5065 /*********************************************************************
5069 * This message is sent by SetWindowLong on having changed either the Style
5070 * or the extended style.
5072 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5074 * See also EDIT_WM_NCCreate
5076 * It appears that the Windows version of the edit control allows the style
5077 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5078 * style variable which will generally be different. In this function we
5079 * update the internal style based on what changed in the externally visible
5082 * Much of this content as based upon the MSDN, especially:
5083 * Platform SDK Documentation -> User Interface Services ->
5084 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5085 * Edit Control Styles
5087 static LRESULT
EDIT_WM_StyleChanged ( EDITSTATE
*es
, WPARAM which
, const STYLESTRUCT
*style
)
5089 if (GWL_STYLE
== which
) {
5090 DWORD style_change_mask
;
5092 /* Only a subset of changes can be applied after the control
5095 style_change_mask
= ES_UPPERCASE
| ES_LOWERCASE
|
5097 if (es
->style
& ES_MULTILINE
)
5098 style_change_mask
|= ES_WANTRETURN
;
5100 new_style
= style
->styleNew
& style_change_mask
;
5102 /* Number overrides lowercase overrides uppercase (at least it
5103 * does in Win95). However I'll bet that ES_NUMBER would be
5104 * invalid under Win 3.1.
5106 if (new_style
& ES_NUMBER
) {
5107 ; /* do not override the ES_NUMBER */
5108 } else if (new_style
& ES_LOWERCASE
) {
5109 new_style
&= ~ES_UPPERCASE
;
5112 es
->style
= (es
->style
& ~style_change_mask
) | new_style
;
5113 } else if (GWL_EXSTYLE
== which
) {
5114 ; /* FIXME - what is needed here */
5116 WARN ("Invalid style change %d\n",which
);
5122 /*********************************************************************
5127 static LRESULT
EDIT_WM_SysKeyDown(EDITSTATE
*es
, INT key
, DWORD key_data
)
5129 if ((key
== VK_BACK
) && (key_data
& 0x2000)) {
5130 if (EDIT_EM_CanUndo(es
))
5133 } else if (key
== VK_UP
|| key
== VK_DOWN
) {
5134 if (EDIT_CheckCombo(es
, WM_SYSKEYDOWN
, key
))
5137 return DefWindowProcW(es
->hwndSelf
, WM_SYSKEYDOWN
, (WPARAM
)key
, (LPARAM
)key_data
);
5141 /*********************************************************************
5146 static void EDIT_WM_Timer(EDITSTATE
*es
)
5148 if (es
->region_posx
< 0) {
5149 EDIT_MoveBackward(es
, TRUE
);
5150 } else if (es
->region_posx
> 0) {
5151 EDIT_MoveForward(es
, TRUE
);
5154 * FIXME: gotta do some vertical scrolling here, like
5155 * EDIT_EM_LineScroll(hwnd, 0, 1);
5159 /*********************************************************************
5164 static LRESULT
EDIT_WM_VScroll(EDITSTATE
*es
, INT action
, INT pos
)
5168 if (!(es
->style
& ES_MULTILINE
))
5171 if (!(es
->style
& ES_AUTOVSCROLL
))
5180 TRACE("action %d (%s)\n", action
, (action
== SB_LINEUP
? "SB_LINEUP" :
5181 (action
== SB_LINEDOWN
? "SB_LINEDOWN" :
5182 (action
== SB_PAGEUP
? "SB_PAGEUP" :
5184 EDIT_EM_Scroll(es
, action
);
5191 TRACE("SB_BOTTOM\n");
5192 dy
= es
->line_count
- 1 - es
->y_offset
;
5195 TRACE("SB_THUMBTRACK %d\n", pos
);
5196 es
->flags
|= EF_VSCROLL_TRACK
;
5197 if(es
->style
& WS_VSCROLL
)
5198 dy
= pos
- es
->y_offset
;
5201 /* Assume default scroll range 0-100 */
5204 if(pos
< 0 || pos
> 100) return 0;
5205 vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
5206 new_y
= pos
* (es
->line_count
- vlc
) / 100;
5207 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
5208 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5209 es
->line_count
, es
->y_offset
, pos
, dy
);
5212 case SB_THUMBPOSITION
:
5213 TRACE("SB_THUMBPOSITION %d\n", pos
);
5214 es
->flags
&= ~EF_VSCROLL_TRACK
;
5215 if(es
->style
& WS_VSCROLL
)
5216 dy
= pos
- es
->y_offset
;
5219 /* Assume default scroll range 0-100 */
5222 if(pos
< 0 || pos
> 100) return 0;
5223 vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
5224 new_y
= pos
* (es
->line_count
- vlc
) / 100;
5225 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
5226 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5227 es
->line_count
, es
->y_offset
, pos
, dy
);
5231 /* force scroll info update */
5232 EDIT_UpdateScrollInfo(es
);
5233 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
5237 TRACE("SB_ENDSCROLL\n");
5240 * FIXME : the next two are undocumented !
5241 * Are we doing the right thing ?
5242 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5243 * although it's also a regular control message.
5245 case EM_GETTHUMB
: /* this one is used by NT notepad */
5251 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_VSCROLL
)
5252 ret
= GetScrollPos(es
->hwndSelf
, SB_VERT
);
5255 /* Assume default scroll range 0-100 */
5256 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
5257 ret
= es
->line_count
? es
->y_offset
* 100 / (es
->line_count
- vlc
) : 0;
5259 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
5263 case EM_LINESCROLL16
:
5264 TRACE("EM_LINESCROLL16 %d\n", pos
);
5270 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5275 EDIT_EM_LineScroll(es
, 0, dy
);
5279 /*********************************************************************
5284 static void EDIT_UpdateTextRegion(EDITSTATE
*es
, HRGN hrgn
, BOOL bErase
)
5286 if (es
->flags
& EF_UPDATE
) {
5287 es
->flags
&= ~EF_UPDATE
;
5288 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
5290 InvalidateRgn(es
->hwndSelf
, hrgn
, bErase
);
5294 /*********************************************************************
5299 static void EDIT_UpdateText(EDITSTATE
*es
, LPRECT rc
, BOOL bErase
)
5301 if (es
->flags
& EF_UPDATE
) {
5302 es
->flags
&= ~EF_UPDATE
;
5303 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
5305 InvalidateRect(es
->hwndSelf
, rc
, bErase
);