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 #include <wine/debug.h>
51 WINE_DEFAULT_DEBUG_CHANNEL(edit
);
52 WINE_DECLARE_DEBUG_CHANNEL(combo
);
53 WINE_DECLARE_DEBUG_CHANNEL(relay
);
55 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
56 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
57 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
58 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
61 * extra flags for EDITSTATE.flags field
63 #define EF_MODIFIED 0x0001 /* text has been modified */
64 #define EF_FOCUSED 0x0002 /* we have input focus */
65 #define EF_UPDATE 0x0004 /* notify parent of changed state */
66 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
67 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
68 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
69 wrapped line, instead of in front of the next character */
70 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
71 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
75 END_0
= 0, /* line ends with terminating '\0' character */
76 END_WRAP
, /* line is wrapped */
77 END_HARD
, /* line ends with a hard return '\r\n' */
78 END_SOFT
, /* line ends with a soft return '\r\r\n' */
79 END_RICH
/* line ends with a single '\n' */
82 typedef struct tagLINEDEF
{
83 INT length
; /* bruto length of a line in bytes */
84 INT net_length
; /* netto length of a line in visible characters */
86 INT width
; /* width of the line in pixels */
87 INT index
; /* line index into the buffer */
88 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data */
89 struct tagLINEDEF
*next
;
94 BOOL is_unicode
; /* how the control was created */
95 LPWSTR text
; /* the actual contents of the control */
96 UINT text_length
; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
97 UINT buffer_size
; /* the size of the buffer in characters */
98 UINT buffer_limit
; /* the maximum size to which the buffer may grow in characters */
99 HFONT font
; /* NULL means standard system font */
100 INT x_offset
; /* scroll offset for multi lines this is in pixels
101 for single lines it's in characters */
102 INT line_height
; /* height of a screen line in pixels */
103 INT char_width
; /* average character width in pixels */
104 DWORD style
; /* sane version of wnd->dwStyle */
105 WORD flags
; /* flags that are not in es->style or wnd->flags (EF_XXX) */
106 INT undo_insert_count
; /* number of characters inserted in sequence */
107 UINT undo_position
; /* character index of the insertion and deletion */
108 LPWSTR undo_text
; /* deleted text */
109 UINT undo_buffer_size
; /* size of the deleted text buffer */
110 INT selection_start
; /* == selection_end if no selection */
111 INT selection_end
; /* == current caret position */
112 WCHAR password_char
; /* == 0 if no password char, and for multi line controls */
113 INT left_margin
; /* in pixels */
114 INT right_margin
; /* in pixels */
116 INT text_width
; /* width of the widest line in pixels for multi line controls
117 and just line width for single line controls */
118 INT region_posx
; /* Position of cursor relative to region: */
119 INT region_posy
; /* -1: to left, 0: within, 1: to right */
120 void *word_break_proc
; /* 32-bit word break proc: ANSI or Unicode */
121 INT line_count
; /* number of lines */
122 INT y_offset
; /* scroll offset in number of lines */
123 BOOL bCaptureState
; /* flag indicating whether mouse was captured */
124 BOOL bEnableState
; /* flag keeping the enable state */
125 HWND hwndSelf
; /* the our window handle */
126 HWND hwndParent
; /* Handle of parent for sending EN_* messages.
127 Even if parent will change, EN_* messages
128 should be sent to the first parent. */
129 HWND hwndListBox
; /* handle of ComboBox's listbox or NULL */
130 INT wheelDeltaRemainder
; /* scroll wheel delta left over after scrolling whole lines */
132 * only for multi line controls
134 INT lock_count
; /* amount of re-entries in the EditWndProc */
137 LINEDEF
*first_line_def
; /* linked list of (soft) linebreaks */
138 HLOCAL hloc32W
; /* our unicode local memory block */
139 HLOCAL hloc32A
; /* alias for ANSI control receiving EM_GETHANDLE
141 HLOCAL hlocapp
; /* The text buffer handle belongs to the app */
145 UINT composition_len
; /* length of composition, 0 == no composition */
146 int composition_start
; /* the character position for the composition */
150 SCRIPT_LOGATTR
*logAttr
;
151 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data for single line controls */
155 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
156 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
158 /* used for disabled or read-only edit control */
159 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
161 { /* Notify parent which has created this edit control */ \
162 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
163 SendMessageW(es->hwndParent, WM_COMMAND, \
164 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
165 (LPARAM)(es->hwndSelf)); \
168 static const WCHAR empty_stringW
[] = {0};
169 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
);
171 /*********************************************************************
176 static inline BOOL
EDIT_EM_CanUndo(const EDITSTATE
*es
)
178 return (es
->undo_insert_count
|| strlenW(es
->undo_text
));
182 /*********************************************************************
187 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE
*es
)
189 es
->undo_insert_count
= 0;
190 *es
->undo_text
= '\0';
194 /**********************************************************************
197 * Returns the window version in case Wine emulates a later version
198 * of windows than the application expects.
200 * In a number of cases when windows runs an application that was
201 * designed for an earlier windows version, windows reverts
202 * to "old" behaviour of that earlier version.
204 * An example is a disabled edit control that needs to be painted.
205 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
206 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
207 * applications with an expected version 0f 4.0 or higher.
210 static DWORD
get_app_version(void)
212 static DWORD version
;
215 DWORD dwEmulatedVersion
;
217 DWORD dwProcVersion
= GetProcessVersion(0);
219 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOW
);
220 GetVersionExW( &info
);
221 dwEmulatedVersion
= MAKELONG( info
.dwMinorVersion
, info
.dwMajorVersion
);
222 /* FIXME: this may not be 100% correct; see discussion on the
223 * wine developer list in Nov 1999 */
224 version
= dwProcVersion
< dwEmulatedVersion
? dwProcVersion
: dwEmulatedVersion
;
229 static HBRUSH
EDIT_NotifyCtlColor(EDITSTATE
*es
, HDC hdc
)
234 if ( get_app_version() >= 0x40000 && (!es
->bEnableState
|| (es
->style
& ES_READONLY
)))
235 msg
= WM_CTLCOLORSTATIC
;
237 msg
= WM_CTLCOLOREDIT
;
239 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
242 hbrush
= GetControlBrush(es
->hwndSelf
, hdc
, msg
);
244 hbrush
= (HBRUSH
)SendMessageW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
246 hbrush
= (HBRUSH
)DefWindowProcW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
252 static inline UINT
get_text_length(EDITSTATE
*es
)
254 if(es
->text_length
== (UINT
)-1)
255 es
->text_length
= strlenW(es
->text
);
256 return es
->text_length
;
260 /*********************************************************************
264 * Find the beginning of words.
265 * Note: unlike the specs for a WordBreakProc, this function can
266 * only be called without linebreaks between s[0] up to
267 * s[count - 1]. Remember it is only called
268 * internally, so we can decide this for ourselves.
269 * Additionally we will always be breaking the full string.
272 static INT
EDIT_WordBreakProc(EDITSTATE
*es
, LPWSTR s
, INT index
, INT count
, INT action
)
276 TRACE("s=%p, index=%d, count=%d, action=%d\n", s
, index
, count
, action
);
284 memset(&psa
,0,sizeof(SCRIPT_ANALYSIS
));
285 psa
.eScript
= SCRIPT_UNDEFINED
;
287 es
->logAttr
= HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR
) * get_text_length(es
));
288 ScriptBreak(es
->text
, get_text_length(es
), &psa
, es
->logAttr
);
295 while (index
&& !es
->logAttr
[index
].fSoftBreak
)
302 while (index
< count
&& s
[index
] && !es
->logAttr
[index
].fSoftBreak
)
307 ret
= es
->logAttr
[index
].fWhiteSpace
;
310 ERR("unknown action code, please report !\n");
317 /*********************************************************************
319 * EDIT_CallWordBreakProc
321 * Call appropriate WordBreakProc (internal or external).
323 * Note: The "start" argument should always be an index referring
324 * to es->text. The actual wordbreak proc might be
325 * 16 bit, so we can't always pass any 32 bit LPSTR.
326 * Hence we assume that es->text is the buffer that holds
327 * the string under examination (we can decide this for ourselves).
330 static INT
EDIT_CallWordBreakProc(EDITSTATE
*es
, INT start
, INT index
, INT count
, INT action
)
334 if (es
->word_break_proc
)
338 EDITWORDBREAKPROCW wbpW
= (EDITWORDBREAKPROCW
)es
->word_break_proc
;
340 TRACE_(relay
)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
341 es
->word_break_proc
, debugstr_wn(es
->text
+ start
, count
), index
, count
, action
);
342 ret
= wbpW(es
->text
+ start
, index
, count
, action
);
346 EDITWORDBREAKPROCA wbpA
= (EDITWORDBREAKPROCA
)es
->word_break_proc
;
350 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, NULL
, 0, NULL
, NULL
);
351 textA
= HeapAlloc(GetProcessHeap(), 0, countA
);
354 if (textA
== NULL
) return 0;
356 WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, textA
, countA
, NULL
, NULL
);
357 TRACE_(relay
)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
358 es
->word_break_proc
, debugstr_an(textA
, countA
), index
, countA
, action
);
359 ret
= wbpA(textA
, index
, countA
, action
);
360 HeapFree(GetProcessHeap(), 0, textA
);
364 ret
= EDIT_WordBreakProc(es
, es
->text
, index
+start
, count
+start
, action
) - start
;
369 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF
*line_def
)
373 ScriptStringFree(&line_def
->ssa
);
374 line_def
->ssa
= NULL
;
378 static inline void EDIT_InvalidateUniscribeData(EDITSTATE
*es
)
380 LINEDEF
*line_def
= es
->first_line_def
;
383 EDIT_InvalidateUniscribeData_linedef(line_def
);
384 line_def
= line_def
->next
;
388 ScriptStringFree(&es
->ssa
);
393 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData_linedef(EDITSTATE
*es
, HDC dc
, LINEDEF
*line_def
)
398 if (line_def
->net_length
&& !line_def
->ssa
)
400 int index
= line_def
->index
;
401 HFONT old_font
= NULL
;
403 SCRIPT_TABDEF tabdef
;
407 udc
= GetDC(es
->hwndSelf
);
409 old_font
= SelectObject(udc
, es
->font
);
411 tabdef
.cTabStops
= es
->tabs_count
;
413 tabdef
.pTabStops
= es
->tabs
;
414 tabdef
.iTabOrigin
= 0;
416 hr
= ScriptStringAnalyse(udc
, &es
->text
[index
], line_def
->net_length
,
419 (3*line_def
->net_length
/2+16), -1,
421 (1.5*line_def
->net_length
+16), -1,
423 SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
|SSA_TAB
, -1,
424 NULL
, NULL
, NULL
, &tabdef
, NULL
, &line_def
->ssa
);
427 WARN("ScriptStringAnalyse failed (%x)\n",hr
);
428 line_def
->ssa
= NULL
;
432 SelectObject(udc
, old_font
);
434 ReleaseDC(es
->hwndSelf
, udc
);
437 return line_def
->ssa
;
440 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData(EDITSTATE
*es
, HDC dc
, INT line
)
444 if (!(es
->style
& ES_MULTILINE
))
448 INT length
= get_text_length(es
);
449 HFONT old_font
= NULL
;
453 udc
= GetDC(es
->hwndSelf
);
455 old_font
= SelectObject(udc
, es
->font
);
457 if (es
->style
& ES_PASSWORD
)
460 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
);
462 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
);
467 ScriptStringAnalyse(udc
, es
->text
, length
, (3*length
/2+16), -1, SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
, -1, NULL
, NULL
, NULL
, NULL
, NULL
, &es
->ssa
);
469 ScriptStringAnalyse(udc
, es
->text
, length
, (1.5*length
+16), -1, SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
, -1, NULL
, NULL
, NULL
, NULL
, NULL
, &es
->ssa
);
473 SelectObject(udc
, old_font
);
475 ReleaseDC(es
->hwndSelf
, udc
);
481 line_def
= es
->first_line_def
;
482 while (line_def
&& line
)
484 line_def
= line_def
->next
;
488 return EDIT_UpdateUniscribeData_linedef(es
,dc
,line_def
);
492 static inline INT
get_vertical_line_count(EDITSTATE
*es
)
494 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
498 /*********************************************************************
500 * EDIT_BuildLineDefs_ML
502 * Build linked list of text lines.
503 * Lines can end with '\0' (last line), a character (if it is wrapped),
504 * a soft return '\r\r\n' or a hard return '\r\n'
507 static void EDIT_BuildLineDefs_ML(EDITSTATE
*es
, INT istart
, INT iend
, INT delta
, HRGN hrgn
)
509 LPWSTR current_position
, cp
;
511 LINEDEF
*current_line
;
512 LINEDEF
*previous_line
;
514 INT line_index
= 0, nstart_line
, nstart_index
;
515 INT line_count
= es
->line_count
;
520 if (istart
== iend
&& delta
== 0)
523 previous_line
= NULL
;
524 current_line
= es
->first_line_def
;
526 /* Find starting line. istart must lie inside an existing line or
527 * at the end of buffer */
529 if (istart
< current_line
->index
+ current_line
->length
||
530 current_line
->ending
== END_0
)
533 previous_line
= current_line
;
534 current_line
= current_line
->next
;
536 } while (current_line
);
538 if (!current_line
) /* Error occurred start is not inside previous buffer */
540 FIXME(" modification occurred outside buffer\n");
544 /* Remember start of modifications in order to calculate update region */
545 nstart_line
= line_index
;
546 nstart_index
= current_line
->index
;
548 /* We must start to reformat from the previous line since the modifications
549 * may have caused the line to wrap upwards. */
550 if (!(es
->style
& ES_AUTOHSCROLL
) && line_index
> 0)
553 current_line
= previous_line
;
555 start_line
= current_line
;
557 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
558 current_position
= es
->text
+ current_line
->index
;
559 vlc
= get_vertical_line_count(es
);
561 if (current_line
!= start_line
)
563 if (!current_line
|| current_line
->index
+ delta
> current_position
- es
->text
)
565 /* The buffer has been expanded, create a new line and
566 insert it into the link list */
567 LINEDEF
*new_line
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
));
570 if (new_line
== NULL
)
573 new_line
->next
= previous_line
->next
;
574 previous_line
->next
= new_line
;
575 current_line
= new_line
;
578 else if (current_line
->index
+ delta
< current_position
- es
->text
)
580 /* The previous line merged with this line so we delete this extra entry */
581 previous_line
->next
= current_line
->next
;
582 HeapFree(GetProcessHeap(), 0, current_line
);
583 current_line
= previous_line
->next
;
587 else /* current_line->index + delta == current_position */
589 if (current_position
- es
->text
> iend
)
590 break; /* We reached end of line modifications */
591 /* else recalculate this line */
595 current_line
->index
= current_position
- es
->text
;
596 orig_net_length
= current_line
->net_length
;
598 /* Find end of line */
599 cp
= current_position
;
601 if (*cp
== '\n') break;
602 if ((*cp
== '\r') && (*(cp
+ 1) == '\n'))
607 /* Mark type of line termination */
609 current_line
->ending
= END_0
;
610 current_line
->net_length
= strlenW(current_position
);
611 } else if ((cp
> current_position
) && (*(cp
- 1) == '\r')) {
612 current_line
->ending
= END_SOFT
;
613 current_line
->net_length
= cp
- current_position
- 1;
614 } else if (*cp
== '\n') {
615 current_line
->ending
= END_RICH
;
616 current_line
->net_length
= cp
- current_position
;
618 current_line
->ending
= END_HARD
;
619 current_line
->net_length
= cp
- current_position
;
622 if (current_line
->net_length
)
625 EDIT_InvalidateUniscribeData_linedef(current_line
);
626 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
627 if (current_line
->ssa
)
629 sz
= ScriptString_pSize(current_line
->ssa
);
630 /* Calculate line width */
631 current_line
->width
= sz
->cx
;
633 else current_line
->width
= es
->char_width
* current_line
->net_length
;
635 else current_line
->width
= 0;
637 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
639 /* Line breaks just look back from the end and find the next break and try that. */
641 if (!(es
->style
& ES_AUTOHSCROLL
)) {
642 if (current_line
->width
> fw
&& fw
> es
->char_width
) {
649 prev
= current_line
->net_length
- 1;
650 w
= current_line
->net_length
;
651 d
= (float)current_line
->width
/(float)fw
;
652 if (d
> 1.2f
) d
-= 0.2f
;
654 if (next
>= prev
) next
= prev
-1;
656 prev
= EDIT_CallWordBreakProc(es
, current_position
- es
->text
,
657 next
, current_line
->net_length
, WB_LEFT
);
658 current_line
->net_length
= prev
;
659 EDIT_InvalidateUniscribeData_linedef(current_line
);
660 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
661 if (current_line
->ssa
)
662 sz
= ScriptString_pSize(current_line
->ssa
);
665 current_line
->width
= sz
->cx
;
669 } while (prev
&& current_line
->width
> fw
);
670 current_line
->net_length
= w
;
672 if (prev
== 0) { /* Didn't find a line break so force a break */
676 EDIT_InvalidateUniscribeData_linedef(current_line
);
677 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
679 if (current_line
->ssa
)
681 count
= ScriptString_pcOutChars(current_line
->ssa
);
682 piDx
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * (*count
));
683 ScriptStringGetLogicalWidths(current_line
->ssa
,piDx
);
685 prev
= current_line
->net_length
-1;
687 current_line
->width
-= piDx
[prev
];
689 } while ( prev
> 0 && current_line
->width
> fw
);
692 HeapFree(GetProcessHeap(),0,piDx
);
695 prev
= (fw
/ es
->char_width
);
698 /* If the first line we are calculating, wrapped before istart, we must
699 * adjust istart in order for this to be reflected in the update region. */
700 if (current_line
->index
== nstart_index
&& istart
> current_line
->index
+ prev
)
701 istart
= current_line
->index
+ prev
;
702 /* else if we are updating the previous line before the first line we
703 * are re-calculating and it expanded */
704 else if (current_line
== start_line
&&
705 current_line
->index
!= nstart_index
&& orig_net_length
< prev
)
707 /* Line expanded due to an upwards line wrap so we must partially include
708 * previous line in update region */
709 nstart_line
= line_index
;
710 nstart_index
= current_line
->index
;
711 istart
= current_line
->index
+ orig_net_length
;
714 current_line
->net_length
= prev
;
715 current_line
->ending
= END_WRAP
;
717 if (current_line
->net_length
> 0)
719 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
720 if (current_line
->ssa
)
722 sz
= ScriptString_pSize(current_line
->ssa
);
723 current_line
->width
= sz
->cx
;
726 current_line
->width
= 0;
728 else current_line
->width
= 0;
730 else if (current_line
== start_line
&&
731 current_line
->index
!= nstart_index
&&
732 orig_net_length
< current_line
->net_length
) {
733 /* The previous line expanded but it's still not as wide as the client rect */
734 /* The expansion is due to an upwards line wrap so we must partially include
735 it in the update region */
736 nstart_line
= line_index
;
737 nstart_index
= current_line
->index
;
738 istart
= current_line
->index
+ orig_net_length
;
743 /* Adjust length to include line termination */
744 switch (current_line
->ending
) {
746 current_line
->length
= current_line
->net_length
+ 3;
749 current_line
->length
= current_line
->net_length
+ 1;
752 current_line
->length
= current_line
->net_length
+ 2;
756 current_line
->length
= current_line
->net_length
;
759 es
->text_width
= max(es
->text_width
, current_line
->width
);
760 current_position
+= current_line
->length
;
761 previous_line
= current_line
;
763 /* Discard data for non-visible lines. It will be calculated as needed */
764 if ((line_index
< es
->y_offset
) || (line_index
> es
->y_offset
+ vlc
))
765 EDIT_InvalidateUniscribeData_linedef(current_line
);
767 current_line
= current_line
->next
;
769 } while (previous_line
->ending
!= END_0
);
771 /* Finish adjusting line indexes by delta or remove hanging lines */
772 if (previous_line
->ending
== END_0
)
774 LINEDEF
*pnext
= NULL
;
776 previous_line
->next
= NULL
;
779 pnext
= current_line
->next
;
780 EDIT_InvalidateUniscribeData_linedef(current_line
);
781 HeapFree(GetProcessHeap(), 0, current_line
);
782 current_line
= pnext
;
790 current_line
->index
+= delta
;
791 current_line
= current_line
->next
;
795 /* Calculate rest of modification rectangle */
800 * We calculate two rectangles. One for the first line which may have
801 * an indent with respect to the format rect. The other is a format-width
802 * rectangle that spans the rest of the lines that changed or moved.
804 rc
.top
= es
->format_rect
.top
+ nstart_line
* es
->line_height
-
805 (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
806 rc
.bottom
= rc
.top
+ es
->line_height
;
807 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
))
808 rc
.left
= es
->format_rect
.left
;
810 rc
.left
= LOWORD(EDIT_EM_PosFromChar(es
, nstart_index
, FALSE
));
811 rc
.right
= es
->format_rect
.right
;
812 SetRectRgn(hrgn
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
815 rc
.left
= es
->format_rect
.left
;
816 rc
.right
= es
->format_rect
.right
;
818 * If lines were added or removed we must re-paint the remainder of the
819 * lines since the remaining lines were either shifted up or down.
821 if (line_count
< es
->line_count
) /* We added lines */
822 rc
.bottom
= es
->line_count
* es
->line_height
;
823 else if (line_count
> es
->line_count
) /* We removed lines */
824 rc
.bottom
= line_count
* es
->line_height
;
826 rc
.bottom
= line_index
* es
->line_height
;
827 rc
.bottom
+= es
->format_rect
.top
;
828 rc
.bottom
-= (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
829 tmphrgn
= CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
830 CombineRgn(hrgn
, hrgn
, tmphrgn
, RGN_OR
);
831 DeleteObject(tmphrgn
);
835 /*********************************************************************
837 * EDIT_CalcLineWidth_SL
840 static void EDIT_CalcLineWidth_SL(EDITSTATE
*es
)
842 EDIT_UpdateUniscribeData(es
, NULL
, 0);
846 size
= ScriptString_pSize(es
->ssa
);
847 es
->text_width
= size
->cx
;
853 /*********************************************************************
857 * Beware: This is not the function called on EM_CHARFROMPOS
858 * The position _can_ be outside the formatting / client
860 * The return value is only the character index
863 static INT
EDIT_CharFromPos(EDITSTATE
*es
, INT x
, INT y
, LPBOOL after_wrap
)
867 if (es
->style
& ES_MULTILINE
) {
869 INT line
= (y
- es
->format_rect
.top
) / es
->line_height
+ es
->y_offset
;
871 LINEDEF
*line_def
= es
->first_line_def
;
872 EDIT_UpdateUniscribeData(es
, NULL
, line
);
873 while ((line
> 0) && line_def
->next
) {
874 line_index
+= line_def
->length
;
875 line_def
= line_def
->next
;
879 x
+= es
->x_offset
- es
->format_rect
.left
;
880 if (es
->style
& ES_RIGHT
)
881 x
-= (es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
;
882 else if (es
->style
& ES_CENTER
)
883 x
-= ((es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
) / 2;
884 if (x
>= line_def
->width
) {
886 *after_wrap
= (line_def
->ending
== END_WRAP
);
887 return line_index
+ line_def
->net_length
;
889 if (x
<= 0 || !line_def
->ssa
) {
895 ScriptStringXtoCP(line_def
->ssa
, x
, &index
, &trailing
);
896 if (trailing
) index
++;
899 *after_wrap
= ((index
== line_index
+ line_def
->net_length
) &&
900 (line_def
->ending
== END_WRAP
));
906 x
-= es
->format_rect
.left
;
912 INT indent
= (es
->format_rect
.right
- es
->format_rect
.left
) - es
->text_width
;
913 if (es
->style
& ES_RIGHT
)
915 else if (es
->style
& ES_CENTER
)
919 EDIT_UpdateUniscribeData(es
, NULL
, 0);
924 if (es
->x_offset
>= get_text_length(es
))
927 size
= ScriptString_pSize(es
->ssa
);
930 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
937 if (x
+ xoff
> 0 || !es
->ssa
)
939 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
940 if (trailing
) index
++;
949 const SIZE
*size
= NULL
;
951 size
= ScriptString_pSize(es
->ssa
);
954 else if (x
> size
->cx
)
955 index
= get_text_length(es
);
958 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
959 if (trailing
) index
++;
965 index
= es
->x_offset
;
972 /*********************************************************************
976 * adjusts the point to be within the formatting rectangle
977 * (so CharFromPos returns the nearest _visible_ character)
980 static void EDIT_ConfinePoint(const EDITSTATE
*es
, LPINT x
, LPINT y
)
982 *x
= min(max(*x
, es
->format_rect
.left
), es
->format_rect
.right
- 1);
983 *y
= min(max(*y
, es
->format_rect
.top
), es
->format_rect
.bottom
- 1);
987 /*********************************************************************
992 static INT
EDIT_EM_LineFromChar(EDITSTATE
*es
, INT index
)
997 if (!(es
->style
& ES_MULTILINE
))
999 if (index
> (INT
)get_text_length(es
))
1000 return es
->line_count
- 1;
1002 index
= min(es
->selection_start
, es
->selection_end
);
1005 line_def
= es
->first_line_def
;
1006 index
-= line_def
->length
;
1007 while ((index
>= 0) && line_def
->next
) {
1009 line_def
= line_def
->next
;
1010 index
-= line_def
->length
;
1016 /*********************************************************************
1021 static INT
EDIT_EM_LineIndex(const EDITSTATE
*es
, INT line
)
1024 const LINEDEF
*line_def
;
1026 if (!(es
->style
& ES_MULTILINE
))
1028 if (line
>= es
->line_count
)
1032 line_def
= es
->first_line_def
;
1034 INT index
= es
->selection_end
- line_def
->length
;
1035 while ((index
>= 0) && line_def
->next
) {
1036 line_index
+= line_def
->length
;
1037 line_def
= line_def
->next
;
1038 index
-= line_def
->length
;
1042 line_index
+= line_def
->length
;
1043 line_def
= line_def
->next
;
1051 /*********************************************************************
1056 static INT
EDIT_EM_LineLength(EDITSTATE
*es
, INT index
)
1060 if (!(es
->style
& ES_MULTILINE
))
1061 return get_text_length(es
);
1064 /* get the number of remaining non-selected chars of selected lines */
1065 INT32 l
; /* line number */
1066 INT32 li
; /* index of first char in line */
1068 l
= EDIT_EM_LineFromChar(es
, es
->selection_start
);
1069 /* # chars before start of selection area */
1070 count
= es
->selection_start
- EDIT_EM_LineIndex(es
, l
);
1071 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1072 /* # chars after end of selection */
1073 li
= EDIT_EM_LineIndex(es
, l
);
1074 count
+= li
+ EDIT_EM_LineLength(es
, li
) - es
->selection_end
;
1077 line_def
= es
->first_line_def
;
1078 index
-= line_def
->length
;
1079 while ((index
>= 0) && line_def
->next
) {
1080 line_def
= line_def
->next
;
1081 index
-= line_def
->length
;
1083 return line_def
->net_length
;
1087 /*********************************************************************
1092 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
)
1094 INT len
= get_text_length(es
);
1103 index
= min(index
, len
);
1104 if (es
->style
& ES_MULTILINE
) {
1105 l
= EDIT_EM_LineFromChar(es
, index
);
1106 EDIT_UpdateUniscribeData(es
, NULL
, l
);
1108 y
= (l
- es
->y_offset
) * es
->line_height
;
1109 li
= EDIT_EM_LineIndex(es
, l
);
1110 if (after_wrap
&& (li
== index
) && l
) {
1112 line_def
= es
->first_line_def
;
1114 line_def
= line_def
->next
;
1117 if (line_def
->ending
== END_WRAP
) {
1119 y
-= es
->line_height
;
1120 li
= EDIT_EM_LineIndex(es
, l
);
1124 line_def
= es
->first_line_def
;
1125 while (line_def
->index
!= li
)
1126 line_def
= line_def
->next
;
1128 lw
= line_def
->width
;
1129 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1132 ScriptStringCPtoX(line_def
->ssa
, (index
- 1) - li
, TRUE
, &x
);
1138 if (es
->style
& ES_RIGHT
)
1140 else if (es
->style
& ES_CENTER
)
1145 EDIT_UpdateUniscribeData(es
, NULL
, 0);
1150 if (es
->x_offset
>= get_text_length(es
))
1152 int leftover
= es
->x_offset
- get_text_length(es
);
1156 size
= ScriptString_pSize(es
->ssa
);
1161 xoff
+= es
->char_width
* leftover
;
1164 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
1171 if (index
>= get_text_length(es
))
1176 size
= ScriptString_pSize(es
->ssa
);
1183 ScriptStringCPtoX(es
->ssa
, index
, FALSE
, &xi
);
1189 if (index
>= es
->x_offset
) {
1190 if (!es
->x_offset
&& (es
->style
& (ES_RIGHT
| ES_CENTER
)))
1192 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1193 if (w
> es
->text_width
)
1195 if (es
->style
& ES_RIGHT
)
1196 x
+= w
- es
->text_width
;
1197 else if (es
->style
& ES_CENTER
)
1198 x
+= (w
- es
->text_width
) / 2;
1204 x
+= es
->format_rect
.left
;
1205 y
+= es
->format_rect
.top
;
1206 return MAKELONG((INT16
)x
, (INT16
)y
);
1210 /*********************************************************************
1214 * Calculates the bounding rectangle for a line from a starting
1215 * column to an ending column.
1218 static void EDIT_GetLineRect(EDITSTATE
*es
, INT line
, INT scol
, INT ecol
, LPRECT rc
)
1220 SCRIPT_STRING_ANALYSIS ssa
;
1224 if (es
->style
& ES_MULTILINE
)
1226 const LINEDEF
*line_def
= NULL
;
1227 rc
->top
= es
->format_rect
.top
+ (line
- es
->y_offset
) * es
->line_height
;
1228 if (line
>= es
->line_count
)
1231 line_def
= es
->first_line_def
;
1233 INT index
= es
->selection_end
- line_def
->length
;
1234 while ((index
>= 0) && line_def
->next
) {
1235 line_index
+= line_def
->length
;
1236 line_def
= line_def
->next
;
1237 index
-= line_def
->length
;
1241 line_index
+= line_def
->length
;
1242 line_def
= line_def
->next
;
1246 ssa
= line_def
->ssa
;
1251 rc
->top
= es
->format_rect
.top
;
1255 rc
->bottom
= rc
->top
+ es
->line_height
;
1256 pt1
= (scol
== 0) ? es
->format_rect
.left
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ scol
, TRUE
));
1257 pt2
= (ecol
== -1) ? es
->format_rect
.right
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ ecol
, TRUE
));
1260 ScriptStringCPtoX(ssa
, scol
, FALSE
, &pt3
);
1261 pt3
+=es
->format_rect
.left
;
1264 rc
->right
= max(max(pt1
, pt2
),pt3
);
1265 rc
->left
= min(min(pt1
, pt2
),pt3
);
1269 static inline void text_buffer_changed(EDITSTATE
*es
)
1271 es
->text_length
= (UINT
)-1;
1273 HeapFree( GetProcessHeap(), 0, es
->logAttr
);
1275 EDIT_InvalidateUniscribeData(es
);
1278 /*********************************************************************
1282 static void EDIT_LockBuffer(EDITSTATE
*es
)
1284 if (es
->hlocapp
) return;
1289 /* FIXME: What is this ? */
1290 CHAR
*textA
= NULL
; // ReactOS Hacked! r45670
1297 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1298 textA
= LocalLock(es
->hloc32A
);
1299 //countA = strlen(textA) + 1;
1303 ERR("no buffer ... please report\n");
1307 if (textA
) //// ReactOS
1310 if(!es
->hloc32W
) return;
1314 CHAR
*textA
= LocalLock(es
->hloc32A
);
1317 UINT countW_new
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
1318 if(countW_new
> es
->buffer_size
+ 1)
1320 UINT alloc_size
= ROUND_TO_GROW(countW_new
* sizeof(WCHAR
));
1321 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es
->buffer_size
, countW_new
);
1322 hloc32W_new
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1325 es
->hloc32W
= hloc32W_new
;
1326 es
->buffer_size
= LocalSize(hloc32W_new
)/sizeof(WCHAR
) - 1;
1327 TRACE("Real new size %d+1 WCHARs\n", es
->buffer_size
);
1330 WARN("FAILED! Will synchronize partially\n");
1332 es
->text
= LocalLock(es
->hloc32W
);
1333 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, es
->text
, es
->buffer_size
+ 1);
1334 LocalUnlock(es
->hloc32A
);
1336 else es
->text
= LocalLock(es
->hloc32W
);
1342 /*********************************************************************
1347 static void EDIT_UnlockBuffer(EDITSTATE
*es
, BOOL force
)
1349 if (es
->hlocapp
) return;
1351 /* Edit window might be already destroyed */
1352 if(!IsWindow(es
->hwndSelf
))
1354 WARN("edit hwnd %p already destroyed\n", es
->hwndSelf
);
1358 if (!es
->lock_count
) {
1359 ERR("lock_count == 0 ... please report\n");
1363 ERR("es->text == 0 ... please report\n");
1367 if (force
|| (es
->lock_count
== 1)) {
1370 UINT countW
= get_text_length(es
) + 1;
1374 UINT countA_new
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, NULL
, 0, NULL
, NULL
);
1375 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1376 TRACE("%d WCHARs translated to %d bytes\n", countW
, countA_new
);
1377 countA
= LocalSize(es
->hloc32A
);
1378 if(countA_new
> countA
)
1381 UINT alloc_size
= ROUND_TO_GROW(countA_new
);
1382 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA
, alloc_size
);
1383 hloc32A_new
= LocalReAlloc(es
->hloc32A
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1386 es
->hloc32A
= hloc32A_new
;
1387 countA
= LocalSize(hloc32A_new
);
1388 TRACE("Real new size %d bytes\n", countA
);
1391 WARN("FAILED! Will synchronize partially\n");
1393 WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
,
1394 LocalLock(es
->hloc32A
), countA
, NULL
, NULL
);
1395 LocalUnlock(es
->hloc32A
);
1398 LocalUnlock(es
->hloc32W
);
1402 ERR("no buffer ... please report\n");
1410 /*********************************************************************
1414 * Try to fit size + 1 characters in the buffer.
1416 static BOOL
EDIT_MakeFit(EDITSTATE
*es
, UINT size
)
1420 if (size
<= es
->buffer_size
)
1423 TRACE("trying to ReAlloc to %d+1 characters\n", size
);
1425 /* Force edit to unlock its buffer. es->text now NULL */
1426 EDIT_UnlockBuffer(es
, TRUE
);
1429 UINT alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1430 if ((hNew32W
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
))) {
1431 TRACE("Old 32 bit handle %p, new handle %p\n", es
->hloc32W
, hNew32W
);
1432 es
->hloc32W
= hNew32W
;
1433 es
->buffer_size
= LocalSize(hNew32W
)/sizeof(WCHAR
) - 1;
1437 EDIT_LockBuffer(es
);
1439 if (es
->buffer_size
< size
) {
1440 WARN("FAILED ! We now have %d+1\n", es
->buffer_size
);
1441 EDIT_NOTIFY_PARENT(es
, EN_ERRSPACE
);
1444 TRACE("We now have %d+1\n", es
->buffer_size
);
1450 /*********************************************************************
1454 * Try to fit size + 1 bytes in the undo buffer.
1457 static BOOL
EDIT_MakeUndoFit(EDITSTATE
*es
, UINT size
)
1461 if (size
<= es
->undo_buffer_size
)
1464 TRACE("trying to ReAlloc to %d+1\n", size
);
1466 alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1467 if ((es
->undo_text
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, es
->undo_text
, alloc_size
))) {
1468 es
->undo_buffer_size
= alloc_size
/sizeof(WCHAR
) - 1;
1473 WARN("FAILED ! We now have %d+1\n", es
->undo_buffer_size
);
1479 /*********************************************************************
1481 * EDIT_UpdateTextRegion
1484 static void EDIT_UpdateTextRegion(EDITSTATE
*es
, HRGN hrgn
, BOOL bErase
)
1486 if (es
->flags
& EF_UPDATE
) {
1487 es
->flags
&= ~EF_UPDATE
;
1488 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1490 InvalidateRgn(es
->hwndSelf
, hrgn
, bErase
);
1494 /*********************************************************************
1499 static void EDIT_UpdateText(EDITSTATE
*es
, const RECT
*rc
, BOOL bErase
)
1501 if (es
->flags
& EF_UPDATE
) {
1502 es
->flags
&= ~EF_UPDATE
;
1503 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1505 InvalidateRect(es
->hwndSelf
, rc
, bErase
);
1508 /*********************************************************************
1510 * EDIT_SL_InvalidateText
1512 * Called from EDIT_InvalidateText().
1513 * Does the job for single-line controls only.
1516 static void EDIT_SL_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1521 EDIT_GetLineRect(es
, 0, start
, end
, &line_rect
);
1522 if (IntersectRect(&rc
, &line_rect
, &es
->format_rect
))
1523 EDIT_UpdateText(es
, &rc
, TRUE
);
1526 /*********************************************************************
1528 * EDIT_ML_InvalidateText
1530 * Called from EDIT_InvalidateText().
1531 * Does the job for multi-line controls only.
1534 static void EDIT_ML_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1536 INT vlc
= get_vertical_line_count(es
);
1537 INT sl
= EDIT_EM_LineFromChar(es
, start
);
1538 INT el
= EDIT_EM_LineFromChar(es
, end
);
1547 if ((el
< es
->y_offset
) || (sl
> es
->y_offset
+ vlc
))
1550 sc
= start
- EDIT_EM_LineIndex(es
, sl
);
1551 ec
= end
- EDIT_EM_LineIndex(es
, el
);
1552 if (sl
< es
->y_offset
) {
1556 if (el
> es
->y_offset
+ vlc
) {
1557 el
= es
->y_offset
+ vlc
;
1558 ec
= EDIT_EM_LineLength(es
, EDIT_EM_LineIndex(es
, el
));
1560 GetClientRect(es
->hwndSelf
, &rc1
);
1561 IntersectRect(&rcWnd
, &rc1
, &es
->format_rect
);
1563 EDIT_GetLineRect(es
, sl
, sc
, ec
, &rcLine
);
1564 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1565 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1567 EDIT_GetLineRect(es
, sl
, sc
,
1568 EDIT_EM_LineLength(es
,
1569 EDIT_EM_LineIndex(es
, sl
)),
1571 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1572 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1573 for (l
= sl
+ 1 ; l
< el
; l
++) {
1574 EDIT_GetLineRect(es
, l
, 0,
1575 EDIT_EM_LineLength(es
,
1576 EDIT_EM_LineIndex(es
, l
)),
1578 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1579 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1581 EDIT_GetLineRect(es
, el
, 0, ec
, &rcLine
);
1582 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1583 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1588 /*********************************************************************
1590 * EDIT_InvalidateText
1592 * Invalidate the text from offset start up to, but not including,
1593 * offset end. Useful for (re)painting the selection.
1594 * Regions outside the linewidth are not invalidated.
1595 * end == -1 means end == TextLength.
1596 * start and end need not be ordered.
1599 static void EDIT_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1605 end
= get_text_length(es
);
1613 if (es
->style
& ES_MULTILINE
)
1614 EDIT_ML_InvalidateText(es
, start
, end
);
1616 EDIT_SL_InvalidateText(es
, start
, end
);
1620 /*********************************************************************
1624 * note: unlike the specs say: the order of start and end
1625 * _is_ preserved in Windows. (i.e. start can be > end)
1626 * In other words: this handler is OK
1629 static void EDIT_EM_SetSel(EDITSTATE
*es
, UINT start
, UINT end
, BOOL after_wrap
)
1631 UINT old_start
= es
->selection_start
;
1632 UINT old_end
= es
->selection_end
;
1633 UINT len
= get_text_length(es
);
1635 if (start
== (UINT
)-1) {
1636 start
= es
->selection_end
;
1637 end
= es
->selection_end
;
1639 start
= min(start
, len
);
1640 end
= min(end
, len
);
1642 es
->selection_start
= start
;
1643 es
->selection_end
= end
;
1645 es
->flags
|= EF_AFTER_WRAP
;
1647 es
->flags
&= ~EF_AFTER_WRAP
;
1648 /* Compute the necessary invalidation region. */
1649 /* Note that we don't need to invalidate regions which have
1650 * "never" been selected, or those which are "still" selected.
1651 * In fact, every time we hit a selection boundary, we can
1652 * *toggle* whether we need to invalidate. Thus we can optimize by
1653 * *sorting* the interval endpoints. Let's assume that we sort them
1655 * start <= end <= old_start <= old_end
1656 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1657 * in 5 comparisons; i.e. it is impossible to do better than the
1659 ORDER_UINT(end
, old_end
);
1660 ORDER_UINT(start
, old_start
);
1661 ORDER_UINT(old_start
, old_end
);
1662 ORDER_UINT(start
, end
);
1663 /* Note that at this point 'end' and 'old_start' are not in order, but
1664 * start is definitely the min. and old_end is definitely the max. */
1665 if (end
!= old_start
)
1669 * ORDER_UINT32(end, old_start);
1670 * EDIT_InvalidateText(es, start, end);
1671 * EDIT_InvalidateText(es, old_start, old_end);
1672 * in place of the following if statement.
1673 * (That would complete the optimal five-comparison four-element sort.)
1675 if (old_start
> end
)
1677 EDIT_InvalidateText(es
, start
, end
);
1678 EDIT_InvalidateText(es
, old_start
, old_end
);
1682 EDIT_InvalidateText(es
, start
, old_start
);
1683 EDIT_InvalidateText(es
, end
, old_end
);
1686 else EDIT_InvalidateText(es
, start
, old_end
);
1690 /*********************************************************************
1692 * EDIT_UpdateScrollInfo
1695 static void EDIT_UpdateScrollInfo(EDITSTATE
*es
)
1697 if ((es
->style
& WS_VSCROLL
) && !(es
->flags
& EF_VSCROLL_TRACK
))
1700 si
.cbSize
= sizeof(SCROLLINFO
);
1701 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1703 si
.nMax
= es
->line_count
- 1;
1704 si
.nPage
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1705 si
.nPos
= es
->y_offset
;
1706 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1707 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1708 SetScrollInfo(es
->hwndSelf
, SB_VERT
, &si
, TRUE
);
1711 if ((es
->style
& WS_HSCROLL
) && !(es
->flags
& EF_HSCROLL_TRACK
))
1714 si
.cbSize
= sizeof(SCROLLINFO
);
1715 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1717 si
.nMax
= es
->text_width
- 1;
1718 si
.nPage
= es
->format_rect
.right
- es
->format_rect
.left
;
1719 si
.nPos
= es
->x_offset
;
1720 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1721 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1722 SetScrollInfo(es
->hwndSelf
, SB_HORZ
, &si
, TRUE
);
1727 /*********************************************************************
1729 * EDIT_EM_LineScroll_internal
1731 * Version of EDIT_EM_LineScroll for internal use.
1732 * It doesn't refuse if ES_MULTILINE is set and assumes that
1733 * dx is in pixels, dy - in lines.
1736 static BOOL
EDIT_EM_LineScroll_internal(EDITSTATE
*es
, INT dx
, INT dy
)
1739 INT x_offset_in_pixels
;
1740 INT lines_per_page
= (es
->format_rect
.bottom
- es
->format_rect
.top
) /
1743 if (es
->style
& ES_MULTILINE
)
1745 x_offset_in_pixels
= es
->x_offset
;
1750 x_offset_in_pixels
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->x_offset
, FALSE
));
1753 if (-dx
> x_offset_in_pixels
)
1754 dx
= -x_offset_in_pixels
;
1755 if (dx
> es
->text_width
- x_offset_in_pixels
)
1756 dx
= es
->text_width
- x_offset_in_pixels
;
1757 nyoff
= max(0, es
->y_offset
+ dy
);
1758 if (nyoff
>= es
->line_count
- lines_per_page
)
1759 nyoff
= max(0, es
->line_count
- lines_per_page
);
1760 dy
= (es
->y_offset
- nyoff
) * es
->line_height
;
1765 es
->y_offset
= nyoff
;
1766 if(es
->style
& ES_MULTILINE
)
1769 es
->x_offset
+= dx
/ es
->char_width
;
1771 GetClientRect(es
->hwndSelf
, &rc1
);
1772 IntersectRect(&rc
, &rc1
, &es
->format_rect
);
1773 ScrollWindowEx(es
->hwndSelf
, -dx
, dy
,
1774 NULL
, &rc
, NULL
, NULL
, SW_INVALIDATE
);
1775 /* force scroll info update */
1776 EDIT_UpdateScrollInfo(es
);
1778 if (dx
&& !(es
->flags
& EF_HSCROLL_TRACK
))
1779 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
1780 if (dy
&& !(es
->flags
& EF_VSCROLL_TRACK
))
1781 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
1785 /*********************************************************************
1789 * NOTE: dx is in average character widths, dy - in lines;
1792 static BOOL
EDIT_EM_LineScroll(EDITSTATE
*es
, INT dx
, INT dy
)
1794 if (!(es
->style
& ES_MULTILINE
))
1797 dx
*= es
->char_width
;
1798 return EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1802 /*********************************************************************
1807 static LRESULT
EDIT_EM_Scroll(EDITSTATE
*es
, INT action
)
1811 if (!(es
->style
& ES_MULTILINE
))
1812 return (LRESULT
)FALSE
;
1822 if (es
->y_offset
< es
->line_count
- 1)
1827 dy
= -(es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1830 if (es
->y_offset
< es
->line_count
- 1)
1831 dy
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1834 return (LRESULT
)FALSE
;
1837 INT vlc
= get_vertical_line_count(es
);
1838 /* check if we are going to move too far */
1839 if(es
->y_offset
+ dy
> es
->line_count
- vlc
)
1840 dy
= max(es
->line_count
- vlc
, 0) - es
->y_offset
;
1842 /* Notification is done in EDIT_EM_LineScroll */
1844 EDIT_EM_LineScroll(es
, 0, dy
);
1845 return MAKELONG(dy
, TRUE
);
1849 return (LRESULT
)FALSE
;
1853 /*********************************************************************
1858 static void EDIT_SetCaretPos(EDITSTATE
*es
, INT pos
,
1861 LRESULT res
= EDIT_EM_PosFromChar(es
, pos
, after_wrap
);
1862 TRACE("%d - %dx%d\n", pos
, (short)LOWORD(res
), (short)HIWORD(res
));
1863 SetCaretPos((short)LOWORD(res
), (short)HIWORD(res
));
1867 /*********************************************************************
1872 static void EDIT_EM_ScrollCaret(EDITSTATE
*es
)
1874 if (es
->style
& ES_MULTILINE
) {
1878 INT cw
= es
->char_width
;
1883 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1884 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
));
1885 vlc
= get_vertical_line_count(es
);
1886 if (l
>= es
->y_offset
+ vlc
)
1887 dy
= l
- vlc
+ 1 - es
->y_offset
;
1888 if (l
< es
->y_offset
)
1889 dy
= l
- es
->y_offset
;
1890 ww
= es
->format_rect
.right
- es
->format_rect
.left
;
1891 if (x
< es
->format_rect
.left
)
1892 dx
= x
- es
->format_rect
.left
- ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1893 if (x
> es
->format_rect
.right
)
1894 dx
= x
- es
->format_rect
.left
- (HSCROLL_FRACTION
- 1) * ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1895 if (dy
|| dx
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1897 /* check if we are going to move too far */
1898 if(es
->x_offset
+ dx
+ ww
> es
->text_width
)
1899 dx
= es
->text_width
- ww
- es
->x_offset
;
1900 if(dx
|| dy
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1901 EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1908 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1909 format_width
= es
->format_rect
.right
- es
->format_rect
.left
;
1910 if (x
< es
->format_rect
.left
) {
1911 goal
= es
->format_rect
.left
+ format_width
/ HSCROLL_FRACTION
;
1914 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1915 } while ((x
< goal
) && es
->x_offset
);
1916 /* FIXME: use ScrollWindow() somehow to improve performance */
1917 EDIT_UpdateText(es
, NULL
, TRUE
);
1918 } else if (x
> es
->format_rect
.right
) {
1920 INT len
= get_text_length(es
);
1921 goal
= es
->format_rect
.right
- format_width
/ HSCROLL_FRACTION
;
1924 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1925 x_last
= (short)LOWORD(EDIT_EM_PosFromChar(es
, len
, FALSE
));
1926 } while ((x
> goal
) && (x_last
> es
->format_rect
.right
));
1927 /* FIXME: use ScrollWindow() somehow to improve performance */
1928 EDIT_UpdateText(es
, NULL
, TRUE
);
1932 if(es
->flags
& EF_FOCUSED
)
1933 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
1937 /*********************************************************************
1942 static void EDIT_MoveBackward(EDITSTATE
*es
, BOOL extend
)
1944 INT e
= es
->selection_end
;
1948 if ((es
->style
& ES_MULTILINE
) && e
&&
1949 (es
->text
[e
- 1] == '\r') && (es
->text
[e
] == '\n')) {
1951 if (e
&& (es
->text
[e
- 1] == '\r'))
1955 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1956 EDIT_EM_ScrollCaret(es
);
1960 /*********************************************************************
1964 * Only for multi line controls
1965 * Move the caret one line down, on a column with the nearest
1966 * x coordinate on the screen (might be a different column).
1969 static void EDIT_MoveDown_ML(EDITSTATE
*es
, BOOL extend
)
1971 INT s
= es
->selection_start
;
1972 INT e
= es
->selection_end
;
1973 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
1974 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
1975 INT x
= (short)LOWORD(pos
);
1976 INT y
= (short)HIWORD(pos
);
1978 e
= EDIT_CharFromPos(es
, x
, y
+ es
->line_height
, &after_wrap
);
1981 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
1982 EDIT_EM_ScrollCaret(es
);
1986 /*********************************************************************
1991 static void EDIT_MoveEnd(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
1993 BOOL after_wrap
= FALSE
;
1996 /* Pass a high value in x to make sure of receiving the end of the line */
1997 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
1998 e
= EDIT_CharFromPos(es
, 0x3fffffff,
1999 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), &after_wrap
);
2001 e
= get_text_length(es
);
2002 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, after_wrap
);
2003 EDIT_EM_ScrollCaret(es
);
2007 /*********************************************************************
2012 static void EDIT_MoveForward(EDITSTATE
*es
, BOOL extend
)
2014 INT e
= es
->selection_end
;
2018 if ((es
->style
& ES_MULTILINE
) && (es
->text
[e
- 1] == '\r')) {
2019 if (es
->text
[e
] == '\n')
2021 else if ((es
->text
[e
] == '\r') && (es
->text
[e
+ 1] == '\n'))
2025 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
2026 EDIT_EM_ScrollCaret(es
);
2030 /*********************************************************************
2034 * Home key: move to beginning of line.
2037 static void EDIT_MoveHome(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
2041 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2042 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
2043 e
= EDIT_CharFromPos(es
, -es
->x_offset
,
2044 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), NULL
);
2047 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
2048 EDIT_EM_ScrollCaret(es
);
2052 /*********************************************************************
2054 * EDIT_MovePageDown_ML
2056 * Only for multi line controls
2057 * Move the caret one page down, on a column with the nearest
2058 * x coordinate on the screen (might be a different column).
2061 static void EDIT_MovePageDown_ML(EDITSTATE
*es
, BOOL extend
)
2063 INT s
= es
->selection_start
;
2064 INT e
= es
->selection_end
;
2065 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2066 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2067 INT x
= (short)LOWORD(pos
);
2068 INT y
= (short)HIWORD(pos
);
2070 e
= EDIT_CharFromPos(es
, x
,
2071 y
+ (es
->format_rect
.bottom
- es
->format_rect
.top
),
2075 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2076 EDIT_EM_ScrollCaret(es
);
2080 /*********************************************************************
2082 * EDIT_MovePageUp_ML
2084 * Only for multi line controls
2085 * Move the caret one page up, on a column with the nearest
2086 * x coordinate on the screen (might be a different column).
2089 static void EDIT_MovePageUp_ML(EDITSTATE
*es
, BOOL extend
)
2091 INT s
= es
->selection_start
;
2092 INT e
= es
->selection_end
;
2093 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2094 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2095 INT x
= (short)LOWORD(pos
);
2096 INT y
= (short)HIWORD(pos
);
2098 e
= EDIT_CharFromPos(es
, x
,
2099 y
- (es
->format_rect
.bottom
- es
->format_rect
.top
),
2103 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2104 EDIT_EM_ScrollCaret(es
);
2108 /*********************************************************************
2112 * Only for multi line controls
2113 * Move the caret one line up, on a column with the nearest
2114 * x coordinate on the screen (might be a different column).
2117 static void EDIT_MoveUp_ML(EDITSTATE
*es
, BOOL extend
)
2119 INT s
= es
->selection_start
;
2120 INT e
= es
->selection_end
;
2121 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2122 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2123 INT x
= (short)LOWORD(pos
);
2124 INT y
= (short)HIWORD(pos
);
2126 e
= EDIT_CharFromPos(es
, x
, y
- es
->line_height
, &after_wrap
);
2129 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2130 EDIT_EM_ScrollCaret(es
);
2134 /*********************************************************************
2136 * EDIT_MoveWordBackward
2139 static void EDIT_MoveWordBackward(EDITSTATE
*es
, BOOL extend
)
2141 INT s
= es
->selection_start
;
2142 INT e
= es
->selection_end
;
2147 l
= EDIT_EM_LineFromChar(es
, e
);
2148 ll
= EDIT_EM_LineLength(es
, e
);
2149 li
= EDIT_EM_LineIndex(es
, l
);
2152 li
= EDIT_EM_LineIndex(es
, l
- 1);
2153 e
= li
+ EDIT_EM_LineLength(es
, li
);
2156 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
2160 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2161 EDIT_EM_ScrollCaret(es
);
2165 /*********************************************************************
2167 * EDIT_MoveWordForward
2170 static void EDIT_MoveWordForward(EDITSTATE
*es
, BOOL extend
)
2172 INT s
= es
->selection_start
;
2173 INT e
= es
->selection_end
;
2178 l
= EDIT_EM_LineFromChar(es
, e
);
2179 ll
= EDIT_EM_LineLength(es
, e
);
2180 li
= EDIT_EM_LineIndex(es
, l
);
2182 if ((es
->style
& ES_MULTILINE
) && (l
!= es
->line_count
- 1))
2183 e
= EDIT_EM_LineIndex(es
, l
+ 1);
2185 e
= li
+ EDIT_CallWordBreakProc(es
,
2186 li
, e
- li
+ 1, ll
, WB_RIGHT
);
2190 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2191 EDIT_EM_ScrollCaret(es
);
2195 /*********************************************************************
2200 static INT
EDIT_PaintText(EDITSTATE
*es
, HDC dc
, INT x
, INT y
, INT line
, INT col
, INT count
, BOOL rev
)
2204 LOGFONTW underline_font
;
2205 HFONT hUnderline
= 0;
2214 BkMode
= GetBkMode(dc
);
2215 BkColor
= GetBkColor(dc
);
2216 TextColor
= GetTextColor(dc
);
2218 if (es
->composition_len
== 0)
2220 SetBkColor(dc
, GetSysColor(COLOR_HIGHLIGHT
));
2221 SetTextColor(dc
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
2222 SetBkMode( dc
, OPAQUE
);
2226 HFONT current
= GetCurrentObject(dc
,OBJ_FONT
);
2227 GetObjectW(current
,sizeof(LOGFONTW
),&underline_font
);
2228 underline_font
.lfUnderline
= TRUE
;
2229 hUnderline
= CreateFontIndirectW(&underline_font
);
2230 old_font
= SelectObject(dc
,hUnderline
);
2233 li
= EDIT_EM_LineIndex(es
, line
);
2234 if (es
->style
& ES_MULTILINE
) {
2235 ret
= (INT
)LOWORD(TabbedTextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
,
2236 es
->tabs_count
, es
->tabs
, es
->format_rect
.left
- es
->x_offset
));
2238 TextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
);
2239 GetTextExtentPoint32W(dc
, es
->text
+ li
+ col
, count
, &size
);
2243 if (es
->composition_len
== 0)
2245 SetBkColor(dc
, BkColor
);
2246 SetTextColor(dc
, TextColor
);
2247 SetBkMode( dc
, BkMode
);
2252 SelectObject(dc
,old_font
);
2254 DeleteObject(hUnderline
);
2261 /*********************************************************************
2266 static void EDIT_PaintLine(EDITSTATE
*es
, HDC dc
, INT line
, BOOL rev
)
2275 SCRIPT_STRING_ANALYSIS ssa
;
2277 if (es
->style
& ES_MULTILINE
) {
2278 INT vlc
= get_vertical_line_count(es
);
2280 if ((line
< es
->y_offset
) || (line
> es
->y_offset
+ vlc
) || (line
>= es
->line_count
))
2285 TRACE("line=%d\n", line
);
2287 ssa
= EDIT_UpdateUniscribeData(es
, dc
, line
);
2288 pos
= EDIT_EM_PosFromChar(es
, EDIT_EM_LineIndex(es
, line
), FALSE
);
2289 x
= (short)LOWORD(pos
);
2290 y
= (short)HIWORD(pos
);
2292 if (es
->style
& ES_MULTILINE
)
2294 int line_idx
= line
;
2296 if (es
->style
& ES_RIGHT
|| es
->style
& ES_CENTER
)
2298 LINEDEF
*line_def
= es
->first_line_def
;
2301 while (line_def
&& line_idx
)
2303 line_def
= line_def
->next
;
2306 w
= es
->format_rect
.right
- es
->format_rect
.left
;
2307 lw
= line_def
->width
;
2309 if (es
->style
& ES_RIGHT
)
2311 else if (es
->style
& ES_CENTER
)
2314 x
+= es
->format_rect
.left
;
2319 li
= EDIT_EM_LineIndex(es
, line
);
2320 ll
= EDIT_EM_LineLength(es
, li
);
2321 s
= min(es
->selection_start
, es
->selection_end
);
2322 e
= max(es
->selection_start
, es
->selection_end
);
2323 s
= min(li
+ ll
, max(li
, s
));
2324 e
= min(li
+ ll
, max(li
, e
));
2328 ScriptStringOut(ssa
, x
, y
, 0, &es
->format_rect
, s
- li
, e
- li
, FALSE
);
2329 else if (rev
&& (s
!= e
) &&
2330 ((es
->flags
& EF_FOCUSED
) || (es
->style
& ES_NOHIDESEL
))) {
2331 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, s
- li
, FALSE
);
2332 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, s
- li
, e
- s
, TRUE
);
2333 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, e
- li
, li
+ ll
- e
, FALSE
);
2335 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, ll
, FALSE
);
2339 /*********************************************************************
2341 * EDIT_AdjustFormatRect
2343 * Adjusts the format rectangle for the current font and the
2344 * current client rectangle.
2347 static void EDIT_AdjustFormatRect(EDITSTATE
*es
)
2351 es
->format_rect
.right
= max(es
->format_rect
.right
, es
->format_rect
.left
+ es
->char_width
);
2352 if (es
->style
& ES_MULTILINE
)
2354 INT fw
, vlc
, max_x_offset
, max_y_offset
;
2356 vlc
= get_vertical_line_count(es
);
2357 es
->format_rect
.bottom
= es
->format_rect
.top
+ vlc
* es
->line_height
;
2359 /* correct es->x_offset */
2360 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2361 max_x_offset
= es
->text_width
- fw
;
2362 if(max_x_offset
< 0) max_x_offset
= 0;
2363 if(es
->x_offset
> max_x_offset
)
2364 es
->x_offset
= max_x_offset
;
2366 /* correct es->y_offset */
2367 max_y_offset
= es
->line_count
- vlc
;
2368 if(max_y_offset
< 0) max_y_offset
= 0;
2369 if(es
->y_offset
> max_y_offset
)
2370 es
->y_offset
= max_y_offset
;
2372 /* force scroll info update */
2373 EDIT_UpdateScrollInfo(es
);
2376 /* Windows doesn't care to fix text placement for SL controls */
2377 es
->format_rect
.bottom
= es
->format_rect
.top
+ es
->line_height
;
2379 /* Always stay within the client area */
2380 GetClientRect(es
->hwndSelf
, &ClientRect
);
2381 es
->format_rect
.bottom
= min(es
->format_rect
.bottom
, ClientRect
.bottom
);
2383 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
))
2384 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2386 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
2390 /*********************************************************************
2394 * note: this is not (exactly) the handler called on EM_SETRECTNP
2395 * it is also used to set the rect of a single line control
2398 static void EDIT_SetRectNP(EDITSTATE
*es
, const RECT
*rc
)
2402 ExStyle
= GetWindowLongPtrW(es
->hwndSelf
, GWL_EXSTYLE
);
2404 CopyRect(&es
->format_rect
, rc
);
2406 if (ExStyle
& WS_EX_CLIENTEDGE
) {
2407 es
->format_rect
.left
++;
2408 es
->format_rect
.right
--;
2410 if (es
->format_rect
.bottom
- es
->format_rect
.top
2411 >= es
->line_height
+ 2)
2413 es
->format_rect
.top
++;
2414 es
->format_rect
.bottom
--;
2417 else if (es
->style
& WS_BORDER
) {
2418 bw
= GetSystemMetrics(SM_CXBORDER
) + 1;
2419 bh
= GetSystemMetrics(SM_CYBORDER
) + 1;
2420 es
->format_rect
.left
+= bw
;
2421 es
->format_rect
.right
-= bw
;
2422 if (es
->format_rect
.bottom
- es
->format_rect
.top
2423 >= es
->line_height
+ 2 * bh
)
2425 es
->format_rect
.top
+= bh
;
2426 es
->format_rect
.bottom
-= bh
;
2430 es
->format_rect
.left
+= es
->left_margin
;
2431 es
->format_rect
.right
-= es
->right_margin
;
2432 EDIT_AdjustFormatRect(es
);
2436 /*********************************************************************
2440 * returns line number (not index) in high-order word of result.
2441 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2442 * to Richedit, not to the edit control. Original documentation is valid.
2443 * FIXME: do the specs mean to return -1 if outside client area or
2444 * if outside formatting rectangle ???
2447 static LRESULT
EDIT_EM_CharFromPos(EDITSTATE
*es
, INT x
, INT y
)
2455 GetClientRect(es
->hwndSelf
, &rc
);
2456 if (!PtInRect(&rc
, pt
))
2459 index
= EDIT_CharFromPos(es
, x
, y
, NULL
);
2460 return MAKELONG(index
, EDIT_EM_LineFromChar(es
, index
));
2464 /*********************************************************************
2468 * Enable or disable soft breaks.
2470 * This means: insert or remove the soft linebreak character (\r\r\n).
2471 * Take care to check if the text still fits the buffer after insertion.
2472 * If not, notify with EN_ERRSPACE.
2475 static BOOL
EDIT_EM_FmtLines(EDITSTATE
*es
, BOOL add_eol
)
2477 es
->flags
&= ~EF_USE_SOFTBRK
;
2479 es
->flags
|= EF_USE_SOFTBRK
;
2480 FIXME("soft break enabled, not implemented\n");
2486 /*********************************************************************
2490 * Hopefully this won't fire back at us.
2491 * We always start with a fixed buffer in the local heap.
2492 * Despite of the documentation says that the local heap is used
2493 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2494 * buffer on the local heap.
2497 static HLOCAL
EDIT_EM_GetHandle(EDITSTATE
*es
)
2501 if (!(es
->style
& ES_MULTILINE
))
2505 hLocal
= es
->hloc32W
;
2511 UINT countA
, alloc_size
;
2512 TRACE("Allocating 32-bit ANSI alias buffer\n");
2513 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, NULL
, 0, NULL
, NULL
);
2514 alloc_size
= ROUND_TO_GROW(countA
);
2515 if(!(es
->hloc32A
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
2517 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size
);
2520 textA
= LocalLock(es
->hloc32A
);
2521 WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, countA
, NULL
, NULL
);
2522 LocalUnlock(es
->hloc32A
);
2524 hLocal
= es
->hloc32A
;
2527 EDIT_UnlockBuffer(es
, TRUE
);
2529 /* The text buffer handle belongs to the app */
2530 es
->hlocapp
= hLocal
;
2532 TRACE("Returning %p, LocalSize() = %ld\n", hLocal
, LocalSize(hLocal
));
2537 /*********************************************************************
2542 static INT
EDIT_EM_GetLine(EDITSTATE
*es
, INT line
, LPWSTR dst
, BOOL unicode
)
2545 INT line_len
, dst_len
;
2548 if (es
->style
& ES_MULTILINE
) {
2549 if (line
>= es
->line_count
)
2553 i
= EDIT_EM_LineIndex(es
, line
);
2555 line_len
= EDIT_EM_LineLength(es
, i
);
2556 dst_len
= *(WORD
*)dst
;
2559 if(dst_len
<= line_len
)
2561 memcpy(dst
, src
, dst_len
* sizeof(WCHAR
));
2564 else /* Append 0 if enough space */
2566 memcpy(dst
, src
, line_len
* sizeof(WCHAR
));
2573 INT ret
= WideCharToMultiByte(CP_ACP
, 0, src
, line_len
, (LPSTR
)dst
, dst_len
, NULL
, NULL
);
2574 if(!ret
&& line_len
) /* Insufficient buffer size */
2576 if(ret
< dst_len
) /* Append 0 if enough space */
2577 ((LPSTR
)dst
)[ret
] = 0;
2583 /*********************************************************************
2588 static LRESULT
EDIT_EM_GetSel(const EDITSTATE
*es
, PUINT start
, PUINT end
)
2590 UINT s
= es
->selection_start
;
2591 UINT e
= es
->selection_end
;
2598 return MAKELONG(s
, e
);
2602 /*********************************************************************
2606 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2609 static void EDIT_EM_ReplaceSel(EDITSTATE
*es
, BOOL can_undo
, LPCWSTR lpsz_replace
, BOOL send_update
, BOOL honor_limit
)
2611 UINT strl
= strlenW(lpsz_replace
);
2612 UINT tl
= get_text_length(es
);
2623 TRACE("%s, can_undo %d, send_update %d\n",
2624 debugstr_w(lpsz_replace
), can_undo
, send_update
);
2626 s
= es
->selection_start
;
2627 e
= es
->selection_end
;
2629 EDIT_InvalidateUniscribeData(es
);
2630 if ((s
== e
) && !strl
)
2635 size
= tl
- (e
- s
) + strl
;
2639 /* Issue the EN_MAXTEXT notification and continue with replacing text
2640 * so that buffer limit is honored. */
2641 if ((honor_limit
) && (size
> es
->buffer_limit
)) {
2642 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2643 /* Buffer limit can be smaller than the actual length of text in combobox */
2644 if (es
->buffer_limit
< (tl
- (e
-s
)))
2647 strl
= min(strl
, es
->buffer_limit
- (tl
- (e
-s
)));
2650 if (!EDIT_MakeFit(es
, tl
- (e
- s
) + strl
))
2654 /* there is something to be deleted */
2655 TRACE("deleting stuff.\n");
2657 buf
= HeapAlloc(GetProcessHeap(), 0, (bufl
+ 1) * sizeof(WCHAR
));
2659 memcpy(buf
, es
->text
+ s
, bufl
* sizeof(WCHAR
));
2660 buf
[bufl
] = 0; /* ensure 0 termination */
2662 strcpyW(es
->text
+ s
, es
->text
+ e
);
2663 text_buffer_changed(es
);
2666 /* there is an insertion */
2667 tl
= get_text_length(es
);
2668 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
));
2669 for (p
= es
->text
+ tl
; p
>= es
->text
+ s
; p
--)
2671 for (i
= 0 , p
= es
->text
+ s
; i
< strl
; i
++)
2672 p
[i
] = lpsz_replace
[i
];
2673 if(es
->style
& ES_UPPERCASE
)
2674 CharUpperBuffW(p
, strl
);
2675 else if(es
->style
& ES_LOWERCASE
)
2676 CharLowerBuffW(p
, strl
);
2677 text_buffer_changed(es
);
2679 if (es
->style
& ES_MULTILINE
)
2681 INT st
= min(es
->selection_start
, es
->selection_end
);
2682 INT vlc
= get_vertical_line_count(es
);
2684 hrgn
= CreateRectRgn(0, 0, 0, 0);
2685 EDIT_BuildLineDefs_ML(es
, st
, st
+ strl
,
2686 strl
- abs(es
->selection_end
- es
->selection_start
), hrgn
);
2687 /* if text is too long undo all changes */
2688 if (honor_limit
&& !(es
->style
& ES_AUTOVSCROLL
) && (es
->line_count
> vlc
)) {
2690 strcpyW(es
->text
+ e
, es
->text
+ e
+ strl
);
2692 for (i
= 0 , p
= es
->text
; i
< e
- s
; i
++)
2694 text_buffer_changed(es
);
2695 EDIT_BuildLineDefs_ML(es
, s
, e
,
2696 abs(es
->selection_end
- es
->selection_start
) - strl
, hrgn
);
2699 hrgn
= CreateRectRgn(0, 0, 0, 0);
2700 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2704 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2705 EDIT_InvalidateUniscribeData(es
);
2706 EDIT_CalcLineWidth_SL(es
);
2707 /* remove chars that don't fit */
2708 if (honor_limit
&& !(es
->style
& ES_AUTOHSCROLL
) && (es
->text_width
> fw
)) {
2709 while ((es
->text_width
> fw
) && s
+ strl
>= s
) {
2710 strcpyW(es
->text
+ s
+ strl
- 1, es
->text
+ s
+ strl
);
2712 es
->text_length
= -1;
2713 EDIT_InvalidateUniscribeData(es
);
2714 EDIT_CalcLineWidth_SL(es
);
2716 text_buffer_changed(es
);
2717 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2723 utl
= strlenW(es
->undo_text
);
2724 if (!es
->undo_insert_count
&& (*es
->undo_text
&& (s
== es
->undo_position
))) {
2725 /* undo-buffer is extended to the right */
2726 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2727 memcpy(es
->undo_text
+ utl
, buf
, (e
- s
)*sizeof(WCHAR
));
2728 (es
->undo_text
+ utl
)[e
- s
] = 0; /* ensure 0 termination */
2729 } else if (!es
->undo_insert_count
&& (*es
->undo_text
&& (e
== es
->undo_position
))) {
2730 /* undo-buffer is extended to the left */
2731 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2732 for (p
= es
->undo_text
+ utl
; p
>= es
->undo_text
; p
--)
2734 for (i
= 0 , p
= es
->undo_text
; i
< e
- s
; i
++)
2736 es
->undo_position
= s
;
2738 /* new undo-buffer */
2739 EDIT_MakeUndoFit(es
, e
- s
);
2740 memcpy(es
->undo_text
, buf
, (e
- s
)*sizeof(WCHAR
));
2741 es
->undo_text
[e
- s
] = 0; /* ensure 0 termination */
2742 es
->undo_position
= s
;
2744 /* any deletion makes the old insertion-undo invalid */
2745 es
->undo_insert_count
= 0;
2747 EDIT_EM_EmptyUndoBuffer(es
);
2751 if ((s
== es
->undo_position
) ||
2752 ((es
->undo_insert_count
) &&
2753 (s
== es
->undo_position
+ es
->undo_insert_count
)))
2755 * insertion is new and at delete position or
2756 * an extension to either left or right
2758 es
->undo_insert_count
+= strl
;
2760 /* new insertion undo */
2761 es
->undo_position
= s
;
2762 es
->undo_insert_count
= strl
;
2763 /* new insertion makes old delete-buffer invalid */
2764 *es
->undo_text
= '\0';
2767 EDIT_EM_EmptyUndoBuffer(es
);
2770 HeapFree(GetProcessHeap(), 0, buf
);
2774 /* If text has been deleted and we're right or center aligned then scroll rightward */
2775 if (es
->style
& (ES_RIGHT
| ES_CENTER
))
2777 INT delta
= strl
- abs(es
->selection_end
- es
->selection_start
);
2779 if (delta
< 0 && es
->x_offset
)
2781 if (abs(delta
) > es
->x_offset
)
2784 es
->x_offset
+= delta
;
2788 EDIT_EM_SetSel(es
, s
, s
, FALSE
);
2789 es
->flags
|= EF_MODIFIED
;
2790 if (send_update
) es
->flags
|= EF_UPDATE
;
2793 EDIT_UpdateTextRegion(es
, hrgn
, TRUE
);
2797 EDIT_UpdateText(es
, NULL
, TRUE
);
2799 EDIT_EM_ScrollCaret(es
);
2801 /* force scroll info update */
2802 EDIT_UpdateScrollInfo(es
);
2805 if(send_update
|| (es
->flags
& EF_UPDATE
))
2807 es
->flags
&= ~EF_UPDATE
;
2808 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
2810 EDIT_InvalidateUniscribeData(es
);
2814 /*********************************************************************
2818 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2821 static void EDIT_EM_SetHandle(EDITSTATE
*es
, HLOCAL hloc
)
2823 if (!(es
->style
& ES_MULTILINE
))
2827 WARN("called with NULL handle\n");
2831 EDIT_UnlockBuffer(es
, TRUE
);
2837 LocalFree(es
->hloc32A
);
2849 countA
= LocalSize(hloc
);
2850 textA
= LocalLock(hloc
);
2851 countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
2852 if(!(hloc32W_new
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, countW
* sizeof(WCHAR
))))
2854 ERR("Could not allocate new unicode buffer\n");
2857 textW
= LocalLock(hloc32W_new
);
2858 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, textW
, countW
);
2859 LocalUnlock(hloc32W_new
);
2863 LocalFree(es
->hloc32W
);
2865 es
->hloc32W
= hloc32W_new
;
2869 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
2871 /* The text buffer handle belongs to the control */
2874 EDIT_LockBuffer(es
);
2875 text_buffer_changed(es
);
2877 es
->x_offset
= es
->y_offset
= 0;
2878 es
->selection_start
= es
->selection_end
= 0;
2879 EDIT_EM_EmptyUndoBuffer(es
);
2880 es
->flags
&= ~EF_MODIFIED
;
2881 es
->flags
&= ~EF_UPDATE
;
2882 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2883 EDIT_UpdateText(es
, NULL
, TRUE
);
2884 EDIT_EM_ScrollCaret(es
);
2885 /* force scroll info update */
2886 EDIT_UpdateScrollInfo(es
);
2890 /*********************************************************************
2894 * NOTE: this version currently implements WinNT limits
2897 static void EDIT_EM_SetLimitText(EDITSTATE
*es
, UINT limit
)
2899 if (!limit
) limit
= ~0u;
2900 if (!(es
->style
& ES_MULTILINE
)) limit
= min(limit
, 0x7ffffffe);
2901 es
->buffer_limit
= limit
;
2905 /*********************************************************************
2909 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2910 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2911 * margin according to the textmetrics of the current font.
2913 * When EC_USEFONTINFO is used in the non_cjk case the margins only
2914 * change if the edit control is equal to or larger than a certain
2915 * size. Though there is an exception for the empty client rect case
2916 * with small font sizes.
2918 static BOOL
is_cjk(UINT charset
)
2922 case SHIFTJIS_CHARSET
:
2923 case HANGUL_CHARSET
:
2924 case GB2312_CHARSET
:
2925 case CHINESEBIG5_CHARSET
:
2928 /* HANGUL_CHARSET is strange, though treated as CJK by Win 8, it is
2929 * not by other versions including Win 10. */
2933 static void EDIT_EM_SetMargins(EDITSTATE
*es
, INT action
,
2934 WORD left
, WORD right
, BOOL repaint
)
2937 INT default_left_margin
= 0; /* in pixels */
2938 INT default_right_margin
= 0; /* in pixels */
2940 /* Set the default margins depending on the font */
2941 if (es
->font
&& (left
== EC_USEFONTINFO
|| right
== EC_USEFONTINFO
)) {
2942 HDC dc
= GetDC(es
->hwndSelf
);
2943 HFONT old_font
= SelectObject(dc
, es
->font
);
2944 LONG width
= GdiGetCharDimensions(dc
, &tm
, NULL
);
2947 /* The default margins are only non zero for TrueType or Vector fonts */
2948 if (tm
.tmPitchAndFamily
& ( TMPF_VECTOR
| TMPF_TRUETYPE
)) {
2949 if (!is_cjk(tm
.tmCharSet
)) {
2950 default_left_margin
= width
/ 2;
2951 default_right_margin
= width
/ 2;
2953 GetClientRect(es
->hwndSelf
, &rc
);
2954 if (rc
.right
- rc
.left
< (width
/ 2 + width
) * 2 &&
2955 (width
>= 28 || !IsRectEmpty(&rc
)) ) {
2956 default_left_margin
= es
->left_margin
;
2957 default_right_margin
= es
->right_margin
;
2960 /* FIXME: figure out the CJK values. They are not affected by the client rect. */
2961 default_left_margin
= width
/ 2;
2962 default_right_margin
= width
/ 2;
2965 SelectObject(dc
, old_font
);
2966 ReleaseDC(es
->hwndSelf
, dc
);
2969 if (action
& EC_LEFTMARGIN
) {
2970 es
->format_rect
.left
-= es
->left_margin
;
2971 if (left
!= EC_USEFONTINFO
)
2972 es
->left_margin
= left
;
2974 es
->left_margin
= default_left_margin
;
2975 es
->format_rect
.left
+= es
->left_margin
;
2978 if (action
& EC_RIGHTMARGIN
) {
2979 es
->format_rect
.right
+= es
->right_margin
;
2980 if (right
!= EC_USEFONTINFO
)
2981 es
->right_margin
= right
;
2983 es
->right_margin
= default_right_margin
;
2984 es
->format_rect
.right
-= es
->right_margin
;
2987 if (action
& (EC_LEFTMARGIN
| EC_RIGHTMARGIN
)) {
2988 EDIT_AdjustFormatRect(es
);
2989 if (repaint
) EDIT_UpdateText(es
, NULL
, TRUE
);
2992 TRACE("left=%d, right=%d\n", es
->left_margin
, es
->right_margin
);
2996 /*********************************************************************
2998 * EM_SETPASSWORDCHAR