4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
57 #include "wine/unicode.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(edit
);
63 WINE_DECLARE_DEBUG_CHANNEL(combo
);
64 WINE_DECLARE_DEBUG_CHANNEL(relay
);
66 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
67 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
68 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
69 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
72 * extra flags for EDITSTATE.flags field
74 #define EF_MODIFIED 0x0001 /* text has been modified */
75 #define EF_FOCUSED 0x0002 /* we have input focus */
76 #define EF_UPDATE 0x0004 /* notify parent of changed state */
77 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
79 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
80 wrapped line, instead of in front of the next character */
81 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
82 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
86 END_0
= 0, /* line ends with terminating '\0' character */
87 END_WRAP
, /* line is wrapped */
88 END_HARD
, /* line ends with a hard return '\r\n' */
89 END_SOFT
, /* line ends with a soft return '\r\r\n' */
90 END_RICH
/* line ends with a single '\n' */
93 typedef struct tagLINEDEF
{
94 INT length
; /* bruto length of a line in bytes */
95 INT net_length
; /* netto length of a line in visible characters */
97 INT width
; /* width of the line in pixels */
98 INT index
; /* line index into the buffer */
99 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data */
100 struct tagLINEDEF
*next
;
105 BOOL is_unicode
; /* how the control was created */
106 LPWSTR text
; /* the actual contents of the control */
107 UINT text_length
; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
108 UINT buffer_size
; /* the size of the buffer in characters */
109 UINT buffer_limit
; /* the maximum size to which the buffer may grow in characters */
110 HFONT font
; /* NULL means standard system font */
111 INT x_offset
; /* scroll offset for multi lines this is in pixels
112 for single lines it's in characters */
113 INT line_height
; /* height of a screen line in pixels */
114 INT char_width
; /* average character width in pixels */
115 DWORD style
; /* sane version of wnd->dwStyle */
116 WORD flags
; /* flags that are not in es->style or wnd->flags (EF_XXX) */
117 INT undo_insert_count
; /* number of characters inserted in sequence */
118 UINT undo_position
; /* character index of the insertion and deletion */
119 LPWSTR undo_text
; /* deleted text */
120 UINT undo_buffer_size
; /* size of the deleted text buffer */
121 INT selection_start
; /* == selection_end if no selection */
122 INT selection_end
; /* == current caret position */
123 WCHAR password_char
; /* == 0 if no password char, and for multi line controls */
124 INT left_margin
; /* in pixels */
125 INT right_margin
; /* in pixels */
127 INT text_width
; /* width of the widest line in pixels for multi line controls
128 and just line width for single line controls */
129 INT region_posx
; /* Position of cursor relative to region: */
130 INT region_posy
; /* -1: to left, 0: within, 1: to right */
131 void *word_break_proc
; /* 32-bit word break proc: ANSI or Unicode */
132 INT line_count
; /* number of lines */
133 INT y_offset
; /* scroll offset in number of lines */
134 BOOL bCaptureState
; /* flag indicating whether mouse was captured */
135 BOOL bEnableState
; /* flag keeping the enable state */
136 HWND hwndSelf
; /* the our window handle */
137 HWND hwndParent
; /* Handle of parent for sending EN_* messages.
138 Even if parent will change, EN_* messages
139 should be sent to the first parent. */
140 HWND hwndListBox
; /* handle of ComboBox's listbox or NULL */
141 INT wheelDeltaRemainder
; /* scroll wheel delta left over after scrolling whole lines */
143 * only for multi line controls
145 INT lock_count
; /* amount of re-entries in the EditWndProc */
148 LINEDEF
*first_line_def
; /* linked list of (soft) linebreaks */
149 HLOCAL hloc32W
; /* our unicode local memory block */
150 HLOCAL hloc32A
; /* alias for ANSI control receiving EM_GETHANDLE
152 HLOCAL hlocapp
; /* The text buffer handle belongs to the app */
156 UINT composition_len
; /* length of composition, 0 == no composition */
157 int composition_start
; /* the character position for the composition */
161 SCRIPT_LOGATTR
*logAttr
;
162 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data for single line controls */
166 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
167 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
169 /* used for disabled or read-only edit control */
170 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
172 { /* Notify parent which has created this edit control */ \
173 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
174 SendMessageW(es->hwndParent, WM_COMMAND, \
175 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
176 (LPARAM)(es->hwndSelf)); \
179 static const WCHAR empty_stringW
[] = {0};
180 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
);
182 /*********************************************************************
187 static inline BOOL
EDIT_EM_CanUndo(const EDITSTATE
*es
)
189 return (es
->undo_insert_count
|| strlenW(es
->undo_text
));
193 /*********************************************************************
198 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE
*es
)
200 es
->undo_insert_count
= 0;
201 *es
->undo_text
= '\0';
205 /**********************************************************************
208 * Returns the window version in case Wine emulates a later version
209 * of windows than the application expects.
211 * In a number of cases when windows runs an application that was
212 * designed for an earlier windows version, windows reverts
213 * to "old" behaviour of that earlier version.
215 * An example is a disabled edit control that needs to be painted.
216 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
217 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
218 * applications with an expected version 0f 4.0 or higher.
221 static DWORD
get_app_version(void)
223 static DWORD version
;
226 DWORD dwEmulatedVersion
;
228 DWORD dwProcVersion
= GetProcessVersion(0);
230 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOW
);
231 GetVersionExW( &info
);
232 dwEmulatedVersion
= MAKELONG( info
.dwMinorVersion
, info
.dwMajorVersion
);
233 /* FIXME: this may not be 100% correct; see discussion on the
234 * wine developer list in Nov 1999 */
235 version
= dwProcVersion
< dwEmulatedVersion
? dwProcVersion
: dwEmulatedVersion
;
240 static HBRUSH
EDIT_NotifyCtlColor(EDITSTATE
*es
, HDC hdc
)
245 if ( get_app_version() >= 0x40000 && (!es
->bEnableState
|| (es
->style
& ES_READONLY
)))
246 msg
= WM_CTLCOLORSTATIC
;
248 msg
= WM_CTLCOLOREDIT
;
250 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
251 hbrush
= (HBRUSH
)SendMessageW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
253 hbrush
= (HBRUSH
)DefWindowProcW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
258 static inline UINT
get_text_length(EDITSTATE
*es
)
260 if(es
->text_length
== (UINT
)-1)
261 es
->text_length
= strlenW(es
->text
);
262 return es
->text_length
;
266 /*********************************************************************
270 * Find the beginning of words.
271 * Note: unlike the specs for a WordBreakProc, this function can
272 * only be called without linebreaks between s[0] up to
273 * s[count - 1]. Remember it is only called
274 * internally, so we can decide this for ourselves.
275 * Additionally we will always be breaking the full string.
278 static INT
EDIT_WordBreakProc(EDITSTATE
*es
, LPWSTR s
, INT index
, INT count
, INT action
)
282 TRACE("s=%p, index=%d, count=%d, action=%d\n", s
, index
, count
, action
);
290 memset(&psa
,0,sizeof(SCRIPT_ANALYSIS
));
291 psa
.eScript
= SCRIPT_UNDEFINED
;
293 es
->logAttr
= HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR
) * get_text_length(es
));
294 ScriptBreak(es
->text
, get_text_length(es
), &psa
, es
->logAttr
);
301 while (index
&& !es
->logAttr
[index
].fSoftBreak
)
308 while (index
< count
&& s
[index
] && !es
->logAttr
[index
].fSoftBreak
)
313 ret
= es
->logAttr
[index
].fWhiteSpace
;
316 ERR("unknown action code, please report !\n");
323 /*********************************************************************
325 * EDIT_CallWordBreakProc
327 * Call appropriate WordBreakProc (internal or external).
329 * Note: The "start" argument should always be an index referring
330 * to es->text. The actual wordbreak proc might be
331 * 16 bit, so we can't always pass any 32 bit LPSTR.
332 * Hence we assume that es->text is the buffer that holds
333 * the string under examination (we can decide this for ourselves).
336 static INT
EDIT_CallWordBreakProc(EDITSTATE
*es
, INT start
, INT index
, INT count
, INT action
)
340 if (es
->word_break_proc
)
344 EDITWORDBREAKPROCW wbpW
= (EDITWORDBREAKPROCW
)es
->word_break_proc
;
346 TRACE_(relay
)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
347 es
->word_break_proc
, debugstr_wn(es
->text
+ start
, count
), index
, count
, action
);
348 ret
= wbpW(es
->text
+ start
, index
, count
, action
);
352 EDITWORDBREAKPROCA wbpA
= (EDITWORDBREAKPROCA
)es
->word_break_proc
;
356 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, NULL
, 0, NULL
, NULL
);
357 textA
= HeapAlloc(GetProcessHeap(), 0, countA
);
358 WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, textA
, countA
, NULL
, NULL
);
359 TRACE_(relay
)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
360 es
->word_break_proc
, debugstr_an(textA
, countA
), index
, countA
, action
);
361 ret
= wbpA(textA
, index
, countA
, action
);
362 HeapFree(GetProcessHeap(), 0, textA
);
366 ret
= EDIT_WordBreakProc(es
, es
->text
, index
+start
, count
+start
, action
) - start
;
371 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF
*line_def
)
375 ScriptStringFree(&line_def
->ssa
);
376 line_def
->ssa
= NULL
;
380 static inline void EDIT_InvalidateUniscribeData(EDITSTATE
*es
)
382 LINEDEF
*line_def
= es
->first_line_def
;
385 EDIT_InvalidateUniscribeData_linedef(line_def
);
386 line_def
= line_def
->next
;
390 ScriptStringFree(&es
->ssa
);
395 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData_linedef(EDITSTATE
*es
, HDC dc
, LINEDEF
*line_def
)
400 if (line_def
->net_length
&& !line_def
->ssa
)
402 int index
= line_def
->index
;
403 HFONT old_font
= NULL
;
405 SCRIPT_TABDEF tabdef
;
409 udc
= GetDC(es
->hwndSelf
);
411 old_font
= SelectObject(udc
, es
->font
);
413 tabdef
.cTabStops
= es
->tabs_count
;
415 tabdef
.pTabStops
= es
->tabs
;
416 tabdef
.iTabOrigin
= 0;
418 hr
= ScriptStringAnalyse(udc
, &es
->text
[index
], line_def
->net_length
,
419 (1.5*line_def
->net_length
+16), -1,
420 SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
|SSA_TAB
, -1,
421 NULL
, NULL
, NULL
, &tabdef
, NULL
, &line_def
->ssa
);
424 WARN("ScriptStringAnalyse failed (%x)\n",hr
);
425 line_def
->ssa
= NULL
;
429 SelectObject(udc
, old_font
);
431 ReleaseDC(es
->hwndSelf
, udc
);
434 return line_def
->ssa
;
437 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData(EDITSTATE
*es
, HDC dc
, INT line
)
441 if (!(es
->style
& ES_MULTILINE
))
445 INT length
= get_text_length(es
);
446 HFONT old_font
= NULL
;
450 udc
= GetDC(es
->hwndSelf
);
452 old_font
= SelectObject(udc
, es
->font
);
454 if (es
->style
& ES_PASSWORD
)
455 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
);
457 ScriptStringAnalyse(udc
, es
->text
, length
, (1.5*length
+16), -1, SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
, -1, NULL
, NULL
, NULL
, NULL
, NULL
, &es
->ssa
);
460 SelectObject(udc
, old_font
);
462 ReleaseDC(es
->hwndSelf
, udc
);
468 line_def
= es
->first_line_def
;
469 while (line_def
&& line
)
471 line_def
= line_def
->next
;
475 return EDIT_UpdateUniscribeData_linedef(es
,dc
,line_def
);
479 static inline INT
get_vertical_line_count(EDITSTATE
*es
)
481 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
485 /*********************************************************************
487 * EDIT_BuildLineDefs_ML
489 * Build linked list of text lines.
490 * Lines can end with '\0' (last line), a character (if it is wrapped),
491 * a soft return '\r\r\n' or a hard return '\r\n'
494 static void EDIT_BuildLineDefs_ML(EDITSTATE
*es
, INT istart
, INT iend
, INT delta
, HRGN hrgn
)
496 LPWSTR current_position
, cp
;
498 LINEDEF
*current_line
;
499 LINEDEF
*previous_line
;
501 INT line_index
= 0, nstart_line
, nstart_index
;
502 INT line_count
= es
->line_count
;
507 if (istart
== iend
&& delta
== 0)
510 previous_line
= NULL
;
511 current_line
= es
->first_line_def
;
513 /* Find starting line. istart must lie inside an existing line or
514 * at the end of buffer */
516 if (istart
< current_line
->index
+ current_line
->length
||
517 current_line
->ending
== END_0
)
520 previous_line
= current_line
;
521 current_line
= current_line
->next
;
523 } while (current_line
);
525 if (!current_line
) /* Error occurred start is not inside previous buffer */
527 FIXME(" modification occurred outside buffer\n");
531 /* Remember start of modifications in order to calculate update region */
532 nstart_line
= line_index
;
533 nstart_index
= current_line
->index
;
535 /* We must start to reformat from the previous line since the modifications
536 * may have caused the line to wrap upwards. */
537 if (!(es
->style
& ES_AUTOHSCROLL
) && line_index
> 0)
540 current_line
= previous_line
;
542 start_line
= current_line
;
544 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
545 current_position
= es
->text
+ current_line
->index
;
546 vlc
= get_vertical_line_count(es
);
548 if (current_line
!= start_line
)
550 if (!current_line
|| current_line
->index
+ delta
> current_position
- es
->text
)
552 /* The buffer has been expanded, create a new line and
553 insert it into the link list */
554 LINEDEF
*new_line
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
));
555 new_line
->next
= previous_line
->next
;
556 previous_line
->next
= new_line
;
557 current_line
= new_line
;
560 else if (current_line
->index
+ delta
< current_position
- es
->text
)
562 /* The previous line merged with this line so we delete this extra entry */
563 previous_line
->next
= current_line
->next
;
564 HeapFree(GetProcessHeap(), 0, current_line
);
565 current_line
= previous_line
->next
;
569 else /* current_line->index + delta == current_position */
571 if (current_position
- es
->text
> iend
)
572 break; /* We reached end of line modifications */
573 /* else recalculate this line */
577 current_line
->index
= current_position
- es
->text
;
578 orig_net_length
= current_line
->net_length
;
580 /* Find end of line */
581 cp
= current_position
;
583 if (*cp
== '\n') break;
584 if ((*cp
== '\r') && (*(cp
+ 1) == '\n'))
589 /* Mark type of line termination */
591 current_line
->ending
= END_0
;
592 current_line
->net_length
= strlenW(current_position
);
593 } else if ((cp
> current_position
) && (*(cp
- 1) == '\r')) {
594 current_line
->ending
= END_SOFT
;
595 current_line
->net_length
= cp
- current_position
- 1;
596 } else if (*cp
== '\n') {
597 current_line
->ending
= END_RICH
;
598 current_line
->net_length
= cp
- current_position
;
600 current_line
->ending
= END_HARD
;
601 current_line
->net_length
= cp
- current_position
;
604 if (current_line
->net_length
)
607 EDIT_InvalidateUniscribeData_linedef(current_line
);
608 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
609 if (current_line
->ssa
)
611 sz
= ScriptString_pSize(current_line
->ssa
);
612 /* Calculate line width */
613 current_line
->width
= sz
->cx
;
615 else current_line
->width
= es
->char_width
* current_line
->net_length
;
617 else current_line
->width
= 0;
619 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
621 /* Line breaks just look back from the end and find the next break and try that. */
623 if (!(es
->style
& ES_AUTOHSCROLL
)) {
624 if (current_line
->width
> fw
&& fw
> es
->char_width
) {
631 prev
= current_line
->net_length
- 1;
632 w
= current_line
->net_length
;
633 d
= (float)current_line
->width
/(float)fw
;
634 if (d
> 1.2f
) d
-= 0.2f
;
636 if (next
>= prev
) next
= prev
-1;
638 prev
= EDIT_CallWordBreakProc(es
, current_position
- es
->text
,
639 next
, current_line
->net_length
, WB_LEFT
);
640 current_line
->net_length
= prev
;
641 EDIT_InvalidateUniscribeData_linedef(current_line
);
642 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
643 if (current_line
->ssa
)
644 sz
= ScriptString_pSize(current_line
->ssa
);
647 current_line
->width
= sz
->cx
;
651 } while (prev
&& current_line
->width
> fw
);
652 current_line
->net_length
= w
;
654 if (prev
== 0) { /* Didn't find a line break so force a break */
658 EDIT_InvalidateUniscribeData_linedef(current_line
);
659 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
661 if (current_line
->ssa
)
663 count
= ScriptString_pcOutChars(current_line
->ssa
);
664 piDx
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * (*count
));
665 ScriptStringGetLogicalWidths(current_line
->ssa
,piDx
);
667 prev
= current_line
->net_length
-1;
669 current_line
->width
-= piDx
[prev
];
671 } while ( prev
> 0 && current_line
->width
> fw
);
674 HeapFree(GetProcessHeap(),0,piDx
);
677 prev
= (fw
/ es
->char_width
);
680 /* If the first line we are calculating, wrapped before istart, we must
681 * adjust istart in order for this to be reflected in the update region. */
682 if (current_line
->index
== nstart_index
&& istart
> current_line
->index
+ prev
)
683 istart
= current_line
->index
+ prev
;
684 /* else if we are updating the previous line before the first line we
685 * are re-calculating and it expanded */
686 else if (current_line
== start_line
&&
687 current_line
->index
!= nstart_index
&& orig_net_length
< prev
)
689 /* Line expanded due to an upwards line wrap so we must partially include
690 * previous line in update region */
691 nstart_line
= line_index
;
692 nstart_index
= current_line
->index
;
693 istart
= current_line
->index
+ orig_net_length
;
696 current_line
->net_length
= prev
;
697 current_line
->ending
= END_WRAP
;
699 if (current_line
->net_length
> 0)
701 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
702 if (current_line
->ssa
)
704 sz
= ScriptString_pSize(current_line
->ssa
);
705 current_line
->width
= sz
->cx
;
708 current_line
->width
= 0;
710 else current_line
->width
= 0;
712 else if (current_line
== start_line
&&
713 current_line
->index
!= nstart_index
&&
714 orig_net_length
< current_line
->net_length
) {
715 /* The previous line expanded but it's still not as wide as the client rect */
716 /* The expansion is due to an upwards line wrap so we must partially include
717 it in the update region */
718 nstart_line
= line_index
;
719 nstart_index
= current_line
->index
;
720 istart
= current_line
->index
+ orig_net_length
;
725 /* Adjust length to include line termination */
726 switch (current_line
->ending
) {
728 current_line
->length
= current_line
->net_length
+ 3;
731 current_line
->length
= current_line
->net_length
+ 1;
734 current_line
->length
= current_line
->net_length
+ 2;
738 current_line
->length
= current_line
->net_length
;
741 es
->text_width
= max(es
->text_width
, current_line
->width
);
742 current_position
+= current_line
->length
;
743 previous_line
= current_line
;
745 /* Discard data for non-visible lines. It will be calculated as needed */
746 if ((line_index
< es
->y_offset
) || (line_index
> es
->y_offset
+ vlc
))
747 EDIT_InvalidateUniscribeData_linedef(current_line
);
749 current_line
= current_line
->next
;
751 } while (previous_line
->ending
!= END_0
);
753 /* Finish adjusting line indexes by delta or remove hanging lines */
754 if (previous_line
->ending
== END_0
)
756 LINEDEF
*pnext
= NULL
;
758 previous_line
->next
= NULL
;
761 pnext
= current_line
->next
;
762 EDIT_InvalidateUniscribeData_linedef(current_line
);
763 HeapFree(GetProcessHeap(), 0, current_line
);
764 current_line
= pnext
;
772 current_line
->index
+= delta
;
773 current_line
= current_line
->next
;
777 /* Calculate rest of modification rectangle */
782 * We calculate two rectangles. One for the first line which may have
783 * an indent with respect to the format rect. The other is a format-width
784 * rectangle that spans the rest of the lines that changed or moved.
786 rc
.top
= es
->format_rect
.top
+ nstart_line
* es
->line_height
-
787 (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
788 rc
.bottom
= rc
.top
+ es
->line_height
;
789 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
))
790 rc
.left
= es
->format_rect
.left
;
792 rc
.left
= LOWORD(EDIT_EM_PosFromChar(es
, nstart_index
, FALSE
));
793 rc
.right
= es
->format_rect
.right
;
794 SetRectRgn(hrgn
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
797 rc
.left
= es
->format_rect
.left
;
798 rc
.right
= es
->format_rect
.right
;
800 * If lines were added or removed we must re-paint the remainder of the
801 * lines since the remaining lines were either shifted up or down.
803 if (line_count
< es
->line_count
) /* We added lines */
804 rc
.bottom
= es
->line_count
* es
->line_height
;
805 else if (line_count
> es
->line_count
) /* We removed lines */
806 rc
.bottom
= line_count
* es
->line_height
;
808 rc
.bottom
= line_index
* es
->line_height
;
809 rc
.bottom
+= es
->format_rect
.top
;
810 rc
.bottom
-= (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
811 tmphrgn
= CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
812 CombineRgn(hrgn
, hrgn
, tmphrgn
, RGN_OR
);
813 DeleteObject(tmphrgn
);
817 /*********************************************************************
819 * EDIT_CalcLineWidth_SL
822 static void EDIT_CalcLineWidth_SL(EDITSTATE
*es
)
824 EDIT_UpdateUniscribeData(es
, NULL
, 0);
828 size
= ScriptString_pSize(es
->ssa
);
829 es
->text_width
= size
->cx
;
835 /*********************************************************************
839 * Beware: This is not the function called on EM_CHARFROMPOS
840 * The position _can_ be outside the formatting / client
842 * The return value is only the character index
845 static INT
EDIT_CharFromPos(EDITSTATE
*es
, INT x
, INT y
, LPBOOL after_wrap
)
849 if (es
->style
& ES_MULTILINE
) {
851 INT line
= (y
- es
->format_rect
.top
) / es
->line_height
+ es
->y_offset
;
853 LINEDEF
*line_def
= es
->first_line_def
;
854 EDIT_UpdateUniscribeData(es
, NULL
, line
);
855 while ((line
> 0) && line_def
->next
) {
856 line_index
+= line_def
->length
;
857 line_def
= line_def
->next
;
861 x
+= es
->x_offset
- es
->format_rect
.left
;
862 if (es
->style
& ES_RIGHT
)
863 x
-= (es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
;
864 else if (es
->style
& ES_CENTER
)
865 x
-= ((es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
) / 2;
866 if (x
>= line_def
->width
) {
868 *after_wrap
= (line_def
->ending
== END_WRAP
);
869 return line_index
+ line_def
->net_length
;
871 if (x
<= 0 || !line_def
->ssa
) {
877 ScriptStringXtoCP(line_def
->ssa
, x
, &index
, &trailing
);
878 if (trailing
) index
++;
881 *after_wrap
= ((index
== line_index
+ line_def
->net_length
) &&
882 (line_def
->ending
== END_WRAP
));
888 x
-= es
->format_rect
.left
;
894 INT indent
= (es
->format_rect
.right
- es
->format_rect
.left
) - es
->text_width
;
895 if (es
->style
& ES_RIGHT
)
897 else if (es
->style
& ES_CENTER
)
901 EDIT_UpdateUniscribeData(es
, NULL
, 0);
906 if (es
->x_offset
>= get_text_length(es
))
909 size
= ScriptString_pSize(es
->ssa
);
912 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
919 if (x
+ xoff
> 0 || !es
->ssa
)
921 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
922 if (trailing
) index
++;
931 const SIZE
*size
= NULL
;
933 size
= ScriptString_pSize(es
->ssa
);
936 else if (x
> size
->cx
)
937 index
= get_text_length(es
);
940 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
941 if (trailing
) index
++;
947 index
= es
->x_offset
;
954 /*********************************************************************
958 * adjusts the point to be within the formatting rectangle
959 * (so CharFromPos returns the nearest _visible_ character)
962 static void EDIT_ConfinePoint(const EDITSTATE
*es
, LPINT x
, LPINT y
)
964 *x
= min(max(*x
, es
->format_rect
.left
), es
->format_rect
.right
- 1);
965 *y
= min(max(*y
, es
->format_rect
.top
), es
->format_rect
.bottom
- 1);
969 /*********************************************************************
974 static INT
EDIT_EM_LineFromChar(EDITSTATE
*es
, INT index
)
979 if (!(es
->style
& ES_MULTILINE
))
981 if (index
> (INT
)get_text_length(es
))
982 return es
->line_count
- 1;
984 index
= min(es
->selection_start
, es
->selection_end
);
987 line_def
= es
->first_line_def
;
988 index
-= line_def
->length
;
989 while ((index
>= 0) && line_def
->next
) {
991 line_def
= line_def
->next
;
992 index
-= line_def
->length
;
998 /*********************************************************************
1003 static INT
EDIT_EM_LineIndex(const EDITSTATE
*es
, INT line
)
1006 const LINEDEF
*line_def
;
1008 if (!(es
->style
& ES_MULTILINE
))
1010 if (line
>= es
->line_count
)
1014 line_def
= es
->first_line_def
;
1016 INT index
= es
->selection_end
- line_def
->length
;
1017 while ((index
>= 0) && line_def
->next
) {
1018 line_index
+= line_def
->length
;
1019 line_def
= line_def
->next
;
1020 index
-= line_def
->length
;
1024 line_index
+= line_def
->length
;
1025 line_def
= line_def
->next
;
1033 /*********************************************************************
1038 static INT
EDIT_EM_LineLength(EDITSTATE
*es
, INT index
)
1042 if (!(es
->style
& ES_MULTILINE
))
1043 return get_text_length(es
);
1046 /* get the number of remaining non-selected chars of selected lines */
1047 INT32 l
; /* line number */
1048 INT32 li
; /* index of first char in line */
1050 l
= EDIT_EM_LineFromChar(es
, es
->selection_start
);
1051 /* # chars before start of selection area */
1052 count
= es
->selection_start
- EDIT_EM_LineIndex(es
, l
);
1053 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1054 /* # chars after end of selection */
1055 li
= EDIT_EM_LineIndex(es
, l
);
1056 count
+= li
+ EDIT_EM_LineLength(es
, li
) - es
->selection_end
;
1059 line_def
= es
->first_line_def
;
1060 index
-= line_def
->length
;
1061 while ((index
>= 0) && line_def
->next
) {
1062 line_def
= line_def
->next
;
1063 index
-= line_def
->length
;
1065 return line_def
->net_length
;
1069 /*********************************************************************
1074 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
)
1076 INT len
= get_text_length(es
);
1085 index
= min(index
, len
);
1086 if (es
->style
& ES_MULTILINE
) {
1087 l
= EDIT_EM_LineFromChar(es
, index
);
1088 EDIT_UpdateUniscribeData(es
, NULL
, l
);
1090 y
= (l
- es
->y_offset
) * es
->line_height
;
1091 li
= EDIT_EM_LineIndex(es
, l
);
1092 if (after_wrap
&& (li
== index
) && l
) {
1094 line_def
= es
->first_line_def
;
1096 line_def
= line_def
->next
;
1099 if (line_def
->ending
== END_WRAP
) {
1101 y
-= es
->line_height
;
1102 li
= EDIT_EM_LineIndex(es
, l
);
1106 line_def
= es
->first_line_def
;
1107 while (line_def
->index
!= li
)
1108 line_def
= line_def
->next
;
1110 lw
= line_def
->width
;
1111 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1114 ScriptStringCPtoX(line_def
->ssa
, (index
- 1) - li
, TRUE
, &x
);
1120 if (es
->style
& ES_RIGHT
)
1122 else if (es
->style
& ES_CENTER
)
1127 EDIT_UpdateUniscribeData(es
, NULL
, 0);
1132 if (es
->x_offset
>= get_text_length(es
))
1134 int leftover
= es
->x_offset
- get_text_length(es
);
1138 size
= ScriptString_pSize(es
->ssa
);
1143 xoff
+= es
->char_width
* leftover
;
1146 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
1153 if (index
>= get_text_length(es
))
1158 size
= ScriptString_pSize(es
->ssa
);
1165 ScriptStringCPtoX(es
->ssa
, index
, FALSE
, &xi
);
1171 if (index
>= es
->x_offset
) {
1172 if (!es
->x_offset
&& (es
->style
& (ES_RIGHT
| ES_CENTER
)))
1174 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1175 if (w
> es
->text_width
)
1177 if (es
->style
& ES_RIGHT
)
1178 x
+= w
- es
->text_width
;
1179 else if (es
->style
& ES_CENTER
)
1180 x
+= (w
- es
->text_width
) / 2;
1186 x
+= es
->format_rect
.left
;
1187 y
+= es
->format_rect
.top
;
1188 return MAKELONG((INT16
)x
, (INT16
)y
);
1192 /*********************************************************************
1196 * Calculates the bounding rectangle for a line from a starting
1197 * column to an ending column.
1200 static void EDIT_GetLineRect(EDITSTATE
*es
, INT line
, INT scol
, INT ecol
, LPRECT rc
)
1202 SCRIPT_STRING_ANALYSIS ssa
;
1206 if (es
->style
& ES_MULTILINE
)
1208 const LINEDEF
*line_def
= NULL
;
1209 rc
->top
= es
->format_rect
.top
+ (line
- es
->y_offset
) * es
->line_height
;
1210 if (line
>= es
->line_count
)
1213 line_def
= es
->first_line_def
;
1215 INT index
= es
->selection_end
- line_def
->length
;
1216 while ((index
>= 0) && line_def
->next
) {
1217 line_index
+= line_def
->length
;
1218 line_def
= line_def
->next
;
1219 index
-= line_def
->length
;
1223 line_index
+= line_def
->length
;
1224 line_def
= line_def
->next
;
1228 ssa
= line_def
->ssa
;
1233 rc
->top
= es
->format_rect
.top
;
1237 rc
->bottom
= rc
->top
+ es
->line_height
;
1238 pt1
= (scol
== 0) ? es
->format_rect
.left
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ scol
, TRUE
));
1239 pt2
= (ecol
== -1) ? es
->format_rect
.right
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ ecol
, TRUE
));
1242 ScriptStringCPtoX(ssa
, scol
, FALSE
, &pt3
);
1243 pt3
+=es
->format_rect
.left
;
1246 rc
->right
= max(max(pt1
, pt2
),pt3
);
1247 rc
->left
= min(min(pt1
, pt2
),pt3
);
1251 static inline void text_buffer_changed(EDITSTATE
*es
)
1253 es
->text_length
= (UINT
)-1;
1255 HeapFree( GetProcessHeap(), 0, es
->logAttr
);
1257 EDIT_InvalidateUniscribeData(es
);
1260 /*********************************************************************
1264 static void EDIT_LockBuffer(EDITSTATE
*es
)
1266 if (es
->hlocapp
) return;
1270 if(!es
->hloc32W
) return;
1274 CHAR
*textA
= LocalLock(es
->hloc32A
);
1276 UINT countW_new
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
1277 if(countW_new
> es
->buffer_size
+ 1)
1279 UINT alloc_size
= ROUND_TO_GROW(countW_new
* sizeof(WCHAR
));
1280 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es
->buffer_size
, countW_new
);
1281 hloc32W_new
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1284 es
->hloc32W
= hloc32W_new
;
1285 es
->buffer_size
= LocalSize(hloc32W_new
)/sizeof(WCHAR
) - 1;
1286 TRACE("Real new size %d+1 WCHARs\n", es
->buffer_size
);
1289 WARN("FAILED! Will synchronize partially\n");
1291 es
->text
= LocalLock(es
->hloc32W
);
1292 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, es
->text
, es
->buffer_size
+ 1);
1293 LocalUnlock(es
->hloc32A
);
1295 else es
->text
= LocalLock(es
->hloc32W
);
1301 /*********************************************************************
1306 static void EDIT_UnlockBuffer(EDITSTATE
*es
, BOOL force
)
1308 if (es
->hlocapp
) return;
1310 /* Edit window might be already destroyed */
1311 if(!IsWindow(es
->hwndSelf
))
1313 WARN("edit hwnd %p already destroyed\n", es
->hwndSelf
);
1317 if (!es
->lock_count
) {
1318 ERR("lock_count == 0 ... please report\n");
1322 ERR("es->text == 0 ... please report\n");
1326 if (force
|| (es
->lock_count
== 1)) {
1329 UINT countW
= get_text_length(es
) + 1;
1333 UINT countA_new
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, NULL
, 0, NULL
, NULL
);
1334 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1335 TRACE("%d WCHARs translated to %d bytes\n", countW
, countA_new
);
1336 countA
= LocalSize(es
->hloc32A
);
1337 if(countA_new
> countA
)
1340 UINT alloc_size
= ROUND_TO_GROW(countA_new
);
1341 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA
, alloc_size
);
1342 hloc32A_new
= LocalReAlloc(es
->hloc32A
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1345 es
->hloc32A
= hloc32A_new
;
1346 countA
= LocalSize(hloc32A_new
);
1347 TRACE("Real new size %d bytes\n", countA
);
1350 WARN("FAILED! Will synchronize partially\n");
1352 WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
,
1353 LocalLock(es
->hloc32A
), countA
, NULL
, NULL
);
1354 LocalUnlock(es
->hloc32A
);
1357 LocalUnlock(es
->hloc32W
);
1361 ERR("no buffer ... please report\n");
1369 /*********************************************************************
1373 * Try to fit size + 1 characters in the buffer.
1375 static BOOL
EDIT_MakeFit(EDITSTATE
*es
, UINT size
)
1379 if (size
<= es
->buffer_size
)
1382 TRACE("trying to ReAlloc to %d+1 characters\n", size
);
1384 /* Force edit to unlock its buffer. es->text now NULL */
1385 EDIT_UnlockBuffer(es
, TRUE
);
1388 UINT alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1389 if ((hNew32W
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
))) {
1390 TRACE("Old 32 bit handle %p, new handle %p\n", es
->hloc32W
, hNew32W
);
1391 es
->hloc32W
= hNew32W
;
1392 es
->buffer_size
= LocalSize(hNew32W
)/sizeof(WCHAR
) - 1;
1396 EDIT_LockBuffer(es
);
1398 if (es
->buffer_size
< size
) {
1399 WARN("FAILED ! We now have %d+1\n", es
->buffer_size
);
1400 EDIT_NOTIFY_PARENT(es
, EN_ERRSPACE
);
1403 TRACE("We now have %d+1\n", es
->buffer_size
);
1409 /*********************************************************************
1413 * Try to fit size + 1 bytes in the undo buffer.
1416 static BOOL
EDIT_MakeUndoFit(EDITSTATE
*es
, UINT size
)
1420 if (size
<= es
->undo_buffer_size
)
1423 TRACE("trying to ReAlloc to %d+1\n", size
);
1425 alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1426 if ((es
->undo_text
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, es
->undo_text
, alloc_size
))) {
1427 es
->undo_buffer_size
= alloc_size
/sizeof(WCHAR
) - 1;
1432 WARN("FAILED ! We now have %d+1\n", es
->undo_buffer_size
);
1438 /*********************************************************************
1440 * EDIT_UpdateTextRegion
1443 static void EDIT_UpdateTextRegion(EDITSTATE
*es
, HRGN hrgn
, BOOL bErase
)
1445 if (es
->flags
& EF_UPDATE
) {
1446 es
->flags
&= ~EF_UPDATE
;
1447 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1449 InvalidateRgn(es
->hwndSelf
, hrgn
, bErase
);
1453 /*********************************************************************
1458 static void EDIT_UpdateText(EDITSTATE
*es
, const RECT
*rc
, BOOL bErase
)
1460 if (es
->flags
& EF_UPDATE
) {
1461 es
->flags
&= ~EF_UPDATE
;
1462 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1464 InvalidateRect(es
->hwndSelf
, rc
, bErase
);
1467 /*********************************************************************
1469 * EDIT_SL_InvalidateText
1471 * Called from EDIT_InvalidateText().
1472 * Does the job for single-line controls only.
1475 static void EDIT_SL_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1480 EDIT_GetLineRect(es
, 0, start
, end
, &line_rect
);
1481 if (IntersectRect(&rc
, &line_rect
, &es
->format_rect
))
1482 EDIT_UpdateText(es
, &rc
, TRUE
);
1485 /*********************************************************************
1487 * EDIT_ML_InvalidateText
1489 * Called from EDIT_InvalidateText().
1490 * Does the job for multi-line controls only.
1493 static void EDIT_ML_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1495 INT vlc
= get_vertical_line_count(es
);
1496 INT sl
= EDIT_EM_LineFromChar(es
, start
);
1497 INT el
= EDIT_EM_LineFromChar(es
, end
);
1506 if ((el
< es
->y_offset
) || (sl
> es
->y_offset
+ vlc
))
1509 sc
= start
- EDIT_EM_LineIndex(es
, sl
);
1510 ec
= end
- EDIT_EM_LineIndex(es
, el
);
1511 if (sl
< es
->y_offset
) {
1515 if (el
> es
->y_offset
+ vlc
) {
1516 el
= es
->y_offset
+ vlc
;
1517 ec
= EDIT_EM_LineLength(es
, EDIT_EM_LineIndex(es
, el
));
1519 GetClientRect(es
->hwndSelf
, &rc1
);
1520 IntersectRect(&rcWnd
, &rc1
, &es
->format_rect
);
1522 EDIT_GetLineRect(es
, sl
, sc
, ec
, &rcLine
);
1523 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1524 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1526 EDIT_GetLineRect(es
, sl
, sc
,
1527 EDIT_EM_LineLength(es
,
1528 EDIT_EM_LineIndex(es
, sl
)),
1530 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1531 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1532 for (l
= sl
+ 1 ; l
< el
; l
++) {
1533 EDIT_GetLineRect(es
, l
, 0,
1534 EDIT_EM_LineLength(es
,
1535 EDIT_EM_LineIndex(es
, l
)),
1537 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1538 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1540 EDIT_GetLineRect(es
, el
, 0, ec
, &rcLine
);
1541 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1542 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1547 /*********************************************************************
1549 * EDIT_InvalidateText
1551 * Invalidate the text from offset start up to, but not including,
1552 * offset end. Useful for (re)painting the selection.
1553 * Regions outside the linewidth are not invalidated.
1554 * end == -1 means end == TextLength.
1555 * start and end need not be ordered.
1558 static void EDIT_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1564 end
= get_text_length(es
);
1572 if (es
->style
& ES_MULTILINE
)
1573 EDIT_ML_InvalidateText(es
, start
, end
);
1575 EDIT_SL_InvalidateText(es
, start
, end
);
1579 /*********************************************************************
1583 * note: unlike the specs say: the order of start and end
1584 * _is_ preserved in Windows. (i.e. start can be > end)
1585 * In other words: this handler is OK
1588 static void EDIT_EM_SetSel(EDITSTATE
*es
, UINT start
, UINT end
, BOOL after_wrap
)
1590 UINT old_start
= es
->selection_start
;
1591 UINT old_end
= es
->selection_end
;
1592 UINT len
= get_text_length(es
);
1594 if (start
== (UINT
)-1) {
1595 start
= es
->selection_end
;
1596 end
= es
->selection_end
;
1598 start
= min(start
, len
);
1599 end
= min(end
, len
);
1601 es
->selection_start
= start
;
1602 es
->selection_end
= end
;
1604 es
->flags
|= EF_AFTER_WRAP
;
1606 es
->flags
&= ~EF_AFTER_WRAP
;
1607 /* Compute the necessary invalidation region. */
1608 /* Note that we don't need to invalidate regions which have
1609 * "never" been selected, or those which are "still" selected.
1610 * In fact, every time we hit a selection boundary, we can
1611 * *toggle* whether we need to invalidate. Thus we can optimize by
1612 * *sorting* the interval endpoints. Let's assume that we sort them
1614 * start <= end <= old_start <= old_end
1615 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1616 * in 5 comparisons; i.e. it is impossible to do better than the
1618 ORDER_UINT(end
, old_end
);
1619 ORDER_UINT(start
, old_start
);
1620 ORDER_UINT(old_start
, old_end
);
1621 ORDER_UINT(start
, end
);
1622 /* Note that at this point 'end' and 'old_start' are not in order, but
1623 * start is definitely the min. and old_end is definitely the max. */
1624 if (end
!= old_start
)
1628 * ORDER_UINT32(end, old_start);
1629 * EDIT_InvalidateText(es, start, end);
1630 * EDIT_InvalidateText(es, old_start, old_end);
1631 * in place of the following if statement.
1632 * (That would complete the optimal five-comparison four-element sort.)
1634 if (old_start
> end
)
1636 EDIT_InvalidateText(es
, start
, end
);
1637 EDIT_InvalidateText(es
, old_start
, old_end
);
1641 EDIT_InvalidateText(es
, start
, old_start
);
1642 EDIT_InvalidateText(es
, end
, old_end
);
1645 else EDIT_InvalidateText(es
, start
, old_end
);
1649 /*********************************************************************
1651 * EDIT_UpdateScrollInfo
1654 static void EDIT_UpdateScrollInfo(EDITSTATE
*es
)
1656 if ((es
->style
& WS_VSCROLL
) && !(es
->flags
& EF_VSCROLL_TRACK
))
1659 si
.cbSize
= sizeof(SCROLLINFO
);
1660 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1662 si
.nMax
= es
->line_count
- 1;
1663 si
.nPage
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1664 si
.nPos
= es
->y_offset
;
1665 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1666 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1667 SetScrollInfo(es
->hwndSelf
, SB_VERT
, &si
, TRUE
);
1670 if ((es
->style
& WS_HSCROLL
) && !(es
->flags
& EF_HSCROLL_TRACK
))
1673 si
.cbSize
= sizeof(SCROLLINFO
);
1674 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1676 si
.nMax
= es
->text_width
- 1;
1677 si
.nPage
= es
->format_rect
.right
- es
->format_rect
.left
;
1678 si
.nPos
= es
->x_offset
;
1679 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1680 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1681 SetScrollInfo(es
->hwndSelf
, SB_HORZ
, &si
, TRUE
);
1686 /*********************************************************************
1688 * EDIT_EM_LineScroll_internal
1690 * Version of EDIT_EM_LineScroll for internal use.
1691 * It doesn't refuse if ES_MULTILINE is set and assumes that
1692 * dx is in pixels, dy - in lines.
1695 static BOOL
EDIT_EM_LineScroll_internal(EDITSTATE
*es
, INT dx
, INT dy
)
1698 INT x_offset_in_pixels
;
1699 INT lines_per_page
= (es
->format_rect
.bottom
- es
->format_rect
.top
) /
1702 if (es
->style
& ES_MULTILINE
)
1704 x_offset_in_pixels
= es
->x_offset
;
1709 x_offset_in_pixels
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->x_offset
, FALSE
));
1712 if (-dx
> x_offset_in_pixels
)
1713 dx
= -x_offset_in_pixels
;
1714 if (dx
> es
->text_width
- x_offset_in_pixels
)
1715 dx
= es
->text_width
- x_offset_in_pixels
;
1716 nyoff
= max(0, es
->y_offset
+ dy
);
1717 if (nyoff
>= es
->line_count
- lines_per_page
)
1718 nyoff
= max(0, es
->line_count
- lines_per_page
);
1719 dy
= (es
->y_offset
- nyoff
) * es
->line_height
;
1724 es
->y_offset
= nyoff
;
1725 if(es
->style
& ES_MULTILINE
)
1728 es
->x_offset
+= dx
/ es
->char_width
;
1730 GetClientRect(es
->hwndSelf
, &rc1
);
1731 IntersectRect(&rc
, &rc1
, &es
->format_rect
);
1732 ScrollWindowEx(es
->hwndSelf
, -dx
, dy
,
1733 NULL
, &rc
, NULL
, NULL
, SW_INVALIDATE
);
1734 /* force scroll info update */
1735 EDIT_UpdateScrollInfo(es
);
1737 if (dx
&& !(es
->flags
& EF_HSCROLL_TRACK
))
1738 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
1739 if (dy
&& !(es
->flags
& EF_VSCROLL_TRACK
))
1740 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
1744 /*********************************************************************
1748 * NOTE: dx is in average character widths, dy - in lines;
1751 static BOOL
EDIT_EM_LineScroll(EDITSTATE
*es
, INT dx
, INT dy
)
1753 if (!(es
->style
& ES_MULTILINE
))
1756 dx
*= es
->char_width
;
1757 return EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1761 /*********************************************************************
1766 static LRESULT
EDIT_EM_Scroll(EDITSTATE
*es
, INT action
)
1770 if (!(es
->style
& ES_MULTILINE
))
1771 return (LRESULT
)FALSE
;
1781 if (es
->y_offset
< es
->line_count
- 1)
1786 dy
= -(es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1789 if (es
->y_offset
< es
->line_count
- 1)
1790 dy
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1793 return (LRESULT
)FALSE
;
1796 INT vlc
= get_vertical_line_count(es
);
1797 /* check if we are going to move too far */
1798 if(es
->y_offset
+ dy
> es
->line_count
- vlc
)
1799 dy
= max(es
->line_count
- vlc
, 0) - es
->y_offset
;
1801 /* Notification is done in EDIT_EM_LineScroll */
1803 EDIT_EM_LineScroll(es
, 0, dy
);
1804 return MAKELONG(dy
, TRUE
);
1808 return (LRESULT
)FALSE
;
1812 /*********************************************************************
1817 static void EDIT_SetCaretPos(EDITSTATE
*es
, INT pos
,
1820 LRESULT res
= EDIT_EM_PosFromChar(es
, pos
, after_wrap
);
1821 TRACE("%d - %dx%d\n", pos
, (short)LOWORD(res
), (short)HIWORD(res
));
1822 SetCaretPos((short)LOWORD(res
), (short)HIWORD(res
));
1826 /*********************************************************************
1831 static void EDIT_EM_ScrollCaret(EDITSTATE
*es
)
1833 if (es
->style
& ES_MULTILINE
) {
1837 INT cw
= es
->char_width
;
1842 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1843 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
));
1844 vlc
= get_vertical_line_count(es
);
1845 if (l
>= es
->y_offset
+ vlc
)
1846 dy
= l
- vlc
+ 1 - es
->y_offset
;
1847 if (l
< es
->y_offset
)
1848 dy
= l
- es
->y_offset
;
1849 ww
= es
->format_rect
.right
- es
->format_rect
.left
;
1850 if (x
< es
->format_rect
.left
)
1851 dx
= x
- es
->format_rect
.left
- ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1852 if (x
> es
->format_rect
.right
)
1853 dx
= x
- es
->format_rect
.left
- (HSCROLL_FRACTION
- 1) * ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1854 if (dy
|| dx
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1856 /* check if we are going to move too far */
1857 if(es
->x_offset
+ dx
+ ww
> es
->text_width
)
1858 dx
= es
->text_width
- ww
- es
->x_offset
;
1859 if(dx
|| dy
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1860 EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1867 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1868 format_width
= es
->format_rect
.right
- es
->format_rect
.left
;
1869 if (x
< es
->format_rect
.left
) {
1870 goal
= es
->format_rect
.left
+ format_width
/ HSCROLL_FRACTION
;
1873 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1874 } while ((x
< goal
) && es
->x_offset
);
1875 /* FIXME: use ScrollWindow() somehow to improve performance */
1876 EDIT_UpdateText(es
, NULL
, TRUE
);
1877 } else if (x
> es
->format_rect
.right
) {
1879 INT len
= get_text_length(es
);
1880 goal
= es
->format_rect
.right
- format_width
/ HSCROLL_FRACTION
;
1883 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1884 x_last
= (short)LOWORD(EDIT_EM_PosFromChar(es
, len
, FALSE
));
1885 } while ((x
> goal
) && (x_last
> es
->format_rect
.right
));
1886 /* FIXME: use ScrollWindow() somehow to improve performance */
1887 EDIT_UpdateText(es
, NULL
, TRUE
);
1891 if(es
->flags
& EF_FOCUSED
)
1892 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
1896 /*********************************************************************
1901 static void EDIT_MoveBackward(EDITSTATE
*es
, BOOL extend
)
1903 INT e
= es
->selection_end
;
1907 if ((es
->style
& ES_MULTILINE
) && e
&&
1908 (es
->text
[e
- 1] == '\r') && (es
->text
[e
] == '\n')) {
1910 if (e
&& (es
->text
[e
- 1] == '\r'))
1914 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1915 EDIT_EM_ScrollCaret(es
);
1919 /*********************************************************************
1923 * Only for multi line controls
1924 * Move the caret one line down, on a column with the nearest
1925 * x coordinate on the screen (might be a different column).
1928 static void EDIT_MoveDown_ML(EDITSTATE
*es
, BOOL extend
)
1930 INT s
= es
->selection_start
;
1931 INT e
= es
->selection_end
;
1932 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
1933 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
1934 INT x
= (short)LOWORD(pos
);
1935 INT y
= (short)HIWORD(pos
);
1937 e
= EDIT_CharFromPos(es
, x
, y
+ es
->line_height
, &after_wrap
);
1940 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
1941 EDIT_EM_ScrollCaret(es
);
1945 /*********************************************************************
1950 static void EDIT_MoveEnd(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
1952 BOOL after_wrap
= FALSE
;
1955 /* Pass a high value in x to make sure of receiving the end of the line */
1956 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
1957 e
= EDIT_CharFromPos(es
, 0x3fffffff,
1958 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), &after_wrap
);
1960 e
= get_text_length(es
);
1961 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, after_wrap
);
1962 EDIT_EM_ScrollCaret(es
);
1966 /*********************************************************************
1971 static void EDIT_MoveForward(EDITSTATE
*es
, BOOL extend
)
1973 INT e
= es
->selection_end
;
1977 if ((es
->style
& ES_MULTILINE
) && (es
->text
[e
- 1] == '\r')) {
1978 if (es
->text
[e
] == '\n')
1980 else if ((es
->text
[e
] == '\r') && (es
->text
[e
+ 1] == '\n'))
1984 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1985 EDIT_EM_ScrollCaret(es
);
1989 /*********************************************************************
1993 * Home key: move to beginning of line.
1996 static void EDIT_MoveHome(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
2000 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2001 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
2002 e
= EDIT_CharFromPos(es
, -es
->x_offset
,
2003 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), NULL
);
2006 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
2007 EDIT_EM_ScrollCaret(es
);
2011 /*********************************************************************
2013 * EDIT_MovePageDown_ML
2015 * Only for multi line controls
2016 * Move the caret one page down, on a column with the nearest
2017 * x coordinate on the screen (might be a different column).
2020 static void EDIT_MovePageDown_ML(EDITSTATE
*es
, BOOL extend
)
2022 INT s
= es
->selection_start
;
2023 INT e
= es
->selection_end
;
2024 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2025 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2026 INT x
= (short)LOWORD(pos
);
2027 INT y
= (short)HIWORD(pos
);
2029 e
= EDIT_CharFromPos(es
, x
,
2030 y
+ (es
->format_rect
.bottom
- es
->format_rect
.top
),
2034 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2035 EDIT_EM_ScrollCaret(es
);
2039 /*********************************************************************
2041 * EDIT_MovePageUp_ML
2043 * Only for multi line controls
2044 * Move the caret one page up, on a column with the nearest
2045 * x coordinate on the screen (might be a different column).
2048 static void EDIT_MovePageUp_ML(EDITSTATE
*es
, BOOL extend
)
2050 INT s
= es
->selection_start
;
2051 INT e
= es
->selection_end
;
2052 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2053 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2054 INT x
= (short)LOWORD(pos
);
2055 INT y
= (short)HIWORD(pos
);
2057 e
= EDIT_CharFromPos(es
, x
,
2058 y
- (es
->format_rect
.bottom
- es
->format_rect
.top
),
2062 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2063 EDIT_EM_ScrollCaret(es
);
2067 /*********************************************************************
2071 * Only for multi line controls
2072 * Move the caret one line up, on a column with the nearest
2073 * x coordinate on the screen (might be a different column).
2076 static void EDIT_MoveUp_ML(EDITSTATE
*es
, BOOL extend
)
2078 INT s
= es
->selection_start
;
2079 INT e
= es
->selection_end
;
2080 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2081 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2082 INT x
= (short)LOWORD(pos
);
2083 INT y
= (short)HIWORD(pos
);
2085 e
= EDIT_CharFromPos(es
, x
, y
- es
->line_height
, &after_wrap
);
2088 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2089 EDIT_EM_ScrollCaret(es
);
2093 /*********************************************************************
2095 * EDIT_MoveWordBackward
2098 static void EDIT_MoveWordBackward(EDITSTATE
*es
, BOOL extend
)
2100 INT s
= es
->selection_start
;
2101 INT e
= es
->selection_end
;
2106 l
= EDIT_EM_LineFromChar(es
, e
);
2107 ll
= EDIT_EM_LineLength(es
, e
);
2108 li
= EDIT_EM_LineIndex(es
, l
);
2111 li
= EDIT_EM_LineIndex(es
, l
- 1);
2112 e
= li
+ EDIT_EM_LineLength(es
, li
);
2115 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
2119 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2120 EDIT_EM_ScrollCaret(es
);
2124 /*********************************************************************
2126 * EDIT_MoveWordForward
2129 static void EDIT_MoveWordForward(EDITSTATE
*es
, BOOL extend
)
2131 INT s
= es
->selection_start
;
2132 INT e
= es
->selection_end
;
2137 l
= EDIT_EM_LineFromChar(es
, e
);
2138 ll
= EDIT_EM_LineLength(es
, e
);
2139 li
= EDIT_EM_LineIndex(es
, l
);
2141 if ((es
->style
& ES_MULTILINE
) && (l
!= es
->line_count
- 1))
2142 e
= EDIT_EM_LineIndex(es
, l
+ 1);
2144 e
= li
+ EDIT_CallWordBreakProc(es
,
2145 li
, e
- li
+ 1, ll
, WB_RIGHT
);
2149 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2150 EDIT_EM_ScrollCaret(es
);
2154 /*********************************************************************
2159 static INT
EDIT_PaintText(EDITSTATE
*es
, HDC dc
, INT x
, INT y
, INT line
, INT col
, INT count
, BOOL rev
)
2163 LOGFONTW underline_font
;
2164 HFONT hUnderline
= 0;
2173 BkMode
= GetBkMode(dc
);
2174 BkColor
= GetBkColor(dc
);
2175 TextColor
= GetTextColor(dc
);
2177 if (es
->composition_len
== 0)
2179 SetBkColor(dc
, GetSysColor(COLOR_HIGHLIGHT
));
2180 SetTextColor(dc
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
2181 SetBkMode( dc
, OPAQUE
);
2185 HFONT current
= GetCurrentObject(dc
,OBJ_FONT
);
2186 GetObjectW(current
,sizeof(LOGFONTW
),&underline_font
);
2187 underline_font
.lfUnderline
= TRUE
;
2188 hUnderline
= CreateFontIndirectW(&underline_font
);
2189 old_font
= SelectObject(dc
,hUnderline
);
2192 li
= EDIT_EM_LineIndex(es
, line
);
2193 if (es
->style
& ES_MULTILINE
) {
2194 ret
= (INT
)LOWORD(TabbedTextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
,
2195 es
->tabs_count
, es
->tabs
, es
->format_rect
.left
- es
->x_offset
));
2197 TextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
);
2198 GetTextExtentPoint32W(dc
, es
->text
+ li
+ col
, count
, &size
);
2202 if (es
->composition_len
== 0)
2204 SetBkColor(dc
, BkColor
);
2205 SetTextColor(dc
, TextColor
);
2206 SetBkMode( dc
, BkMode
);
2211 SelectObject(dc
,old_font
);
2213 DeleteObject(hUnderline
);
2220 /*********************************************************************
2225 static void EDIT_PaintLine(EDITSTATE
*es
, HDC dc
, INT line
, BOOL rev
)
2234 SCRIPT_STRING_ANALYSIS ssa
;
2236 if (es
->style
& ES_MULTILINE
) {
2237 INT vlc
= get_vertical_line_count(es
);
2239 if ((line
< es
->y_offset
) || (line
> es
->y_offset
+ vlc
) || (line
>= es
->line_count
))
2244 TRACE("line=%d\n", line
);
2246 ssa
= EDIT_UpdateUniscribeData(es
, dc
, line
);
2247 pos
= EDIT_EM_PosFromChar(es
, EDIT_EM_LineIndex(es
, line
), FALSE
);
2248 x
= (short)LOWORD(pos
);
2249 y
= (short)HIWORD(pos
);
2251 if (es
->style
& ES_MULTILINE
)
2253 int line_idx
= line
;
2255 if (es
->style
& ES_RIGHT
|| es
->style
& ES_CENTER
)
2257 LINEDEF
*line_def
= es
->first_line_def
;
2260 while (line_def
&& line_idx
)
2262 line_def
= line_def
->next
;
2265 w
= es
->format_rect
.right
- es
->format_rect
.left
;
2266 lw
= line_def
->width
;
2268 if (es
->style
& ES_RIGHT
)
2270 else if (es
->style
& ES_CENTER
)
2273 x
+= es
->format_rect
.left
;
2278 li
= EDIT_EM_LineIndex(es
, line
);
2279 ll
= EDIT_EM_LineLength(es
, li
);
2280 s
= min(es
->selection_start
, es
->selection_end
);
2281 e
= max(es
->selection_start
, es
->selection_end
);
2282 s
= min(li
+ ll
, max(li
, s
));
2283 e
= min(li
+ ll
, max(li
, e
));
2287 ScriptStringOut(ssa
, x
, y
, 0, &es
->format_rect
, s
- li
, e
- li
, FALSE
);
2288 else if (rev
&& (s
!= e
) &&
2289 ((es
->flags
& EF_FOCUSED
) || (es
->style
& ES_NOHIDESEL
))) {
2290 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, s
- li
, FALSE
);
2291 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, s
- li
, e
- s
, TRUE
);
2292 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, e
- li
, li
+ ll
- e
, FALSE
);
2294 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, ll
, FALSE
);
2298 /*********************************************************************
2300 * EDIT_AdjustFormatRect
2302 * Adjusts the format rectangle for the current font and the
2303 * current client rectangle.
2306 static void EDIT_AdjustFormatRect(EDITSTATE
*es
)
2310 es
->format_rect
.right
= max(es
->format_rect
.right
, es
->format_rect
.left
+ es
->char_width
);
2311 if (es
->style
& ES_MULTILINE
)
2313 INT fw
, vlc
, max_x_offset
, max_y_offset
;
2315 vlc
= get_vertical_line_count(es
);
2316 es
->format_rect
.bottom
= es
->format_rect
.top
+ vlc
* es
->line_height
;
2318 /* correct es->x_offset */
2319 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2320 max_x_offset
= es
->text_width
- fw
;
2321 if(max_x_offset
< 0) max_x_offset
= 0;
2322 if(es
->x_offset
> max_x_offset
)
2323 es
->x_offset
= max_x_offset
;
2325 /* correct es->y_offset */
2326 max_y_offset
= es
->line_count
- vlc
;
2327 if(max_y_offset
< 0) max_y_offset
= 0;
2328 if(es
->y_offset
> max_y_offset
)
2329 es
->y_offset
= max_y_offset
;
2331 /* force scroll info update */
2332 EDIT_UpdateScrollInfo(es
);
2335 /* Windows doesn't care to fix text placement for SL controls */
2336 es
->format_rect
.bottom
= es
->format_rect
.top
+ es
->line_height
;
2338 /* Always stay within the client area */
2339 GetClientRect(es
->hwndSelf
, &ClientRect
);
2340 es
->format_rect
.bottom
= min(es
->format_rect
.bottom
, ClientRect
.bottom
);
2342 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
))
2343 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2345 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
2349 /*********************************************************************
2353 * note: this is not (exactly) the handler called on EM_SETRECTNP
2354 * it is also used to set the rect of a single line control
2357 static void EDIT_SetRectNP(EDITSTATE
*es
, const RECT
*rc
)
2361 ExStyle
= GetWindowLongPtrW(es
->hwndSelf
, GWL_EXSTYLE
);
2363 CopyRect(&es
->format_rect
, rc
);
2365 if (ExStyle
& WS_EX_CLIENTEDGE
) {
2366 es
->format_rect
.left
++;
2367 es
->format_rect
.right
--;
2369 if (es
->format_rect
.bottom
- es
->format_rect
.top
2370 >= es
->line_height
+ 2)
2372 es
->format_rect
.top
++;
2373 es
->format_rect
.bottom
--;
2376 else if (es
->style
& WS_BORDER
) {
2377 bw
= GetSystemMetrics(SM_CXBORDER
) + 1;
2378 bh
= GetSystemMetrics(SM_CYBORDER
) + 1;
2379 InflateRect(&es
->format_rect
, -bw
, 0);
2380 if (es
->format_rect
.bottom
- es
->format_rect
.top
>= es
->line_height
+ 2 * bh
)
2381 InflateRect(&es
->format_rect
, 0, -bh
);
2384 es
->format_rect
.left
+= es
->left_margin
;
2385 es
->format_rect
.right
-= es
->right_margin
;
2386 EDIT_AdjustFormatRect(es
);
2390 /*********************************************************************
2394 * returns line number (not index) in high-order word of result.
2395 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2396 * to Richedit, not to the edit control. Original documentation is valid.
2397 * FIXME: do the specs mean to return -1 if outside client area or
2398 * if outside formatting rectangle ???
2401 static LRESULT
EDIT_EM_CharFromPos(EDITSTATE
*es
, INT x
, INT y
)
2409 GetClientRect(es
->hwndSelf
, &rc
);
2410 if (!PtInRect(&rc
, pt
))
2413 index
= EDIT_CharFromPos(es
, x
, y
, NULL
);
2414 return MAKELONG(index
, EDIT_EM_LineFromChar(es
, index
));
2418 /*********************************************************************
2422 * Enable or disable soft breaks.
2424 * This means: insert or remove the soft linebreak character (\r\r\n).
2425 * Take care to check if the text still fits the buffer after insertion.
2426 * If not, notify with EN_ERRSPACE.
2429 static BOOL
EDIT_EM_FmtLines(EDITSTATE
*es
, BOOL add_eol
)
2431 es
->flags
&= ~EF_USE_SOFTBRK
;
2433 es
->flags
|= EF_USE_SOFTBRK
;
2434 FIXME("soft break enabled, not implemented\n");
2440 /*********************************************************************
2444 * Hopefully this won't fire back at us.
2445 * We always start with a fixed buffer in the local heap.
2446 * Despite of the documentation says that the local heap is used
2447 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2448 * buffer on the local heap.
2451 static HLOCAL
EDIT_EM_GetHandle(EDITSTATE
*es
)
2455 if (!(es
->style
& ES_MULTILINE
))
2459 hLocal
= es
->hloc32W
;
2465 UINT countA
, alloc_size
;
2466 TRACE("Allocating 32-bit ANSI alias buffer\n");
2467 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, NULL
, 0, NULL
, NULL
);
2468 alloc_size
= ROUND_TO_GROW(countA
);
2469 if(!(es
->hloc32A
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
2471 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size
);
2474 textA
= LocalLock(es
->hloc32A
);
2475 WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, countA
, NULL
, NULL
);
2476 LocalUnlock(es
->hloc32A
);
2478 hLocal
= es
->hloc32A
;
2481 EDIT_UnlockBuffer(es
, TRUE
);
2483 /* The text buffer handle belongs to the app */
2484 es
->hlocapp
= hLocal
;
2486 TRACE("Returning %p, LocalSize() = %ld\n", hLocal
, LocalSize(hLocal
));
2491 /*********************************************************************
2496 static INT
EDIT_EM_GetLine(EDITSTATE
*es
, INT line
, LPWSTR dst
, BOOL unicode
)
2499 INT line_len
, dst_len
;
2502 if (es
->style
& ES_MULTILINE
) {
2503 if (line
>= es
->line_count
)
2507 i
= EDIT_EM_LineIndex(es
, line
);
2509 line_len
= EDIT_EM_LineLength(es
, i
);
2510 dst_len
= *(WORD
*)dst
;
2513 if(dst_len
<= line_len
)
2515 memcpy(dst
, src
, dst_len
* sizeof(WCHAR
));
2518 else /* Append 0 if enough space */
2520 memcpy(dst
, src
, line_len
* sizeof(WCHAR
));
2527 INT ret
= WideCharToMultiByte(CP_ACP
, 0, src
, line_len
, (LPSTR
)dst
, dst_len
, NULL
, NULL
);
2528 if(!ret
&& line_len
) /* Insufficient buffer size */
2530 if(ret
< dst_len
) /* Append 0 if enough space */
2531 ((LPSTR
)dst
)[ret
] = 0;
2537 /*********************************************************************
2542 static LRESULT
EDIT_EM_GetSel(const EDITSTATE
*es
, PUINT start
, PUINT end
)
2544 UINT s
= es
->selection_start
;
2545 UINT e
= es
->selection_end
;
2552 return MAKELONG(s
, e
);
2556 /*********************************************************************
2560 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2563 static void EDIT_EM_ReplaceSel(EDITSTATE
*es
, BOOL can_undo
, LPCWSTR lpsz_replace
, BOOL send_update
, BOOL honor_limit
)
2565 UINT strl
= strlenW(lpsz_replace
);
2566 UINT tl
= get_text_length(es
);
2577 TRACE("%s, can_undo %d, send_update %d\n",
2578 debugstr_w(lpsz_replace
), can_undo
, send_update
);
2580 s
= es
->selection_start
;
2581 e
= es
->selection_end
;
2583 EDIT_InvalidateUniscribeData(es
);
2584 if ((s
== e
) && !strl
)
2589 size
= tl
- (e
- s
) + strl
;
2593 /* Issue the EN_MAXTEXT notification and continue with replacing text
2594 * so that buffer limit is honored. */
2595 if ((honor_limit
) && (size
> es
->buffer_limit
)) {
2596 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2597 /* Buffer limit can be smaller than the actual length of text in combobox */
2598 if (es
->buffer_limit
< (tl
- (e
-s
)))
2601 strl
= es
->buffer_limit
- (tl
- (e
-s
));
2604 if (!EDIT_MakeFit(es
, tl
- (e
- s
) + strl
))
2608 /* there is something to be deleted */
2609 TRACE("deleting stuff.\n");
2611 buf
= HeapAlloc(GetProcessHeap(), 0, (bufl
+ 1) * sizeof(WCHAR
));
2613 memcpy(buf
, es
->text
+ s
, bufl
* sizeof(WCHAR
));
2614 buf
[bufl
] = 0; /* ensure 0 termination */
2616 strcpyW(es
->text
+ s
, es
->text
+ e
);
2617 text_buffer_changed(es
);
2620 /* there is an insertion */
2621 tl
= get_text_length(es
);
2622 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
));
2623 for (p
= es
->text
+ tl
; p
>= es
->text
+ s
; p
--)
2625 for (i
= 0 , p
= es
->text
+ s
; i
< strl
; i
++)
2626 p
[i
] = lpsz_replace
[i
];
2627 if(es
->style
& ES_UPPERCASE
)
2628 CharUpperBuffW(p
, strl
);
2629 else if(es
->style
& ES_LOWERCASE
)
2630 CharLowerBuffW(p
, strl
);
2631 text_buffer_changed(es
);
2633 if (es
->style
& ES_MULTILINE
)
2635 INT st
= min(es
->selection_start
, es
->selection_end
);
2636 INT vlc
= get_vertical_line_count(es
);
2638 hrgn
= CreateRectRgn(0, 0, 0, 0);
2639 EDIT_BuildLineDefs_ML(es
, st
, st
+ strl
,
2640 strl
- abs(es
->selection_end
- es
->selection_start
), hrgn
);
2641 /* if text is too long undo all changes */
2642 if (honor_limit
&& !(es
->style
& ES_AUTOVSCROLL
) && (es
->line_count
> vlc
)) {
2644 strcpyW(es
->text
+ e
, es
->text
+ e
+ strl
);
2646 for (i
= 0 , p
= es
->text
; i
< e
- s
; i
++)
2648 text_buffer_changed(es
);
2649 EDIT_BuildLineDefs_ML(es
, s
, e
,
2650 abs(es
->selection_end
- es
->selection_start
) - strl
, hrgn
);
2653 hrgn
= CreateRectRgn(0, 0, 0, 0);
2654 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2658 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2659 EDIT_InvalidateUniscribeData(es
);
2660 EDIT_CalcLineWidth_SL(es
);
2661 /* remove chars that don't fit */
2662 if (honor_limit
&& !(es
->style
& ES_AUTOHSCROLL
) && (es
->text_width
> fw
)) {
2663 while ((es
->text_width
> fw
) && s
+ strl
>= s
) {
2664 strcpyW(es
->text
+ s
+ strl
- 1, es
->text
+ s
+ strl
);
2666 es
->text_length
= -1;
2667 EDIT_InvalidateUniscribeData(es
);
2668 EDIT_CalcLineWidth_SL(es
);
2670 text_buffer_changed(es
);
2671 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2677 utl
= strlenW(es
->undo_text
);
2678 if (!es
->undo_insert_count
&& (*es
->undo_text
&& (s
== es
->undo_position
))) {
2679 /* undo-buffer is extended to the right */
2680 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2681 memcpy(es
->undo_text
+ utl
, buf
, (e
- s
)*sizeof(WCHAR
));
2682 (es
->undo_text
+ utl
)[e
- s
] = 0; /* ensure 0 termination */
2683 } else if (!es
->undo_insert_count
&& (*es
->undo_text
&& (e
== es
->undo_position
))) {
2684 /* undo-buffer is extended to the left */
2685 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2686 for (p
= es
->undo_text
+ utl
; p
>= es
->undo_text
; p
--)
2688 for (i
= 0 , p
= es
->undo_text
; i
< e
- s
; i
++)
2690 es
->undo_position
= s
;
2692 /* new undo-buffer */
2693 EDIT_MakeUndoFit(es
, e
- s
);
2694 memcpy(es
->undo_text
, buf
, (e
- s
)*sizeof(WCHAR
));
2695 es
->undo_text
[e
- s
] = 0; /* ensure 0 termination */
2696 es
->undo_position
= s
;
2698 /* any deletion makes the old insertion-undo invalid */
2699 es
->undo_insert_count
= 0;
2701 EDIT_EM_EmptyUndoBuffer(es
);
2705 if ((s
== es
->undo_position
) ||
2706 ((es
->undo_insert_count
) &&
2707 (s
== es
->undo_position
+ es
->undo_insert_count
)))
2709 * insertion is new and at delete position or
2710 * an extension to either left or right
2712 es
->undo_insert_count
+= strl
;
2714 /* new insertion undo */
2715 es
->undo_position
= s
;
2716 es
->undo_insert_count
= strl
;
2717 /* new insertion makes old delete-buffer invalid */
2718 *es
->undo_text
= '\0';
2721 EDIT_EM_EmptyUndoBuffer(es
);
2724 HeapFree(GetProcessHeap(), 0, buf
);
2728 /* If text has been deleted and we're right or center aligned then scroll rightward */
2729 if (es
->style
& (ES_RIGHT
| ES_CENTER
))
2731 INT delta
= strl
- abs(es
->selection_end
- es
->selection_start
);
2733 if (delta
< 0 && es
->x_offset
)
2735 if (abs(delta
) > es
->x_offset
)
2738 es
->x_offset
+= delta
;
2742 EDIT_EM_SetSel(es
, s
, s
, FALSE
);
2743 es
->flags
|= EF_MODIFIED
;
2744 if (send_update
) es
->flags
|= EF_UPDATE
;
2747 EDIT_UpdateTextRegion(es
, hrgn
, TRUE
);
2751 EDIT_UpdateText(es
, NULL
, TRUE
);
2753 EDIT_EM_ScrollCaret(es
);
2755 /* force scroll info update */
2756 EDIT_UpdateScrollInfo(es
);
2759 if(send_update
|| (es
->flags
& EF_UPDATE
))
2761 es
->flags
&= ~EF_UPDATE
;
2762 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
2764 EDIT_InvalidateUniscribeData(es
);
2768 /*********************************************************************
2772 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2775 static void EDIT_EM_SetHandle(EDITSTATE
*es
, HLOCAL hloc
)
2777 if (!(es
->style
& ES_MULTILINE
))
2781 WARN("called with NULL handle\n");
2785 EDIT_UnlockBuffer(es
, TRUE
);
2791 LocalFree(es
->hloc32A
);
2803 countA
= LocalSize(hloc
);
2804 textA
= LocalLock(hloc
);
2805 countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
2806 if(!(hloc32W_new
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, countW
* sizeof(WCHAR
))))
2808 ERR("Could not allocate new unicode buffer\n");
2811 textW
= LocalLock(hloc32W_new
);
2812 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, textW
, countW
);
2813 LocalUnlock(hloc32W_new
);
2817 LocalFree(es
->hloc32W
);
2819 es
->hloc32W
= hloc32W_new
;
2823 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
2825 /* The text buffer handle belongs to the control */
2828 EDIT_LockBuffer(es
);
2829 text_buffer_changed(es
);
2831 es
->x_offset
= es
->y_offset
= 0;
2832 es
->selection_start
= es
->selection_end
= 0;
2833 EDIT_EM_EmptyUndoBuffer(es
);
2834 es
->flags
&= ~EF_MODIFIED
;
2835 es
->flags
&= ~EF_UPDATE
;
2836 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2837 EDIT_UpdateText(es
, NULL
, TRUE
);
2838 EDIT_EM_ScrollCaret(es
);
2839 /* force scroll info update */
2840 EDIT_UpdateScrollInfo(es
);
2844 /*********************************************************************
2848 * NOTE: this version currently implements WinNT limits
2851 static void EDIT_EM_SetLimitText(EDITSTATE
*es
, UINT limit
)
2853 if (!limit
) limit
= ~0u;
2854 if (!(es
->style
& ES_MULTILINE
)) limit
= min(limit
, 0x7ffffffe);
2855 es
->buffer_limit
= limit
;
2859 /*********************************************************************
2863 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2864 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2865 * margin according to the textmetrics of the current font.
2867 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2868 * of the char's width as the margin, but this is not how Windows handles this.
2869 * For all other fonts Windows sets the margins to zero.
2871 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2872 * edit control is equal to or larger than a certain size.
2873 * Interestingly if one subtracts both the left and right margins from
2874 * this size one always seems to get an even number. The extents of
2875 * the (four character) string "'**'" match this quite closely, so
2876 * we'll use this until we come up with a better idea.
2878 static int calc_min_set_margin_size(HDC dc
, INT left
, INT right
)
2880 static const WCHAR magic_string
[] = {'\'','*','*','\'', 0};
2883 GetTextExtentPointW(dc
, magic_string
, sizeof(magic_string
)/sizeof(WCHAR
) - 1, &sz
);
2884 return sz
.cx
+ left
+ right
;
2887 static void EDIT_EM_SetMargins(EDITSTATE
*es
, INT action
,
2888 WORD left
, WORD right
, BOOL repaint
)
2891 INT default_left_margin
= 0; /* in pixels */
2892 INT default_right_margin
= 0; /* in pixels */
2894 /* Set the default margins depending on the font */
2895 if (es
->font
&& (left
== EC_USEFONTINFO
|| right
== EC_USEFONTINFO
)) {
2896 HDC dc
= GetDC(es
->hwndSelf
);
2897 HFONT old_font
= SelectObject(dc
, es
->font
);
2898 GetTextMetricsW(dc
, &tm
);
2899 /* The default margins are only non zero for TrueType or Vector fonts */
2900 if (tm
.tmPitchAndFamily
& ( TMPF_VECTOR
| TMPF_TRUETYPE
)) {
2903 /* This must be calculated more exactly! But how? */
2904 default_left_margin
= tm
.tmAveCharWidth
/ 2;
2905 default_right_margin
= tm
.tmAveCharWidth
/ 2;
2906 min_size
= calc_min_set_margin_size(dc
, default_left_margin
, default_right_margin
);
2907 GetClientRect(es
->hwndSelf
, &rc
);
2908 if (!IsRectEmpty(&rc
) && (rc
.right
- rc
.left
< min_size
)) {
2909 default_left_margin
= es
->left_margin
;
2910 default_right_margin
= es
->right_margin
;
2913 SelectObject(dc
, old_font
);
2914 ReleaseDC(es
->hwndSelf
, dc
);
2917 if (action
& EC_LEFTMARGIN
) {
2918 es
->format_rect
.left
-= es
->left_margin
;
2919 if (left
!= EC_USEFONTINFO
)
2920 es
->left_margin
= left
;
2922 es
->left_margin
= default_left_margin
;
2923 es
->format_rect
.left
+= es
->left_margin
;
2926 if (action
& EC_RIGHTMARGIN
) {
2927 es
->format_rect
.right
+= es
->right_margin
;
2928 if (right
!= EC_USEFONTINFO
)
2929 es
->right_margin
= right
;
2931 es
->right_margin
= default_right_margin
;
2932 es
->format_rect
.right
-= es
->right_margin
;
2935 if (action
& (EC_LEFTMARGIN
| EC_RIGHTMARGIN
)) {
2936 EDIT_AdjustFormatRect(es
);
2937 if (repaint
) EDIT_UpdateText(es
, NULL
, TRUE
);
2940 TRACE("left=%d, right=%d\n", es
->left_margin
, es
->right_margin
);
2944 /*********************************************************************
2946 * EM_SETPASSWORDCHAR
2949 static void EDIT_EM_SetPasswordChar(EDITSTATE
*es
, WCHAR c
)
2953 if (es
->style
& ES_MULTILINE
)
2956 if (es
->password_char
== c
)
2959 style
= GetWindowLongW( es
->hwndSelf
, GWL_STYLE
);
2960 es
->password_char
= c
;
2962 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
| ES_PASSWORD
);
2963 es
->style
|= ES_PASSWORD
;
2965 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
& ~ES_PASSWORD
);
2966 es
->style
&= ~ES_PASSWORD
;
2968 EDIT_InvalidateUniscribeData(es
);
2969 EDIT_UpdateText(es
, NULL
, TRUE
);
2973 /*********************************************************************
2978 static BOOL
EDIT_EM_SetTabStops(EDITSTATE
*es
, INT count
, const INT
*tabs
)
2980 if (!(es
->style
& ES_MULTILINE
))
2982 HeapFree(GetProcessHeap(), 0, es
->tabs
);
2983 es
->tabs_count
= count
;
2987 es
->tabs
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(INT
));
2988 memcpy(es
->tabs
, tabs
, count
* sizeof(INT
));
2990 EDIT_InvalidateUniscribeData(es
);
2995 /*********************************************************************
2997 * EM_SETWORDBREAKPROC
3000 static void EDIT_EM_SetWordBreakProc(EDITSTATE
*es
, void *wbp
)
3002 if (es
->word_break_proc
== wbp
)
3005 es
->word_break_proc
= wbp
;
3007 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
)) {
3008 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
3009 EDIT_UpdateText(es
, NULL
, TRUE
);
3014 /*********************************************************************
3019 static BOOL
EDIT_EM_Undo(EDITSTATE
*es
)
3024 /* As per MSDN spec, for a single-line edit control,
3025 the return value is always TRUE */
3026 if( es
->style
& ES_READONLY
)
3027 return !(es
->style
& ES_MULTILINE
);
3029 ulength
= strlenW(es
->undo_text
);
3031 utext
= HeapAlloc(GetProcessHeap(), 0, (ulength
+ 1) * sizeof(WCHAR
));
3033 strcpyW(utext
, es
->undo_text
);
3035 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3036 es
->undo_insert_count
, debugstr_w(utext
));
3038 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3039 EDIT_EM_EmptyUndoBuffer(es
);
3040 EDIT_EM_ReplaceSel(es
, TRUE
, utext
, TRUE
, TRUE
);
3041 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3042 /* send the notification after the selection start and end are set */
3043 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
3044 EDIT_EM_ScrollCaret(es
);
3045 HeapFree(GetProcessHeap(), 0, utext
);
3047 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3048 es
->undo_insert_count
, debugstr_w(es
->undo_text
));
3053 /* Helper function for WM_CHAR
3055 * According to an MSDN blog article titled "Just because you're a control
3056 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3057 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3058 * a dialog box or not.
3060 static inline BOOL
EDIT_IsInsideDialog(EDITSTATE
*es
)
3062 return (es
->flags
& EF_DIALOGMODE
);
3066 /*********************************************************************
3071 static void EDIT_WM_Paste(EDITSTATE
*es
)
3076 /* Protect read-only edit control from modification */
3077 if(es
->style
& ES_READONLY
)
3080 OpenClipboard(es
->hwndSelf
);
3081 if ((hsrc
= GetClipboardData(CF_UNICODETEXT
))) {
3082 src
= GlobalLock(hsrc
);
3083 EDIT_EM_ReplaceSel(es
, TRUE
, src
, TRUE
, TRUE
);
3086 else if (es
->style
& ES_PASSWORD
) {
3087 /* clear selected text in password edit box even with empty clipboard */
3088 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
3094 /*********************************************************************
3099 static void EDIT_WM_Copy(EDITSTATE
*es
)
3101 INT s
= min(es
->selection_start
, es
->selection_end
);
3102 INT e
= max(es
->selection_start
, es
->selection_end
);
3110 hdst
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (len
+ 1) * sizeof(WCHAR
));
3111 dst
= GlobalLock(hdst
);
3112 memcpy(dst
, es
->text
+ s
, len
* sizeof(WCHAR
));
3113 dst
[len
] = 0; /* ensure 0 termination */
3114 TRACE("%s\n", debugstr_w(dst
));
3116 OpenClipboard(es
->hwndSelf
);
3118 SetClipboardData(CF_UNICODETEXT
, hdst
);
3123 /*********************************************************************
3128 static inline void EDIT_WM_Clear(EDITSTATE
*es
)
3130 /* Protect read-only edit control from modification */
3131 if(es
->style
& ES_READONLY
)
3134 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
3138 /*********************************************************************
3143 static inline void EDIT_WM_Cut(EDITSTATE
*es
)
3150 /*********************************************************************
3155 static LRESULT
EDIT_WM_Char(EDITSTATE
*es
, WCHAR c
)
3159 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3163 /* If it's not a multiline edit box, it would be ignored below.
3164 * For multiline edit without ES_WANTRETURN, we have to make a
3167 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_WANTRETURN
))
3168 if (EDIT_IsInsideDialog(es
))
3171 if (es
->style
& ES_MULTILINE
) {
3172 if (es
->style
& ES_READONLY
) {
3173 EDIT_MoveHome(es
, FALSE
, FALSE
);
3174 EDIT_MoveDown_ML(es
, FALSE
);
3176 static const WCHAR cr_lfW
[] = {'\r','\n',0};
3177 EDIT_EM_ReplaceSel(es
, TRUE
, cr_lfW
, TRUE
, TRUE
);
3182 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_READONLY
))
3184 static const WCHAR tabW
[] = {'\t',0};
3185 if (EDIT_IsInsideDialog(es
))
3187 EDIT_EM_ReplaceSel(es
, TRUE
, tabW
, TRUE
, TRUE
);
3191 if (!(es
->style
& ES_READONLY
) && !control
) {
3192 if (es
->selection_start
!= es
->selection_end
)
3195 /* delete character left of caret */
3196 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3197 EDIT_MoveBackward(es
, TRUE
);
3203 if (!(es
->style
& ES_PASSWORD
))
3204 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3207 if (!(es
->style
& ES_READONLY
))
3208 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3211 if (!((es
->style
& ES_READONLY
) || (es
->style
& ES_PASSWORD
)))
3212 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3215 if (!(es
->style
& ES_READONLY
))
3216 SendMessageW(es
->hwndSelf
, WM_UNDO
, 0, 0);
3220 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3221 if( (es
->style
& ES_NUMBER
) && !( c
>= '0' && c
<= '9') )
3224 if (!(es
->style
& ES_READONLY
) && (c
>= ' ') && (c
!= 127)) {
3228 EDIT_EM_ReplaceSel(es
, TRUE
, str
, TRUE
, TRUE
);
3236 /*********************************************************************
3238 * EDIT_ContextMenuCommand
3241 static void EDIT_ContextMenuCommand(EDITSTATE
*es
, UINT id
)
3245 SendMessageW(es
->hwndSelf
, WM_UNDO
, 0, 0);
3248 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3251 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3254 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3257 SendMessageW(es
->hwndSelf
, WM_CLEAR
, 0, 0);
3260 SendMessageW(es
->hwndSelf
, EM_SETSEL
, 0, -1);
3263 ERR("unknown menu item, please report\n");
3269 /*********************************************************************
3273 * Note: the resource files resource/sysres_??.rc cannot define a
3274 * single popup menu. Hence we use a (dummy) menubar
3275 * containing the single popup menu as its first item.
3277 * FIXME: the message identifiers have been chosen arbitrarily,
3278 * hence we use MF_BYPOSITION.
3279 * We might as well use the "real" values (anybody knows ?)
3280 * The menu definition is in resources/sysres_??.rc.
3281 * Once these are OK, we better use MF_BYCOMMAND here
3282 * (as we do in EDIT_WM_Command()).
3285 static void EDIT_WM_ContextMenu(EDITSTATE
*es
, INT x
, INT y
)
3287 HMENU menu
= LoadMenuA(user32_module
, "EDITMENU");
3288 HMENU popup
= GetSubMenu(menu
, 0);
3289 UINT start
= es
->selection_start
;
3290 UINT end
= es
->selection_end
;
3293 ORDER_UINT(start
, end
);
3296 EnableMenuItem(popup
, 0, MF_BYPOSITION
| (EDIT_EM_CanUndo(es
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3298 EnableMenuItem(popup
, 2, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3300 EnableMenuItem(popup
, 3, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) ? MF_ENABLED
: MF_GRAYED
));
3302 EnableMenuItem(popup
, 4, MF_BYPOSITION
| (IsClipboardFormatAvailable(CF_UNICODETEXT
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3304 EnableMenuItem(popup
, 5, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3306 EnableMenuItem(popup
, 7, MF_BYPOSITION
| (start
|| (end
!= get_text_length(es
)) ? MF_ENABLED
: MF_GRAYED
));
3308 if (x
== -1 && y
== -1) /* passed via VK_APPS press/release */
3311 /* Windows places the menu at the edit's center in this case */
3312 WIN_GetRectangles( es
->hwndSelf
, COORDS_SCREEN
, NULL
, &rc
);
3313 x
= rc
.left
+ (rc
.right
- rc
.left
) / 2;
3314 y
= rc
.top
+ (rc
.bottom
- rc
.top
) / 2;
3317 if (!(es
->flags
& EF_FOCUSED
))
3318 SetFocus(es
->hwndSelf
);
3320 cmd
= TrackPopupMenu(popup
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
| TPM_RETURNCMD
| TPM_NONOTIFY
,
3321 x
, y
, 0, es
->hwndSelf
, NULL
);
3324 EDIT_ContextMenuCommand(es
, cmd
);
3330 /*********************************************************************
3335 static INT
EDIT_WM_GetText(const EDITSTATE
*es
, INT count
, LPWSTR dst
, BOOL unicode
)
3337 if(!count
) return 0;
3341 lstrcpynW(dst
, es
->text
, count
);
3342 return strlenW(dst
);
3346 LPSTR textA
= (LPSTR
)dst
;
3347 if (!WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, count
, NULL
, NULL
))
3348 textA
[count
- 1] = 0; /* ensure 0 termination */
3349 return strlen(textA
);
3353 /*********************************************************************
3358 static BOOL
EDIT_CheckCombo(EDITSTATE
*es
, UINT msg
, INT key
)
3360 HWND hLBox
= es
->hwndListBox
;
3368 hCombo
= GetParent(es
->hwndSelf
);
3372 TRACE_(combo
)("[%p]: handling msg %x (%x)\n", es
->hwndSelf
, msg
, key
);
3374 if (key
== VK_UP
|| key
== VK_DOWN
)
3376 if (SendMessageW(hCombo
, CB_GETEXTENDEDUI
, 0, 0))
3379 if (msg
== WM_KEYDOWN
|| nEUI
)
3380 bDropped
= (BOOL
)SendMessageW(hCombo
, CB_GETDROPPEDSTATE
, 0, 0);
3386 if (!bDropped
&& nEUI
&& (key
== VK_UP
|| key
== VK_DOWN
))
3388 /* make sure ComboLBox pops up */
3389 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, FALSE
, 0);
3394 SendMessageW(hLBox
, WM_KEYDOWN
, key
, 0);
3397 case WM_SYSKEYDOWN
: /* Handle Alt+up/down arrows */
3399 SendMessageW(hCombo
, CB_SHOWDROPDOWN
, !bDropped
, 0);
3401 SendMessageW(hLBox
, WM_KEYDOWN
, VK_F4
, 0);
3406 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, TRUE
, 0);
3412 /*********************************************************************
3416 * Handling of special keys that don't produce a WM_CHAR
3417 * (i.e. non-printable keys) & Backspace & Delete
3420 static LRESULT
EDIT_WM_KeyDown(EDITSTATE
*es
, INT key
)
3425 if (GetKeyState(VK_MENU
) & 0x8000)
3428 shift
= GetKeyState(VK_SHIFT
) & 0x8000;
3429 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3434 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
) || key
== VK_F4
)
3439 if ((es
->style
& ES_MULTILINE
) && (key
== VK_UP
))
3440 EDIT_MoveUp_ML(es
, shift
);
3443 EDIT_MoveWordBackward(es
, shift
);
3445 EDIT_MoveBackward(es
, shift
);
3448 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
))
3452 if ((es
->style
& ES_MULTILINE
) && (key
== VK_DOWN
))
3453 EDIT_MoveDown_ML(es
, shift
);
3455 EDIT_MoveWordForward(es
, shift
);
3457 EDIT_MoveForward(es
, shift
);
3460 EDIT_MoveHome(es
, shift
, control
);
3463 EDIT_MoveEnd(es
, shift
, control
);
3466 if (es
->style
& ES_MULTILINE
)
3467 EDIT_MovePageUp_ML(es
, shift
);
3469 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
3472 if (es
->style
& ES_MULTILINE
)
3473 EDIT_MovePageDown_ML(es
, shift
);
3475 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
3478 if (!(es
->style
& ES_READONLY
) && !(shift
&& control
)) {
3479 if (es
->selection_start
!= es
->selection_end
) {
3486 /* delete character left of caret */
3487 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3488 EDIT_MoveBackward(es
, TRUE
);
3490 } else if (control
) {
3491 /* delete to end of line */
3492 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3493 EDIT_MoveEnd(es
, TRUE
, FALSE
);
3496 /* delete character right of caret */
3497 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3498 EDIT_MoveForward(es
, TRUE
);
3506 if (!(es
->style
& ES_READONLY
))
3512 /* If the edit doesn't want the return send a message to the default object */
3513 if(!(es
->style
& ES_MULTILINE
) || !(es
->style
& ES_WANTRETURN
))
3517 if (!EDIT_IsInsideDialog(es
)) break;
3519 dw
= SendMessageW(es
->hwndParent
, DM_GETDEFID
, 0, 0);
3520 if (HIWORD(dw
) == DC_HASDEFID
)
3522 HWND hwDefCtrl
= GetDlgItem(es
->hwndParent
, LOWORD(dw
));
3525 SendMessageW(es
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
3526 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
3532 if ((es
->style
& ES_MULTILINE
) && EDIT_IsInsideDialog(es
))
3533 PostMessageW(es
->hwndParent
, WM_CLOSE
, 0, 0);
3536 if ((es
->style
& ES_MULTILINE
) && EDIT_IsInsideDialog(es
))
3537 SendMessageW(es
->hwndParent
, WM_NEXTDLGCTL
, shift
, 0);
3544 /*********************************************************************
3549 static LRESULT
EDIT_WM_KillFocus(EDITSTATE
*es
)
3551 es
->flags
&= ~EF_FOCUSED
;
3553 if(!(es
->style
& ES_NOHIDESEL
))
3554 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
3555 EDIT_NOTIFY_PARENT(es
, EN_KILLFOCUS
);
3556 /* throw away left over scroll when we lose focus */
3557 es
->wheelDeltaRemainder
= 0;
3562 /*********************************************************************
3566 * The caret position has been set on the WM_LBUTTONDOWN message
3569 static LRESULT
EDIT_WM_LButtonDblClk(EDITSTATE
*es
)
3572 INT e
= es
->selection_end
;
3577 es
->bCaptureState
= TRUE
;
3578 SetCapture(es
->hwndSelf
);
3580 l
= EDIT_EM_LineFromChar(es
, e
);
3581 li
= EDIT_EM_LineIndex(es
, l
);
3582 ll
= EDIT_EM_LineLength(es
, e
);
3583 s
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
3584 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_RIGHT
);
3585 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
3586 EDIT_EM_ScrollCaret(es
);
3587 es
->region_posx
= es
->region_posy
= 0;
3588 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
3593 /*********************************************************************
3598 static LRESULT
EDIT_WM_LButtonDown(EDITSTATE
*es
, DWORD keys
, INT x
, INT y
)
3603 es
->bCaptureState
= TRUE
;
3604 SetCapture(es
->hwndSelf
);
3605 EDIT_ConfinePoint(es
, &x
, &y
);
3606 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
3607 EDIT_EM_SetSel(es
, (keys
& MK_SHIFT
) ? es
->selection_start
: e
, e
, after_wrap
);
3608 EDIT_EM_ScrollCaret(es
);
3609 es
->region_posx
= es
->region_posy
= 0;
3610 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
3612 if (!(es
->flags
& EF_FOCUSED
))
3613 SetFocus(es
->hwndSelf
);
3619 /*********************************************************************
3624 static LRESULT
EDIT_WM_LButtonUp(EDITSTATE
*es
)
3626 if (es
->bCaptureState
) {
3627 KillTimer(es
->hwndSelf
, 0);
3628 if (GetCapture() == es
->hwndSelf
) ReleaseCapture();
3630 es
->bCaptureState
= FALSE
;
3635 /*********************************************************************
3640 static LRESULT
EDIT_WM_MButtonDown(EDITSTATE
*es
)
3642 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3647 /*********************************************************************
3652 static LRESULT
EDIT_WM_MouseMove(EDITSTATE
*es
, INT x
, INT y
)
3658 /* If the mouse has been captured by process other than the edit control itself,
3659 * the windows edit controls will not select the strings with mouse move.
3661 if (!es
->bCaptureState
|| GetCapture() != es
->hwndSelf
)
3665 * FIXME: gotta do some scrolling if outside client
3666 * area. Maybe reset the timer ?
3669 EDIT_ConfinePoint(es
, &x
, &y
);
3670 es
->region_posx
= (prex
< x
) ? -1 : ((prex
> x
) ? 1 : 0);
3671 es
->region_posy
= (prey
< y
) ? -1 : ((prey
> y
) ? 1 : 0);
3672 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
3673 EDIT_EM_SetSel(es
, es
->selection_start
, e
, after_wrap
);
3674 EDIT_SetCaretPos(es
,es
->selection_end
,es
->flags
& EF_AFTER_WRAP
);
3679 /*********************************************************************
3684 static void EDIT_WM_Paint(EDITSTATE
*es
, HDC hdc
)
3697 BOOL rev
= es
->bEnableState
&&
3698 ((es
->flags
& EF_FOCUSED
) ||
3699 (es
->style
& ES_NOHIDESEL
));
3700 dc
= hdc
? hdc
: BeginPaint(es
->hwndSelf
, &ps
);
3702 /* The dc we use for calculating may not be the one we paint into.
3703 This is the safest action. */
3704 EDIT_InvalidateUniscribeData(es
);
3705 GetClientRect(es
->hwndSelf
, &rcClient
);
3707 /* get the background brush */
3708 brush
= EDIT_NotifyCtlColor(es
, dc
);
3710 /* paint the border and the background */
3711 IntersectClipRect(dc
, rcClient
.left
, rcClient
.top
, rcClient
.right
, rcClient
.bottom
);
3713 if(es
->style
& WS_BORDER
) {
3714 bw
= GetSystemMetrics(SM_CXBORDER
);
3715 bh
= GetSystemMetrics(SM_CYBORDER
);
3717 if(es
->style
& ES_MULTILINE
) {
3718 if(es
->style
& WS_HSCROLL
) rc
.bottom
+=bh
;
3719 if(es
->style
& WS_VSCROLL
) rc
.right
+=bw
;
3722 /* Draw the frame. Same code as in nonclient.c */
3723 old_brush
= SelectObject(dc
, GetSysColorBrush(COLOR_WINDOWFRAME
));
3724 PatBlt(dc
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, bh
, PATCOPY
);
3725 PatBlt(dc
, rc
.left
, rc
.top
, bw
, rc
.bottom
- rc
.top
, PATCOPY
);
3726 PatBlt(dc
, rc
.left
, rc
.bottom
- 1, rc
.right
- rc
.left
, -bw
, PATCOPY
);
3727 PatBlt(dc
, rc
.right
- 1, rc
.top
, -bw
, rc
.bottom
- rc
.top
, PATCOPY
);
3728 SelectObject(dc
, old_brush
);
3730 /* Keep the border clean */
3731 IntersectClipRect(dc
, rc
.left
+bw
, rc
.top
+bh
,
3732 max(rc
.right
-bw
, rc
.left
+bw
), max(rc
.bottom
-bh
, rc
.top
+bh
));
3735 GetClipBox(dc
, &rc
);
3736 FillRect(dc
, &rc
, brush
);
3738 IntersectClipRect(dc
, es
->format_rect
.left
,
3739 es
->format_rect
.top
,
3740 es
->format_rect
.right
,
3741 es
->format_rect
.bottom
);
3742 if (es
->style
& ES_MULTILINE
) {
3744 IntersectClipRect(dc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
3747 old_font
= SelectObject(dc
, es
->font
);
3749 if (!es
->bEnableState
)
3750 SetTextColor(dc
, GetSysColor(COLOR_GRAYTEXT
));
3751 GetClipBox(dc
, &rcRgn
);
3752 if (es
->style
& ES_MULTILINE
) {
3753 INT vlc
= get_vertical_line_count(es
);
3754 for (i
= es
->y_offset
; i
<= min(es
->y_offset
+ vlc
, es
->y_offset
+ es
->line_count
- 1) ; i
++) {
3755 EDIT_UpdateUniscribeData(es
, dc
, i
);
3756 EDIT_GetLineRect(es
, i
, 0, -1, &rcLine
);
3757 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
3758 EDIT_PaintLine(es
, dc
, i
, rev
);
3761 EDIT_UpdateUniscribeData(es
, dc
, 0);
3762 EDIT_GetLineRect(es
, 0, 0, -1, &rcLine
);
3763 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
3764 EDIT_PaintLine(es
, dc
, 0, rev
);
3767 SelectObject(dc
, old_font
);
3770 EndPaint(es
->hwndSelf
, &ps
);
3774 /*********************************************************************
3779 static void EDIT_WM_SetFocus(EDITSTATE
*es
)
3781 es
->flags
|= EF_FOCUSED
;
3783 if (!(es
->style
& ES_NOHIDESEL
))
3784 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
3786 /* single line edit updates itself */
3787 if (IsWindowVisible(es
->hwndSelf
) && !(es
->style
& ES_MULTILINE
))
3789 HDC hdc
= GetDC(es
->hwndSelf
);
3790 EDIT_WM_Paint(es
, hdc
);
3791 ReleaseDC(es
->hwndSelf
, hdc
);
3794 CreateCaret(es
->hwndSelf
, 0, 1, es
->line_height
);
3795 EDIT_SetCaretPos(es
, es
->selection_end
,
3796 es
->flags
& EF_AFTER_WRAP
);
3797 ShowCaret(es
->hwndSelf
);
3798 EDIT_NOTIFY_PARENT(es
, EN_SETFOCUS
);
3802 /*********************************************************************
3806 * With Win95 look the margins are set to default font value unless
3807 * the system font (font == 0) is being set, in which case they are left
3811 static void EDIT_WM_SetFont(EDITSTATE
*es
, HFONT font
, BOOL redraw
)
3819 EDIT_InvalidateUniscribeData(es
);
3820 dc
= GetDC(es
->hwndSelf
);
3822 old_font
= SelectObject(dc
, font
);
3823 GetTextMetricsW(dc
, &tm
);
3824 es
->line_height
= tm
.tmHeight
;
3825 es
->char_width
= tm
.tmAveCharWidth
;
3827 SelectObject(dc
, old_font
);
3828 ReleaseDC(es
->hwndSelf
, dc
);
3830 /* Reset the format rect and the margins */
3831 GetClientRect(es
->hwndSelf
, &clientRect
);
3832 EDIT_SetRectNP(es
, &clientRect
);
3833 EDIT_EM_SetMargins(es
, EC_LEFTMARGIN
| EC_RIGHTMARGIN
,
3834 EC_USEFONTINFO
, EC_USEFONTINFO
, FALSE
);
3836 if (es
->style
& ES_MULTILINE
)
3837 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
3839 EDIT_CalcLineWidth_SL(es
);
3842 EDIT_UpdateText(es
, NULL
, TRUE
);
3843 if (es
->flags
& EF_FOCUSED
) {
3845 CreateCaret(es
->hwndSelf
, 0, 1, es
->line_height
);
3846 EDIT_SetCaretPos(es
, es
->selection_end
,
3847 es
->flags
& EF_AFTER_WRAP
);
3848 ShowCaret(es
->hwndSelf
);
3853 /*********************************************************************
3858 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3859 * The modified flag is reset. No notifications are sent.
3861 * For single-line controls, reception of WM_SETTEXT triggers:
3862 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3865 static void EDIT_WM_SetText(EDITSTATE
*es
, LPCWSTR text
, BOOL unicode
)
3867 LPWSTR textW
= NULL
;
3868 if (!unicode
&& text
)
3870 LPCSTR textA
= (LPCSTR
)text
;
3871 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
3872 textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
));
3874 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
3878 if (es
->flags
& EF_UPDATE
)
3879 /* fixed this bug once; complain if we see it about to happen again. */
3880 ERR("SetSel may generate UPDATE message whose handler may reset "
3883 EDIT_EM_SetSel(es
, 0, (UINT
)-1, FALSE
);
3886 TRACE("%s\n", debugstr_w(text
));
3887 EDIT_EM_ReplaceSel(es
, FALSE
, text
, FALSE
, FALSE
);
3889 HeapFree(GetProcessHeap(), 0, textW
);
3894 EDIT_EM_ReplaceSel(es
, FALSE
, empty_stringW
, FALSE
, FALSE
);
3897 es
->flags
&= ~EF_MODIFIED
;
3898 EDIT_EM_SetSel(es
, 0, 0, FALSE
);
3900 /* Send the notification after the selection start and end have been set
3901 * edit control doesn't send notification on WM_SETTEXT
3902 * if it is multiline, or it is part of combobox
3904 if( !((es
->style
& ES_MULTILINE
) || es
->hwndListBox
))
3906 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
3907 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
3909 EDIT_EM_ScrollCaret(es
);
3910 EDIT_UpdateScrollInfo(es
);
3911 EDIT_InvalidateUniscribeData(es
);
3915 /*********************************************************************
3920 static void EDIT_WM_Size(EDITSTATE
*es
, UINT action
)
3922 if ((action
== SIZE_MAXIMIZED
) || (action
== SIZE_RESTORED
)) {
3924 GetClientRect(es
->hwndSelf
, &rc
);
3925 EDIT_SetRectNP(es
, &rc
);
3926 EDIT_UpdateText(es
, NULL
, TRUE
);
3931 /*********************************************************************
3935 * This message is sent by SetWindowLong on having changed either the Style
3936 * or the extended style.
3938 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3940 * See also EDIT_WM_NCCreate
3942 * It appears that the Windows version of the edit control allows the style
3943 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3944 * style variable which will generally be different. In this function we
3945 * update the internal style based on what changed in the externally visible
3948 * Much of this content as based upon the MSDN, especially:
3949 * Platform SDK Documentation -> User Interface Services ->
3950 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3951 * Edit Control Styles
3953 static LRESULT
EDIT_WM_StyleChanged ( EDITSTATE
*es
, WPARAM which
, const STYLESTRUCT
*style
)
3955 if (GWL_STYLE
== which
) {
3956 DWORD style_change_mask
;
3958 /* Only a subset of changes can be applied after the control
3961 style_change_mask
= ES_UPPERCASE
| ES_LOWERCASE
|
3963 if (es
->style
& ES_MULTILINE
)
3964 style_change_mask
|= ES_WANTRETURN
;
3966 new_style
= style
->styleNew
& style_change_mask
;
3968 /* Number overrides lowercase overrides uppercase (at least it
3969 * does in Win95). However I'll bet that ES_NUMBER would be
3970 * invalid under Win 3.1.
3972 if (new_style
& ES_NUMBER
) {
3973 ; /* do not override the ES_NUMBER */
3974 } else if (new_style
& ES_LOWERCASE
) {
3975 new_style
&= ~ES_UPPERCASE
;
3978 es
->style
= (es
->style
& ~style_change_mask
) | new_style
;
3979 } else if (GWL_EXSTYLE
== which
) {
3980 ; /* FIXME - what is needed here */
3982 WARN ("Invalid style change %ld\n",which
);
3988 /*********************************************************************
3993 static LRESULT
EDIT_WM_SysKeyDown(EDITSTATE
*es
, INT key
, DWORD key_data
)
3995 if ((key
== VK_BACK
) && (key_data
& 0x2000)) {
3996 if (EDIT_EM_CanUndo(es
))
3999 } else if (key
== VK_UP
|| key
== VK_DOWN
) {
4000 if (EDIT_CheckCombo(es
, WM_SYSKEYDOWN
, key
))
4003 return DefWindowProcW(es
->hwndSelf
, WM_SYSKEYDOWN
, key
, key_data
);
4007 /*********************************************************************
4012 static void EDIT_WM_Timer(EDITSTATE
*es
)
4014 if (es
->region_posx
< 0) {
4015 EDIT_MoveBackward(es
, TRUE
);
4016 } else if (es
->region_posx
> 0) {
4017 EDIT_MoveForward(es
, TRUE
);
4020 * FIXME: gotta do some vertical scrolling here, like
4021 * EDIT_EM_LineScroll(hwnd, 0, 1);
4025 /*********************************************************************
4030 static LRESULT
EDIT_WM_HScroll(EDITSTATE
*es
, INT action
, INT pos
)
4035 if (!(es
->style
& ES_MULTILINE
))
4038 if (!(es
->style
& ES_AUTOHSCROLL
))
4042 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4045 TRACE("SB_LINELEFT\n");
4047 dx
= -es
->char_width
;
4050 TRACE("SB_LINERIGHT\n");
4051 if (es
->x_offset
< es
->text_width
)
4052 dx
= es
->char_width
;
4055 TRACE("SB_PAGELEFT\n");
4057 dx
= -fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4060 TRACE("SB_PAGERIGHT\n");
4061 if (es
->x_offset
< es
->text_width
)
4062 dx
= fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4070 TRACE("SB_RIGHT\n");
4071 if (es
->x_offset
< es
->text_width
)
4072 dx
= es
->text_width
- es
->x_offset
;
4075 TRACE("SB_THUMBTRACK %d\n", pos
);
4076 es
->flags
|= EF_HSCROLL_TRACK
;
4077 if(es
->style
& WS_HSCROLL
)
4078 dx
= pos
- es
->x_offset
;
4083 if(pos
< 0 || pos
> 100) return 0;
4084 /* Assume default scroll range 0-100 */
4085 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4086 new_x
= pos
* (es
->text_width
- fw
) / 100;
4087 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4090 case SB_THUMBPOSITION
:
4091 TRACE("SB_THUMBPOSITION %d\n", pos
);
4092 es
->flags
&= ~EF_HSCROLL_TRACK
;
4093 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4094 dx
= pos
- es
->x_offset
;
4099 if(pos
< 0 || pos
> 100) return 0;
4100 /* Assume default scroll range 0-100 */
4101 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4102 new_x
= pos
* (es
->text_width
- fw
) / 100;
4103 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4106 /* force scroll info update */
4107 EDIT_UpdateScrollInfo(es
);
4108 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
4112 TRACE("SB_ENDSCROLL\n");
4115 * FIXME : the next two are undocumented !
4116 * Are we doing the right thing ?
4117 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4118 * although it's also a regular control message.
4120 case EM_GETTHUMB
: /* this one is used by NT notepad */
4123 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4124 ret
= GetScrollPos(es
->hwndSelf
, SB_HORZ
);
4127 /* Assume default scroll range 0-100 */
4128 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4129 ret
= es
->text_width
? es
->x_offset
* 100 / (es
->text_width
- fw
) : 0;
4131 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4135 TRACE("EM_LINESCROLL16\n");
4140 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4146 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4147 /* check if we are going to move too far */
4148 if(es
->x_offset
+ dx
+ fw
> es
->text_width
)
4149 dx
= es
->text_width
- fw
- es
->x_offset
;
4151 EDIT_EM_LineScroll_internal(es
, dx
, 0);
4157 /*********************************************************************
4162 static LRESULT
EDIT_WM_VScroll(EDITSTATE
*es
, INT action
, INT pos
)
4166 if (!(es
->style
& ES_MULTILINE
))
4169 if (!(es
->style
& ES_AUTOVSCROLL
))
4178 TRACE("action %d (%s)\n", action
, (action
== SB_LINEUP
? "SB_LINEUP" :
4179 (action
== SB_LINEDOWN
? "SB_LINEDOWN" :
4180 (action
== SB_PAGEUP
? "SB_PAGEUP" :
4182 EDIT_EM_Scroll(es
, action
);
4189 TRACE("SB_BOTTOM\n");
4190 dy
= es
->line_count
- 1 - es
->y_offset
;
4193 TRACE("SB_THUMBTRACK %d\n", pos
);
4194 es
->flags
|= EF_VSCROLL_TRACK
;
4195 if(es
->style
& WS_VSCROLL
)
4196 dy
= pos
- es
->y_offset
;
4199 /* Assume default scroll range 0-100 */
4202 if(pos
< 0 || pos
> 100) return 0;
4203 vlc
= get_vertical_line_count(es
);
4204 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4205 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4206 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4207 es
->line_count
, es
->y_offset
, pos
, dy
);
4210 case SB_THUMBPOSITION
:
4211 TRACE("SB_THUMBPOSITION %d\n", pos
);
4212 es
->flags
&= ~EF_VSCROLL_TRACK
;
4213 if(es
->style
& WS_VSCROLL
)
4214 dy
= pos
- es
->y_offset
;
4217 /* Assume default scroll range 0-100 */
4220 if(pos
< 0 || pos
> 100) return 0;
4221 vlc
= get_vertical_line_count(es
);
4222 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4223 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4224 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4225 es
->line_count
, es
->y_offset
, pos
, dy
);
4229 /* force scroll info update */
4230 EDIT_UpdateScrollInfo(es
);
4231 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
4235 TRACE("SB_ENDSCROLL\n");
4238 * FIXME : the next two are undocumented !
4239 * Are we doing the right thing ?
4240 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4241 * although it's also a regular control message.
4243 case EM_GETTHUMB
: /* this one is used by NT notepad */
4246 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_VSCROLL
)
4247 ret
= GetScrollPos(es
->hwndSelf
, SB_VERT
);
4250 /* Assume default scroll range 0-100 */
4251 INT vlc
= get_vertical_line_count(es
);
4252 ret
= es
->line_count
? es
->y_offset
* 100 / (es
->line_count
- vlc
) : 0;
4254 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4258 TRACE("EM_LINESCROLL %d\n", pos
);
4263 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4268 EDIT_EM_LineScroll(es
, 0, dy
);
4272 /*********************************************************************
4276 * FIXME: is this right ? (or should it be only VSCROLL)
4277 * (and maybe only for edit controls that really have their
4278 * own scrollbars) (and maybe only for multiline controls ?)
4279 * All in all: very poorly documented
4282 static LRESULT
EDIT_EM_GetThumb(EDITSTATE
*es
)
4284 return MAKELONG(EDIT_WM_VScroll(es
, EM_GETTHUMB
, 0),
4285 EDIT_WM_HScroll(es
, EM_GETTHUMB
, 0));
4289 /********************************************************************
4291 * The Following code is to handle inline editing from IMEs
4294 static void EDIT_GetCompositionStr(HIMC hIMC
, LPARAM CompFlag
, EDITSTATE
*es
)
4298 LPSTR lpCompStrAttr
= NULL
;
4301 buflen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
4308 lpCompStr
= HeapAlloc(GetProcessHeap(),0,buflen
+ sizeof(WCHAR
));
4311 ERR("Unable to allocate IME CompositionString\n");
4316 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, buflen
);
4317 lpCompStr
[buflen
/sizeof(WCHAR
)] = 0;
4319 if (CompFlag
& GCS_COMPATTR
)
4322 * We do not use the attributes yet. it would tell us what characters
4323 * are in transition and which are converted or decided upon
4325 dwBufLenAttr
= ImmGetCompositionStringW(hIMC
, GCS_COMPATTR
, NULL
, 0);
4329 lpCompStrAttr
= HeapAlloc(GetProcessHeap(),0,dwBufLenAttr
+1);
4332 ERR("Unable to allocate IME Attribute String\n");
4333 HeapFree(GetProcessHeap(),0,lpCompStr
);
4336 ImmGetCompositionStringW(hIMC
,GCS_COMPATTR
, lpCompStrAttr
,
4338 lpCompStrAttr
[dwBufLenAttr
] = 0;
4342 /* check for change in composition start */
4343 if (es
->selection_end
< es
->composition_start
)
4344 es
->composition_start
= es
->selection_end
;
4346 /* replace existing selection string */
4347 es
->selection_start
= es
->composition_start
;
4349 if (es
->composition_len
> 0)
4350 es
->selection_end
= es
->composition_start
+ es
->composition_len
;
4352 es
->selection_end
= es
->selection_start
;
4354 EDIT_EM_ReplaceSel(es
, FALSE
, lpCompStr
, TRUE
, TRUE
);
4355 es
->composition_len
= abs(es
->composition_start
- es
->selection_end
);
4357 es
->selection_start
= es
->composition_start
;
4358 es
->selection_end
= es
->selection_start
+ es
->composition_len
;
4360 HeapFree(GetProcessHeap(),0,lpCompStrAttr
);
4361 HeapFree(GetProcessHeap(),0,lpCompStr
);
4364 static void EDIT_GetResultStr(HIMC hIMC
, EDITSTATE
*es
)
4369 buflen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
4375 lpResultStr
= HeapAlloc(GetProcessHeap(),0, buflen
+sizeof(WCHAR
));
4378 ERR("Unable to alloc buffer for IME string\n");
4382 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpResultStr
, buflen
);
4383 lpResultStr
[buflen
/sizeof(WCHAR
)] = 0;
4385 /* check for change in composition start */
4386 if (es
->selection_end
< es
->composition_start
)
4387 es
->composition_start
= es
->selection_end
;
4389 es
->selection_start
= es
->composition_start
;
4390 es
->selection_end
= es
->composition_start
+ es
->composition_len
;
4391 EDIT_EM_ReplaceSel(es
, TRUE
, lpResultStr
, TRUE
, TRUE
);
4392 es
->composition_start
= es
->selection_end
;
4393 es
->composition_len
= 0;
4395 HeapFree(GetProcessHeap(),0,lpResultStr
);
4398 static void EDIT_ImeComposition(HWND hwnd
, LPARAM CompFlag
, EDITSTATE
*es
)
4403 if (es
->composition_len
== 0 && es
->selection_start
!= es
->selection_end
)
4405 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
4406 es
->composition_start
= es
->selection_end
;
4409 hIMC
= ImmGetContext(hwnd
);
4413 if (CompFlag
& GCS_RESULTSTR
)
4415 EDIT_GetResultStr(hIMC
, es
);
4420 if (CompFlag
& GCS_COMPSTR
)
4421 EDIT_GetCompositionStr(hIMC
, CompFlag
, es
);
4422 cursor
= ImmGetCompositionStringW(hIMC
, GCS_CURSORPOS
, 0, 0);
4424 ImmReleaseContext(hwnd
, hIMC
);
4425 EDIT_SetCaretPos(es
, es
->selection_start
+ cursor
, es
->flags
& EF_AFTER_WRAP
);
4429 /*********************************************************************
4433 * See also EDIT_WM_StyleChanged
4435 static LRESULT
EDIT_WM_NCCreate(HWND hwnd
, LPCREATESTRUCTW lpcs
, BOOL unicode
)
4440 TRACE("Creating %s edit control, style = %08x\n",
4441 unicode
? "Unicode" : "ANSI", lpcs
->style
);
4443 if (!(es
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*es
))))
4445 SetWindowLongPtrW( hwnd
, 0, (LONG_PTR
)es
);
4448 * Note: since the EDITSTATE has not been fully initialized yet,
4449 * we can't use any API calls that may send
4450 * WM_XXX messages before WM_NCCREATE is completed.
4453 es
->is_unicode
= unicode
;
4454 es
->style
= lpcs
->style
;
4456 es
->bEnableState
= !(es
->style
& WS_DISABLED
);
4458 es
->hwndSelf
= hwnd
;
4459 /* Save parent, which will be notified by EN_* messages */
4460 es
->hwndParent
= lpcs
->hwndParent
;
4462 if (es
->style
& ES_COMBO
)
4463 es
->hwndListBox
= GetDlgItem(es
->hwndParent
, ID_CB_LISTBOX
);
4465 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4466 if (lpcs
->dwExStyle
& WS_EX_RIGHT
) es
->style
|= ES_RIGHT
;
4468 /* Number overrides lowercase overrides uppercase (at least it
4469 * does in Win95). However I'll bet that ES_NUMBER would be
4470 * invalid under Win 3.1.
4472 if (es
->style
& ES_NUMBER
) {
4473 ; /* do not override the ES_NUMBER */
4474 } else if (es
->style
& ES_LOWERCASE
) {
4475 es
->style
&= ~ES_UPPERCASE
;
4477 if (es
->style
& ES_MULTILINE
) {
4478 es
->buffer_limit
= BUFLIMIT_INITIAL
;
4479 if (es
->style
& WS_VSCROLL
)
4480 es
->style
|= ES_AUTOVSCROLL
;
4481 if (es
->style
& WS_HSCROLL
)
4482 es
->style
|= ES_AUTOHSCROLL
;
4483 es
->style
&= ~ES_PASSWORD
;
4484 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
)) {
4485 /* Confirmed - RIGHT overrides CENTER */
4486 if (es
->style
& ES_RIGHT
)
4487 es
->style
&= ~ES_CENTER
;
4488 es
->style
&= ~WS_HSCROLL
;
4489 es
->style
&= ~ES_AUTOHSCROLL
;
4492 es
->buffer_limit
= BUFLIMIT_INITIAL
;
4493 if ((es
->style
& ES_RIGHT
) && (es
->style
& ES_CENTER
))
4494 es
->style
&= ~ES_CENTER
;
4495 es
->style
&= ~WS_HSCROLL
;
4496 es
->style
&= ~WS_VSCROLL
;
4497 if (es
->style
& ES_PASSWORD
)
4498 es
->password_char
= '*';
4501 alloc_size
= ROUND_TO_GROW((es
->buffer_size
+ 1) * sizeof(WCHAR
));
4502 if(!(es
->hloc32W
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
4504 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
4506 if (!(es
->undo_text
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (es
->buffer_size
+ 1) * sizeof(WCHAR
))))
4508 es
->undo_buffer_size
= es
->buffer_size
;
4510 if (es
->style
& ES_MULTILINE
)
4511 if (!(es
->first_line_def
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
))))
4516 * In Win95 look and feel, the WS_BORDER style is replaced by the
4517 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4518 * control a nonclient area so we don't need to draw the border.
4519 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4520 * a nonclient area and we should handle painting the border ourselves.
4522 * When making modifications please ensure that the code still works
4523 * for edit controls created directly with style 0x50800000, exStyle 0
4524 * (which should have a single pixel border)
4526 if (lpcs
->dwExStyle
& WS_EX_CLIENTEDGE
)
4527 es
->style
&= ~WS_BORDER
;
4528 else if (es
->style
& WS_BORDER
)
4529 SetWindowLongW(hwnd
, GWL_STYLE
, es
->style
& ~WS_BORDER
);
4534 SetWindowLongPtrW(es
->hwndSelf
, 0, 0);
4535 EDIT_InvalidateUniscribeData(es
);
4536 HeapFree(GetProcessHeap(), 0, es
->first_line_def
);
4537 HeapFree(GetProcessHeap(), 0, es
->undo_text
);
4538 if (es
->hloc32W
) LocalFree(es
->hloc32W
);
4539 HeapFree(GetProcessHeap(), 0, es
->logAttr
);
4540 HeapFree(GetProcessHeap(), 0, es
);
4545 /*********************************************************************
4550 static LRESULT
EDIT_WM_Create(EDITSTATE
*es
, LPCWSTR name
)
4554 TRACE("%s\n", debugstr_w(name
));
4556 * To initialize some final structure members, we call some helper
4557 * functions. However, since the EDITSTATE is not consistent (i.e.
4558 * not fully initialized), we should be very careful which
4559 * functions can be called, and in what order.
4561 EDIT_WM_SetFont(es
, 0, FALSE
);
4562 EDIT_EM_EmptyUndoBuffer(es
);
4564 /* We need to calculate the format rect
4565 (applications may send EM_SETMARGINS before the control gets visible) */
4566 GetClientRect(es
->hwndSelf
, &clientRect
);
4567 EDIT_SetRectNP(es
, &clientRect
);
4569 if (name
&& *name
) {
4570 EDIT_EM_ReplaceSel(es
, FALSE
, name
, FALSE
, FALSE
);
4571 /* if we insert text to the editline, the text scrolls out
4572 * of the window, as the caret is placed after the insert
4573 * pos normally; thus we reset es->selection... to 0 and
4576 es
->selection_start
= es
->selection_end
= 0;
4577 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4578 * Messages are only to be sent when the USER does something to
4579 * change the contents. So I am removing this EN_CHANGE
4581 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4583 EDIT_EM_ScrollCaret(es
);
4585 /* force scroll info update */
4586 EDIT_UpdateScrollInfo(es
);
4587 /* The rule seems to return 1 here for success */
4588 /* Power Builder masked edit controls will crash */
4590 /* FIXME: is that in all cases so ? */
4595 /*********************************************************************
4600 static LRESULT
EDIT_WM_NCDestroy(EDITSTATE
*es
)
4604 /* The app can own the text buffer handle */
4605 if (es
->hloc32W
&& (es
->hloc32W
!= es
->hlocapp
)) {
4606 LocalFree(es
->hloc32W
);
4608 if (es
->hloc32A
&& (es
->hloc32A
!= es
->hlocapp
)) {
4609 LocalFree(es
->hloc32A
);
4611 EDIT_InvalidateUniscribeData(es
);
4612 pc
= es
->first_line_def
;
4616 HeapFree(GetProcessHeap(), 0, pc
);
4620 SetWindowLongPtrW( es
->hwndSelf
, 0, 0 );
4621 HeapFree(GetProcessHeap(), 0, es
->undo_text
);
4622 HeapFree(GetProcessHeap(), 0, es
);
4628 static inline LRESULT
DefWindowProcT(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
4631 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
4633 return DefWindowProcA(hwnd
, msg
, wParam
, lParam
);
4636 /*********************************************************************
4638 * EditWndProc_common
4640 * The messages are in the order of the actual integer values
4641 * (which can be found in include/windows.h)
4643 LRESULT
EditWndProc_common( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
4645 EDITSTATE
*es
= (EDITSTATE
*)GetWindowLongPtrW( hwnd
, 0 );
4648 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), wParam
, lParam
);
4650 if (!es
&& msg
!= WM_NCCREATE
)
4651 return DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
4653 if (es
&& (msg
!= WM_NCDESTROY
)) EDIT_LockBuffer(es
);
4657 result
= EDIT_EM_GetSel(es
, (PUINT
)wParam
, (PUINT
)lParam
);
4661 EDIT_EM_SetSel(es
, wParam
, lParam
, FALSE
);
4662 EDIT_EM_ScrollCaret(es
);
4668 *((LPRECT
)lParam
) = es
->format_rect
;
4672 if ((es
->style
& ES_MULTILINE
) && lParam
) {
4673 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
4674 EDIT_UpdateText(es
, NULL
, TRUE
);
4679 if ((es
->style
& ES_MULTILINE
) && lParam
)
4680 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
4684 result
= EDIT_EM_Scroll(es
, (INT
)wParam
);
4688 result
= (LRESULT
)EDIT_EM_LineScroll(es
, (INT
)wParam
, (INT
)lParam
);
4691 case EM_SCROLLCARET
:
4692 EDIT_EM_ScrollCaret(es
);
4697 result
= ((es
->flags
& EF_MODIFIED
) != 0);
4702 es
->flags
|= EF_MODIFIED
;
4704 es
->flags
&= ~(EF_MODIFIED
| EF_UPDATE
); /* reset pending updates */
4707 case EM_GETLINECOUNT
:
4708 result
= (es
->style
& ES_MULTILINE
) ? es
->line_count
: 1;
4712 result
= (LRESULT
)EDIT_EM_LineIndex(es
, (INT
)wParam
);
4716 EDIT_EM_SetHandle(es
, (HLOCAL
)wParam
);
4720 result
= (LRESULT
)EDIT_EM_GetHandle(es
);
4724 result
= EDIT_EM_GetThumb(es
);
4727 /* these messages missing from specs */
4732 FIXME("undocumented message 0x%x, please report\n", msg
);
4733 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
4737 result
= (LRESULT
)EDIT_EM_LineLength(es
, (INT
)wParam
);
4745 textW
= (LPWSTR
)lParam
;
4748 LPSTR textA
= (LPSTR
)lParam
;
4749 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
4750 if (!(textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
)))) break;
4751 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
4754 EDIT_EM_ReplaceSel(es
, (BOOL
)wParam
, textW
, TRUE
, TRUE
);
4758 HeapFree(GetProcessHeap(), 0, textW
);
4763 result
= (LRESULT
)EDIT_EM_GetLine(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
4766 case EM_SETLIMITTEXT
:
4767 EDIT_EM_SetLimitText(es
, wParam
);
4771 result
= (LRESULT
)EDIT_EM_CanUndo(es
);
4776 result
= (LRESULT
)EDIT_EM_Undo(es
);
4780 result
= (LRESULT
)EDIT_EM_FmtLines(es
, (BOOL
)wParam
);
4783 case EM_LINEFROMCHAR
:
4784 result
= (LRESULT
)EDIT_EM_LineFromChar(es
, (INT
)wParam
);
4787 case EM_SETTABSTOPS
:
4788 result
= (LRESULT
)EDIT_EM_SetTabStops(es
, (INT
)wParam
, (LPINT
)lParam
);
4791 case EM_SETPASSWORDCHAR
:
4796 charW
= (WCHAR
)wParam
;
4799 CHAR charA
= wParam
;
4800 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
4803 EDIT_EM_SetPasswordChar(es
, charW
);
4807 case EM_EMPTYUNDOBUFFER
:
4808 EDIT_EM_EmptyUndoBuffer(es
);
4811 case EM_GETFIRSTVISIBLELINE
:
4812 result
= (es
->style
& ES_MULTILINE
) ? es
->y_offset
: es
->x_offset
;
4815 case EM_SETREADONLY
:
4817 DWORD old_style
= es
->style
;
4820 SetWindowLongW( hwnd
, GWL_STYLE
,
4821 GetWindowLongW( hwnd
, GWL_STYLE
) | ES_READONLY
);
4822 es
->style
|= ES_READONLY
;
4824 SetWindowLongW( hwnd
, GWL_STYLE
,
4825 GetWindowLongW( hwnd
, GWL_STYLE
) & ~ES_READONLY
);
4826 es
->style
&= ~ES_READONLY
;
4829 if (old_style
^ es
->style
)
4830 InvalidateRect(es
->hwndSelf
, NULL
, TRUE
);
4836 case EM_SETWORDBREAKPROC
:
4837 EDIT_EM_SetWordBreakProc(es
, (void *)lParam
);
4840 case EM_GETWORDBREAKPROC
:
4841 result
= (LRESULT
)es
->word_break_proc
;
4844 case EM_GETPASSWORDCHAR
:
4847 result
= es
->password_char
;
4850 WCHAR charW
= es
->password_char
;
4852 WideCharToMultiByte(CP_ACP
, 0, &charW
, 1, &charA
, 1, NULL
, NULL
);
4859 EDIT_EM_SetMargins(es
, (INT
)wParam
, LOWORD(lParam
), HIWORD(lParam
), TRUE
);
4863 result
= MAKELONG(es
->left_margin
, es
->right_margin
);
4866 case EM_GETLIMITTEXT
:
4867 result
= es
->buffer_limit
;
4870 case EM_POSFROMCHAR
:
4871 if ((INT
)wParam
>= get_text_length(es
)) result
= -1;
4872 else result
= EDIT_EM_PosFromChar(es
, (INT
)wParam
, FALSE
);
4875 case EM_CHARFROMPOS
:
4876 result
= EDIT_EM_CharFromPos(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4879 /* End of the EM_ messages which were in numerical order; what order
4880 * are these in? vaguely alphabetical?
4884 result
= EDIT_WM_NCCreate(hwnd
, (LPCREATESTRUCTW
)lParam
, unicode
);
4888 result
= EDIT_WM_NCDestroy(es
);
4893 result
= DLGC_HASSETSEL
| DLGC_WANTCHARS
| DLGC_WANTARROWS
;
4895 if (es
->style
& ES_MULTILINE
)
4896 result
|= DLGC_WANTALLKEYS
;
4900 es
->flags
|=EF_DIALOGMODE
;
4902 if (((LPMSG
)lParam
)->message
== WM_KEYDOWN
)
4904 int vk
= (int)((LPMSG
)lParam
)->wParam
;
4906 if (es
->hwndListBox
)
4908 if (vk
== VK_RETURN
|| vk
== VK_ESCAPE
)
4909 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
4910 result
|= DLGC_WANTMESSAGE
;
4922 strng
[0] = wParam
>> 8;
4923 strng
[1] = wParam
& 0xff;
4924 if (strng
[0]) MultiByteToWideChar(CP_ACP
, 0, strng
, 2, &charW
, 1);
4925 else MultiByteToWideChar(CP_ACP
, 0, &strng
[1], 1, &charW
, 1);
4926 result
= EDIT_WM_Char(es
, charW
);
4938 CHAR charA
= wParam
;
4939 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
4942 if (es
->hwndListBox
)
4944 if (charW
== VK_RETURN
|| charW
== VK_ESCAPE
)
4946 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
4947 SendMessageW(GetParent(hwnd
), WM_KEYDOWN
, charW
, 0);
4951 result
= EDIT_WM_Char(es
, charW
);
4958 if (wParam
== UNICODE_NOCHAR
) return TRUE
;
4959 if (wParam
<= 0x000fffff)
4961 if(wParam
> 0xffff) /* convert to surrogates */
4964 EDIT_WM_Char(es
, (wParam
>> 10) + 0xd800);
4965 EDIT_WM_Char(es
, (wParam
& 0x03ff) + 0xdc00);
4967 else EDIT_WM_Char(es
, wParam
);
4977 case WM_CONTEXTMENU
:
4978 EDIT_WM_ContextMenu(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4987 result
= EDIT_WM_Create(es
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
4990 LPCSTR nameA
= ((LPCREATESTRUCTA
)lParam
)->lpszName
;
4991 LPWSTR nameW
= NULL
;
4994 INT countW
= MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, NULL
, 0);
4995 if((nameW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
))))
4996 MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, nameW
, countW
);
4998 result
= EDIT_WM_Create(es
, nameW
);
4999 HeapFree(GetProcessHeap(), 0, nameW
);
5008 es
->bEnableState
= (BOOL
) wParam
;
5009 EDIT_UpdateText(es
, NULL
, TRUE
);
5013 /* we do the proper erase in EDIT_WM_Paint */
5018 result
= (LRESULT
)es
->font
;
5022 result
= (LRESULT
)EDIT_WM_GetText(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
5025 case WM_GETTEXTLENGTH
:
5026 if (unicode
) result
= get_text_length(es
);
5027 else result
= WideCharToMultiByte( CP_ACP
, 0, es
->text
, get_text_length(es
),
5028 NULL
, 0, NULL
, NULL
);
5032 result
= EDIT_WM_HScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
5036 result
= EDIT_WM_KeyDown(es
, (INT
)wParam
);
5040 result
= EDIT_WM_KillFocus(es
);
5043 case WM_LBUTTONDBLCLK
:
5044 result
= EDIT_WM_LButtonDblClk(es
);
5047 case WM_LBUTTONDOWN
:
5048 result
= EDIT_WM_LButtonDown(es
, wParam
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5052 result
= EDIT_WM_LButtonUp(es
);
5055 case WM_MBUTTONDOWN
:
5056 result
= EDIT_WM_MButtonDown(es
);
5060 result
= EDIT_WM_MouseMove(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5063 case WM_PRINTCLIENT
:
5065 EDIT_WM_Paint(es
, (HDC
)wParam
);
5073 EDIT_WM_SetFocus(es
);
5077 EDIT_WM_SetFont(es
, (HFONT
)wParam
, LOWORD(lParam
) != 0);
5081 /* FIXME: actually set an internal flag and behave accordingly */
5085 EDIT_WM_SetText(es
, (LPCWSTR
)lParam
, unicode
);
5090 EDIT_WM_Size(es
, (UINT
)wParam
);
5093 case WM_STYLECHANGED
:
5094 result
= EDIT_WM_StyleChanged(es
, wParam
, (const STYLESTRUCT
*)lParam
);
5097 case WM_STYLECHANGING
:
5098 result
= 0; /* See EDIT_WM_StyleChanged */
5102 result
= EDIT_WM_SysKeyDown(es
, (INT
)wParam
, (DWORD
)lParam
);
5110 result
= EDIT_WM_VScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
5116 UINT pulScrollLines
= 3;
5117 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
5119 if (wParam
& (MK_SHIFT
| MK_CONTROL
)) {
5120 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
5123 wheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
5124 /* if scrolling changes direction, ignore left overs */
5125 if ((wheelDelta
< 0 && es
->wheelDeltaRemainder
< 0) ||
5126 (wheelDelta
> 0 && es
->wheelDeltaRemainder
> 0))
5127 es
->wheelDeltaRemainder
+= wheelDelta
;
5129 es
->wheelDeltaRemainder
= wheelDelta
;
5130 if (es
->wheelDeltaRemainder
&& pulScrollLines
)
5133 pulScrollLines
= (int) min((UINT
) es
->line_count
, pulScrollLines
);
5134 cLineScroll
= pulScrollLines
* (float)es
->wheelDeltaRemainder
/ WHEEL_DELTA
;
5135 es
->wheelDeltaRemainder
-= WHEEL_DELTA
* cLineScroll
/ (int)pulScrollLines
;
5136 result
= EDIT_EM_LineScroll(es
, 0, -cLineScroll
);
5142 /* IME messages to make the edit control IME aware */
5143 case WM_IME_SETCONTEXT
:
5146 case WM_IME_STARTCOMPOSITION
:
5147 es
->composition_start
= es
->selection_end
;
5148 es
->composition_len
= 0;
5151 case WM_IME_COMPOSITION
:
5152 EDIT_ImeComposition(hwnd
, lParam
, es
);
5155 case WM_IME_ENDCOMPOSITION
:
5156 if (es
->composition_len
> 0)
5158 EDIT_EM_ReplaceSel(es
, TRUE
, empty_stringW
, TRUE
, TRUE
);
5159 es
->selection_end
= es
->selection_start
;
5160 es
->composition_len
= 0;
5164 case WM_IME_COMPOSITIONFULL
:
5170 case WM_IME_CONTROL
:
5173 case WM_IME_REQUEST
:
5176 case IMR_QUERYCHARPOSITION
:
5179 IMECHARPOSITION
*chpos
= (IMECHARPOSITION
*)lParam
;
5181 pos
= EDIT_EM_PosFromChar(es
, es
->selection_start
+ chpos
->dwCharPos
, FALSE
);
5182 chpos
->pt
.x
= LOWORD(pos
);
5183 chpos
->pt
.y
= HIWORD(pos
);
5184 chpos
->cLineHeight
= es
->line_height
;
5185 chpos
->rcDocument
= es
->format_rect
;
5186 MapWindowPoints(hwnd
, 0, &chpos
->pt
, 1);
5187 MapWindowPoints(hwnd
, 0, (POINT
*)&chpos
->rcDocument
, 2);
5195 result
= DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
5199 if (IsWindow(hwnd
) && es
) EDIT_UnlockBuffer(es
, FALSE
);
5201 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), result
);
5207 /*********************************************************************
5208 * edit class descriptor
5210 static const WCHAR editW
[] = {'E','d','i','t',0};
5211 const struct builtin_class_descr EDIT_builtin_class
=
5214 CS_DBLCLKS
| CS_PARENTDC
, /* style */
5215 WINPROC_EDIT
, /* proc */
5217 sizeof(EDITSTATE
*) + sizeof(WORD
), /* extra */
5219 sizeof(EDITSTATE
*), /* extra */
5221 IDC_IBEAM
, /* cursor */