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
27 * -!ES_AUTOVSCROLL (every multi line control *is* auto vscroll)
28 * -!ES_AUTOHSCROLL (every single line control *is* auto hscroll)
30 * When there is no autoscrolling, the control should first check whether
31 * the new text would fit. If not, an EN_MAXTEXT should be sent.
32 * However, currently this would require the actual change to be made,
33 * then call EDIT_BuildLineDefs() and then find out that the new text doesn't
34 * fit. After all this, things should be put back in the state before the
35 * changes. Note that for multi line controls !ES_AUTOHSCROLL works : wordwrap.
50 #include "wine/winbase16.h"
51 #include "wine/winuser16.h"
52 #include "wine/unicode.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(edit
);
60 WINE_DECLARE_DEBUG_CHANNEL(combo
);
61 WINE_DECLARE_DEBUG_CHANNEL(relay
);
63 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
64 FIXME: BTW, new specs say 65535 (do you dare ???) */
65 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
66 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
67 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
68 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
71 * extra flags for EDITSTATE.flags field
73 #define EF_MODIFIED 0x0001 /* text has been modified */
74 #define EF_FOCUSED 0x0002 /* we have input focus */
75 #define EF_UPDATE 0x0004 /* notify parent of changed state */
76 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
77 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
79 wrapped line, instead of in front of the next character */
80 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
84 END_0
= 0, /* line ends with terminating '\0' character */
85 END_WRAP
, /* line is wrapped */
86 END_HARD
, /* line ends with a hard return '\r\n' */
87 END_SOFT
, /* line ends with a soft return '\r\r\n' */
88 END_RICH
/* line ends with a single '\n' */
91 typedef struct tagLINEDEF
{
92 INT length
; /* bruto length of a line in bytes */
93 INT net_length
; /* netto length of a line in visible characters */
95 INT width
; /* width of the line in pixels */
96 INT index
; /* line index into the buffer */
97 struct tagLINEDEF
*next
;
102 BOOL is_unicode
; /* how the control was created */
103 LPWSTR text
; /* the actual contents of the control */
104 UINT buffer_size
; /* the size of the buffer in characters */
105 UINT buffer_limit
; /* the maximum size to which the buffer may grow in characters */
106 HFONT font
; /* NULL means standard system font */
107 INT x_offset
; /* scroll offset for multi lines this is in pixels
108 for single lines it's in characters */
109 INT line_height
; /* height of a screen line in pixels */
110 INT char_width
; /* average character width in pixels */
111 DWORD style
; /* sane version of wnd->dwStyle */
112 WORD flags
; /* flags that are not in es->style or wnd->flags (EF_XXX) */
113 INT undo_insert_count
; /* number of characters inserted in sequence */
114 UINT undo_position
; /* character index of the insertion and deletion */
115 LPWSTR undo_text
; /* deleted text */
116 UINT undo_buffer_size
; /* size of the deleted text buffer */
117 INT selection_start
; /* == selection_end if no selection */
118 INT selection_end
; /* == current caret position */
119 WCHAR password_char
; /* == 0 if no password char, and for multi line controls */
120 INT left_margin
; /* in pixels */
121 INT right_margin
; /* in pixels */
123 INT text_width
; /* width of the widest line in pixels for multi line controls
124 and just line width for single line controls */
125 INT region_posx
; /* Position of cursor relative to region: */
126 INT region_posy
; /* -1: to left, 0: within, 1: to right */
127 EDITWORDBREAKPROC16 word_break_proc16
;
128 void *word_break_proc
; /* 32-bit word break proc: ANSI or Unicode */
129 INT line_count
; /* number of lines */
130 INT y_offset
; /* scroll offset in number of lines */
131 BOOL bCaptureState
; /* flag indicating whether mouse was captured */
132 BOOL bEnableState
; /* flag keeping the enable state */
133 HWND hwndSelf
; /* the our window handle */
134 HWND hwndParent
; /* Handle of parent for sending EN_* messages.
135 Even if parent will change, EN_* messages
136 should be sent to the first parent. */
137 HWND hwndListBox
; /* handle of ComboBox's listbox or NULL */
139 * only for multi line controls
141 INT lock_count
; /* amount of re-entries in the EditWndProc */
144 LINEDEF
*first_line_def
; /* linked list of (soft) linebreaks */
145 HLOCAL hloc32W
; /* our unicode local memory block */
146 HLOCAL16 hloc16
; /* alias for 16-bit control receiving EM_GETHANDLE16
148 HLOCAL hloc32A
; /* alias for ANSI control receiving EM_GETHANDLE
153 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
154 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
156 /* used for disabled or read-only edit control */
157 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
159 { /* Notify parent which has created this edit control */ \
160 TRACE("notification " str " sent to hwnd=%p\n", es->hwndParent); \
161 SendMessageW(es->hwndParent, WM_COMMAND, \
162 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
163 (LPARAM)(es->hwndSelf)); \
166 /*********************************************************************
173 * These functions have trivial implementations
174 * We still like to call them internally
175 * "static inline" makes them more like macro's
177 static inline BOOL
EDIT_EM_CanUndo(EDITSTATE
*es
);
178 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE
*es
);
179 static inline void EDIT_WM_Clear(EDITSTATE
*es
);
180 static inline void EDIT_WM_Cut(EDITSTATE
*es
);
183 * Helper functions only valid for one type of control
185 static void EDIT_BuildLineDefs_ML(EDITSTATE
*es
, INT iStart
, INT iEnd
, INT delta
, HRGN hrgn
);
186 static void EDIT_CalcLineWidth_SL(EDITSTATE
*es
);
187 static LPWSTR
EDIT_GetPasswordPointer_SL(EDITSTATE
*es
);
188 static void EDIT_MoveDown_ML(EDITSTATE
*es
, BOOL extend
);
189 static void EDIT_MovePageDown_ML(EDITSTATE
*es
, BOOL extend
);
190 static void EDIT_MovePageUp_ML(EDITSTATE
*es
, BOOL extend
);
191 static void EDIT_MoveUp_ML(EDITSTATE
*es
, BOOL extend
);
193 * Helper functions valid for both single line _and_ multi line controls
195 static INT
EDIT_CallWordBreakProc(EDITSTATE
*es
, INT start
, INT index
, INT count
, INT action
);
196 static INT
EDIT_CharFromPos(EDITSTATE
*es
, INT x
, INT y
, LPBOOL after_wrap
);
197 static void EDIT_ConfinePoint(EDITSTATE
*es
, LPINT x
, LPINT y
);
198 static void EDIT_GetLineRect(EDITSTATE
*es
, INT line
, INT scol
, INT ecol
, LPRECT rc
);
199 static void EDIT_InvalidateText(EDITSTATE
*es
, INT start
, INT end
);
200 static void EDIT_LockBuffer(EDITSTATE
*es
);
201 static BOOL
EDIT_MakeFit(EDITSTATE
*es
, UINT size
);
202 static BOOL
EDIT_MakeUndoFit(EDITSTATE
*es
, UINT size
);
203 static void EDIT_MoveBackward(EDITSTATE
*es
, BOOL extend
);
204 static void EDIT_MoveEnd(EDITSTATE
*es
, BOOL extend
);
205 static void EDIT_MoveForward(EDITSTATE
*es
, BOOL extend
);
206 static void EDIT_MoveHome(EDITSTATE
*es
, BOOL extend
);
207 static void EDIT_MoveWordBackward(EDITSTATE
*es
, BOOL extend
);
208 static void EDIT_MoveWordForward(EDITSTATE
*es
, BOOL extend
);
209 static void EDIT_PaintLine(EDITSTATE
*es
, HDC hdc
, INT line
, BOOL rev
);
210 static INT
EDIT_PaintText(EDITSTATE
*es
, HDC hdc
, INT x
, INT y
, INT line
, INT col
, INT count
, BOOL rev
);
211 static void EDIT_SetCaretPos(EDITSTATE
*es
, INT pos
, BOOL after_wrap
);
212 static void EDIT_SetRectNP(EDITSTATE
*es
, LPRECT lprc
);
213 static void EDIT_UnlockBuffer(EDITSTATE
*es
, BOOL force
);
214 static void EDIT_UpdateScrollInfo(EDITSTATE
*es
);
215 static INT CALLBACK
EDIT_WordBreakProc(LPWSTR s
, INT index
, INT count
, INT action
);
217 * EM_XXX message handlers
219 static LRESULT
EDIT_EM_CharFromPos(EDITSTATE
*es
, INT x
, INT y
);
220 static BOOL
EDIT_EM_FmtLines(EDITSTATE
*es
, BOOL add_eol
);
221 static HLOCAL
EDIT_EM_GetHandle(EDITSTATE
*es
);
222 static HLOCAL16
EDIT_EM_GetHandle16(EDITSTATE
*es
);
223 static INT
EDIT_EM_GetLine(EDITSTATE
*es
, INT line
, LPARAM lParam
, BOOL unicode
);
224 static LRESULT
EDIT_EM_GetSel(EDITSTATE
*es
, PUINT start
, PUINT end
);
225 static LRESULT
EDIT_EM_GetThumb(EDITSTATE
*es
);
226 static INT
EDIT_EM_LineFromChar(EDITSTATE
*es
, INT index
);
227 static INT
EDIT_EM_LineIndex(EDITSTATE
*es
, INT line
);
228 static INT
EDIT_EM_LineLength(EDITSTATE
*es
, INT index
);
229 static BOOL
EDIT_EM_LineScroll(EDITSTATE
*es
, INT dx
, INT dy
);
230 static BOOL
EDIT_EM_LineScroll_internal(EDITSTATE
*es
, INT dx
, INT dy
);
231 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
);
232 static void EDIT_EM_ReplaceSel(EDITSTATE
*es
, BOOL can_undo
, LPCWSTR lpsz_replace
, BOOL send_update
, BOOL honor_limit
);
233 static LRESULT
EDIT_EM_Scroll(EDITSTATE
*es
, INT action
);
234 static void EDIT_EM_ScrollCaret(EDITSTATE
*es
);
235 static void EDIT_EM_SetHandle(EDITSTATE
*es
, HLOCAL hloc
);
236 static void EDIT_EM_SetHandle16(EDITSTATE
*es
, HLOCAL16 hloc
);
237 static void EDIT_EM_SetLimitText(EDITSTATE
*es
, INT limit
);
238 static void EDIT_EM_SetMargins(EDITSTATE
*es
, INT action
, INT left
, INT right
);
239 static void EDIT_EM_SetPasswordChar(EDITSTATE
*es
, WCHAR c
);
240 static void EDIT_EM_SetSel(EDITSTATE
*es
, UINT start
, UINT end
, BOOL after_wrap
);
241 static BOOL
EDIT_EM_SetTabStops(EDITSTATE
*es
, INT count
, LPINT tabs
);
242 static BOOL
EDIT_EM_SetTabStops16(EDITSTATE
*es
, INT count
, LPINT16 tabs
);
243 static void EDIT_EM_SetWordBreakProc(EDITSTATE
*es
, LPARAM lParam
);
244 static void EDIT_EM_SetWordBreakProc16(EDITSTATE
*es
, EDITWORDBREAKPROC16 wbp
);
245 static BOOL
EDIT_EM_Undo(EDITSTATE
*es
);
247 * WM_XXX message handlers
249 static void EDIT_WM_Char(EDITSTATE
*es
, WCHAR c
);
250 static void EDIT_WM_Command(EDITSTATE
*es
, INT code
, INT id
, HWND conrtol
);
251 static void EDIT_WM_ContextMenu(EDITSTATE
*es
, INT x
, INT y
);
252 static void EDIT_WM_Copy(EDITSTATE
*es
);
253 static LRESULT
EDIT_WM_Create(EDITSTATE
*es
, LPCWSTR name
);
254 static LRESULT
EDIT_WM_Destroy(EDITSTATE
*es
);
255 static LRESULT
EDIT_WM_EraseBkGnd(EDITSTATE
*es
, HDC dc
);
256 static INT
EDIT_WM_GetText(EDITSTATE
*es
, INT count
, LPARAM lParam
, BOOL unicode
);
257 static LRESULT
EDIT_WM_HScroll(EDITSTATE
*es
, INT action
, INT pos
);
258 static LRESULT
EDIT_WM_KeyDown(EDITSTATE
*es
, INT key
);
259 static LRESULT
EDIT_WM_KillFocus(EDITSTATE
*es
);
260 static LRESULT
EDIT_WM_LButtonDblClk(EDITSTATE
*es
);
261 static LRESULT
EDIT_WM_LButtonDown(EDITSTATE
*es
, DWORD keys
, INT x
, INT y
);
262 static LRESULT
EDIT_WM_LButtonUp(EDITSTATE
*es
);
263 static LRESULT
EDIT_WM_MButtonDown(EDITSTATE
*es
);
264 static LRESULT
EDIT_WM_MouseMove(EDITSTATE
*es
, INT x
, INT y
);
265 static LRESULT
EDIT_WM_NCCreate(HWND hwnd
, LPCREATESTRUCTW lpcs
, BOOL unicode
);
266 static void EDIT_WM_Paint(EDITSTATE
*es
, WPARAM wParam
);
267 static void EDIT_WM_Paste(EDITSTATE
*es
);
268 static void EDIT_WM_SetFocus(EDITSTATE
*es
);
269 static void EDIT_WM_SetFont(EDITSTATE
*es
, HFONT font
, BOOL redraw
);
270 static void EDIT_WM_SetText(EDITSTATE
*es
, LPARAM lParam
, BOOL unicode
);
271 static void EDIT_WM_Size(EDITSTATE
*es
, UINT action
, INT width
, INT height
);
272 static LRESULT
EDIT_WM_StyleChanged(EDITSTATE
*es
, WPARAM which
, const STYLESTRUCT
*style
);
273 static LRESULT
EDIT_WM_SysKeyDown(EDITSTATE
*es
, INT key
, DWORD key_data
);
274 static void EDIT_WM_Timer(EDITSTATE
*es
);
275 static LRESULT
EDIT_WM_VScroll(EDITSTATE
*es
, INT action
, INT pos
);
276 static void EDIT_UpdateText(EDITSTATE
*es
, LPRECT rc
, BOOL bErase
);
277 static void EDIT_UpdateTextRegion(EDITSTATE
*es
, HRGN hrgn
, BOOL bErase
);
279 LRESULT WINAPI
EditWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
280 LRESULT WINAPI
EditWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
282 /*********************************************************************
283 * edit class descriptor
285 const struct builtin_class_descr EDIT_builtin_class
=
288 CS_DBLCLKS
| CS_PARENTDC
, /* style */
289 EditWndProcA
, /* procA */
290 EditWndProcW
, /* procW */
291 sizeof(EDITSTATE
*), /* extra */
292 IDC_IBEAM
, /* cursor */
297 /*********************************************************************
302 static inline BOOL
EDIT_EM_CanUndo(EDITSTATE
*es
)
304 return (es
->undo_insert_count
|| strlenW(es
->undo_text
));
308 /*********************************************************************
313 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE
*es
)
315 es
->undo_insert_count
= 0;
316 *es
->undo_text
= '\0';
320 /*********************************************************************
325 static inline void EDIT_WM_Clear(EDITSTATE
*es
)
327 static const WCHAR empty_stringW
[] = {0};
329 /* Protect read-only edit control from modification */
330 if(es
->style
& ES_READONLY
)
333 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
337 /*********************************************************************
342 static inline void EDIT_WM_Cut(EDITSTATE
*es
)
349 /**********************************************************************
352 * Returns the window version in case Wine emulates a later version
353 * of windows than the application expects.
355 * In a number of cases when windows runs an application that was
356 * designed for an earlier windows version, windows reverts
357 * to "old" behaviour of that earlier version.
359 * An example is a disabled edit control that needs to be painted.
360 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
361 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
362 * applications with an expected version 0f 4.0 or higher.
365 static DWORD
get_app_version(void)
367 static DWORD version
;
370 DWORD dwEmulatedVersion
;
372 DWORD dwProcVersion
= GetProcessVersion(0);
374 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOW
);
375 GetVersionExW( &info
);
376 dwEmulatedVersion
= MAKELONG( info
.dwMinorVersion
, info
.dwMajorVersion
);
377 /* FIXME: this may not be 100% correct; see discussion on the
378 * wine developer list in Nov 1999 */
379 version
= dwProcVersion
< dwEmulatedVersion
? dwProcVersion
: dwEmulatedVersion
;
385 static HBRUSH
EDIT_NotifyCtlColor(EDITSTATE
*es
, HDC hdc
)
389 if ( get_app_version() >= 0x40000 && (!es
->bEnableState
|| (es
->style
& ES_READONLY
)))
390 msg
= WM_CTLCOLORSTATIC
;
392 msg
= WM_CTLCOLOREDIT
;
394 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
395 return (HBRUSH
)SendMessageW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
398 static inline LRESULT
DefWindowProcT(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
401 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
403 return DefWindowProcA(hwnd
, msg
, wParam
, lParam
);
406 /*********************************************************************
410 * The messages are in the order of the actual integer values
411 * (which can be found in include/windows.h)
412 * Wherever possible the 16 bit versions are converted to
413 * the 32 bit ones, so that we can 'fall through' to the
414 * helper functions. These are mostly 32 bit (with a few
415 * exceptions, clearly indicated by a '16' extension to their
419 static LRESULT WINAPI
EditWndProc_common( HWND hwnd
, UINT msg
,
420 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
422 EDITSTATE
*es
= (EDITSTATE
*)GetWindowLongW( hwnd
, 0 );
425 TRACE("hwnd=%p msg=%x (%s) wparam=%x lparam=%lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), wParam
, lParam
);
427 if (!es
&& msg
!= WM_NCCREATE
)
428 return DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
429 else if (msg
== WM_NCCREATE
)
430 return EDIT_WM_NCCreate(hwnd
, (LPCREATESTRUCTW
)lParam
, unicode
);
431 else if (msg
== WM_DESTROY
)
432 return EDIT_WM_Destroy(es
);
435 if (es
) EDIT_LockBuffer(es
);
443 result
= EDIT_EM_GetSel(es
, (PUINT
)wParam
, (PUINT
)lParam
);
447 if ((short)LOWORD(lParam
) == -1)
448 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
450 EDIT_EM_SetSel(es
, LOWORD(lParam
), HIWORD(lParam
), FALSE
);
452 EDIT_EM_ScrollCaret(es
);
456 EDIT_EM_SetSel(es
, wParam
, lParam
, FALSE
);
457 EDIT_EM_ScrollCaret(es
);
464 RECT16
*r16
= MapSL(lParam
);
465 r16
->left
= es
->format_rect
.left
;
466 r16
->top
= es
->format_rect
.top
;
467 r16
->right
= es
->format_rect
.right
;
468 r16
->bottom
= es
->format_rect
.bottom
;
473 CopyRect((LPRECT
)lParam
, &es
->format_rect
);
477 if ((es
->style
& ES_MULTILINE
) && lParam
) {
479 RECT16
*r16
= MapSL(lParam
);
482 rc
.right
= r16
->right
;
483 rc
.bottom
= r16
->bottom
;
484 EDIT_SetRectNP(es
, &rc
);
485 EDIT_UpdateText(es
, NULL
, TRUE
);
489 if ((es
->style
& ES_MULTILINE
) && lParam
) {
490 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
491 EDIT_UpdateText(es
, NULL
, TRUE
);
496 if ((es
->style
& ES_MULTILINE
) && lParam
) {
498 RECT16
*r16
= MapSL(lParam
);
501 rc
.right
= r16
->right
;
502 rc
.bottom
= r16
->bottom
;
503 EDIT_SetRectNP(es
, &rc
);
507 if ((es
->style
& ES_MULTILINE
) && lParam
)
508 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
513 result
= EDIT_EM_Scroll(es
, (INT
)wParam
);
516 case EM_LINESCROLL16
:
517 wParam
= (WPARAM
)(INT
)(SHORT
)HIWORD(lParam
);
518 lParam
= (LPARAM
)(INT
)(SHORT
)LOWORD(lParam
);
521 result
= (LRESULT
)EDIT_EM_LineScroll(es
, (INT
)wParam
, (INT
)lParam
);
524 case EM_SCROLLCARET16
:
526 EDIT_EM_ScrollCaret(es
);
532 result
= ((es
->flags
& EF_MODIFIED
) != 0);
538 es
->flags
|= EF_MODIFIED
;
540 es
->flags
&= ~(EF_MODIFIED
| EF_UPDATE
); /* reset pending updates */
543 case EM_GETLINECOUNT16
:
544 case EM_GETLINECOUNT
:
545 result
= (es
->style
& ES_MULTILINE
) ? es
->line_count
: 1;
549 if ((INT16
)wParam
== -1)
553 result
= (LRESULT
)EDIT_EM_LineIndex(es
, (INT
)wParam
);
557 EDIT_EM_SetHandle16(es
, (HLOCAL16
)wParam
);
560 EDIT_EM_SetHandle(es
, (HLOCAL
)wParam
);
564 result
= (LRESULT
)EDIT_EM_GetHandle16(es
);
567 result
= (LRESULT
)EDIT_EM_GetHandle(es
);
572 result
= EDIT_EM_GetThumb(es
);
575 /* these messages missing from specs */
584 FIXME("undocumented message 0x%x, please report\n", msg
);
585 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
588 case EM_LINELENGTH16
:
590 result
= (LRESULT
)EDIT_EM_LineLength(es
, (INT
)wParam
);
593 case EM_REPLACESEL16
:
594 lParam
= (LPARAM
)MapSL(lParam
);
595 unicode
= FALSE
; /* 16-bit message is always ascii */
602 textW
= (LPWSTR
)lParam
;
605 LPSTR textA
= (LPSTR
)lParam
;
606 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
607 if((textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
))))
608 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
611 EDIT_EM_ReplaceSel(es
, (BOOL
)wParam
, textW
, TRUE
, TRUE
);
615 HeapFree(GetProcessHeap(), 0, textW
);
620 lParam
= (LPARAM
)MapSL(lParam
);
621 unicode
= FALSE
; /* 16-bit message is always ascii */
624 result
= (LRESULT
)EDIT_EM_GetLine(es
, (INT
)wParam
, lParam
, unicode
);
628 case EM_SETLIMITTEXT
:
629 EDIT_EM_SetLimitText(es
, (INT
)wParam
);
634 result
= (LRESULT
)EDIT_EM_CanUndo(es
);
640 result
= (LRESULT
)EDIT_EM_Undo(es
);
645 result
= (LRESULT
)EDIT_EM_FmtLines(es
, (BOOL
)wParam
);
648 case EM_LINEFROMCHAR16
:
649 case EM_LINEFROMCHAR
:
650 result
= (LRESULT
)EDIT_EM_LineFromChar(es
, (INT
)wParam
);
653 case EM_SETTABSTOPS16
:
654 result
= (LRESULT
)EDIT_EM_SetTabStops16(es
, (INT
)wParam
, MapSL(lParam
));
657 result
= (LRESULT
)EDIT_EM_SetTabStops(es
, (INT
)wParam
, (LPINT
)lParam
);
660 case EM_SETPASSWORDCHAR16
:
661 unicode
= FALSE
; /* 16-bit message is always ascii */
663 case EM_SETPASSWORDCHAR
:
668 charW
= (WCHAR
)wParam
;
672 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
675 EDIT_EM_SetPasswordChar(es
, charW
);
679 case EM_EMPTYUNDOBUFFER16
:
680 case EM_EMPTYUNDOBUFFER
:
681 EDIT_EM_EmptyUndoBuffer(es
);
684 case EM_GETFIRSTVISIBLELINE16
:
685 result
= es
->y_offset
;
687 case EM_GETFIRSTVISIBLELINE
:
688 result
= (es
->style
& ES_MULTILINE
) ? es
->y_offset
: es
->x_offset
;
691 case EM_SETREADONLY16
:
694 SetWindowLongW( hwnd
, GWL_STYLE
,
695 GetWindowLongW( hwnd
, GWL_STYLE
) | ES_READONLY
);
696 es
->style
|= ES_READONLY
;
698 SetWindowLongW( hwnd
, GWL_STYLE
,
699 GetWindowLongW( hwnd
, GWL_STYLE
) & ~ES_READONLY
);
700 es
->style
&= ~ES_READONLY
;
705 case EM_SETWORDBREAKPROC16
:
706 EDIT_EM_SetWordBreakProc16(es
, (EDITWORDBREAKPROC16
)lParam
);
708 case EM_SETWORDBREAKPROC
:
709 EDIT_EM_SetWordBreakProc(es
, lParam
);
712 case EM_GETWORDBREAKPROC16
:
713 result
= (LRESULT
)es
->word_break_proc16
;
715 case EM_GETWORDBREAKPROC
:
716 result
= (LRESULT
)es
->word_break_proc
;
719 case EM_GETPASSWORDCHAR16
:
720 unicode
= FALSE
; /* 16-bit message is always ascii */
722 case EM_GETPASSWORDCHAR
:
725 result
= es
->password_char
;
728 WCHAR charW
= es
->password_char
;
730 WideCharToMultiByte(CP_ACP
, 0, &charW
, 1, &charA
, 1, NULL
, NULL
);
736 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
739 EDIT_EM_SetMargins(es
, (INT
)wParam
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
743 result
= MAKELONG(es
->left_margin
, es
->right_margin
);
746 case EM_GETLIMITTEXT
:
747 result
= es
->buffer_limit
;
751 result
= EDIT_EM_PosFromChar(es
, (INT
)wParam
, FALSE
);
755 result
= EDIT_EM_CharFromPos(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
758 /* End of the EM_ messages which were in numerical order; what order
759 * are these in? vaguely alphabetical?
763 result
= DLGC_HASSETSEL
| DLGC_WANTCHARS
| DLGC_WANTARROWS
;
765 if (lParam
&& (((LPMSG
)lParam
)->message
== WM_KEYDOWN
))
767 int vk
= (int)((LPMSG
)lParam
)->wParam
;
769 if (vk
== VK_RETURN
&& (GetWindowLongW( hwnd
, GWL_STYLE
) & ES_WANTRETURN
))
771 result
|= DLGC_WANTMESSAGE
;
773 else if (es
->hwndListBox
&& (vk
== VK_RETURN
|| vk
== VK_ESCAPE
))
775 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
776 result
|= DLGC_WANTMESSAGE
;
787 strng
[0] = wParam
>> 8;
788 strng
[1] = wParam
& 0xff;
789 if (strng
[0]) MultiByteToWideChar(CP_ACP
, 0, strng
, 2, &charW
, 1);
790 else MultiByteToWideChar(CP_ACP
, 0, &strng
[1], 1, &charW
, 1);
791 EDIT_WM_Char(es
, charW
);
804 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
807 if ((charW
== VK_RETURN
|| charW
== VK_ESCAPE
) && es
->hwndListBox
)
809 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
810 SendMessageW(GetParent(hwnd
), WM_KEYDOWN
, charW
, 0);
813 EDIT_WM_Char(es
, charW
);
822 EDIT_WM_Command(es
, HIWORD(wParam
), LOWORD(wParam
), (HWND
)lParam
);
826 EDIT_WM_ContextMenu(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
835 result
= EDIT_WM_Create(es
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
838 LPCSTR nameA
= ((LPCREATESTRUCTA
)lParam
)->lpszName
;
842 INT countW
= MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, NULL
, 0);
843 if((nameW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
))))
844 MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, nameW
, countW
);
846 result
= EDIT_WM_Create(es
, nameW
);
848 HeapFree(GetProcessHeap(), 0, nameW
);
857 es
->bEnableState
= (BOOL
) wParam
;
858 EDIT_UpdateText(es
, NULL
, TRUE
);
862 result
= EDIT_WM_EraseBkGnd(es
, (HDC
)wParam
);
866 result
= (LRESULT
)es
->font
;
870 result
= (LRESULT
)EDIT_WM_GetText(es
, (INT
)wParam
, lParam
, unicode
);
873 case WM_GETTEXTLENGTH
:
874 if (unicode
) result
= strlenW(es
->text
);
875 else result
= WideCharToMultiByte( CP_ACP
, 0, es
->text
, strlenW(es
->text
),
876 NULL
, 0, NULL
, NULL
);
880 result
= EDIT_WM_HScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
884 result
= EDIT_WM_KeyDown(es
, (INT
)wParam
);
888 result
= EDIT_WM_KillFocus(es
);
891 case WM_LBUTTONDBLCLK
:
892 result
= EDIT_WM_LButtonDblClk(es
);
896 result
= EDIT_WM_LButtonDown(es
, wParam
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
900 result
= EDIT_WM_LButtonUp(es
);
904 result
= EDIT_WM_MButtonDown(es
);
907 case WM_MOUSEACTIVATE
:
908 result
= MA_ACTIVATE
;
912 result
= EDIT_WM_MouseMove(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
916 EDIT_WM_Paint(es
, wParam
);
924 EDIT_WM_SetFocus(es
);
928 EDIT_WM_SetFont(es
, (HFONT
)wParam
, LOWORD(lParam
) != 0);
932 /* FIXME: actually set an internal flag and behave accordingly */
936 EDIT_WM_SetText(es
, lParam
, unicode
);
941 EDIT_WM_Size(es
, (UINT
)wParam
, LOWORD(lParam
), HIWORD(lParam
));
944 case WM_STYLECHANGED
:
945 result
= EDIT_WM_StyleChanged(es
, wParam
, (const STYLESTRUCT
*)lParam
);
948 case WM_STYLECHANGING
:
949 result
= 0; /* See EDIT_WM_StyleChanged */
953 result
= EDIT_WM_SysKeyDown(es
, (INT
)wParam
, (DWORD
)lParam
);
961 result
= EDIT_WM_VScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
966 int gcWheelDelta
= 0;
967 UINT pulScrollLines
= 3;
968 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
970 if (wParam
& (MK_SHIFT
| MK_CONTROL
)) {
971 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
974 gcWheelDelta
-= GET_WHEEL_DELTA_WPARAM(wParam
);
975 if (abs(gcWheelDelta
) >= WHEEL_DELTA
&& pulScrollLines
)
977 int cLineScroll
= (int) min((UINT
) es
->line_count
, pulScrollLines
);
978 cLineScroll
*= (gcWheelDelta
/ WHEEL_DELTA
);
979 result
= EDIT_EM_LineScroll(es
, 0, cLineScroll
);
984 result
= DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
988 if (es
) EDIT_UnlockBuffer(es
, FALSE
);
990 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), result
);
995 /*********************************************************************
997 * EditWndProcW (USER32.@)
999 LRESULT WINAPI
EditWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1001 return EditWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);
1004 /*********************************************************************
1006 * EditWndProc (USER32.@)
1008 LRESULT WINAPI
EditWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1010 return EditWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
1013 /*********************************************************************
1015 * EDIT_BuildLineDefs_ML
1017 * Build linked list of text lines.
1018 * Lines can end with '\0' (last line), a character (if it is wrapped),
1019 * a soft return '\r\r\n' or a hard return '\r\n'
1022 static void EDIT_BuildLineDefs_ML(EDITSTATE
*es
, INT istart
, INT iend
, INT delta
, HRGN hrgn
)
1026 LPWSTR current_position
, cp
;
1028 LINEDEF
*current_line
;
1029 LINEDEF
*previous_line
;
1030 LINEDEF
*start_line
;
1031 INT line_index
= 0, nstart_line
= 0, nstart_index
= 0;
1032 INT line_count
= es
->line_count
;
1033 INT orig_net_length
;
1036 if (istart
== iend
&& delta
== 0)
1039 dc
= GetDC(es
->hwndSelf
);
1041 old_font
= SelectObject(dc
, es
->font
);
1043 previous_line
= NULL
;
1044 current_line
= es
->first_line_def
;
1046 /* Find starting line. istart must lie inside an existing line or
1047 * at the end of buffer */
1049 if (istart
< current_line
->index
+ current_line
->length
||
1050 current_line
->ending
== END_0
)
1053 previous_line
= current_line
;
1054 current_line
= current_line
->next
;
1056 } while (current_line
);
1058 if (!current_line
) /* Error occurred start is not inside previous buffer */
1060 FIXME(" modification occurred outside buffer\n");
1061 ReleaseDC(es
->hwndSelf
, dc
);
1065 /* Remember start of modifications in order to calculate update region */
1066 nstart_line
= line_index
;
1067 nstart_index
= current_line
->index
;
1069 /* We must start to reformat from the previous line since the modifications
1070 * may have caused the line to wrap upwards. */
1071 if (!(es
->style
& ES_AUTOHSCROLL
) && line_index
> 0)
1074 current_line
= previous_line
;
1076 start_line
= current_line
;
1078 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
1079 current_position
= es
->text
+ current_line
->index
;
1081 if (current_line
!= start_line
)
1083 if (!current_line
|| current_line
->index
+ delta
> current_position
- es
->text
)
1085 /* The buffer has been expanded, create a new line and
1086 insert it into the link list */
1087 LINEDEF
*new_line
= HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF
));
1088 new_line
->next
= previous_line
->next
;
1089 previous_line
->next
= new_line
;
1090 current_line
= new_line
;
1093 else if (current_line
->index
+ delta
< current_position
- es
->text
)
1095 /* The previous line merged with this line so we delete this extra entry */
1096 previous_line
->next
= current_line
->next
;
1097 HeapFree(GetProcessHeap(), 0, current_line
);
1098 current_line
= previous_line
->next
;
1102 else /* current_line->index + delta == current_position */
1104 if (current_position
- es
->text
> iend
)
1105 break; /* We reached end of line modifications */
1106 /* else recalulate this line */
1110 current_line
->index
= current_position
- es
->text
;
1111 orig_net_length
= current_line
->net_length
;
1113 /* Find end of line */
1114 cp
= current_position
;
1116 if (*cp
== '\n') break;
1117 if ((*cp
== '\r') && (*(cp
+ 1) == '\n'))
1122 /* Mark type of line termination */
1124 current_line
->ending
= END_0
;
1125 current_line
->net_length
= strlenW(current_position
);
1126 } else if ((cp
> current_position
) && (*(cp
- 1) == '\r')) {
1127 current_line
->ending
= END_SOFT
;
1128 current_line
->net_length
= cp
- current_position
- 1;
1129 } else if (*cp
== '\n') {
1130 current_line
->ending
= END_RICH
;
1131 current_line
->net_length
= cp
- current_position
;
1133 current_line
->ending
= END_HARD
;
1134 current_line
->net_length
= cp
- current_position
;
1137 /* Calculate line width */
1138 current_line
->width
= (INT
)LOWORD(GetTabbedTextExtentW(dc
,
1139 current_position
, current_line
->net_length
,
1140 es
->tabs_count
, es
->tabs
));
1142 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1143 if (!(es
->style
& ES_AUTOHSCROLL
)) {
1144 if (current_line
->width
> fw
) {
1149 next
= EDIT_CallWordBreakProc(es
, current_position
- es
->text
,
1150 prev
+ 1, current_line
->net_length
, WB_RIGHT
);
1151 current_line
->width
= (INT
)LOWORD(GetTabbedTextExtentW(dc
,
1152 current_position
, next
, es
->tabs_count
, es
->tabs
));
1153 } while (current_line
->width
<= fw
);
1154 if (!prev
) { /* Didn't find a line break so force a break */
1159 current_line
->width
= (INT
)LOWORD(GetTabbedTextExtentW(dc
,
1160 current_position
, next
, es
->tabs_count
, es
->tabs
));
1161 } while (current_line
->width
<= fw
);
1166 /* If the first line we are calculating, wrapped before istart, we must
1167 * adjust istart in order for this to be reflected in the update region. */
1168 if (current_line
->index
== nstart_index
&& istart
> current_line
->index
+ prev
)
1169 istart
= current_line
->index
+ prev
;
1170 /* else if we are updating the previous line before the first line we
1171 * are re-calculating and it expanded */
1172 else if (current_line
== start_line
&&
1173 current_line
->index
!= nstart_index
&& orig_net_length
< prev
)
1175 /* Line expanded due to an upwards line wrap so we must partially include
1176 * previous line in update region */
1177 nstart_line
= line_index
;
1178 nstart_index
= current_line
->index
;
1179 istart
= current_line
->index
+ orig_net_length
;
1182 current_line
->net_length
= prev
;
1183 current_line
->ending
= END_WRAP
;
1184 current_line
->width
= (INT
)LOWORD(GetTabbedTextExtentW(dc
, current_position
,
1185 current_line
->net_length
, es
->tabs_count
, es
->tabs
));
1187 else if (orig_net_length
< current_line
->net_length
&&
1188 current_line
== start_line
&&
1189 current_line
->index
!= nstart_index
) {
1190 /* The previous line expanded but it's still not as wide as the client rect */
1191 /* The expansion is due to an upwards line wrap so we must partially include
1192 it in the update region */
1193 nstart_line
= line_index
;
1194 nstart_index
= current_line
->index
;
1195 istart
= current_line
->index
+ orig_net_length
;
1200 /* Adjust length to include line termination */
1201 switch (current_line
->ending
) {
1203 current_line
->length
= current_line
->net_length
+ 3;
1206 current_line
->length
= current_line
->net_length
+ 1;
1209 current_line
->length
= current_line
->net_length
+ 2;
1213 current_line
->length
= current_line
->net_length
;
1216 es
->text_width
= max(es
->text_width
, current_line
->width
);
1217 current_position
+= current_line
->length
;
1218 previous_line
= current_line
;
1219 current_line
= current_line
->next
;
1221 } while (previous_line
->ending
!= END_0
);
1223 /* Finish adjusting line indexes by delta or remove hanging lines */
1224 if (previous_line
->ending
== END_0
)
1226 LINEDEF
*pnext
= NULL
;
1228 previous_line
->next
= NULL
;
1229 while (current_line
)
1231 pnext
= current_line
->next
;
1232 HeapFree(GetProcessHeap(), 0, current_line
);
1233 current_line
= pnext
;
1239 while (current_line
)
1241 current_line
->index
+= delta
;
1242 current_line
= current_line
->next
;
1246 /* Calculate rest of modification rectangle */
1251 * We calculate two rectangles. One for the first line which may have
1252 * an indent with respect to the format rect. The other is a format-width
1253 * rectangle that spans the rest of the lines that changed or moved.
1255 rc
.top
= es
->format_rect
.top
+ nstart_line
* es
->line_height
-
1256 (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
1257 rc
.bottom
= rc
.top
+ es
->line_height
;
1258 rc
.left
= es
->format_rect
.left
+ (INT
)LOWORD(GetTabbedTextExtentW(dc
,
1259 es
->text
+ nstart_index
, istart
- nstart_index
,
1260 es
->tabs_count
, es
->tabs
)) - es
->x_offset
; /* Adjust for horz scroll */
1261 rc
.right
= es
->format_rect
.right
;
1262 SetRectRgn(hrgn
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
1265 rc
.left
= es
->format_rect
.left
;
1266 rc
.right
= es
->format_rect
.right
;
1268 * If lines were added or removed we must re-paint the remainder of the
1269 * lines since the remaining lines were either shifted up or down.
1271 if (line_count
< es
->line_count
) /* We added lines */
1272 rc
.bottom
= es
->line_count
* es
->line_height
;
1273 else if (line_count
> es
->line_count
) /* We removed lines */
1274 rc
.bottom
= line_count
* es
->line_height
;
1276 rc
.bottom
= line_index
* es
->line_height
;
1277 rc
.bottom
-= (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
1278 tmphrgn
= CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
1279 CombineRgn(hrgn
, hrgn
, tmphrgn
, RGN_OR
);
1280 DeleteObject(tmphrgn
);
1284 SelectObject(dc
, old_font
);
1286 ReleaseDC(es
->hwndSelf
, dc
);
1289 /*********************************************************************
1291 * EDIT_CalcLineWidth_SL
1294 static void EDIT_CalcLineWidth_SL(EDITSTATE
*es
)
1296 es
->text_width
= (short)LOWORD(EDIT_EM_PosFromChar(es
, strlenW(es
->text
), FALSE
));
1299 /*********************************************************************
1301 * EDIT_CallWordBreakProc
1303 * Call appropriate WordBreakProc (internal or external).
1305 * Note: The "start" argument should always be an index referring
1306 * to es->text. The actual wordbreak proc might be
1307 * 16 bit, so we can't always pass any 32 bit LPSTR.
1308 * Hence we assume that es->text is the buffer that holds
1309 * the string under examination (we can decide this for ourselves).
1312 static INT
EDIT_CallWordBreakProc(EDITSTATE
*es
, INT start
, INT index
, INT count
, INT action
)
1314 INT ret
, iWndsLocks
;
1316 /* To avoid any deadlocks, all the locks on the window structures
1317 must be suspended before the control is passed to the application */
1318 iWndsLocks
= WIN_SuspendWndsLock();
1320 if (es
->word_break_proc16
) {
1327 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, NULL
, 0, NULL
, NULL
);
1328 hglob16
= GlobalAlloc16(GMEM_MOVEABLE
| GMEM_ZEROINIT
, countA
);
1329 segptr
= K32WOWGlobalLock16(hglob16
);
1330 WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, MapSL(segptr
), countA
, NULL
, NULL
);
1331 args
[4] = SELECTOROF(segptr
);
1332 args
[3] = OFFSETOF(segptr
);
1336 WOWCallback16Ex((DWORD
)es
->word_break_proc16
, WCB16_PASCAL
, sizeof(args
), args
, &result
);
1337 ret
= LOWORD(result
);
1338 GlobalUnlock16(hglob16
);
1339 GlobalFree16(hglob16
);
1341 else if (es
->word_break_proc
)
1345 EDITWORDBREAKPROCW wbpW
= (EDITWORDBREAKPROCW
)es
->word_break_proc
;
1347 TRACE_(relay
)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1348 es
->word_break_proc
, debugstr_wn(es
->text
+ start
, count
), index
, count
, action
);
1349 ret
= wbpW(es
->text
+ start
, index
, count
, action
);
1353 EDITWORDBREAKPROCA wbpA
= (EDITWORDBREAKPROCA
)es
->word_break_proc
;
1357 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, NULL
, 0, NULL
, NULL
);
1358 textA
= HeapAlloc(GetProcessHeap(), 0, countA
);
1359 WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, textA
, countA
, NULL
, NULL
);
1360 TRACE_(relay
)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1361 es
->word_break_proc
, debugstr_an(textA
, countA
), index
, countA
, action
);
1362 ret
= wbpA(textA
, index
, countA
, action
);
1363 HeapFree(GetProcessHeap(), 0, textA
);
1367 ret
= EDIT_WordBreakProc(es
->text
+ start
, index
, count
, action
);
1369 WIN_RestoreWndsLock(iWndsLocks
);
1374 /*********************************************************************
1378 * Beware: This is not the function called on EM_CHARFROMPOS
1379 * The position _can_ be outside the formatting / client
1381 * The return value is only the character index
1384 static INT
EDIT_CharFromPos(EDITSTATE
*es
, INT x
, INT y
, LPBOOL after_wrap
)
1390 if (es
->style
& ES_MULTILINE
) {
1391 INT line
= (y
- es
->format_rect
.top
) / es
->line_height
+ es
->y_offset
;
1393 LINEDEF
*line_def
= es
->first_line_def
;
1395 while ((line
> 0) && line_def
->next
) {
1396 line_index
+= line_def
->length
;
1397 line_def
= line_def
->next
;
1400 x
+= es
->x_offset
- es
->format_rect
.left
;
1401 if (x
>= line_def
->width
) {
1403 *after_wrap
= (line_def
->ending
== END_WRAP
);
1404 return line_index
+ line_def
->net_length
;
1408 *after_wrap
= FALSE
;
1411 dc
= GetDC(es
->hwndSelf
);
1413 old_font
= SelectObject(dc
, es
->font
);
1414 low
= line_index
+ 1;
1415 high
= line_index
+ line_def
->net_length
+ 1;
1416 while (low
< high
- 1)
1418 INT mid
= (low
+ high
) / 2;
1419 if (LOWORD(GetTabbedTextExtentW(dc
, es
->text
+ line_index
,mid
- line_index
, es
->tabs_count
, es
->tabs
)) > x
) high
= mid
;
1425 *after_wrap
= ((index
== line_index
+ line_def
->net_length
) &&
1426 (line_def
->ending
== END_WRAP
));
1431 *after_wrap
= FALSE
;
1432 x
-= es
->format_rect
.left
;
1434 return es
->x_offset
;
1435 text
= EDIT_GetPasswordPointer_SL(es
);
1436 dc
= GetDC(es
->hwndSelf
);
1438 old_font
= SelectObject(dc
, es
->font
);
1442 INT high
= es
->x_offset
;
1443 while (low
< high
- 1)
1445 INT mid
= (low
+ high
) / 2;
1446 GetTextExtentPoint32W( dc
, text
+ mid
,
1447 es
->x_offset
- mid
, &size
);
1448 if (size
.cx
> -x
) low
= mid
;
1455 INT low
= es
->x_offset
;
1456 INT high
= strlenW(es
->text
) + 1;
1457 while (low
< high
- 1)
1459 INT mid
= (low
+ high
) / 2;
1460 GetTextExtentPoint32W( dc
, text
+ es
->x_offset
,
1461 mid
- es
->x_offset
, &size
);
1462 if (size
.cx
> x
) high
= mid
;
1467 if (es
->style
& ES_PASSWORD
)
1468 HeapFree(GetProcessHeap(), 0, text
);
1471 SelectObject(dc
, old_font
);
1472 ReleaseDC(es
->hwndSelf
, dc
);
1477 /*********************************************************************
1481 * adjusts the point to be within the formatting rectangle
1482 * (so CharFromPos returns the nearest _visible_ character)
1485 static void EDIT_ConfinePoint(EDITSTATE
*es
, LPINT x
, LPINT y
)
1487 *x
= min(max(*x
, es
->format_rect
.left
), es
->format_rect
.right
- 1);
1488 *y
= min(max(*y
, es
->format_rect
.top
), es
->format_rect
.bottom
- 1);
1492 /*********************************************************************
1496 * Calculates the bounding rectangle for a line from a starting
1497 * column to an ending column.
1500 static void EDIT_GetLineRect(EDITSTATE
*es
, INT line
, INT scol
, INT ecol
, LPRECT rc
)
1502 INT line_index
= EDIT_EM_LineIndex(es
, line
);
1504 if (es
->style
& ES_MULTILINE
)
1505 rc
->top
= es
->format_rect
.top
+ (line
- es
->y_offset
) * es
->line_height
;
1507 rc
->top
= es
->format_rect
.top
;
1508 rc
->bottom
= rc
->top
+ es
->line_height
;
1509 rc
->left
= (scol
== 0) ? es
->format_rect
.left
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ scol
, TRUE
));
1510 rc
->right
= (ecol
== -1) ? es
->format_rect
.right
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ ecol
, TRUE
));
1514 /*********************************************************************
1516 * EDIT_GetPasswordPointer_SL
1518 * note: caller should free the (optionally) allocated buffer
1521 static LPWSTR
EDIT_GetPasswordPointer_SL(EDITSTATE
*es
)
1523 if (es
->style
& ES_PASSWORD
) {
1524 INT len
= strlenW(es
->text
);
1525 LPWSTR text
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1527 while(len
) text
[--len
] = es
->password_char
;
1534 /*********************************************************************
1538 * This acts as a LOCAL_Lock(), but it locks only once. This way
1539 * you can call it whenever you like, without unlocking.
1541 * Initially the edit control allocates a HLOCAL32 buffer
1542 * (32 bit linear memory handler). However, 16 bit application
1543 * might send a EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1544 * handler). From that moment on we have to keep using this 16 bit memory
1545 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1546 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1550 static void EDIT_LockBuffer(EDITSTATE
*es
)
1552 HINSTANCE16 hInstance
= GetWindowLongPtrW( es
->hwndSelf
, GWLP_HINSTANCE
);
1556 BOOL _16bit
= FALSE
;
1562 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1563 textA
= LocalLock(es
->hloc32A
);
1564 countA
= strlen(textA
) + 1;
1568 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1569 textA
= LOCAL_Lock(hInstance
, es
->hloc16
);
1570 countA
= strlen(textA
) + 1;
1575 ERR("no buffer ... please report\n");
1582 UINT countW_new
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
1583 TRACE("%d bytes translated to %d WCHARs\n", countA
, countW_new
);
1584 if(countW_new
> es
->buffer_size
+ 1)
1586 UINT alloc_size
= ROUND_TO_GROW(countW_new
* sizeof(WCHAR
));
1587 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es
->buffer_size
, countW_new
);
1588 hloc32W_new
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1591 es
->hloc32W
= hloc32W_new
;
1592 es
->buffer_size
= LocalSize(hloc32W_new
)/sizeof(WCHAR
) - 1;
1593 TRACE("Real new size %d+1 WCHARs\n", es
->buffer_size
);
1596 WARN("FAILED! Will synchronize partially\n");
1600 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1601 es
->text
= LocalLock(es
->hloc32W
);
1605 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, es
->text
, es
->buffer_size
+ 1);
1607 LOCAL_Unlock(hInstance
, es
->hloc16
);
1609 LocalUnlock(es
->hloc32A
);
1616 /*********************************************************************
1618 * EDIT_SL_InvalidateText
1620 * Called from EDIT_InvalidateText().
1621 * Does the job for single-line controls only.
1624 static void EDIT_SL_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1629 EDIT_GetLineRect(es
, 0, start
, end
, &line_rect
);
1630 if (IntersectRect(&rc
, &line_rect
, &es
->format_rect
))
1631 EDIT_UpdateText(es
, &rc
, TRUE
);
1635 /*********************************************************************
1637 * EDIT_ML_InvalidateText
1639 * Called from EDIT_InvalidateText().
1640 * Does the job for multi-line controls only.
1643 static void EDIT_ML_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1645 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1646 INT sl
= EDIT_EM_LineFromChar(es
, start
);
1647 INT el
= EDIT_EM_LineFromChar(es
, end
);
1656 if ((el
< es
->y_offset
) || (sl
> es
->y_offset
+ vlc
))
1659 sc
= start
- EDIT_EM_LineIndex(es
, sl
);
1660 ec
= end
- EDIT_EM_LineIndex(es
, el
);
1661 if (sl
< es
->y_offset
) {
1665 if (el
> es
->y_offset
+ vlc
) {
1666 el
= es
->y_offset
+ vlc
;
1667 ec
= EDIT_EM_LineLength(es
, EDIT_EM_LineIndex(es
, el
));
1669 GetClientRect(es
->hwndSelf
, &rc1
);
1670 IntersectRect(&rcWnd
, &rc1
, &es
->format_rect
);
1672 EDIT_GetLineRect(es
, sl
, sc
, ec
, &rcLine
);
1673 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1674 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1676 EDIT_GetLineRect(es
, sl
, sc
,
1677 EDIT_EM_LineLength(es
,
1678 EDIT_EM_LineIndex(es
, sl
)),
1680 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1681 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1682 for (l
= sl
+ 1 ; l
< el
; l
++) {
1683 EDIT_GetLineRect(es
, l
, 0,
1684 EDIT_EM_LineLength(es
,
1685 EDIT_EM_LineIndex(es
, l
)),
1687 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1688 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1690 EDIT_GetLineRect(es
, el
, 0, ec
, &rcLine
);
1691 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1692 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1697 /*********************************************************************
1699 * EDIT_InvalidateText
1701 * Invalidate the text from offset start upto, but not including,
1702 * offset end. Useful for (re)painting the selection.
1703 * Regions outside the linewidth are not invalidated.
1704 * end == -1 means end == TextLength.
1705 * start and end need not be ordered.
1708 static void EDIT_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1714 end
= strlenW(es
->text
);
1722 if (es
->style
& ES_MULTILINE
)
1723 EDIT_ML_InvalidateText(es
, start
, end
);
1725 EDIT_SL_InvalidateText(es
, start
, end
);
1729 /*********************************************************************
1733 * Try to fit size + 1 characters in the buffer.
1735 static BOOL
EDIT_MakeFit(EDITSTATE
*es
, UINT size
)
1739 if (size
<= es
->buffer_size
)
1742 TRACE("trying to ReAlloc to %d+1 characters\n", size
);
1744 /* Force edit to unlock it's buffer. es->text now NULL */
1745 EDIT_UnlockBuffer(es
, TRUE
);
1748 UINT alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1749 if ((hNew32W
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
))) {
1750 TRACE("Old 32 bit handle %p, new handle %p\n", es
->hloc32W
, hNew32W
);
1751 es
->hloc32W
= hNew32W
;
1752 es
->buffer_size
= LocalSize(hNew32W
)/sizeof(WCHAR
) - 1;
1756 EDIT_LockBuffer(es
);
1758 if (es
->buffer_size
< size
) {
1759 WARN("FAILED ! We now have %d+1\n", es
->buffer_size
);
1760 EDIT_NOTIFY_PARENT(es
, EN_ERRSPACE
, "EN_ERRSPACE");
1763 TRACE("We now have %d+1\n", es
->buffer_size
);
1769 /*********************************************************************
1773 * Try to fit size + 1 bytes in the undo buffer.
1776 static BOOL
EDIT_MakeUndoFit(EDITSTATE
*es
, UINT size
)
1780 if (size
<= es
->undo_buffer_size
)
1783 TRACE("trying to ReAlloc to %d+1\n", size
);
1785 alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1786 if ((es
->undo_text
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, es
->undo_text
, alloc_size
))) {
1787 es
->undo_buffer_size
= alloc_size
/sizeof(WCHAR
) - 1;
1792 WARN("FAILED ! We now have %d+1\n", es
->undo_buffer_size
);
1798 /*********************************************************************
1803 static void EDIT_MoveBackward(EDITSTATE
*es
, BOOL extend
)
1805 INT e
= es
->selection_end
;
1809 if ((es
->style
& ES_MULTILINE
) && e
&&
1810 (es
->text
[e
- 1] == '\r') && (es
->text
[e
] == '\n')) {
1812 if (e
&& (es
->text
[e
- 1] == '\r'))
1816 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1817 EDIT_EM_ScrollCaret(es
);
1821 /*********************************************************************
1825 * Only for multi line controls
1826 * Move the caret one line down, on a column with the nearest
1827 * x coordinate on the screen (might be a different column).
1830 static void EDIT_MoveDown_ML(EDITSTATE
*es
, BOOL extend
)
1832 INT s
= es
->selection_start
;
1833 INT e
= es
->selection_end
;
1834 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
1835 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
1836 INT x
= (short)LOWORD(pos
);
1837 INT y
= (short)HIWORD(pos
);
1839 e
= EDIT_CharFromPos(es
, x
, y
+ es
->line_height
, &after_wrap
);
1842 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
1843 EDIT_EM_ScrollCaret(es
);
1847 /*********************************************************************
1852 static void EDIT_MoveEnd(EDITSTATE
*es
, BOOL extend
)
1854 BOOL after_wrap
= FALSE
;
1857 /* Pass a high value in x to make sure of receiving the end of the line */
1858 if (es
->style
& ES_MULTILINE
)
1859 e
= EDIT_CharFromPos(es
, 0x3fffffff,
1860 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), &after_wrap
);
1862 e
= strlenW(es
->text
);
1863 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, after_wrap
);
1864 EDIT_EM_ScrollCaret(es
);
1868 /*********************************************************************
1873 static void EDIT_MoveForward(EDITSTATE
*es
, BOOL extend
)
1875 INT e
= es
->selection_end
;
1879 if ((es
->style
& ES_MULTILINE
) && (es
->text
[e
- 1] == '\r')) {
1880 if (es
->text
[e
] == '\n')
1882 else if ((es
->text
[e
] == '\r') && (es
->text
[e
+ 1] == '\n'))
1886 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1887 EDIT_EM_ScrollCaret(es
);
1891 /*********************************************************************
1895 * Home key: move to beginning of line.
1898 static void EDIT_MoveHome(EDITSTATE
*es
, BOOL extend
)
1902 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1903 if (es
->style
& ES_MULTILINE
)
1904 e
= EDIT_CharFromPos(es
, -es
->x_offset
,
1905 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), NULL
);
1908 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1909 EDIT_EM_ScrollCaret(es
);
1913 /*********************************************************************
1915 * EDIT_MovePageDown_ML
1917 * Only for multi line controls
1918 * Move the caret one page down, on a column with the nearest
1919 * x coordinate on the screen (might be a different column).
1922 static void EDIT_MovePageDown_ML(EDITSTATE
*es
, BOOL extend
)
1924 INT s
= es
->selection_start
;
1925 INT e
= es
->selection_end
;
1926 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
1927 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
1928 INT x
= (short)LOWORD(pos
);
1929 INT y
= (short)HIWORD(pos
);
1931 e
= EDIT_CharFromPos(es
, x
,
1932 y
+ (es
->format_rect
.bottom
- es
->format_rect
.top
),
1936 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
1937 EDIT_EM_ScrollCaret(es
);
1941 /*********************************************************************
1943 * EDIT_MovePageUp_ML
1945 * Only for multi line controls
1946 * Move the caret one page up, on a column with the nearest
1947 * x coordinate on the screen (might be a different column).
1950 static void EDIT_MovePageUp_ML(EDITSTATE
*es
, BOOL extend
)
1952 INT s
= es
->selection_start
;
1953 INT e
= es
->selection_end
;
1954 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
1955 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
1956 INT x
= (short)LOWORD(pos
);
1957 INT y
= (short)HIWORD(pos
);
1959 e
= EDIT_CharFromPos(es
, x
,
1960 y
- (es
->format_rect
.bottom
- es
->format_rect
.top
),
1964 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
1965 EDIT_EM_ScrollCaret(es
);
1969 /*********************************************************************
1973 * Only for multi line controls
1974 * Move the caret one line up, on a column with the nearest
1975 * x coordinate on the screen (might be a different column).
1978 static void EDIT_MoveUp_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 /*********************************************************************
1997 * EDIT_MoveWordBackward
2000 static void EDIT_MoveWordBackward(EDITSTATE
*es
, BOOL extend
)
2002 INT s
= es
->selection_start
;
2003 INT e
= es
->selection_end
;
2008 l
= EDIT_EM_LineFromChar(es
, e
);
2009 ll
= EDIT_EM_LineLength(es
, e
);
2010 li
= EDIT_EM_LineIndex(es
, l
);
2013 li
= EDIT_EM_LineIndex(es
, l
- 1);
2014 e
= li
+ EDIT_EM_LineLength(es
, li
);
2017 e
= li
+ (INT
)EDIT_CallWordBreakProc(es
,
2018 li
, e
- li
, ll
, WB_LEFT
);
2022 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2023 EDIT_EM_ScrollCaret(es
);
2027 /*********************************************************************
2029 * EDIT_MoveWordForward
2032 static void EDIT_MoveWordForward(EDITSTATE
*es
, BOOL extend
)
2034 INT s
= es
->selection_start
;
2035 INT e
= es
->selection_end
;
2040 l
= EDIT_EM_LineFromChar(es
, e
);
2041 ll
= EDIT_EM_LineLength(es
, e
);
2042 li
= EDIT_EM_LineIndex(es
, l
);
2044 if ((es
->style
& ES_MULTILINE
) && (l
!= es
->line_count
- 1))
2045 e
= EDIT_EM_LineIndex(es
, l
+ 1);
2047 e
= li
+ EDIT_CallWordBreakProc(es
,
2048 li
, e
- li
+ 1, ll
, WB_RIGHT
);
2052 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2053 EDIT_EM_ScrollCaret(es
);
2057 /*********************************************************************
2062 static void EDIT_PaintLine(EDITSTATE
*es
, HDC dc
, INT line
, BOOL rev
)
2064 INT s
= es
->selection_start
;
2065 INT e
= es
->selection_end
;
2072 if (es
->style
& ES_MULTILINE
) {
2073 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
2074 if ((line
< es
->y_offset
) || (line
> es
->y_offset
+ vlc
) || (line
>= es
->line_count
))
2079 TRACE("line=%d\n", line
);
2081 pos
= EDIT_EM_PosFromChar(es
, EDIT_EM_LineIndex(es
, line
), FALSE
);
2082 x
= (short)LOWORD(pos
);
2083 y
= (short)HIWORD(pos
);
2084 li
= EDIT_EM_LineIndex(es
, line
);
2085 ll
= EDIT_EM_LineLength(es
, li
);
2086 s
= min(es
->selection_start
, es
->selection_end
);
2087 e
= max(es
->selection_start
, es
->selection_end
);
2088 s
= min(li
+ ll
, max(li
, s
));
2089 e
= min(li
+ ll
, max(li
, e
));
2090 if (rev
&& (s
!= e
) &&
2091 ((es
->flags
& EF_FOCUSED
) || (es
->style
& ES_NOHIDESEL
))) {
2092 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, s
- li
, FALSE
);
2093 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, s
- li
, e
- s
, TRUE
);
2094 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, e
- li
, li
+ ll
- e
, FALSE
);
2096 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, ll
, FALSE
);
2100 /*********************************************************************
2105 static INT
EDIT_PaintText(EDITSTATE
*es
, HDC dc
, INT x
, INT y
, INT line
, INT col
, INT count
, BOOL rev
)
2116 BkMode
= GetBkMode(dc
);
2117 BkColor
= GetBkColor(dc
);
2118 TextColor
= GetTextColor(dc
);
2120 SetBkColor(dc
, GetSysColor(COLOR_HIGHLIGHT
));
2121 SetTextColor(dc
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
2122 SetBkMode( dc
, OPAQUE
);
2124 li
= EDIT_EM_LineIndex(es
, line
);
2125 if (es
->style
& ES_MULTILINE
) {
2126 ret
= (INT
)LOWORD(TabbedTextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
,
2127 es
->tabs_count
, es
->tabs
, es
->format_rect
.left
- es
->x_offset
));
2129 LPWSTR text
= EDIT_GetPasswordPointer_SL(es
);
2130 TextOutW(dc
, x
, y
, text
+ li
+ col
, count
);
2131 GetTextExtentPoint32W(dc
, text
+ li
+ col
, count
, &size
);
2133 if (es
->style
& ES_PASSWORD
)
2134 HeapFree(GetProcessHeap(), 0, text
);
2137 SetBkColor(dc
, BkColor
);
2138 SetTextColor(dc
, TextColor
);
2139 SetBkMode( dc
, BkMode
);
2145 /*********************************************************************
2150 static void EDIT_SetCaretPos(EDITSTATE
*es
, INT pos
,
2153 LRESULT res
= EDIT_EM_PosFromChar(es
, pos
, after_wrap
);
2154 TRACE("%d - %dx%d\n", pos
, (short)LOWORD(res
), (short)HIWORD(res
));
2155 SetCaretPos((short)LOWORD(res
), (short)HIWORD(res
));
2159 /*********************************************************************
2163 * note: this is not (exactly) the handler called on EM_SETRECTNP
2164 * it is also used to set the rect of a single line control
2167 static void EDIT_SetRectNP(EDITSTATE
*es
, LPRECT rc
)
2169 CopyRect(&es
->format_rect
, rc
);
2170 if (es
->style
& WS_BORDER
) {
2171 INT bw
= GetSystemMetrics(SM_CXBORDER
) + 1;
2172 es
->format_rect
.left
+= bw
;
2173 es
->format_rect
.top
+= bw
;
2174 es
->format_rect
.right
-= bw
;
2175 es
->format_rect
.bottom
-= bw
;
2177 es
->format_rect
.left
+= es
->left_margin
;
2178 es
->format_rect
.right
-= es
->right_margin
;
2179 es
->format_rect
.right
= max(es
->format_rect
.right
, es
->format_rect
.left
+ es
->char_width
);
2180 if (es
->style
& ES_MULTILINE
)
2182 INT fw
, vlc
, max_x_offset
, max_y_offset
;
2184 vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
2185 es
->format_rect
.bottom
= es
->format_rect
.top
+ max(1, vlc
) * es
->line_height
;
2187 /* correct es->x_offset */
2188 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2189 max_x_offset
= es
->text_width
- fw
;
2190 if(max_x_offset
< 0) max_x_offset
= 0;
2191 if(es
->x_offset
> max_x_offset
)
2192 es
->x_offset
= max_x_offset
;
2194 /* correct es->y_offset */
2195 max_y_offset
= es
->line_count
- vlc
;
2196 if(max_y_offset
< 0) max_y_offset
= 0;
2197 if(es
->y_offset
> max_y_offset
)
2198 es
->y_offset
= max_y_offset
;
2200 /* force scroll info update */
2201 EDIT_UpdateScrollInfo(es
);
2204 /* Windows doesn't care to fix text placement for SL controls */
2205 es
->format_rect
.bottom
= es
->format_rect
.top
+ es
->line_height
;
2207 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
))
2208 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
2212 /*********************************************************************
2217 static void EDIT_UnlockBuffer(EDITSTATE
*es
, BOOL force
)
2219 HINSTANCE16 hInstance
= GetWindowLongPtrW( es
->hwndSelf
, GWLP_HINSTANCE
);
2221 /* Edit window might be already destroyed */
2222 if(!IsWindow(es
->hwndSelf
))
2224 WARN("edit hwnd %p already destroyed\n", es
->hwndSelf
);
2228 if (!es
->lock_count
) {
2229 ERR("lock_count == 0 ... please report\n");
2233 ERR("es->text == 0 ... please report\n");
2237 if (force
|| (es
->lock_count
== 1)) {
2240 BOOL _16bit
= FALSE
;
2242 UINT countW
= strlenW(es
->text
) + 1;
2246 UINT countA_new
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, NULL
, 0, NULL
, NULL
);
2247 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2248 TRACE("%d WCHARs translated to %d bytes\n", countW
, countA_new
);
2249 countA
= LocalSize(es
->hloc32A
);
2250 if(countA_new
> countA
)
2253 UINT alloc_size
= ROUND_TO_GROW(countA_new
);
2254 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA
, alloc_size
);
2255 hloc32A_new
= LocalReAlloc(es
->hloc32A
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
2258 es
->hloc32A
= hloc32A_new
;
2259 countA
= LocalSize(hloc32A_new
);
2260 TRACE("Real new size %d bytes\n", countA
);
2263 WARN("FAILED! Will synchronize partially\n");
2265 textA
= LocalLock(es
->hloc32A
);
2269 UINT countA_new
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, NULL
, 0, NULL
, NULL
);
2270 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2271 TRACE("%d WCHARs translated to %d bytes\n", countW
, countA_new
);
2272 countA
= LOCAL_Size(hInstance
, es
->hloc16
);
2273 if(countA_new
> countA
)
2275 HLOCAL16 hloc16_new
;
2276 UINT alloc_size
= ROUND_TO_GROW(countA_new
);
2277 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA
, alloc_size
);
2278 hloc16_new
= LOCAL_ReAlloc(hInstance
, es
->hloc16
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
2281 es
->hloc16
= hloc16_new
;
2282 countA
= LOCAL_Size(hInstance
, hloc16_new
);
2283 TRACE("Real new size %d bytes\n", countA
);
2286 WARN("FAILED! Will synchronize partially\n");
2288 textA
= LOCAL_Lock(hInstance
, es
->hloc16
);
2294 WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, textA
, countA
, NULL
, NULL
);
2296 LOCAL_Unlock(hInstance
, es
->hloc16
);
2298 LocalUnlock(es
->hloc32A
);
2301 LocalUnlock(es
->hloc32W
);
2305 ERR("no buffer ... please report\n");
2313 /*********************************************************************
2315 * EDIT_UpdateScrollInfo
2318 static void EDIT_UpdateScrollInfo(EDITSTATE
*es
)
2320 if ((es
->style
& WS_VSCROLL
) && !(es
->flags
& EF_VSCROLL_TRACK
))
2323 si
.cbSize
= sizeof(SCROLLINFO
);
2324 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
2326 si
.nMax
= es
->line_count
- 1;
2327 si
.nPage
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
2328 si
.nPos
= es
->y_offset
;
2329 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2330 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
2331 SetScrollInfo(es
->hwndSelf
, SB_VERT
, &si
, TRUE
);
2334 if ((es
->style
& WS_HSCROLL
) && !(es
->flags
& EF_HSCROLL_TRACK
))
2337 si
.cbSize
= sizeof(SCROLLINFO
);
2338 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
2340 si
.nMax
= es
->text_width
- 1;
2341 si
.nPage
= es
->format_rect
.right
- es
->format_rect
.left
;
2342 si
.nPos
= es
->x_offset
;
2343 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2344 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
2345 SetScrollInfo(es
->hwndSelf
, SB_HORZ
, &si
, TRUE
);
2349 /*********************************************************************
2351 * EDIT_WordBreakProc
2353 * Find the beginning of words.
2354 * Note: unlike the specs for a WordBreakProc, this function only
2355 * allows to be called without linebreaks between s[0] upto
2356 * s[count - 1]. Remember it is only called
2357 * internally, so we can decide this for ourselves.
2360 static INT CALLBACK
EDIT_WordBreakProc(LPWSTR s
, INT index
, INT count
, INT action
)
2364 TRACE("s=%p, index=%d, count=%d, action=%d\n", s
, index
, count
, action
);
2374 if (s
[index
] == ' ') {
2375 while (index
&& (s
[index
] == ' '))
2378 while (index
&& (s
[index
] != ' '))
2380 if (s
[index
] == ' ')
2384 while (index
&& (s
[index
] != ' '))
2386 if (s
[index
] == ' ')
2396 if (s
[index
] == ' ')
2397 while ((index
< count
) && (s
[index
] == ' ')) index
++;
2399 while (s
[index
] && (s
[index
] != ' ') && (index
< count
))
2401 while ((s
[index
] == ' ') && (index
< count
)) index
++;
2405 case WB_ISDELIMITER
:
2406 ret
= (s
[index
] == ' ');
2409 ERR("unknown action code, please report !\n");
2416 /*********************************************************************
2420 * returns line number (not index) in high-order word of result.
2421 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2422 * to Richedit, not to the edit control. Original documentation is valid.
2423 * FIXME: do the specs mean to return -1 if outside client area or
2424 * if outside formatting rectangle ???
2427 static LRESULT
EDIT_EM_CharFromPos(EDITSTATE
*es
, INT x
, INT y
)
2435 GetClientRect(es
->hwndSelf
, &rc
);
2436 if (!PtInRect(&rc
, pt
))
2439 index
= EDIT_CharFromPos(es
, x
, y
, NULL
);
2440 return MAKELONG(index
, EDIT_EM_LineFromChar(es
, index
));
2444 /*********************************************************************
2448 * Enable or disable soft breaks.
2450 * This means: insert or remove the soft linebreak character (\r\r\n).
2451 * Take care to check if the text still fits the buffer after insertion.
2452 * If not, notify with EN_ERRSPACE.
2455 static BOOL
EDIT_EM_FmtLines(EDITSTATE
*es
, BOOL add_eol
)
2457 es
->flags
&= ~EF_USE_SOFTBRK
;
2459 es
->flags
|= EF_USE_SOFTBRK
;
2460 FIXME("soft break enabled, not implemented\n");
2466 /*********************************************************************
2470 * Hopefully this won't fire back at us.
2471 * We always start with a fixed buffer in the local heap.
2472 * Despite of the documentation says that the local heap is used
2473 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2474 * buffer on the local heap.
2477 static HLOCAL
EDIT_EM_GetHandle(EDITSTATE
*es
)
2481 if (!(es
->style
& ES_MULTILINE
))
2485 hLocal
= es
->hloc32W
;
2491 UINT countA
, alloc_size
;
2492 TRACE("Allocating 32-bit ANSI alias buffer\n");
2493 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, NULL
, 0, NULL
, NULL
);
2494 alloc_size
= ROUND_TO_GROW(countA
);
2495 if(!(es
->hloc32A
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
2497 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size
);
2500 textA
= LocalLock(es
->hloc32A
);
2501 WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, countA
, NULL
, NULL
);
2502 LocalUnlock(es
->hloc32A
);
2504 hLocal
= es
->hloc32A
;
2507 TRACE("Returning %p, LocalSize() = %ld\n", hLocal
, LocalSize(hLocal
));
2512 /*********************************************************************
2516 * Hopefully this won't fire back at us.
2517 * We always start with a buffer in 32 bit linear memory.
2518 * However, with this message a 16 bit application requests
2519 * a handle of 16 bit local heap memory, where it expects to find
2521 * It's a pitty that from this moment on we have to use this
2522 * local heap, because applications may rely on the handle
2525 * In this function we'll try to switch to local heap.
2527 static HLOCAL16
EDIT_EM_GetHandle16(EDITSTATE
*es
)
2529 HINSTANCE16 hInstance
= GetWindowLongPtrW( es
->hwndSelf
, GWLP_HINSTANCE
);
2531 UINT countA
, alloc_size
;
2533 if (!(es
->style
& ES_MULTILINE
))
2539 if (!LOCAL_HeapSize(hInstance
)) {
2540 if (!LocalInit16(hInstance
, 0,
2541 GlobalSize16(hInstance
))) {
2542 ERR("could not initialize local heap\n");
2545 TRACE("local heap initialized\n");
2548 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, NULL
, 0, NULL
, NULL
);
2549 alloc_size
= ROUND_TO_GROW(countA
);
2551 TRACE("Allocating 16-bit ANSI alias buffer\n");
2552 if (!(es
->hloc16
= LOCAL_Alloc(hInstance
, LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
))) {
2553 ERR("could not allocate new 16 bit buffer\n");
2557 if (!(textA
= (LPSTR
)LOCAL_Lock(hInstance
, es
->hloc16
))) {
2558 ERR("could not lock new 16 bit buffer\n");
2559 LOCAL_Free(hInstance
, es
->hloc16
);
2564 WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, countA
, NULL
, NULL
);
2565 LOCAL_Unlock(hInstance
, es
->hloc16
);
2567 TRACE("Returning %04X, LocalSize() = %d\n", es
->hloc16
, LOCAL_Size(hInstance
, es
->hloc16
));
2572 /*********************************************************************
2577 static INT
EDIT_EM_GetLine(EDITSTATE
*es
, INT line
, LPARAM lParam
, BOOL unicode
)
2580 INT line_len
, dst_len
;
2583 if (es
->style
& ES_MULTILINE
) {
2584 if (line
>= es
->line_count
)
2588 i
= EDIT_EM_LineIndex(es
, line
);
2590 line_len
= EDIT_EM_LineLength(es
, i
);
2591 dst_len
= *(WORD
*)lParam
;
2594 LPWSTR dst
= (LPWSTR
)lParam
;
2595 if(dst_len
<= line_len
)
2597 memcpy(dst
, src
, dst_len
* sizeof(WCHAR
));
2600 else /* Append 0 if enough space */
2602 memcpy(dst
, src
, line_len
* sizeof(WCHAR
));
2609 LPSTR dst
= (LPSTR
)lParam
;
2611 ret
= WideCharToMultiByte(CP_ACP
, 0, src
, line_len
, dst
, dst_len
, NULL
, NULL
);
2612 if(!ret
) /* Insufficient buffer size */
2614 if(ret
< dst_len
) /* Append 0 if enough space */
2621 /*********************************************************************
2626 static LRESULT
EDIT_EM_GetSel(EDITSTATE
*es
, PUINT start
, PUINT end
)
2628 UINT s
= es
->selection_start
;
2629 UINT e
= es
->selection_end
;
2636 return MAKELONG(s
, e
);
2640 /*********************************************************************
2644 * FIXME: is this right ? (or should it be only VSCROLL)
2645 * (and maybe only for edit controls that really have their
2646 * own scrollbars) (and maybe only for multiline controls ?)
2647 * All in all: very poorly documented
2650 static LRESULT
EDIT_EM_GetThumb(EDITSTATE
*es
)
2652 return MAKELONG(EDIT_WM_VScroll(es
, EM_GETTHUMB16
, 0),
2653 EDIT_WM_HScroll(es
, EM_GETTHUMB16
, 0));
2657 /*********************************************************************
2662 static INT
EDIT_EM_LineFromChar(EDITSTATE
*es
, INT index
)
2667 if (!(es
->style
& ES_MULTILINE
))
2669 if (index
> (INT
)strlenW(es
->text
))
2670 return es
->line_count
- 1;
2672 index
= min(es
->selection_start
, es
->selection_end
);
2675 line_def
= es
->first_line_def
;
2676 index
-= line_def
->length
;
2677 while ((index
>= 0) && line_def
->next
) {
2679 line_def
= line_def
->next
;
2680 index
-= line_def
->length
;
2686 /*********************************************************************
2691 static INT
EDIT_EM_LineIndex(EDITSTATE
*es
, INT line
)
2696 if (!(es
->style
& ES_MULTILINE
))
2698 if (line
>= es
->line_count
)
2702 line_def
= es
->first_line_def
;
2704 INT index
= es
->selection_end
- line_def
->length
;
2705 while ((index
>= 0) && line_def
->next
) {
2706 line_index
+= line_def
->length
;
2707 line_def
= line_def
->next
;
2708 index
-= line_def
->length
;
2712 line_index
+= line_def
->length
;
2713 line_def
= line_def
->next
;
2721 /*********************************************************************
2726 static INT
EDIT_EM_LineLength(EDITSTATE
*es
, INT index
)
2730 if (!(es
->style
& ES_MULTILINE
))
2731 return strlenW(es
->text
);
2734 /* get the number of remaining non-selected chars of selected lines */
2735 INT32 l
; /* line number */
2736 INT32 li
; /* index of first char in line */
2738 l
= EDIT_EM_LineFromChar(es
, es
->selection_start
);
2739 /* # chars before start of selection area */
2740 count
= es
->selection_start
- EDIT_EM_LineIndex(es
, l
);
2741 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
2742 /* # chars after end of selection */
2743 li
= EDIT_EM_LineIndex(es
, l
);
2744 count
+= li
+ EDIT_EM_LineLength(es
, li
) - es
->selection_end
;
2747 line_def
= es
->first_line_def
;
2748 index
-= line_def
->length
;
2749 while ((index
>= 0) && line_def
->next
) {
2750 line_def
= line_def
->next
;
2751 index
-= line_def
->length
;
2753 return line_def
->net_length
;
2757 /*********************************************************************
2761 * NOTE: dx is in average character widths, dy - in lines;
2764 static BOOL
EDIT_EM_LineScroll(EDITSTATE
*es
, INT dx
, INT dy
)
2766 if (!(es
->style
& ES_MULTILINE
))
2769 dx
*= es
->char_width
;
2770 return EDIT_EM_LineScroll_internal(es
, dx
, dy
);
2773 /*********************************************************************
2775 * EDIT_EM_LineScroll_internal
2777 * Version of EDIT_EM_LineScroll for internal use.
2778 * It doesn't refuse if ES_MULTILINE is set and assumes that
2779 * dx is in pixels, dy - in lines.
2782 static BOOL
EDIT_EM_LineScroll_internal(EDITSTATE
*es
, INT dx
, INT dy
)
2785 INT x_offset_in_pixels
;
2787 if (es
->style
& ES_MULTILINE
)
2789 x_offset_in_pixels
= es
->x_offset
;
2794 x_offset_in_pixels
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->x_offset
, FALSE
));
2797 if (-dx
> x_offset_in_pixels
)
2798 dx
= -x_offset_in_pixels
;
2799 if (dx
> es
->text_width
- x_offset_in_pixels
)
2800 dx
= es
->text_width
- x_offset_in_pixels
;
2801 nyoff
= max(0, es
->y_offset
+ dy
);
2802 if (nyoff
>= es
->line_count
)
2803 nyoff
= es
->line_count
- 1;
2804 dy
= (es
->y_offset
- nyoff
) * es
->line_height
;
2809 es
->y_offset
= nyoff
;
2810 if(es
->style
& ES_MULTILINE
)
2813 es
->x_offset
+= dx
/ es
->char_width
;
2815 GetClientRect(es
->hwndSelf
, &rc1
);
2816 IntersectRect(&rc
, &rc1
, &es
->format_rect
);
2817 ScrollWindowEx(es
->hwndSelf
, -dx
, dy
,
2818 NULL
, &rc
, NULL
, NULL
, SW_INVALIDATE
);
2819 /* force scroll info update */
2820 EDIT_UpdateScrollInfo(es
);
2822 if (dx
&& !(es
->flags
& EF_HSCROLL_TRACK
))
2823 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
, "EN_HSCROLL");
2824 if (dy
&& !(es
->flags
& EF_VSCROLL_TRACK
))
2825 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
, "EN_VSCROLL");
2830 /*********************************************************************
2835 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
)
2837 INT len
= strlenW(es
->text
);
2846 index
= min(index
, len
);
2847 dc
= GetDC(es
->hwndSelf
);
2849 old_font
= SelectObject(dc
, es
->font
);
2850 if (es
->style
& ES_MULTILINE
) {
2851 l
= EDIT_EM_LineFromChar(es
, index
);
2852 y
= (l
- es
->y_offset
) * es
->line_height
;
2853 li
= EDIT_EM_LineIndex(es
, l
);
2854 if (after_wrap
&& (li
== index
) && l
) {
2856 LINEDEF
*line_def
= es
->first_line_def
;
2858 line_def
= line_def
->next
;
2861 if (line_def
->ending
== END_WRAP
) {
2863 y
-= es
->line_height
;
2864 li
= EDIT_EM_LineIndex(es
, l
);
2867 x
= LOWORD(GetTabbedTextExtentW(dc
, es
->text
+ li
, index
- li
,
2868 es
->tabs_count
, es
->tabs
)) - es
->x_offset
;
2870 LPWSTR text
= EDIT_GetPasswordPointer_SL(es
);
2871 if (index
< es
->x_offset
) {
2872 GetTextExtentPoint32W(dc
, text
+ index
,
2873 es
->x_offset
- index
, &size
);
2876 GetTextExtentPoint32W(dc
, text
+ es
->x_offset
,
2877 index
- es
->x_offset
, &size
);
2881 if (es
->style
& ES_PASSWORD
)
2882 HeapFree(GetProcessHeap(), 0, text
);
2884 x
+= es
->format_rect
.left
;
2885 y
+= es
->format_rect
.top
;
2887 SelectObject(dc
, old_font
);
2888 ReleaseDC(es
->hwndSelf
, dc
);
2889 return MAKELONG((INT16
)x
, (INT16
)y
);
2893 /*********************************************************************
2897 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2900 static void EDIT_EM_ReplaceSel(EDITSTATE
*es
, BOOL can_undo
, LPCWSTR lpsz_replace
, BOOL send_update
, BOOL honor_limit
)
2902 UINT strl
= strlenW(lpsz_replace
);
2903 UINT tl
= strlenW(es
->text
);
2912 TRACE("%s, can_undo %d, send_update %d\n",
2913 debugstr_w(lpsz_replace
), can_undo
, send_update
);
2915 s
= es
->selection_start
;
2916 e
= es
->selection_end
;
2918 if ((s
== e
) && !strl
)
2923 /* Issue the EN_MAXTEXT notification and continue with replacing text
2924 * such that buffer limit is honored. */
2925 size
= tl
- (e
- s
) + strl
;
2926 if ((honor_limit
) && (es
->buffer_limit
> 0) && (size
> es
->buffer_limit
)) {
2927 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
, "EN_MAXTEXT");
2928 strl
= es
->buffer_limit
- (tl
- (e
-s
));
2931 if (!EDIT_MakeFit(es
, tl
- (e
- s
) + strl
))
2935 /* there is something to be deleted */
2936 TRACE("deleting stuff.\n");
2938 utl
= strlenW(es
->undo_text
);
2939 if (!es
->undo_insert_count
&& (*es
->undo_text
&& (s
== es
->undo_position
))) {
2940 /* undo-buffer is extended to the right */
2941 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2942 strncpyW(es
->undo_text
+ utl
, es
->text
+ s
, e
- s
+ 1);
2943 (es
->undo_text
+ utl
)[e
- s
] = 0; /* ensure 0 termination */
2944 } else if (!es
->undo_insert_count
&& (*es
->undo_text
&& (e
== es
->undo_position
))) {
2945 /* undo-buffer is extended to the left */
2946 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2947 for (p
= es
->undo_text
+ utl
; p
>= es
->undo_text
; p
--)
2949 for (i
= 0 , p
= es
->undo_text
; i
< e
- s
; i
++)
2950 p
[i
] = (es
->text
+ s
)[i
];
2951 es
->undo_position
= s
;
2953 /* new undo-buffer */
2954 EDIT_MakeUndoFit(es
, e
- s
);
2955 strncpyW(es
->undo_text
, es
->text
+ s
, e
- s
+ 1);
2956 es
->undo_text
[e
- s
] = 0; /* ensure 0 termination */
2957 es
->undo_position
= s
;
2959 /* any deletion makes the old insertion-undo invalid */
2960 es
->undo_insert_count
= 0;
2962 EDIT_EM_EmptyUndoBuffer(es
);
2965 strcpyW(es
->text
+ s
, es
->text
+ e
);
2968 /* there is an insertion */
2970 if ((s
== es
->undo_position
) ||
2971 ((es
->undo_insert_count
) &&
2972 (s
== es
->undo_position
+ es
->undo_insert_count
)))
2974 * insertion is new and at delete position or
2975 * an extension to either left or right
2977 es
->undo_insert_count
+= strl
;
2979 /* new insertion undo */
2980 es
->undo_position
= s
;
2981 es
->undo_insert_count
= strl
;
2982 /* new insertion makes old delete-buffer invalid */
2983 *es
->undo_text
= '\0';
2986 EDIT_EM_EmptyUndoBuffer(es
);
2989 tl
= strlenW(es
->text
);
2990 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
));
2991 for (p
= es
->text
+ tl
; p
>= es
->text
+ s
; p
--)
2993 for (i
= 0 , p
= es
->text
+ s
; i
< strl
; i
++)
2994 p
[i
] = lpsz_replace
[i
];
2995 if(es
->style
& ES_UPPERCASE
)
2996 CharUpperBuffW(p
, strl
);
2997 else if(es
->style
& ES_LOWERCASE
)
2998 CharLowerBuffW(p
, strl
);
3001 if (es
->style
& ES_MULTILINE
)
3003 INT s
= min(es
->selection_start
, es
->selection_end
);
3005 hrgn
= CreateRectRgn(0, 0, 0, 0);
3006 EDIT_BuildLineDefs_ML(es
, s
, s
+ strl
,
3007 strl
- abs(es
->selection_end
- es
->selection_start
), hrgn
);
3010 EDIT_CalcLineWidth_SL(es
);
3012 EDIT_EM_SetSel(es
, s
, s
, FALSE
);
3013 es
->flags
|= EF_MODIFIED
;
3014 if (send_update
) es
->flags
|= EF_UPDATE
;
3017 EDIT_UpdateTextRegion(es
, hrgn
, TRUE
);
3021 EDIT_UpdateText(es
, NULL
, TRUE
);
3023 EDIT_EM_ScrollCaret(es
);
3025 /* force scroll info update */
3026 EDIT_UpdateScrollInfo(es
);
3029 if(es
->flags
& EF_UPDATE
)
3031 es
->flags
&= ~EF_UPDATE
;
3032 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
, "EN_CHANGE");
3037 /*********************************************************************
3042 static LRESULT
EDIT_EM_Scroll(EDITSTATE
*es
, INT action
)
3046 if (!(es
->style
& ES_MULTILINE
))
3047 return (LRESULT
)FALSE
;
3057 if (es
->y_offset
< es
->line_count
- 1)
3062 dy
= -(es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
3065 if (es
->y_offset
< es
->line_count
- 1)
3066 dy
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
3069 return (LRESULT
)FALSE
;
3072 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
3073 /* check if we are going to move too far */
3074 if(es
->y_offset
+ dy
> es
->line_count
- vlc
)
3075 dy
= es
->line_count
- vlc
- es
->y_offset
;
3077 /* Notification is done in EDIT_EM_LineScroll */
3079 EDIT_EM_LineScroll(es
, 0, dy
);
3081 return MAKELONG((INT16
)dy
, (BOOL16
)TRUE
);
3085 /*********************************************************************
3090 static void EDIT_EM_ScrollCaret(EDITSTATE
*es
)
3092 if (es
->style
& ES_MULTILINE
) {
3097 INT cw
= es
->char_width
;
3102 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
3103 li
= EDIT_EM_LineIndex(es
, l
);
3104 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
));
3105 vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
3106 if (l
>= es
->y_offset
+ vlc
)
3107 dy
= l
- vlc
+ 1 - es
->y_offset
;
3108 if (l
< es
->y_offset
)
3109 dy
= l
- es
->y_offset
;
3110 ww
= es
->format_rect
.right
- es
->format_rect
.left
;
3111 if (x
< es
->format_rect
.left
)
3112 dx
= x
- es
->format_rect
.left
- ww
/ HSCROLL_FRACTION
/ cw
* cw
;
3113 if (x
> es
->format_rect
.right
)
3114 dx
= x
- es
->format_rect
.left
- (HSCROLL_FRACTION
- 1) * ww
/ HSCROLL_FRACTION
/ cw
* cw
;
3117 /* check if we are going to move too far */
3118 if(es
->x_offset
+ dx
+ ww
> es
->text_width
)
3119 dx
= es
->text_width
- ww
- es
->x_offset
;
3121 EDIT_EM_LineScroll_internal(es
, dx
, dy
);
3128 if (!(es
->style
& ES_AUTOHSCROLL
))
3131 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
3132 format_width
= es
->format_rect
.right
- es
->format_rect
.left
;
3133 if (x
< es
->format_rect
.left
) {
3134 goal
= es
->format_rect
.left
+ format_width
/ HSCROLL_FRACTION
;
3137 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
3138 } while ((x
< goal
) && es
->x_offset
);
3139 /* FIXME: use ScrollWindow() somehow to improve performance */
3140 EDIT_UpdateText(es
, NULL
, TRUE
);
3141 } else if (x
> es
->format_rect
.right
) {
3143 INT len
= strlenW(es
->text
);
3144 goal
= es
->format_rect
.right
- format_width
/ HSCROLL_FRACTION
;
3147 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
3148 x_last
= (short)LOWORD(EDIT_EM_PosFromChar(es
, len
, FALSE
));
3149 } while ((x
> goal
) && (x_last
> es
->format_rect
.right
));
3150 /* FIXME: use ScrollWindow() somehow to improve performance */
3151 EDIT_UpdateText(es
, NULL
, TRUE
);
3155 if(es
->flags
& EF_FOCUSED
)
3156 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
3160 /*********************************************************************
3164 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3167 static void EDIT_EM_SetHandle(EDITSTATE
*es
, HLOCAL hloc
)
3169 HINSTANCE16 hInstance
= GetWindowLongPtrW( es
->hwndSelf
, GWLP_HINSTANCE
);
3171 if (!(es
->style
& ES_MULTILINE
))
3175 WARN("called with NULL handle\n");
3179 EDIT_UnlockBuffer(es
, TRUE
);
3183 LOCAL_Free(hInstance
, es
->hloc16
);
3184 es
->hloc16
= (HLOCAL16
)NULL
;
3191 LocalFree(es
->hloc32A
);
3203 countA
= LocalSize(hloc
);
3204 textA
= LocalLock(hloc
);
3205 countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
3206 if(!(hloc32W_new
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, countW
* sizeof(WCHAR
))))
3208 ERR("Could not allocate new unicode buffer\n");
3211 textW
= LocalLock(hloc32W_new
);
3212 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, textW
, countW
);
3213 LocalUnlock(hloc32W_new
);
3217 LocalFree(es
->hloc32W
);
3219 es
->hloc32W
= hloc32W_new
;
3223 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
3225 EDIT_LockBuffer(es
);
3227 es
->x_offset
= es
->y_offset
= 0;
3228 es
->selection_start
= es
->selection_end
= 0;
3229 EDIT_EM_EmptyUndoBuffer(es
);
3230 es
->flags
&= ~EF_MODIFIED
;
3231 es
->flags
&= ~EF_UPDATE
;
3232 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
3233 EDIT_UpdateText(es
, NULL
, TRUE
);
3234 EDIT_EM_ScrollCaret(es
);
3235 /* force scroll info update */
3236 EDIT_UpdateScrollInfo(es
);
3240 /*********************************************************************
3244 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3247 static void EDIT_EM_SetHandle16(EDITSTATE
*es
, HLOCAL16 hloc
)
3249 HINSTANCE16 hInstance
= GetWindowLongPtrW( es
->hwndSelf
, GWLP_HINSTANCE
);
3255 if (!(es
->style
& ES_MULTILINE
))
3259 WARN("called with NULL handle\n");
3263 EDIT_UnlockBuffer(es
, TRUE
);
3267 LocalFree(es
->hloc32A
);
3271 countA
= LOCAL_Size(hInstance
, hloc
);
3272 textA
= LOCAL_Lock(hInstance
, hloc
);
3273 countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
3274 if(!(hloc32W_new
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, countW
* sizeof(WCHAR
))))
3276 ERR("Could not allocate new unicode buffer\n");
3279 textW
= LocalLock(hloc32W_new
);
3280 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, textW
, countW
);
3281 LocalUnlock(hloc32W_new
);
3282 LOCAL_Unlock(hInstance
, hloc
);
3285 LocalFree(es
->hloc32W
);
3287 es
->hloc32W
= hloc32W_new
;
3290 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
3292 EDIT_LockBuffer(es
);
3294 es
->x_offset
= es
->y_offset
= 0;
3295 es
->selection_start
= es
->selection_end
= 0;
3296 EDIT_EM_EmptyUndoBuffer(es
);
3297 es
->flags
&= ~EF_MODIFIED
;
3298 es
->flags
&= ~EF_UPDATE
;
3299 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
3300 EDIT_UpdateText(es
, NULL
, TRUE
);
3301 EDIT_EM_ScrollCaret(es
);
3302 /* force scroll info update */
3303 EDIT_UpdateScrollInfo(es
);
3307 /*********************************************************************
3311 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3312 * However, the windows version is not complied to yet in all of edit.c
3314 * Additionally as the wrapper for RichEdit controls we need larger buffers
3315 * at present -1 will represent nolimit
3317 static void EDIT_EM_SetLimitText(EDITSTATE
*es
, INT limit
)
3319 if (limit
== 0xFFFFFFFF)
3320 es
->buffer_limit
= -1;
3321 else if (es
->style
& ES_MULTILINE
) {
3323 es
->buffer_limit
= min(limit
, BUFLIMIT_MULTI
);
3325 es
->buffer_limit
= BUFLIMIT_MULTI
;
3328 es
->buffer_limit
= min(limit
, BUFLIMIT_SINGLE
);
3330 es
->buffer_limit
= BUFLIMIT_SINGLE
;
3335 /*********************************************************************
3339 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3340 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3341 * margin according to the textmetrics of the current font.
3343 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3344 * of the char's width as the margin, but this is not how Windows handles this.
3345 * For all other fonts Windows sets the margins to zero.
3348 static void EDIT_EM_SetMargins(EDITSTATE
*es
, INT action
,
3349 INT left
, INT right
)
3352 INT default_left_margin
= 0; /* in pixels */
3353 INT default_right_margin
= 0; /* in pixels */
3355 /* Set the default margins depending on the font */
3356 if (es
->font
&& (left
== EC_USEFONTINFO
|| right
== EC_USEFONTINFO
)) {
3357 HDC dc
= GetDC(es
->hwndSelf
);
3358 HFONT old_font
= SelectObject(dc
, es
->font
);
3359 GetTextMetricsW(dc
, &tm
);
3360 /* The default margins are only non zero for TrueType or Vector fonts */
3361 if (tm
.tmPitchAndFamily
& ( TMPF_VECTOR
| TMPF_TRUETYPE
)) {
3362 /* This must be calculated more exactly! But how? */
3363 default_left_margin
= tm
.tmAveCharWidth
/ 3;
3364 default_right_margin
= tm
.tmAveCharWidth
/ 3;
3366 SelectObject(dc
, old_font
);
3367 ReleaseDC(es
->hwndSelf
, dc
);
3370 if (action
& EC_LEFTMARGIN
) {
3371 if (left
!= EC_USEFONTINFO
)
3372 es
->left_margin
= left
;
3374 es
->left_margin
= default_left_margin
;
3377 if (action
& EC_RIGHTMARGIN
) {
3378 if (right
!= EC_USEFONTINFO
)
3379 es
->right_margin
= right
;
3381 es
->right_margin
= default_right_margin
;
3383 TRACE("left=%d, right=%d\n", es
->left_margin
, es
->right_margin
);
3387 /*********************************************************************
3389 * EM_SETPASSWORDCHAR
3392 static void EDIT_EM_SetPasswordChar(EDITSTATE
*es
, WCHAR c
)
3396 if (es
->style
& ES_MULTILINE
)
3399 if (es
->password_char
== c
)
3402 style
= GetWindowLongW( es
->hwndSelf
, GWL_STYLE
);
3403 es
->password_char
= c
;
3405 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
| ES_PASSWORD
);
3406 es
->style
|= ES_PASSWORD
;
3408 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
& ~ES_PASSWORD
);
3409 es
->style
&= ~ES_PASSWORD
;
3411 EDIT_UpdateText(es
, NULL
, TRUE
);
3415 /*********************************************************************
3419 * note: unlike the specs say: the order of start and end
3420 * _is_ preserved in Windows. (i.e. start can be > end)
3421 * In other words: this handler is OK
3424 static void EDIT_EM_SetSel(EDITSTATE
*es
, UINT start
, UINT end
, BOOL after_wrap
)
3426 UINT old_start
= es
->selection_start
;
3427 UINT old_end
= es
->selection_end
;
3428 UINT len
= strlenW(es
->text
);
3430 if (start
== (UINT
)-1) {
3431 start
= es
->selection_end
;
3432 end
= es
->selection_end
;
3434 start
= min(start
, len
);
3435 end
= min(end
, len
);
3437 es
->selection_start
= start
;
3438 es
->selection_end
= end
;
3440 es
->flags
|= EF_AFTER_WRAP
;
3442 es
->flags
&= ~EF_AFTER_WRAP
;
3443 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3444 ORDER_UINT(start
, end
);
3445 ORDER_UINT(end
, old_end
);
3446 ORDER_UINT(start
, old_start
);
3447 ORDER_UINT(old_start
, old_end
);
3448 if (end
!= old_start
)
3452 * ORDER_UINT32(end, old_start);
3453 * EDIT_InvalidateText(es, start, end);
3454 * EDIT_InvalidateText(es, old_start, old_end);
3455 * in place of the following if statement.
3457 if (old_start
> end
)
3459 EDIT_InvalidateText(es
, start
, end
);
3460 EDIT_InvalidateText(es
, old_start
, old_end
);
3464 EDIT_InvalidateText(es
, start
, old_start
);
3465 EDIT_InvalidateText(es
, end
, old_end
);
3468 else EDIT_InvalidateText(es
, start
, old_end
);
3472 /*********************************************************************
3477 static BOOL
EDIT_EM_SetTabStops(EDITSTATE
*es
, INT count
, LPINT tabs
)
3479 if (!(es
->style
& ES_MULTILINE
))
3482 HeapFree(GetProcessHeap(), 0, es
->tabs
);
3483 es
->tabs_count
= count
;
3487 es
->tabs
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(INT
));
3488 memcpy(es
->tabs
, tabs
, count
* sizeof(INT
));
3494 /*********************************************************************
3499 static BOOL
EDIT_EM_SetTabStops16(EDITSTATE
*es
, INT count
, LPINT16 tabs
)
3501 if (!(es
->style
& ES_MULTILINE
))
3504 HeapFree(GetProcessHeap(), 0, es
->tabs
);
3505 es
->tabs_count
= count
;
3510 es
->tabs
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(INT
));
3511 for (i
= 0 ; i
< count
; i
++)
3512 es
->tabs
[i
] = *tabs
++;
3518 /*********************************************************************
3520 * EM_SETWORDBREAKPROC
3523 static void EDIT_EM_SetWordBreakProc(EDITSTATE
*es
, LPARAM lParam
)
3525 if (es
->word_break_proc
== (void *)lParam
)
3528 es
->word_break_proc
= (void *)lParam
;
3529 es
->word_break_proc16
= NULL
;
3531 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
)) {
3532 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
3533 EDIT_UpdateText(es
, NULL
, TRUE
);
3538 /*********************************************************************
3540 * EM_SETWORDBREAKPROC16
3543 static void EDIT_EM_SetWordBreakProc16(EDITSTATE
*es
, EDITWORDBREAKPROC16 wbp
)
3545 if (es
->word_break_proc16
== wbp
)
3548 es
->word_break_proc
= NULL
;
3549 es
->word_break_proc16
= wbp
;
3550 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
)) {
3551 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
3552 EDIT_UpdateText(es
, NULL
, TRUE
);
3557 /*********************************************************************
3562 static BOOL
EDIT_EM_Undo(EDITSTATE
*es
)
3567 /* As per MSDN spec, for a single-line edit control,
3568 the return value is always TRUE */
3569 if( es
->style
& ES_READONLY
)
3570 return !(es
->style
& ES_MULTILINE
);
3572 ulength
= strlenW(es
->undo_text
);
3574 utext
= HeapAlloc(GetProcessHeap(), 0, (ulength
+ 1) * sizeof(WCHAR
));
3576 strcpyW(utext
, es
->undo_text
);
3578 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3579 es
->undo_insert_count
, debugstr_w(utext
));
3581 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3582 EDIT_EM_EmptyUndoBuffer(es
);
3583 EDIT_EM_ReplaceSel(es
, TRUE
, utext
, TRUE
, TRUE
);
3584 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3585 /* send the notification after the selection start and end are set */
3586 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
, "EN_CHANGE");
3587 EDIT_EM_ScrollCaret(es
);
3588 HeapFree(GetProcessHeap(), 0, utext
);
3590 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3591 es
->undo_insert_count
, debugstr_w(es
->undo_text
));
3596 /*********************************************************************
3601 static void EDIT_WM_Char(EDITSTATE
*es
, WCHAR c
)
3605 /* Protect read-only edit control from modification */
3606 if(es
->style
& ES_READONLY
)
3609 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3613 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3614 if(!(es
->style
& ES_MULTILINE
) && !(es
->style
& ES_WANTRETURN
))
3617 if (es
->style
& ES_MULTILINE
) {
3618 if (es
->style
& ES_READONLY
) {
3619 EDIT_MoveHome(es
, FALSE
);
3620 EDIT_MoveDown_ML(es
, FALSE
);
3622 static const WCHAR cr_lfW
[] = {'\r','\n',0};
3623 EDIT_EM_ReplaceSel(es
, TRUE
, cr_lfW
, TRUE
, TRUE
);
3628 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_READONLY
))
3630 static const WCHAR tabW
[] = {'\t',0};
3631 EDIT_EM_ReplaceSel(es
, TRUE
, tabW
, TRUE
, TRUE
);
3635 if (!(es
->style
& ES_READONLY
) && !control
) {
3636 if (es
->selection_start
!= es
->selection_end
)
3639 /* delete character left of caret */
3640 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3641 EDIT_MoveBackward(es
, TRUE
);
3647 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3650 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3653 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3657 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3658 if( (es
->style
& ES_NUMBER
) && !( c
>= '0' && c
<= '9') )
3661 if (!(es
->style
& ES_READONLY
) && (c
>= ' ') && (c
!= 127)) {
3665 EDIT_EM_ReplaceSel(es
, TRUE
, str
, TRUE
, TRUE
);
3672 /*********************************************************************
3677 static void EDIT_WM_Command(EDITSTATE
*es
, INT code
, INT id
, HWND control
)
3679 if (code
|| control
)
3699 EDIT_EM_SetSel(es
, 0, (UINT
)-1, FALSE
);
3700 EDIT_EM_ScrollCaret(es
);
3703 ERR("unknown menu item, please report\n");
3709 /*********************************************************************
3713 * Note: the resource files resource/sysres_??.rc cannot define a
3714 * single popup menu. Hence we use a (dummy) menubar
3715 * containing the single popup menu as its first item.
3717 * FIXME: the message identifiers have been chosen arbitrarily,
3718 * hence we use MF_BYPOSITION.
3719 * We might as well use the "real" values (anybody knows ?)
3720 * The menu definition is in resources/sysres_??.rc.
3721 * Once these are OK, we better use MF_BYCOMMAND here
3722 * (as we do in EDIT_WM_Command()).
3725 static void EDIT_WM_ContextMenu(EDITSTATE
*es
, INT x
, INT y
)
3727 HMENU menu
= LoadMenuA(user32_module
, "EDITMENU");
3728 HMENU popup
= GetSubMenu(menu
, 0);
3729 UINT start
= es
->selection_start
;
3730 UINT end
= es
->selection_end
;
3732 ORDER_UINT(start
, end
);
3735 EnableMenuItem(popup
, 0, MF_BYPOSITION
| (EDIT_EM_CanUndo(es
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3737 EnableMenuItem(popup
, 2, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3739 EnableMenuItem(popup
, 3, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) ? MF_ENABLED
: MF_GRAYED
));
3741 EnableMenuItem(popup
, 4, MF_BYPOSITION
| (IsClipboardFormatAvailable(CF_UNICODETEXT
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3743 EnableMenuItem(popup
, 5, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3745 EnableMenuItem(popup
, 7, MF_BYPOSITION
| (start
|| (end
!= strlenW(es
->text
)) ? MF_ENABLED
: MF_GRAYED
));
3747 TrackPopupMenu(popup
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, es
->hwndSelf
, NULL
);
3752 /*********************************************************************
3757 static void EDIT_WM_Copy(EDITSTATE
*es
)
3759 INT s
= min(es
->selection_start
, es
->selection_end
);
3760 INT e
= max(es
->selection_start
, es
->selection_end
);
3766 hdst
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (DWORD
)(e
- s
+ 1) * sizeof(WCHAR
));
3767 dst
= GlobalLock(hdst
);
3768 strncpyW(dst
, es
->text
+ s
, e
- s
);
3769 dst
[e
- s
] = 0; /* ensure 0 termination */
3770 TRACE("%s\n", debugstr_w(dst
));
3772 OpenClipboard(es
->hwndSelf
);
3774 SetClipboardData(CF_UNICODETEXT
, hdst
);
3779 /*********************************************************************
3784 static LRESULT
EDIT_WM_Create(EDITSTATE
*es
, LPCWSTR name
)
3786 TRACE("%s\n", debugstr_w(name
));
3788 * To initialize some final structure members, we call some helper
3789 * functions. However, since the EDITSTATE is not consistent (i.e.
3790 * not fully initialized), we should be very careful which
3791 * functions can be called, and in what order.
3793 EDIT_WM_SetFont(es
, 0, FALSE
);
3794 EDIT_EM_EmptyUndoBuffer(es
);
3796 if (name
&& *name
) {
3797 EDIT_EM_ReplaceSel(es
, FALSE
, name
, FALSE
, TRUE
);
3798 /* if we insert text to the editline, the text scrolls out
3799 * of the window, as the caret is placed after the insert
3800 * pos normally; thus we reset es->selection... to 0 and
3803 es
->selection_start
= es
->selection_end
= 0;
3804 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
3805 * Messages are only to be sent when the USER does something to
3806 * change the contents. So I am removing this EN_CHANGE
3808 * EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3810 EDIT_EM_ScrollCaret(es
);
3812 /* force scroll info update */
3813 EDIT_UpdateScrollInfo(es
);
3814 /* The rule seems to return 1 here for success */
3815 /* Power Builder masked edit controls will crash */
3817 /* FIXME: is that in all cases so ? */
3822 /*********************************************************************
3827 static LRESULT
EDIT_WM_Destroy(EDITSTATE
*es
)
3832 while (LocalUnlock(es
->hloc32W
)) ;
3833 LocalFree(es
->hloc32W
);
3836 while (LocalUnlock(es
->hloc32A
)) ;
3837 LocalFree(es
->hloc32A
);
3840 HINSTANCE16 hInstance
= GetWindowLongPtrW( es
->hwndSelf
, GWLP_HINSTANCE
);
3841 while (LOCAL_Unlock(hInstance
, es
->hloc16
)) ;
3842 LOCAL_Free(hInstance
, es
->hloc16
);
3845 pc
= es
->first_line_def
;
3849 HeapFree(GetProcessHeap(), 0, pc
);
3853 SetWindowLongW( es
->hwndSelf
, 0, 0 );
3854 HeapFree(GetProcessHeap(), 0, es
);
3860 /*********************************************************************
3865 static LRESULT
EDIT_WM_EraseBkGnd(EDITSTATE
*es
, HDC dc
)
3867 /* we do the proper erase in EDIT_WM_Paint */
3872 /*********************************************************************
3877 static INT
EDIT_WM_GetText(EDITSTATE
*es
, INT count
, LPARAM lParam
, BOOL unicode
)
3879 if(!count
) return 0;
3883 LPWSTR textW
= (LPWSTR
)lParam
;
3884 lstrcpynW(textW
, es
->text
, count
);
3885 return strlenW(textW
);
3889 LPSTR textA
= (LPSTR
)lParam
;
3890 if (!WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, count
, NULL
, NULL
))
3891 textA
[count
- 1] = 0; /* ensure 0 termination */
3892 return strlen(textA
);
3896 /*********************************************************************
3901 static LRESULT
EDIT_WM_HScroll(EDITSTATE
*es
, INT action
, INT pos
)
3906 if (!(es
->style
& ES_MULTILINE
))
3909 if (!(es
->style
& ES_AUTOHSCROLL
))
3913 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
3916 TRACE("SB_LINELEFT\n");
3918 dx
= -es
->char_width
;
3921 TRACE("SB_LINERIGHT\n");
3922 if (es
->x_offset
< es
->text_width
)
3923 dx
= es
->char_width
;
3926 TRACE("SB_PAGELEFT\n");
3928 dx
= -fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
3931 TRACE("SB_PAGERIGHT\n");
3932 if (es
->x_offset
< es
->text_width
)
3933 dx
= fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
3941 TRACE("SB_RIGHT\n");
3942 if (es
->x_offset
< es
->text_width
)
3943 dx
= es
->text_width
- es
->x_offset
;
3946 TRACE("SB_THUMBTRACK %d\n", pos
);
3947 es
->flags
|= EF_HSCROLL_TRACK
;
3948 if(es
->style
& WS_HSCROLL
)
3949 dx
= pos
- es
->x_offset
;
3954 if(pos
< 0 || pos
> 100) return 0;
3955 /* Assume default scroll range 0-100 */
3956 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
3957 new_x
= pos
* (es
->text_width
- fw
) / 100;
3958 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
3961 case SB_THUMBPOSITION
:
3962 TRACE("SB_THUMBPOSITION %d\n", pos
);
3963 es
->flags
&= ~EF_HSCROLL_TRACK
;
3964 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
3965 dx
= pos
- es
->x_offset
;
3970 if(pos
< 0 || pos
> 100) return 0;
3971 /* Assume default scroll range 0-100 */
3972 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
3973 new_x
= pos
* (es
->text_width
- fw
) / 100;
3974 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
3977 /* force scroll info update */
3978 EDIT_UpdateScrollInfo(es
);
3979 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
, "EN_HSCROLL");
3983 TRACE("SB_ENDSCROLL\n");
3986 * FIXME : the next two are undocumented !
3987 * Are we doing the right thing ?
3988 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3989 * although it's also a regular control message.
3991 case EM_GETTHUMB
: /* this one is used by NT notepad */
3995 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
3996 ret
= GetScrollPos(es
->hwndSelf
, SB_HORZ
);
3999 /* Assume default scroll range 0-100 */
4000 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4001 ret
= es
->text_width
? es
->x_offset
* 100 / (es
->text_width
- fw
) : 0;
4003 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4006 case EM_LINESCROLL16
:
4007 TRACE("EM_LINESCROLL16\n");
4012 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4018 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4019 /* check if we are going to move too far */
4020 if(es
->x_offset
+ dx
+ fw
> es
->text_width
)
4021 dx
= es
->text_width
- fw
- es
->x_offset
;
4023 EDIT_EM_LineScroll_internal(es
, dx
, 0);
4029 /*********************************************************************
4034 static BOOL
EDIT_CheckCombo(EDITSTATE
*es
, UINT msg
, INT key
)
4036 HWND hLBox
= es
->hwndListBox
;
4044 hCombo
= GetParent(es
->hwndSelf
);
4048 TRACE_(combo
)("[%p]: handling msg %x (%x)\n", es
->hwndSelf
, msg
, key
);
4050 if (key
== VK_UP
|| key
== VK_DOWN
)
4052 if (SendMessageW(hCombo
, CB_GETEXTENDEDUI
, 0, 0))
4055 if (msg
== WM_KEYDOWN
|| nEUI
)
4056 bDropped
= (BOOL
)SendMessageW(hCombo
, CB_GETDROPPEDSTATE
, 0, 0);
4062 if (!bDropped
&& nEUI
&& (key
== VK_UP
|| key
== VK_DOWN
))
4064 /* make sure ComboLBox pops up */
4065 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, FALSE
, 0);
4070 SendMessageW(hLBox
, WM_KEYDOWN
, (WPARAM
)key
, 0);
4073 case WM_SYSKEYDOWN
: /* Handle Alt+up/down arrows */
4075 SendMessageW(hCombo
, CB_SHOWDROPDOWN
, bDropped
? FALSE
: TRUE
, 0);
4077 SendMessageW(hLBox
, WM_KEYDOWN
, (WPARAM
)VK_F4
, 0);
4082 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, TRUE
, 0);
4088 /*********************************************************************
4092 * Handling of special keys that don't produce a WM_CHAR
4093 * (i.e. non-printable keys) & Backspace & Delete
4096 static LRESULT
EDIT_WM_KeyDown(EDITSTATE
*es
, INT key
)
4101 if (GetKeyState(VK_MENU
) & 0x8000)
4104 shift
= GetKeyState(VK_SHIFT
) & 0x8000;
4105 control
= GetKeyState(VK_CONTROL
) & 0x8000;
4110 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
) || key
== VK_F4
)
4115 if ((es
->style
& ES_MULTILINE
) && (key
== VK_UP
))
4116 EDIT_MoveUp_ML(es
, shift
);
4119 EDIT_MoveWordBackward(es
, shift
);
4121 EDIT_MoveBackward(es
, shift
);
4124 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
))
4128 if ((es
->style
& ES_MULTILINE
) && (key
== VK_DOWN
))
4129 EDIT_MoveDown_ML(es
, shift
);
4131 EDIT_MoveWordForward(es
, shift
);
4133 EDIT_MoveForward(es
, shift
);
4136 EDIT_MoveHome(es
, shift
);
4139 EDIT_MoveEnd(es
, shift
);
4142 if (es
->style
& ES_MULTILINE
)
4143 EDIT_MovePageUp_ML(es
, shift
);
4145 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
4148 if (es
->style
& ES_MULTILINE
)
4149 EDIT_MovePageDown_ML(es
, shift
);
4151 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
4154 if (!(es
->style
& ES_READONLY
) && !(shift
&& control
)) {
4155 if (es
->selection_start
!= es
->selection_end
) {
4162 /* delete character left of caret */
4163 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
4164 EDIT_MoveBackward(es
, TRUE
);
4166 } else if (control
) {
4167 /* delete to end of line */
4168 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
4169 EDIT_MoveEnd(es
, TRUE
);
4172 /* delete character right of caret */
4173 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
4174 EDIT_MoveForward(es
, TRUE
);
4182 if (!(es
->style
& ES_READONLY
))
4188 /* If the edit doesn't want the return send a message to the default object */
4189 if(!(es
->style
& ES_WANTRETURN
))
4191 HWND hwndParent
= GetParent(es
->hwndSelf
);
4192 DWORD dw
= SendMessageW( hwndParent
, DM_GETDEFID
, 0, 0 );
4193 if (HIWORD(dw
) == DC_HASDEFID
)
4195 SendMessageW( hwndParent
, WM_COMMAND
,
4196 MAKEWPARAM( LOWORD(dw
), BN_CLICKED
),
4197 (LPARAM
)GetDlgItem( hwndParent
, LOWORD(dw
) ) );
4206 /*********************************************************************
4211 static LRESULT
EDIT_WM_KillFocus(EDITSTATE
*es
)
4213 es
->flags
&= ~EF_FOCUSED
;
4215 if(!(es
->style
& ES_NOHIDESEL
))
4216 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
4217 EDIT_NOTIFY_PARENT(es
, EN_KILLFOCUS
, "EN_KILLFOCUS");
4222 /*********************************************************************
4226 * The caret position has been set on the WM_LBUTTONDOWN message
4229 static LRESULT
EDIT_WM_LButtonDblClk(EDITSTATE
*es
)
4232 INT e
= es
->selection_end
;
4237 if (!(es
->flags
& EF_FOCUSED
))
4240 l
= EDIT_EM_LineFromChar(es
, e
);
4241 li
= EDIT_EM_LineIndex(es
, l
);
4242 ll
= EDIT_EM_LineLength(es
, e
);
4243 s
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
4244 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_RIGHT
);
4245 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
4246 EDIT_EM_ScrollCaret(es
);
4251 /*********************************************************************
4256 static LRESULT
EDIT_WM_LButtonDown(EDITSTATE
*es
, DWORD keys
, INT x
, INT y
)
4261 SetFocus(es
->hwndSelf
);
4262 if (!(es
->flags
& EF_FOCUSED
))
4265 es
->bCaptureState
= TRUE
;
4266 SetCapture(es
->hwndSelf
);
4267 EDIT_ConfinePoint(es
, &x
, &y
);
4268 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
4269 EDIT_EM_SetSel(es
, (keys
& MK_SHIFT
) ? es
->selection_start
: e
, e
, after_wrap
);
4270 EDIT_EM_ScrollCaret(es
);
4271 es
->region_posx
= es
->region_posy
= 0;
4272 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
4277 /*********************************************************************
4282 static LRESULT
EDIT_WM_LButtonUp(EDITSTATE
*es
)
4284 if (es
->bCaptureState
) {
4285 KillTimer(es
->hwndSelf
, 0);
4286 if (GetCapture() == es
->hwndSelf
) ReleaseCapture();
4288 es
->bCaptureState
= FALSE
;
4293 /*********************************************************************
4298 static LRESULT
EDIT_WM_MButtonDown(EDITSTATE
*es
)
4300 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
4305 /*********************************************************************
4310 static LRESULT
EDIT_WM_MouseMove(EDITSTATE
*es
, INT x
, INT y
)
4316 if (GetCapture() != es
->hwndSelf
)
4320 * FIXME: gotta do some scrolling if outside client
4321 * area. Maybe reset the timer ?
4324 EDIT_ConfinePoint(es
, &x
, &y
);
4325 es
->region_posx
= (prex
< x
) ? -1 : ((prex
> x
) ? 1 : 0);
4326 es
->region_posy
= (prey
< y
) ? -1 : ((prey
> y
) ? 1 : 0);
4327 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
4328 EDIT_EM_SetSel(es
, es
->selection_start
, e
, after_wrap
);
4329 EDIT_SetCaretPos(es
,es
->selection_end
,es
->flags
& EF_AFTER_WRAP
);
4334 /*********************************************************************
4338 * See also EDIT_WM_StyleChanged
4340 static LRESULT
EDIT_WM_NCCreate(HWND hwnd
, LPCREATESTRUCTW lpcs
, BOOL unicode
)
4345 TRACE("Creating %s edit control, style = %08lx\n",
4346 unicode
? "Unicode" : "ANSI", lpcs
->style
);
4348 if (!(es
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*es
))))
4350 SetWindowLongW( hwnd
, 0, (LONG
)es
);
4353 * Note: since the EDITSTATE has not been fully initialized yet,
4354 * we can't use any API calls that may send
4355 * WM_XXX messages before WM_NCCREATE is completed.
4358 es
->is_unicode
= unicode
;
4359 es
->style
= lpcs
->style
;
4361 es
->bEnableState
= !(es
->style
& WS_DISABLED
);
4363 es
->hwndSelf
= hwnd
;
4364 /* Save parent, which will be notified by EN_* messages */
4365 es
->hwndParent
= lpcs
->hwndParent
;
4367 if (es
->style
& ES_COMBO
)
4368 es
->hwndListBox
= GetDlgItem(es
->hwndParent
, ID_CB_LISTBOX
);
4370 /* Number overrides lowercase overrides uppercase (at least it
4371 * does in Win95). However I'll bet that ES_NUMBER would be
4372 * invalid under Win 3.1.
4374 if (es
->style
& ES_NUMBER
) {
4375 ; /* do not override the ES_NUMBER */
4376 } else if (es
->style
& ES_LOWERCASE
) {
4377 es
->style
&= ~ES_UPPERCASE
;
4379 if (es
->style
& ES_MULTILINE
) {
4380 es
->buffer_limit
= BUFLIMIT_MULTI
;
4381 if (es
->style
& WS_VSCROLL
)
4382 es
->style
|= ES_AUTOVSCROLL
;
4383 if (es
->style
& WS_HSCROLL
)
4384 es
->style
|= ES_AUTOHSCROLL
;
4385 es
->style
&= ~ES_PASSWORD
;
4386 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
)) {
4387 /* Confirmed - RIGHT overrides CENTER */
4388 if (es
->style
& ES_RIGHT
)
4389 es
->style
&= ~ES_CENTER
;
4390 es
->style
&= ~WS_HSCROLL
;
4391 es
->style
&= ~ES_AUTOHSCROLL
;
4394 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4395 es
->style
|= ES_AUTOVSCROLL
;
4397 es
->buffer_limit
= BUFLIMIT_SINGLE
;
4398 es
->style
&= ~ES_CENTER
;
4399 es
->style
&= ~ES_RIGHT
;
4400 es
->style
&= ~WS_HSCROLL
;
4401 es
->style
&= ~WS_VSCROLL
;
4402 es
->style
&= ~ES_AUTOVSCROLL
;
4403 es
->style
&= ~ES_WANTRETURN
;
4404 if (es
->style
& ES_PASSWORD
)
4405 es
->password_char
= '*';
4407 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4408 es
->style
|= ES_AUTOHSCROLL
;
4411 alloc_size
= ROUND_TO_GROW((es
->buffer_size
+ 1) * sizeof(WCHAR
));
4412 if(!(es
->hloc32W
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
4414 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
4416 if (!(es
->undo_text
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (es
->buffer_size
+ 1) * sizeof(WCHAR
))))
4418 es
->undo_buffer_size
= es
->buffer_size
;
4420 if (es
->style
& ES_MULTILINE
)
4421 if (!(es
->first_line_def
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
))))
4426 * In Win95 look and feel, the WS_BORDER style is replaced by the
4427 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4428 * control a non client area. Not always. This coordinates in some
4429 * way with the window creation code in dialog.c When making
4430 * modifications please ensure that the code still works for edit
4431 * controls created directly with style 0x50800000, exStyle 0 (
4432 * which should have a single pixel border)
4434 es
->style
&= ~WS_BORDER
;
4439 /*********************************************************************
4444 static void EDIT_WM_Paint(EDITSTATE
*es
, WPARAM wParam
)
4455 BOOL rev
= es
->bEnableState
&&
4456 ((es
->flags
& EF_FOCUSED
) ||
4457 (es
->style
& ES_NOHIDESEL
));
4459 dc
= BeginPaint(es
->hwndSelf
, &ps
);
4463 GetClientRect(es
->hwndSelf
, &rcClient
);
4465 /* paint the background */
4466 if (!(brush
= EDIT_NotifyCtlColor(es
, dc
)))
4467 brush
= (HBRUSH
)GetStockObject(WHITE_BRUSH
);
4468 IntersectClipRect(dc
, rcClient
.left
, rcClient
.top
, rcClient
.right
, rcClient
.bottom
);
4469 GetClipBox(dc
, &rc
);
4470 FillRect(dc
, &rc
, brush
);
4472 /* draw the border */
4473 if(es
->style
& WS_BORDER
) {
4475 if(es
->style
& ES_MULTILINE
) {
4476 if(es
->style
& WS_HSCROLL
) rc
.bottom
++;
4477 if(es
->style
& WS_VSCROLL
) rc
.right
++;
4479 Rectangle(dc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
4481 IntersectClipRect(dc
, es
->format_rect
.left
,
4482 es
->format_rect
.top
,
4483 es
->format_rect
.right
,
4484 es
->format_rect
.bottom
);
4485 if (es
->style
& ES_MULTILINE
) {
4487 IntersectClipRect(dc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
4490 old_font
= SelectObject(dc
, es
->font
);
4491 EDIT_NotifyCtlColor(es
, dc
);
4493 if (!es
->bEnableState
)
4494 SetTextColor(dc
, GetSysColor(COLOR_GRAYTEXT
));
4495 GetClipBox(dc
, &rcRgn
);
4496 if (es
->style
& ES_MULTILINE
) {
4497 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
4498 for (i
= es
->y_offset
; i
<= min(es
->y_offset
+ vlc
, es
->y_offset
+ es
->line_count
- 1) ; i
++) {
4499 EDIT_GetLineRect(es
, i
, 0, -1, &rcLine
);
4500 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
4501 EDIT_PaintLine(es
, dc
, i
, rev
);
4504 EDIT_GetLineRect(es
, 0, 0, -1, &rcLine
);
4505 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
4506 EDIT_PaintLine(es
, dc
, 0, rev
);
4509 SelectObject(dc
, old_font
);
4512 EndPaint(es
->hwndSelf
, &ps
);
4516 /*********************************************************************
4521 static void EDIT_WM_Paste(EDITSTATE
*es
)
4526 /* Protect read-only edit control from modification */
4527 if(es
->style
& ES_READONLY
)
4530 OpenClipboard(es
->hwndSelf
);
4531 if ((hsrc
= GetClipboardData(CF_UNICODETEXT
))) {
4532 src
= (LPWSTR
)GlobalLock(hsrc
);
4533 EDIT_EM_ReplaceSel(es
, TRUE
, src
, TRUE
, TRUE
);
4540 /*********************************************************************
4545 static void EDIT_WM_SetFocus(EDITSTATE
*es
)
4547 es
->flags
|= EF_FOCUSED
;
4548 CreateCaret(es
->hwndSelf
, 0, 2, es
->line_height
);
4549 EDIT_SetCaretPos(es
, es
->selection_end
,
4550 es
->flags
& EF_AFTER_WRAP
);
4551 if(!(es
->style
& ES_NOHIDESEL
))
4552 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
4553 ShowCaret(es
->hwndSelf
);
4554 EDIT_NOTIFY_PARENT(es
, EN_SETFOCUS
, "EN_SETFOCUS");
4558 /*********************************************************************
4562 * With Win95 look the margins are set to default font value unless
4563 * the system font (font == 0) is being set, in which case they are left
4567 static void EDIT_WM_SetFont(EDITSTATE
*es
, HFONT font
, BOOL redraw
)
4575 dc
= GetDC(es
->hwndSelf
);
4577 old_font
= SelectObject(dc
, font
);
4578 GetTextMetricsW(dc
, &tm
);
4579 es
->line_height
= tm
.tmHeight
;
4580 es
->char_width
= tm
.tmAveCharWidth
;
4582 SelectObject(dc
, old_font
);
4583 ReleaseDC(es
->hwndSelf
, dc
);
4584 EDIT_EM_SetMargins(es
, EC_LEFTMARGIN
| EC_RIGHTMARGIN
,
4585 EC_USEFONTINFO
, EC_USEFONTINFO
);
4587 /* Force the recalculation of the format rect for each font change */
4588 GetClientRect(es
->hwndSelf
, &r
);
4589 EDIT_SetRectNP(es
, &r
);
4591 if (es
->style
& ES_MULTILINE
)
4592 EDIT_BuildLineDefs_ML(es
, 0, strlenW(es
->text
), 0, NULL
);
4594 EDIT_CalcLineWidth_SL(es
);
4597 EDIT_UpdateText(es
, NULL
, TRUE
);
4598 if (es
->flags
& EF_FOCUSED
) {
4600 CreateCaret(es
->hwndSelf
, 0, 2, es
->line_height
);
4601 EDIT_SetCaretPos(es
, es
->selection_end
,
4602 es
->flags
& EF_AFTER_WRAP
);
4603 ShowCaret(es
->hwndSelf
);
4608 /*********************************************************************
4613 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4614 * The modified flag is reset. No notifications are sent.
4616 * For single-line controls, reception of WM_SETTEXT triggers:
4617 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4620 static void EDIT_WM_SetText(EDITSTATE
*es
, LPARAM lParam
, BOOL unicode
)
4625 text
= (LPWSTR
)lParam
;
4628 LPCSTR textA
= (LPCSTR
)lParam
;
4629 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
4630 if((text
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
))))
4631 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, text
, countW
);
4634 EDIT_EM_SetSel(es
, 0, (UINT
)-1, FALSE
);
4636 TRACE("%s\n", debugstr_w(text
));
4637 EDIT_EM_ReplaceSel(es
, FALSE
, text
, FALSE
, FALSE
);
4639 HeapFree(GetProcessHeap(), 0, text
);
4641 static const WCHAR empty_stringW
[] = {0};
4643 EDIT_EM_ReplaceSel(es
, FALSE
, empty_stringW
, FALSE
, FALSE
);
4646 es
->flags
&= ~EF_MODIFIED
;
4647 EDIT_EM_SetSel(es
, 0, 0, FALSE
);
4648 /* Send the notification after the selection start and end have been set
4649 * edit control doesn't send notification on WM_SETTEXT
4650 * if it is multiline, or it is part of combobox
4652 if( !((es
->style
& ES_MULTILINE
) || es
->hwndListBox
))
4654 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
, "EN_CHANGE");
4655 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
, "EN_UPDATE");
4657 EDIT_EM_ScrollCaret(es
);
4661 /*********************************************************************
4666 static void EDIT_WM_Size(EDITSTATE
*es
, UINT action
, INT width
, INT height
)
4668 if ((action
== SIZE_MAXIMIZED
) || (action
== SIZE_RESTORED
)) {
4670 TRACE("width = %d, height = %d\n", width
, height
);
4671 SetRect(&rc
, 0, 0, width
, height
);
4672 EDIT_SetRectNP(es
, &rc
);
4673 EDIT_UpdateText(es
, NULL
, TRUE
);
4678 /*********************************************************************
4682 * This message is sent by SetWindowLong on having changed either the Style
4683 * or the extended style.
4685 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4687 * See also EDIT_WM_NCCreate
4689 * It appears that the Windows version of the edit control allows the style
4690 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4691 * style variable which will generally be different. In this function we
4692 * update the internal style based on what changed in the externally visible
4695 * Much of this content as based upon the MSDN, especially:
4696 * Platform SDK Documentation -> User Interface Services ->
4697 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4698 * Edit Control Styles
4700 static LRESULT
EDIT_WM_StyleChanged ( EDITSTATE
*es
, WPARAM which
, const STYLESTRUCT
*style
)
4702 if (GWL_STYLE
== which
) {
4703 DWORD style_change_mask
;
4705 /* Only a subset of changes can be applied after the control
4708 style_change_mask
= ES_UPPERCASE
| ES_LOWERCASE
|
4710 if (es
->style
& ES_MULTILINE
)
4711 style_change_mask
|= ES_WANTRETURN
;
4713 new_style
= style
->styleNew
& style_change_mask
;
4715 /* Number overrides lowercase overrides uppercase (at least it
4716 * does in Win95). However I'll bet that ES_NUMBER would be
4717 * invalid under Win 3.1.
4719 if (new_style
& ES_NUMBER
) {
4720 ; /* do not override the ES_NUMBER */
4721 } else if (new_style
& ES_LOWERCASE
) {
4722 new_style
&= ~ES_UPPERCASE
;
4725 es
->style
= (es
->style
& ~style_change_mask
) | new_style
;
4726 } else if (GWL_EXSTYLE
== which
) {
4727 ; /* FIXME - what is needed here */
4729 WARN ("Invalid style change %d\n",which
);
4735 /*********************************************************************
4740 static LRESULT
EDIT_WM_SysKeyDown(EDITSTATE
*es
, INT key
, DWORD key_data
)
4742 if ((key
== VK_BACK
) && (key_data
& 0x2000)) {
4743 if (EDIT_EM_CanUndo(es
))
4746 } else if (key
== VK_UP
|| key
== VK_DOWN
) {
4747 if (EDIT_CheckCombo(es
, WM_SYSKEYDOWN
, key
))
4750 return DefWindowProcW(es
->hwndSelf
, WM_SYSKEYDOWN
, (WPARAM
)key
, (LPARAM
)key_data
);
4754 /*********************************************************************
4759 static void EDIT_WM_Timer(EDITSTATE
*es
)
4761 if (es
->region_posx
< 0) {
4762 EDIT_MoveBackward(es
, TRUE
);
4763 } else if (es
->region_posx
> 0) {
4764 EDIT_MoveForward(es
, TRUE
);
4767 * FIXME: gotta do some vertical scrolling here, like
4768 * EDIT_EM_LineScroll(hwnd, 0, 1);
4772 /*********************************************************************
4777 static LRESULT
EDIT_WM_VScroll(EDITSTATE
*es
, INT action
, INT pos
)
4781 if (!(es
->style
& ES_MULTILINE
))
4784 if (!(es
->style
& ES_AUTOVSCROLL
))
4793 TRACE("action %d (%s)\n", action
, (action
== SB_LINEUP
? "SB_LINEUP" :
4794 (action
== SB_LINEDOWN
? "SB_LINEDOWN" :
4795 (action
== SB_PAGEUP
? "SB_PAGEUP" :
4797 EDIT_EM_Scroll(es
, action
);
4804 TRACE("SB_BOTTOM\n");
4805 dy
= es
->line_count
- 1 - es
->y_offset
;
4808 TRACE("SB_THUMBTRACK %d\n", pos
);
4809 es
->flags
|= EF_VSCROLL_TRACK
;
4810 if(es
->style
& WS_VSCROLL
)
4811 dy
= pos
- es
->y_offset
;
4814 /* Assume default scroll range 0-100 */
4817 if(pos
< 0 || pos
> 100) return 0;
4818 vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
4819 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4820 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4821 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4822 es
->line_count
, es
->y_offset
, pos
, dy
);
4825 case SB_THUMBPOSITION
:
4826 TRACE("SB_THUMBPOSITION %d\n", pos
);
4827 es
->flags
&= ~EF_VSCROLL_TRACK
;
4828 if(es
->style
& WS_VSCROLL
)
4829 dy
= pos
- es
->y_offset
;
4832 /* Assume default scroll range 0-100 */
4835 if(pos
< 0 || pos
> 100) return 0;
4836 vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
4837 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4838 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4839 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4840 es
->line_count
, es
->y_offset
, pos
, dy
);
4844 /* force scroll info update */
4845 EDIT_UpdateScrollInfo(es
);
4846 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
, "EN_VSCROLL");
4850 TRACE("SB_ENDSCROLL\n");
4853 * FIXME : the next two are undocumented !
4854 * Are we doing the right thing ?
4855 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4856 * although it's also a regular control message.
4858 case EM_GETTHUMB
: /* this one is used by NT notepad */
4862 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_VSCROLL
)
4863 ret
= GetScrollPos(es
->hwndSelf
, SB_VERT
);
4866 /* Assume default scroll range 0-100 */
4867 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
4868 ret
= es
->line_count
? es
->y_offset
* 100 / (es
->line_count
- vlc
) : 0;
4870 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4873 case EM_LINESCROLL16
:
4874 TRACE("EM_LINESCROLL16 %d\n", pos
);
4879 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4884 EDIT_EM_LineScroll(es
, 0, dy
);
4888 /*********************************************************************
4893 static void EDIT_UpdateTextRegion(EDITSTATE
*es
, HRGN hrgn
, BOOL bErase
)
4895 if (es
->flags
& EF_UPDATE
) EDIT_NOTIFY_PARENT(es
, EN_UPDATE
, "EN_UPDATE");
4896 InvalidateRgn(es
->hwndSelf
, hrgn
, bErase
);
4900 /*********************************************************************
4905 static void EDIT_UpdateText(EDITSTATE
*es
, LPRECT rc
, BOOL bErase
)
4907 if (es
->flags
& EF_UPDATE
) EDIT_NOTIFY_PARENT(es
, EN_UPDATE
, "EN_UPDATE");
4908 InvalidateRect(es
->hwndSelf
, rc
, bErase
);