4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
46 #define WIN32_LEAN_AND_MEAN
49 WINE_DEFAULT_DEBUG_CHANNEL(edit
);
50 WINE_DECLARE_DEBUG_CHANNEL(combo
);
51 WINE_DECLARE_DEBUG_CHANNEL(relay
);
53 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
54 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
55 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
56 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
59 * extra flags for EDITSTATE.flags field
61 #define EF_MODIFIED 0x0001 /* text has been modified */
62 #define EF_FOCUSED 0x0002 /* we have input focus */
63 #define EF_UPDATE 0x0004 /* notify parent of changed state */
64 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
65 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
66 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
67 wrapped line, instead of in front of the next character */
68 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
69 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
73 END_0
= 0, /* line ends with terminating '\0' character */
74 END_WRAP
, /* line is wrapped */
75 END_HARD
, /* line ends with a hard return '\r\n' */
76 END_SOFT
, /* line ends with a soft return '\r\r\n' */
77 END_RICH
/* line ends with a single '\n' */
80 typedef struct tagLINEDEF
{
81 INT length
; /* bruto length of a line in bytes */
82 INT net_length
; /* netto length of a line in visible characters */
84 INT width
; /* width of the line in pixels */
85 INT index
; /* line index into the buffer */
86 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data */
87 struct tagLINEDEF
*next
;
92 BOOL is_unicode
; /* how the control was created */
93 LPWSTR text
; /* the actual contents of the control */
94 UINT text_length
; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
95 UINT buffer_size
; /* the size of the buffer in characters */
96 UINT buffer_limit
; /* the maximum size to which the buffer may grow in characters */
97 HFONT font
; /* NULL means standard system font */
98 INT x_offset
; /* scroll offset for multi lines this is in pixels
99 for single lines it's in characters */
100 INT line_height
; /* height of a screen line in pixels */
101 INT char_width
; /* average character width in pixels */
102 DWORD style
; /* sane version of wnd->dwStyle */
103 WORD flags
; /* flags that are not in es->style or wnd->flags (EF_XXX) */
104 INT undo_insert_count
; /* number of characters inserted in sequence */
105 UINT undo_position
; /* character index of the insertion and deletion */
106 LPWSTR undo_text
; /* deleted text */
107 UINT undo_buffer_size
; /* size of the deleted text buffer */
108 INT selection_start
; /* == selection_end if no selection */
109 INT selection_end
; /* == current caret position */
110 WCHAR password_char
; /* == 0 if no password char, and for multi line controls */
111 INT left_margin
; /* in pixels */
112 INT right_margin
; /* in pixels */
114 INT text_width
; /* width of the widest line in pixels for multi line controls
115 and just line width for single line controls */
116 INT region_posx
; /* Position of cursor relative to region: */
117 INT region_posy
; /* -1: to left, 0: within, 1: to right */
118 void *word_break_proc
; /* 32-bit word break proc: ANSI or Unicode */
119 INT line_count
; /* number of lines */
120 INT y_offset
; /* scroll offset in number of lines */
121 BOOL bCaptureState
; /* flag indicating whether mouse was captured */
122 BOOL bEnableState
; /* flag keeping the enable state */
123 HWND hwndSelf
; /* the our window handle */
124 HWND hwndParent
; /* Handle of parent for sending EN_* messages.
125 Even if parent will change, EN_* messages
126 should be sent to the first parent. */
127 HWND hwndListBox
; /* handle of ComboBox's listbox or NULL */
128 INT wheelDeltaRemainder
; /* scroll wheel delta left over after scrolling whole lines */
130 * only for multi line controls
132 INT lock_count
; /* amount of re-entries in the EditWndProc */
135 LINEDEF
*first_line_def
; /* linked list of (soft) linebreaks */
136 HLOCAL hloc32W
; /* our unicode local memory block */
137 HLOCAL hloc32A
; /* alias for ANSI control receiving EM_GETHANDLE
139 HLOCAL hlocapp
; /* The text buffer handle belongs to the app */
143 UINT composition_len
; /* length of composition, 0 == no composition */
144 int composition_start
; /* the character position for the composition */
148 SCRIPT_LOGATTR
*logAttr
;
149 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data for single line controls */
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) \
159 { /* Notify parent which has created this edit control */ \
160 TRACE("notification " #wNotifyCode " 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 static const WCHAR empty_stringW
[] = {0};
167 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
);
169 /*********************************************************************
174 static inline BOOL
EDIT_EM_CanUndo(const EDITSTATE
*es
)
176 return (es
->undo_insert_count
|| strlenW(es
->undo_text
));
180 /*********************************************************************
185 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE
*es
)
187 es
->undo_insert_count
= 0;
188 *es
->undo_text
= '\0';
192 /**********************************************************************
195 * Returns the window version in case Wine emulates a later version
196 * of windows than the application expects.
198 * In a number of cases when windows runs an application that was
199 * designed for an earlier windows version, windows reverts
200 * to "old" behaviour of that earlier version.
202 * An example is a disabled edit control that needs to be painted.
203 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
204 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
205 * applications with an expected version 0f 4.0 or higher.
208 static DWORD
get_app_version(void)
210 static DWORD version
;
213 DWORD dwEmulatedVersion
;
215 DWORD dwProcVersion
= GetProcessVersion(0);
217 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOW
);
218 GetVersionExW( &info
);
219 dwEmulatedVersion
= MAKELONG( info
.dwMinorVersion
, info
.dwMajorVersion
);
220 /* FIXME: this may not be 100% correct; see discussion on the
221 * wine developer list in Nov 1999 */
222 version
= dwProcVersion
< dwEmulatedVersion
? dwProcVersion
: dwEmulatedVersion
;
227 static HBRUSH
EDIT_NotifyCtlColor(EDITSTATE
*es
, HDC hdc
)
232 if ( get_app_version() >= 0x40000 && (!es
->bEnableState
|| (es
->style
& ES_READONLY
)))
233 msg
= WM_CTLCOLORSTATIC
;
235 msg
= WM_CTLCOLOREDIT
;
237 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
240 hbrush
= GetControlBrush(es
->hwndSelf
, hdc
, msg
);
242 hbrush
= (HBRUSH
)SendMessageW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
244 hbrush
= (HBRUSH
)DefWindowProcW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
250 static inline UINT
get_text_length(EDITSTATE
*es
)
252 if(es
->text_length
== (UINT
)-1)
253 es
->text_length
= strlenW(es
->text
);
254 return es
->text_length
;
258 /*********************************************************************
262 * Find the beginning of words.
263 * Note: unlike the specs for a WordBreakProc, this function can
264 * only be called without linebreaks between s[0] up to
265 * s[count - 1]. Remember it is only called
266 * internally, so we can decide this for ourselves.
267 * Additionally we will always be breaking the full string.
270 static INT
EDIT_WordBreakProc(EDITSTATE
*es
, LPWSTR s
, INT index
, INT count
, INT action
)
274 TRACE("s=%p, index=%d, count=%d, action=%d\n", s
, index
, count
, action
);
282 memset(&psa
,0,sizeof(SCRIPT_ANALYSIS
));
283 psa
.eScript
= SCRIPT_UNDEFINED
;
285 es
->logAttr
= HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR
) * get_text_length(es
));
286 ScriptBreak(es
->text
, get_text_length(es
), &psa
, es
->logAttr
);
293 while (index
&& !es
->logAttr
[index
].fSoftBreak
)
300 while (index
< count
&& s
[index
] && !es
->logAttr
[index
].fSoftBreak
)
305 ret
= es
->logAttr
[index
].fWhiteSpace
;
308 ERR("unknown action code, please report !\n");
315 /*********************************************************************
317 * EDIT_CallWordBreakProc
319 * Call appropriate WordBreakProc (internal or external).
321 * Note: The "start" argument should always be an index referring
322 * to es->text. The actual wordbreak proc might be
323 * 16 bit, so we can't always pass any 32 bit LPSTR.
324 * Hence we assume that es->text is the buffer that holds
325 * the string under examination (we can decide this for ourselves).
328 static INT
EDIT_CallWordBreakProc(EDITSTATE
*es
, INT start
, INT index
, INT count
, INT action
)
332 if (es
->word_break_proc
)
336 EDITWORDBREAKPROCW wbpW
= (EDITWORDBREAKPROCW
)es
->word_break_proc
;
338 TRACE_(relay
)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
339 es
->word_break_proc
, debugstr_wn(es
->text
+ start
, count
), index
, count
, action
);
340 ret
= wbpW(es
->text
+ start
, index
, count
, action
);
344 EDITWORDBREAKPROCA wbpA
= (EDITWORDBREAKPROCA
)es
->word_break_proc
;
348 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, NULL
, 0, NULL
, NULL
);
349 textA
= HeapAlloc(GetProcessHeap(), 0, countA
);
352 if (textA
== NULL
) return 0;
354 WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, textA
, countA
, NULL
, NULL
);
355 TRACE_(relay
)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
356 es
->word_break_proc
, debugstr_an(textA
, countA
), index
, countA
, action
);
357 ret
= wbpA(textA
, index
, countA
, action
);
358 HeapFree(GetProcessHeap(), 0, textA
);
362 ret
= EDIT_WordBreakProc(es
, es
->text
, index
+start
, count
+start
, action
) - start
;
367 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF
*line_def
)
371 ScriptStringFree(&line_def
->ssa
);
372 line_def
->ssa
= NULL
;
376 static inline void EDIT_InvalidateUniscribeData(EDITSTATE
*es
)
378 LINEDEF
*line_def
= es
->first_line_def
;
381 EDIT_InvalidateUniscribeData_linedef(line_def
);
382 line_def
= line_def
->next
;
386 ScriptStringFree(&es
->ssa
);
391 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData_linedef(EDITSTATE
*es
, HDC dc
, LINEDEF
*line_def
)
396 if (line_def
->net_length
&& !line_def
->ssa
)
398 int index
= line_def
->index
;
399 HFONT old_font
= NULL
;
401 SCRIPT_TABDEF tabdef
;
405 udc
= GetDC(es
->hwndSelf
);
407 old_font
= SelectObject(udc
, es
->font
);
409 tabdef
.cTabStops
= es
->tabs_count
;
411 tabdef
.pTabStops
= es
->tabs
;
412 tabdef
.iTabOrigin
= 0;
414 hr
= ScriptStringAnalyse(udc
, &es
->text
[index
], line_def
->net_length
,
417 (3*line_def
->net_length
/2+16), -1,
419 (1.5*line_def
->net_length
+16), -1,
421 SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
|SSA_TAB
, -1,
422 NULL
, NULL
, NULL
, &tabdef
, NULL
, &line_def
->ssa
);
425 WARN("ScriptStringAnalyse failed (%x)\n",hr
);
426 line_def
->ssa
= NULL
;
430 SelectObject(udc
, old_font
);
432 ReleaseDC(es
->hwndSelf
, udc
);
435 return line_def
->ssa
;
438 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData(EDITSTATE
*es
, HDC dc
, INT line
)
442 if (!(es
->style
& ES_MULTILINE
))
446 INT length
= get_text_length(es
);
447 HFONT old_font
= NULL
;
451 udc
= GetDC(es
->hwndSelf
);
453 old_font
= SelectObject(udc
, es
->font
);
455 if (es
->style
& ES_PASSWORD
)
458 ScriptStringAnalyse(udc
, &es
->password_char
, length
, (3*length
/2+16), -1, SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
|SSA_PASSWORD
, -1, NULL
, NULL
, NULL
, NULL
, NULL
, &es
->ssa
);
460 ScriptStringAnalyse(udc
, &es
->password_char
, length
, (1.5*length
+16), -1, SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
|SSA_PASSWORD
, -1, NULL
, NULL
, NULL
, NULL
, NULL
, &es
->ssa
);
465 ScriptStringAnalyse(udc
, es
->text
, length
, (3*length
/2+16), -1, SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
, -1, NULL
, NULL
, NULL
, NULL
, NULL
, &es
->ssa
);
467 ScriptStringAnalyse(udc
, es
->text
, length
, (1.5*length
+16), -1, SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
, -1, NULL
, NULL
, NULL
, NULL
, NULL
, &es
->ssa
);
471 SelectObject(udc
, old_font
);
473 ReleaseDC(es
->hwndSelf
, udc
);
479 line_def
= es
->first_line_def
;
480 while (line_def
&& line
)
482 line_def
= line_def
->next
;
486 return EDIT_UpdateUniscribeData_linedef(es
,dc
,line_def
);
490 static inline INT
get_vertical_line_count(EDITSTATE
*es
)
492 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
496 /*********************************************************************
498 * EDIT_BuildLineDefs_ML
500 * Build linked list of text lines.
501 * Lines can end with '\0' (last line), a character (if it is wrapped),
502 * a soft return '\r\r\n' or a hard return '\r\n'
505 static void EDIT_BuildLineDefs_ML(EDITSTATE
*es
, INT istart
, INT iend
, INT delta
, HRGN hrgn
)
507 LPWSTR current_position
, cp
;
509 LINEDEF
*current_line
;
510 LINEDEF
*previous_line
;
512 INT line_index
= 0, nstart_line
, nstart_index
;
513 INT line_count
= es
->line_count
;
518 if (istart
== iend
&& delta
== 0)
521 previous_line
= NULL
;
522 current_line
= es
->first_line_def
;
524 /* Find starting line. istart must lie inside an existing line or
525 * at the end of buffer */
527 if (istart
< current_line
->index
+ current_line
->length
||
528 current_line
->ending
== END_0
)
531 previous_line
= current_line
;
532 current_line
= current_line
->next
;
534 } while (current_line
);
536 if (!current_line
) /* Error occurred start is not inside previous buffer */
538 FIXME(" modification occurred outside buffer\n");
542 /* Remember start of modifications in order to calculate update region */
543 nstart_line
= line_index
;
544 nstart_index
= current_line
->index
;
546 /* We must start to reformat from the previous line since the modifications
547 * may have caused the line to wrap upwards. */
548 if (!(es
->style
& ES_AUTOHSCROLL
) && line_index
> 0)
551 current_line
= previous_line
;
553 start_line
= current_line
;
555 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
556 current_position
= es
->text
+ current_line
->index
;
557 vlc
= get_vertical_line_count(es
);
559 if (current_line
!= start_line
)
561 if (!current_line
|| current_line
->index
+ delta
> current_position
- es
->text
)
563 /* The buffer has been expanded, create a new line and
564 insert it into the link list */
565 LINEDEF
*new_line
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
));
568 if (new_line
== NULL
)
571 new_line
->next
= previous_line
->next
;
572 previous_line
->next
= new_line
;
573 current_line
= new_line
;
576 else if (current_line
->index
+ delta
< current_position
- es
->text
)
578 /* The previous line merged with this line so we delete this extra entry */
579 previous_line
->next
= current_line
->next
;
580 HeapFree(GetProcessHeap(), 0, current_line
);
581 current_line
= previous_line
->next
;
585 else /* current_line->index + delta == current_position */
587 if (current_position
- es
->text
> iend
)
588 break; /* We reached end of line modifications */
589 /* else recalculate this line */
593 current_line
->index
= current_position
- es
->text
;
594 orig_net_length
= current_line
->net_length
;
596 /* Find end of line */
597 cp
= current_position
;
599 if (*cp
== '\n') break;
600 if ((*cp
== '\r') && (*(cp
+ 1) == '\n'))
605 /* Mark type of line termination */
607 current_line
->ending
= END_0
;
608 current_line
->net_length
= strlenW(current_position
);
609 } else if ((cp
> current_position
) && (*(cp
- 1) == '\r')) {
610 current_line
->ending
= END_SOFT
;
611 current_line
->net_length
= cp
- current_position
- 1;
612 } else if (*cp
== '\n') {
613 current_line
->ending
= END_RICH
;
614 current_line
->net_length
= cp
- current_position
;
616 current_line
->ending
= END_HARD
;
617 current_line
->net_length
= cp
- current_position
;
620 if (current_line
->net_length
)
623 EDIT_InvalidateUniscribeData_linedef(current_line
);
624 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
625 if (current_line
->ssa
)
627 sz
= ScriptString_pSize(current_line
->ssa
);
628 /* Calculate line width */
629 current_line
->width
= sz
->cx
;
631 else current_line
->width
= es
->char_width
* current_line
->net_length
;
633 else current_line
->width
= 0;
635 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
637 /* Line breaks just look back from the end and find the next break and try that. */
639 if (!(es
->style
& ES_AUTOHSCROLL
)) {
640 if (current_line
->width
> fw
&& fw
> es
->char_width
) {
647 prev
= current_line
->net_length
- 1;
648 w
= current_line
->net_length
;
649 d
= (float)current_line
->width
/(float)fw
;
650 if (d
> 1.2f
) d
-= 0.2f
;
652 if (next
>= prev
) next
= prev
-1;
654 prev
= EDIT_CallWordBreakProc(es
, current_position
- es
->text
,
655 next
, current_line
->net_length
, WB_LEFT
);
656 current_line
->net_length
= prev
;
657 EDIT_InvalidateUniscribeData_linedef(current_line
);
658 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
659 if (current_line
->ssa
)
660 sz
= ScriptString_pSize(current_line
->ssa
);
663 current_line
->width
= sz
->cx
;
667 } while (prev
&& current_line
->width
> fw
);
668 current_line
->net_length
= w
;
670 if (prev
== 0) { /* Didn't find a line break so force a break */
674 EDIT_InvalidateUniscribeData_linedef(current_line
);
675 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
677 if (current_line
->ssa
)
679 count
= ScriptString_pcOutChars(current_line
->ssa
);
680 piDx
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * (*count
));
681 ScriptStringGetLogicalWidths(current_line
->ssa
,piDx
);
683 prev
= current_line
->net_length
-1;
685 current_line
->width
-= piDx
[prev
];
687 } while ( prev
> 0 && current_line
->width
> fw
);
690 HeapFree(GetProcessHeap(),0,piDx
);
693 prev
= (fw
/ es
->char_width
);
696 /* If the first line we are calculating, wrapped before istart, we must
697 * adjust istart in order for this to be reflected in the update region. */
698 if (current_line
->index
== nstart_index
&& istart
> current_line
->index
+ prev
)
699 istart
= current_line
->index
+ prev
;
700 /* else if we are updating the previous line before the first line we
701 * are re-calculating and it expanded */
702 else if (current_line
== start_line
&&
703 current_line
->index
!= nstart_index
&& orig_net_length
< prev
)
705 /* Line expanded due to an upwards line wrap so we must partially include
706 * previous line in update region */
707 nstart_line
= line_index
;
708 nstart_index
= current_line
->index
;
709 istart
= current_line
->index
+ orig_net_length
;
712 current_line
->net_length
= prev
;
713 current_line
->ending
= END_WRAP
;
715 if (current_line
->net_length
> 0)
717 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
718 if (current_line
->ssa
)
720 sz
= ScriptString_pSize(current_line
->ssa
);
721 current_line
->width
= sz
->cx
;
724 current_line
->width
= 0;
726 else current_line
->width
= 0;
728 else if (current_line
== start_line
&&
729 current_line
->index
!= nstart_index
&&
730 orig_net_length
< current_line
->net_length
) {
731 /* The previous line expanded but it's still not as wide as the client rect */
732 /* The expansion is due to an upwards line wrap so we must partially include
733 it in the update region */
734 nstart_line
= line_index
;
735 nstart_index
= current_line
->index
;
736 istart
= current_line
->index
+ orig_net_length
;
741 /* Adjust length to include line termination */
742 switch (current_line
->ending
) {
744 current_line
->length
= current_line
->net_length
+ 3;
747 current_line
->length
= current_line
->net_length
+ 1;
750 current_line
->length
= current_line
->net_length
+ 2;
754 current_line
->length
= current_line
->net_length
;
757 es
->text_width
= max(es
->text_width
, current_line
->width
);
758 current_position
+= current_line
->length
;
759 previous_line
= current_line
;
761 /* Discard data for non-visible lines. It will be calculated as needed */
762 if ((line_index
< es
->y_offset
) || (line_index
> es
->y_offset
+ vlc
))
763 EDIT_InvalidateUniscribeData_linedef(current_line
);
765 current_line
= current_line
->next
;
767 } while (previous_line
->ending
!= END_0
);
769 /* Finish adjusting line indexes by delta or remove hanging lines */
770 if (previous_line
->ending
== END_0
)
772 LINEDEF
*pnext
= NULL
;
774 previous_line
->next
= NULL
;
777 pnext
= current_line
->next
;
778 EDIT_InvalidateUniscribeData_linedef(current_line
);
779 HeapFree(GetProcessHeap(), 0, current_line
);
780 current_line
= pnext
;
788 current_line
->index
+= delta
;
789 current_line
= current_line
->next
;
793 /* Calculate rest of modification rectangle */
798 * We calculate two rectangles. One for the first line which may have
799 * an indent with respect to the format rect. The other is a format-width
800 * rectangle that spans the rest of the lines that changed or moved.
802 rc
.top
= es
->format_rect
.top
+ nstart_line
* es
->line_height
-
803 (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
804 rc
.bottom
= rc
.top
+ es
->line_height
;
805 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
))
806 rc
.left
= es
->format_rect
.left
;
808 rc
.left
= LOWORD(EDIT_EM_PosFromChar(es
, nstart_index
, FALSE
));
809 rc
.right
= es
->format_rect
.right
;
810 SetRectRgn(hrgn
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
813 rc
.left
= es
->format_rect
.left
;
814 rc
.right
= es
->format_rect
.right
;
816 * If lines were added or removed we must re-paint the remainder of the
817 * lines since the remaining lines were either shifted up or down.
819 if (line_count
< es
->line_count
) /* We added lines */
820 rc
.bottom
= es
->line_count
* es
->line_height
;
821 else if (line_count
> es
->line_count
) /* We removed lines */
822 rc
.bottom
= line_count
* es
->line_height
;
824 rc
.bottom
= line_index
* es
->line_height
;
825 rc
.bottom
+= es
->format_rect
.top
;
826 rc
.bottom
-= (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
827 tmphrgn
= CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
828 CombineRgn(hrgn
, hrgn
, tmphrgn
, RGN_OR
);
829 DeleteObject(tmphrgn
);
833 /*********************************************************************
835 * EDIT_CalcLineWidth_SL
838 static void EDIT_CalcLineWidth_SL(EDITSTATE
*es
)
840 EDIT_UpdateUniscribeData(es
, NULL
, 0);
844 size
= ScriptString_pSize(es
->ssa
);
845 es
->text_width
= size
->cx
;
851 /*********************************************************************
855 * Beware: This is not the function called on EM_CHARFROMPOS
856 * The position _can_ be outside the formatting / client
858 * The return value is only the character index
861 static INT
EDIT_CharFromPos(EDITSTATE
*es
, INT x
, INT y
, LPBOOL after_wrap
)
865 if (es
->style
& ES_MULTILINE
) {
867 INT line
= (y
- es
->format_rect
.top
) / es
->line_height
+ es
->y_offset
;
869 LINEDEF
*line_def
= es
->first_line_def
;
870 EDIT_UpdateUniscribeData(es
, NULL
, line
);
871 while ((line
> 0) && line_def
->next
) {
872 line_index
+= line_def
->length
;
873 line_def
= line_def
->next
;
877 x
+= es
->x_offset
- es
->format_rect
.left
;
878 if (es
->style
& ES_RIGHT
)
879 x
-= (es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
;
880 else if (es
->style
& ES_CENTER
)
881 x
-= ((es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
) / 2;
882 if (x
>= line_def
->width
) {
884 *after_wrap
= (line_def
->ending
== END_WRAP
);
885 return line_index
+ line_def
->net_length
;
887 if (x
<= 0 || !line_def
->ssa
) {
893 ScriptStringXtoCP(line_def
->ssa
, x
, &index
, &trailing
);
894 if (trailing
) index
++;
897 *after_wrap
= ((index
== line_index
+ line_def
->net_length
) &&
898 (line_def
->ending
== END_WRAP
));
904 x
-= es
->format_rect
.left
;
910 INT indent
= (es
->format_rect
.right
- es
->format_rect
.left
) - es
->text_width
;
911 if (es
->style
& ES_RIGHT
)
913 else if (es
->style
& ES_CENTER
)
917 EDIT_UpdateUniscribeData(es
, NULL
, 0);
922 if (es
->x_offset
>= get_text_length(es
))
925 size
= ScriptString_pSize(es
->ssa
);
928 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
935 if (x
+ xoff
> 0 || !es
->ssa
)
937 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
938 if (trailing
) index
++;
947 const SIZE
*size
= NULL
;
949 size
= ScriptString_pSize(es
->ssa
);
952 else if (x
> size
->cx
)
953 index
= get_text_length(es
);
956 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
957 if (trailing
) index
++;
963 index
= es
->x_offset
;
970 /*********************************************************************
974 * adjusts the point to be within the formatting rectangle
975 * (so CharFromPos returns the nearest _visible_ character)
978 static void EDIT_ConfinePoint(const EDITSTATE
*es
, LPINT x
, LPINT y
)
980 *x
= min(max(*x
, es
->format_rect
.left
), es
->format_rect
.right
- 1);
981 *y
= min(max(*y
, es
->format_rect
.top
), es
->format_rect
.bottom
- 1);
985 /*********************************************************************
990 static INT
EDIT_EM_LineFromChar(EDITSTATE
*es
, INT index
)
995 if (!(es
->style
& ES_MULTILINE
))
997 if (index
> (INT
)get_text_length(es
))
998 return es
->line_count
- 1;
1000 index
= min(es
->selection_start
, es
->selection_end
);
1003 line_def
= es
->first_line_def
;
1004 index
-= line_def
->length
;
1005 while ((index
>= 0) && line_def
->next
) {
1007 line_def
= line_def
->next
;
1008 index
-= line_def
->length
;
1014 /*********************************************************************
1019 static INT
EDIT_EM_LineIndex(const EDITSTATE
*es
, INT line
)
1022 const LINEDEF
*line_def
;
1024 if (!(es
->style
& ES_MULTILINE
))
1026 if (line
>= es
->line_count
)
1030 line_def
= es
->first_line_def
;
1032 INT index
= es
->selection_end
- line_def
->length
;
1033 while ((index
>= 0) && line_def
->next
) {
1034 line_index
+= line_def
->length
;
1035 line_def
= line_def
->next
;
1036 index
-= line_def
->length
;
1040 line_index
+= line_def
->length
;
1041 line_def
= line_def
->next
;
1049 /*********************************************************************
1054 static INT
EDIT_EM_LineLength(EDITSTATE
*es
, INT index
)
1058 if (!(es
->style
& ES_MULTILINE
))
1059 return get_text_length(es
);
1062 /* get the number of remaining non-selected chars of selected lines */
1063 INT32 l
; /* line number */
1064 INT32 li
; /* index of first char in line */
1066 l
= EDIT_EM_LineFromChar(es
, es
->selection_start
);
1067 /* # chars before start of selection area */
1068 count
= es
->selection_start
- EDIT_EM_LineIndex(es
, l
);
1069 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1070 /* # chars after end of selection */
1071 li
= EDIT_EM_LineIndex(es
, l
);
1072 count
+= li
+ EDIT_EM_LineLength(es
, li
) - es
->selection_end
;
1075 line_def
= es
->first_line_def
;
1076 index
-= line_def
->length
;
1077 while ((index
>= 0) && line_def
->next
) {
1078 line_def
= line_def
->next
;
1079 index
-= line_def
->length
;
1081 return line_def
->net_length
;
1085 /*********************************************************************
1090 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
)
1092 INT len
= get_text_length(es
);
1101 index
= min(index
, len
);
1102 if (es
->style
& ES_MULTILINE
) {
1103 l
= EDIT_EM_LineFromChar(es
, index
);
1104 EDIT_UpdateUniscribeData(es
, NULL
, l
);
1106 y
= (l
- es
->y_offset
) * es
->line_height
;
1107 li
= EDIT_EM_LineIndex(es
, l
);
1108 if (after_wrap
&& (li
== index
) && l
) {
1110 line_def
= es
->first_line_def
;
1112 line_def
= line_def
->next
;
1115 if (line_def
->ending
== END_WRAP
) {
1117 y
-= es
->line_height
;
1118 li
= EDIT_EM_LineIndex(es
, l
);
1122 line_def
= es
->first_line_def
;
1123 while (line_def
->index
!= li
)
1124 line_def
= line_def
->next
;
1126 lw
= line_def
->width
;
1127 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1130 ScriptStringCPtoX(line_def
->ssa
, (index
- 1) - li
, TRUE
, &x
);
1136 if (es
->style
& ES_RIGHT
)
1138 else if (es
->style
& ES_CENTER
)
1143 EDIT_UpdateUniscribeData(es
, NULL
, 0);
1148 if (es
->x_offset
>= get_text_length(es
))
1150 int leftover
= es
->x_offset
- get_text_length(es
);
1154 size
= ScriptString_pSize(es
->ssa
);
1159 xoff
+= es
->char_width
* leftover
;
1162 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
1169 if (index
>= get_text_length(es
))
1174 size
= ScriptString_pSize(es
->ssa
);
1181 ScriptStringCPtoX(es
->ssa
, index
, FALSE
, &xi
);
1187 if (index
>= es
->x_offset
) {
1188 if (!es
->x_offset
&& (es
->style
& (ES_RIGHT
| ES_CENTER
)))
1190 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1191 if (w
> es
->text_width
)
1193 if (es
->style
& ES_RIGHT
)
1194 x
+= w
- es
->text_width
;
1195 else if (es
->style
& ES_CENTER
)
1196 x
+= (w
- es
->text_width
) / 2;
1202 x
+= es
->format_rect
.left
;
1203 y
+= es
->format_rect
.top
;
1204 return MAKELONG((INT16
)x
, (INT16
)y
);
1208 /*********************************************************************
1212 * Calculates the bounding rectangle for a line from a starting
1213 * column to an ending column.
1216 static void EDIT_GetLineRect(EDITSTATE
*es
, INT line
, INT scol
, INT ecol
, LPRECT rc
)
1218 SCRIPT_STRING_ANALYSIS ssa
;
1222 if (es
->style
& ES_MULTILINE
)
1224 const LINEDEF
*line_def
= NULL
;
1225 rc
->top
= es
->format_rect
.top
+ (line
- es
->y_offset
) * es
->line_height
;
1226 if (line
>= es
->line_count
)
1229 line_def
= es
->first_line_def
;
1231 INT index
= es
->selection_end
- line_def
->length
;
1232 while ((index
>= 0) && line_def
->next
) {
1233 line_index
+= line_def
->length
;
1234 line_def
= line_def
->next
;
1235 index
-= line_def
->length
;
1239 line_index
+= line_def
->length
;
1240 line_def
= line_def
->next
;
1244 ssa
= line_def
->ssa
;
1249 rc
->top
= es
->format_rect
.top
;
1253 rc
->bottom
= rc
->top
+ es
->line_height
;
1254 pt1
= (scol
== 0) ? es
->format_rect
.left
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ scol
, TRUE
));
1255 pt2
= (ecol
== -1) ? es
->format_rect
.right
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ ecol
, TRUE
));
1258 ScriptStringCPtoX(ssa
, scol
, FALSE
, &pt3
);
1259 pt3
+=es
->format_rect
.left
;
1262 rc
->right
= max(max(pt1
, pt2
),pt3
);
1263 rc
->left
= min(min(pt1
, pt2
),pt3
);
1267 static inline void text_buffer_changed(EDITSTATE
*es
)
1269 es
->text_length
= (UINT
)-1;
1271 HeapFree( GetProcessHeap(), 0, es
->logAttr
);
1273 EDIT_InvalidateUniscribeData(es
);
1276 /*********************************************************************
1280 static void EDIT_LockBuffer(EDITSTATE
*es
)
1282 if (es
->hlocapp
) return;
1287 /* FIXME: What is this ? */
1288 CHAR
*textA
= NULL
; // ReactOS Hacked! r45670
1295 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1296 textA
= LocalLock(es
->hloc32A
);
1297 //countA = strlen(textA) + 1;
1301 ERR("no buffer ... please report\n");
1305 if (textA
) //// ReactOS
1308 if(!es
->hloc32W
) return;
1312 CHAR
*textA
= LocalLock(es
->hloc32A
);
1315 UINT countW_new
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
1316 if(countW_new
> es
->buffer_size
+ 1)
1318 UINT alloc_size
= ROUND_TO_GROW(countW_new
* sizeof(WCHAR
));
1319 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es
->buffer_size
, countW_new
);
1320 hloc32W_new
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1323 es
->hloc32W
= hloc32W_new
;
1324 es
->buffer_size
= LocalSize(hloc32W_new
)/sizeof(WCHAR
) - 1;
1325 TRACE("Real new size %d+1 WCHARs\n", es
->buffer_size
);
1328 WARN("FAILED! Will synchronize partially\n");
1330 es
->text
= LocalLock(es
->hloc32W
);
1331 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, es
->text
, es
->buffer_size
+ 1);
1332 LocalUnlock(es
->hloc32A
);
1334 else es
->text
= LocalLock(es
->hloc32W
);
1340 /*********************************************************************
1345 static void EDIT_UnlockBuffer(EDITSTATE
*es
, BOOL force
)
1347 if (es
->hlocapp
) return;
1349 /* Edit window might be already destroyed */
1350 if(!IsWindow(es
->hwndSelf
))
1352 WARN("edit hwnd %p already destroyed\n", es
->hwndSelf
);
1356 if (!es
->lock_count
) {
1357 ERR("lock_count == 0 ... please report\n");
1361 ERR("es->text == 0 ... please report\n");
1365 if (force
|| (es
->lock_count
== 1)) {
1368 UINT countW
= get_text_length(es
) + 1;
1372 UINT countA_new
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, NULL
, 0, NULL
, NULL
);
1373 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1374 TRACE("%d WCHARs translated to %d bytes\n", countW
, countA_new
);
1375 countA
= LocalSize(es
->hloc32A
);
1376 if(countA_new
> countA
)
1379 UINT alloc_size
= ROUND_TO_GROW(countA_new
);
1380 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA
, alloc_size
);
1381 hloc32A_new
= LocalReAlloc(es
->hloc32A
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1384 es
->hloc32A
= hloc32A_new
;
1385 countA
= LocalSize(hloc32A_new
);
1386 TRACE("Real new size %d bytes\n", countA
);
1389 WARN("FAILED! Will synchronize partially\n");
1391 WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
,
1392 LocalLock(es
->hloc32A
), countA
, NULL
, NULL
);
1393 LocalUnlock(es
->hloc32A
);
1396 LocalUnlock(es
->hloc32W
);
1400 ERR("no buffer ... please report\n");
1408 /*********************************************************************
1412 * Try to fit size + 1 characters in the buffer.
1414 static BOOL
EDIT_MakeFit(EDITSTATE
*es
, UINT size
)
1418 if (size
<= es
->buffer_size
)
1421 TRACE("trying to ReAlloc to %d+1 characters\n", size
);
1423 /* Force edit to unlock its buffer. es->text now NULL */
1424 EDIT_UnlockBuffer(es
, TRUE
);
1427 UINT alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1428 if ((hNew32W
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
))) {
1429 TRACE("Old 32 bit handle %p, new handle %p\n", es
->hloc32W
, hNew32W
);
1430 es
->hloc32W
= hNew32W
;
1431 es
->buffer_size
= LocalSize(hNew32W
)/sizeof(WCHAR
) - 1;
1435 EDIT_LockBuffer(es
);
1437 if (es
->buffer_size
< size
) {
1438 WARN("FAILED ! We now have %d+1\n", es
->buffer_size
);
1439 EDIT_NOTIFY_PARENT(es
, EN_ERRSPACE
);
1442 TRACE("We now have %d+1\n", es
->buffer_size
);
1448 /*********************************************************************
1452 * Try to fit size + 1 bytes in the undo buffer.
1455 static BOOL
EDIT_MakeUndoFit(EDITSTATE
*es
, UINT size
)
1459 if (size
<= es
->undo_buffer_size
)
1462 TRACE("trying to ReAlloc to %d+1\n", size
);
1464 alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1465 if ((es
->undo_text
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, es
->undo_text
, alloc_size
))) {
1466 es
->undo_buffer_size
= alloc_size
/sizeof(WCHAR
) - 1;
1471 WARN("FAILED ! We now have %d+1\n", es
->undo_buffer_size
);
1477 /*********************************************************************
1479 * EDIT_UpdateTextRegion
1482 static void EDIT_UpdateTextRegion(EDITSTATE
*es
, HRGN hrgn
, BOOL bErase
)
1484 if (es
->flags
& EF_UPDATE
) {
1485 es
->flags
&= ~EF_UPDATE
;
1486 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1488 InvalidateRgn(es
->hwndSelf
, hrgn
, bErase
);
1492 /*********************************************************************
1497 static void EDIT_UpdateText(EDITSTATE
*es
, const RECT
*rc
, BOOL bErase
)
1499 if (es
->flags
& EF_UPDATE
) {
1500 es
->flags
&= ~EF_UPDATE
;
1501 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1503 InvalidateRect(es
->hwndSelf
, rc
, bErase
);
1506 /*********************************************************************
1508 * EDIT_SL_InvalidateText
1510 * Called from EDIT_InvalidateText().
1511 * Does the job for single-line controls only.
1514 static void EDIT_SL_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1519 EDIT_GetLineRect(es
, 0, start
, end
, &line_rect
);
1520 if (IntersectRect(&rc
, &line_rect
, &es
->format_rect
))
1521 EDIT_UpdateText(es
, &rc
, TRUE
);
1524 /*********************************************************************
1526 * EDIT_ML_InvalidateText
1528 * Called from EDIT_InvalidateText().
1529 * Does the job for multi-line controls only.
1532 static void EDIT_ML_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1534 INT vlc
= get_vertical_line_count(es
);
1535 INT sl
= EDIT_EM_LineFromChar(es
, start
);
1536 INT el
= EDIT_EM_LineFromChar(es
, end
);
1545 if ((el
< es
->y_offset
) || (sl
> es
->y_offset
+ vlc
))
1548 sc
= start
- EDIT_EM_LineIndex(es
, sl
);
1549 ec
= end
- EDIT_EM_LineIndex(es
, el
);
1550 if (sl
< es
->y_offset
) {
1554 if (el
> es
->y_offset
+ vlc
) {
1555 el
= es
->y_offset
+ vlc
;
1556 ec
= EDIT_EM_LineLength(es
, EDIT_EM_LineIndex(es
, el
));
1558 GetClientRect(es
->hwndSelf
, &rc1
);
1559 IntersectRect(&rcWnd
, &rc1
, &es
->format_rect
);
1561 EDIT_GetLineRect(es
, sl
, sc
, ec
, &rcLine
);
1562 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1563 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1565 EDIT_GetLineRect(es
, sl
, sc
,
1566 EDIT_EM_LineLength(es
,
1567 EDIT_EM_LineIndex(es
, sl
)),
1569 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1570 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1571 for (l
= sl
+ 1 ; l
< el
; l
++) {
1572 EDIT_GetLineRect(es
, l
, 0,
1573 EDIT_EM_LineLength(es
,
1574 EDIT_EM_LineIndex(es
, l
)),
1576 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1577 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1579 EDIT_GetLineRect(es
, el
, 0, ec
, &rcLine
);
1580 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1581 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1586 /*********************************************************************
1588 * EDIT_InvalidateText
1590 * Invalidate the text from offset start up to, but not including,
1591 * offset end. Useful for (re)painting the selection.
1592 * Regions outside the linewidth are not invalidated.
1593 * end == -1 means end == TextLength.
1594 * start and end need not be ordered.
1597 static void EDIT_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1603 end
= get_text_length(es
);
1611 if (es
->style
& ES_MULTILINE
)
1612 EDIT_ML_InvalidateText(es
, start
, end
);
1614 EDIT_SL_InvalidateText(es
, start
, end
);
1618 /*********************************************************************
1622 * note: unlike the specs say: the order of start and end
1623 * _is_ preserved in Windows. (i.e. start can be > end)
1624 * In other words: this handler is OK
1627 static void EDIT_EM_SetSel(EDITSTATE
*es
, UINT start
, UINT end
, BOOL after_wrap
)
1629 UINT old_start
= es
->selection_start
;
1630 UINT old_end
= es
->selection_end
;
1631 UINT len
= get_text_length(es
);
1633 if (start
== (UINT
)-1) {
1634 start
= es
->selection_end
;
1635 end
= es
->selection_end
;
1637 start
= min(start
, len
);
1638 end
= min(end
, len
);
1640 es
->selection_start
= start
;
1641 es
->selection_end
= end
;
1643 es
->flags
|= EF_AFTER_WRAP
;
1645 es
->flags
&= ~EF_AFTER_WRAP
;
1646 /* Compute the necessary invalidation region. */
1647 /* Note that we don't need to invalidate regions which have
1648 * "never" been selected, or those which are "still" selected.
1649 * In fact, every time we hit a selection boundary, we can
1650 * *toggle* whether we need to invalidate. Thus we can optimize by
1651 * *sorting* the interval endpoints. Let's assume that we sort them
1653 * start <= end <= old_start <= old_end
1654 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1655 * in 5 comparisons; i.e. it is impossible to do better than the
1657 ORDER_UINT(end
, old_end
);
1658 ORDER_UINT(start
, old_start
);
1659 ORDER_UINT(old_start
, old_end
);
1660 ORDER_UINT(start
, end
);
1661 /* Note that at this point 'end' and 'old_start' are not in order, but
1662 * start is definitely the min. and old_end is definitely the max. */
1663 if (end
!= old_start
)
1667 * ORDER_UINT32(end, old_start);
1668 * EDIT_InvalidateText(es, start, end);
1669 * EDIT_InvalidateText(es, old_start, old_end);
1670 * in place of the following if statement.
1671 * (That would complete the optimal five-comparison four-element sort.)
1673 if (old_start
> end
)
1675 EDIT_InvalidateText(es
, start
, end
);
1676 EDIT_InvalidateText(es
, old_start
, old_end
);
1680 EDIT_InvalidateText(es
, start
, old_start
);
1681 EDIT_InvalidateText(es
, end
, old_end
);
1684 else EDIT_InvalidateText(es
, start
, old_end
);
1688 /*********************************************************************
1690 * EDIT_UpdateScrollInfo
1693 static void EDIT_UpdateScrollInfo(EDITSTATE
*es
)
1695 if ((es
->style
& WS_VSCROLL
) && !(es
->flags
& EF_VSCROLL_TRACK
))
1698 si
.cbSize
= sizeof(SCROLLINFO
);
1699 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1701 si
.nMax
= es
->line_count
- 1;
1702 si
.nPage
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1703 si
.nPos
= es
->y_offset
;
1704 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1705 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1706 SetScrollInfo(es
->hwndSelf
, SB_VERT
, &si
, TRUE
);
1709 if ((es
->style
& WS_HSCROLL
) && !(es
->flags
& EF_HSCROLL_TRACK
))
1712 si
.cbSize
= sizeof(SCROLLINFO
);
1713 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1715 si
.nMax
= es
->text_width
- 1;
1716 si
.nPage
= es
->format_rect
.right
- es
->format_rect
.left
;
1717 si
.nPos
= es
->x_offset
;
1718 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1719 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1720 SetScrollInfo(es
->hwndSelf
, SB_HORZ
, &si
, TRUE
);
1725 /*********************************************************************
1727 * EDIT_EM_LineScroll_internal
1729 * Version of EDIT_EM_LineScroll for internal use.
1730 * It doesn't refuse if ES_MULTILINE is set and assumes that
1731 * dx is in pixels, dy - in lines.
1734 static BOOL
EDIT_EM_LineScroll_internal(EDITSTATE
*es
, INT dx
, INT dy
)
1737 INT x_offset_in_pixels
;
1738 INT lines_per_page
= (es
->format_rect
.bottom
- es
->format_rect
.top
) /
1741 if (es
->style
& ES_MULTILINE
)
1743 x_offset_in_pixels
= es
->x_offset
;
1748 x_offset_in_pixels
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->x_offset
, FALSE
));
1751 if (-dx
> x_offset_in_pixels
)
1752 dx
= -x_offset_in_pixels
;
1753 if (dx
> es
->text_width
- x_offset_in_pixels
)
1754 dx
= es
->text_width
- x_offset_in_pixels
;
1755 nyoff
= max(0, es
->y_offset
+ dy
);
1756 if (nyoff
>= es
->line_count
- lines_per_page
)
1757 nyoff
= max(0, es
->line_count
- lines_per_page
);
1758 dy
= (es
->y_offset
- nyoff
) * es
->line_height
;
1763 es
->y_offset
= nyoff
;
1764 if(es
->style
& ES_MULTILINE
)
1767 es
->x_offset
+= dx
/ es
->char_width
;
1769 GetClientRect(es
->hwndSelf
, &rc1
);
1770 IntersectRect(&rc
, &rc1
, &es
->format_rect
);
1771 ScrollWindowEx(es
->hwndSelf
, -dx
, dy
,
1772 NULL
, &rc
, NULL
, NULL
, SW_INVALIDATE
);
1773 /* force scroll info update */
1774 EDIT_UpdateScrollInfo(es
);
1776 if (dx
&& !(es
->flags
& EF_HSCROLL_TRACK
))
1777 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
1778 if (dy
&& !(es
->flags
& EF_VSCROLL_TRACK
))
1779 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
1783 /*********************************************************************
1787 * NOTE: dx is in average character widths, dy - in lines;
1790 static BOOL
EDIT_EM_LineScroll(EDITSTATE
*es
, INT dx
, INT dy
)
1792 if (!(es
->style
& ES_MULTILINE
))
1795 dx
*= es
->char_width
;
1796 return EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1800 /*********************************************************************
1805 static LRESULT
EDIT_EM_Scroll(EDITSTATE
*es
, INT action
)
1809 if (!(es
->style
& ES_MULTILINE
))
1810 return (LRESULT
)FALSE
;
1820 if (es
->y_offset
< es
->line_count
- 1)
1825 dy
= -(es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1828 if (es
->y_offset
< es
->line_count
- 1)
1829 dy
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1832 return (LRESULT
)FALSE
;
1835 INT vlc
= get_vertical_line_count(es
);
1836 /* check if we are going to move too far */
1837 if(es
->y_offset
+ dy
> es
->line_count
- vlc
)
1838 dy
= max(es
->line_count
- vlc
, 0) - es
->y_offset
;
1840 /* Notification is done in EDIT_EM_LineScroll */
1842 EDIT_EM_LineScroll(es
, 0, dy
);
1843 return MAKELONG(dy
, TRUE
);
1847 return (LRESULT
)FALSE
;
1851 /*********************************************************************
1856 static void EDIT_SetCaretPos(EDITSTATE
*es
, INT pos
,
1859 LRESULT res
= EDIT_EM_PosFromChar(es
, pos
, after_wrap
);
1860 TRACE("%d - %dx%d\n", pos
, (short)LOWORD(res
), (short)HIWORD(res
));
1861 SetCaretPos((short)LOWORD(res
), (short)HIWORD(res
));
1865 /*********************************************************************
1870 static void EDIT_EM_ScrollCaret(EDITSTATE
*es
)
1872 if (es
->style
& ES_MULTILINE
) {
1876 INT cw
= es
->char_width
;
1881 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1882 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
));
1883 vlc
= get_vertical_line_count(es
);
1884 if (l
>= es
->y_offset
+ vlc
)
1885 dy
= l
- vlc
+ 1 - es
->y_offset
;
1886 if (l
< es
->y_offset
)
1887 dy
= l
- es
->y_offset
;
1888 ww
= es
->format_rect
.right
- es
->format_rect
.left
;
1889 if (x
< es
->format_rect
.left
)
1890 dx
= x
- es
->format_rect
.left
- ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1891 if (x
> es
->format_rect
.right
)
1892 dx
= x
- es
->format_rect
.left
- (HSCROLL_FRACTION
- 1) * ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1893 if (dy
|| dx
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1895 /* check if we are going to move too far */
1896 if(es
->x_offset
+ dx
+ ww
> es
->text_width
)
1897 dx
= es
->text_width
- ww
- es
->x_offset
;
1898 if(dx
|| dy
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1899 EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1906 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1907 format_width
= es
->format_rect
.right
- es
->format_rect
.left
;
1908 if (x
< es
->format_rect
.left
) {
1909 goal
= es
->format_rect
.left
+ format_width
/ HSCROLL_FRACTION
;
1912 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1913 } while ((x
< goal
) && es
->x_offset
);
1914 /* FIXME: use ScrollWindow() somehow to improve performance */
1915 EDIT_UpdateText(es
, NULL
, TRUE
);
1916 } else if (x
> es
->format_rect
.right
) {
1918 INT len
= get_text_length(es
);
1919 goal
= es
->format_rect
.right
- format_width
/ HSCROLL_FRACTION
;
1922 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1923 x_last
= (short)LOWORD(EDIT_EM_PosFromChar(es
, len
, FALSE
));
1924 } while ((x
> goal
) && (x_last
> es
->format_rect
.right
));
1925 /* FIXME: use ScrollWindow() somehow to improve performance */
1926 EDIT_UpdateText(es
, NULL
, TRUE
);
1930 if(es
->flags
& EF_FOCUSED
)
1931 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
1935 /*********************************************************************
1940 static void EDIT_MoveBackward(EDITSTATE
*es
, BOOL extend
)
1942 INT e
= es
->selection_end
;
1946 if ((es
->style
& ES_MULTILINE
) && e
&&
1947 (es
->text
[e
- 1] == '\r') && (es
->text
[e
] == '\n')) {
1949 if (e
&& (es
->text
[e
- 1] == '\r'))
1953 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1954 EDIT_EM_ScrollCaret(es
);
1958 /*********************************************************************
1962 * Only for multi line controls
1963 * Move the caret one line down, on a column with the nearest
1964 * x coordinate on the screen (might be a different column).
1967 static void EDIT_MoveDown_ML(EDITSTATE
*es
, BOOL extend
)
1969 INT s
= es
->selection_start
;
1970 INT e
= es
->selection_end
;
1971 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
1972 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
1973 INT x
= (short)LOWORD(pos
);
1974 INT y
= (short)HIWORD(pos
);
1976 e
= EDIT_CharFromPos(es
, x
, y
+ es
->line_height
, &after_wrap
);
1979 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
1980 EDIT_EM_ScrollCaret(es
);
1984 /*********************************************************************
1989 static void EDIT_MoveEnd(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
1991 BOOL after_wrap
= FALSE
;
1994 /* Pass a high value in x to make sure of receiving the end of the line */
1995 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
1996 e
= EDIT_CharFromPos(es
, 0x3fffffff,
1997 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), &after_wrap
);
1999 e
= get_text_length(es
);
2000 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, after_wrap
);
2001 EDIT_EM_ScrollCaret(es
);
2005 /*********************************************************************
2010 static void EDIT_MoveForward(EDITSTATE
*es
, BOOL extend
)
2012 INT e
= es
->selection_end
;
2016 if ((es
->style
& ES_MULTILINE
) && (es
->text
[e
- 1] == '\r')) {
2017 if (es
->text
[e
] == '\n')
2019 else if ((es
->text
[e
] == '\r') && (es
->text
[e
+ 1] == '\n'))
2023 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
2024 EDIT_EM_ScrollCaret(es
);
2028 /*********************************************************************
2032 * Home key: move to beginning of line.
2035 static void EDIT_MoveHome(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
2039 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2040 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
2041 e
= EDIT_CharFromPos(es
, -es
->x_offset
,
2042 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), NULL
);
2045 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
2046 EDIT_EM_ScrollCaret(es
);
2050 /*********************************************************************
2052 * EDIT_MovePageDown_ML
2054 * Only for multi line controls
2055 * Move the caret one page down, on a column with the nearest
2056 * x coordinate on the screen (might be a different column).
2059 static void EDIT_MovePageDown_ML(EDITSTATE
*es
, BOOL extend
)
2061 INT s
= es
->selection_start
;
2062 INT e
= es
->selection_end
;
2063 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2064 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2065 INT x
= (short)LOWORD(pos
);
2066 INT y
= (short)HIWORD(pos
);
2068 e
= EDIT_CharFromPos(es
, x
,
2069 y
+ (es
->format_rect
.bottom
- es
->format_rect
.top
),
2073 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2074 EDIT_EM_ScrollCaret(es
);
2078 /*********************************************************************
2080 * EDIT_MovePageUp_ML
2082 * Only for multi line controls
2083 * Move the caret one page up, on a column with the nearest
2084 * x coordinate on the screen (might be a different column).
2087 static void EDIT_MovePageUp_ML(EDITSTATE
*es
, BOOL extend
)
2089 INT s
= es
->selection_start
;
2090 INT e
= es
->selection_end
;
2091 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2092 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2093 INT x
= (short)LOWORD(pos
);
2094 INT y
= (short)HIWORD(pos
);
2096 e
= EDIT_CharFromPos(es
, x
,
2097 y
- (es
->format_rect
.bottom
- es
->format_rect
.top
),
2101 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2102 EDIT_EM_ScrollCaret(es
);
2106 /*********************************************************************
2110 * Only for multi line controls
2111 * Move the caret one line up, on a column with the nearest
2112 * x coordinate on the screen (might be a different column).
2115 static void EDIT_MoveUp_ML(EDITSTATE
*es
, BOOL extend
)
2117 INT s
= es
->selection_start
;
2118 INT e
= es
->selection_end
;
2119 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2120 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2121 INT x
= (short)LOWORD(pos
);
2122 INT y
= (short)HIWORD(pos
);
2124 e
= EDIT_CharFromPos(es
, x
, y
- es
->line_height
, &after_wrap
);
2127 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2128 EDIT_EM_ScrollCaret(es
);
2132 /*********************************************************************
2134 * EDIT_MoveWordBackward
2137 static void EDIT_MoveWordBackward(EDITSTATE
*es
, BOOL extend
)
2139 INT s
= es
->selection_start
;
2140 INT e
= es
->selection_end
;
2145 l
= EDIT_EM_LineFromChar(es
, e
);
2146 ll
= EDIT_EM_LineLength(es
, e
);
2147 li
= EDIT_EM_LineIndex(es
, l
);
2150 li
= EDIT_EM_LineIndex(es
, l
- 1);
2151 e
= li
+ EDIT_EM_LineLength(es
, li
);
2154 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
2158 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2159 EDIT_EM_ScrollCaret(es
);
2163 /*********************************************************************
2165 * EDIT_MoveWordForward
2168 static void EDIT_MoveWordForward(EDITSTATE
*es
, BOOL extend
)
2170 INT s
= es
->selection_start
;
2171 INT e
= es
->selection_end
;
2176 l
= EDIT_EM_LineFromChar(es
, e
);
2177 ll
= EDIT_EM_LineLength(es
, e
);
2178 li
= EDIT_EM_LineIndex(es
, l
);
2180 if ((es
->style
& ES_MULTILINE
) && (l
!= es
->line_count
- 1))
2181 e
= EDIT_EM_LineIndex(es
, l
+ 1);
2183 e
= li
+ EDIT_CallWordBreakProc(es
,
2184 li
, e
- li
+ 1, ll
, WB_RIGHT
);
2188 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2189 EDIT_EM_ScrollCaret(es
);
2193 /*********************************************************************
2198 static INT
EDIT_PaintText(EDITSTATE
*es
, HDC dc
, INT x
, INT y
, INT line
, INT col
, INT count
, BOOL rev
)
2202 LOGFONTW underline_font
;
2203 HFONT hUnderline
= 0;
2212 BkMode
= GetBkMode(dc
);
2213 BkColor
= GetBkColor(dc
);
2214 TextColor
= GetTextColor(dc
);
2216 if (es
->composition_len
== 0)
2218 SetBkColor(dc
, GetSysColor(COLOR_HIGHLIGHT
));
2219 SetTextColor(dc
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
2220 SetBkMode( dc
, OPAQUE
);
2224 HFONT current
= GetCurrentObject(dc
,OBJ_FONT
);
2225 GetObjectW(current
,sizeof(LOGFONTW
),&underline_font
);
2226 underline_font
.lfUnderline
= TRUE
;
2227 hUnderline
= CreateFontIndirectW(&underline_font
);
2228 old_font
= SelectObject(dc
,hUnderline
);
2231 li
= EDIT_EM_LineIndex(es
, line
);
2232 if (es
->style
& ES_MULTILINE
) {
2233 ret
= (INT
)LOWORD(TabbedTextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
,
2234 es
->tabs_count
, es
->tabs
, es
->format_rect
.left
- es
->x_offset
));
2236 TextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
);
2237 GetTextExtentPoint32W(dc
, es
->text
+ li
+ col
, count
, &size
);
2241 if (es
->composition_len
== 0)
2243 SetBkColor(dc
, BkColor
);
2244 SetTextColor(dc
, TextColor
);
2245 SetBkMode( dc
, BkMode
);
2250 SelectObject(dc
,old_font
);
2252 DeleteObject(hUnderline
);
2259 /*********************************************************************
2264 static void EDIT_PaintLine(EDITSTATE
*es
, HDC dc
, INT line
, BOOL rev
)
2273 SCRIPT_STRING_ANALYSIS ssa
;
2275 if (es
->style
& ES_MULTILINE
) {
2276 INT vlc
= get_vertical_line_count(es
);
2278 if ((line
< es
->y_offset
) || (line
> es
->y_offset
+ vlc
) || (line
>= es
->line_count
))
2283 TRACE("line=%d\n", line
);
2285 ssa
= EDIT_UpdateUniscribeData(es
, dc
, line
);
2286 pos
= EDIT_EM_PosFromChar(es
, EDIT_EM_LineIndex(es
, line
), FALSE
);
2287 x
= (short)LOWORD(pos
);
2288 y
= (short)HIWORD(pos
);
2290 if (es
->style
& ES_MULTILINE
)
2292 int line_idx
= line
;
2294 if (es
->style
& ES_RIGHT
|| es
->style
& ES_CENTER
)
2296 LINEDEF
*line_def
= es
->first_line_def
;
2299 while (line_def
&& line_idx
)
2301 line_def
= line_def
->next
;
2304 w
= es
->format_rect
.right
- es
->format_rect
.left
;
2305 lw
= line_def
->width
;
2307 if (es
->style
& ES_RIGHT
)
2309 else if (es
->style
& ES_CENTER
)
2312 x
+= es
->format_rect
.left
;
2317 li
= EDIT_EM_LineIndex(es
, line
);
2318 ll
= EDIT_EM_LineLength(es
, li
);
2319 s
= min(es
->selection_start
, es
->selection_end
);
2320 e
= max(es
->selection_start
, es
->selection_end
);
2321 s
= min(li
+ ll
, max(li
, s
));
2322 e
= min(li
+ ll
, max(li
, e
));
2326 ScriptStringOut(ssa
, x
, y
, 0, &es
->format_rect
, s
- li
, e
- li
, FALSE
);
2327 else if (rev
&& (s
!= e
) &&
2328 ((es
->flags
& EF_FOCUSED
) || (es
->style
& ES_NOHIDESEL
))) {
2329 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, s
- li
, FALSE
);
2330 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, s
- li
, e
- s
, TRUE
);
2331 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, e
- li
, li
+ ll
- e
, FALSE
);
2333 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, ll
, FALSE
);
2337 /*********************************************************************
2339 * EDIT_AdjustFormatRect
2341 * Adjusts the format rectangle for the current font and the
2342 * current client rectangle.
2345 static void EDIT_AdjustFormatRect(EDITSTATE
*es
)
2349 es
->format_rect
.right
= max(es
->format_rect
.right
, es
->format_rect
.left
+ es
->char_width
);
2350 if (es
->style
& ES_MULTILINE
)
2352 INT fw
, vlc
, max_x_offset
, max_y_offset
;
2354 vlc
= get_vertical_line_count(es
);
2355 es
->format_rect
.bottom
= es
->format_rect
.top
+ vlc
* es
->line_height
;
2357 /* correct es->x_offset */
2358 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2359 max_x_offset
= es
->text_width
- fw
;
2360 if(max_x_offset
< 0) max_x_offset
= 0;
2361 if(es
->x_offset
> max_x_offset
)
2362 es
->x_offset
= max_x_offset
;
2364 /* correct es->y_offset */
2365 max_y_offset
= es
->line_count
- vlc
;
2366 if(max_y_offset
< 0) max_y_offset
= 0;
2367 if(es
->y_offset
> max_y_offset
)
2368 es
->y_offset
= max_y_offset
;
2370 /* force scroll info update */
2371 EDIT_UpdateScrollInfo(es
);
2374 /* Windows doesn't care to fix text placement for SL controls */
2375 es
->format_rect
.bottom
= es
->format_rect
.top
+ es
->line_height
;
2377 /* Always stay within the client area */
2378 GetClientRect(es
->hwndSelf
, &ClientRect
);
2379 es
->format_rect
.bottom
= min(es
->format_rect
.bottom
, ClientRect
.bottom
);
2381 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
))
2382 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2384 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
2388 /*********************************************************************
2392 * note: this is not (exactly) the handler called on EM_SETRECTNP
2393 * it is also used to set the rect of a single line control
2396 static void EDIT_SetRectNP(EDITSTATE
*es
, const RECT
*rc
)
2400 ExStyle
= GetWindowLongPtrW(es
->hwndSelf
, GWL_EXSTYLE
);
2402 CopyRect(&es
->format_rect
, rc
);
2404 if (ExStyle
& WS_EX_CLIENTEDGE
) {
2405 es
->format_rect
.left
++;
2406 es
->format_rect
.right
--;
2408 if (es
->format_rect
.bottom
- es
->format_rect
.top
2409 >= es
->line_height
+ 2)
2411 es
->format_rect
.top
++;
2412 es
->format_rect
.bottom
--;
2415 else if (es
->style
& WS_BORDER
) {
2416 bw
= GetSystemMetrics(SM_CXBORDER
) + 1;
2417 bh
= GetSystemMetrics(SM_CYBORDER
) + 1;
2418 es
->format_rect
.left
+= bw
;
2419 es
->format_rect
.right
-= bw
;
2420 if (es
->format_rect
.bottom
- es
->format_rect
.top
2421 >= es
->line_height
+ 2 * bh
)
2423 es
->format_rect
.top
+= bh
;
2424 es
->format_rect
.bottom
-= bh
;
2428 es
->format_rect
.left
+= es
->left_margin
;
2429 es
->format_rect
.right
-= es
->right_margin
;
2430 EDIT_AdjustFormatRect(es
);
2434 /*********************************************************************
2438 * returns line number (not index) in high-order word of result.
2439 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2440 * to Richedit, not to the edit control. Original documentation is valid.
2441 * FIXME: do the specs mean to return -1 if outside client area or
2442 * if outside formatting rectangle ???
2445 static LRESULT
EDIT_EM_CharFromPos(EDITSTATE
*es
, INT x
, INT y
)
2453 GetClientRect(es
->hwndSelf
, &rc
);
2454 if (!PtInRect(&rc
, pt
))
2457 index
= EDIT_CharFromPos(es
, x
, y
, NULL
);
2458 return MAKELONG(index
, EDIT_EM_LineFromChar(es
, index
));
2462 /*********************************************************************
2466 * Enable or disable soft breaks.
2468 * This means: insert or remove the soft linebreak character (\r\r\n).
2469 * Take care to check if the text still fits the buffer after insertion.
2470 * If not, notify with EN_ERRSPACE.
2473 static BOOL
EDIT_EM_FmtLines(EDITSTATE
*es
, BOOL add_eol
)
2475 es
->flags
&= ~EF_USE_SOFTBRK
;
2477 es
->flags
|= EF_USE_SOFTBRK
;
2478 FIXME("soft break enabled, not implemented\n");
2484 /*********************************************************************
2488 * Hopefully this won't fire back at us.
2489 * We always start with a fixed buffer in the local heap.
2490 * Despite of the documentation says that the local heap is used
2491 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2492 * buffer on the local heap.
2495 static HLOCAL
EDIT_EM_GetHandle(EDITSTATE
*es
)
2499 if (!(es
->style
& ES_MULTILINE
))
2503 hLocal
= es
->hloc32W
;
2509 UINT countA
, alloc_size
;
2510 TRACE("Allocating 32-bit ANSI alias buffer\n");
2511 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, NULL
, 0, NULL
, NULL
);
2512 alloc_size
= ROUND_TO_GROW(countA
);
2513 if(!(es
->hloc32A
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
2515 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size
);
2518 textA
= LocalLock(es
->hloc32A
);
2519 WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, countA
, NULL
, NULL
);
2520 LocalUnlock(es
->hloc32A
);
2522 hLocal
= es
->hloc32A
;
2525 EDIT_UnlockBuffer(es
, TRUE
);
2527 /* The text buffer handle belongs to the app */
2528 es
->hlocapp
= hLocal
;
2530 TRACE("Returning %p, LocalSize() = %ld\n", hLocal
, LocalSize(hLocal
));
2535 /*********************************************************************
2540 static INT
EDIT_EM_GetLine(EDITSTATE
*es
, INT line
, LPWSTR dst
, BOOL unicode
)
2543 INT line_len
, dst_len
;
2546 if (es
->style
& ES_MULTILINE
) {
2547 if (line
>= es
->line_count
)
2551 i
= EDIT_EM_LineIndex(es
, line
);
2553 line_len
= EDIT_EM_LineLength(es
, i
);
2554 dst_len
= *(WORD
*)dst
;
2557 if(dst_len
<= line_len
)
2559 memcpy(dst
, src
, dst_len
* sizeof(WCHAR
));
2562 else /* Append 0 if enough space */
2564 memcpy(dst
, src
, line_len
* sizeof(WCHAR
));
2571 INT ret
= WideCharToMultiByte(CP_ACP
, 0, src
, line_len
, (LPSTR
)dst
, dst_len
, NULL
, NULL
);
2572 if(!ret
&& line_len
) /* Insufficient buffer size */
2574 if(ret
< dst_len
) /* Append 0 if enough space */
2575 ((LPSTR
)dst
)[ret
] = 0;
2581 /*********************************************************************
2586 static LRESULT
EDIT_EM_GetSel(const EDITSTATE
*es
, PUINT start
, PUINT end
)
2588 UINT s
= es
->selection_start
;
2589 UINT e
= es
->selection_end
;
2596 return MAKELONG(s
, e
);
2600 /*********************************************************************
2604 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2607 static void EDIT_EM_ReplaceSel(EDITSTATE
*es
, BOOL can_undo
, LPCWSTR lpsz_replace
, BOOL send_update
, BOOL honor_limit
)
2609 UINT strl
= strlenW(lpsz_replace
);
2610 UINT tl
= get_text_length(es
);
2621 TRACE("%s, can_undo %d, send_update %d\n",
2622 debugstr_w(lpsz_replace
), can_undo
, send_update
);
2624 s
= es
->selection_start
;
2625 e
= es
->selection_end
;
2627 EDIT_InvalidateUniscribeData(es
);
2628 if ((s
== e
) && !strl
)
2633 size
= tl
- (e
- s
) + strl
;
2637 /* Issue the EN_MAXTEXT notification and continue with replacing text
2638 * so that buffer limit is honored. */
2639 if ((honor_limit
) && (size
> es
->buffer_limit
)) {
2640 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2641 /* Buffer limit can be smaller than the actual length of text in combobox */
2642 if (es
->buffer_limit
< (tl
- (e
-s
)))
2645 strl
= min(strl
, es
->buffer_limit
- (tl
- (e
-s
)));
2648 if (!EDIT_MakeFit(es
, tl
- (e
- s
) + strl
))
2652 /* there is something to be deleted */
2653 TRACE("deleting stuff.\n");
2655 buf
= HeapAlloc(GetProcessHeap(), 0, (bufl
+ 1) * sizeof(WCHAR
));
2657 memcpy(buf
, es
->text
+ s
, bufl
* sizeof(WCHAR
));
2658 buf
[bufl
] = 0; /* ensure 0 termination */
2660 strcpyW(es
->text
+ s
, es
->text
+ e
);
2661 text_buffer_changed(es
);
2664 /* there is an insertion */
2665 tl
= get_text_length(es
);
2666 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
));
2667 for (p
= es
->text
+ tl
; p
>= es
->text
+ s
; p
--)
2669 for (i
= 0 , p
= es
->text
+ s
; i
< strl
; i
++)
2670 p
[i
] = lpsz_replace
[i
];
2671 if(es
->style
& ES_UPPERCASE
)
2672 CharUpperBuffW(p
, strl
);
2673 else if(es
->style
& ES_LOWERCASE
)
2674 CharLowerBuffW(p
, strl
);
2675 text_buffer_changed(es
);
2677 if (es
->style
& ES_MULTILINE
)
2679 INT st
= min(es
->selection_start
, es
->selection_end
);
2680 INT vlc
= get_vertical_line_count(es
);
2682 hrgn
= CreateRectRgn(0, 0, 0, 0);
2683 EDIT_BuildLineDefs_ML(es
, st
, st
+ strl
,
2684 strl
- abs(es
->selection_end
- es
->selection_start
), hrgn
);
2685 /* if text is too long undo all changes */
2686 if (honor_limit
&& !(es
->style
& ES_AUTOVSCROLL
) && (es
->line_count
> vlc
)) {
2688 strcpyW(es
->text
+ e
, es
->text
+ e
+ strl
);
2690 for (i
= 0 , p
= es
->text
; i
< e
- s
; i
++)
2692 text_buffer_changed(es
);
2693 EDIT_BuildLineDefs_ML(es
, s
, e
,
2694 abs(es
->selection_end
- es
->selection_start
) - strl
, hrgn
);
2697 hrgn
= CreateRectRgn(0, 0, 0, 0);
2698 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2702 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2703 EDIT_InvalidateUniscribeData(es
);
2704 EDIT_CalcLineWidth_SL(es
);
2705 /* remove chars that don't fit */
2706 if (honor_limit
&& !(es
->style
& ES_AUTOHSCROLL
) && (es
->text_width
> fw
)) {
2707 while ((es
->text_width
> fw
) && s
+ strl
>= s
) {
2708 strcpyW(es
->text
+ s
+ strl
- 1, es
->text
+ s
+ strl
);
2710 es
->text_length
= -1;
2711 EDIT_InvalidateUniscribeData(es
);
2712 EDIT_CalcLineWidth_SL(es
);
2714 text_buffer_changed(es
);
2715 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2721 utl
= strlenW(es
->undo_text
);
2722 if (!es
->undo_insert_count
&& (*es
->undo_text
&& (s
== es
->undo_position
))) {
2723 /* undo-buffer is extended to the right */
2724 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2725 memcpy(es
->undo_text
+ utl
, buf
, (e
- s
)*sizeof(WCHAR
));
2726 (es
->undo_text
+ utl
)[e
- s
] = 0; /* ensure 0 termination */
2727 } else if (!es
->undo_insert_count
&& (*es
->undo_text
&& (e
== es
->undo_position
))) {
2728 /* undo-buffer is extended to the left */
2729 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2730 for (p
= es
->undo_text
+ utl
; p
>= es
->undo_text
; p
--)
2732 for (i
= 0 , p
= es
->undo_text
; i
< e
- s
; i
++)
2734 es
->undo_position
= s
;
2736 /* new undo-buffer */
2737 EDIT_MakeUndoFit(es
, e
- s
);
2738 memcpy(es
->undo_text
, buf
, (e
- s
)*sizeof(WCHAR
));
2739 es
->undo_text
[e
- s
] = 0; /* ensure 0 termination */
2740 es
->undo_position
= s
;
2742 /* any deletion makes the old insertion-undo invalid */
2743 es
->undo_insert_count
= 0;
2745 EDIT_EM_EmptyUndoBuffer(es
);
2749 if ((s
== es
->undo_position
) ||
2750 ((es
->undo_insert_count
) &&
2751 (s
== es
->undo_position
+ es
->undo_insert_count
)))
2753 * insertion is new and at delete position or
2754 * an extension to either left or right
2756 es
->undo_insert_count
+= strl
;
2758 /* new insertion undo */
2759 es
->undo_position
= s
;
2760 es
->undo_insert_count
= strl
;
2761 /* new insertion makes old delete-buffer invalid */
2762 *es
->undo_text
= '\0';
2765 EDIT_EM_EmptyUndoBuffer(es
);
2768 HeapFree(GetProcessHeap(), 0, buf
);
2772 /* If text has been deleted and we're right or center aligned then scroll rightward */
2773 if (es
->style
& (ES_RIGHT
| ES_CENTER
))
2775 INT delta
= strl
- abs(es
->selection_end
- es
->selection_start
);
2777 if (delta
< 0 && es
->x_offset
)
2779 if (abs(delta
) > es
->x_offset
)
2782 es
->x_offset
+= delta
;
2786 EDIT_EM_SetSel(es
, s
, s
, FALSE
);
2787 es
->flags
|= EF_MODIFIED
;
2788 if (send_update
) es
->flags
|= EF_UPDATE
;
2791 EDIT_UpdateTextRegion(es
, hrgn
, TRUE
);
2795 EDIT_UpdateText(es
, NULL
, TRUE
);
2797 EDIT_EM_ScrollCaret(es
);
2799 /* force scroll info update */
2800 EDIT_UpdateScrollInfo(es
);
2803 if(send_update
|| (es
->flags
& EF_UPDATE
))
2805 es
->flags
&= ~EF_UPDATE
;
2806 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
2808 EDIT_InvalidateUniscribeData(es
);
2812 /*********************************************************************
2816 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2819 static void EDIT_EM_SetHandle(EDITSTATE
*es
, HLOCAL hloc
)
2821 if (!(es
->style
& ES_MULTILINE
))
2825 WARN("called with NULL handle\n");
2829 EDIT_UnlockBuffer(es
, TRUE
);
2835 LocalFree(es
->hloc32A
);
2847 countA
= LocalSize(hloc
);
2848 textA
= LocalLock(hloc
);
2849 countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
2850 if(!(hloc32W_new
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, countW
* sizeof(WCHAR
))))
2852 ERR("Could not allocate new unicode buffer\n");
2855 textW
= LocalLock(hloc32W_new
);
2856 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, textW
, countW
);
2857 LocalUnlock(hloc32W_new
);
2861 LocalFree(es
->hloc32W
);
2863 es
->hloc32W
= hloc32W_new
;
2867 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
2869 /* The text buffer handle belongs to the control */
2872 EDIT_LockBuffer(es
);
2873 text_buffer_changed(es
);
2875 es
->x_offset
= es
->y_offset
= 0;
2876 es
->selection_start
= es
->selection_end
= 0;
2877 EDIT_EM_EmptyUndoBuffer(es
);
2878 es
->flags
&= ~EF_MODIFIED
;
2879 es
->flags
&= ~EF_UPDATE
;
2880 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2881 EDIT_UpdateText(es
, NULL
, TRUE
);
2882 EDIT_EM_ScrollCaret(es
);
2883 /* force scroll info update */
2884 EDIT_UpdateScrollInfo(es
);
2888 /*********************************************************************
2892 * NOTE: this version currently implements WinNT limits
2895 static void EDIT_EM_SetLimitText(EDITSTATE
*es
, UINT limit
)
2897 if (!limit
) limit
= ~0u;
2898 if (!(es
->style
& ES_MULTILINE
)) limit
= min(limit
, 0x7ffffffe);
2899 es
->buffer_limit
= limit
;
2903 /*********************************************************************
2907 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2908 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2909 * margin according to the textmetrics of the current font.
2911 * When EC_USEFONTINFO is used in the non_cjk case the margins only
2912 * change if the edit control is equal to or larger than a certain
2913 * size. Though there is an exception for the empty client rect case
2914 * with small font sizes.
2916 static BOOL
is_cjk(UINT charset
)
2920 case SHIFTJIS_CHARSET
:
2921 case HANGUL_CHARSET
:
2922 case GB2312_CHARSET
:
2923 case CHINESEBIG5_CHARSET
:
2926 /* HANGUL_CHARSET is strange, though treated as CJK by Win 8, it is
2927 * not by other versions including Win 10. */
2931 static void EDIT_EM_SetMargins(EDITSTATE
*es
, INT action
,
2932 WORD left
, WORD right
, BOOL repaint
)
2935 INT default_left_margin
= 0; /* in pixels */
2936 INT default_right_margin
= 0; /* in pixels */
2938 /* Set the default margins depending on the font */
2939 if (es
->font
&& (left
== EC_USEFONTINFO
|| right
== EC_USEFONTINFO
)) {
2940 HDC dc
= GetDC(es
->hwndSelf
);
2941 HFONT old_font
= SelectObject(dc
, es
->font
);
2942 LONG width
= GdiGetCharDimensions(dc
, &tm
, NULL
);
2945 /* The default margins are only non zero for TrueType or Vector fonts */
2946 if (tm
.tmPitchAndFamily
& ( TMPF_VECTOR
| TMPF_TRUETYPE
)) {
2947 if (!is_cjk(tm
.tmCharSet
)) {
2948 default_left_margin
= width
/ 2;
2949 default_right_margin
= width
/ 2;
2951 GetClientRect(es
->hwndSelf
, &rc
);
2952 if (rc
.right
- rc
.left
< (width
/ 2 + width
) * 2 &&
2953 (width
>= 28 || !IsRectEmpty(&rc
)) ) {
2954 default_left_margin
= es
->left_margin
;
2955 default_right_margin
= es
->right_margin
;
2958 /* FIXME: figure out the CJK values. They are not affected by the client rect. */
2959 default_left_margin
= width
/ 2;
2960 default_right_margin
= width
/ 2;
2963 SelectObject(dc
, old_font
);
2964 ReleaseDC(es
->hwndSelf
, dc
);
2967 if (action
& EC_LEFTMARGIN
) {
2968 es
->format_rect
.left
-= es
->left_margin
;
2969 if (left
!= EC_USEFONTINFO
)
2970 es
->left_margin
= left
;
2972 es
->left_margin
= default_left_margin
;
2973 es
->format_rect
.left
+= es
->left_margin
;
2976 if (action
& EC_RIGHTMARGIN
) {
2977 es
->format_rect
.right
+= es
->right_margin
;
2978 if (right
!= EC_USEFONTINFO
)
2979 es
->right_margin
= right
;
2981 es
->right_margin
= default_right_margin
;
2982 es
->format_rect
.right
-= es
->right_margin
;
2985 if (action
& (EC_LEFTMARGIN
| EC_RIGHTMARGIN
)) {
2986 EDIT_AdjustFormatRect(es
);
2987 if (repaint
) EDIT_UpdateText(es
, NULL
, TRUE
);
2990 TRACE("left=%d, right=%d\n", es
->left_margin
, es
->right_margin
);
2994 /*********************************************************************
2996 * EM_SETPASSWORDCHAR
2999 static void EDIT_EM_SetPasswordChar(EDITSTATE
*es
, WCHAR c
)
3003 if (es
->style
& ES_MULTILINE
)
3006 if (es
->password_char
== c
)
3009 style
= GetWindowLongW( es
->hwndSelf
, GWL_STYLE
);
3010 es
->password_char
= c
;
3012 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
| ES_PASSWORD
);
3013 es
->style
|= ES_PASSWORD
;
3015 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
& ~ES_PASSWORD
);
3016 es
->style
&= ~ES_PASSWORD
;
3018 EDIT_InvalidateUniscribeData(es
);
3019 EDIT_UpdateText(es
, NULL
, TRUE
);
3023 /*********************************************************************
3028 static BOOL
EDIT_EM_SetTabStops(EDITSTATE
*es
, INT count
, const INT
*tabs
)
3030 if (!(es
->style
& ES_MULTILINE
))
3032 HeapFree(GetProcessHeap(), 0, es
->tabs
);
3033 es
->tabs_count
= count
;
3037 es
->tabs
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(INT
));
3039 /* ReactOS r33503 */
3040 if (es
->tabs
== NULL
)
3046 memcpy(es
->tabs
, tabs
, count
* sizeof(INT
));
3048 EDIT_InvalidateUniscribeData(es
);
3053 /*********************************************************************
3055 * EM_SETWORDBREAKPROC
3058 static void EDIT_EM_SetWordBreakProc(EDITSTATE
*es
, void *wbp
)
3060 if (es
->word_break_proc
== wbp
)
3063 es
->word_break_proc
= wbp
;
3065 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
)) {
3066 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
3067 EDIT_UpdateText(es
, NULL
, TRUE
);
3072 /*********************************************************************
3077 static BOOL
EDIT_EM_Undo(EDITSTATE
*es
)
3082 /* As per MSDN spec, for a single-line edit control,
3083 the return value is always TRUE */
3084 if( es
->style
& ES_READONLY
)
3085 return !(es
->style
& ES_MULTILINE
);
3087 ulength
= strlenW(es
->undo_text
);
3089 utext
= HeapAlloc(GetProcessHeap(), 0, (ulength
+ 1) * sizeof(WCHAR
));
3091 /* ReactOS r33503 */
3096 strcpyW(utext
, es
->undo_text
);
3098 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3099 es
->undo_insert_count
, debugstr_w(utext
));
3101 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3102 EDIT_EM_EmptyUndoBuffer(es
);
3103 EDIT_EM_ReplaceSel(es
, TRUE
, utext
, TRUE
, TRUE
);
3104 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3105 /* send the notification after the selection start and end are set */
3106 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
3107 EDIT_EM_ScrollCaret(es
);
3108 HeapFree(GetProcessHeap(), 0, utext
);
3110 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3111 es
->undo_insert_count
, debugstr_w(es
->undo_text
));
3116 /* Helper function for WM_CHAR
3118 * According to an MSDN blog article titled "Just because you're a control
3119 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3120 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3121 * a dialog box or not.
3123 static inline BOOL
EDIT_IsInsideDialog(EDITSTATE
*es
)
3125 return (es
->flags
& EF_DIALOGMODE
);
3129 /*********************************************************************
3134 static void EDIT_WM_Paste(EDITSTATE
*es
)
3139 /* Protect read-only edit control from modification */
3140 if(es
->style
& ES_READONLY
)
3143 OpenClipboard(es
->hwndSelf
);
3144 if ((hsrc
= GetClipboardData(CF_UNICODETEXT
))) {
3145 src
= GlobalLock(hsrc
);
3146 EDIT_EM_ReplaceSel(es
, TRUE
, src
, TRUE
, TRUE
);
3149 else if (es
->style
& ES_PASSWORD
) {
3150 /* clear selected text in password edit box even with empty clipboard */
3151 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
3157 /*********************************************************************
3162 static void EDIT_WM_Copy(EDITSTATE
*es
)
3164 INT s
= min(es
->selection_start
, es
->selection_end
);
3165 INT e
= max(es
->selection_start
, es
->selection_end
);
3173 hdst
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (len
+ 1) * sizeof(WCHAR
));
3174 dst
= GlobalLock(hdst
);
3175 memcpy(dst
, es
->text
+ s
, len
* sizeof(WCHAR
));
3176 dst
[len
] = 0; /* ensure 0 termination */
3177 TRACE("%s\n", debugstr_w(dst
));
3179 OpenClipboard(es
->hwndSelf
);
3181 SetClipboardData(CF_UNICODETEXT
, hdst
);
3186 /*********************************************************************
3191 static inline void EDIT_WM_Clear(EDITSTATE
*es
)
3193 /* Protect read-only edit control from modification */
3194 if(es
->style
& ES_READONLY
)
3197 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
3201 /*********************************************************************
3206 static inline void EDIT_WM_Cut(EDITSTATE
*es
)
3213 /*********************************************************************
3218 static LRESULT
EDIT_WM_Char(EDITSTATE
*es
, WCHAR c
)
3222 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3226 /* If it's not a multiline edit box, it would be ignored below.
3227 * For multiline edit without ES_WANTRETURN, we have to make a
3230 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_WANTRETURN
))
3231 if (EDIT_IsInsideDialog(es
))
3234 if (es
->style
& ES_MULTILINE
) {
3235 if (es
->style
& ES_READONLY
) {
3236 EDIT_MoveHome(es
, FALSE
, FALSE
);
3237 EDIT_MoveDown_ML(es
, FALSE
);
3239 static const WCHAR cr_lfW
[] = {'\r','\n',0};
3240 EDIT_EM_ReplaceSel(es
, TRUE
, cr_lfW
, TRUE
, TRUE
);
3245 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_READONLY
))
3247 static const WCHAR tabW
[] = {'\t',0};
3248 if (EDIT_IsInsideDialog(es
))
3250 EDIT_EM_ReplaceSel(es
, TRUE
, tabW
, TRUE
, TRUE
);
3254 if (!(es
->style
& ES_READONLY
) && !control
) {
3255 if (es
->selection_start
!= es
->selection_end
)
3258 /* delete character left of caret */
3259 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3260 EDIT_MoveBackward(es
, TRUE
);
3266 if (!(es
->style
& ES_PASSWORD
))
3267 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3270 if (!(es
->style
& ES_READONLY
))
3271 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3274 if (!((es
->style
& ES_READONLY
) || (es
->style
& ES_PASSWORD
)))
3275 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3278 if (!(es
->style
& ES_READONLY
))
3279 SendMessageW(es
->hwndSelf
, WM_UNDO
, 0, 0);
3283 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3284 if( (es
->style
& ES_NUMBER
) && !( c
>= '0' && c
<= '9') )
3287 if (!(es
->style
& ES_READONLY
) && (c
>= ' ') && (c
!= 127)) {
3291 EDIT_EM_ReplaceSel(es
, TRUE
, str
, TRUE
, TRUE
);
3299 /*********************************************************************
3301 * EDIT_ContextMenuCommand
3304 static void EDIT_ContextMenuCommand(EDITSTATE
*es
, UINT id
)
3308 SendMessageW(es
->hwndSelf
, WM_UNDO
, 0, 0);
3311 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3314 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3317 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3320 SendMessageW(es
->hwndSelf
, WM_CLEAR
, 0, 0);
3323 SendMessageW(es
->hwndSelf
, EM_SETSEL
, 0, -1);
3326 ERR("unknown menu item, please report\n");
3332 /*********************************************************************
3336 * Note: the resource files resource/sysres_??.rc cannot define a
3337 * single popup menu. Hence we use a (dummy) menubar
3338 * containing the single popup menu as its first item.
3340 * FIXME: the message identifiers have been chosen arbitrarily,
3341 * hence we use MF_BYPOSITION.
3342 * We might as well use the "real" values (anybody knows ?)
3343 * The menu definition is in resources/sysres_??.rc.
3344 * Once these are OK, we better use MF_BYCOMMAND here
3345 * (as we do in EDIT_WM_Command()).
3348 static void EDIT_WM_ContextMenu(EDITSTATE
*es
, INT x
, INT y
)
3350 HMENU menu
= LoadMenuA(user32_module
, "EDITMENU");
3351 HMENU popup
= GetSubMenu(menu
, 0);
3352 UINT start
= es
->selection_start
;
3353 UINT end
= es
->selection_end
;
3356 ORDER_UINT(start
, end
);
3359 EnableMenuItem(popup
, 0, MF_BYPOSITION
| (EDIT_EM_CanUndo(es
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3361 EnableMenuItem(popup
, 2, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3363 EnableMenuItem(popup
, 3, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) ? MF_ENABLED
: MF_GRAYED
));
3365 EnableMenuItem(popup
, 4, MF_BYPOSITION
| (IsClipboardFormatAvailable(CF_UNICODETEXT
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3367 EnableMenuItem(popup
, 5, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3369 EnableMenuItem(popup
, 7, MF_BYPOSITION
| (start
|| (end
!= get_text_length(es
)) ? MF_ENABLED
: MF_GRAYED
));
3371 if (x
== -1 && y
== -1) /* passed via VK_APPS press/release */
3374 /* Windows places the menu at the edit's center in this case */
3376 /* ReactOS r55202 */
3377 GetClientRect(es
->hwndSelf
, &rc
);
3378 MapWindowPoints(es
->hwndSelf
, 0, (POINT
*)&rc
, 2);
3380 WIN_GetRectangles( es
->hwndSelf
, COORDS_SCREEN
, NULL
, &rc
);
3382 x
= rc
.left
+ (rc
.right
- rc
.left
) / 2;
3383 y
= rc
.top
+ (rc
.bottom
- rc
.top
) / 2;
3386 if (!(es
->flags
& EF_FOCUSED
))
3387 SetFocus(es
->hwndSelf
);
3389 cmd
= TrackPopupMenu(popup
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
| TPM_RETURNCMD
| TPM_NONOTIFY
,
3390 x
, y
, 0, es
->hwndSelf
, NULL
);
3393 EDIT_ContextMenuCommand(es
, cmd
);
3399 /*********************************************************************
3404 static INT
EDIT_WM_GetText(const EDITSTATE
*es
, INT count
, LPWSTR dst
, BOOL unicode
)
3406 if(!count
) return 0;
3410 lstrcpynW(dst
, es
->text
, count
);
3411 return strlenW(dst
);
3415 LPSTR textA
= (LPSTR
)dst
;
3416 if (!WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, count
, NULL
, NULL
))
3417 textA
[count
- 1] = 0; /* ensure 0 termination */
3418 return strlen(textA
);
3422 /*********************************************************************
3427 static BOOL
EDIT_CheckCombo(EDITSTATE
*es
, UINT msg
, INT key
)
3429 HWND hLBox
= es
->hwndListBox
;
3437 hCombo
= GetParent(es
->hwndSelf
);
3441 TRACE_(combo
)("[%p]: handling msg %x (%x)\n", es
->hwndSelf
, msg
, key
);
3443 if (key
== VK_UP
|| key
== VK_DOWN
)
3445 if (SendMessageW(hCombo
, CB_GETEXTENDEDUI
, 0, 0))
3448 if (msg
== WM_KEYDOWN
|| nEUI
)
3449 bDropped
= (BOOL
)SendMessageW(hCombo
, CB_GETDROPPEDSTATE
, 0, 0);
3455 if (!bDropped
&& nEUI
&& (key
== VK_UP
|| key
== VK_DOWN
))
3457 /* make sure ComboLBox pops up */
3458 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, FALSE
, 0);
3463 SendMessageW(hLBox
, WM_KEYDOWN
, key
, 0);
3466 case WM_SYSKEYDOWN
: /* Handle Alt+up/down arrows */
3468 SendMessageW(hCombo
, CB_SHOWDROPDOWN
, !bDropped
, 0);
3470 SendMessageW(hLBox
, WM_KEYDOWN
, VK_F4
, 0);
3475 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, TRUE
, 0);
3481 /*********************************************************************
3485 * Handling of special keys that don't produce a WM_CHAR
3486 * (i.e. non-printable keys) & Backspace & Delete
3489 static LRESULT
EDIT_WM_KeyDown(EDITSTATE
*es
, INT key
)
3494 if (GetKeyState(VK_MENU
) & 0x8000)
3497 shift
= GetKeyState(VK_SHIFT
) & 0x8000;
3498 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3503 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
) || key
== VK_F4
)
3508 if ((es
->style
& ES_MULTILINE
) && (key
== VK_UP
))
3509 EDIT_MoveUp_ML(es
, shift
);
3512 EDIT_MoveWordBackward(es
, shift
);
3514 EDIT_MoveBackward(es
, shift
);
3517 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
))
3521 if ((es
->style
& ES_MULTILINE
) && (key
== VK_DOWN
))
3522 EDIT_MoveDown_ML(es
, shift
);
3524 EDIT_MoveWordForward(es
, shift
);
3526 EDIT_MoveForward(es
, shift
);
3529 EDIT_MoveHome(es
, shift
, control
);
3532 EDIT_MoveEnd(es
, shift
, control
);
3535 if (es
->style
& ES_MULTILINE
)
3536 EDIT_MovePageUp_ML(es
, shift
);
3538 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
3541 if (es
->style
& ES_MULTILINE
)
3542 EDIT_MovePageDown_ML(es
, shift
);
3544 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
3547 if (!(es
->style
& ES_READONLY
) && !(shift
&& control
)) {
3548 if (es
->selection_start
!= es
->selection_end
) {
3555 /* delete character left of caret */
3556 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3557 EDIT_MoveBackward(es
, TRUE
);
3559 } else if (control
) {
3560 /* delete to end of line */
3561 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3562 EDIT_MoveEnd(es
, TRUE
, FALSE
);
3565 /* delete character right of caret */
3566 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3567 EDIT_MoveForward(es
, TRUE
);
3575 if (!(es
->style
& ES_READONLY
))
3581 /* If the edit doesn't want the return send a message to the default object */
3582 if(!(es
->style
& ES_MULTILINE
) || !(es
->style
& ES_WANTRETURN
))
3586 if (!EDIT_IsInsideDialog(es
)) break;
3588 dw
= SendMessageW(es
->hwndParent
, DM_GETDEFID
, 0, 0);
3589 if (HIWORD(dw
) == DC_HASDEFID
)
3591 HWND hwDefCtrl
= GetDlgItem(es
->hwndParent
, LOWORD(dw
));
3594 SendMessageW(es
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
3595 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
3601 if ((es
->style
& ES_MULTILINE
) && EDIT_IsInsideDialog(es
))
3602 PostMessageW(es
->hwndParent
, WM_CLOSE
, 0, 0);
3605 if ((es
->style
& ES_MULTILINE
) && EDIT_IsInsideDialog(es
))
3606 SendMessageW(es
->hwndParent
, WM_NEXTDLGCTL
, shift
, 0);
3609 /* ReactOS CORE-1419 */
3613 FIXME("Ctrl+Backspace\n");
3622 /*********************************************************************
3627 static LRESULT
EDIT_WM_KillFocus(EDITSTATE
*es
)
3632 es
->flags
&= ~EF_FOCUSED
;
3634 if(!(es
->style
& ES_NOHIDESEL
))
3635 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
3637 /* throw away left over scroll when we lose focus */
3638 es
->wheelDeltaRemainder
= 0;
3640 if (es
->hwndListBox
== NULL
)
3641 EDIT_NOTIFY_PARENT(es
, EN_KILLFOCUS
);
3643 { /* send the undocumented WM_CBLOSTTEXTFOCUS message to combobox */
3644 hCombo
= GetParent(es
->hwndSelf
);
3645 lStyles
= GetWindowLong(hCombo
, GWL_STYLE
);
3646 if ((lStyles
& CBS_DROPDOWN
) || (lStyles
& CBS_SIMPLE
))
3647 SendMessage(hCombo
, WM_CBLOSTTEXTFOCUS
, 0, 0);
3654 /*********************************************************************
3658 * The caret position has been set on the WM_LBUTTONDOWN message
3661 static LRESULT
EDIT_WM_LButtonDblClk(EDITSTATE
*es
)
3664 INT e
= es
->selection_end
;
3669 es
->bCaptureState
= TRUE
;
3670 SetCapture(es
->hwndSelf
);
3672 l
= EDIT_EM_LineFromChar(es
, e
);
3673 li
= EDIT_EM_LineIndex(es
, l
);
3674 ll
= EDIT_EM_LineLength(es
, e
);
3675 s
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
3676 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_RIGHT
);
3677 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
3678 EDIT_EM_ScrollCaret(es
);
3679 es
->region_posx
= es
->region_posy
= 0;
3680 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
3685 /*********************************************************************
3690 static LRESULT
EDIT_WM_LButtonDown(EDITSTATE
*es
, DWORD keys
, INT x
, INT y
)
3695 es
->bCaptureState
= TRUE
;
3696 SetCapture(es
->hwndSelf
);
3697 EDIT_ConfinePoint(es
, &x
, &y
);
3698 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
3699 EDIT_EM_SetSel(es
, (keys
& MK_SHIFT
) ? es
->selection_start
: e
, e
, after_wrap
);
3700 EDIT_EM_ScrollCaret(es
);
3701 es
->region_posx
= es
->region_posy
= 0;
3702 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
3704 if (!(es
->flags
& EF_FOCUSED
))
3705 SetFocus(es
->hwndSelf
);
3711 /*********************************************************************
3716 static LRESULT
EDIT_WM_LButtonUp(EDITSTATE
*es
)
3718 if (es
->bCaptureState
) {
3719 KillTimer(es
->hwndSelf
, 0);
3720 if (GetCapture() == es
->hwndSelf
) ReleaseCapture();
3722 es
->bCaptureState
= FALSE
;
3727 /*********************************************************************
3732 static LRESULT
EDIT_WM_MButtonDown(EDITSTATE
*es
)
3734 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3739 /*********************************************************************
3744 static LRESULT
EDIT_WM_MouseMove(EDITSTATE
*es
, INT x
, INT y
)
3750 /* If the mouse has been captured by process other than the edit control itself,
3751 * the windows edit controls will not select the strings with mouse move.
3753 if (!es
->bCaptureState
|| GetCapture() != es
->hwndSelf
)
3757 * FIXME: gotta do some scrolling if outside client
3758 * area. Maybe reset the timer ?
3761 EDIT_ConfinePoint(es
, &x
, &y
);
3762 es
->region_posx
= (prex
< x
) ? -1 : ((prex
> x
) ? 1 : 0);
3763 es
->region_posy
= (prey
< y
) ? -1 : ((prey
> y
) ? 1 : 0);
3764 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
3765 EDIT_EM_SetSel(es
, es
->selection_start
, e
, after_wrap
);
3766 EDIT_SetCaretPos(es
,es
->selection_end
,es
->flags
& EF_AFTER_WRAP
);
3771 /*********************************************************************
3776 static void EDIT_WM_Paint(EDITSTATE
*es
, HDC hdc
)
3789 BOOL rev
= es
->bEnableState
&&
3790 ((es
->flags
& EF_FOCUSED
) ||
3791 (es
->style
& ES_NOHIDESEL
));
3792 dc
= hdc
? hdc
: BeginPaint(es
->hwndSelf
, &ps
);
3794 /* The dc we use for calculating may not be the one we paint into.
3795 This is the safest action. */
3796 EDIT_InvalidateUniscribeData(es
);
3797 GetClientRect(es
->hwndSelf
, &rcClient
);
3799 /* get the background brush */
3800 brush
= EDIT_NotifyCtlColor(es
, dc
);
3802 /* paint the border and the background */
3803 IntersectClipRect(dc
, rcClient
.left
, rcClient
.top
, rcClient
.right
, rcClient
.bottom
);
3805 if(es
->style
& WS_BORDER
) {
3806 bw
= GetSystemMetrics(SM_CXBORDER
);
3807 bh
= GetSystemMetrics(SM_CYBORDER
);
3809 if(es
->style
& ES_MULTILINE
) {
3810 if(es
->style
& WS_HSCROLL
) rc
.bottom
+=bh
;
3811 if(es
->style
& WS_VSCROLL
) rc
.right
+=bw
;
3814 /* Draw the frame. Same code as in nonclient.c */
3815 old_brush
= SelectObject(dc
, GetSysColorBrush(COLOR_WINDOWFRAME
));
3816 PatBlt(dc
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, bh
, PATCOPY
);
3817 PatBlt(dc
, rc
.left
, rc
.top
, bw
, rc
.bottom
- rc
.top
, PATCOPY
);
3818 PatBlt(dc
, rc
.left
, rc
.bottom
- 1, rc
.right
- rc
.left
, -bw
, PATCOPY
);
3819 PatBlt(dc
, rc
.right
- 1, rc
.top
, -bw
, rc
.bottom
- rc
.top
, PATCOPY
);
3820 SelectObject(dc
, old_brush
);
3822 /* Keep the border clean */
3823 IntersectClipRect(dc
, rc
.left
+bw
, rc
.top
+bh
,
3824 max(rc
.right
-bw
, rc
.left
+bw
), max(rc
.bottom
-bh
, rc
.top
+bh
));
3827 GetClipBox(dc
, &rc
);
3828 FillRect(dc
, &rc
, brush
);
3830 IntersectClipRect(dc
, es
->format_rect
.left
,
3831 es
->format_rect
.top
,
3832 es
->format_rect
.right
,
3833 es
->format_rect
.bottom
);
3834 if (es
->style
& ES_MULTILINE
) {
3836 IntersectClipRect(dc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
3839 old_font
= SelectObject(dc
, es
->font
);
3841 if (!es
->bEnableState
)
3842 SetTextColor(dc
, GetSysColor(COLOR_GRAYTEXT
));
3843 GetClipBox(dc
, &rcRgn
);
3844 if (es
->style
& ES_MULTILINE
) {
3845 INT vlc
= get_vertical_line_count(es
);
3846 for (i
= es
->y_offset
; i
<= min(es
->y_offset
+ vlc
, es
->y_offset
+ es
->line_count
- 1) ; i
++) {
3847 EDIT_UpdateUniscribeData(es
, dc
, i
);
3848 EDIT_GetLineRect(es
, i
, 0, -1, &rcLine
);
3849 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
3850 EDIT_PaintLine(es
, dc
, i
, rev
);
3853 EDIT_UpdateUniscribeData(es
, dc
, 0);
3854 EDIT_GetLineRect(es
, 0, 0, -1, &rcLine
);
3855 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
3856 EDIT_PaintLine(es
, dc
, 0, rev
);
3859 SelectObject(dc
, old_font
);
3862 EndPaint(es
->hwndSelf
, &ps
);
3866 /*********************************************************************
3871 static void EDIT_WM_SetFocus(EDITSTATE
*es
)
3873 es
->flags
|= EF_FOCUSED
;
3875 if (!(es
->style
& ES_NOHIDESEL
))
3876 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
3878 /* single line edit updates itself */
3879 if (IsWindowVisible(es
->hwndSelf
) && !(es
->style
& ES_MULTILINE
))
3881 HDC hdc
= GetDC(es
->hwndSelf
);
3882 EDIT_WM_Paint(es
, hdc
);
3883 ReleaseDC(es
->hwndSelf
, hdc
);
3886 CreateCaret(es
->hwndSelf
, 0, 1, es
->line_height
);
3887 EDIT_SetCaretPos(es
, es
->selection_end
,
3888 es
->flags
& EF_AFTER_WRAP
);
3889 ShowCaret(es
->hwndSelf
);
3890 EDIT_NOTIFY_PARENT(es
, EN_SETFOCUS
);
3894 /*********************************************************************
3898 * With Win95 look the margins are set to default font value unless
3899 * the system font (font == 0) is being set, in which case they are left
3903 static void EDIT_WM_SetFont(EDITSTATE
*es
, HFONT font
, BOOL redraw
)
3911 EDIT_InvalidateUniscribeData(es
);
3912 dc
= GetDC(es
->hwndSelf
);
3914 old_font
= SelectObject(dc
, font
);
3915 GetTextMetricsW(dc
, &tm
);
3916 es
->line_height
= tm
.tmHeight
;
3917 es
->char_width
= tm
.tmAveCharWidth
;
3919 SelectObject(dc
, old_font
);
3920 ReleaseDC(es
->hwndSelf
, dc
);
3922 /* Reset the format rect and the margins */
3923 GetClientRect(es
->hwndSelf
, &clientRect
);
3924 EDIT_SetRectNP(es
, &clientRect
);
3925 EDIT_EM_SetMargins(es
, EC_LEFTMARGIN
| EC_RIGHTMARGIN
,
3926 EC_USEFONTINFO
, EC_USEFONTINFO
, FALSE
);
3928 if (es
->style
& ES_MULTILINE
)
3929 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
3931 EDIT_CalcLineWidth_SL(es
);
3934 EDIT_UpdateText(es
, NULL
, TRUE
);
3935 if (es
->flags
& EF_FOCUSED
) {
3937 CreateCaret(es
->hwndSelf
, 0, 1, es
->line_height
);
3938 EDIT_SetCaretPos(es
, es
->selection_end
,
3939 es
->flags
& EF_AFTER_WRAP
);
3940 ShowCaret(es
->hwndSelf
);
3945 /*********************************************************************
3950 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3951 * The modified flag is reset. No notifications are sent.
3953 * For single-line controls, reception of WM_SETTEXT triggers:
3954 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3957 static void EDIT_WM_SetText(EDITSTATE
*es
, LPCWSTR text
, BOOL unicode
)
3959 LPWSTR textW
= NULL
;
3960 if (!unicode
&& text
)
3962 LPCSTR textA
= (LPCSTR
)text
;
3963 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
3964 textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
));
3966 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
3970 if (es
->flags
& EF_UPDATE
)
3971 /* fixed this bug once; complain if we see it about to happen again. */
3972 ERR("SetSel may generate UPDATE message whose handler may reset "
3975 EDIT_EM_SetSel(es
, 0, (UINT
)-1, FALSE
);
3978 TRACE("%s\n", debugstr_w(text
));
3979 EDIT_EM_ReplaceSel(es
, FALSE
, text
, FALSE
, FALSE
);
3981 HeapFree(GetProcessHeap(), 0, textW
);
3986 EDIT_EM_ReplaceSel(es
, FALSE
, empty_stringW
, FALSE
, FALSE
);
3989 es
->flags
&= ~EF_MODIFIED
;
3990 EDIT_EM_SetSel(es
, 0, 0, FALSE
);
3992 /* Send the notification after the selection start and end have been set
3993 * edit control doesn't send notification on WM_SETTEXT
3994 * if it is multiline, or it is part of combobox
3996 if( !((es
->style
& ES_MULTILINE
) || es
->hwndListBox
))
3998 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
3999 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
4001 EDIT_EM_ScrollCaret(es
);
4002 EDIT_UpdateScrollInfo(es
);
4003 EDIT_InvalidateUniscribeData(es
);
4007 /*********************************************************************
4012 static void EDIT_WM_Size(EDITSTATE
*es
, UINT action
)
4014 if ((action
== SIZE_MAXIMIZED
) || (action
== SIZE_RESTORED
)) {
4016 GetClientRect(es
->hwndSelf
, &rc
);
4017 EDIT_SetRectNP(es
, &rc
);
4018 EDIT_UpdateText(es
, NULL
, TRUE
);
4023 /*********************************************************************
4027 * This message is sent by SetWindowLong on having changed either the Style
4028 * or the extended style.
4030 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4032 * See also EDIT_WM_NCCreate
4034 * It appears that the Windows version of the edit control allows the style
4035 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4036 * style variable which will generally be different. In this function we
4037 * update the internal style based on what changed in the externally visible
4040 * Much of this content as based upon the MSDN, especially:
4041 * Platform SDK Documentation -> User Interface Services ->
4042 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4043 * Edit Control Styles
4045 static LRESULT
EDIT_WM_StyleChanged ( EDITSTATE
*es
, WPARAM which
, const STYLESTRUCT
*style
)
4047 if (GWL_STYLE
== which
) {
4048 DWORD style_change_mask
;
4050 /* Only a subset of changes can be applied after the control
4053 style_change_mask
= ES_UPPERCASE
| ES_LOWERCASE
|
4055 if (es
->style
& ES_MULTILINE
)
4056 style_change_mask
|= ES_WANTRETURN
;
4058 new_style
= style
->styleNew
& style_change_mask
;
4060 /* Number overrides lowercase overrides uppercase (at least it
4061 * does in Win95). However I'll bet that ES_NUMBER would be
4062 * invalid under Win 3.1.
4064 if (new_style
& ES_NUMBER
) {
4065 ; /* do not override the ES_NUMBER */
4066 } else if (new_style
& ES_LOWERCASE
) {
4067 new_style
&= ~ES_UPPERCASE
;
4070 es
->style
= (es
->style
& ~style_change_mask
) | new_style
;
4071 } else if (GWL_EXSTYLE
== which
) {
4072 ; /* FIXME - what is needed here */
4074 WARN ("Invalid style change %ld\n",which
);
4080 /*********************************************************************
4085 static LRESULT
EDIT_WM_SysKeyDown(EDITSTATE
*es
, INT key
, DWORD key_data
)
4087 if ((key
== VK_BACK
) && (key_data
& 0x2000)) {
4088 if (EDIT_EM_CanUndo(es
))
4091 } else if (key
== VK_UP
|| key
== VK_DOWN
) {
4092 if (EDIT_CheckCombo(es
, WM_SYSKEYDOWN
, key
))
4095 return DefWindowProcW(es
->hwndSelf
, WM_SYSKEYDOWN
, key
, key_data
);
4099 /*********************************************************************
4104 static void EDIT_WM_Timer(EDITSTATE
*es
)
4106 if (es
->region_posx
< 0) {
4107 EDIT_MoveBackward(es
, TRUE
);
4108 } else if (es
->region_posx
> 0) {
4109 EDIT_MoveForward(es
, TRUE
);
4112 * FIXME: gotta do some vertical scrolling here, like
4113 * EDIT_EM_LineScroll(hwnd, 0, 1);
4117 /*********************************************************************
4122 static LRESULT
EDIT_WM_HScroll(EDITSTATE
*es
, INT action
, INT pos
)
4127 if (!(es
->style
& ES_MULTILINE
))
4130 if (!(es
->style
& ES_AUTOHSCROLL
))
4134 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4137 TRACE("SB_LINELEFT\n");
4139 dx
= -es
->char_width
;
4142 TRACE("SB_LINERIGHT\n");
4143 if (es
->x_offset
< es
->text_width
)
4144 dx
= es
->char_width
;
4147 TRACE("SB_PAGELEFT\n");
4149 dx
= -fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4152 TRACE("SB_PAGERIGHT\n");
4153 if (es
->x_offset
< es
->text_width
)
4154 dx
= fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4162 TRACE("SB_RIGHT\n");
4163 if (es
->x_offset
< es
->text_width
)
4164 dx
= es
->text_width
- es
->x_offset
;
4167 TRACE("SB_THUMBTRACK %d\n", pos
);
4168 es
->flags
|= EF_HSCROLL_TRACK
;
4169 if(es
->style
& WS_HSCROLL
)
4170 dx
= pos
- es
->x_offset
;
4175 if(pos
< 0 || pos
> 100) return 0;
4176 /* Assume default scroll range 0-100 */
4177 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4178 new_x
= pos
* (es
->text_width
- fw
) / 100;
4179 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4182 case SB_THUMBPOSITION
:
4183 TRACE("SB_THUMBPOSITION %d\n", pos
);
4184 es
->flags
&= ~EF_HSCROLL_TRACK
;
4185 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4186 dx
= pos
- es
->x_offset
;
4191 if(pos
< 0 || pos
> 100) return 0;
4192 /* Assume default scroll range 0-100 */
4193 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4194 new_x
= pos
* (es
->text_width
- fw
) / 100;
4195 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4198 /* force scroll info update */
4199 EDIT_UpdateScrollInfo(es
);
4200 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
4204 TRACE("SB_ENDSCROLL\n");
4207 * FIXME : the next two are undocumented !
4208 * Are we doing the right thing ?
4209 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4210 * although it's also a regular control message.
4212 case EM_GETTHUMB
: /* this one is used by NT notepad */
4215 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4216 ret
= GetScrollPos(es
->hwndSelf
, SB_HORZ
);
4219 /* Assume default scroll range 0-100 */
4220 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4221 ret
= es
->text_width
? es
->x_offset
* 100 / (es
->text_width
- fw
) : 0;
4223 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4227 TRACE("EM_LINESCROLL16\n");
4232 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4238 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4239 /* check if we are going to move too far */
4240 if(es
->x_offset
+ dx
+ fw
> es
->text_width
)
4241 dx
= es
->text_width
- fw
- es
->x_offset
;
4243 EDIT_EM_LineScroll_internal(es
, dx
, 0);
4249 /*********************************************************************
4254 static LRESULT
EDIT_WM_VScroll(EDITSTATE
*es
, INT action
, INT pos
)
4258 if (!(es
->style
& ES_MULTILINE
))
4261 if (!(es
->style
& ES_AUTOVSCROLL
))
4270 TRACE("action %d (%s)\n", action
, (action
== SB_LINEUP
? "SB_LINEUP" :
4271 (action
== SB_LINEDOWN
? "SB_LINEDOWN" :
4272 (action
== SB_PAGEUP
? "SB_PAGEUP" :
4274 EDIT_EM_Scroll(es
, action
);
4281 TRACE("SB_BOTTOM\n");
4282 dy
= es
->line_count
- 1 - es
->y_offset
;
4285 TRACE("SB_THUMBTRACK %d\n", pos
);
4286 es
->flags
|= EF_VSCROLL_TRACK
;
4287 if(es
->style
& WS_VSCROLL
)
4288 dy
= pos
- es
->y_offset
;
4291 /* Assume default scroll range 0-100 */
4294 if(pos
< 0 || pos
> 100) return 0;
4295 vlc
= get_vertical_line_count(es
);
4296 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4297 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4298 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4299 es
->line_count
, es
->y_offset
, pos
, dy
);
4302 case SB_THUMBPOSITION
:
4303 TRACE("SB_THUMBPOSITION %d\n", pos
);
4304 es
->flags
&= ~EF_VSCROLL_TRACK
;
4305 if(es
->style
& WS_VSCROLL
)
4306 dy
= pos
- es
->y_offset
;
4309 /* Assume default scroll range 0-100 */
4312 if(pos
< 0 || pos
> 100) return 0;
4313 vlc
= get_vertical_line_count(es
);
4314 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4315 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4316 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4317 es
->line_count
, es
->y_offset
, pos
, dy
);
4321 /* force scroll info update */
4322 EDIT_UpdateScrollInfo(es
);
4323 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
4327 TRACE("SB_ENDSCROLL\n");
4330 * FIXME : the next two are undocumented !
4331 * Are we doing the right thing ?
4332 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4333 * although it's also a regular control message.
4335 case EM_GETTHUMB
: /* this one is used by NT notepad */
4338 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_VSCROLL
)
4339 ret
= GetScrollPos(es
->hwndSelf
, SB_VERT
);
4342 /* Assume default scroll range 0-100 */
4343 INT vlc
= get_vertical_line_count(es
);
4344 ret
= es
->line_count
? es
->y_offset
* 100 / (es
->line_count
- vlc
) : 0;
4346 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4350 TRACE("EM_LINESCROLL %d\n", pos
);
4355 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4360 EDIT_EM_LineScroll(es
, 0, dy
);
4364 /*********************************************************************
4368 * FIXME: is this right ? (or should it be only VSCROLL)
4369 * (and maybe only for edit controls that really have their
4370 * own scrollbars) (and maybe only for multiline controls ?)
4371 * All in all: very poorly documented
4374 static LRESULT
EDIT_EM_GetThumb(EDITSTATE
*es
)
4376 return MAKELONG(EDIT_WM_VScroll(es
, EM_GETTHUMB
, 0),
4377 EDIT_WM_HScroll(es
, EM_GETTHUMB
, 0));
4381 /********************************************************************
4383 * The Following code is to handle inline editing from IMEs
4386 static void EDIT_GetCompositionStr(HIMC hIMC
, LPARAM CompFlag
, EDITSTATE
*es
)
4390 LPSTR lpCompStrAttr
= NULL
;
4393 buflen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
4400 lpCompStr
= HeapAlloc(GetProcessHeap(),0,buflen
+ sizeof(WCHAR
));
4403 ERR("Unable to allocate IME CompositionString\n");
4408 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, buflen
);
4409 lpCompStr
[buflen
/sizeof(WCHAR
)] = 0;
4411 if (CompFlag
& GCS_COMPATTR
)
4414 * We do not use the attributes yet. it would tell us what characters
4415 * are in transition and which are converted or decided upon
4417 dwBufLenAttr
= ImmGetCompositionStringW(hIMC
, GCS_COMPATTR
, NULL
, 0);
4421 lpCompStrAttr
= HeapAlloc(GetProcessHeap(),0,dwBufLenAttr
+1);
4424 ERR("Unable to allocate IME Attribute String\n");
4425 HeapFree(GetProcessHeap(),0,lpCompStr
);
4428 ImmGetCompositionStringW(hIMC
,GCS_COMPATTR
, lpCompStrAttr
,
4430 lpCompStrAttr
[dwBufLenAttr
] = 0;
4434 /* check for change in composition start */
4435 if (es
->selection_end
< es
->composition_start
)
4436 es
->composition_start
= es
->selection_end
;
4438 /* replace existing selection string */
4439 es
->selection_start
= es
->composition_start
;
4441 if (es
->composition_len
> 0)
4442 es
->selection_end
= es
->composition_start
+ es
->composition_len
;
4444 es
->selection_end
= es
->selection_start
;
4446 EDIT_EM_ReplaceSel(es
, FALSE
, lpCompStr
, TRUE
, TRUE
);
4447 es
->composition_len
= abs(es
->composition_start
- es
->selection_end
);
4449 es
->selection_start
= es
->composition_start
;
4450 es
->selection_end
= es
->selection_start
+ es
->composition_len
;
4452 HeapFree(GetProcessHeap(),0,lpCompStrAttr
);
4453 HeapFree(GetProcessHeap(),0,lpCompStr
);
4456 static void EDIT_GetResultStr(HIMC hIMC
, EDITSTATE
*es
)
4461 buflen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
4467 lpResultStr
= HeapAlloc(GetProcessHeap(),0, buflen
+sizeof(WCHAR
));
4470 ERR("Unable to alloc buffer for IME string\n");
4474 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpResultStr
, buflen
);
4475 lpResultStr
[buflen
/sizeof(WCHAR
)] = 0;
4477 /* check for change in composition start */
4478 if (es
->selection_end
< es
->composition_start
)
4479 es
->composition_start
= es
->selection_end
;
4481 es
->selection_start
= es
->composition_start
;
4482 es
->selection_end
= es
->composition_start
+ es
->composition_len
;
4483 EDIT_EM_ReplaceSel(es
, TRUE
, lpResultStr
, TRUE
, TRUE
);
4484 es
->composition_start
= es
->selection_end
;
4485 es
->composition_len
= 0;
4487 HeapFree(GetProcessHeap(),0,lpResultStr
);
4490 static void EDIT_ImeComposition(HWND hwnd
, LPARAM CompFlag
, EDITSTATE
*es
)
4495 if (es
->composition_len
== 0 && es
->selection_start
!= es
->selection_end
)
4497 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
4498 es
->composition_start
= es
->selection_end
;
4501 hIMC
= ImmGetContext(hwnd
);
4505 if (CompFlag
& GCS_RESULTSTR
)
4507 EDIT_GetResultStr(hIMC
, es
);
4512 if (CompFlag
& GCS_COMPSTR
)
4513 EDIT_GetCompositionStr(hIMC
, CompFlag
, es
);
4514 cursor
= ImmGetCompositionStringW(hIMC
, GCS_CURSORPOS
, 0, 0);
4516 ImmReleaseContext(hwnd
, hIMC
);
4517 EDIT_SetCaretPos(es
, es
->selection_start
+ cursor
, es
->flags
& EF_AFTER_WRAP
);
4521 /*********************************************************************
4525 * See also EDIT_WM_StyleChanged
4527 static LRESULT
EDIT_WM_NCCreate(HWND hwnd
, LPCREATESTRUCTW lpcs
, BOOL unicode
)
4532 TRACE("Creating %s edit control, style = %08x\n",
4533 unicode
? "Unicode" : "ANSI", lpcs
->style
);
4535 if (!(es
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*es
))))
4537 SetWindowLongPtrW( hwnd
, 0, (LONG_PTR
)es
);
4540 * Note: since the EDITSTATE has not been fully initialized yet,
4541 * we can't use any API calls that may send
4542 * WM_XXX messages before WM_NCCREATE is completed.
4545 es
->is_unicode
= unicode
;
4546 es
->style
= lpcs
->style
;
4548 es
->bEnableState
= !(es
->style
& WS_DISABLED
);
4550 es
->hwndSelf
= hwnd
;
4551 /* Save parent, which will be notified by EN_* messages */
4552 es
->hwndParent
= lpcs
->hwndParent
;
4554 if (es
->style
& ES_COMBO
)
4555 es
->hwndListBox
= GetDlgItem(es
->hwndParent
, ID_CB_LISTBOX
);
4557 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4558 if (lpcs
->dwExStyle
& WS_EX_RIGHT
) es
->style
|= ES_RIGHT
;
4560 /* Number overrides lowercase overrides uppercase (at least it
4561 * does in Win95). However I'll bet that ES_NUMBER would be
4562 * invalid under Win 3.1.
4564 if (es
->style
& ES_NUMBER
) {
4565 ; /* do not override the ES_NUMBER */
4566 } else if (es
->style
& ES_LOWERCASE
) {
4567 es
->style
&= ~ES_UPPERCASE
;
4569 if (es
->style
& ES_MULTILINE
) {
4570 es
->buffer_limit
= BUFLIMIT_INITIAL
;
4571 if (es
->style
& WS_VSCROLL
)
4572 es
->style
|= ES_AUTOVSCROLL
;
4573 if (es
->style
& WS_HSCROLL
)
4574 es
->style
|= ES_AUTOHSCROLL
;
4575 es
->style
&= ~ES_PASSWORD
;
4576 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
)) {
4577 /* Confirmed - RIGHT overrides CENTER */
4578 if (es
->style
& ES_RIGHT
)
4579 es
->style
&= ~ES_CENTER
;
4580 es
->style
&= ~WS_HSCROLL
;
4581 es
->style
&= ~ES_AUTOHSCROLL
;
4584 es
->buffer_limit
= BUFLIMIT_INITIAL
;
4585 if ((es
->style
& ES_RIGHT
) && (es
->style
& ES_CENTER
))
4586 es
->style
&= ~ES_CENTER
;
4587 es
->style
&= ~WS_HSCROLL
;
4588 es
->style
&= ~WS_VSCROLL
;
4589 if (es
->style
& ES_PASSWORD
)
4590 es
->password_char
= '*';
4593 alloc_size
= ROUND_TO_GROW((es
->buffer_size
+ 1) * sizeof(WCHAR
));
4594 if(!(es
->hloc32W
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
4596 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
4598 if (!(es
->undo_text
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (es
->buffer_size
+ 1) * sizeof(WCHAR
))))
4600 es
->undo_buffer_size
= es
->buffer_size
;
4602 if (es
->style
& ES_MULTILINE
)
4603 if (!(es
->first_line_def
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
))))
4608 * In Win95 look and feel, the WS_BORDER style is replaced by the
4609 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4610 * control a nonclient area so we don't need to draw the border.
4611 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4612 * a nonclient area and we should handle painting the border ourselves.
4614 * When making modifications please ensure that the code still works
4615 * for edit controls created directly with style 0x50800000, exStyle 0
4616 * (which should have a single pixel border)
4618 if (lpcs
->dwExStyle
& WS_EX_CLIENTEDGE
)
4619 es
->style
&= ~WS_BORDER
;
4620 else if (es
->style
& WS_BORDER
)
4621 SetWindowLongW(hwnd
, GWL_STYLE
, es
->style
& ~WS_BORDER
);
4626 SetWindowLongPtrW(es
->hwndSelf
, 0, 0);
4627 EDIT_InvalidateUniscribeData(es
);
4628 HeapFree(GetProcessHeap(), 0, es
->first_line_def
);
4629 HeapFree(GetProcessHeap(), 0, es
->undo_text
);
4630 if (es
->hloc32W
) LocalFree(es
->hloc32W
);
4631 HeapFree(GetProcessHeap(), 0, es
->logAttr
);
4632 HeapFree(GetProcessHeap(), 0, es
);
4637 /*********************************************************************
4642 static LRESULT
EDIT_WM_Create(EDITSTATE
*es
, LPCWSTR name
)
4646 TRACE("%s\n", debugstr_w(name
));
4648 * To initialize some final structure members, we call some helper
4649 * functions. However, since the EDITSTATE is not consistent (i.e.
4650 * not fully initialized), we should be very careful which
4651 * functions can be called, and in what order.
4653 EDIT_WM_SetFont(es
, 0, FALSE
);
4654 EDIT_EM_EmptyUndoBuffer(es
);
4656 /* We need to calculate the format rect
4657 (applications may send EM_SETMARGINS before the control gets visible) */
4658 GetClientRect(es
->hwndSelf
, &clientRect
);
4659 EDIT_SetRectNP(es
, &clientRect
);
4661 if (name
&& *name
) {
4662 EDIT_EM_ReplaceSel(es
, FALSE
, name
, FALSE
, FALSE
);
4663 /* if we insert text to the editline, the text scrolls out
4664 * of the window, as the caret is placed after the insert
4665 * pos normally; thus we reset es->selection... to 0 and
4668 es
->selection_start
= es
->selection_end
= 0;
4669 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4670 * Messages are only to be sent when the USER does something to
4671 * change the contents. So I am removing this EN_CHANGE
4673 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4675 EDIT_EM_ScrollCaret(es
);
4677 /* force scroll info update */
4678 EDIT_UpdateScrollInfo(es
);
4679 /* The rule seems to return 1 here for success */
4680 /* Power Builder masked edit controls will crash */
4682 /* FIXME: is that in all cases so ? */
4687 /*********************************************************************
4692 static LRESULT
EDIT_WM_NCDestroy(EDITSTATE
*es
)
4696 /* The app can own the text buffer handle */
4697 if (es
->hloc32W
&& (es
->hloc32W
!= es
->hlocapp
)) {
4698 LocalFree(es
->hloc32W
);
4700 if (es
->hloc32A
&& (es
->hloc32A
!= es
->hlocapp
)) {
4701 LocalFree(es
->hloc32A
);
4703 EDIT_InvalidateUniscribeData(es
);
4704 pc
= es
->first_line_def
;
4708 HeapFree(GetProcessHeap(), 0, pc
);
4712 SetWindowLongPtrW( es
->hwndSelf
, 0, 0 );
4713 HeapFree(GetProcessHeap(), 0, es
->undo_text
);
4714 HeapFree(GetProcessHeap(), 0, es
);
4720 static inline LRESULT
DefWindowProcT(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
4723 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
4725 return DefWindowProcA(hwnd
, msg
, wParam
, lParam
);
4728 /*********************************************************************
4730 * EditWndProc_common
4732 * The messages are in the order of the actual integer values
4733 * (which can be found in include/windows.h)
4735 LRESULT WINAPI
EditWndProc_common( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
4737 EDITSTATE
*es
= (EDITSTATE
*)GetWindowLongPtrW( hwnd
, 0 );
4740 /* ReactOS r50219 */
4743 pWnd
= ValidateHwnd(hwnd
);
4748 NtUserSetWindowFNID(hwnd
, FNID_EDIT
);
4752 if (pWnd
->fnid
!= FNID_EDIT
)
4754 ERR("Wrong window class for Edit! fnId 0x%x\n",pWnd
->fnid
);
4761 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), wParam
, lParam
);
4763 if (!es
&& msg
!= WM_NCCREATE
)
4764 return DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
4766 if (es
&& (msg
!= WM_NCDESTROY
)) EDIT_LockBuffer(es
);
4770 result
= EDIT_EM_GetSel(es
, (PUINT
)wParam
, (PUINT
)lParam
);
4774 EDIT_EM_SetSel(es
, wParam
, lParam
, FALSE
);
4775 EDIT_EM_ScrollCaret(es
);
4781 CopyRect((LPRECT
)lParam
, &es
->format_rect
);
4785 if ((es
->style
& ES_MULTILINE
) && lParam
) {
4786 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
4787 EDIT_UpdateText(es
, NULL
, TRUE
);
4792 if ((es
->style
& ES_MULTILINE
) && lParam
)
4793 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
4797 result
= EDIT_EM_Scroll(es
, (INT
)wParam
);
4801 result
= (LRESULT
)EDIT_EM_LineScroll(es
, (INT
)wParam
, (INT
)lParam
);
4804 case EM_SCROLLCARET
:
4805 EDIT_EM_ScrollCaret(es
);
4810 result
= ((es
->flags
& EF_MODIFIED
) != 0);
4815 es
->flags
|= EF_MODIFIED
;
4817 es
->flags
&= ~(EF_MODIFIED
| EF_UPDATE
); /* reset pending updates */
4820 case EM_GETLINECOUNT
:
4821 result
= (es
->style
& ES_MULTILINE
) ? es
->line_count
: 1;
4825 result
= (LRESULT
)EDIT_EM_LineIndex(es
, (INT
)wParam
);
4829 EDIT_EM_SetHandle(es
, (HLOCAL
)wParam
);
4833 result
= (LRESULT
)EDIT_EM_GetHandle(es
);
4837 result
= EDIT_EM_GetThumb(es
);
4840 /* these messages missing from specs */
4845 FIXME("undocumented message 0x%x, please report\n", msg
);
4846 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
4850 result
= (LRESULT
)EDIT_EM_LineLength(es
, (INT
)wParam
);
4858 textW
= (LPWSTR
)lParam
;
4861 LPSTR textA
= (LPSTR
)lParam
;
4862 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
4863 if (!(textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
)))) break;
4864 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
4867 EDIT_EM_ReplaceSel(es
, (BOOL
)wParam
, textW
, TRUE
, TRUE
);
4871 HeapFree(GetProcessHeap(), 0, textW
);
4876 result
= (LRESULT
)EDIT_EM_GetLine(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
4879 case EM_SETLIMITTEXT
:
4880 EDIT_EM_SetLimitText(es
, wParam
);
4884 result
= (LRESULT
)EDIT_EM_CanUndo(es
);
4889 result
= (LRESULT
)EDIT_EM_Undo(es
);
4893 result
= (LRESULT
)EDIT_EM_FmtLines(es
, (BOOL
)wParam
);
4896 case EM_LINEFROMCHAR
:
4897 result
= (LRESULT
)EDIT_EM_LineFromChar(es
, (INT
)wParam
);
4900 case EM_SETTABSTOPS
:
4901 result
= (LRESULT
)EDIT_EM_SetTabStops(es
, (INT
)wParam
, (LPINT
)lParam
);
4904 case EM_SETPASSWORDCHAR
:
4909 charW
= (WCHAR
)wParam
;
4912 CHAR charA
= wParam
;
4913 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
4916 EDIT_EM_SetPasswordChar(es
, charW
);
4920 case EM_EMPTYUNDOBUFFER
:
4921 EDIT_EM_EmptyUndoBuffer(es
);
4924 case EM_GETFIRSTVISIBLELINE
:
4925 result
= (es
->style
& ES_MULTILINE
) ? es
->y_offset
: es
->x_offset
;
4928 case EM_SETREADONLY
:
4930 DWORD old_style
= es
->style
;
4933 SetWindowLongW( hwnd
, GWL_STYLE
,
4934 GetWindowLongW( hwnd
, GWL_STYLE
) | ES_READONLY
);
4935 es
->style
|= ES_READONLY
;
4937 SetWindowLongW( hwnd
, GWL_STYLE
,
4938 GetWindowLongW( hwnd
, GWL_STYLE
) & ~ES_READONLY
);
4939 es
->style
&= ~ES_READONLY
;
4942 if (old_style
^ es
->style
)
4943 InvalidateRect(es
->hwndSelf
, NULL
, TRUE
);
4949 case EM_SETWORDBREAKPROC
:
4950 EDIT_EM_SetWordBreakProc(es
, (void *)lParam
);
4953 case EM_GETWORDBREAKPROC
:
4954 result
= (LRESULT
)es
->word_break_proc
;
4957 case EM_GETPASSWORDCHAR
:
4960 result
= es
->password_char
;
4963 WCHAR charW
= es
->password_char
;
4965 WideCharToMultiByte(CP_ACP
, 0, &charW
, 1, &charA
, 1, NULL
, NULL
);
4972 EDIT_EM_SetMargins(es
, (INT
)wParam
, LOWORD(lParam
), HIWORD(lParam
), TRUE
);
4976 result
= MAKELONG(es
->left_margin
, es
->right_margin
);
4979 case EM_GETLIMITTEXT
:
4980 result
= es
->buffer_limit
;
4983 case EM_POSFROMCHAR
:
4984 if ((INT
)wParam
>= get_text_length(es
)) result
= -1;
4985 else result
= EDIT_EM_PosFromChar(es
, (INT
)wParam
, FALSE
);
4988 case EM_CHARFROMPOS
:
4989 result
= EDIT_EM_CharFromPos(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4992 /* End of the EM_ messages which were in numerical order; what order
4993 * are these in? vaguely alphabetical?
4997 result
= EDIT_WM_NCCreate(hwnd
, (LPCREATESTRUCTW
)lParam
, unicode
);
5001 result
= EDIT_WM_NCDestroy(es
);
5003 NtUserSetWindowFNID(hwnd
, FNID_DESTROY
);
5010 result
= DLGC_HASSETSEL
| DLGC_WANTCHARS
| DLGC_WANTARROWS
;
5012 if (es
->style
& ES_MULTILINE
)
5013 result
|= DLGC_WANTALLKEYS
;
5017 es
->flags
|=EF_DIALOGMODE
;
5019 if (((LPMSG
)lParam
)->message
== WM_KEYDOWN
)
5021 int vk
= (int)((LPMSG
)lParam
)->wParam
;
5023 if (es
->hwndListBox
)
5025 if (vk
== VK_RETURN
|| vk
== VK_ESCAPE
)
5026 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
5027 result
|= DLGC_WANTMESSAGE
;
5039 strng
[0] = wParam
>> 8;
5040 strng
[1] = wParam
& 0xff;
5041 if (strng
[0]) MultiByteToWideChar(CP_ACP
, 0, strng
, 2, &charW
, 1);
5042 else MultiByteToWideChar(CP_ACP
, 0, &strng
[1], 1, &charW
, 1);
5043 result
= EDIT_WM_Char(es
, charW
);
5055 CHAR charA
= wParam
;
5056 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
5059 if (es
->hwndListBox
)
5061 if (charW
== VK_RETURN
|| charW
== VK_ESCAPE
)
5063 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
5064 SendMessageW(GetParent(hwnd
), WM_KEYDOWN
, charW
, 0);
5068 result
= EDIT_WM_Char(es
, charW
);
5075 if (wParam
== UNICODE_NOCHAR
) return TRUE
;
5076 if (wParam
<= 0x000fffff)
5078 if(wParam
> 0xffff) /* convert to surrogates */
5081 EDIT_WM_Char(es
, (wParam
>> 10) + 0xd800);
5082 EDIT_WM_Char(es
, (wParam
& 0x03ff) + 0xdc00);
5084 else EDIT_WM_Char(es
, wParam
);
5094 case WM_CONTEXTMENU
:
5095 EDIT_WM_ContextMenu(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5104 result
= EDIT_WM_Create(es
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
5107 LPCSTR nameA
= ((LPCREATESTRUCTA
)lParam
)->lpszName
;
5108 LPWSTR nameW
= NULL
;
5111 INT countW
= MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, NULL
, 0);
5112 if((nameW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
))))
5113 MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, nameW
, countW
);
5115 result
= EDIT_WM_Create(es
, nameW
);
5116 HeapFree(GetProcessHeap(), 0, nameW
);
5125 es
->bEnableState
= (BOOL
) wParam
;
5126 EDIT_UpdateText(es
, NULL
, TRUE
);
5130 /* we do the proper erase in EDIT_WM_Paint */
5135 result
= (LRESULT
)es
->font
;
5139 result
= (LRESULT
)EDIT_WM_GetText(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
5142 case WM_GETTEXTLENGTH
:
5143 if (unicode
) result
= get_text_length(es
);
5144 else result
= WideCharToMultiByte( CP_ACP
, 0, es
->text
, get_text_length(es
),
5145 NULL
, 0, NULL
, NULL
);
5149 result
= EDIT_WM_HScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
5153 result
= EDIT_WM_KeyDown(es
, (INT
)wParam
);
5157 result
= EDIT_WM_KillFocus(es
);
5160 case WM_LBUTTONDBLCLK
:
5161 result
= EDIT_WM_LButtonDblClk(es
);
5164 case WM_LBUTTONDOWN
:
5165 result
= EDIT_WM_LButtonDown(es
, wParam
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5169 result
= EDIT_WM_LButtonUp(es
);
5172 case WM_MBUTTONDOWN
:
5173 result
= EDIT_WM_MButtonDown(es
);
5177 result
= EDIT_WM_MouseMove(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5180 case WM_PRINTCLIENT
:
5182 EDIT_WM_Paint(es
, (HDC
)wParam
);
5190 EDIT_WM_SetFocus(es
);
5194 EDIT_WM_SetFont(es
, (HFONT
)wParam
, LOWORD(lParam
) != 0);
5198 /* FIXME: actually set an internal flag and behave accordingly */
5202 EDIT_WM_SetText(es
, (LPCWSTR
)lParam
, unicode
);
5207 EDIT_WM_Size(es
, (UINT
)wParam
);
5210 case WM_STYLECHANGED
:
5211 result
= EDIT_WM_StyleChanged(es
, wParam
, (const STYLESTRUCT
*)lParam
);
5214 case WM_STYLECHANGING
:
5215 result
= 0; /* See EDIT_WM_StyleChanged */
5219 result
= EDIT_WM_SysKeyDown(es
, (INT
)wParam
, (DWORD
)lParam
);
5227 result
= EDIT_WM_VScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
5233 UINT pulScrollLines
= 3;
5234 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
5236 if (wParam
& (MK_SHIFT
| MK_CONTROL
)) {
5237 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
5240 wheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
5241 /* if scrolling changes direction, ignore left overs */
5242 if ((wheelDelta
< 0 && es
->wheelDeltaRemainder
< 0) ||
5243 (wheelDelta
> 0 && es
->wheelDeltaRemainder
> 0))
5244 es
->wheelDeltaRemainder
+= wheelDelta
;
5246 es
->wheelDeltaRemainder
= wheelDelta
;
5247 if (es
->wheelDeltaRemainder
&& pulScrollLines
)
5250 pulScrollLines
= (int) min((UINT
) es
->line_count
, pulScrollLines
);
5251 cLineScroll
= pulScrollLines
* (float)es
->wheelDeltaRemainder
/ WHEEL_DELTA
;
5252 es
->wheelDeltaRemainder
-= WHEEL_DELTA
* cLineScroll
/ (int)pulScrollLines
;
5253 result
= EDIT_EM_LineScroll(es
, 0, -cLineScroll
);
5259 /* IME messages to make the edit control IME aware */
5260 case WM_IME_SETCONTEXT
:
5263 case WM_IME_STARTCOMPOSITION
:
5264 es
->composition_start
= es
->selection_end
;
5265 es
->composition_len
= 0;
5268 case WM_IME_COMPOSITION
:
5269 EDIT_ImeComposition(hwnd
, lParam
, es
);
5272 case WM_IME_ENDCOMPOSITION
:
5273 if (es
->composition_len
> 0)
5275 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
5276 es
->selection_end
= es
->selection_start
;
5277 es
->composition_len
= 0;
5281 case WM_IME_COMPOSITIONFULL
:
5287 case WM_IME_CONTROL
:
5290 case WM_IME_REQUEST
:
5293 case IMR_QUERYCHARPOSITION
:
5296 IMECHARPOSITION
*chpos
= (IMECHARPOSITION
*)lParam
;
5298 pos
= EDIT_EM_PosFromChar(es
, es
->selection_start
+ chpos
->dwCharPos
, FALSE
);
5299 chpos
->pt
.x
= LOWORD(pos
);
5300 chpos
->pt
.y
= HIWORD(pos
);
5301 chpos
->cLineHeight
= es
->line_height
;
5302 chpos
->rcDocument
= es
->format_rect
;
5303 MapWindowPoints(hwnd
, 0, &chpos
->pt
, 1);
5304 MapWindowPoints(hwnd
, 0, (POINT
*)&chpos
->rcDocument
, 2);
5312 result
= DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
5317 /* ReactOS: check GetWindowLong in case es has been destroyed during processing */
5318 if (IsWindow(hwnd
) && es
&& GetWindowLongPtrW(hwnd
, 0))
5319 EDIT_UnlockBuffer(es
, FALSE
);
5321 if (IsWindow(hwnd
) && es
) EDIT_UnlockBuffer(es
, FALSE
);
5324 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), result
);
5331 /*********************************************************************
5333 * EditWndProc (USER32.@)
5335 LRESULT WINAPI
EditWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
5337 return EditWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
5340 /*********************************************************************
5342 * EditWndProcW (USER32.@)
5344 LRESULT WINAPI
EditWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
5346 return EditWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);
5349 #endif /* __REACTOS__ */
5351 /*********************************************************************
5352 * edit class descriptor
5354 static const WCHAR editW
[] = {'E','d','i','t',0};
5355 const struct builtin_class_descr EDIT_builtin_class
=
5358 CS_DBLCLKS
| CS_PARENTDC
, /* style */
5360 EditWndProcA
, /* procA */
5361 EditWndProcW
, /* procW */
5363 WINPROC_EDIT
, /* proc */
5366 sizeof(EDITSTATE
*) + sizeof(WORD
), /* extra */
5368 sizeof(EDITSTATE
*), /* extra */
5370 IDC_IBEAM
, /* cursor */