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
24 * - EM_GETIMESTATUS, EM_SETIMESTATUS
42 #include "user_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(edit
);
46 WINE_DECLARE_DEBUG_CHANNEL(combo
);
47 WINE_DECLARE_DEBUG_CHANNEL(relay
);
49 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
50 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
51 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
52 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
55 * extra flags for EDITSTATE.flags field
57 #define EF_MODIFIED 0x0001 /* text has been modified */
58 #define EF_FOCUSED 0x0002 /* we have input focus */
59 #define EF_UPDATE 0x0004 /* notify parent of changed state */
60 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
61 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
62 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
63 wrapped line, instead of in front of the next character */
64 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
65 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
69 END_0
= 0, /* line ends with terminating '\0' character */
70 END_WRAP
, /* line is wrapped */
71 END_HARD
, /* line ends with a hard return '\r\n' */
72 END_SOFT
, /* line ends with a soft return '\r\r\n' */
73 END_RICH
/* line ends with a single '\n' */
76 typedef struct tagLINEDEF
{
77 INT length
; /* bruto length of a line in bytes */
78 INT net_length
; /* netto length of a line in visible characters */
80 INT width
; /* width of the line in pixels */
81 INT index
; /* line index into the buffer */
82 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data */
83 struct tagLINEDEF
*next
;
88 BOOL is_unicode
; /* how the control was created */
89 LPWSTR text
; /* the actual contents of the control */
90 UINT text_length
; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
91 UINT buffer_size
; /* the size of the buffer in characters */
92 UINT buffer_limit
; /* the maximum size to which the buffer may grow in characters */
93 HFONT font
; /* NULL means standard system font */
94 INT x_offset
; /* scroll offset for multi lines this is in pixels
95 for single lines it's in characters */
96 INT line_height
; /* height of a screen line in pixels */
97 INT char_width
; /* average character width in pixels */
98 DWORD style
; /* sane version of wnd->dwStyle */
99 WORD flags
; /* flags that are not in es->style or wnd->flags (EF_XXX) */
100 INT undo_insert_count
; /* number of characters inserted in sequence */
101 UINT undo_position
; /* character index of the insertion and deletion */
102 LPWSTR undo_text
; /* deleted text */
103 UINT undo_buffer_size
; /* size of the deleted text buffer */
104 INT selection_start
; /* == selection_end if no selection */
105 INT selection_end
; /* == current caret position */
106 WCHAR password_char
; /* == 0 if no password char, and for multi line controls */
107 INT left_margin
; /* in pixels */
108 INT right_margin
; /* in pixels */
110 INT text_width
; /* width of the widest line in pixels for multi line controls
111 and just line width for single line controls */
112 INT region_posx
; /* Position of cursor relative to region: */
113 INT region_posy
; /* -1: to left, 0: within, 1: to right */
114 void *word_break_proc
; /* 32-bit word break proc: ANSI or Unicode */
115 INT line_count
; /* number of lines */
116 INT y_offset
; /* scroll offset in number of lines */
117 BOOL bCaptureState
; /* flag indicating whether mouse was captured */
118 BOOL bEnableState
; /* flag keeping the enable state */
119 HWND hwndSelf
; /* the our window handle */
120 HWND hwndParent
; /* Handle of parent for sending EN_* messages.
121 Even if parent will change, EN_* messages
122 should be sent to the first parent. */
123 HWND hwndListBox
; /* handle of ComboBox's listbox or NULL */
124 INT wheelDeltaRemainder
; /* scroll wheel delta left over after scrolling whole lines */
126 * only for multi line controls
128 INT lock_count
; /* amount of re-entries in the EditWndProc */
131 LINEDEF
*first_line_def
; /* linked list of (soft) linebreaks */
132 HLOCAL hloc32W
; /* our unicode local memory block */
133 HLOCAL hloc32A
; /* alias for ANSI control receiving EM_GETHANDLE
135 HLOCAL hlocapp
; /* The text buffer handle belongs to the app */
139 UINT composition_len
; /* length of composition, 0 == no composition */
140 int composition_start
; /* the character position for the composition */
144 SCRIPT_LOGATTR
*logAttr
;
145 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data for single line controls */
149 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
150 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
152 /* used for disabled or read-only edit control */
153 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
155 { /* Notify parent which has created this edit control */ \
156 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
157 SendMessageW(es->hwndParent, WM_COMMAND, \
158 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
159 (LPARAM)(es->hwndSelf)); \
162 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
);
164 /*********************************************************************
169 static inline BOOL
EDIT_EM_CanUndo(const EDITSTATE
*es
)
171 return (es
->undo_insert_count
|| lstrlenW(es
->undo_text
));
175 /*********************************************************************
180 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE
*es
)
182 es
->undo_insert_count
= 0;
183 *es
->undo_text
= '\0';
187 /**********************************************************************
190 * Returns the window version in case Wine emulates a later version
191 * of windows than the application expects.
193 * In a number of cases when windows runs an application that was
194 * designed for an earlier windows version, windows reverts
195 * to "old" behaviour of that earlier version.
197 * An example is a disabled edit control that needs to be painted.
198 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
199 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
200 * applications with an expected version 0f 4.0 or higher.
203 static DWORD
get_app_version(void)
205 static DWORD version
;
208 DWORD dwEmulatedVersion
;
210 DWORD dwProcVersion
= GetProcessVersion(0);
212 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOW
);
213 GetVersionExW( &info
);
214 dwEmulatedVersion
= MAKELONG( info
.dwMinorVersion
, info
.dwMajorVersion
);
215 /* FIXME: this may not be 100% correct; see discussion on the
216 * wine developer list in Nov 1999 */
217 version
= dwProcVersion
< dwEmulatedVersion
? dwProcVersion
: dwEmulatedVersion
;
222 static HBRUSH
EDIT_NotifyCtlColor(EDITSTATE
*es
, HDC hdc
)
227 if ( get_app_version() >= 0x40000 && (!es
->bEnableState
|| (es
->style
& ES_READONLY
)))
228 msg
= WM_CTLCOLORSTATIC
;
230 msg
= WM_CTLCOLOREDIT
;
232 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
233 hbrush
= (HBRUSH
)SendMessageW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
235 hbrush
= (HBRUSH
)DefWindowProcW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
240 static inline UINT
get_text_length(EDITSTATE
*es
)
242 if(es
->text_length
== (UINT
)-1)
243 es
->text_length
= lstrlenW(es
->text
);
244 return es
->text_length
;
248 /*********************************************************************
252 * Find the beginning of words.
253 * Note: unlike the specs for a WordBreakProc, this function can
254 * only be called without linebreaks between s[0] up to
255 * s[count - 1]. Remember it is only called
256 * internally, so we can decide this for ourselves.
257 * Additionally we will always be breaking the full string.
260 static INT
EDIT_WordBreakProc(EDITSTATE
*es
, LPWSTR s
, INT index
, INT count
, INT action
)
264 TRACE("s=%p, index=%d, count=%d, action=%d\n", s
, index
, count
, action
);
272 memset(&psa
,0,sizeof(SCRIPT_ANALYSIS
));
273 psa
.eScript
= SCRIPT_UNDEFINED
;
275 es
->logAttr
= HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR
) * get_text_length(es
));
276 ScriptBreak(es
->text
, get_text_length(es
), &psa
, es
->logAttr
);
283 while (index
&& !es
->logAttr
[index
].fSoftBreak
)
290 while (index
< count
&& s
[index
] && !es
->logAttr
[index
].fSoftBreak
)
295 ret
= es
->logAttr
[index
].fWhiteSpace
;
298 ERR("unknown action code, please report !\n");
305 /*********************************************************************
307 * EDIT_CallWordBreakProc
309 * Call appropriate WordBreakProc (internal or external).
311 * Note: The "start" argument should always be an index referring
312 * to es->text. The actual wordbreak proc might be
313 * 16 bit, so we can't always pass any 32 bit LPSTR.
314 * Hence we assume that es->text is the buffer that holds
315 * the string under examination (we can decide this for ourselves).
318 static INT
EDIT_CallWordBreakProc(EDITSTATE
*es
, INT start
, INT index
, INT count
, INT action
)
322 if (es
->word_break_proc
)
326 EDITWORDBREAKPROCW wbpW
= (EDITWORDBREAKPROCW
)es
->word_break_proc
;
328 TRACE_(relay
)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
329 es
->word_break_proc
, debugstr_wn(es
->text
+ start
, count
), index
, count
, action
);
330 ret
= wbpW(es
->text
+ start
, index
, count
, action
);
331 TRACE_(relay
)("\1Ret wordbreakW %p retval=%d\n", es
->word_break_proc
, ret
);
335 EDITWORDBREAKPROCA wbpA
= (EDITWORDBREAKPROCA
)es
->word_break_proc
;
339 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, NULL
, 0, NULL
, NULL
);
340 textA
= HeapAlloc(GetProcessHeap(), 0, countA
);
341 WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, textA
, countA
, NULL
, NULL
);
342 TRACE_(relay
)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
343 es
->word_break_proc
, debugstr_an(textA
, countA
), index
, countA
, action
);
344 ret
= wbpA(textA
, index
, countA
, action
);
345 HeapFree(GetProcessHeap(), 0, textA
);
346 TRACE_(relay
)("\1Ret wordbreakA %p retval=%d\n", es
->word_break_proc
, ret
);
350 ret
= EDIT_WordBreakProc(es
, es
->text
, index
+start
, count
+start
, action
) - start
;
355 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF
*line_def
)
359 ScriptStringFree(&line_def
->ssa
);
360 line_def
->ssa
= NULL
;
364 static inline void EDIT_InvalidateUniscribeData(EDITSTATE
*es
)
366 LINEDEF
*line_def
= es
->first_line_def
;
369 EDIT_InvalidateUniscribeData_linedef(line_def
);
370 line_def
= line_def
->next
;
374 ScriptStringFree(&es
->ssa
);
379 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData_linedef(EDITSTATE
*es
, HDC dc
, LINEDEF
*line_def
)
384 if (line_def
->net_length
&& !line_def
->ssa
)
386 int index
= line_def
->index
;
387 HFONT old_font
= NULL
;
389 SCRIPT_TABDEF tabdef
;
393 udc
= GetDC(es
->hwndSelf
);
395 old_font
= SelectObject(udc
, es
->font
);
397 tabdef
.cTabStops
= es
->tabs_count
;
398 tabdef
.iScale
= GdiGetCharDimensions(udc
, NULL
, NULL
);
399 tabdef
.pTabStops
= es
->tabs
;
400 tabdef
.iTabOrigin
= 0;
402 hr
= ScriptStringAnalyse(udc
, &es
->text
[index
], line_def
->net_length
,
403 (1.5*line_def
->net_length
+16), -1,
404 SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
|SSA_TAB
, -1,
405 NULL
, NULL
, NULL
, &tabdef
, NULL
, &line_def
->ssa
);
408 WARN("ScriptStringAnalyse failed (%x)\n",hr
);
409 line_def
->ssa
= NULL
;
413 SelectObject(udc
, old_font
);
415 ReleaseDC(es
->hwndSelf
, udc
);
418 return line_def
->ssa
;
421 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData(EDITSTATE
*es
, HDC dc
, INT line
)
425 if (!(es
->style
& ES_MULTILINE
))
429 INT length
= get_text_length(es
);
430 HFONT old_font
= NULL
;
434 udc
= GetDC(es
->hwndSelf
);
436 old_font
= SelectObject(udc
, es
->font
);
438 if (es
->style
& ES_PASSWORD
)
439 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
);
441 ScriptStringAnalyse(udc
, es
->text
, length
, (1.5*length
+16), -1, SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
, -1, NULL
, NULL
, NULL
, NULL
, NULL
, &es
->ssa
);
444 SelectObject(udc
, old_font
);
446 ReleaseDC(es
->hwndSelf
, udc
);
452 line_def
= es
->first_line_def
;
453 while (line_def
&& line
)
455 line_def
= line_def
->next
;
459 return EDIT_UpdateUniscribeData_linedef(es
,dc
,line_def
);
463 static inline INT
get_vertical_line_count(EDITSTATE
*es
)
465 INT vlc
= es
->line_height
? (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
: 0;
469 /*********************************************************************
471 * EDIT_BuildLineDefs_ML
473 * Build linked list of text lines.
474 * Lines can end with '\0' (last line), a character (if it is wrapped),
475 * a soft return '\r\r\n' or a hard return '\r\n'
478 static void EDIT_BuildLineDefs_ML(EDITSTATE
*es
, INT istart
, INT iend
, INT delta
, HRGN hrgn
)
480 LPWSTR current_position
, cp
;
482 LINEDEF
*current_line
;
483 LINEDEF
*previous_line
;
485 INT line_index
= 0, nstart_line
, nstart_index
;
486 INT line_count
= es
->line_count
;
491 if (istart
== iend
&& delta
== 0)
494 previous_line
= NULL
;
495 current_line
= es
->first_line_def
;
497 /* Find starting line. istart must lie inside an existing line or
498 * at the end of buffer */
500 if (istart
< current_line
->index
+ current_line
->length
||
501 current_line
->ending
== END_0
)
504 previous_line
= current_line
;
505 current_line
= current_line
->next
;
507 } while (current_line
);
509 if (!current_line
) /* Error occurred start is not inside previous buffer */
511 FIXME(" modification occurred outside buffer\n");
515 /* Remember start of modifications in order to calculate update region */
516 nstart_line
= line_index
;
517 nstart_index
= current_line
->index
;
519 /* We must start to reformat from the previous line since the modifications
520 * may have caused the line to wrap upwards. */
521 if (!(es
->style
& ES_AUTOHSCROLL
) && line_index
> 0)
524 current_line
= previous_line
;
526 start_line
= current_line
;
528 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
529 current_position
= es
->text
+ current_line
->index
;
530 vlc
= get_vertical_line_count(es
);
532 if (current_line
!= start_line
)
534 if (!current_line
|| current_line
->index
+ delta
> current_position
- es
->text
)
536 /* The buffer has been expanded, create a new line and
537 insert it into the link list */
538 LINEDEF
*new_line
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
));
539 new_line
->next
= previous_line
->next
;
540 previous_line
->next
= new_line
;
541 current_line
= new_line
;
544 else if (current_line
->index
+ delta
< current_position
- es
->text
)
546 /* The previous line merged with this line so we delete this extra entry */
547 previous_line
->next
= current_line
->next
;
548 HeapFree(GetProcessHeap(), 0, current_line
);
549 current_line
= previous_line
->next
;
553 else /* current_line->index + delta == current_position */
555 if (current_position
- es
->text
> iend
)
556 break; /* We reached end of line modifications */
557 /* else recalculate this line */
561 current_line
->index
= current_position
- es
->text
;
562 orig_net_length
= current_line
->net_length
;
564 /* Find end of line */
565 cp
= current_position
;
567 if (*cp
== '\n') break;
568 if ((*cp
== '\r') && (*(cp
+ 1) == '\n'))
573 /* Mark type of line termination */
575 current_line
->ending
= END_0
;
576 current_line
->net_length
= lstrlenW(current_position
);
577 } else if ((cp
> current_position
) && (*(cp
- 1) == '\r')) {
578 current_line
->ending
= END_SOFT
;
579 current_line
->net_length
= cp
- current_position
- 1;
580 } else if (*cp
== '\n') {
581 current_line
->ending
= END_RICH
;
582 current_line
->net_length
= cp
- current_position
;
584 current_line
->ending
= END_HARD
;
585 current_line
->net_length
= cp
- current_position
;
588 if (current_line
->net_length
)
591 EDIT_InvalidateUniscribeData_linedef(current_line
);
592 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
593 if (current_line
->ssa
)
595 sz
= ScriptString_pSize(current_line
->ssa
);
596 /* Calculate line width */
597 current_line
->width
= sz
->cx
;
599 else current_line
->width
= es
->char_width
* current_line
->net_length
;
601 else current_line
->width
= 0;
603 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
605 /* Line breaks just look back from the end and find the next break and try that. */
607 if (!(es
->style
& ES_AUTOHSCROLL
)) {
608 if (current_line
->width
> fw
&& fw
> es
->char_width
) {
615 prev
= current_line
->net_length
- 1;
616 w
= current_line
->net_length
;
617 d
= (float)current_line
->width
/(float)fw
;
618 if (d
> 1.2f
) d
-= 0.2f
;
620 if (next
>= prev
) next
= prev
-1;
622 prev
= EDIT_CallWordBreakProc(es
, current_position
- es
->text
,
623 next
, current_line
->net_length
, WB_LEFT
);
624 current_line
->net_length
= prev
;
625 EDIT_InvalidateUniscribeData_linedef(current_line
);
626 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
627 if (current_line
->ssa
)
628 sz
= ScriptString_pSize(current_line
->ssa
);
631 current_line
->width
= sz
->cx
;
635 } while (prev
&& current_line
->width
> fw
);
636 current_line
->net_length
= w
;
638 if (prev
== 0) { /* Didn't find a line break so force a break */
642 EDIT_InvalidateUniscribeData_linedef(current_line
);
643 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
645 if (current_line
->ssa
)
647 count
= ScriptString_pcOutChars(current_line
->ssa
);
648 piDx
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * (*count
));
649 ScriptStringGetLogicalWidths(current_line
->ssa
,piDx
);
651 prev
= current_line
->net_length
-1;
653 current_line
->width
-= piDx
[prev
];
655 } while ( prev
> 0 && current_line
->width
> fw
);
658 HeapFree(GetProcessHeap(),0,piDx
);
661 prev
= (fw
/ es
->char_width
);
664 /* If the first line we are calculating, wrapped before istart, we must
665 * adjust istart in order for this to be reflected in the update region. */
666 if (current_line
->index
== nstart_index
&& istart
> current_line
->index
+ prev
)
667 istart
= current_line
->index
+ prev
;
668 /* else if we are updating the previous line before the first line we
669 * are re-calculating and it expanded */
670 else if (current_line
== start_line
&&
671 current_line
->index
!= nstart_index
&& orig_net_length
< prev
)
673 /* Line expanded due to an upwards line wrap so we must partially include
674 * previous line in update region */
675 nstart_line
= line_index
;
676 nstart_index
= current_line
->index
;
677 istart
= current_line
->index
+ orig_net_length
;
680 current_line
->net_length
= prev
;
681 current_line
->ending
= END_WRAP
;
683 if (current_line
->net_length
> 0)
685 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
686 if (current_line
->ssa
)
688 sz
= ScriptString_pSize(current_line
->ssa
);
689 current_line
->width
= sz
->cx
;
692 current_line
->width
= 0;
694 else current_line
->width
= 0;
696 else if (current_line
== start_line
&&
697 current_line
->index
!= nstart_index
&&
698 orig_net_length
< current_line
->net_length
) {
699 /* The previous line expanded but it's still not as wide as the client rect */
700 /* The expansion is due to an upwards line wrap so we must partially include
701 it in the update region */
702 nstart_line
= line_index
;
703 nstart_index
= current_line
->index
;
704 istart
= current_line
->index
+ orig_net_length
;
709 /* Adjust length to include line termination */
710 switch (current_line
->ending
) {
712 current_line
->length
= current_line
->net_length
+ 3;
715 current_line
->length
= current_line
->net_length
+ 1;
718 current_line
->length
= current_line
->net_length
+ 2;
722 current_line
->length
= current_line
->net_length
;
725 es
->text_width
= max(es
->text_width
, current_line
->width
);
726 current_position
+= current_line
->length
;
727 previous_line
= current_line
;
729 /* Discard data for non-visible lines. It will be calculated as needed */
730 if ((line_index
< es
->y_offset
) || (line_index
> es
->y_offset
+ vlc
))
731 EDIT_InvalidateUniscribeData_linedef(current_line
);
733 current_line
= current_line
->next
;
735 } while (previous_line
->ending
!= END_0
);
737 /* Finish adjusting line indexes by delta or remove hanging lines */
738 if (previous_line
->ending
== END_0
)
740 LINEDEF
*pnext
= NULL
;
742 previous_line
->next
= NULL
;
745 pnext
= current_line
->next
;
746 EDIT_InvalidateUniscribeData_linedef(current_line
);
747 HeapFree(GetProcessHeap(), 0, current_line
);
748 current_line
= pnext
;
756 current_line
->index
+= delta
;
757 current_line
= current_line
->next
;
761 /* Calculate rest of modification rectangle */
766 * We calculate two rectangles. One for the first line which may have
767 * an indent with respect to the format rect. The other is a format-width
768 * rectangle that spans the rest of the lines that changed or moved.
770 rc
.top
= es
->format_rect
.top
+ nstart_line
* es
->line_height
-
771 (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
772 rc
.bottom
= rc
.top
+ es
->line_height
;
773 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
))
774 rc
.left
= es
->format_rect
.left
;
776 rc
.left
= LOWORD(EDIT_EM_PosFromChar(es
, nstart_index
, FALSE
));
777 rc
.right
= es
->format_rect
.right
;
778 SetRectRgn(hrgn
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
781 rc
.left
= es
->format_rect
.left
;
782 rc
.right
= es
->format_rect
.right
;
784 * If lines were added or removed we must re-paint the remainder of the
785 * lines since the remaining lines were either shifted up or down.
787 if (line_count
< es
->line_count
) /* We added lines */
788 rc
.bottom
= es
->line_count
* es
->line_height
;
789 else if (line_count
> es
->line_count
) /* We removed lines */
790 rc
.bottom
= line_count
* es
->line_height
;
792 rc
.bottom
= line_index
* es
->line_height
;
793 rc
.bottom
+= es
->format_rect
.top
;
794 rc
.bottom
-= (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
795 tmphrgn
= CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
796 CombineRgn(hrgn
, hrgn
, tmphrgn
, RGN_OR
);
797 DeleteObject(tmphrgn
);
801 /*********************************************************************
803 * EDIT_CalcLineWidth_SL
806 static void EDIT_CalcLineWidth_SL(EDITSTATE
*es
)
808 EDIT_UpdateUniscribeData(es
, NULL
, 0);
812 size
= ScriptString_pSize(es
->ssa
);
813 es
->text_width
= size
->cx
;
819 /*********************************************************************
823 * Beware: This is not the function called on EM_CHARFROMPOS
824 * The position _can_ be outside the formatting / client
826 * The return value is only the character index
829 static INT
EDIT_CharFromPos(EDITSTATE
*es
, INT x
, INT y
, LPBOOL after_wrap
)
833 if (es
->style
& ES_MULTILINE
) {
835 INT line
= (y
- es
->format_rect
.top
) / es
->line_height
+ es
->y_offset
;
837 LINEDEF
*line_def
= es
->first_line_def
;
838 EDIT_UpdateUniscribeData(es
, NULL
, line
);
839 while ((line
> 0) && line_def
->next
) {
840 line_index
+= line_def
->length
;
841 line_def
= line_def
->next
;
845 x
+= es
->x_offset
- es
->format_rect
.left
;
846 if (es
->style
& ES_RIGHT
)
847 x
-= (es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
;
848 else if (es
->style
& ES_CENTER
)
849 x
-= ((es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
) / 2;
850 if (x
>= line_def
->width
) {
852 *after_wrap
= (line_def
->ending
== END_WRAP
);
853 return line_index
+ line_def
->net_length
;
855 if (x
<= 0 || !line_def
->ssa
) {
861 ScriptStringXtoCP(line_def
->ssa
, x
, &index
, &trailing
);
862 if (trailing
) index
++;
865 *after_wrap
= ((index
== line_index
+ line_def
->net_length
) &&
866 (line_def
->ending
== END_WRAP
));
872 x
-= es
->format_rect
.left
;
878 INT indent
= (es
->format_rect
.right
- es
->format_rect
.left
) - es
->text_width
;
879 if (es
->style
& ES_RIGHT
)
881 else if (es
->style
& ES_CENTER
)
885 EDIT_UpdateUniscribeData(es
, NULL
, 0);
890 if (es
->x_offset
>= get_text_length(es
))
893 size
= ScriptString_pSize(es
->ssa
);
896 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
903 if (x
+ xoff
> 0 || !es
->ssa
)
905 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
906 if (trailing
) index
++;
915 const SIZE
*size
= NULL
;
917 size
= ScriptString_pSize(es
->ssa
);
920 else if (x
> size
->cx
)
921 index
= get_text_length(es
);
924 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
925 if (trailing
) index
++;
931 index
= es
->x_offset
;
938 /*********************************************************************
942 * adjusts the point to be within the formatting rectangle
943 * (so CharFromPos returns the nearest _visible_ character)
946 static void EDIT_ConfinePoint(const EDITSTATE
*es
, LPINT x
, LPINT y
)
948 *x
= min(max(*x
, es
->format_rect
.left
), es
->format_rect
.right
- 1);
949 *y
= min(max(*y
, es
->format_rect
.top
), es
->format_rect
.bottom
- 1);
953 /*********************************************************************
958 static INT
EDIT_EM_LineFromChar(EDITSTATE
*es
, INT index
)
963 if (!(es
->style
& ES_MULTILINE
))
965 if (index
> (INT
)get_text_length(es
))
966 return es
->line_count
- 1;
968 index
= min(es
->selection_start
, es
->selection_end
);
971 line_def
= es
->first_line_def
;
972 index
-= line_def
->length
;
973 while ((index
>= 0) && line_def
->next
) {
975 line_def
= line_def
->next
;
976 index
-= line_def
->length
;
982 /*********************************************************************
987 static INT
EDIT_EM_LineIndex(const EDITSTATE
*es
, INT line
)
990 const LINEDEF
*line_def
;
992 if (!(es
->style
& ES_MULTILINE
))
994 if (line
>= es
->line_count
)
998 line_def
= es
->first_line_def
;
1000 INT index
= es
->selection_end
- line_def
->length
;
1001 while ((index
>= 0) && line_def
->next
) {
1002 line_index
+= line_def
->length
;
1003 line_def
= line_def
->next
;
1004 index
-= line_def
->length
;
1008 line_index
+= line_def
->length
;
1009 line_def
= line_def
->next
;
1017 /*********************************************************************
1022 static INT
EDIT_EM_LineLength(EDITSTATE
*es
, INT index
)
1026 if (!(es
->style
& ES_MULTILINE
))
1027 return get_text_length(es
);
1030 /* get the number of remaining non-selected chars of selected lines */
1031 INT32 l
; /* line number */
1032 INT32 li
; /* index of first char in line */
1034 l
= EDIT_EM_LineFromChar(es
, es
->selection_start
);
1035 /* # chars before start of selection area */
1036 count
= es
->selection_start
- EDIT_EM_LineIndex(es
, l
);
1037 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1038 /* # chars after end of selection */
1039 li
= EDIT_EM_LineIndex(es
, l
);
1040 count
+= li
+ EDIT_EM_LineLength(es
, li
) - es
->selection_end
;
1043 line_def
= es
->first_line_def
;
1044 index
-= line_def
->length
;
1045 while ((index
>= 0) && line_def
->next
) {
1046 line_def
= line_def
->next
;
1047 index
-= line_def
->length
;
1049 return line_def
->net_length
;
1053 /*********************************************************************
1058 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
)
1060 INT len
= get_text_length(es
);
1069 index
= min(index
, len
);
1070 if (es
->style
& ES_MULTILINE
) {
1071 l
= EDIT_EM_LineFromChar(es
, index
);
1072 EDIT_UpdateUniscribeData(es
, NULL
, l
);
1074 y
= (l
- es
->y_offset
) * es
->line_height
;
1075 li
= EDIT_EM_LineIndex(es
, l
);
1076 if (after_wrap
&& (li
== index
) && l
) {
1078 line_def
= es
->first_line_def
;
1080 line_def
= line_def
->next
;
1083 if (line_def
->ending
== END_WRAP
) {
1085 y
-= es
->line_height
;
1086 li
= EDIT_EM_LineIndex(es
, l
);
1090 line_def
= es
->first_line_def
;
1091 while (line_def
->index
!= li
)
1092 line_def
= line_def
->next
;
1094 lw
= line_def
->width
;
1095 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1097 ScriptStringCPtoX(line_def
->ssa
, (index
- 1) - li
, TRUE
, &x
);
1100 if (es
->style
& ES_RIGHT
)
1102 else if (es
->style
& ES_CENTER
)
1107 EDIT_UpdateUniscribeData(es
, NULL
, 0);
1112 if (es
->x_offset
>= get_text_length(es
))
1114 int leftover
= es
->x_offset
- get_text_length(es
);
1118 size
= ScriptString_pSize(es
->ssa
);
1123 xoff
+= es
->char_width
* leftover
;
1126 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
1133 if (index
>= get_text_length(es
))
1138 size
= ScriptString_pSize(es
->ssa
);
1145 ScriptStringCPtoX(es
->ssa
, index
, FALSE
, &xi
);
1151 if (index
>= es
->x_offset
) {
1152 if (!es
->x_offset
&& (es
->style
& (ES_RIGHT
| ES_CENTER
)))
1154 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1155 if (w
> es
->text_width
)
1157 if (es
->style
& ES_RIGHT
)
1158 x
+= w
- es
->text_width
;
1159 else if (es
->style
& ES_CENTER
)
1160 x
+= (w
- es
->text_width
) / 2;
1166 x
+= es
->format_rect
.left
;
1167 y
+= es
->format_rect
.top
;
1168 return MAKELONG((INT16
)x
, (INT16
)y
);
1172 /*********************************************************************
1176 * Calculates the bounding rectangle for a line from a starting
1177 * column to an ending column.
1180 static void EDIT_GetLineRect(EDITSTATE
*es
, INT line
, INT scol
, INT ecol
, LPRECT rc
)
1182 SCRIPT_STRING_ANALYSIS ssa
;
1186 if (es
->style
& ES_MULTILINE
)
1188 const LINEDEF
*line_def
= NULL
;
1189 rc
->top
= es
->format_rect
.top
+ (line
- es
->y_offset
) * es
->line_height
;
1190 if (line
>= es
->line_count
)
1193 line_def
= es
->first_line_def
;
1195 INT index
= es
->selection_end
- line_def
->length
;
1196 while ((index
>= 0) && line_def
->next
) {
1197 line_index
+= line_def
->length
;
1198 line_def
= line_def
->next
;
1199 index
-= line_def
->length
;
1203 line_index
+= line_def
->length
;
1204 line_def
= line_def
->next
;
1208 ssa
= line_def
->ssa
;
1213 rc
->top
= es
->format_rect
.top
;
1217 rc
->bottom
= rc
->top
+ es
->line_height
;
1218 pt1
= (scol
== 0) ? es
->format_rect
.left
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ scol
, TRUE
));
1219 pt2
= (ecol
== -1) ? es
->format_rect
.right
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ ecol
, TRUE
));
1222 ScriptStringCPtoX(ssa
, scol
, FALSE
, &pt3
);
1223 pt3
+=es
->format_rect
.left
;
1226 rc
->right
= max(max(pt1
, pt2
),pt3
);
1227 rc
->left
= min(min(pt1
, pt2
),pt3
);
1231 static inline void text_buffer_changed(EDITSTATE
*es
)
1233 es
->text_length
= (UINT
)-1;
1235 HeapFree( GetProcessHeap(), 0, es
->logAttr
);
1237 EDIT_InvalidateUniscribeData(es
);
1240 /*********************************************************************
1244 static void EDIT_LockBuffer(EDITSTATE
*es
)
1248 if(!es
->hloc32W
) return;
1252 CHAR
*textA
= LocalLock(es
->hloc32A
);
1254 UINT countW_new
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
1255 if(countW_new
> es
->buffer_size
+ 1)
1257 UINT alloc_size
= ROUND_TO_GROW(countW_new
* sizeof(WCHAR
));
1258 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es
->buffer_size
, countW_new
);
1259 hloc32W_new
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1262 es
->hloc32W
= hloc32W_new
;
1263 es
->buffer_size
= LocalSize(hloc32W_new
)/sizeof(WCHAR
) - 1;
1264 TRACE("Real new size %d+1 WCHARs\n", es
->buffer_size
);
1267 WARN("FAILED! Will synchronize partially\n");
1269 es
->text
= LocalLock(es
->hloc32W
);
1270 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, es
->text
, es
->buffer_size
+ 1);
1271 LocalUnlock(es
->hloc32A
);
1273 else es
->text
= LocalLock(es
->hloc32W
);
1279 /*********************************************************************
1284 static void EDIT_UnlockBuffer(EDITSTATE
*es
, BOOL force
)
1286 /* Edit window might be already destroyed */
1287 if(!IsWindow(es
->hwndSelf
))
1289 WARN("edit hwnd %p already destroyed\n", es
->hwndSelf
);
1293 if (!es
->lock_count
) {
1294 ERR("lock_count == 0 ... please report\n");
1298 ERR("es->text == 0 ... please report\n");
1301 if (force
|| (es
->lock_count
== 1)) {
1304 UINT countW
= get_text_length(es
) + 1;
1308 UINT countA_new
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, NULL
, 0, NULL
, NULL
);
1309 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1310 TRACE("%d WCHARs translated to %d bytes\n", countW
, countA_new
);
1311 countA
= LocalSize(es
->hloc32A
);
1312 if(countA_new
> countA
)
1315 UINT alloc_size
= ROUND_TO_GROW(countA_new
);
1316 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA
, alloc_size
);
1317 hloc32A_new
= LocalReAlloc(es
->hloc32A
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1320 es
->hloc32A
= hloc32A_new
;
1321 countA
= LocalSize(hloc32A_new
);
1322 TRACE("Real new size %d bytes\n", countA
);
1325 WARN("FAILED! Will synchronize partially\n");
1327 WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
,
1328 LocalLock(es
->hloc32A
), countA
, NULL
, NULL
);
1329 LocalUnlock(es
->hloc32A
);
1332 LocalUnlock(es
->hloc32W
);
1336 ERR("no buffer ... please report\n");
1344 /*********************************************************************
1348 * Try to fit size + 1 characters in the buffer.
1350 static BOOL
EDIT_MakeFit(EDITSTATE
*es
, UINT size
)
1354 if (size
<= es
->buffer_size
)
1357 TRACE("trying to ReAlloc to %d+1 characters\n", size
);
1359 /* Force edit to unlock its buffer. es->text now NULL */
1360 EDIT_UnlockBuffer(es
, TRUE
);
1363 UINT alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1364 if ((hNew32W
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
))) {
1365 TRACE("Old 32 bit handle %p, new handle %p\n", es
->hloc32W
, hNew32W
);
1366 es
->hloc32W
= hNew32W
;
1367 es
->buffer_size
= LocalSize(hNew32W
)/sizeof(WCHAR
) - 1;
1371 EDIT_LockBuffer(es
);
1373 if (es
->buffer_size
< size
) {
1374 WARN("FAILED ! We now have %d+1\n", es
->buffer_size
);
1375 EDIT_NOTIFY_PARENT(es
, EN_ERRSPACE
);
1378 TRACE("We now have %d+1\n", es
->buffer_size
);
1384 /*********************************************************************
1388 * Try to fit size + 1 bytes in the undo buffer.
1391 static BOOL
EDIT_MakeUndoFit(EDITSTATE
*es
, UINT size
)
1395 if (size
<= es
->undo_buffer_size
)
1398 TRACE("trying to ReAlloc to %d+1\n", size
);
1400 alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1401 if ((es
->undo_text
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, es
->undo_text
, alloc_size
))) {
1402 es
->undo_buffer_size
= alloc_size
/sizeof(WCHAR
) - 1;
1407 WARN("FAILED ! We now have %d+1\n", es
->undo_buffer_size
);
1413 /*********************************************************************
1415 * EDIT_UpdateTextRegion
1418 static void EDIT_UpdateTextRegion(EDITSTATE
*es
, HRGN hrgn
, BOOL bErase
)
1420 if (es
->flags
& EF_UPDATE
) {
1421 es
->flags
&= ~EF_UPDATE
;
1422 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1424 InvalidateRgn(es
->hwndSelf
, hrgn
, bErase
);
1428 /*********************************************************************
1433 static void EDIT_UpdateText(EDITSTATE
*es
, const RECT
*rc
, BOOL bErase
)
1435 if (es
->flags
& EF_UPDATE
) {
1436 es
->flags
&= ~EF_UPDATE
;
1437 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1439 InvalidateRect(es
->hwndSelf
, rc
, bErase
);
1442 /*********************************************************************
1444 * EDIT_SL_InvalidateText
1446 * Called from EDIT_InvalidateText().
1447 * Does the job for single-line controls only.
1450 static void EDIT_SL_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1455 EDIT_GetLineRect(es
, 0, start
, end
, &line_rect
);
1456 if (IntersectRect(&rc
, &line_rect
, &es
->format_rect
))
1457 EDIT_UpdateText(es
, &rc
, TRUE
);
1460 /*********************************************************************
1462 * EDIT_ML_InvalidateText
1464 * Called from EDIT_InvalidateText().
1465 * Does the job for multi-line controls only.
1468 static void EDIT_ML_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1470 INT vlc
= get_vertical_line_count(es
);
1471 INT sl
= EDIT_EM_LineFromChar(es
, start
);
1472 INT el
= EDIT_EM_LineFromChar(es
, end
);
1481 if ((el
< es
->y_offset
) || (sl
> es
->y_offset
+ vlc
))
1484 sc
= start
- EDIT_EM_LineIndex(es
, sl
);
1485 ec
= end
- EDIT_EM_LineIndex(es
, el
);
1486 if (sl
< es
->y_offset
) {
1490 if (el
> es
->y_offset
+ vlc
) {
1491 el
= es
->y_offset
+ vlc
;
1492 ec
= EDIT_EM_LineLength(es
, EDIT_EM_LineIndex(es
, el
));
1494 GetClientRect(es
->hwndSelf
, &rc1
);
1495 IntersectRect(&rcWnd
, &rc1
, &es
->format_rect
);
1497 EDIT_GetLineRect(es
, sl
, sc
, ec
, &rcLine
);
1498 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1499 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1501 EDIT_GetLineRect(es
, sl
, sc
,
1502 EDIT_EM_LineLength(es
,
1503 EDIT_EM_LineIndex(es
, sl
)),
1505 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1506 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1507 for (l
= sl
+ 1 ; l
< el
; l
++) {
1508 EDIT_GetLineRect(es
, l
, 0,
1509 EDIT_EM_LineLength(es
,
1510 EDIT_EM_LineIndex(es
, l
)),
1512 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1513 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1515 EDIT_GetLineRect(es
, el
, 0, ec
, &rcLine
);
1516 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1517 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1522 /*********************************************************************
1524 * EDIT_InvalidateText
1526 * Invalidate the text from offset start up to, but not including,
1527 * offset end. Useful for (re)painting the selection.
1528 * Regions outside the linewidth are not invalidated.
1529 * end == -1 means end == TextLength.
1530 * start and end need not be ordered.
1533 static void EDIT_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1539 end
= get_text_length(es
);
1547 if (es
->style
& ES_MULTILINE
)
1548 EDIT_ML_InvalidateText(es
, start
, end
);
1550 EDIT_SL_InvalidateText(es
, start
, end
);
1554 /*********************************************************************
1558 * note: unlike the specs say: the order of start and end
1559 * _is_ preserved in Windows. (i.e. start can be > end)
1560 * In other words: this handler is OK
1563 static void EDIT_EM_SetSel(EDITSTATE
*es
, UINT start
, UINT end
, BOOL after_wrap
)
1565 UINT old_start
= es
->selection_start
;
1566 UINT old_end
= es
->selection_end
;
1567 UINT len
= get_text_length(es
);
1569 if (start
== (UINT
)-1) {
1570 start
= es
->selection_end
;
1571 end
= es
->selection_end
;
1573 start
= min(start
, len
);
1574 end
= min(end
, len
);
1576 es
->selection_start
= start
;
1577 es
->selection_end
= end
;
1579 es
->flags
|= EF_AFTER_WRAP
;
1581 es
->flags
&= ~EF_AFTER_WRAP
;
1582 /* Compute the necessary invalidation region. */
1583 /* Note that we don't need to invalidate regions which have
1584 * "never" been selected, or those which are "still" selected.
1585 * In fact, every time we hit a selection boundary, we can
1586 * *toggle* whether we need to invalidate. Thus we can optimize by
1587 * *sorting* the interval endpoints. Let's assume that we sort them
1589 * start <= end <= old_start <= old_end
1590 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1591 * in 5 comparisons; i.e. it is impossible to do better than the
1593 ORDER_UINT(end
, old_end
);
1594 ORDER_UINT(start
, old_start
);
1595 ORDER_UINT(old_start
, old_end
);
1596 ORDER_UINT(start
, end
);
1597 /* Note that at this point 'end' and 'old_start' are not in order, but
1598 * start is definitely the min. and old_end is definitely the max. */
1599 if (end
!= old_start
)
1603 * ORDER_UINT32(end, old_start);
1604 * EDIT_InvalidateText(es, start, end);
1605 * EDIT_InvalidateText(es, old_start, old_end);
1606 * in place of the following if statement.
1607 * (That would complete the optimal five-comparison four-element sort.)
1609 if (old_start
> end
)
1611 EDIT_InvalidateText(es
, start
, end
);
1612 EDIT_InvalidateText(es
, old_start
, old_end
);
1616 EDIT_InvalidateText(es
, start
, old_start
);
1617 EDIT_InvalidateText(es
, end
, old_end
);
1620 else EDIT_InvalidateText(es
, start
, old_end
);
1624 /*********************************************************************
1626 * EDIT_UpdateScrollInfo
1629 static void EDIT_UpdateScrollInfo(EDITSTATE
*es
)
1631 if ((es
->style
& WS_VSCROLL
) && !(es
->flags
& EF_VSCROLL_TRACK
))
1634 si
.cbSize
= sizeof(SCROLLINFO
);
1635 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1637 si
.nMax
= es
->line_count
- 1;
1638 si
.nPage
= es
->line_height
? (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
: 0;
1639 si
.nPos
= es
->y_offset
;
1640 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1641 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1642 SetScrollInfo(es
->hwndSelf
, SB_VERT
, &si
, TRUE
);
1645 if ((es
->style
& WS_HSCROLL
) && !(es
->flags
& EF_HSCROLL_TRACK
))
1648 si
.cbSize
= sizeof(SCROLLINFO
);
1649 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1651 si
.nMax
= es
->text_width
- 1;
1652 si
.nPage
= es
->format_rect
.right
- es
->format_rect
.left
;
1653 si
.nPos
= es
->x_offset
;
1654 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1655 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1656 SetScrollInfo(es
->hwndSelf
, SB_HORZ
, &si
, TRUE
);
1661 /*********************************************************************
1663 * EDIT_EM_LineScroll_internal
1665 * Version of EDIT_EM_LineScroll for internal use.
1666 * It doesn't refuse if ES_MULTILINE is set and assumes that
1667 * dx is in pixels, dy - in lines.
1670 static BOOL
EDIT_EM_LineScroll_internal(EDITSTATE
*es
, INT dx
, INT dy
)
1673 INT x_offset_in_pixels
;
1676 if (!es
->line_height
|| !es
->char_width
)
1679 lines_per_page
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1681 if (es
->style
& ES_MULTILINE
)
1683 x_offset_in_pixels
= es
->x_offset
;
1688 x_offset_in_pixels
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->x_offset
, FALSE
));
1691 if (-dx
> x_offset_in_pixels
)
1692 dx
= -x_offset_in_pixels
;
1693 if (dx
> es
->text_width
- x_offset_in_pixels
)
1694 dx
= es
->text_width
- x_offset_in_pixels
;
1695 nyoff
= max(0, es
->y_offset
+ dy
);
1696 if (nyoff
>= es
->line_count
- lines_per_page
)
1697 nyoff
= max(0, es
->line_count
- lines_per_page
);
1698 dy
= (es
->y_offset
- nyoff
) * es
->line_height
;
1703 es
->y_offset
= nyoff
;
1704 if(es
->style
& ES_MULTILINE
)
1707 es
->x_offset
+= dx
/ es
->char_width
;
1709 GetClientRect(es
->hwndSelf
, &rc1
);
1710 IntersectRect(&rc
, &rc1
, &es
->format_rect
);
1711 ScrollWindowEx(es
->hwndSelf
, -dx
, dy
,
1712 NULL
, &rc
, NULL
, NULL
, SW_INVALIDATE
);
1713 /* force scroll info update */
1714 EDIT_UpdateScrollInfo(es
);
1716 if (dx
&& !(es
->flags
& EF_HSCROLL_TRACK
))
1717 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
1718 if (dy
&& !(es
->flags
& EF_VSCROLL_TRACK
))
1719 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
1723 /*********************************************************************
1727 * NOTE: dx is in average character widths, dy - in lines;
1730 static BOOL
EDIT_EM_LineScroll(EDITSTATE
*es
, INT dx
, INT dy
)
1732 if (!(es
->style
& ES_MULTILINE
))
1735 dx
*= es
->char_width
;
1736 return EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1740 /*********************************************************************
1745 static LRESULT
EDIT_EM_Scroll(EDITSTATE
*es
, INT action
)
1749 if (!(es
->style
& ES_MULTILINE
))
1750 return (LRESULT
)FALSE
;
1760 if (es
->y_offset
< es
->line_count
- 1)
1765 dy
= -(es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1768 if (es
->y_offset
< es
->line_count
- 1)
1769 dy
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1772 return (LRESULT
)FALSE
;
1775 INT vlc
= get_vertical_line_count(es
);
1776 /* check if we are going to move too far */
1777 if(es
->y_offset
+ dy
> es
->line_count
- vlc
)
1778 dy
= max(es
->line_count
- vlc
, 0) - es
->y_offset
;
1780 /* Notification is done in EDIT_EM_LineScroll */
1782 EDIT_EM_LineScroll(es
, 0, dy
);
1783 return MAKELONG(dy
, TRUE
);
1787 return (LRESULT
)FALSE
;
1791 /*********************************************************************
1796 static void EDIT_SetCaretPos(EDITSTATE
*es
, INT pos
,
1801 if (es
->flags
& EF_FOCUSED
)
1803 res
= EDIT_EM_PosFromChar(es
, pos
, after_wrap
);
1804 TRACE("%d - %dx%d\n", pos
, (short)LOWORD(res
), (short)HIWORD(res
));
1805 SetCaretPos((short)LOWORD(res
), (short)HIWORD(res
));
1810 /*********************************************************************
1815 static void EDIT_EM_ScrollCaret(EDITSTATE
*es
)
1817 if (es
->style
& ES_MULTILINE
) {
1821 INT cw
= es
->char_width
;
1826 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1827 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
));
1828 vlc
= get_vertical_line_count(es
);
1829 if (l
>= es
->y_offset
+ vlc
)
1830 dy
= l
- vlc
+ 1 - es
->y_offset
;
1831 if (l
< es
->y_offset
)
1832 dy
= l
- es
->y_offset
;
1833 ww
= es
->format_rect
.right
- es
->format_rect
.left
;
1834 if (x
< es
->format_rect
.left
)
1835 dx
= x
- es
->format_rect
.left
- ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1836 if (x
> es
->format_rect
.right
)
1837 dx
= x
- es
->format_rect
.left
- (HSCROLL_FRACTION
- 1) * ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1838 if (dy
|| dx
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1840 /* check if we are going to move too far */
1841 if(es
->x_offset
+ dx
+ ww
> es
->text_width
)
1842 dx
= es
->text_width
- ww
- es
->x_offset
;
1843 if(dx
|| dy
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1844 EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1851 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1852 format_width
= es
->format_rect
.right
- es
->format_rect
.left
;
1853 if (x
< es
->format_rect
.left
) {
1854 goal
= es
->format_rect
.left
+ format_width
/ HSCROLL_FRACTION
;
1857 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1858 } while ((x
< goal
) && es
->x_offset
);
1859 /* FIXME: use ScrollWindow() somehow to improve performance */
1860 EDIT_UpdateText(es
, NULL
, TRUE
);
1861 } else if (x
> es
->format_rect
.right
) {
1863 INT len
= get_text_length(es
);
1864 goal
= es
->format_rect
.right
- format_width
/ HSCROLL_FRACTION
;
1867 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1868 x_last
= (short)LOWORD(EDIT_EM_PosFromChar(es
, len
, FALSE
));
1869 } while ((x
> goal
) && (x_last
> es
->format_rect
.right
));
1870 /* FIXME: use ScrollWindow() somehow to improve performance */
1871 EDIT_UpdateText(es
, NULL
, TRUE
);
1875 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
1879 /*********************************************************************
1884 static void EDIT_MoveBackward(EDITSTATE
*es
, BOOL extend
)
1886 INT e
= es
->selection_end
;
1890 if ((es
->style
& ES_MULTILINE
) && e
&&
1891 (es
->text
[e
- 1] == '\r') && (es
->text
[e
] == '\n')) {
1893 if (e
&& (es
->text
[e
- 1] == '\r'))
1897 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1898 EDIT_EM_ScrollCaret(es
);
1902 /*********************************************************************
1906 * Only for multi line controls
1907 * Move the caret one line down, on a column with the nearest
1908 * x coordinate on the screen (might be a different column).
1911 static void EDIT_MoveDown_ML(EDITSTATE
*es
, BOOL extend
)
1913 INT s
= es
->selection_start
;
1914 INT e
= es
->selection_end
;
1915 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
1916 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
1917 INT x
= (short)LOWORD(pos
);
1918 INT y
= (short)HIWORD(pos
);
1920 e
= EDIT_CharFromPos(es
, x
, y
+ es
->line_height
, &after_wrap
);
1923 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
1924 EDIT_EM_ScrollCaret(es
);
1928 /*********************************************************************
1933 static void EDIT_MoveEnd(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
1935 BOOL after_wrap
= FALSE
;
1938 /* Pass a high value in x to make sure of receiving the end of the line */
1939 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
1940 e
= EDIT_CharFromPos(es
, 0x3fffffff,
1941 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), &after_wrap
);
1943 e
= get_text_length(es
);
1944 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, after_wrap
);
1945 EDIT_EM_ScrollCaret(es
);
1949 /*********************************************************************
1954 static void EDIT_MoveForward(EDITSTATE
*es
, BOOL extend
)
1956 INT e
= es
->selection_end
;
1960 if ((es
->style
& ES_MULTILINE
) && (es
->text
[e
- 1] == '\r')) {
1961 if (es
->text
[e
] == '\n')
1963 else if ((es
->text
[e
] == '\r') && (es
->text
[e
+ 1] == '\n'))
1967 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1968 EDIT_EM_ScrollCaret(es
);
1972 /*********************************************************************
1976 * Home key: move to beginning of line.
1979 static void EDIT_MoveHome(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
1983 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1984 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
1985 e
= EDIT_CharFromPos(es
, -es
->x_offset
,
1986 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), NULL
);
1989 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1990 EDIT_EM_ScrollCaret(es
);
1994 /*********************************************************************
1996 * EDIT_MovePageDown_ML
1998 * Only for multi line controls
1999 * Move the caret one page down, on a column with the nearest
2000 * x coordinate on the screen (might be a different column).
2003 static void EDIT_MovePageDown_ML(EDITSTATE
*es
, BOOL extend
)
2005 INT s
= es
->selection_start
;
2006 INT e
= es
->selection_end
;
2007 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2008 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2009 INT x
= (short)LOWORD(pos
);
2010 INT y
= (short)HIWORD(pos
);
2012 e
= EDIT_CharFromPos(es
, x
,
2013 y
+ (es
->format_rect
.bottom
- es
->format_rect
.top
),
2017 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2018 EDIT_EM_ScrollCaret(es
);
2022 /*********************************************************************
2024 * EDIT_MovePageUp_ML
2026 * Only for multi line controls
2027 * Move the caret one page up, on a column with the nearest
2028 * x coordinate on the screen (might be a different column).
2031 static void EDIT_MovePageUp_ML(EDITSTATE
*es
, BOOL extend
)
2033 INT s
= es
->selection_start
;
2034 INT e
= es
->selection_end
;
2035 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2036 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2037 INT x
= (short)LOWORD(pos
);
2038 INT y
= (short)HIWORD(pos
);
2040 e
= EDIT_CharFromPos(es
, x
,
2041 y
- (es
->format_rect
.bottom
- es
->format_rect
.top
),
2045 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2046 EDIT_EM_ScrollCaret(es
);
2050 /*********************************************************************
2054 * Only for multi line controls
2055 * Move the caret one line up, on a column with the nearest
2056 * x coordinate on the screen (might be a different column).
2059 static void EDIT_MoveUp_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
, y
- es
->line_height
, &after_wrap
);
2071 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2072 EDIT_EM_ScrollCaret(es
);
2076 /*********************************************************************
2078 * EDIT_MoveWordBackward
2081 static void EDIT_MoveWordBackward(EDITSTATE
*es
, BOOL extend
)
2083 INT s
= es
->selection_start
;
2084 INT e
= es
->selection_end
;
2089 l
= EDIT_EM_LineFromChar(es
, e
);
2090 ll
= EDIT_EM_LineLength(es
, e
);
2091 li
= EDIT_EM_LineIndex(es
, l
);
2094 li
= EDIT_EM_LineIndex(es
, l
- 1);
2095 e
= li
+ EDIT_EM_LineLength(es
, li
);
2098 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
2102 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2103 EDIT_EM_ScrollCaret(es
);
2107 /*********************************************************************
2109 * EDIT_MoveWordForward
2112 static void EDIT_MoveWordForward(EDITSTATE
*es
, BOOL extend
)
2114 INT s
= es
->selection_start
;
2115 INT e
= es
->selection_end
;
2120 l
= EDIT_EM_LineFromChar(es
, e
);
2121 ll
= EDIT_EM_LineLength(es
, e
);
2122 li
= EDIT_EM_LineIndex(es
, l
);
2124 if ((es
->style
& ES_MULTILINE
) && (l
!= es
->line_count
- 1))
2125 e
= EDIT_EM_LineIndex(es
, l
+ 1);
2127 e
= li
+ EDIT_CallWordBreakProc(es
,
2128 li
, e
- li
+ 1, ll
, WB_RIGHT
);
2132 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2133 EDIT_EM_ScrollCaret(es
);
2137 /*********************************************************************
2142 static INT
EDIT_PaintText(EDITSTATE
*es
, HDC dc
, INT x
, INT y
, INT line
, INT col
, INT count
, BOOL rev
)
2146 LOGFONTW underline_font
;
2147 HFONT hUnderline
= 0;
2156 BkMode
= GetBkMode(dc
);
2157 BkColor
= GetBkColor(dc
);
2158 TextColor
= GetTextColor(dc
);
2160 if (es
->composition_len
== 0)
2162 SetBkColor(dc
, GetSysColor(COLOR_HIGHLIGHT
));
2163 SetTextColor(dc
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
2164 SetBkMode( dc
, OPAQUE
);
2168 HFONT current
= GetCurrentObject(dc
,OBJ_FONT
);
2169 GetObjectW(current
,sizeof(LOGFONTW
),&underline_font
);
2170 underline_font
.lfUnderline
= TRUE
;
2171 hUnderline
= CreateFontIndirectW(&underline_font
);
2172 old_font
= SelectObject(dc
,hUnderline
);
2175 li
= EDIT_EM_LineIndex(es
, line
);
2176 if (es
->style
& ES_MULTILINE
) {
2177 ret
= (INT
)LOWORD(TabbedTextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
,
2178 es
->tabs_count
, es
->tabs
, es
->format_rect
.left
- es
->x_offset
));
2180 TextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
);
2181 GetTextExtentPoint32W(dc
, es
->text
+ li
+ col
, count
, &size
);
2185 if (es
->composition_len
== 0)
2187 SetBkColor(dc
, BkColor
);
2188 SetTextColor(dc
, TextColor
);
2189 SetBkMode( dc
, BkMode
);
2194 SelectObject(dc
,old_font
);
2196 DeleteObject(hUnderline
);
2203 /*********************************************************************
2208 static void EDIT_PaintLine(EDITSTATE
*es
, HDC dc
, INT line
, BOOL rev
)
2217 SCRIPT_STRING_ANALYSIS ssa
;
2219 if (es
->style
& ES_MULTILINE
) {
2220 INT vlc
= get_vertical_line_count(es
);
2222 if ((line
< es
->y_offset
) || (line
> es
->y_offset
+ vlc
) || (line
>= es
->line_count
))
2227 TRACE("line=%d\n", line
);
2229 ssa
= EDIT_UpdateUniscribeData(es
, dc
, line
);
2230 pos
= EDIT_EM_PosFromChar(es
, EDIT_EM_LineIndex(es
, line
), FALSE
);
2231 x
= (short)LOWORD(pos
);
2232 y
= (short)HIWORD(pos
);
2234 if (es
->style
& ES_MULTILINE
)
2236 int line_idx
= line
;
2238 if (es
->style
& ES_RIGHT
|| es
->style
& ES_CENTER
)
2240 LINEDEF
*line_def
= es
->first_line_def
;
2243 while (line_def
&& line_idx
)
2245 line_def
= line_def
->next
;
2248 w
= es
->format_rect
.right
- es
->format_rect
.left
;
2249 lw
= line_def
->width
;
2251 if (es
->style
& ES_RIGHT
)
2253 else if (es
->style
& ES_CENTER
)
2256 x
+= es
->format_rect
.left
;
2261 li
= EDIT_EM_LineIndex(es
, line
);
2262 ll
= EDIT_EM_LineLength(es
, li
);
2263 s
= min(es
->selection_start
, es
->selection_end
);
2264 e
= max(es
->selection_start
, es
->selection_end
);
2265 s
= min(li
+ ll
, max(li
, s
));
2266 e
= min(li
+ ll
, max(li
, e
));
2270 ScriptStringOut(ssa
, x
, y
, 0, &es
->format_rect
, s
- li
, e
- li
, FALSE
);
2271 else if (rev
&& (s
!= e
) &&
2272 ((es
->flags
& EF_FOCUSED
) || (es
->style
& ES_NOHIDESEL
))) {
2273 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, s
- li
, FALSE
);
2274 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, s
- li
, e
- s
, TRUE
);
2275 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, e
- li
, li
+ ll
- e
, FALSE
);
2277 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, ll
, FALSE
);
2281 /*********************************************************************
2283 * EDIT_AdjustFormatRect
2285 * Adjusts the format rectangle for the current font and the
2286 * current client rectangle.
2289 static void EDIT_AdjustFormatRect(EDITSTATE
*es
)
2293 es
->format_rect
.right
= max(es
->format_rect
.right
, es
->format_rect
.left
+ es
->char_width
);
2294 if (es
->style
& ES_MULTILINE
)
2296 INT fw
, vlc
, max_x_offset
, max_y_offset
;
2298 vlc
= get_vertical_line_count(es
);
2299 es
->format_rect
.bottom
= es
->format_rect
.top
+ vlc
* es
->line_height
;
2301 /* correct es->x_offset */
2302 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2303 max_x_offset
= es
->text_width
- fw
;
2304 if(max_x_offset
< 0) max_x_offset
= 0;
2305 if(es
->x_offset
> max_x_offset
)
2306 es
->x_offset
= max_x_offset
;
2308 /* correct es->y_offset */
2309 max_y_offset
= es
->line_count
- vlc
;
2310 if(max_y_offset
< 0) max_y_offset
= 0;
2311 if(es
->y_offset
> max_y_offset
)
2312 es
->y_offset
= max_y_offset
;
2314 /* force scroll info update */
2315 EDIT_UpdateScrollInfo(es
);
2318 /* Windows doesn't care to fix text placement for SL controls */
2319 es
->format_rect
.bottom
= es
->format_rect
.top
+ es
->line_height
;
2321 /* Always stay within the client area */
2322 GetClientRect(es
->hwndSelf
, &ClientRect
);
2323 es
->format_rect
.bottom
= min(es
->format_rect
.bottom
, ClientRect
.bottom
);
2325 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
))
2326 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2328 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
2332 /*********************************************************************
2336 * note: this is not (exactly) the handler called on EM_SETRECTNP
2337 * it is also used to set the rect of a single line control
2340 static void EDIT_SetRectNP(EDITSTATE
*es
, const RECT
*rc
)
2344 ExStyle
= GetWindowLongPtrW(es
->hwndSelf
, GWL_EXSTYLE
);
2346 CopyRect(&es
->format_rect
, rc
);
2348 if (ExStyle
& WS_EX_CLIENTEDGE
) {
2349 es
->format_rect
.left
++;
2350 es
->format_rect
.right
--;
2352 if (es
->format_rect
.bottom
- es
->format_rect
.top
2353 >= es
->line_height
+ 2)
2355 es
->format_rect
.top
++;
2356 es
->format_rect
.bottom
--;
2359 else if (es
->style
& WS_BORDER
) {
2360 bw
= GetSystemMetrics(SM_CXBORDER
) + 1;
2361 bh
= GetSystemMetrics(SM_CYBORDER
) + 1;
2362 InflateRect(&es
->format_rect
, -bw
, 0);
2363 if (es
->format_rect
.bottom
- es
->format_rect
.top
>= es
->line_height
+ 2 * bh
)
2364 InflateRect(&es
->format_rect
, 0, -bh
);
2367 es
->format_rect
.left
+= es
->left_margin
;
2368 es
->format_rect
.right
-= es
->right_margin
;
2369 EDIT_AdjustFormatRect(es
);
2373 /*********************************************************************
2377 * returns line number (not index) in high-order word of result.
2378 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2379 * to Richedit, not to the edit control. Original documentation is valid.
2380 * FIXME: do the specs mean to return -1 if outside client area or
2381 * if outside formatting rectangle ???
2384 static LRESULT
EDIT_EM_CharFromPos(EDITSTATE
*es
, INT x
, INT y
)
2392 GetClientRect(es
->hwndSelf
, &rc
);
2393 if (!PtInRect(&rc
, pt
))
2396 index
= EDIT_CharFromPos(es
, x
, y
, NULL
);
2397 return MAKELONG(index
, EDIT_EM_LineFromChar(es
, index
));
2401 /*********************************************************************
2405 * Enable or disable soft breaks.
2407 * This means: insert or remove the soft linebreak character (\r\r\n).
2408 * Take care to check if the text still fits the buffer after insertion.
2409 * If not, notify with EN_ERRSPACE.
2412 static BOOL
EDIT_EM_FmtLines(EDITSTATE
*es
, BOOL add_eol
)
2414 es
->flags
&= ~EF_USE_SOFTBRK
;
2416 es
->flags
|= EF_USE_SOFTBRK
;
2417 FIXME("soft break enabled, not implemented\n");
2423 /*********************************************************************
2427 * Hopefully this won't fire back at us.
2428 * We always start with a fixed buffer in the local heap.
2429 * Despite of the documentation says that the local heap is used
2430 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2431 * buffer on the local heap.
2434 static HLOCAL
EDIT_EM_GetHandle(EDITSTATE
*es
)
2438 if (!(es
->style
& ES_MULTILINE
))
2442 hLocal
= es
->hloc32W
;
2448 UINT countA
, alloc_size
;
2449 TRACE("Allocating 32-bit ANSI alias buffer\n");
2450 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, NULL
, 0, NULL
, NULL
);
2451 alloc_size
= ROUND_TO_GROW(countA
);
2452 if(!(es
->hloc32A
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
2454 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size
);
2457 textA
= LocalLock(es
->hloc32A
);
2458 WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, countA
, NULL
, NULL
);
2459 LocalUnlock(es
->hloc32A
);
2461 hLocal
= es
->hloc32A
;
2464 EDIT_UnlockBuffer(es
, TRUE
);
2466 /* The text buffer handle belongs to the app */
2467 es
->hlocapp
= hLocal
;
2469 TRACE("Returning %p, LocalSize() = %ld\n", hLocal
, LocalSize(hLocal
));
2474 /*********************************************************************
2479 static INT
EDIT_EM_GetLine(EDITSTATE
*es
, INT line
, LPWSTR dst
, BOOL unicode
)
2482 INT line_len
, dst_len
;
2485 if (es
->style
& ES_MULTILINE
) {
2486 if (line
>= es
->line_count
)
2490 i
= EDIT_EM_LineIndex(es
, line
);
2492 line_len
= EDIT_EM_LineLength(es
, i
);
2493 dst_len
= *(WORD
*)dst
;
2496 if(dst_len
<= line_len
)
2498 memcpy(dst
, src
, dst_len
* sizeof(WCHAR
));
2501 else /* Append 0 if enough space */
2503 memcpy(dst
, src
, line_len
* sizeof(WCHAR
));
2510 INT ret
= WideCharToMultiByte(CP_ACP
, 0, src
, line_len
, (LPSTR
)dst
, dst_len
, NULL
, NULL
);
2511 if(!ret
&& line_len
) /* Insufficient buffer size */
2513 if(ret
< dst_len
) /* Append 0 if enough space */
2514 ((LPSTR
)dst
)[ret
] = 0;
2520 /*********************************************************************
2525 static LRESULT
EDIT_EM_GetSel(const EDITSTATE
*es
, PUINT start
, PUINT end
)
2527 UINT s
= es
->selection_start
;
2528 UINT e
= es
->selection_end
;
2535 return MAKELONG(s
, e
);
2539 /*********************************************************************
2543 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2546 static void EDIT_EM_ReplaceSel(EDITSTATE
*es
, BOOL can_undo
, const WCHAR
*lpsz_replace
, UINT strl
,
2547 BOOL send_update
, BOOL honor_limit
)
2549 UINT tl
= get_text_length(es
);
2560 TRACE("%s, can_undo %d, send_update %d\n",
2561 debugstr_wn(lpsz_replace
, strl
), can_undo
, send_update
);
2563 s
= es
->selection_start
;
2564 e
= es
->selection_end
;
2566 EDIT_InvalidateUniscribeData(es
);
2567 if ((s
== e
) && !strl
)
2572 size
= tl
- (e
- s
) + strl
;
2576 /* Issue the EN_MAXTEXT notification and continue with replacing text
2577 * so that buffer limit is honored. */
2578 if ((honor_limit
) && (size
> es
->buffer_limit
)) {
2579 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2580 /* Buffer limit can be smaller than the actual length of text in combobox */
2581 if (es
->buffer_limit
< (tl
- (e
-s
)))
2584 strl
= min(strl
, es
->buffer_limit
- (tl
- (e
-s
)));
2587 if (!EDIT_MakeFit(es
, tl
- (e
- s
) + strl
))
2591 /* there is something to be deleted */
2592 TRACE("deleting stuff.\n");
2594 buf
= HeapAlloc(GetProcessHeap(), 0, (bufl
+ 1) * sizeof(WCHAR
));
2596 memcpy(buf
, es
->text
+ s
, bufl
* sizeof(WCHAR
));
2597 buf
[bufl
] = 0; /* ensure 0 termination */
2599 lstrcpyW(es
->text
+ s
, es
->text
+ e
);
2600 text_buffer_changed(es
);
2603 /* there is an insertion */
2604 tl
= get_text_length(es
);
2605 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
));
2606 for (p
= es
->text
+ tl
; p
>= es
->text
+ s
; p
--)
2608 for (i
= 0 , p
= es
->text
+ s
; i
< strl
; i
++)
2609 p
[i
] = lpsz_replace
[i
];
2610 if(es
->style
& ES_UPPERCASE
)
2611 CharUpperBuffW(p
, strl
);
2612 else if(es
->style
& ES_LOWERCASE
)
2613 CharLowerBuffW(p
, strl
);
2614 text_buffer_changed(es
);
2616 if (es
->style
& ES_MULTILINE
)
2618 INT st
= min(es
->selection_start
, es
->selection_end
);
2619 INT vlc
= get_vertical_line_count(es
);
2621 hrgn
= CreateRectRgn(0, 0, 0, 0);
2622 EDIT_BuildLineDefs_ML(es
, st
, st
+ strl
,
2623 strl
- abs(es
->selection_end
- es
->selection_start
), hrgn
);
2624 /* if text is too long undo all changes */
2625 if (honor_limit
&& !(es
->style
& ES_AUTOVSCROLL
) && (es
->line_count
> vlc
)) {
2627 lstrcpyW(es
->text
+ e
, es
->text
+ e
+ strl
);
2629 for (i
= 0 , p
= es
->text
; i
< e
- s
; i
++)
2631 text_buffer_changed(es
);
2632 EDIT_BuildLineDefs_ML(es
, s
, e
,
2633 abs(es
->selection_end
- es
->selection_start
) - strl
, hrgn
);
2636 SetRectRgn(hrgn
, 0, 0, 0, 0);
2637 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2641 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2642 EDIT_InvalidateUniscribeData(es
);
2643 EDIT_CalcLineWidth_SL(es
);
2644 /* remove chars that don't fit */
2645 if (honor_limit
&& !(es
->style
& ES_AUTOHSCROLL
) && (es
->text_width
> fw
)) {
2646 while ((es
->text_width
> fw
) && s
+ strl
>= s
) {
2647 lstrcpyW(es
->text
+ s
+ strl
- 1, es
->text
+ s
+ strl
);
2649 es
->text_length
= -1;
2650 EDIT_InvalidateUniscribeData(es
);
2651 EDIT_CalcLineWidth_SL(es
);
2653 text_buffer_changed(es
);
2654 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2660 utl
= lstrlenW(es
->undo_text
);
2661 if (!es
->undo_insert_count
&& (*es
->undo_text
&& (s
== es
->undo_position
))) {
2662 /* undo-buffer is extended to the right */
2663 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2664 memcpy(es
->undo_text
+ utl
, buf
, (e
- s
)*sizeof(WCHAR
));
2665 (es
->undo_text
+ utl
)[e
- s
] = 0; /* ensure 0 termination */
2666 } else if (!es
->undo_insert_count
&& (*es
->undo_text
&& (e
== es
->undo_position
))) {
2667 /* undo-buffer is extended to the left */
2668 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2669 for (p
= es
->undo_text
+ utl
; p
>= es
->undo_text
; p
--)
2671 for (i
= 0 , p
= es
->undo_text
; i
< e
- s
; i
++)
2673 es
->undo_position
= s
;
2675 /* new undo-buffer */
2676 EDIT_MakeUndoFit(es
, e
- s
);
2677 memcpy(es
->undo_text
, buf
, (e
- s
)*sizeof(WCHAR
));
2678 es
->undo_text
[e
- s
] = 0; /* ensure 0 termination */
2679 es
->undo_position
= s
;
2681 /* any deletion makes the old insertion-undo invalid */
2682 es
->undo_insert_count
= 0;
2684 EDIT_EM_EmptyUndoBuffer(es
);
2688 if ((s
== es
->undo_position
) ||
2689 ((es
->undo_insert_count
) &&
2690 (s
== es
->undo_position
+ es
->undo_insert_count
)))
2692 * insertion is new and at delete position or
2693 * an extension to either left or right
2695 es
->undo_insert_count
+= strl
;
2697 /* new insertion undo */
2698 es
->undo_position
= s
;
2699 es
->undo_insert_count
= strl
;
2700 /* new insertion makes old delete-buffer invalid */
2701 *es
->undo_text
= '\0';
2704 EDIT_EM_EmptyUndoBuffer(es
);
2707 HeapFree(GetProcessHeap(), 0, buf
);
2711 /* If text has been deleted and we're right or center aligned then scroll rightward */
2712 if (es
->style
& (ES_RIGHT
| ES_CENTER
))
2714 INT delta
= strl
- abs(es
->selection_end
- es
->selection_start
);
2716 if (delta
< 0 && es
->x_offset
)
2718 if (abs(delta
) > es
->x_offset
)
2721 es
->x_offset
+= delta
;
2725 EDIT_EM_SetSel(es
, s
, s
, FALSE
);
2726 es
->flags
|= EF_MODIFIED
;
2727 if (send_update
) es
->flags
|= EF_UPDATE
;
2730 EDIT_UpdateTextRegion(es
, hrgn
, TRUE
);
2734 EDIT_UpdateText(es
, NULL
, TRUE
);
2736 EDIT_EM_ScrollCaret(es
);
2738 /* force scroll info update */
2739 EDIT_UpdateScrollInfo(es
);
2742 if(send_update
|| (es
->flags
& EF_UPDATE
))
2744 es
->flags
&= ~EF_UPDATE
;
2745 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
2747 EDIT_InvalidateUniscribeData(es
);
2751 /*********************************************************************
2755 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2758 static void EDIT_EM_SetHandle(EDITSTATE
*es
, HLOCAL hloc
)
2760 if (!(es
->style
& ES_MULTILINE
))
2764 WARN("called with NULL handle\n");
2768 EDIT_UnlockBuffer(es
, TRUE
);
2774 LocalFree(es
->hloc32A
);
2786 countA
= LocalSize(hloc
);
2787 textA
= LocalLock(hloc
);
2788 countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
2789 if(!(hloc32W_new
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, countW
* sizeof(WCHAR
))))
2791 ERR("Could not allocate new unicode buffer\n");
2794 textW
= LocalLock(hloc32W_new
);
2795 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, textW
, countW
);
2796 LocalUnlock(hloc32W_new
);
2800 LocalFree(es
->hloc32W
);
2802 es
->hloc32W
= hloc32W_new
;
2806 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
2808 /* The text buffer handle belongs to the control */
2811 EDIT_LockBuffer(es
);
2812 text_buffer_changed(es
);
2814 es
->x_offset
= es
->y_offset
= 0;
2815 es
->selection_start
= es
->selection_end
= 0;
2816 EDIT_EM_EmptyUndoBuffer(es
);
2817 es
->flags
&= ~EF_MODIFIED
;
2818 es
->flags
&= ~EF_UPDATE
;
2819 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2820 EDIT_UpdateText(es
, NULL
, TRUE
);
2821 EDIT_EM_ScrollCaret(es
);
2822 /* force scroll info update */
2823 EDIT_UpdateScrollInfo(es
);
2827 /*********************************************************************
2831 * NOTE: this version currently implements WinNT limits
2834 static void EDIT_EM_SetLimitText(EDITSTATE
*es
, UINT limit
)
2836 if (!limit
) limit
= ~0u;
2837 if (!(es
->style
& ES_MULTILINE
)) limit
= min(limit
, 0x7ffffffe);
2838 es
->buffer_limit
= limit
;
2841 static BOOL
is_cjk_charset(HDC dc
)
2843 switch (GdiGetCodePage(dc
)) {
2844 case 932: case 936: case 949: case 950: case 1361:
2851 static BOOL
is_cjk_font(HDC dc
)
2853 const DWORD FS_DBCS_MASK
= FS_JISJAPAN
|FS_CHINESESIMP
|FS_WANSUNG
|FS_CHINESETRAD
|FS_JOHAB
;
2855 return (GetTextCharsetInfo(dc
, &fs
, 0) != DEFAULT_CHARSET
&&
2856 (fs
.fsCsb
[0] & FS_DBCS_MASK
));
2859 static int get_cjk_fontinfo_margin(int width
, int side_bearing
)
2862 if (side_bearing
< 0)
2863 margin
= min(-side_bearing
, width
/2);
2869 struct char_width_info
{
2870 INT min_lsb
, min_rsb
, unknown
;
2873 /* Undocumented gdi32 export */
2874 extern BOOL WINAPI
GetCharWidthInfo(HDC
, struct char_width_info
*);
2876 /*********************************************************************
2880 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2881 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2882 * margin according to the textmetrics of the current font.
2884 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2885 * equal to or larger than a certain size. The empty client rect is treated as
2888 static void EDIT_EM_SetMargins(EDITSTATE
*es
, INT action
,
2889 WORD left
, WORD right
, BOOL repaint
)
2892 INT default_left_margin
= 0; /* in pixels */
2893 INT default_right_margin
= 0; /* in pixels */
2895 /* Set the default margins depending on the font */
2896 if (es
->font
&& (left
== EC_USEFONTINFO
|| right
== EC_USEFONTINFO
)) {
2897 HDC dc
= GetDC(es
->hwndSelf
);
2898 HFONT old_font
= SelectObject(dc
, es
->font
);
2899 LONG width
= GdiGetCharDimensions(dc
, &tm
, NULL
), rc_width
;
2902 /* The default margins are only non zero for TrueType or Vector fonts */
2903 if (tm
.tmPitchAndFamily
& ( TMPF_VECTOR
| TMPF_TRUETYPE
)) {
2904 struct char_width_info width_info
;
2906 if ((is_cjk_charset(dc
) || is_cjk_font(dc
)) &&
2907 GetCharWidthInfo(dc
, &width_info
))
2909 default_left_margin
= get_cjk_fontinfo_margin(width
, width_info
.min_lsb
);
2910 default_right_margin
= get_cjk_fontinfo_margin(width
, width_info
.min_rsb
);
2914 default_left_margin
= width
/ 2;
2915 default_right_margin
= width
/ 2;
2918 GetClientRect(es
->hwndSelf
, &rc
);
2919 rc_width
= !IsRectEmpty(&rc
) ? rc
.right
- rc
.left
: 80;
2920 if (rc_width
< default_left_margin
+ default_right_margin
+ width
* 2) {
2921 default_left_margin
= es
->left_margin
;
2922 default_right_margin
= es
->right_margin
;
2925 SelectObject(dc
, old_font
);
2926 ReleaseDC(es
->hwndSelf
, dc
);
2929 if (action
& EC_LEFTMARGIN
) {
2930 es
->format_rect
.left
-= es
->left_margin
;
2931 if (left
!= EC_USEFONTINFO
)
2932 es
->left_margin
= left
;
2934 es
->left_margin
= default_left_margin
;
2935 es
->format_rect
.left
+= es
->left_margin
;
2938 if (action
& EC_RIGHTMARGIN
) {
2939 es
->format_rect
.right
+= es
->right_margin
;
2940 if (right
!= EC_USEFONTINFO
)
2941 es
->right_margin
= right
;
2943 es
->right_margin
= default_right_margin
;
2944 es
->format_rect
.right
-= es
->right_margin
;
2947 if (action
& (EC_LEFTMARGIN
| EC_RIGHTMARGIN
)) {
2948 EDIT_AdjustFormatRect(es
);
2949 if (repaint
) EDIT_UpdateText(es
, NULL
, TRUE
);
2952 TRACE("left=%d, right=%d\n", es
->left_margin
, es
->right_margin
);
2956 /*********************************************************************
2958 * EM_SETPASSWORDCHAR
2961 static void EDIT_EM_SetPasswordChar(EDITSTATE
*es
, WCHAR c
)
2965 if (es
->style
& ES_MULTILINE
)
2968 if (es
->password_char
== c
)
2971 style
= GetWindowLongW( es
->hwndSelf
, GWL_STYLE
);
2972 es
->password_char
= c
;
2974 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
| ES_PASSWORD
);
2975 es
->style
|= ES_PASSWORD
;
2977 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
& ~ES_PASSWORD
);
2978 es
->style
&= ~ES_PASSWORD
;
2980 EDIT_InvalidateUniscribeData(es
);
2981 EDIT_UpdateText(es
, NULL
, TRUE
);
2985 /*********************************************************************
2990 static BOOL
EDIT_EM_SetTabStops(EDITSTATE
*es
, INT count
, const INT
*tabs
)
2992 if (!(es
->style
& ES_MULTILINE
))
2994 HeapFree(GetProcessHeap(), 0, es
->tabs
);
2995 es
->tabs_count
= count
;
2999 es
->tabs
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(INT
));
3000 memcpy(es
->tabs
, tabs
, count
* sizeof(INT
));
3002 EDIT_InvalidateUniscribeData(es
);
3007 /*********************************************************************
3009 * EM_SETWORDBREAKPROC
3012 static void EDIT_EM_SetWordBreakProc(EDITSTATE
*es
, void *wbp
)
3014 if (es
->word_break_proc
== wbp
)
3017 es
->word_break_proc
= wbp
;
3019 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
)) {
3020 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
3021 EDIT_UpdateText(es
, NULL
, TRUE
);
3026 /*********************************************************************
3031 static BOOL
EDIT_EM_Undo(EDITSTATE
*es
)
3036 /* As per MSDN spec, for a single-line edit control,
3037 the return value is always TRUE */
3038 if( es
->style
& ES_READONLY
)
3039 return !(es
->style
& ES_MULTILINE
);
3041 ulength
= lstrlenW(es
->undo_text
);
3043 utext
= HeapAlloc(GetProcessHeap(), 0, (ulength
+ 1) * sizeof(WCHAR
));
3045 lstrcpyW(utext
, es
->undo_text
);
3047 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3048 es
->undo_insert_count
, debugstr_w(utext
));
3050 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3051 EDIT_EM_EmptyUndoBuffer(es
);
3052 EDIT_EM_ReplaceSel(es
, TRUE
, utext
, ulength
, TRUE
, TRUE
);
3053 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3054 /* send the notification after the selection start and end are set */
3055 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
3056 EDIT_EM_ScrollCaret(es
);
3057 HeapFree(GetProcessHeap(), 0, utext
);
3059 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3060 es
->undo_insert_count
, debugstr_w(es
->undo_text
));
3065 /* Helper function for WM_CHAR
3067 * According to an MSDN blog article titled "Just because you're a control
3068 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3069 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3070 * a dialog box or not.
3072 static inline BOOL
EDIT_IsInsideDialog(EDITSTATE
*es
)
3074 return (es
->flags
& EF_DIALOGMODE
);
3078 /*********************************************************************
3083 static void EDIT_WM_Paste(EDITSTATE
*es
)
3089 /* Protect read-only edit control from modification */
3090 if(es
->style
& ES_READONLY
)
3093 OpenClipboard(es
->hwndSelf
);
3094 if ((hsrc
= GetClipboardData(CF_UNICODETEXT
))) {
3095 src
= GlobalLock(hsrc
);
3096 len
= lstrlenW(src
);
3097 /* Protect single-line edit against pasting new line character */
3098 if (!(es
->style
& ES_MULTILINE
) && ((ptr
= wcschr(src
, '\n')))) {
3100 if (len
&& src
[len
- 1] == '\r')
3103 EDIT_EM_ReplaceSel(es
, TRUE
, src
, len
, TRUE
, TRUE
);
3106 else if (es
->style
& ES_PASSWORD
) {
3107 /* clear selected text in password edit box even with empty clipboard */
3108 EDIT_EM_ReplaceSel(es
, TRUE
, NULL
, 0, TRUE
, TRUE
);
3114 /*********************************************************************
3119 static void EDIT_WM_Copy(EDITSTATE
*es
)
3121 INT s
= min(es
->selection_start
, es
->selection_end
);
3122 INT e
= max(es
->selection_start
, es
->selection_end
);
3130 hdst
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (len
+ 1) * sizeof(WCHAR
));
3131 dst
= GlobalLock(hdst
);
3132 memcpy(dst
, es
->text
+ s
, len
* sizeof(WCHAR
));
3133 dst
[len
] = 0; /* ensure 0 termination */
3134 TRACE("%s\n", debugstr_w(dst
));
3136 OpenClipboard(es
->hwndSelf
);
3138 SetClipboardData(CF_UNICODETEXT
, hdst
);
3143 /*********************************************************************
3148 static inline void EDIT_WM_Clear(EDITSTATE
*es
)
3150 /* Protect read-only edit control from modification */
3151 if(es
->style
& ES_READONLY
)
3154 EDIT_EM_ReplaceSel(es
, TRUE
, NULL
, 0, TRUE
, TRUE
);
3158 /*********************************************************************
3163 static inline void EDIT_WM_Cut(EDITSTATE
*es
)
3170 /*********************************************************************
3175 static LRESULT
EDIT_WM_Char(EDITSTATE
*es
, WCHAR c
)
3179 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3183 /* If it's not a multiline edit box, it would be ignored below.
3184 * For multiline edit without ES_WANTRETURN, we have to make a
3187 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_WANTRETURN
))
3188 if (EDIT_IsInsideDialog(es
))
3191 if (es
->style
& ES_MULTILINE
) {
3192 if (es
->style
& ES_READONLY
) {
3193 EDIT_MoveHome(es
, FALSE
, FALSE
);
3194 EDIT_MoveDown_ML(es
, FALSE
);
3196 EDIT_EM_ReplaceSel(es
, TRUE
, L
"\r\n", 2, TRUE
, TRUE
);
3201 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_READONLY
))
3203 if (EDIT_IsInsideDialog(es
))
3205 EDIT_EM_ReplaceSel(es
, TRUE
, L
"\t", 1, TRUE
, TRUE
);
3209 if (!(es
->style
& ES_READONLY
) && !control
) {
3210 if (es
->selection_start
!= es
->selection_end
)
3213 /* delete character left of caret */
3214 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3215 EDIT_MoveBackward(es
, TRUE
);
3221 if (!(es
->style
& ES_PASSWORD
))
3222 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3225 if (!(es
->style
& ES_READONLY
))
3226 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3229 if (!((es
->style
& ES_READONLY
) || (es
->style
& ES_PASSWORD
)))
3230 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3233 if (!(es
->style
& ES_READONLY
))
3234 SendMessageW(es
->hwndSelf
, WM_UNDO
, 0, 0);
3238 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3239 if( (es
->style
& ES_NUMBER
) && !( c
>= '0' && c
<= '9') )
3242 if (!(es
->style
& ES_READONLY
) && (c
>= ' ') && (c
!= 127))
3243 EDIT_EM_ReplaceSel(es
, TRUE
, &c
, 1, TRUE
, TRUE
);
3250 /*********************************************************************
3252 * EDIT_ContextMenuCommand
3255 static void EDIT_ContextMenuCommand(EDITSTATE
*es
, UINT id
)
3259 SendMessageW(es
->hwndSelf
, WM_UNDO
, 0, 0);
3262 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3265 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3268 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3271 SendMessageW(es
->hwndSelf
, WM_CLEAR
, 0, 0);
3274 SendMessageW(es
->hwndSelf
, EM_SETSEL
, 0, -1);
3277 ERR("unknown menu item, please report\n");
3283 /*********************************************************************
3287 * Note: the resource files resource/sysres_??.rc cannot define a
3288 * single popup menu. Hence we use a (dummy) menubar
3289 * containing the single popup menu as its first item.
3291 * FIXME: the message identifiers have been chosen arbitrarily,
3292 * hence we use MF_BYPOSITION.
3293 * We might as well use the "real" values (anybody knows ?)
3294 * The menu definition is in resources/sysres_??.rc.
3295 * Once these are OK, we better use MF_BYCOMMAND here
3296 * (as we do in EDIT_WM_Command()).
3299 static void EDIT_WM_ContextMenu(EDITSTATE
*es
, INT x
, INT y
)
3301 HMENU menu
= LoadMenuA(user32_module
, "EDITMENU");
3302 HMENU popup
= GetSubMenu(menu
, 0);
3303 UINT start
= es
->selection_start
;
3304 UINT end
= es
->selection_end
;
3307 ORDER_UINT(start
, end
);
3310 EnableMenuItem(popup
, 0, MF_BYPOSITION
| (EDIT_EM_CanUndo(es
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3312 EnableMenuItem(popup
, 2, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3314 EnableMenuItem(popup
, 3, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) ? MF_ENABLED
: MF_GRAYED
));
3316 EnableMenuItem(popup
, 4, MF_BYPOSITION
| (IsClipboardFormatAvailable(CF_UNICODETEXT
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3318 EnableMenuItem(popup
, 5, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3320 EnableMenuItem(popup
, 7, MF_BYPOSITION
| (start
|| (end
!= get_text_length(es
)) ? MF_ENABLED
: MF_GRAYED
));
3322 if (x
== -1 && y
== -1) /* passed via VK_APPS press/release */
3325 /* Windows places the menu at the edit's center in this case */
3326 WIN_GetRectangles( es
->hwndSelf
, COORDS_SCREEN
, NULL
, &rc
);
3327 x
= rc
.left
+ (rc
.right
- rc
.left
) / 2;
3328 y
= rc
.top
+ (rc
.bottom
- rc
.top
) / 2;
3331 if (!(es
->flags
& EF_FOCUSED
))
3332 SetFocus(es
->hwndSelf
);
3334 cmd
= TrackPopupMenu(popup
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
| TPM_RETURNCMD
| TPM_NONOTIFY
,
3335 x
, y
, 0, es
->hwndSelf
, NULL
);
3338 EDIT_ContextMenuCommand(es
, cmd
);
3344 /*********************************************************************
3349 static INT
EDIT_WM_GetText(const EDITSTATE
*es
, INT count
, LPWSTR dst
, BOOL unicode
)
3351 if(!count
) return 0;
3355 lstrcpynW(dst
, es
->text
, count
);
3356 return lstrlenW(dst
);
3360 LPSTR textA
= (LPSTR
)dst
;
3361 if (!WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, count
, NULL
, NULL
))
3362 textA
[count
- 1] = 0; /* ensure 0 termination */
3363 return strlen(textA
);
3367 /*********************************************************************
3372 static BOOL
EDIT_CheckCombo(EDITSTATE
*es
, UINT msg
, INT key
)
3374 HWND hLBox
= es
->hwndListBox
;
3382 hCombo
= GetParent(es
->hwndSelf
);
3386 TRACE_(combo
)("[%p]: handling msg %x (%x)\n", es
->hwndSelf
, msg
, key
);
3388 if (key
== VK_UP
|| key
== VK_DOWN
)
3390 if (SendMessageW(hCombo
, CB_GETEXTENDEDUI
, 0, 0))
3393 if (msg
== WM_KEYDOWN
|| nEUI
)
3394 bDropped
= (BOOL
)SendMessageW(hCombo
, CB_GETDROPPEDSTATE
, 0, 0);
3400 if (!bDropped
&& nEUI
&& (key
== VK_UP
|| key
== VK_DOWN
))
3402 /* make sure ComboLBox pops up */
3403 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, FALSE
, 0);
3408 SendMessageW(hLBox
, WM_KEYDOWN
, key
, 0);
3411 case WM_SYSKEYDOWN
: /* Handle Alt+up/down arrows */
3413 SendMessageW(hCombo
, CB_SHOWDROPDOWN
, !bDropped
, 0);
3415 SendMessageW(hLBox
, WM_KEYDOWN
, VK_F4
, 0);
3420 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, TRUE
, 0);
3426 /*********************************************************************
3430 * Handling of special keys that don't produce a WM_CHAR
3431 * (i.e. non-printable keys) & Backspace & Delete
3434 static LRESULT
EDIT_WM_KeyDown(EDITSTATE
*es
, INT key
)
3439 if (GetKeyState(VK_MENU
) & 0x8000)
3442 shift
= GetKeyState(VK_SHIFT
) & 0x8000;
3443 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3448 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
) || key
== VK_F4
)
3453 if ((es
->style
& ES_MULTILINE
) && (key
== VK_UP
))
3454 EDIT_MoveUp_ML(es
, shift
);
3457 EDIT_MoveWordBackward(es
, shift
);
3459 EDIT_MoveBackward(es
, shift
);
3462 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
))
3466 if ((es
->style
& ES_MULTILINE
) && (key
== VK_DOWN
))
3467 EDIT_MoveDown_ML(es
, shift
);
3469 EDIT_MoveWordForward(es
, shift
);
3471 EDIT_MoveForward(es
, shift
);
3474 EDIT_MoveHome(es
, shift
, control
);
3477 EDIT_MoveEnd(es
, shift
, control
);
3480 if (es
->style
& ES_MULTILINE
)
3481 EDIT_MovePageUp_ML(es
, shift
);
3483 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
3486 if (es
->style
& ES_MULTILINE
)
3487 EDIT_MovePageDown_ML(es
, shift
);
3489 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
3492 if (!(es
->style
& ES_READONLY
) && !(shift
&& control
)) {
3493 if (es
->selection_start
!= es
->selection_end
) {
3499 EDIT_EM_SetSel(es
, ~0u, 0, FALSE
);
3501 /* delete character left of caret */
3502 EDIT_MoveBackward(es
, TRUE
);
3504 /* delete to end of line */
3505 EDIT_MoveEnd(es
, TRUE
, FALSE
);
3507 /* delete character right of caret */
3508 EDIT_MoveForward(es
, TRUE
);
3515 if (!(es
->style
& ES_READONLY
))
3521 /* If the edit doesn't want the return send a message to the default object */
3522 if(!(es
->style
& ES_MULTILINE
) || !(es
->style
& ES_WANTRETURN
))
3526 if (!EDIT_IsInsideDialog(es
)) break;
3528 dw
= SendMessageW(es
->hwndParent
, DM_GETDEFID
, 0, 0);
3529 if (HIWORD(dw
) == DC_HASDEFID
)
3531 HWND hwDefCtrl
= GetDlgItem(es
->hwndParent
, LOWORD(dw
));
3534 SendMessageW(es
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
3535 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
3541 if ((es
->style
& ES_MULTILINE
) && EDIT_IsInsideDialog(es
))
3542 PostMessageW(es
->hwndParent
, WM_CLOSE
, 0, 0);
3545 if ((es
->style
& ES_MULTILINE
) && EDIT_IsInsideDialog(es
))
3546 SendMessageW(es
->hwndParent
, WM_NEXTDLGCTL
, shift
, 0);
3553 /*********************************************************************
3558 static LRESULT
EDIT_WM_KillFocus(EDITSTATE
*es
)
3560 es
->flags
&= ~EF_FOCUSED
;
3562 if(!(es
->style
& ES_NOHIDESEL
))
3563 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
3564 EDIT_NOTIFY_PARENT(es
, EN_KILLFOCUS
);
3565 /* throw away left over scroll when we lose focus */
3566 es
->wheelDeltaRemainder
= 0;
3571 /*********************************************************************
3575 * The caret position has been set on the WM_LBUTTONDOWN message
3578 static LRESULT
EDIT_WM_LButtonDblClk(EDITSTATE
*es
)
3581 INT e
= es
->selection_end
;
3586 es
->bCaptureState
= TRUE
;
3587 SetCapture(es
->hwndSelf
);
3589 l
= EDIT_EM_LineFromChar(es
, e
);
3590 li
= EDIT_EM_LineIndex(es
, l
);
3591 ll
= EDIT_EM_LineLength(es
, e
);
3592 s
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
3593 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_RIGHT
);
3594 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
3595 EDIT_EM_ScrollCaret(es
);
3596 es
->region_posx
= es
->region_posy
= 0;
3597 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
3602 /*********************************************************************
3607 static LRESULT
EDIT_WM_LButtonDown(EDITSTATE
*es
, DWORD keys
, INT x
, INT y
)
3612 es
->bCaptureState
= TRUE
;
3613 SetCapture(es
->hwndSelf
);
3614 EDIT_ConfinePoint(es
, &x
, &y
);
3615 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
3616 EDIT_EM_SetSel(es
, (keys
& MK_SHIFT
) ? es
->selection_start
: e
, e
, after_wrap
);
3617 EDIT_EM_ScrollCaret(es
);
3618 es
->region_posx
= es
->region_posy
= 0;
3619 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
3621 if (!(es
->flags
& EF_FOCUSED
))
3622 SetFocus(es
->hwndSelf
);
3628 /*********************************************************************
3633 static LRESULT
EDIT_WM_LButtonUp(EDITSTATE
*es
)
3635 if (es
->bCaptureState
) {
3636 KillTimer(es
->hwndSelf
, 0);
3637 if (GetCapture() == es
->hwndSelf
) ReleaseCapture();
3639 es
->bCaptureState
= FALSE
;
3644 /*********************************************************************
3649 static LRESULT
EDIT_WM_MButtonDown(EDITSTATE
*es
)
3651 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3656 /*********************************************************************
3661 static LRESULT
EDIT_WM_MouseMove(EDITSTATE
*es
, INT x
, INT y
)
3667 /* If the mouse has been captured by process other than the edit control itself,
3668 * the windows edit controls will not select the strings with mouse move.
3670 if (!es
->bCaptureState
|| GetCapture() != es
->hwndSelf
)
3674 * FIXME: gotta do some scrolling if outside client
3675 * area. Maybe reset the timer ?
3678 EDIT_ConfinePoint(es
, &x
, &y
);
3679 es
->region_posx
= (prex
< x
) ? -1 : ((prex
> x
) ? 1 : 0);
3680 es
->region_posy
= (prey
< y
) ? -1 : ((prey
> y
) ? 1 : 0);
3681 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
3682 EDIT_EM_SetSel(es
, es
->selection_start
, e
, after_wrap
);
3683 EDIT_SetCaretPos(es
,es
->selection_end
,es
->flags
& EF_AFTER_WRAP
);
3688 /*********************************************************************
3693 static void EDIT_WM_Paint(EDITSTATE
*es
, HDC hdc
)
3706 BOOL rev
= es
->bEnableState
&&
3707 ((es
->flags
& EF_FOCUSED
) ||
3708 (es
->style
& ES_NOHIDESEL
));
3709 dc
= hdc
? hdc
: BeginPaint(es
->hwndSelf
, &ps
);
3711 /* The dc we use for calculating may not be the one we paint into.
3712 This is the safest action. */
3713 EDIT_InvalidateUniscribeData(es
);
3714 GetClientRect(es
->hwndSelf
, &rcClient
);
3716 /* get the background brush */
3717 brush
= EDIT_NotifyCtlColor(es
, dc
);
3719 /* paint the border and the background */
3720 IntersectClipRect(dc
, rcClient
.left
, rcClient
.top
, rcClient
.right
, rcClient
.bottom
);
3722 if(es
->style
& WS_BORDER
) {
3723 bw
= GetSystemMetrics(SM_CXBORDER
);
3724 bh
= GetSystemMetrics(SM_CYBORDER
);
3726 if(es
->style
& ES_MULTILINE
) {
3727 if(es
->style
& WS_HSCROLL
) rc
.bottom
+=bh
;
3728 if(es
->style
& WS_VSCROLL
) rc
.right
+=bw
;
3731 /* Draw the frame. Same code as in nonclient.c */
3732 old_brush
= SelectObject(dc
, GetSysColorBrush(COLOR_WINDOWFRAME
));
3733 PatBlt(dc
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, bh
, PATCOPY
);
3734 PatBlt(dc
, rc
.left
, rc
.top
, bw
, rc
.bottom
- rc
.top
, PATCOPY
);
3735 PatBlt(dc
, rc
.left
, rc
.bottom
- 1, rc
.right
- rc
.left
, -bw
, PATCOPY
);
3736 PatBlt(dc
, rc
.right
- 1, rc
.top
, -bw
, rc
.bottom
- rc
.top
, PATCOPY
);
3737 SelectObject(dc
, old_brush
);
3739 /* Keep the border clean */
3740 IntersectClipRect(dc
, rc
.left
+bw
, rc
.top
+bh
,
3741 max(rc
.right
-bw
, rc
.left
+bw
), max(rc
.bottom
-bh
, rc
.top
+bh
));
3744 GetClipBox(dc
, &rc
);
3745 FillRect(dc
, &rc
, brush
);
3747 IntersectClipRect(dc
, es
->format_rect
.left
,
3748 es
->format_rect
.top
,
3749 es
->format_rect
.right
,
3750 es
->format_rect
.bottom
);
3751 if (es
->style
& ES_MULTILINE
) {
3753 IntersectClipRect(dc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
3756 old_font
= SelectObject(dc
, es
->font
);
3758 if (!es
->bEnableState
)
3759 SetTextColor(dc
, GetSysColor(COLOR_GRAYTEXT
));
3760 GetClipBox(dc
, &rcRgn
);
3761 if (es
->style
& ES_MULTILINE
) {
3762 INT vlc
= get_vertical_line_count(es
);
3763 for (i
= es
->y_offset
; i
<= min(es
->y_offset
+ vlc
, es
->y_offset
+ es
->line_count
- 1) ; i
++) {
3764 EDIT_UpdateUniscribeData(es
, dc
, i
);
3765 EDIT_GetLineRect(es
, i
, 0, -1, &rcLine
);
3766 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
3767 EDIT_PaintLine(es
, dc
, i
, rev
);
3770 EDIT_UpdateUniscribeData(es
, dc
, 0);
3771 EDIT_GetLineRect(es
, 0, 0, -1, &rcLine
);
3772 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
3773 EDIT_PaintLine(es
, dc
, 0, rev
);
3776 SelectObject(dc
, old_font
);
3779 EndPaint(es
->hwndSelf
, &ps
);
3783 /*********************************************************************
3788 static void EDIT_WM_SetFocus(EDITSTATE
*es
)
3790 es
->flags
|= EF_FOCUSED
;
3792 if (!(es
->style
& ES_NOHIDESEL
))
3793 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
3795 /* single line edit updates itself */
3796 if (IsWindowVisible(es
->hwndSelf
) && !(es
->style
& ES_MULTILINE
))
3798 HDC hdc
= GetDC(es
->hwndSelf
);
3799 EDIT_WM_Paint(es
, hdc
);
3800 ReleaseDC(es
->hwndSelf
, hdc
);
3803 CreateCaret(es
->hwndSelf
, 0, 1, es
->line_height
);
3804 EDIT_SetCaretPos(es
, es
->selection_end
,
3805 es
->flags
& EF_AFTER_WRAP
);
3806 ShowCaret(es
->hwndSelf
);
3807 EDIT_NOTIFY_PARENT(es
, EN_SETFOCUS
);
3810 static DWORD
get_font_margins(HDC hdc
, const TEXTMETRICW
*tm
, BOOL unicode
)
3816 if (!(tm
->tmPitchAndFamily
& (TMPF_VECTOR
| TMPF_TRUETYPE
)))
3817 return MAKELONG(EC_USEFONTINFO
, EC_USEFONTINFO
);
3820 if (!is_cjk_charset(hdc
) && !is_cjk_font(hdc
))
3821 return MAKELONG(EC_USEFONTINFO
, EC_USEFONTINFO
);
3822 if (!GetCharABCWidthsW(hdc
, 0, 255, abc
))
3825 else if (is_cjk_charset(hdc
)) {
3826 if (!GetCharABCWidthsA(hdc
, 0, 255, abc
))
3830 return MAKELONG(EC_USEFONTINFO
, EC_USEFONTINFO
);
3833 for (i
= 0; i
< ARRAY_SIZE(abc
); i
++) {
3834 if (-abc
[i
].abcA
> right
) right
= -abc
[i
].abcA
;
3835 if (-abc
[i
].abcC
> left
) left
= -abc
[i
].abcC
;
3837 return MAKELONG(left
, right
);
3840 /*********************************************************************
3844 * With Win95 look the margins are set to default font value unless
3845 * the system font (font == 0) is being set, in which case they are left
3849 static void EDIT_WM_SetFont(EDITSTATE
*es
, HFONT font
, BOOL redraw
)
3858 EDIT_InvalidateUniscribeData(es
);
3859 dc
= GetDC(es
->hwndSelf
);
3861 old_font
= SelectObject(dc
, font
);
3862 GetTextMetricsW(dc
, &tm
);
3863 es
->line_height
= tm
.tmHeight
;
3864 es
->char_width
= tm
.tmAveCharWidth
;
3865 margins
= get_font_margins(dc
, &tm
, es
->is_unicode
);
3867 SelectObject(dc
, old_font
);
3868 ReleaseDC(es
->hwndSelf
, dc
);
3870 /* Reset the format rect and the margins */
3871 GetClientRect(es
->hwndSelf
, &clientRect
);
3872 EDIT_SetRectNP(es
, &clientRect
);
3874 EDIT_EM_SetMargins(es
, EC_LEFTMARGIN
| EC_RIGHTMARGIN
,
3875 LOWORD(margins
), HIWORD(margins
), FALSE
);
3877 if (es
->style
& ES_MULTILINE
)
3878 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
3880 EDIT_CalcLineWidth_SL(es
);
3883 EDIT_UpdateText(es
, NULL
, TRUE
);
3884 if (es
->flags
& EF_FOCUSED
) {
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
);
3894 /*********************************************************************
3899 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3900 * The modified flag is reset. No notifications are sent.
3902 * For single-line controls, reception of WM_SETTEXT triggers:
3903 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3906 static void EDIT_WM_SetText(EDITSTATE
*es
, LPCWSTR text
, BOOL unicode
)
3908 LPWSTR textW
= NULL
;
3909 if (!unicode
&& text
)
3911 LPCSTR textA
= (LPCSTR
)text
;
3912 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
3913 textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
));
3915 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
3919 if (es
->flags
& EF_UPDATE
)
3920 /* fixed this bug once; complain if we see it about to happen again. */
3921 ERR("SetSel may generate UPDATE message whose handler may reset "
3924 EDIT_EM_SetSel(es
, 0, (UINT
)-1, FALSE
);
3927 TRACE("%s\n", debugstr_w(text
));
3928 EDIT_EM_ReplaceSel(es
, FALSE
, text
, lstrlenW(text
), FALSE
, FALSE
);
3930 HeapFree(GetProcessHeap(), 0, textW
);
3935 EDIT_EM_ReplaceSel(es
, FALSE
, NULL
, 0, FALSE
, FALSE
);
3938 es
->flags
&= ~EF_MODIFIED
;
3939 EDIT_EM_SetSel(es
, 0, 0, FALSE
);
3941 /* Send the notification after the selection start and end have been set
3942 * edit control doesn't send notification on WM_SETTEXT
3943 * if it is multiline, or it is part of combobox
3945 if( !((es
->style
& ES_MULTILINE
) || es
->hwndListBox
))
3947 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
3948 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
3950 EDIT_EM_ScrollCaret(es
);
3951 EDIT_UpdateScrollInfo(es
);
3952 EDIT_InvalidateUniscribeData(es
);
3956 /*********************************************************************
3961 static void EDIT_WM_Size(EDITSTATE
*es
, UINT action
)
3963 if ((action
== SIZE_MAXIMIZED
) || (action
== SIZE_RESTORED
)) {
3965 GetClientRect(es
->hwndSelf
, &rc
);
3966 EDIT_SetRectNP(es
, &rc
);
3967 EDIT_UpdateText(es
, NULL
, TRUE
);
3972 /*********************************************************************
3976 * This message is sent by SetWindowLong on having changed either the Style
3977 * or the extended style.
3979 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3981 * See also EDIT_WM_NCCreate
3983 * It appears that the Windows version of the edit control allows the style
3984 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3985 * style variable which will generally be different. In this function we
3986 * update the internal style based on what changed in the externally visible
3989 * Much of this content as based upon the MSDN, especially:
3990 * Platform SDK Documentation -> User Interface Services ->
3991 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3992 * Edit Control Styles
3994 static LRESULT
EDIT_WM_StyleChanged ( EDITSTATE
*es
, WPARAM which
, const STYLESTRUCT
*style
)
3996 if (GWL_STYLE
== which
) {
3997 DWORD style_change_mask
;
3999 /* Only a subset of changes can be applied after the control
4002 style_change_mask
= ES_UPPERCASE
| ES_LOWERCASE
|
4004 if (es
->style
& ES_MULTILINE
)
4005 style_change_mask
|= ES_WANTRETURN
;
4007 new_style
= style
->styleNew
& style_change_mask
;
4009 /* Number overrides lowercase overrides uppercase (at least it
4010 * does in Win95). However I'll bet that ES_NUMBER would be
4011 * invalid under Win 3.1.
4013 if (new_style
& ES_NUMBER
) {
4014 ; /* do not override the ES_NUMBER */
4015 } else if (new_style
& ES_LOWERCASE
) {
4016 new_style
&= ~ES_UPPERCASE
;
4019 es
->style
= (es
->style
& ~style_change_mask
) | new_style
;
4020 } else if (GWL_EXSTYLE
== which
) {
4021 ; /* FIXME - what is needed here */
4023 WARN ("Invalid style change %ld\n",which
);
4029 /*********************************************************************
4034 static LRESULT
EDIT_WM_SysKeyDown(EDITSTATE
*es
, INT key
, DWORD key_data
)
4036 if ((key
== VK_BACK
) && (key_data
& 0x2000)) {
4037 if (EDIT_EM_CanUndo(es
))
4040 } else if (key
== VK_UP
|| key
== VK_DOWN
) {
4041 if (EDIT_CheckCombo(es
, WM_SYSKEYDOWN
, key
))
4044 return DefWindowProcW(es
->hwndSelf
, WM_SYSKEYDOWN
, key
, key_data
);
4048 /*********************************************************************
4053 static void EDIT_WM_Timer(EDITSTATE
*es
)
4055 if (es
->region_posx
< 0) {
4056 EDIT_MoveBackward(es
, TRUE
);
4057 } else if (es
->region_posx
> 0) {
4058 EDIT_MoveForward(es
, TRUE
);
4061 * FIXME: gotta do some vertical scrolling here, like
4062 * EDIT_EM_LineScroll(hwnd, 0, 1);
4066 /*********************************************************************
4071 static LRESULT
EDIT_WM_HScroll(EDITSTATE
*es
, INT action
, INT pos
)
4076 if (!(es
->style
& ES_MULTILINE
))
4079 if (!(es
->style
& ES_AUTOHSCROLL
))
4083 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4086 TRACE("SB_LINELEFT\n");
4088 dx
= -es
->char_width
;
4091 TRACE("SB_LINERIGHT\n");
4092 if (es
->x_offset
< es
->text_width
)
4093 dx
= es
->char_width
;
4096 TRACE("SB_PAGELEFT\n");
4098 dx
= -fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4101 TRACE("SB_PAGERIGHT\n");
4102 if (es
->x_offset
< es
->text_width
)
4103 dx
= fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4111 TRACE("SB_RIGHT\n");
4112 if (es
->x_offset
< es
->text_width
)
4113 dx
= es
->text_width
- es
->x_offset
;
4116 TRACE("SB_THUMBTRACK %d\n", pos
);
4117 es
->flags
|= EF_HSCROLL_TRACK
;
4118 if(es
->style
& WS_HSCROLL
)
4119 dx
= pos
- es
->x_offset
;
4124 if(pos
< 0 || pos
> 100) return 0;
4125 /* Assume default scroll range 0-100 */
4126 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4127 new_x
= pos
* (es
->text_width
- fw
) / 100;
4128 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4131 case SB_THUMBPOSITION
:
4132 TRACE("SB_THUMBPOSITION %d\n", pos
);
4133 es
->flags
&= ~EF_HSCROLL_TRACK
;
4134 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4135 dx
= pos
- es
->x_offset
;
4140 if(pos
< 0 || pos
> 100) return 0;
4141 /* Assume default scroll range 0-100 */
4142 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4143 new_x
= pos
* (es
->text_width
- fw
) / 100;
4144 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4147 /* force scroll info update */
4148 EDIT_UpdateScrollInfo(es
);
4149 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
4153 TRACE("SB_ENDSCROLL\n");
4156 * FIXME : the next two are undocumented !
4157 * Are we doing the right thing ?
4158 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4159 * although it's also a regular control message.
4161 case EM_GETTHUMB
: /* this one is used by NT notepad */
4164 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4165 ret
= GetScrollPos(es
->hwndSelf
, SB_HORZ
);
4168 /* Assume default scroll range 0-100 */
4169 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4170 ret
= es
->text_width
? es
->x_offset
* 100 / (es
->text_width
- fw
) : 0;
4172 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4176 TRACE("EM_LINESCROLL16\n");
4181 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4187 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4188 /* check if we are going to move too far */
4189 if(es
->x_offset
+ dx
+ fw
> es
->text_width
)
4190 dx
= es
->text_width
- fw
- es
->x_offset
;
4192 EDIT_EM_LineScroll_internal(es
, dx
, 0);
4198 /*********************************************************************
4203 static LRESULT
EDIT_WM_VScroll(EDITSTATE
*es
, INT action
, INT pos
)
4207 if (!(es
->style
& ES_MULTILINE
))
4210 if (!(es
->style
& ES_AUTOVSCROLL
))
4219 TRACE("action %d (%s)\n", action
, (action
== SB_LINEUP
? "SB_LINEUP" :
4220 (action
== SB_LINEDOWN
? "SB_LINEDOWN" :
4221 (action
== SB_PAGEUP
? "SB_PAGEUP" :
4223 EDIT_EM_Scroll(es
, action
);
4230 TRACE("SB_BOTTOM\n");
4231 dy
= es
->line_count
- 1 - es
->y_offset
;
4234 TRACE("SB_THUMBTRACK %d\n", pos
);
4235 es
->flags
|= EF_VSCROLL_TRACK
;
4236 if(es
->style
& WS_VSCROLL
)
4237 dy
= pos
- es
->y_offset
;
4240 /* Assume default scroll range 0-100 */
4243 if(pos
< 0 || pos
> 100) return 0;
4244 vlc
= get_vertical_line_count(es
);
4245 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4246 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4247 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4248 es
->line_count
, es
->y_offset
, pos
, dy
);
4251 case SB_THUMBPOSITION
:
4252 TRACE("SB_THUMBPOSITION %d\n", pos
);
4253 es
->flags
&= ~EF_VSCROLL_TRACK
;
4254 if(es
->style
& WS_VSCROLL
)
4255 dy
= pos
- es
->y_offset
;
4258 /* Assume default scroll range 0-100 */
4261 if(pos
< 0 || pos
> 100) return 0;
4262 vlc
= get_vertical_line_count(es
);
4263 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4264 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4265 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4266 es
->line_count
, es
->y_offset
, pos
, dy
);
4270 /* force scroll info update */
4271 EDIT_UpdateScrollInfo(es
);
4272 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
4276 TRACE("SB_ENDSCROLL\n");
4279 * FIXME : the next two are undocumented !
4280 * Are we doing the right thing ?
4281 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4282 * although it's also a regular control message.
4284 case EM_GETTHUMB
: /* this one is used by NT notepad */
4287 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_VSCROLL
)
4288 ret
= GetScrollPos(es
->hwndSelf
, SB_VERT
);
4291 /* Assume default scroll range 0-100 */
4292 INT vlc
= get_vertical_line_count(es
);
4293 ret
= es
->line_count
? es
->y_offset
* 100 / (es
->line_count
- vlc
) : 0;
4295 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4299 TRACE("EM_LINESCROLL %d\n", pos
);
4304 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4309 EDIT_EM_LineScroll(es
, 0, dy
);
4313 /*********************************************************************
4317 * FIXME: is this right ? (or should it be only VSCROLL)
4318 * (and maybe only for edit controls that really have their
4319 * own scrollbars) (and maybe only for multiline controls ?)
4320 * All in all: very poorly documented
4323 static LRESULT
EDIT_EM_GetThumb(EDITSTATE
*es
)
4325 return MAKELONG(EDIT_WM_VScroll(es
, EM_GETTHUMB
, 0),
4326 EDIT_WM_HScroll(es
, EM_GETTHUMB
, 0));
4330 /********************************************************************
4332 * The Following code is to handle inline editing from IMEs
4335 static void EDIT_GetCompositionStr(HIMC hIMC
, LPARAM CompFlag
, EDITSTATE
*es
)
4339 LPSTR lpCompStrAttr
= NULL
;
4342 buflen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
4349 lpCompStr
= HeapAlloc(GetProcessHeap(),0,buflen
);
4352 ERR("Unable to allocate IME CompositionString\n");
4357 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, buflen
);
4359 if (CompFlag
& GCS_COMPATTR
)
4362 * We do not use the attributes yet. it would tell us what characters
4363 * are in transition and which are converted or decided upon
4365 dwBufLenAttr
= ImmGetCompositionStringW(hIMC
, GCS_COMPATTR
, NULL
, 0);
4369 lpCompStrAttr
= HeapAlloc(GetProcessHeap(),0,dwBufLenAttr
+1);
4372 ERR("Unable to allocate IME Attribute String\n");
4373 HeapFree(GetProcessHeap(),0,lpCompStr
);
4376 ImmGetCompositionStringW(hIMC
,GCS_COMPATTR
, lpCompStrAttr
,
4378 lpCompStrAttr
[dwBufLenAttr
] = 0;
4382 /* check for change in composition start */
4383 if (es
->selection_end
< es
->composition_start
)
4384 es
->composition_start
= es
->selection_end
;
4386 /* replace existing selection string */
4387 es
->selection_start
= es
->composition_start
;
4389 if (es
->composition_len
> 0)
4390 es
->selection_end
= es
->composition_start
+ es
->composition_len
;
4392 es
->selection_end
= es
->selection_start
;
4394 EDIT_EM_ReplaceSel(es
, FALSE
, lpCompStr
, buflen
/ sizeof(WCHAR
), TRUE
, TRUE
);
4395 es
->composition_len
= abs(es
->composition_start
- es
->selection_end
);
4397 es
->selection_start
= es
->composition_start
;
4398 es
->selection_end
= es
->selection_start
+ es
->composition_len
;
4400 HeapFree(GetProcessHeap(),0,lpCompStrAttr
);
4401 HeapFree(GetProcessHeap(),0,lpCompStr
);
4404 static void EDIT_GetResultStr(HIMC hIMC
, EDITSTATE
*es
)
4409 buflen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
4415 lpResultStr
= HeapAlloc(GetProcessHeap(),0, buflen
);
4418 ERR("Unable to alloc buffer for IME string\n");
4422 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpResultStr
, buflen
);
4424 /* check for change in composition start */
4425 if (es
->selection_end
< es
->composition_start
)
4426 es
->composition_start
= es
->selection_end
;
4428 es
->selection_start
= es
->composition_start
;
4429 es
->selection_end
= es
->composition_start
+ es
->composition_len
;
4430 EDIT_EM_ReplaceSel(es
, TRUE
, lpResultStr
, buflen
/ sizeof(WCHAR
), TRUE
, TRUE
);
4431 es
->composition_start
= es
->selection_end
;
4432 es
->composition_len
= 0;
4434 HeapFree(GetProcessHeap(),0,lpResultStr
);
4437 static void EDIT_ImeComposition(HWND hwnd
, LPARAM CompFlag
, EDITSTATE
*es
)
4442 if (es
->composition_len
== 0 && es
->selection_start
!= es
->selection_end
)
4444 EDIT_EM_ReplaceSel(es
, TRUE
, NULL
, 0, TRUE
, TRUE
);
4445 es
->composition_start
= es
->selection_end
;
4448 hIMC
= ImmGetContext(hwnd
);
4452 if (CompFlag
& GCS_RESULTSTR
)
4454 EDIT_GetResultStr(hIMC
, es
);
4459 if (CompFlag
& GCS_COMPSTR
)
4460 EDIT_GetCompositionStr(hIMC
, CompFlag
, es
);
4461 cursor
= ImmGetCompositionStringW(hIMC
, GCS_CURSORPOS
, 0, 0);
4463 ImmReleaseContext(hwnd
, hIMC
);
4464 EDIT_SetCaretPos(es
, es
->selection_start
+ cursor
, es
->flags
& EF_AFTER_WRAP
);
4468 /*********************************************************************
4472 * See also EDIT_WM_StyleChanged
4474 static LRESULT
EDIT_WM_NCCreate(HWND hwnd
, LPCREATESTRUCTW lpcs
, BOOL unicode
)
4479 TRACE("Creating %s edit control, style = %08x\n",
4480 unicode
? "Unicode" : "ANSI", lpcs
->style
);
4482 if (!(es
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*es
))))
4484 SetWindowLongPtrW( hwnd
, 0, (LONG_PTR
)es
);
4487 * Note: since the EDITSTATE has not been fully initialized yet,
4488 * we can't use any API calls that may send
4489 * WM_XXX messages before WM_NCCREATE is completed.
4492 es
->is_unicode
= unicode
;
4493 es
->style
= lpcs
->style
;
4495 es
->bEnableState
= !(es
->style
& WS_DISABLED
);
4497 es
->hwndSelf
= hwnd
;
4498 /* Save parent, which will be notified by EN_* messages */
4499 es
->hwndParent
= lpcs
->hwndParent
;
4501 if (es
->style
& ES_COMBO
)
4502 es
->hwndListBox
= GetDlgItem(es
->hwndParent
, ID_CB_LISTBOX
);
4504 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4505 if (lpcs
->dwExStyle
& WS_EX_RIGHT
) es
->style
|= ES_RIGHT
;
4507 /* Number overrides lowercase overrides uppercase (at least it
4508 * does in Win95). However I'll bet that ES_NUMBER would be
4509 * invalid under Win 3.1.
4511 if (es
->style
& ES_NUMBER
) {
4512 ; /* do not override the ES_NUMBER */
4513 } else if (es
->style
& ES_LOWERCASE
) {
4514 es
->style
&= ~ES_UPPERCASE
;
4516 if (es
->style
& ES_MULTILINE
) {
4517 es
->buffer_limit
= BUFLIMIT_INITIAL
;
4518 if (es
->style
& WS_VSCROLL
)
4519 es
->style
|= ES_AUTOVSCROLL
;
4520 if (es
->style
& WS_HSCROLL
)
4521 es
->style
|= ES_AUTOHSCROLL
;
4522 es
->style
&= ~ES_PASSWORD
;
4523 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
)) {
4524 /* Confirmed - RIGHT overrides CENTER */
4525 if (es
->style
& ES_RIGHT
)
4526 es
->style
&= ~ES_CENTER
;
4527 es
->style
&= ~WS_HSCROLL
;
4528 es
->style
&= ~ES_AUTOHSCROLL
;
4531 es
->buffer_limit
= BUFLIMIT_INITIAL
;
4532 if ((es
->style
& ES_RIGHT
) && (es
->style
& ES_CENTER
))
4533 es
->style
&= ~ES_CENTER
;
4534 es
->style
&= ~WS_HSCROLL
;
4535 es
->style
&= ~WS_VSCROLL
;
4536 if (es
->style
& ES_PASSWORD
)
4537 es
->password_char
= '*';
4540 alloc_size
= ROUND_TO_GROW((es
->buffer_size
+ 1) * sizeof(WCHAR
));
4541 if(!(es
->hloc32W
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
4543 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
4545 if (!(es
->undo_text
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (es
->buffer_size
+ 1) * sizeof(WCHAR
))))
4547 es
->undo_buffer_size
= es
->buffer_size
;
4549 if (es
->style
& ES_MULTILINE
)
4550 if (!(es
->first_line_def
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
))))
4555 * In Win95 look and feel, the WS_BORDER style is replaced by the
4556 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4557 * control a nonclient area so we don't need to draw the border.
4558 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4559 * a nonclient area and we should handle painting the border ourselves.
4561 * When making modifications please ensure that the code still works
4562 * for edit controls created directly with style 0x50800000, exStyle 0
4563 * (which should have a single pixel border)
4565 if (lpcs
->dwExStyle
& WS_EX_CLIENTEDGE
)
4566 es
->style
&= ~WS_BORDER
;
4567 else if (es
->style
& WS_BORDER
)
4568 SetWindowLongW(hwnd
, GWL_STYLE
, es
->style
& ~WS_BORDER
);
4573 SetWindowLongPtrW(es
->hwndSelf
, 0, 0);
4574 EDIT_InvalidateUniscribeData(es
);
4575 HeapFree(GetProcessHeap(), 0, es
->first_line_def
);
4576 HeapFree(GetProcessHeap(), 0, es
->undo_text
);
4577 if (es
->hloc32W
) LocalFree(es
->hloc32W
);
4578 HeapFree(GetProcessHeap(), 0, es
->logAttr
);
4579 HeapFree(GetProcessHeap(), 0, es
);
4584 /*********************************************************************
4589 static LRESULT
EDIT_WM_Create(EDITSTATE
*es
, LPCWSTR name
)
4593 TRACE("%s\n", debugstr_w(name
));
4595 * To initialize some final structure members, we call some helper
4596 * functions. However, since the EDITSTATE is not consistent (i.e.
4597 * not fully initialized), we should be very careful which
4598 * functions can be called, and in what order.
4600 EDIT_WM_SetFont(es
, 0, FALSE
);
4601 EDIT_EM_EmptyUndoBuffer(es
);
4603 /* We need to calculate the format rect
4604 (applications may send EM_SETMARGINS before the control gets visible) */
4605 GetClientRect(es
->hwndSelf
, &clientRect
);
4606 EDIT_SetRectNP(es
, &clientRect
);
4608 if (name
&& *name
) {
4609 EDIT_EM_ReplaceSel(es
, FALSE
, name
, lstrlenW(name
), FALSE
, FALSE
);
4610 /* if we insert text to the editline, the text scrolls out
4611 * of the window, as the caret is placed after the insert
4612 * pos normally; thus we reset es->selection... to 0 and
4615 es
->selection_start
= es
->selection_end
= 0;
4616 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4617 * Messages are only to be sent when the USER does something to
4618 * change the contents. So I am removing this EN_CHANGE
4620 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4622 EDIT_EM_ScrollCaret(es
);
4624 /* force scroll info update */
4625 EDIT_UpdateScrollInfo(es
);
4626 /* The rule seems to return 1 here for success */
4627 /* Power Builder masked edit controls will crash */
4629 /* FIXME: is that in all cases so ? */
4634 /*********************************************************************
4639 static LRESULT
EDIT_WM_NCDestroy(EDITSTATE
*es
)
4643 /* The app can own the text buffer handle */
4644 if (es
->hloc32W
&& (es
->hloc32W
!= es
->hlocapp
)) {
4645 LocalFree(es
->hloc32W
);
4647 if (es
->hloc32A
&& (es
->hloc32A
!= es
->hlocapp
)) {
4648 LocalFree(es
->hloc32A
);
4650 EDIT_InvalidateUniscribeData(es
);
4651 pc
= es
->first_line_def
;
4655 HeapFree(GetProcessHeap(), 0, pc
);
4659 SetWindowLongPtrW( es
->hwndSelf
, 0, 0 );
4660 HeapFree(GetProcessHeap(), 0, es
->undo_text
);
4661 HeapFree(GetProcessHeap(), 0, es
);
4667 static inline LRESULT
DefWindowProcT(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
4670 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
4672 return DefWindowProcA(hwnd
, msg
, wParam
, lParam
);
4675 /*********************************************************************
4677 * EditWndProc_common
4679 * The messages are in the order of the actual integer values
4680 * (which can be found in include/windows.h)
4682 LRESULT
EditWndProc_common( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
4684 EDITSTATE
*es
= (EDITSTATE
*)GetWindowLongPtrW( hwnd
, 0 );
4687 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), wParam
, lParam
);
4689 if (!es
&& msg
!= WM_NCCREATE
)
4690 return DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
4692 if (es
&& (msg
!= WM_NCDESTROY
)) EDIT_LockBuffer(es
);
4696 result
= EDIT_EM_GetSel(es
, (PUINT
)wParam
, (PUINT
)lParam
);
4700 EDIT_EM_SetSel(es
, wParam
, lParam
, FALSE
);
4701 EDIT_EM_ScrollCaret(es
);
4707 *((LPRECT
)lParam
) = es
->format_rect
;
4711 if ((es
->style
& ES_MULTILINE
) && lParam
) {
4712 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
4713 EDIT_UpdateText(es
, NULL
, TRUE
);
4718 if ((es
->style
& ES_MULTILINE
) && lParam
)
4719 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
4723 result
= EDIT_EM_Scroll(es
, (INT
)wParam
);
4727 result
= (LRESULT
)EDIT_EM_LineScroll(es
, (INT
)wParam
, (INT
)lParam
);
4730 case EM_SCROLLCARET
:
4731 EDIT_EM_ScrollCaret(es
);
4736 result
= ((es
->flags
& EF_MODIFIED
) != 0);
4741 es
->flags
|= EF_MODIFIED
;
4743 es
->flags
&= ~(EF_MODIFIED
| EF_UPDATE
); /* reset pending updates */
4746 case EM_GETLINECOUNT
:
4747 result
= (es
->style
& ES_MULTILINE
) ? es
->line_count
: 1;
4751 result
= (LRESULT
)EDIT_EM_LineIndex(es
, (INT
)wParam
);
4755 EDIT_EM_SetHandle(es
, (HLOCAL
)wParam
);
4759 result
= (LRESULT
)EDIT_EM_GetHandle(es
);
4763 result
= EDIT_EM_GetThumb(es
);
4766 /* these messages missing from specs */
4771 FIXME("undocumented message 0x%x, please report\n", msg
);
4772 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
4776 result
= (LRESULT
)EDIT_EM_LineLength(es
, (INT
)wParam
);
4784 textW
= (LPWSTR
)lParam
;
4787 LPSTR textA
= (LPSTR
)lParam
;
4788 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
4789 if (!(textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
)))) break;
4790 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
4793 EDIT_EM_ReplaceSel(es
, (BOOL
)wParam
, textW
, lstrlenW(textW
), TRUE
, TRUE
);
4797 HeapFree(GetProcessHeap(), 0, textW
);
4802 result
= (LRESULT
)EDIT_EM_GetLine(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
4805 case EM_SETLIMITTEXT
:
4806 EDIT_EM_SetLimitText(es
, wParam
);
4810 result
= (LRESULT
)EDIT_EM_CanUndo(es
);
4815 result
= (LRESULT
)EDIT_EM_Undo(es
);
4819 result
= (LRESULT
)EDIT_EM_FmtLines(es
, (BOOL
)wParam
);
4822 case EM_LINEFROMCHAR
:
4823 result
= (LRESULT
)EDIT_EM_LineFromChar(es
, (INT
)wParam
);
4826 case EM_SETTABSTOPS
:
4827 result
= (LRESULT
)EDIT_EM_SetTabStops(es
, (INT
)wParam
, (LPINT
)lParam
);
4830 case EM_SETPASSWORDCHAR
:
4835 charW
= (WCHAR
)wParam
;
4838 CHAR charA
= wParam
;
4839 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
4842 EDIT_EM_SetPasswordChar(es
, charW
);
4846 case EM_EMPTYUNDOBUFFER
:
4847 EDIT_EM_EmptyUndoBuffer(es
);
4850 case EM_GETFIRSTVISIBLELINE
:
4851 result
= (es
->style
& ES_MULTILINE
) ? es
->y_offset
: es
->x_offset
;
4854 case EM_SETREADONLY
:
4856 DWORD old_style
= es
->style
;
4859 SetWindowLongW( hwnd
, GWL_STYLE
,
4860 GetWindowLongW( hwnd
, GWL_STYLE
) | ES_READONLY
);
4861 es
->style
|= ES_READONLY
;
4863 SetWindowLongW( hwnd
, GWL_STYLE
,
4864 GetWindowLongW( hwnd
, GWL_STYLE
) & ~ES_READONLY
);
4865 es
->style
&= ~ES_READONLY
;
4868 if (old_style
^ es
->style
)
4869 InvalidateRect(es
->hwndSelf
, NULL
, TRUE
);
4875 case EM_SETWORDBREAKPROC
:
4876 EDIT_EM_SetWordBreakProc(es
, (void *)lParam
);
4880 case EM_GETWORDBREAKPROC
:
4881 result
= (LRESULT
)es
->word_break_proc
;
4884 case EM_GETPASSWORDCHAR
:
4887 result
= es
->password_char
;
4890 WCHAR charW
= es
->password_char
;
4892 WideCharToMultiByte(CP_ACP
, 0, &charW
, 1, &charA
, 1, NULL
, NULL
);
4899 EDIT_EM_SetMargins(es
, (INT
)wParam
, LOWORD(lParam
), HIWORD(lParam
), TRUE
);
4903 result
= MAKELONG(es
->left_margin
, es
->right_margin
);
4906 case EM_GETLIMITTEXT
:
4907 result
= es
->buffer_limit
;
4910 case EM_POSFROMCHAR
:
4911 if ((INT
)wParam
>= get_text_length(es
)) result
= -1;
4912 else result
= EDIT_EM_PosFromChar(es
, (INT
)wParam
, FALSE
);
4915 case EM_CHARFROMPOS
:
4916 result
= EDIT_EM_CharFromPos(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4919 /* End of the EM_ messages which were in numerical order; what order
4920 * are these in? vaguely alphabetical?
4924 result
= EDIT_WM_NCCreate(hwnd
, (LPCREATESTRUCTW
)lParam
, unicode
);
4928 result
= EDIT_WM_NCDestroy(es
);
4933 result
= DLGC_HASSETSEL
| DLGC_WANTCHARS
| DLGC_WANTARROWS
;
4935 if (es
->style
& ES_MULTILINE
)
4936 result
|= DLGC_WANTALLKEYS
;
4940 es
->flags
|=EF_DIALOGMODE
;
4942 if (((LPMSG
)lParam
)->message
== WM_KEYDOWN
)
4944 int vk
= (int)((LPMSG
)lParam
)->wParam
;
4946 if (es
->hwndListBox
)
4948 if (vk
== VK_RETURN
|| vk
== VK_ESCAPE
)
4949 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
4950 result
|= DLGC_WANTMESSAGE
;
4962 strng
[0] = wParam
>> 8;
4963 strng
[1] = wParam
& 0xff;
4964 if (strng
[0]) MultiByteToWideChar(CP_ACP
, 0, strng
, 2, &charW
, 1);
4965 else MultiByteToWideChar(CP_ACP
, 0, &strng
[1], 1, &charW
, 1);
4966 result
= EDIT_WM_Char(es
, charW
);
4978 CHAR charA
= wParam
;
4979 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
4982 if (es
->hwndListBox
)
4984 if (charW
== VK_RETURN
|| charW
== VK_ESCAPE
)
4986 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
4987 SendMessageW(GetParent(hwnd
), WM_KEYDOWN
, charW
, 0);
4991 result
= EDIT_WM_Char(es
, charW
);
4998 if (wParam
== UNICODE_NOCHAR
) return TRUE
;
4999 if (wParam
<= 0x000fffff)
5001 if(wParam
> 0xffff) /* convert to surrogates */
5004 EDIT_WM_Char(es
, (wParam
>> 10) + 0xd800);
5005 EDIT_WM_Char(es
, (wParam
& 0x03ff) + 0xdc00);
5007 else EDIT_WM_Char(es
, wParam
);
5017 case WM_CONTEXTMENU
:
5018 EDIT_WM_ContextMenu(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5027 result
= EDIT_WM_Create(es
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
5030 LPCSTR nameA
= ((LPCREATESTRUCTA
)lParam
)->lpszName
;
5031 LPWSTR nameW
= NULL
;
5034 INT countW
= MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, NULL
, 0);
5035 if((nameW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
))))
5036 MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, nameW
, countW
);
5038 result
= EDIT_WM_Create(es
, nameW
);
5039 HeapFree(GetProcessHeap(), 0, nameW
);
5048 es
->bEnableState
= (BOOL
) wParam
;
5049 EDIT_UpdateText(es
, NULL
, TRUE
);
5053 /* we do the proper erase in EDIT_WM_Paint */
5058 result
= (LRESULT
)es
->font
;
5062 result
= (LRESULT
)EDIT_WM_GetText(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
5065 case WM_GETTEXTLENGTH
:
5066 if (unicode
) result
= get_text_length(es
);
5067 else result
= WideCharToMultiByte( CP_ACP
, 0, es
->text
, get_text_length(es
),
5068 NULL
, 0, NULL
, NULL
);
5072 result
= EDIT_WM_HScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
5076 result
= EDIT_WM_KeyDown(es
, (INT
)wParam
);
5080 result
= EDIT_WM_KillFocus(es
);
5083 case WM_LBUTTONDBLCLK
:
5084 result
= EDIT_WM_LButtonDblClk(es
);
5087 case WM_LBUTTONDOWN
:
5088 result
= EDIT_WM_LButtonDown(es
, wParam
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5092 result
= EDIT_WM_LButtonUp(es
);
5095 case WM_MBUTTONDOWN
:
5096 result
= EDIT_WM_MButtonDown(es
);
5100 result
= EDIT_WM_MouseMove(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5103 case WM_PRINTCLIENT
:
5105 EDIT_WM_Paint(es
, (HDC
)wParam
);
5113 EDIT_WM_SetFocus(es
);
5117 EDIT_WM_SetFont(es
, (HFONT
)wParam
, LOWORD(lParam
) != 0);
5121 /* FIXME: actually set an internal flag and behave accordingly */
5125 EDIT_WM_SetText(es
, (LPCWSTR
)lParam
, unicode
);
5130 EDIT_WM_Size(es
, (UINT
)wParam
);
5133 case WM_STYLECHANGED
:
5134 result
= EDIT_WM_StyleChanged(es
, wParam
, (const STYLESTRUCT
*)lParam
);
5137 case WM_STYLECHANGING
:
5138 result
= 0; /* See EDIT_WM_StyleChanged */
5142 result
= EDIT_WM_SysKeyDown(es
, (INT
)wParam
, (DWORD
)lParam
);
5150 result
= EDIT_WM_VScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
5156 INT pulScrollLines
= 3;
5157 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
5159 if (wParam
& (MK_SHIFT
| MK_CONTROL
)) {
5160 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
5163 wheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
5164 /* if scrolling changes direction, ignore left overs */
5165 if ((wheelDelta
< 0 && es
->wheelDeltaRemainder
< 0) ||
5166 (wheelDelta
> 0 && es
->wheelDeltaRemainder
> 0))
5167 es
->wheelDeltaRemainder
+= wheelDelta
;
5169 es
->wheelDeltaRemainder
= wheelDelta
;
5170 if (es
->wheelDeltaRemainder
&& pulScrollLines
)
5173 pulScrollLines
= min(es
->line_count
, pulScrollLines
);
5174 cLineScroll
= pulScrollLines
* es
->wheelDeltaRemainder
/ WHEEL_DELTA
;
5175 es
->wheelDeltaRemainder
-= WHEEL_DELTA
* cLineScroll
/ pulScrollLines
;
5176 result
= EDIT_EM_LineScroll(es
, 0, -cLineScroll
);
5182 /* IME messages to make the edit control IME aware */
5183 case WM_IME_SETCONTEXT
:
5186 case WM_IME_STARTCOMPOSITION
:
5187 es
->composition_start
= es
->selection_end
;
5188 es
->composition_len
= 0;
5191 case WM_IME_COMPOSITION
:
5192 EDIT_ImeComposition(hwnd
, lParam
, es
);
5195 case WM_IME_ENDCOMPOSITION
:
5196 if (es
->composition_len
> 0)
5198 EDIT_EM_ReplaceSel(es
, TRUE
, NULL
, 0, TRUE
, TRUE
);
5199 es
->selection_end
= es
->selection_start
;
5200 es
->composition_len
= 0;
5204 case WM_IME_COMPOSITIONFULL
:
5210 case WM_IME_CONTROL
:
5213 case WM_IME_REQUEST
:
5216 case IMR_QUERYCHARPOSITION
:
5219 IMECHARPOSITION
*chpos
= (IMECHARPOSITION
*)lParam
;
5221 pos
= EDIT_EM_PosFromChar(es
, es
->selection_start
+ chpos
->dwCharPos
, FALSE
);
5222 chpos
->pt
.x
= LOWORD(pos
);
5223 chpos
->pt
.y
= HIWORD(pos
);
5224 chpos
->cLineHeight
= es
->line_height
;
5225 chpos
->rcDocument
= es
->format_rect
;
5226 MapWindowPoints(hwnd
, 0, &chpos
->pt
, 1);
5227 MapWindowPoints(hwnd
, 0, (POINT
*)&chpos
->rcDocument
, 2);
5235 result
= DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
5239 if (IsWindow(hwnd
) && es
&& msg
!= EM_GETHANDLE
)
5240 EDIT_UnlockBuffer(es
, FALSE
);
5242 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), result
);
5248 /*********************************************************************
5249 * edit class descriptor
5251 const struct builtin_class_descr EDIT_builtin_class
=
5254 CS_DBLCLKS
| CS_PARENTDC
, /* style */
5255 WINPROC_EDIT
, /* proc */
5257 sizeof(EDITSTATE
*) + sizeof(WORD
), /* extra */
5259 sizeof(EDITSTATE
*), /* extra */
5261 IDC_IBEAM
, /* cursor */