4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * - EDITBALLOONTIP structure
25 * - EM_GETCUEBANNER/Edit_GetCueBannerText
26 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
27 * - EM_SETCUEBANNER/Edit_SetCueBannerText
28 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
29 * - EM_GETIMESTATUS, EM_SETIMESTATUS
48 #include "wine/unicode.h"
50 #include "user_private.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(edit
);
54 WINE_DECLARE_DEBUG_CHANNEL(combo
);
55 WINE_DECLARE_DEBUG_CHANNEL(relay
);
57 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
58 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
59 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
60 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
63 * extra flags for EDITSTATE.flags field
65 #define EF_MODIFIED 0x0001 /* text has been modified */
66 #define EF_FOCUSED 0x0002 /* we have input focus */
67 #define EF_UPDATE 0x0004 /* notify parent of changed state */
68 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
69 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
70 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
71 wrapped line, instead of in front of the next character */
72 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
73 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
77 END_0
= 0, /* line ends with terminating '\0' character */
78 END_WRAP
, /* line is wrapped */
79 END_HARD
, /* line ends with a hard return '\r\n' */
80 END_SOFT
, /* line ends with a soft return '\r\r\n' */
81 END_RICH
/* line ends with a single '\n' */
84 typedef struct tagLINEDEF
{
85 INT length
; /* bruto length of a line in bytes */
86 INT net_length
; /* netto length of a line in visible characters */
88 INT width
; /* width of the line in pixels */
89 INT index
; /* line index into the buffer */
90 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data */
91 struct tagLINEDEF
*next
;
96 BOOL is_unicode
; /* how the control was created */
97 LPWSTR text
; /* the actual contents of the control */
98 UINT text_length
; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
99 UINT buffer_size
; /* the size of the buffer in characters */
100 UINT buffer_limit
; /* the maximum size to which the buffer may grow in characters */
101 HFONT font
; /* NULL means standard system font */
102 INT x_offset
; /* scroll offset for multi lines this is in pixels
103 for single lines it's in characters */
104 INT line_height
; /* height of a screen line in pixels */
105 INT char_width
; /* average character width in pixels */
106 DWORD style
; /* sane version of wnd->dwStyle */
107 WORD flags
; /* flags that are not in es->style or wnd->flags (EF_XXX) */
108 INT undo_insert_count
; /* number of characters inserted in sequence */
109 UINT undo_position
; /* character index of the insertion and deletion */
110 LPWSTR undo_text
; /* deleted text */
111 UINT undo_buffer_size
; /* size of the deleted text buffer */
112 INT selection_start
; /* == selection_end if no selection */
113 INT selection_end
; /* == current caret position */
114 WCHAR password_char
; /* == 0 if no password char, and for multi line controls */
115 INT left_margin
; /* in pixels */
116 INT right_margin
; /* in pixels */
118 INT text_width
; /* width of the widest line in pixels for multi line controls
119 and just line width for single line controls */
120 INT region_posx
; /* Position of cursor relative to region: */
121 INT region_posy
; /* -1: to left, 0: within, 1: to right */
122 void *word_break_proc
; /* 32-bit word break proc: ANSI or Unicode */
123 INT line_count
; /* number of lines */
124 INT y_offset
; /* scroll offset in number of lines */
125 BOOL bCaptureState
; /* flag indicating whether mouse was captured */
126 BOOL bEnableState
; /* flag keeping the enable state */
127 HWND hwndSelf
; /* the our window handle */
128 HWND hwndParent
; /* Handle of parent for sending EN_* messages.
129 Even if parent will change, EN_* messages
130 should be sent to the first parent. */
131 HWND hwndListBox
; /* handle of ComboBox's listbox or NULL */
132 INT wheelDeltaRemainder
; /* scroll wheel delta left over after scrolling whole lines */
134 * only for multi line controls
136 INT lock_count
; /* amount of re-entries in the EditWndProc */
139 LINEDEF
*first_line_def
; /* linked list of (soft) linebreaks */
140 HLOCAL hloc32W
; /* our unicode local memory block */
141 HLOCAL hloc32A
; /* alias for ANSI control receiving EM_GETHANDLE
143 HLOCAL hlocapp
; /* The text buffer handle belongs to the app */
147 UINT composition_len
; /* length of composition, 0 == no composition */
148 int composition_start
; /* the character position for the composition */
152 SCRIPT_LOGATTR
*logAttr
;
153 SCRIPT_STRING_ANALYSIS ssa
; /* Uniscribe Data for single line controls */
157 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
158 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
160 /* used for disabled or read-only edit control */
161 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
163 { /* Notify parent which has created this edit control */ \
164 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
165 SendMessageW(es->hwndParent, WM_COMMAND, \
166 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
167 (LPARAM)(es->hwndSelf)); \
170 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
);
172 /*********************************************************************
177 static inline BOOL
EDIT_EM_CanUndo(const EDITSTATE
*es
)
179 return (es
->undo_insert_count
|| strlenW(es
->undo_text
));
183 /*********************************************************************
188 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE
*es
)
190 es
->undo_insert_count
= 0;
191 *es
->undo_text
= '\0';
195 /**********************************************************************
198 * Returns the window version in case Wine emulates a later version
199 * of windows than the application expects.
201 * In a number of cases when windows runs an application that was
202 * designed for an earlier windows version, windows reverts
203 * to "old" behaviour of that earlier version.
205 * An example is a disabled edit control that needs to be painted.
206 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
207 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
208 * applications with an expected version 0f 4.0 or higher.
211 static DWORD
get_app_version(void)
213 static DWORD version
;
216 DWORD dwEmulatedVersion
;
218 DWORD dwProcVersion
= GetProcessVersion(0);
220 info
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOW
);
221 GetVersionExW( &info
);
222 dwEmulatedVersion
= MAKELONG( info
.dwMinorVersion
, info
.dwMajorVersion
);
223 /* FIXME: this may not be 100% correct; see discussion on the
224 * wine developer list in Nov 1999 */
225 version
= dwProcVersion
< dwEmulatedVersion
? dwProcVersion
: dwEmulatedVersion
;
230 static HBRUSH
EDIT_NotifyCtlColor(EDITSTATE
*es
, HDC hdc
)
235 if ( get_app_version() >= 0x40000 && (!es
->bEnableState
|| (es
->style
& ES_READONLY
)))
236 msg
= WM_CTLCOLORSTATIC
;
238 msg
= WM_CTLCOLOREDIT
;
240 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
241 hbrush
= (HBRUSH
)SendMessageW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
243 hbrush
= (HBRUSH
)DefWindowProcW(GetParent(es
->hwndSelf
), msg
, (WPARAM
)hdc
, (LPARAM
)es
->hwndSelf
);
248 static inline UINT
get_text_length(EDITSTATE
*es
)
250 if(es
->text_length
== (UINT
)-1)
251 es
->text_length
= strlenW(es
->text
);
252 return es
->text_length
;
256 /*********************************************************************
260 * Find the beginning of words.
261 * Note: unlike the specs for a WordBreakProc, this function can
262 * only be called without linebreaks between s[0] up to
263 * s[count - 1]. Remember it is only called
264 * internally, so we can decide this for ourselves.
265 * Additionally we will always be breaking the full string.
268 static INT
EDIT_WordBreakProc(EDITSTATE
*es
, LPWSTR s
, INT index
, INT count
, INT action
)
272 TRACE("s=%p, index=%d, count=%d, action=%d\n", s
, index
, count
, action
);
280 memset(&psa
,0,sizeof(SCRIPT_ANALYSIS
));
281 psa
.eScript
= SCRIPT_UNDEFINED
;
283 es
->logAttr
= HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR
) * get_text_length(es
));
284 ScriptBreak(es
->text
, get_text_length(es
), &psa
, es
->logAttr
);
291 while (index
&& !es
->logAttr
[index
].fSoftBreak
)
298 while (index
< count
&& s
[index
] && !es
->logAttr
[index
].fSoftBreak
)
303 ret
= es
->logAttr
[index
].fWhiteSpace
;
306 ERR("unknown action code, please report !\n");
313 /*********************************************************************
315 * EDIT_CallWordBreakProc
317 * Call appropriate WordBreakProc (internal or external).
319 * Note: The "start" argument should always be an index referring
320 * to es->text. The actual wordbreak proc might be
321 * 16 bit, so we can't always pass any 32 bit LPSTR.
322 * Hence we assume that es->text is the buffer that holds
323 * the string under examination (we can decide this for ourselves).
326 static INT
EDIT_CallWordBreakProc(EDITSTATE
*es
, INT start
, INT index
, INT count
, INT action
)
330 if (es
->word_break_proc
)
334 EDITWORDBREAKPROCW wbpW
= (EDITWORDBREAKPROCW
)es
->word_break_proc
;
336 TRACE_(relay
)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
337 es
->word_break_proc
, debugstr_wn(es
->text
+ start
, count
), index
, count
, action
);
338 ret
= wbpW(es
->text
+ start
, index
, count
, action
);
339 TRACE_(relay
)("\1Ret wordbreakW %p retval=%d\n", es
->word_break_proc
, ret
);
343 EDITWORDBREAKPROCA wbpA
= (EDITWORDBREAKPROCA
)es
->word_break_proc
;
347 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, NULL
, 0, NULL
, NULL
);
348 textA
= HeapAlloc(GetProcessHeap(), 0, countA
);
349 WideCharToMultiByte(CP_ACP
, 0, es
->text
+ start
, count
, textA
, countA
, NULL
, NULL
);
350 TRACE_(relay
)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
351 es
->word_break_proc
, debugstr_an(textA
, countA
), index
, countA
, action
);
352 ret
= wbpA(textA
, index
, countA
, action
);
353 HeapFree(GetProcessHeap(), 0, textA
);
354 TRACE_(relay
)("\1Ret wordbreakA %p retval=%d\n", es
->word_break_proc
, ret
);
358 ret
= EDIT_WordBreakProc(es
, es
->text
, index
+start
, count
+start
, action
) - start
;
363 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF
*line_def
)
367 ScriptStringFree(&line_def
->ssa
);
368 line_def
->ssa
= NULL
;
372 static inline void EDIT_InvalidateUniscribeData(EDITSTATE
*es
)
374 LINEDEF
*line_def
= es
->first_line_def
;
377 EDIT_InvalidateUniscribeData_linedef(line_def
);
378 line_def
= line_def
->next
;
382 ScriptStringFree(&es
->ssa
);
387 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData_linedef(EDITSTATE
*es
, HDC dc
, LINEDEF
*line_def
)
392 if (line_def
->net_length
&& !line_def
->ssa
)
394 int index
= line_def
->index
;
395 HFONT old_font
= NULL
;
397 SCRIPT_TABDEF tabdef
;
401 udc
= GetDC(es
->hwndSelf
);
403 old_font
= SelectObject(udc
, es
->font
);
405 tabdef
.cTabStops
= es
->tabs_count
;
406 tabdef
.iScale
= GdiGetCharDimensions(udc
, NULL
, NULL
);
407 tabdef
.pTabStops
= es
->tabs
;
408 tabdef
.iTabOrigin
= 0;
410 hr
= ScriptStringAnalyse(udc
, &es
->text
[index
], line_def
->net_length
,
411 (1.5*line_def
->net_length
+16), -1,
412 SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
|SSA_TAB
, -1,
413 NULL
, NULL
, NULL
, &tabdef
, NULL
, &line_def
->ssa
);
416 WARN("ScriptStringAnalyse failed (%x)\n",hr
);
417 line_def
->ssa
= NULL
;
421 SelectObject(udc
, old_font
);
423 ReleaseDC(es
->hwndSelf
, udc
);
426 return line_def
->ssa
;
429 static SCRIPT_STRING_ANALYSIS
EDIT_UpdateUniscribeData(EDITSTATE
*es
, HDC dc
, INT line
)
433 if (!(es
->style
& ES_MULTILINE
))
437 INT length
= get_text_length(es
);
438 HFONT old_font
= NULL
;
442 udc
= GetDC(es
->hwndSelf
);
444 old_font
= SelectObject(udc
, es
->font
);
446 if (es
->style
& ES_PASSWORD
)
447 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
);
449 ScriptStringAnalyse(udc
, es
->text
, length
, (1.5*length
+16), -1, SSA_LINK
|SSA_FALLBACK
|SSA_GLYPHS
, -1, NULL
, NULL
, NULL
, NULL
, NULL
, &es
->ssa
);
452 SelectObject(udc
, old_font
);
454 ReleaseDC(es
->hwndSelf
, udc
);
460 line_def
= es
->first_line_def
;
461 while (line_def
&& line
)
463 line_def
= line_def
->next
;
467 return EDIT_UpdateUniscribeData_linedef(es
,dc
,line_def
);
471 static inline INT
get_vertical_line_count(EDITSTATE
*es
)
473 INT vlc
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
477 /*********************************************************************
479 * EDIT_BuildLineDefs_ML
481 * Build linked list of text lines.
482 * Lines can end with '\0' (last line), a character (if it is wrapped),
483 * a soft return '\r\r\n' or a hard return '\r\n'
486 static void EDIT_BuildLineDefs_ML(EDITSTATE
*es
, INT istart
, INT iend
, INT delta
, HRGN hrgn
)
488 LPWSTR current_position
, cp
;
490 LINEDEF
*current_line
;
491 LINEDEF
*previous_line
;
493 INT line_index
= 0, nstart_line
, nstart_index
;
494 INT line_count
= es
->line_count
;
499 if (istart
== iend
&& delta
== 0)
502 previous_line
= NULL
;
503 current_line
= es
->first_line_def
;
505 /* Find starting line. istart must lie inside an existing line or
506 * at the end of buffer */
508 if (istart
< current_line
->index
+ current_line
->length
||
509 current_line
->ending
== END_0
)
512 previous_line
= current_line
;
513 current_line
= current_line
->next
;
515 } while (current_line
);
517 if (!current_line
) /* Error occurred start is not inside previous buffer */
519 FIXME(" modification occurred outside buffer\n");
523 /* Remember start of modifications in order to calculate update region */
524 nstart_line
= line_index
;
525 nstart_index
= current_line
->index
;
527 /* We must start to reformat from the previous line since the modifications
528 * may have caused the line to wrap upwards. */
529 if (!(es
->style
& ES_AUTOHSCROLL
) && line_index
> 0)
532 current_line
= previous_line
;
534 start_line
= current_line
;
536 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
537 current_position
= es
->text
+ current_line
->index
;
538 vlc
= get_vertical_line_count(es
);
540 if (current_line
!= start_line
)
542 if (!current_line
|| current_line
->index
+ delta
> current_position
- es
->text
)
544 /* The buffer has been expanded, create a new line and
545 insert it into the link list */
546 LINEDEF
*new_line
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
));
547 new_line
->next
= previous_line
->next
;
548 previous_line
->next
= new_line
;
549 current_line
= new_line
;
552 else if (current_line
->index
+ delta
< current_position
- es
->text
)
554 /* The previous line merged with this line so we delete this extra entry */
555 previous_line
->next
= current_line
->next
;
556 HeapFree(GetProcessHeap(), 0, current_line
);
557 current_line
= previous_line
->next
;
561 else /* current_line->index + delta == current_position */
563 if (current_position
- es
->text
> iend
)
564 break; /* We reached end of line modifications */
565 /* else recalculate this line */
569 current_line
->index
= current_position
- es
->text
;
570 orig_net_length
= current_line
->net_length
;
572 /* Find end of line */
573 cp
= current_position
;
575 if (*cp
== '\n') break;
576 if ((*cp
== '\r') && (*(cp
+ 1) == '\n'))
581 /* Mark type of line termination */
583 current_line
->ending
= END_0
;
584 current_line
->net_length
= strlenW(current_position
);
585 } else if ((cp
> current_position
) && (*(cp
- 1) == '\r')) {
586 current_line
->ending
= END_SOFT
;
587 current_line
->net_length
= cp
- current_position
- 1;
588 } else if (*cp
== '\n') {
589 current_line
->ending
= END_RICH
;
590 current_line
->net_length
= cp
- current_position
;
592 current_line
->ending
= END_HARD
;
593 current_line
->net_length
= cp
- current_position
;
596 if (current_line
->net_length
)
599 EDIT_InvalidateUniscribeData_linedef(current_line
);
600 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
601 if (current_line
->ssa
)
603 sz
= ScriptString_pSize(current_line
->ssa
);
604 /* Calculate line width */
605 current_line
->width
= sz
->cx
;
607 else current_line
->width
= es
->char_width
* current_line
->net_length
;
609 else current_line
->width
= 0;
611 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
613 /* Line breaks just look back from the end and find the next break and try that. */
615 if (!(es
->style
& ES_AUTOHSCROLL
)) {
616 if (current_line
->width
> fw
&& fw
> es
->char_width
) {
623 prev
= current_line
->net_length
- 1;
624 w
= current_line
->net_length
;
625 d
= (float)current_line
->width
/(float)fw
;
626 if (d
> 1.2f
) d
-= 0.2f
;
628 if (next
>= prev
) next
= prev
-1;
630 prev
= EDIT_CallWordBreakProc(es
, current_position
- es
->text
,
631 next
, current_line
->net_length
, WB_LEFT
);
632 current_line
->net_length
= prev
;
633 EDIT_InvalidateUniscribeData_linedef(current_line
);
634 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
635 if (current_line
->ssa
)
636 sz
= ScriptString_pSize(current_line
->ssa
);
639 current_line
->width
= sz
->cx
;
643 } while (prev
&& current_line
->width
> fw
);
644 current_line
->net_length
= w
;
646 if (prev
== 0) { /* Didn't find a line break so force a break */
650 EDIT_InvalidateUniscribeData_linedef(current_line
);
651 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
653 if (current_line
->ssa
)
655 count
= ScriptString_pcOutChars(current_line
->ssa
);
656 piDx
= HeapAlloc(GetProcessHeap(),0,sizeof(INT
) * (*count
));
657 ScriptStringGetLogicalWidths(current_line
->ssa
,piDx
);
659 prev
= current_line
->net_length
-1;
661 current_line
->width
-= piDx
[prev
];
663 } while ( prev
> 0 && current_line
->width
> fw
);
666 HeapFree(GetProcessHeap(),0,piDx
);
669 prev
= (fw
/ es
->char_width
);
672 /* If the first line we are calculating, wrapped before istart, we must
673 * adjust istart in order for this to be reflected in the update region. */
674 if (current_line
->index
== nstart_index
&& istart
> current_line
->index
+ prev
)
675 istart
= current_line
->index
+ prev
;
676 /* else if we are updating the previous line before the first line we
677 * are re-calculating and it expanded */
678 else if (current_line
== start_line
&&
679 current_line
->index
!= nstart_index
&& orig_net_length
< prev
)
681 /* Line expanded due to an upwards line wrap so we must partially include
682 * previous line in update region */
683 nstart_line
= line_index
;
684 nstart_index
= current_line
->index
;
685 istart
= current_line
->index
+ orig_net_length
;
688 current_line
->net_length
= prev
;
689 current_line
->ending
= END_WRAP
;
691 if (current_line
->net_length
> 0)
693 EDIT_UpdateUniscribeData_linedef(es
, NULL
, current_line
);
694 if (current_line
->ssa
)
696 sz
= ScriptString_pSize(current_line
->ssa
);
697 current_line
->width
= sz
->cx
;
700 current_line
->width
= 0;
702 else current_line
->width
= 0;
704 else if (current_line
== start_line
&&
705 current_line
->index
!= nstart_index
&&
706 orig_net_length
< current_line
->net_length
) {
707 /* The previous line expanded but it's still not as wide as the client rect */
708 /* The expansion is due to an upwards line wrap so we must partially include
709 it in the update region */
710 nstart_line
= line_index
;
711 nstart_index
= current_line
->index
;
712 istart
= current_line
->index
+ orig_net_length
;
717 /* Adjust length to include line termination */
718 switch (current_line
->ending
) {
720 current_line
->length
= current_line
->net_length
+ 3;
723 current_line
->length
= current_line
->net_length
+ 1;
726 current_line
->length
= current_line
->net_length
+ 2;
730 current_line
->length
= current_line
->net_length
;
733 es
->text_width
= max(es
->text_width
, current_line
->width
);
734 current_position
+= current_line
->length
;
735 previous_line
= current_line
;
737 /* Discard data for non-visible lines. It will be calculated as needed */
738 if ((line_index
< es
->y_offset
) || (line_index
> es
->y_offset
+ vlc
))
739 EDIT_InvalidateUniscribeData_linedef(current_line
);
741 current_line
= current_line
->next
;
743 } while (previous_line
->ending
!= END_0
);
745 /* Finish adjusting line indexes by delta or remove hanging lines */
746 if (previous_line
->ending
== END_0
)
748 LINEDEF
*pnext
= NULL
;
750 previous_line
->next
= NULL
;
753 pnext
= current_line
->next
;
754 EDIT_InvalidateUniscribeData_linedef(current_line
);
755 HeapFree(GetProcessHeap(), 0, current_line
);
756 current_line
= pnext
;
764 current_line
->index
+= delta
;
765 current_line
= current_line
->next
;
769 /* Calculate rest of modification rectangle */
774 * We calculate two rectangles. One for the first line which may have
775 * an indent with respect to the format rect. The other is a format-width
776 * rectangle that spans the rest of the lines that changed or moved.
778 rc
.top
= es
->format_rect
.top
+ nstart_line
* es
->line_height
-
779 (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
780 rc
.bottom
= rc
.top
+ es
->line_height
;
781 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
))
782 rc
.left
= es
->format_rect
.left
;
784 rc
.left
= LOWORD(EDIT_EM_PosFromChar(es
, nstart_index
, FALSE
));
785 rc
.right
= es
->format_rect
.right
;
786 SetRectRgn(hrgn
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
789 rc
.left
= es
->format_rect
.left
;
790 rc
.right
= es
->format_rect
.right
;
792 * If lines were added or removed we must re-paint the remainder of the
793 * lines since the remaining lines were either shifted up or down.
795 if (line_count
< es
->line_count
) /* We added lines */
796 rc
.bottom
= es
->line_count
* es
->line_height
;
797 else if (line_count
> es
->line_count
) /* We removed lines */
798 rc
.bottom
= line_count
* es
->line_height
;
800 rc
.bottom
= line_index
* es
->line_height
;
801 rc
.bottom
+= es
->format_rect
.top
;
802 rc
.bottom
-= (es
->y_offset
* es
->line_height
); /* Adjust for vertical scrollbar */
803 tmphrgn
= CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
804 CombineRgn(hrgn
, hrgn
, tmphrgn
, RGN_OR
);
805 DeleteObject(tmphrgn
);
809 /*********************************************************************
811 * EDIT_CalcLineWidth_SL
814 static void EDIT_CalcLineWidth_SL(EDITSTATE
*es
)
816 EDIT_UpdateUniscribeData(es
, NULL
, 0);
820 size
= ScriptString_pSize(es
->ssa
);
821 es
->text_width
= size
->cx
;
827 /*********************************************************************
831 * Beware: This is not the function called on EM_CHARFROMPOS
832 * The position _can_ be outside the formatting / client
834 * The return value is only the character index
837 static INT
EDIT_CharFromPos(EDITSTATE
*es
, INT x
, INT y
, LPBOOL after_wrap
)
841 if (es
->style
& ES_MULTILINE
) {
843 INT line
= (y
- es
->format_rect
.top
) / es
->line_height
+ es
->y_offset
;
845 LINEDEF
*line_def
= es
->first_line_def
;
846 EDIT_UpdateUniscribeData(es
, NULL
, line
);
847 while ((line
> 0) && line_def
->next
) {
848 line_index
+= line_def
->length
;
849 line_def
= line_def
->next
;
853 x
+= es
->x_offset
- es
->format_rect
.left
;
854 if (es
->style
& ES_RIGHT
)
855 x
-= (es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
;
856 else if (es
->style
& ES_CENTER
)
857 x
-= ((es
->format_rect
.right
- es
->format_rect
.left
) - line_def
->width
) / 2;
858 if (x
>= line_def
->width
) {
860 *after_wrap
= (line_def
->ending
== END_WRAP
);
861 return line_index
+ line_def
->net_length
;
863 if (x
<= 0 || !line_def
->ssa
) {
869 ScriptStringXtoCP(line_def
->ssa
, x
, &index
, &trailing
);
870 if (trailing
) index
++;
873 *after_wrap
= ((index
== line_index
+ line_def
->net_length
) &&
874 (line_def
->ending
== END_WRAP
));
880 x
-= es
->format_rect
.left
;
886 INT indent
= (es
->format_rect
.right
- es
->format_rect
.left
) - es
->text_width
;
887 if (es
->style
& ES_RIGHT
)
889 else if (es
->style
& ES_CENTER
)
893 EDIT_UpdateUniscribeData(es
, NULL
, 0);
898 if (es
->x_offset
>= get_text_length(es
))
901 size
= ScriptString_pSize(es
->ssa
);
904 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
911 if (x
+ xoff
> 0 || !es
->ssa
)
913 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
914 if (trailing
) index
++;
923 const SIZE
*size
= NULL
;
925 size
= ScriptString_pSize(es
->ssa
);
928 else if (x
> size
->cx
)
929 index
= get_text_length(es
);
932 ScriptStringXtoCP(es
->ssa
, x
+xoff
, &index
, &trailing
);
933 if (trailing
) index
++;
939 index
= es
->x_offset
;
946 /*********************************************************************
950 * adjusts the point to be within the formatting rectangle
951 * (so CharFromPos returns the nearest _visible_ character)
954 static void EDIT_ConfinePoint(const EDITSTATE
*es
, LPINT x
, LPINT y
)
956 *x
= min(max(*x
, es
->format_rect
.left
), es
->format_rect
.right
- 1);
957 *y
= min(max(*y
, es
->format_rect
.top
), es
->format_rect
.bottom
- 1);
961 /*********************************************************************
966 static INT
EDIT_EM_LineFromChar(EDITSTATE
*es
, INT index
)
971 if (!(es
->style
& ES_MULTILINE
))
973 if (index
> (INT
)get_text_length(es
))
974 return es
->line_count
- 1;
976 index
= min(es
->selection_start
, es
->selection_end
);
979 line_def
= es
->first_line_def
;
980 index
-= line_def
->length
;
981 while ((index
>= 0) && line_def
->next
) {
983 line_def
= line_def
->next
;
984 index
-= line_def
->length
;
990 /*********************************************************************
995 static INT
EDIT_EM_LineIndex(const EDITSTATE
*es
, INT line
)
998 const LINEDEF
*line_def
;
1000 if (!(es
->style
& ES_MULTILINE
))
1002 if (line
>= es
->line_count
)
1006 line_def
= es
->first_line_def
;
1008 INT index
= es
->selection_end
- line_def
->length
;
1009 while ((index
>= 0) && line_def
->next
) {
1010 line_index
+= line_def
->length
;
1011 line_def
= line_def
->next
;
1012 index
-= line_def
->length
;
1016 line_index
+= line_def
->length
;
1017 line_def
= line_def
->next
;
1025 /*********************************************************************
1030 static INT
EDIT_EM_LineLength(EDITSTATE
*es
, INT index
)
1034 if (!(es
->style
& ES_MULTILINE
))
1035 return get_text_length(es
);
1038 /* get the number of remaining non-selected chars of selected lines */
1039 INT32 l
; /* line number */
1040 INT32 li
; /* index of first char in line */
1042 l
= EDIT_EM_LineFromChar(es
, es
->selection_start
);
1043 /* # chars before start of selection area */
1044 count
= es
->selection_start
- EDIT_EM_LineIndex(es
, l
);
1045 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1046 /* # chars after end of selection */
1047 li
= EDIT_EM_LineIndex(es
, l
);
1048 count
+= li
+ EDIT_EM_LineLength(es
, li
) - es
->selection_end
;
1051 line_def
= es
->first_line_def
;
1052 index
-= line_def
->length
;
1053 while ((index
>= 0) && line_def
->next
) {
1054 line_def
= line_def
->next
;
1055 index
-= line_def
->length
;
1057 return line_def
->net_length
;
1061 /*********************************************************************
1066 static LRESULT
EDIT_EM_PosFromChar(EDITSTATE
*es
, INT index
, BOOL after_wrap
)
1068 INT len
= get_text_length(es
);
1077 index
= min(index
, len
);
1078 if (es
->style
& ES_MULTILINE
) {
1079 l
= EDIT_EM_LineFromChar(es
, index
);
1080 EDIT_UpdateUniscribeData(es
, NULL
, l
);
1082 y
= (l
- es
->y_offset
) * es
->line_height
;
1083 li
= EDIT_EM_LineIndex(es
, l
);
1084 if (after_wrap
&& (li
== index
) && l
) {
1086 line_def
= es
->first_line_def
;
1088 line_def
= line_def
->next
;
1091 if (line_def
->ending
== END_WRAP
) {
1093 y
-= es
->line_height
;
1094 li
= EDIT_EM_LineIndex(es
, l
);
1098 line_def
= es
->first_line_def
;
1099 while (line_def
->index
!= li
)
1100 line_def
= line_def
->next
;
1102 lw
= line_def
->width
;
1103 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1106 ScriptStringCPtoX(line_def
->ssa
, (index
- 1) - li
, TRUE
, &x
);
1112 if (es
->style
& ES_RIGHT
)
1114 else if (es
->style
& ES_CENTER
)
1119 EDIT_UpdateUniscribeData(es
, NULL
, 0);
1124 if (es
->x_offset
>= get_text_length(es
))
1126 int leftover
= es
->x_offset
- get_text_length(es
);
1130 size
= ScriptString_pSize(es
->ssa
);
1135 xoff
+= es
->char_width
* leftover
;
1138 ScriptStringCPtoX(es
->ssa
, es
->x_offset
, FALSE
, &xoff
);
1145 if (index
>= get_text_length(es
))
1150 size
= ScriptString_pSize(es
->ssa
);
1157 ScriptStringCPtoX(es
->ssa
, index
, FALSE
, &xi
);
1163 if (index
>= es
->x_offset
) {
1164 if (!es
->x_offset
&& (es
->style
& (ES_RIGHT
| ES_CENTER
)))
1166 w
= es
->format_rect
.right
- es
->format_rect
.left
;
1167 if (w
> es
->text_width
)
1169 if (es
->style
& ES_RIGHT
)
1170 x
+= w
- es
->text_width
;
1171 else if (es
->style
& ES_CENTER
)
1172 x
+= (w
- es
->text_width
) / 2;
1178 x
+= es
->format_rect
.left
;
1179 y
+= es
->format_rect
.top
;
1180 return MAKELONG((INT16
)x
, (INT16
)y
);
1184 /*********************************************************************
1188 * Calculates the bounding rectangle for a line from a starting
1189 * column to an ending column.
1192 static void EDIT_GetLineRect(EDITSTATE
*es
, INT line
, INT scol
, INT ecol
, LPRECT rc
)
1194 SCRIPT_STRING_ANALYSIS ssa
;
1198 if (es
->style
& ES_MULTILINE
)
1200 const LINEDEF
*line_def
= NULL
;
1201 rc
->top
= es
->format_rect
.top
+ (line
- es
->y_offset
) * es
->line_height
;
1202 if (line
>= es
->line_count
)
1205 line_def
= es
->first_line_def
;
1207 INT index
= es
->selection_end
- line_def
->length
;
1208 while ((index
>= 0) && line_def
->next
) {
1209 line_index
+= line_def
->length
;
1210 line_def
= line_def
->next
;
1211 index
-= line_def
->length
;
1215 line_index
+= line_def
->length
;
1216 line_def
= line_def
->next
;
1220 ssa
= line_def
->ssa
;
1225 rc
->top
= es
->format_rect
.top
;
1229 rc
->bottom
= rc
->top
+ es
->line_height
;
1230 pt1
= (scol
== 0) ? es
->format_rect
.left
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ scol
, TRUE
));
1231 pt2
= (ecol
== -1) ? es
->format_rect
.right
: (short)LOWORD(EDIT_EM_PosFromChar(es
, line_index
+ ecol
, TRUE
));
1234 ScriptStringCPtoX(ssa
, scol
, FALSE
, &pt3
);
1235 pt3
+=es
->format_rect
.left
;
1238 rc
->right
= max(max(pt1
, pt2
),pt3
);
1239 rc
->left
= min(min(pt1
, pt2
),pt3
);
1243 static inline void text_buffer_changed(EDITSTATE
*es
)
1245 es
->text_length
= (UINT
)-1;
1247 HeapFree( GetProcessHeap(), 0, es
->logAttr
);
1249 EDIT_InvalidateUniscribeData(es
);
1252 /*********************************************************************
1256 static void EDIT_LockBuffer(EDITSTATE
*es
)
1260 if(!es
->hloc32W
) return;
1264 CHAR
*textA
= LocalLock(es
->hloc32A
);
1266 UINT countW_new
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
1267 if(countW_new
> es
->buffer_size
+ 1)
1269 UINT alloc_size
= ROUND_TO_GROW(countW_new
* sizeof(WCHAR
));
1270 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es
->buffer_size
, countW_new
);
1271 hloc32W_new
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1274 es
->hloc32W
= hloc32W_new
;
1275 es
->buffer_size
= LocalSize(hloc32W_new
)/sizeof(WCHAR
) - 1;
1276 TRACE("Real new size %d+1 WCHARs\n", es
->buffer_size
);
1279 WARN("FAILED! Will synchronize partially\n");
1281 es
->text
= LocalLock(es
->hloc32W
);
1282 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, es
->text
, es
->buffer_size
+ 1);
1283 LocalUnlock(es
->hloc32A
);
1285 else es
->text
= LocalLock(es
->hloc32W
);
1291 /*********************************************************************
1296 static void EDIT_UnlockBuffer(EDITSTATE
*es
, BOOL force
)
1298 /* Edit window might be already destroyed */
1299 if(!IsWindow(es
->hwndSelf
))
1301 WARN("edit hwnd %p already destroyed\n", es
->hwndSelf
);
1305 if (!es
->lock_count
) {
1306 ERR("lock_count == 0 ... please report\n");
1310 ERR("es->text == 0 ... please report\n");
1313 if (force
|| (es
->lock_count
== 1)) {
1316 UINT countW
= get_text_length(es
) + 1;
1320 UINT countA_new
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
, NULL
, 0, NULL
, NULL
);
1321 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1322 TRACE("%d WCHARs translated to %d bytes\n", countW
, countA_new
);
1323 countA
= LocalSize(es
->hloc32A
);
1324 if(countA_new
> countA
)
1327 UINT alloc_size
= ROUND_TO_GROW(countA_new
);
1328 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA
, alloc_size
);
1329 hloc32A_new
= LocalReAlloc(es
->hloc32A
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
);
1332 es
->hloc32A
= hloc32A_new
;
1333 countA
= LocalSize(hloc32A_new
);
1334 TRACE("Real new size %d bytes\n", countA
);
1337 WARN("FAILED! Will synchronize partially\n");
1339 WideCharToMultiByte(CP_ACP
, 0, es
->text
, countW
,
1340 LocalLock(es
->hloc32A
), countA
, NULL
, NULL
);
1341 LocalUnlock(es
->hloc32A
);
1344 LocalUnlock(es
->hloc32W
);
1348 ERR("no buffer ... please report\n");
1356 /*********************************************************************
1360 * Try to fit size + 1 characters in the buffer.
1362 static BOOL
EDIT_MakeFit(EDITSTATE
*es
, UINT size
)
1366 if (size
<= es
->buffer_size
)
1369 TRACE("trying to ReAlloc to %d+1 characters\n", size
);
1371 /* Force edit to unlock its buffer. es->text now NULL */
1372 EDIT_UnlockBuffer(es
, TRUE
);
1375 UINT alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1376 if ((hNew32W
= LocalReAlloc(es
->hloc32W
, alloc_size
, LMEM_MOVEABLE
| LMEM_ZEROINIT
))) {
1377 TRACE("Old 32 bit handle %p, new handle %p\n", es
->hloc32W
, hNew32W
);
1378 es
->hloc32W
= hNew32W
;
1379 es
->buffer_size
= LocalSize(hNew32W
)/sizeof(WCHAR
) - 1;
1383 EDIT_LockBuffer(es
);
1385 if (es
->buffer_size
< size
) {
1386 WARN("FAILED ! We now have %d+1\n", es
->buffer_size
);
1387 EDIT_NOTIFY_PARENT(es
, EN_ERRSPACE
);
1390 TRACE("We now have %d+1\n", es
->buffer_size
);
1396 /*********************************************************************
1400 * Try to fit size + 1 bytes in the undo buffer.
1403 static BOOL
EDIT_MakeUndoFit(EDITSTATE
*es
, UINT size
)
1407 if (size
<= es
->undo_buffer_size
)
1410 TRACE("trying to ReAlloc to %d+1\n", size
);
1412 alloc_size
= ROUND_TO_GROW((size
+ 1) * sizeof(WCHAR
));
1413 if ((es
->undo_text
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, es
->undo_text
, alloc_size
))) {
1414 es
->undo_buffer_size
= alloc_size
/sizeof(WCHAR
) - 1;
1419 WARN("FAILED ! We now have %d+1\n", es
->undo_buffer_size
);
1425 /*********************************************************************
1427 * EDIT_UpdateTextRegion
1430 static void EDIT_UpdateTextRegion(EDITSTATE
*es
, HRGN hrgn
, BOOL bErase
)
1432 if (es
->flags
& EF_UPDATE
) {
1433 es
->flags
&= ~EF_UPDATE
;
1434 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1436 InvalidateRgn(es
->hwndSelf
, hrgn
, bErase
);
1440 /*********************************************************************
1445 static void EDIT_UpdateText(EDITSTATE
*es
, const RECT
*rc
, BOOL bErase
)
1447 if (es
->flags
& EF_UPDATE
) {
1448 es
->flags
&= ~EF_UPDATE
;
1449 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
1451 InvalidateRect(es
->hwndSelf
, rc
, bErase
);
1454 /*********************************************************************
1456 * EDIT_SL_InvalidateText
1458 * Called from EDIT_InvalidateText().
1459 * Does the job for single-line controls only.
1462 static void EDIT_SL_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1467 EDIT_GetLineRect(es
, 0, start
, end
, &line_rect
);
1468 if (IntersectRect(&rc
, &line_rect
, &es
->format_rect
))
1469 EDIT_UpdateText(es
, &rc
, TRUE
);
1472 /*********************************************************************
1474 * EDIT_ML_InvalidateText
1476 * Called from EDIT_InvalidateText().
1477 * Does the job for multi-line controls only.
1480 static void EDIT_ML_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1482 INT vlc
= get_vertical_line_count(es
);
1483 INT sl
= EDIT_EM_LineFromChar(es
, start
);
1484 INT el
= EDIT_EM_LineFromChar(es
, end
);
1493 if ((el
< es
->y_offset
) || (sl
> es
->y_offset
+ vlc
))
1496 sc
= start
- EDIT_EM_LineIndex(es
, sl
);
1497 ec
= end
- EDIT_EM_LineIndex(es
, el
);
1498 if (sl
< es
->y_offset
) {
1502 if (el
> es
->y_offset
+ vlc
) {
1503 el
= es
->y_offset
+ vlc
;
1504 ec
= EDIT_EM_LineLength(es
, EDIT_EM_LineIndex(es
, el
));
1506 GetClientRect(es
->hwndSelf
, &rc1
);
1507 IntersectRect(&rcWnd
, &rc1
, &es
->format_rect
);
1509 EDIT_GetLineRect(es
, sl
, sc
, ec
, &rcLine
);
1510 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1511 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1513 EDIT_GetLineRect(es
, sl
, sc
,
1514 EDIT_EM_LineLength(es
,
1515 EDIT_EM_LineIndex(es
, sl
)),
1517 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1518 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1519 for (l
= sl
+ 1 ; l
< el
; l
++) {
1520 EDIT_GetLineRect(es
, l
, 0,
1521 EDIT_EM_LineLength(es
,
1522 EDIT_EM_LineIndex(es
, l
)),
1524 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1525 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1527 EDIT_GetLineRect(es
, el
, 0, ec
, &rcLine
);
1528 if (IntersectRect(&rcUpdate
, &rcWnd
, &rcLine
))
1529 EDIT_UpdateText(es
, &rcUpdate
, TRUE
);
1534 /*********************************************************************
1536 * EDIT_InvalidateText
1538 * Invalidate the text from offset start up to, but not including,
1539 * offset end. Useful for (re)painting the selection.
1540 * Regions outside the linewidth are not invalidated.
1541 * end == -1 means end == TextLength.
1542 * start and end need not be ordered.
1545 static void EDIT_InvalidateText(EDITSTATE
*es
, INT start
, INT end
)
1551 end
= get_text_length(es
);
1559 if (es
->style
& ES_MULTILINE
)
1560 EDIT_ML_InvalidateText(es
, start
, end
);
1562 EDIT_SL_InvalidateText(es
, start
, end
);
1566 /*********************************************************************
1570 * note: unlike the specs say: the order of start and end
1571 * _is_ preserved in Windows. (i.e. start can be > end)
1572 * In other words: this handler is OK
1575 static void EDIT_EM_SetSel(EDITSTATE
*es
, UINT start
, UINT end
, BOOL after_wrap
)
1577 UINT old_start
= es
->selection_start
;
1578 UINT old_end
= es
->selection_end
;
1579 UINT len
= get_text_length(es
);
1581 if (start
== (UINT
)-1) {
1582 start
= es
->selection_end
;
1583 end
= es
->selection_end
;
1585 start
= min(start
, len
);
1586 end
= min(end
, len
);
1588 es
->selection_start
= start
;
1589 es
->selection_end
= end
;
1591 es
->flags
|= EF_AFTER_WRAP
;
1593 es
->flags
&= ~EF_AFTER_WRAP
;
1594 /* Compute the necessary invalidation region. */
1595 /* Note that we don't need to invalidate regions which have
1596 * "never" been selected, or those which are "still" selected.
1597 * In fact, every time we hit a selection boundary, we can
1598 * *toggle* whether we need to invalidate. Thus we can optimize by
1599 * *sorting* the interval endpoints. Let's assume that we sort them
1601 * start <= end <= old_start <= old_end
1602 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1603 * in 5 comparisons; i.e. it is impossible to do better than the
1605 ORDER_UINT(end
, old_end
);
1606 ORDER_UINT(start
, old_start
);
1607 ORDER_UINT(old_start
, old_end
);
1608 ORDER_UINT(start
, end
);
1609 /* Note that at this point 'end' and 'old_start' are not in order, but
1610 * start is definitely the min. and old_end is definitely the max. */
1611 if (end
!= old_start
)
1615 * ORDER_UINT32(end, old_start);
1616 * EDIT_InvalidateText(es, start, end);
1617 * EDIT_InvalidateText(es, old_start, old_end);
1618 * in place of the following if statement.
1619 * (That would complete the optimal five-comparison four-element sort.)
1621 if (old_start
> end
)
1623 EDIT_InvalidateText(es
, start
, end
);
1624 EDIT_InvalidateText(es
, old_start
, old_end
);
1628 EDIT_InvalidateText(es
, start
, old_start
);
1629 EDIT_InvalidateText(es
, end
, old_end
);
1632 else EDIT_InvalidateText(es
, start
, old_end
);
1636 /*********************************************************************
1638 * EDIT_UpdateScrollInfo
1641 static void EDIT_UpdateScrollInfo(EDITSTATE
*es
)
1643 if ((es
->style
& WS_VSCROLL
) && !(es
->flags
& EF_VSCROLL_TRACK
))
1646 si
.cbSize
= sizeof(SCROLLINFO
);
1647 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1649 si
.nMax
= es
->line_count
- 1;
1650 si
.nPage
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1651 si
.nPos
= es
->y_offset
;
1652 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1653 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1654 SetScrollInfo(es
->hwndSelf
, SB_VERT
, &si
, TRUE
);
1657 if ((es
->style
& WS_HSCROLL
) && !(es
->flags
& EF_HSCROLL_TRACK
))
1660 si
.cbSize
= sizeof(SCROLLINFO
);
1661 si
.fMask
= SIF_PAGE
| SIF_POS
| SIF_RANGE
| SIF_DISABLENOSCROLL
;
1663 si
.nMax
= es
->text_width
- 1;
1664 si
.nPage
= es
->format_rect
.right
- es
->format_rect
.left
;
1665 si
.nPos
= es
->x_offset
;
1666 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1667 si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
);
1668 SetScrollInfo(es
->hwndSelf
, SB_HORZ
, &si
, TRUE
);
1673 /*********************************************************************
1675 * EDIT_EM_LineScroll_internal
1677 * Version of EDIT_EM_LineScroll for internal use.
1678 * It doesn't refuse if ES_MULTILINE is set and assumes that
1679 * dx is in pixels, dy - in lines.
1682 static BOOL
EDIT_EM_LineScroll_internal(EDITSTATE
*es
, INT dx
, INT dy
)
1685 INT x_offset_in_pixels
;
1686 INT lines_per_page
= (es
->format_rect
.bottom
- es
->format_rect
.top
) /
1689 if (es
->style
& ES_MULTILINE
)
1691 x_offset_in_pixels
= es
->x_offset
;
1696 x_offset_in_pixels
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->x_offset
, FALSE
));
1699 if (-dx
> x_offset_in_pixels
)
1700 dx
= -x_offset_in_pixels
;
1701 if (dx
> es
->text_width
- x_offset_in_pixels
)
1702 dx
= es
->text_width
- x_offset_in_pixels
;
1703 nyoff
= max(0, es
->y_offset
+ dy
);
1704 if (nyoff
>= es
->line_count
- lines_per_page
)
1705 nyoff
= max(0, es
->line_count
- lines_per_page
);
1706 dy
= (es
->y_offset
- nyoff
) * es
->line_height
;
1711 es
->y_offset
= nyoff
;
1712 if(es
->style
& ES_MULTILINE
)
1715 es
->x_offset
+= dx
/ es
->char_width
;
1717 GetClientRect(es
->hwndSelf
, &rc1
);
1718 IntersectRect(&rc
, &rc1
, &es
->format_rect
);
1719 ScrollWindowEx(es
->hwndSelf
, -dx
, dy
,
1720 NULL
, &rc
, NULL
, NULL
, SW_INVALIDATE
);
1721 /* force scroll info update */
1722 EDIT_UpdateScrollInfo(es
);
1724 if (dx
&& !(es
->flags
& EF_HSCROLL_TRACK
))
1725 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
1726 if (dy
&& !(es
->flags
& EF_VSCROLL_TRACK
))
1727 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
1731 /*********************************************************************
1735 * NOTE: dx is in average character widths, dy - in lines;
1738 static BOOL
EDIT_EM_LineScroll(EDITSTATE
*es
, INT dx
, INT dy
)
1740 if (!(es
->style
& ES_MULTILINE
))
1743 dx
*= es
->char_width
;
1744 return EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1748 /*********************************************************************
1753 static LRESULT
EDIT_EM_Scroll(EDITSTATE
*es
, INT action
)
1757 if (!(es
->style
& ES_MULTILINE
))
1758 return (LRESULT
)FALSE
;
1768 if (es
->y_offset
< es
->line_count
- 1)
1773 dy
= -(es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1776 if (es
->y_offset
< es
->line_count
- 1)
1777 dy
= (es
->format_rect
.bottom
- es
->format_rect
.top
) / es
->line_height
;
1780 return (LRESULT
)FALSE
;
1783 INT vlc
= get_vertical_line_count(es
);
1784 /* check if we are going to move too far */
1785 if(es
->y_offset
+ dy
> es
->line_count
- vlc
)
1786 dy
= max(es
->line_count
- vlc
, 0) - es
->y_offset
;
1788 /* Notification is done in EDIT_EM_LineScroll */
1790 EDIT_EM_LineScroll(es
, 0, dy
);
1791 return MAKELONG(dy
, TRUE
);
1795 return (LRESULT
)FALSE
;
1799 /*********************************************************************
1804 static void EDIT_SetCaretPos(EDITSTATE
*es
, INT pos
,
1807 LRESULT res
= EDIT_EM_PosFromChar(es
, pos
, after_wrap
);
1808 TRACE("%d - %dx%d\n", pos
, (short)LOWORD(res
), (short)HIWORD(res
));
1809 SetCaretPos((short)LOWORD(res
), (short)HIWORD(res
));
1813 /*********************************************************************
1818 static void EDIT_EM_ScrollCaret(EDITSTATE
*es
)
1820 if (es
->style
& ES_MULTILINE
) {
1824 INT cw
= es
->char_width
;
1829 l
= EDIT_EM_LineFromChar(es
, es
->selection_end
);
1830 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
));
1831 vlc
= get_vertical_line_count(es
);
1832 if (l
>= es
->y_offset
+ vlc
)
1833 dy
= l
- vlc
+ 1 - es
->y_offset
;
1834 if (l
< es
->y_offset
)
1835 dy
= l
- es
->y_offset
;
1836 ww
= es
->format_rect
.right
- es
->format_rect
.left
;
1837 if (x
< es
->format_rect
.left
)
1838 dx
= x
- es
->format_rect
.left
- ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1839 if (x
> es
->format_rect
.right
)
1840 dx
= x
- es
->format_rect
.left
- (HSCROLL_FRACTION
- 1) * ww
/ HSCROLL_FRACTION
/ cw
* cw
;
1841 if (dy
|| dx
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1843 /* check if we are going to move too far */
1844 if(es
->x_offset
+ dx
+ ww
> es
->text_width
)
1845 dx
= es
->text_width
- ww
- es
->x_offset
;
1846 if(dx
|| dy
|| (es
->y_offset
&& (es
->line_count
- es
->y_offset
< vlc
)))
1847 EDIT_EM_LineScroll_internal(es
, dx
, dy
);
1854 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1855 format_width
= es
->format_rect
.right
- es
->format_rect
.left
;
1856 if (x
< es
->format_rect
.left
) {
1857 goal
= es
->format_rect
.left
+ format_width
/ HSCROLL_FRACTION
;
1860 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1861 } while ((x
< goal
) && es
->x_offset
);
1862 /* FIXME: use ScrollWindow() somehow to improve performance */
1863 EDIT_UpdateText(es
, NULL
, TRUE
);
1864 } else if (x
> es
->format_rect
.right
) {
1866 INT len
= get_text_length(es
);
1867 goal
= es
->format_rect
.right
- format_width
/ HSCROLL_FRACTION
;
1870 x
= (short)LOWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, FALSE
));
1871 x_last
= (short)LOWORD(EDIT_EM_PosFromChar(es
, len
, FALSE
));
1872 } while ((x
> goal
) && (x_last
> es
->format_rect
.right
));
1873 /* FIXME: use ScrollWindow() somehow to improve performance */
1874 EDIT_UpdateText(es
, NULL
, TRUE
);
1878 if(es
->flags
& EF_FOCUSED
)
1879 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
1883 /*********************************************************************
1888 static void EDIT_MoveBackward(EDITSTATE
*es
, BOOL extend
)
1890 INT e
= es
->selection_end
;
1894 if ((es
->style
& ES_MULTILINE
) && e
&&
1895 (es
->text
[e
- 1] == '\r') && (es
->text
[e
] == '\n')) {
1897 if (e
&& (es
->text
[e
- 1] == '\r'))
1901 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1902 EDIT_EM_ScrollCaret(es
);
1906 /*********************************************************************
1910 * Only for multi line controls
1911 * Move the caret one line down, on a column with the nearest
1912 * x coordinate on the screen (might be a different column).
1915 static void EDIT_MoveDown_ML(EDITSTATE
*es
, BOOL extend
)
1917 INT s
= es
->selection_start
;
1918 INT e
= es
->selection_end
;
1919 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
1920 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
1921 INT x
= (short)LOWORD(pos
);
1922 INT y
= (short)HIWORD(pos
);
1924 e
= EDIT_CharFromPos(es
, x
, y
+ es
->line_height
, &after_wrap
);
1927 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
1928 EDIT_EM_ScrollCaret(es
);
1932 /*********************************************************************
1937 static void EDIT_MoveEnd(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
1939 BOOL after_wrap
= FALSE
;
1942 /* Pass a high value in x to make sure of receiving the end of the line */
1943 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
1944 e
= EDIT_CharFromPos(es
, 0x3fffffff,
1945 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), &after_wrap
);
1947 e
= get_text_length(es
);
1948 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, after_wrap
);
1949 EDIT_EM_ScrollCaret(es
);
1953 /*********************************************************************
1958 static void EDIT_MoveForward(EDITSTATE
*es
, BOOL extend
)
1960 INT e
= es
->selection_end
;
1964 if ((es
->style
& ES_MULTILINE
) && (es
->text
[e
- 1] == '\r')) {
1965 if (es
->text
[e
] == '\n')
1967 else if ((es
->text
[e
] == '\r') && (es
->text
[e
+ 1] == '\n'))
1971 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1972 EDIT_EM_ScrollCaret(es
);
1976 /*********************************************************************
1980 * Home key: move to beginning of line.
1983 static void EDIT_MoveHome(EDITSTATE
*es
, BOOL extend
, BOOL ctrl
)
1987 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1988 if (!ctrl
&& (es
->style
& ES_MULTILINE
))
1989 e
= EDIT_CharFromPos(es
, -es
->x_offset
,
1990 HIWORD(EDIT_EM_PosFromChar(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
)), NULL
);
1993 EDIT_EM_SetSel(es
, extend
? es
->selection_start
: e
, e
, FALSE
);
1994 EDIT_EM_ScrollCaret(es
);
1998 /*********************************************************************
2000 * EDIT_MovePageDown_ML
2002 * Only for multi line controls
2003 * Move the caret one page down, on a column with the nearest
2004 * x coordinate on the screen (might be a different column).
2007 static void EDIT_MovePageDown_ML(EDITSTATE
*es
, BOOL extend
)
2009 INT s
= es
->selection_start
;
2010 INT e
= es
->selection_end
;
2011 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2012 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2013 INT x
= (short)LOWORD(pos
);
2014 INT y
= (short)HIWORD(pos
);
2016 e
= EDIT_CharFromPos(es
, x
,
2017 y
+ (es
->format_rect
.bottom
- es
->format_rect
.top
),
2021 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2022 EDIT_EM_ScrollCaret(es
);
2026 /*********************************************************************
2028 * EDIT_MovePageUp_ML
2030 * Only for multi line controls
2031 * Move the caret one page up, on a column with the nearest
2032 * x coordinate on the screen (might be a different column).
2035 static void EDIT_MovePageUp_ML(EDITSTATE
*es
, BOOL extend
)
2037 INT s
= es
->selection_start
;
2038 INT e
= es
->selection_end
;
2039 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2040 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2041 INT x
= (short)LOWORD(pos
);
2042 INT y
= (short)HIWORD(pos
);
2044 e
= EDIT_CharFromPos(es
, x
,
2045 y
- (es
->format_rect
.bottom
- es
->format_rect
.top
),
2049 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2050 EDIT_EM_ScrollCaret(es
);
2054 /*********************************************************************
2058 * Only for multi line controls
2059 * Move the caret one line up, on a column with the nearest
2060 * x coordinate on the screen (might be a different column).
2063 static void EDIT_MoveUp_ML(EDITSTATE
*es
, BOOL extend
)
2065 INT s
= es
->selection_start
;
2066 INT e
= es
->selection_end
;
2067 BOOL after_wrap
= (es
->flags
& EF_AFTER_WRAP
);
2068 LRESULT pos
= EDIT_EM_PosFromChar(es
, e
, after_wrap
);
2069 INT x
= (short)LOWORD(pos
);
2070 INT y
= (short)HIWORD(pos
);
2072 e
= EDIT_CharFromPos(es
, x
, y
- es
->line_height
, &after_wrap
);
2075 EDIT_EM_SetSel(es
, s
, e
, after_wrap
);
2076 EDIT_EM_ScrollCaret(es
);
2080 /*********************************************************************
2082 * EDIT_MoveWordBackward
2085 static void EDIT_MoveWordBackward(EDITSTATE
*es
, BOOL extend
)
2087 INT s
= es
->selection_start
;
2088 INT e
= es
->selection_end
;
2093 l
= EDIT_EM_LineFromChar(es
, e
);
2094 ll
= EDIT_EM_LineLength(es
, e
);
2095 li
= EDIT_EM_LineIndex(es
, l
);
2098 li
= EDIT_EM_LineIndex(es
, l
- 1);
2099 e
= li
+ EDIT_EM_LineLength(es
, li
);
2102 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
2106 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2107 EDIT_EM_ScrollCaret(es
);
2111 /*********************************************************************
2113 * EDIT_MoveWordForward
2116 static void EDIT_MoveWordForward(EDITSTATE
*es
, BOOL extend
)
2118 INT s
= es
->selection_start
;
2119 INT e
= es
->selection_end
;
2124 l
= EDIT_EM_LineFromChar(es
, e
);
2125 ll
= EDIT_EM_LineLength(es
, e
);
2126 li
= EDIT_EM_LineIndex(es
, l
);
2128 if ((es
->style
& ES_MULTILINE
) && (l
!= es
->line_count
- 1))
2129 e
= EDIT_EM_LineIndex(es
, l
+ 1);
2131 e
= li
+ EDIT_CallWordBreakProc(es
,
2132 li
, e
- li
+ 1, ll
, WB_RIGHT
);
2136 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
2137 EDIT_EM_ScrollCaret(es
);
2141 /*********************************************************************
2146 static INT
EDIT_PaintText(EDITSTATE
*es
, HDC dc
, INT x
, INT y
, INT line
, INT col
, INT count
, BOOL rev
)
2150 LOGFONTW underline_font
;
2151 HFONT hUnderline
= 0;
2160 BkMode
= GetBkMode(dc
);
2161 BkColor
= GetBkColor(dc
);
2162 TextColor
= GetTextColor(dc
);
2164 if (es
->composition_len
== 0)
2166 SetBkColor(dc
, GetSysColor(COLOR_HIGHLIGHT
));
2167 SetTextColor(dc
, GetSysColor(COLOR_HIGHLIGHTTEXT
));
2168 SetBkMode( dc
, OPAQUE
);
2172 HFONT current
= GetCurrentObject(dc
,OBJ_FONT
);
2173 GetObjectW(current
,sizeof(LOGFONTW
),&underline_font
);
2174 underline_font
.lfUnderline
= TRUE
;
2175 hUnderline
= CreateFontIndirectW(&underline_font
);
2176 old_font
= SelectObject(dc
,hUnderline
);
2179 li
= EDIT_EM_LineIndex(es
, line
);
2180 if (es
->style
& ES_MULTILINE
) {
2181 ret
= (INT
)LOWORD(TabbedTextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
,
2182 es
->tabs_count
, es
->tabs
, es
->format_rect
.left
- es
->x_offset
));
2184 TextOutW(dc
, x
, y
, es
->text
+ li
+ col
, count
);
2185 GetTextExtentPoint32W(dc
, es
->text
+ li
+ col
, count
, &size
);
2189 if (es
->composition_len
== 0)
2191 SetBkColor(dc
, BkColor
);
2192 SetTextColor(dc
, TextColor
);
2193 SetBkMode( dc
, BkMode
);
2198 SelectObject(dc
,old_font
);
2200 DeleteObject(hUnderline
);
2207 /*********************************************************************
2212 static void EDIT_PaintLine(EDITSTATE
*es
, HDC dc
, INT line
, BOOL rev
)
2221 SCRIPT_STRING_ANALYSIS ssa
;
2223 if (es
->style
& ES_MULTILINE
) {
2224 INT vlc
= get_vertical_line_count(es
);
2226 if ((line
< es
->y_offset
) || (line
> es
->y_offset
+ vlc
) || (line
>= es
->line_count
))
2231 TRACE("line=%d\n", line
);
2233 ssa
= EDIT_UpdateUniscribeData(es
, dc
, line
);
2234 pos
= EDIT_EM_PosFromChar(es
, EDIT_EM_LineIndex(es
, line
), FALSE
);
2235 x
= (short)LOWORD(pos
);
2236 y
= (short)HIWORD(pos
);
2238 if (es
->style
& ES_MULTILINE
)
2240 int line_idx
= line
;
2242 if (es
->style
& ES_RIGHT
|| es
->style
& ES_CENTER
)
2244 LINEDEF
*line_def
= es
->first_line_def
;
2247 while (line_def
&& line_idx
)
2249 line_def
= line_def
->next
;
2252 w
= es
->format_rect
.right
- es
->format_rect
.left
;
2253 lw
= line_def
->width
;
2255 if (es
->style
& ES_RIGHT
)
2257 else if (es
->style
& ES_CENTER
)
2260 x
+= es
->format_rect
.left
;
2265 li
= EDIT_EM_LineIndex(es
, line
);
2266 ll
= EDIT_EM_LineLength(es
, li
);
2267 s
= min(es
->selection_start
, es
->selection_end
);
2268 e
= max(es
->selection_start
, es
->selection_end
);
2269 s
= min(li
+ ll
, max(li
, s
));
2270 e
= min(li
+ ll
, max(li
, e
));
2274 ScriptStringOut(ssa
, x
, y
, 0, &es
->format_rect
, s
- li
, e
- li
, FALSE
);
2275 else if (rev
&& (s
!= e
) &&
2276 ((es
->flags
& EF_FOCUSED
) || (es
->style
& ES_NOHIDESEL
))) {
2277 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, s
- li
, FALSE
);
2278 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, s
- li
, e
- s
, TRUE
);
2279 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, e
- li
, li
+ ll
- e
, FALSE
);
2281 x
+= EDIT_PaintText(es
, dc
, x
, y
, line
, 0, ll
, FALSE
);
2285 /*********************************************************************
2287 * EDIT_AdjustFormatRect
2289 * Adjusts the format rectangle for the current font and the
2290 * current client rectangle.
2293 static void EDIT_AdjustFormatRect(EDITSTATE
*es
)
2297 es
->format_rect
.right
= max(es
->format_rect
.right
, es
->format_rect
.left
+ es
->char_width
);
2298 if (es
->style
& ES_MULTILINE
)
2300 INT fw
, vlc
, max_x_offset
, max_y_offset
;
2302 vlc
= get_vertical_line_count(es
);
2303 es
->format_rect
.bottom
= es
->format_rect
.top
+ vlc
* es
->line_height
;
2305 /* correct es->x_offset */
2306 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2307 max_x_offset
= es
->text_width
- fw
;
2308 if(max_x_offset
< 0) max_x_offset
= 0;
2309 if(es
->x_offset
> max_x_offset
)
2310 es
->x_offset
= max_x_offset
;
2312 /* correct es->y_offset */
2313 max_y_offset
= es
->line_count
- vlc
;
2314 if(max_y_offset
< 0) max_y_offset
= 0;
2315 if(es
->y_offset
> max_y_offset
)
2316 es
->y_offset
= max_y_offset
;
2318 /* force scroll info update */
2319 EDIT_UpdateScrollInfo(es
);
2322 /* Windows doesn't care to fix text placement for SL controls */
2323 es
->format_rect
.bottom
= es
->format_rect
.top
+ es
->line_height
;
2325 /* Always stay within the client area */
2326 GetClientRect(es
->hwndSelf
, &ClientRect
);
2327 es
->format_rect
.bottom
= min(es
->format_rect
.bottom
, ClientRect
.bottom
);
2329 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
))
2330 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2332 EDIT_SetCaretPos(es
, es
->selection_end
, es
->flags
& EF_AFTER_WRAP
);
2336 /*********************************************************************
2340 * note: this is not (exactly) the handler called on EM_SETRECTNP
2341 * it is also used to set the rect of a single line control
2344 static void EDIT_SetRectNP(EDITSTATE
*es
, const RECT
*rc
)
2348 ExStyle
= GetWindowLongPtrW(es
->hwndSelf
, GWL_EXSTYLE
);
2350 CopyRect(&es
->format_rect
, rc
);
2352 if (ExStyle
& WS_EX_CLIENTEDGE
) {
2353 es
->format_rect
.left
++;
2354 es
->format_rect
.right
--;
2356 if (es
->format_rect
.bottom
- es
->format_rect
.top
2357 >= es
->line_height
+ 2)
2359 es
->format_rect
.top
++;
2360 es
->format_rect
.bottom
--;
2363 else if (es
->style
& WS_BORDER
) {
2364 bw
= GetSystemMetrics(SM_CXBORDER
) + 1;
2365 bh
= GetSystemMetrics(SM_CYBORDER
) + 1;
2366 InflateRect(&es
->format_rect
, -bw
, 0);
2367 if (es
->format_rect
.bottom
- es
->format_rect
.top
>= es
->line_height
+ 2 * bh
)
2368 InflateRect(&es
->format_rect
, 0, -bh
);
2371 es
->format_rect
.left
+= es
->left_margin
;
2372 es
->format_rect
.right
-= es
->right_margin
;
2373 EDIT_AdjustFormatRect(es
);
2377 /*********************************************************************
2381 * returns line number (not index) in high-order word of result.
2382 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2383 * to Richedit, not to the edit control. Original documentation is valid.
2384 * FIXME: do the specs mean to return -1 if outside client area or
2385 * if outside formatting rectangle ???
2388 static LRESULT
EDIT_EM_CharFromPos(EDITSTATE
*es
, INT x
, INT y
)
2396 GetClientRect(es
->hwndSelf
, &rc
);
2397 if (!PtInRect(&rc
, pt
))
2400 index
= EDIT_CharFromPos(es
, x
, y
, NULL
);
2401 return MAKELONG(index
, EDIT_EM_LineFromChar(es
, index
));
2405 /*********************************************************************
2409 * Enable or disable soft breaks.
2411 * This means: insert or remove the soft linebreak character (\r\r\n).
2412 * Take care to check if the text still fits the buffer after insertion.
2413 * If not, notify with EN_ERRSPACE.
2416 static BOOL
EDIT_EM_FmtLines(EDITSTATE
*es
, BOOL add_eol
)
2418 es
->flags
&= ~EF_USE_SOFTBRK
;
2420 es
->flags
|= EF_USE_SOFTBRK
;
2421 FIXME("soft break enabled, not implemented\n");
2427 /*********************************************************************
2431 * Hopefully this won't fire back at us.
2432 * We always start with a fixed buffer in the local heap.
2433 * Despite of the documentation says that the local heap is used
2434 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2435 * buffer on the local heap.
2438 static HLOCAL
EDIT_EM_GetHandle(EDITSTATE
*es
)
2442 if (!(es
->style
& ES_MULTILINE
))
2446 hLocal
= es
->hloc32W
;
2452 UINT countA
, alloc_size
;
2453 TRACE("Allocating 32-bit ANSI alias buffer\n");
2454 countA
= WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, NULL
, 0, NULL
, NULL
);
2455 alloc_size
= ROUND_TO_GROW(countA
);
2456 if(!(es
->hloc32A
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
2458 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size
);
2461 textA
= LocalLock(es
->hloc32A
);
2462 WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, countA
, NULL
, NULL
);
2463 LocalUnlock(es
->hloc32A
);
2465 hLocal
= es
->hloc32A
;
2468 EDIT_UnlockBuffer(es
, TRUE
);
2470 /* The text buffer handle belongs to the app */
2471 es
->hlocapp
= hLocal
;
2473 TRACE("Returning %p, LocalSize() = %ld\n", hLocal
, LocalSize(hLocal
));
2478 /*********************************************************************
2483 static INT
EDIT_EM_GetLine(EDITSTATE
*es
, INT line
, LPWSTR dst
, BOOL unicode
)
2486 INT line_len
, dst_len
;
2489 if (es
->style
& ES_MULTILINE
) {
2490 if (line
>= es
->line_count
)
2494 i
= EDIT_EM_LineIndex(es
, line
);
2496 line_len
= EDIT_EM_LineLength(es
, i
);
2497 dst_len
= *(WORD
*)dst
;
2500 if(dst_len
<= line_len
)
2502 memcpy(dst
, src
, dst_len
* sizeof(WCHAR
));
2505 else /* Append 0 if enough space */
2507 memcpy(dst
, src
, line_len
* sizeof(WCHAR
));
2514 INT ret
= WideCharToMultiByte(CP_ACP
, 0, src
, line_len
, (LPSTR
)dst
, dst_len
, NULL
, NULL
);
2515 if(!ret
&& line_len
) /* Insufficient buffer size */
2517 if(ret
< dst_len
) /* Append 0 if enough space */
2518 ((LPSTR
)dst
)[ret
] = 0;
2524 /*********************************************************************
2529 static LRESULT
EDIT_EM_GetSel(const EDITSTATE
*es
, PUINT start
, PUINT end
)
2531 UINT s
= es
->selection_start
;
2532 UINT e
= es
->selection_end
;
2539 return MAKELONG(s
, e
);
2543 /*********************************************************************
2547 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2550 static void EDIT_EM_ReplaceSel(EDITSTATE
*es
, BOOL can_undo
, const WCHAR
*lpsz_replace
, UINT strl
,
2551 BOOL send_update
, BOOL honor_limit
)
2553 UINT tl
= get_text_length(es
);
2564 TRACE("%s, can_undo %d, send_update %d\n",
2565 debugstr_wn(lpsz_replace
, strl
), can_undo
, send_update
);
2567 s
= es
->selection_start
;
2568 e
= es
->selection_end
;
2570 EDIT_InvalidateUniscribeData(es
);
2571 if ((s
== e
) && !strl
)
2576 size
= tl
- (e
- s
) + strl
;
2580 /* Issue the EN_MAXTEXT notification and continue with replacing text
2581 * so that buffer limit is honored. */
2582 if ((honor_limit
) && (size
> es
->buffer_limit
)) {
2583 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2584 /* Buffer limit can be smaller than the actual length of text in combobox */
2585 if (es
->buffer_limit
< (tl
- (e
-s
)))
2588 strl
= min(strl
, es
->buffer_limit
- (tl
- (e
-s
)));
2591 if (!EDIT_MakeFit(es
, tl
- (e
- s
) + strl
))
2595 /* there is something to be deleted */
2596 TRACE("deleting stuff.\n");
2598 buf
= HeapAlloc(GetProcessHeap(), 0, (bufl
+ 1) * sizeof(WCHAR
));
2600 memcpy(buf
, es
->text
+ s
, bufl
* sizeof(WCHAR
));
2601 buf
[bufl
] = 0; /* ensure 0 termination */
2603 strcpyW(es
->text
+ s
, es
->text
+ e
);
2604 text_buffer_changed(es
);
2607 /* there is an insertion */
2608 tl
= get_text_length(es
);
2609 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
));
2610 for (p
= es
->text
+ tl
; p
>= es
->text
+ s
; p
--)
2612 for (i
= 0 , p
= es
->text
+ s
; i
< strl
; i
++)
2613 p
[i
] = lpsz_replace
[i
];
2614 if(es
->style
& ES_UPPERCASE
)
2615 CharUpperBuffW(p
, strl
);
2616 else if(es
->style
& ES_LOWERCASE
)
2617 CharLowerBuffW(p
, strl
);
2618 text_buffer_changed(es
);
2620 if (es
->style
& ES_MULTILINE
)
2622 INT st
= min(es
->selection_start
, es
->selection_end
);
2623 INT vlc
= get_vertical_line_count(es
);
2625 hrgn
= CreateRectRgn(0, 0, 0, 0);
2626 EDIT_BuildLineDefs_ML(es
, st
, st
+ strl
,
2627 strl
- abs(es
->selection_end
- es
->selection_start
), hrgn
);
2628 /* if text is too long undo all changes */
2629 if (honor_limit
&& !(es
->style
& ES_AUTOVSCROLL
) && (es
->line_count
> vlc
)) {
2631 strcpyW(es
->text
+ e
, es
->text
+ e
+ strl
);
2633 for (i
= 0 , p
= es
->text
; i
< e
- s
; i
++)
2635 text_buffer_changed(es
);
2636 EDIT_BuildLineDefs_ML(es
, s
, e
,
2637 abs(es
->selection_end
- es
->selection_start
) - strl
, hrgn
);
2640 hrgn
= CreateRectRgn(0, 0, 0, 0);
2641 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2645 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
2646 EDIT_InvalidateUniscribeData(es
);
2647 EDIT_CalcLineWidth_SL(es
);
2648 /* remove chars that don't fit */
2649 if (honor_limit
&& !(es
->style
& ES_AUTOHSCROLL
) && (es
->text_width
> fw
)) {
2650 while ((es
->text_width
> fw
) && s
+ strl
>= s
) {
2651 strcpyW(es
->text
+ s
+ strl
- 1, es
->text
+ s
+ strl
);
2653 es
->text_length
= -1;
2654 EDIT_InvalidateUniscribeData(es
);
2655 EDIT_CalcLineWidth_SL(es
);
2657 text_buffer_changed(es
);
2658 EDIT_NOTIFY_PARENT(es
, EN_MAXTEXT
);
2664 utl
= strlenW(es
->undo_text
);
2665 if (!es
->undo_insert_count
&& (*es
->undo_text
&& (s
== es
->undo_position
))) {
2666 /* undo-buffer is extended to the right */
2667 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2668 memcpy(es
->undo_text
+ utl
, buf
, (e
- s
)*sizeof(WCHAR
));
2669 (es
->undo_text
+ utl
)[e
- s
] = 0; /* ensure 0 termination */
2670 } else if (!es
->undo_insert_count
&& (*es
->undo_text
&& (e
== es
->undo_position
))) {
2671 /* undo-buffer is extended to the left */
2672 EDIT_MakeUndoFit(es
, utl
+ e
- s
);
2673 for (p
= es
->undo_text
+ utl
; p
>= es
->undo_text
; p
--)
2675 for (i
= 0 , p
= es
->undo_text
; i
< e
- s
; i
++)
2677 es
->undo_position
= s
;
2679 /* new undo-buffer */
2680 EDIT_MakeUndoFit(es
, e
- s
);
2681 memcpy(es
->undo_text
, buf
, (e
- s
)*sizeof(WCHAR
));
2682 es
->undo_text
[e
- s
] = 0; /* ensure 0 termination */
2683 es
->undo_position
= s
;
2685 /* any deletion makes the old insertion-undo invalid */
2686 es
->undo_insert_count
= 0;
2688 EDIT_EM_EmptyUndoBuffer(es
);
2692 if ((s
== es
->undo_position
) ||
2693 ((es
->undo_insert_count
) &&
2694 (s
== es
->undo_position
+ es
->undo_insert_count
)))
2696 * insertion is new and at delete position or
2697 * an extension to either left or right
2699 es
->undo_insert_count
+= strl
;
2701 /* new insertion undo */
2702 es
->undo_position
= s
;
2703 es
->undo_insert_count
= strl
;
2704 /* new insertion makes old delete-buffer invalid */
2705 *es
->undo_text
= '\0';
2708 EDIT_EM_EmptyUndoBuffer(es
);
2711 HeapFree(GetProcessHeap(), 0, buf
);
2715 /* If text has been deleted and we're right or center aligned then scroll rightward */
2716 if (es
->style
& (ES_RIGHT
| ES_CENTER
))
2718 INT delta
= strl
- abs(es
->selection_end
- es
->selection_start
);
2720 if (delta
< 0 && es
->x_offset
)
2722 if (abs(delta
) > es
->x_offset
)
2725 es
->x_offset
+= delta
;
2729 EDIT_EM_SetSel(es
, s
, s
, FALSE
);
2730 es
->flags
|= EF_MODIFIED
;
2731 if (send_update
) es
->flags
|= EF_UPDATE
;
2734 EDIT_UpdateTextRegion(es
, hrgn
, TRUE
);
2738 EDIT_UpdateText(es
, NULL
, TRUE
);
2740 EDIT_EM_ScrollCaret(es
);
2742 /* force scroll info update */
2743 EDIT_UpdateScrollInfo(es
);
2746 if(send_update
|| (es
->flags
& EF_UPDATE
))
2748 es
->flags
&= ~EF_UPDATE
;
2749 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
2751 EDIT_InvalidateUniscribeData(es
);
2755 /*********************************************************************
2759 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2762 static void EDIT_EM_SetHandle(EDITSTATE
*es
, HLOCAL hloc
)
2764 if (!(es
->style
& ES_MULTILINE
))
2768 WARN("called with NULL handle\n");
2772 EDIT_UnlockBuffer(es
, TRUE
);
2778 LocalFree(es
->hloc32A
);
2790 countA
= LocalSize(hloc
);
2791 textA
= LocalLock(hloc
);
2792 countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, NULL
, 0);
2793 if(!(hloc32W_new
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, countW
* sizeof(WCHAR
))))
2795 ERR("Could not allocate new unicode buffer\n");
2798 textW
= LocalLock(hloc32W_new
);
2799 MultiByteToWideChar(CP_ACP
, 0, textA
, countA
, textW
, countW
);
2800 LocalUnlock(hloc32W_new
);
2804 LocalFree(es
->hloc32W
);
2806 es
->hloc32W
= hloc32W_new
;
2810 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
2812 /* The text buffer handle belongs to the control */
2815 EDIT_LockBuffer(es
);
2816 text_buffer_changed(es
);
2818 es
->x_offset
= es
->y_offset
= 0;
2819 es
->selection_start
= es
->selection_end
= 0;
2820 EDIT_EM_EmptyUndoBuffer(es
);
2821 es
->flags
&= ~EF_MODIFIED
;
2822 es
->flags
&= ~EF_UPDATE
;
2823 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
2824 EDIT_UpdateText(es
, NULL
, TRUE
);
2825 EDIT_EM_ScrollCaret(es
);
2826 /* force scroll info update */
2827 EDIT_UpdateScrollInfo(es
);
2831 /*********************************************************************
2835 * NOTE: this version currently implements WinNT limits
2838 static void EDIT_EM_SetLimitText(EDITSTATE
*es
, UINT limit
)
2840 if (!limit
) limit
= ~0u;
2841 if (!(es
->style
& ES_MULTILINE
)) limit
= min(limit
, 0x7ffffffe);
2842 es
->buffer_limit
= limit
;
2846 /*********************************************************************
2850 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2851 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2852 * margin according to the textmetrics of the current font.
2854 * When EC_USEFONTINFO is used in the non_cjk case the margins only
2855 * change if the edit control is equal to or larger than a certain
2856 * size. Though there is an exception for the empty client rect case
2857 * with small font sizes.
2859 static BOOL
is_cjk(UINT charset
)
2863 case SHIFTJIS_CHARSET
:
2864 case HANGUL_CHARSET
:
2865 case GB2312_CHARSET
:
2866 case CHINESEBIG5_CHARSET
:
2869 /* HANGUL_CHARSET is strange, though treated as CJK by Win 8, it is
2870 * not by other versions including Win 10. */
2874 static void EDIT_EM_SetMargins(EDITSTATE
*es
, INT action
,
2875 WORD left
, WORD right
, BOOL repaint
)
2878 INT default_left_margin
= 0; /* in pixels */
2879 INT default_right_margin
= 0; /* in pixels */
2881 /* Set the default margins depending on the font */
2882 if (es
->font
&& (left
== EC_USEFONTINFO
|| right
== EC_USEFONTINFO
)) {
2883 HDC dc
= GetDC(es
->hwndSelf
);
2884 HFONT old_font
= SelectObject(dc
, es
->font
);
2885 LONG width
= GdiGetCharDimensions(dc
, &tm
, NULL
);
2888 /* The default margins are only non zero for TrueType or Vector fonts */
2889 if (tm
.tmPitchAndFamily
& ( TMPF_VECTOR
| TMPF_TRUETYPE
)) {
2890 if (!is_cjk(tm
.tmCharSet
)) {
2891 default_left_margin
= width
/ 2;
2892 default_right_margin
= width
/ 2;
2894 GetClientRect(es
->hwndSelf
, &rc
);
2895 if (rc
.right
- rc
.left
< (width
/ 2 + width
) * 2 &&
2896 (width
>= 28 || !IsRectEmpty(&rc
)) ) {
2897 default_left_margin
= es
->left_margin
;
2898 default_right_margin
= es
->right_margin
;
2901 /* FIXME: figure out the CJK values. They are not affected by the client rect. */
2902 default_left_margin
= width
/ 2;
2903 default_right_margin
= width
/ 2;
2906 SelectObject(dc
, old_font
);
2907 ReleaseDC(es
->hwndSelf
, dc
);
2910 if (action
& EC_LEFTMARGIN
) {
2911 es
->format_rect
.left
-= es
->left_margin
;
2912 if (left
!= EC_USEFONTINFO
)
2913 es
->left_margin
= left
;
2915 es
->left_margin
= default_left_margin
;
2916 es
->format_rect
.left
+= es
->left_margin
;
2919 if (action
& EC_RIGHTMARGIN
) {
2920 es
->format_rect
.right
+= es
->right_margin
;
2921 if (right
!= EC_USEFONTINFO
)
2922 es
->right_margin
= right
;
2924 es
->right_margin
= default_right_margin
;
2925 es
->format_rect
.right
-= es
->right_margin
;
2928 if (action
& (EC_LEFTMARGIN
| EC_RIGHTMARGIN
)) {
2929 EDIT_AdjustFormatRect(es
);
2930 if (repaint
) EDIT_UpdateText(es
, NULL
, TRUE
);
2933 TRACE("left=%d, right=%d\n", es
->left_margin
, es
->right_margin
);
2937 /*********************************************************************
2939 * EM_SETPASSWORDCHAR
2942 static void EDIT_EM_SetPasswordChar(EDITSTATE
*es
, WCHAR c
)
2946 if (es
->style
& ES_MULTILINE
)
2949 if (es
->password_char
== c
)
2952 style
= GetWindowLongW( es
->hwndSelf
, GWL_STYLE
);
2953 es
->password_char
= c
;
2955 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
| ES_PASSWORD
);
2956 es
->style
|= ES_PASSWORD
;
2958 SetWindowLongW( es
->hwndSelf
, GWL_STYLE
, style
& ~ES_PASSWORD
);
2959 es
->style
&= ~ES_PASSWORD
;
2961 EDIT_InvalidateUniscribeData(es
);
2962 EDIT_UpdateText(es
, NULL
, TRUE
);
2966 /*********************************************************************
2971 static BOOL
EDIT_EM_SetTabStops(EDITSTATE
*es
, INT count
, const INT
*tabs
)
2973 if (!(es
->style
& ES_MULTILINE
))
2975 HeapFree(GetProcessHeap(), 0, es
->tabs
);
2976 es
->tabs_count
= count
;
2980 es
->tabs
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(INT
));
2981 memcpy(es
->tabs
, tabs
, count
* sizeof(INT
));
2983 EDIT_InvalidateUniscribeData(es
);
2988 /*********************************************************************
2990 * EM_SETWORDBREAKPROC
2993 static void EDIT_EM_SetWordBreakProc(EDITSTATE
*es
, void *wbp
)
2995 if (es
->word_break_proc
== wbp
)
2998 es
->word_break_proc
= wbp
;
3000 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_AUTOHSCROLL
)) {
3001 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
3002 EDIT_UpdateText(es
, NULL
, TRUE
);
3007 /*********************************************************************
3012 static BOOL
EDIT_EM_Undo(EDITSTATE
*es
)
3017 /* As per MSDN spec, for a single-line edit control,
3018 the return value is always TRUE */
3019 if( es
->style
& ES_READONLY
)
3020 return !(es
->style
& ES_MULTILINE
);
3022 ulength
= strlenW(es
->undo_text
);
3024 utext
= HeapAlloc(GetProcessHeap(), 0, (ulength
+ 1) * sizeof(WCHAR
));
3026 strcpyW(utext
, es
->undo_text
);
3028 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3029 es
->undo_insert_count
, debugstr_w(utext
));
3031 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3032 EDIT_EM_EmptyUndoBuffer(es
);
3033 EDIT_EM_ReplaceSel(es
, TRUE
, utext
, ulength
, TRUE
, TRUE
);
3034 EDIT_EM_SetSel(es
, es
->undo_position
, es
->undo_position
+ es
->undo_insert_count
, FALSE
);
3035 /* send the notification after the selection start and end are set */
3036 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
3037 EDIT_EM_ScrollCaret(es
);
3038 HeapFree(GetProcessHeap(), 0, utext
);
3040 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3041 es
->undo_insert_count
, debugstr_w(es
->undo_text
));
3046 /* Helper function for WM_CHAR
3048 * According to an MSDN blog article titled "Just because you're a control
3049 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3050 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3051 * a dialog box or not.
3053 static inline BOOL
EDIT_IsInsideDialog(EDITSTATE
*es
)
3055 return (es
->flags
& EF_DIALOGMODE
);
3059 /*********************************************************************
3064 static void EDIT_WM_Paste(EDITSTATE
*es
)
3070 /* Protect read-only edit control from modification */
3071 if(es
->style
& ES_READONLY
)
3074 OpenClipboard(es
->hwndSelf
);
3075 if ((hsrc
= GetClipboardData(CF_UNICODETEXT
))) {
3076 src
= GlobalLock(hsrc
);
3078 /* Protect single-line edit against pasting new line character */
3079 if (!(es
->style
& ES_MULTILINE
) && ((ptr
= strchrW(src
, '\n')))) {
3081 if (len
&& src
[len
- 1] == '\r')
3084 EDIT_EM_ReplaceSel(es
, TRUE
, src
, len
, TRUE
, TRUE
);
3087 else if (es
->style
& ES_PASSWORD
) {
3088 /* clear selected text in password edit box even with empty clipboard */
3089 EDIT_EM_ReplaceSel(es
, TRUE
, NULL
, 0, TRUE
, TRUE
);
3095 /*********************************************************************
3100 static void EDIT_WM_Copy(EDITSTATE
*es
)
3102 INT s
= min(es
->selection_start
, es
->selection_end
);
3103 INT e
= max(es
->selection_start
, es
->selection_end
);
3111 hdst
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, (len
+ 1) * sizeof(WCHAR
));
3112 dst
= GlobalLock(hdst
);
3113 memcpy(dst
, es
->text
+ s
, len
* sizeof(WCHAR
));
3114 dst
[len
] = 0; /* ensure 0 termination */
3115 TRACE("%s\n", debugstr_w(dst
));
3117 OpenClipboard(es
->hwndSelf
);
3119 SetClipboardData(CF_UNICODETEXT
, hdst
);
3124 /*********************************************************************
3129 static inline void EDIT_WM_Clear(EDITSTATE
*es
)
3131 /* Protect read-only edit control from modification */
3132 if(es
->style
& ES_READONLY
)
3135 EDIT_EM_ReplaceSel(es
, TRUE
, NULL
, 0, TRUE
, TRUE
);
3139 /*********************************************************************
3144 static inline void EDIT_WM_Cut(EDITSTATE
*es
)
3151 /*********************************************************************
3156 static LRESULT
EDIT_WM_Char(EDITSTATE
*es
, WCHAR c
)
3160 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3164 /* If it's not a multiline edit box, it would be ignored below.
3165 * For multiline edit without ES_WANTRETURN, we have to make a
3168 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_WANTRETURN
))
3169 if (EDIT_IsInsideDialog(es
))
3172 if (es
->style
& ES_MULTILINE
) {
3173 if (es
->style
& ES_READONLY
) {
3174 EDIT_MoveHome(es
, FALSE
, FALSE
);
3175 EDIT_MoveDown_ML(es
, FALSE
);
3177 static const WCHAR cr_lfW
[] = {'\r','\n'};
3178 EDIT_EM_ReplaceSel(es
, TRUE
, cr_lfW
, 2, TRUE
, TRUE
);
3183 if ((es
->style
& ES_MULTILINE
) && !(es
->style
& ES_READONLY
))
3185 static const WCHAR tabW
[] = {'\t'};
3186 if (EDIT_IsInsideDialog(es
))
3188 EDIT_EM_ReplaceSel(es
, TRUE
, tabW
, 1, TRUE
, TRUE
);
3192 if (!(es
->style
& ES_READONLY
) && !control
) {
3193 if (es
->selection_start
!= es
->selection_end
)
3196 /* delete character left of caret */
3197 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3198 EDIT_MoveBackward(es
, TRUE
);
3204 if (!(es
->style
& ES_PASSWORD
))
3205 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3208 if (!(es
->style
& ES_READONLY
))
3209 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3212 if (!((es
->style
& ES_READONLY
) || (es
->style
& ES_PASSWORD
)))
3213 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3216 if (!(es
->style
& ES_READONLY
))
3217 SendMessageW(es
->hwndSelf
, WM_UNDO
, 0, 0);
3221 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3222 if( (es
->style
& ES_NUMBER
) && !( c
>= '0' && c
<= '9') )
3225 if (!(es
->style
& ES_READONLY
) && (c
>= ' ') && (c
!= 127))
3226 EDIT_EM_ReplaceSel(es
, TRUE
, &c
, 1, TRUE
, TRUE
);
3233 /*********************************************************************
3235 * EDIT_ContextMenuCommand
3238 static void EDIT_ContextMenuCommand(EDITSTATE
*es
, UINT id
)
3242 SendMessageW(es
->hwndSelf
, WM_UNDO
, 0, 0);
3245 SendMessageW(es
->hwndSelf
, WM_CUT
, 0, 0);
3248 SendMessageW(es
->hwndSelf
, WM_COPY
, 0, 0);
3251 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3254 SendMessageW(es
->hwndSelf
, WM_CLEAR
, 0, 0);
3257 SendMessageW(es
->hwndSelf
, EM_SETSEL
, 0, -1);
3260 ERR("unknown menu item, please report\n");
3266 /*********************************************************************
3270 * Note: the resource files resource/sysres_??.rc cannot define a
3271 * single popup menu. Hence we use a (dummy) menubar
3272 * containing the single popup menu as its first item.
3274 * FIXME: the message identifiers have been chosen arbitrarily,
3275 * hence we use MF_BYPOSITION.
3276 * We might as well use the "real" values (anybody knows ?)
3277 * The menu definition is in resources/sysres_??.rc.
3278 * Once these are OK, we better use MF_BYCOMMAND here
3279 * (as we do in EDIT_WM_Command()).
3282 static void EDIT_WM_ContextMenu(EDITSTATE
*es
, INT x
, INT y
)
3284 HMENU menu
= LoadMenuA(user32_module
, "EDITMENU");
3285 HMENU popup
= GetSubMenu(menu
, 0);
3286 UINT start
= es
->selection_start
;
3287 UINT end
= es
->selection_end
;
3290 ORDER_UINT(start
, end
);
3293 EnableMenuItem(popup
, 0, MF_BYPOSITION
| (EDIT_EM_CanUndo(es
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3295 EnableMenuItem(popup
, 2, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3297 EnableMenuItem(popup
, 3, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_PASSWORD
) ? MF_ENABLED
: MF_GRAYED
));
3299 EnableMenuItem(popup
, 4, MF_BYPOSITION
| (IsClipboardFormatAvailable(CF_UNICODETEXT
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3301 EnableMenuItem(popup
, 5, MF_BYPOSITION
| ((end
- start
) && !(es
->style
& ES_READONLY
) ? MF_ENABLED
: MF_GRAYED
));
3303 EnableMenuItem(popup
, 7, MF_BYPOSITION
| (start
|| (end
!= get_text_length(es
)) ? MF_ENABLED
: MF_GRAYED
));
3305 if (x
== -1 && y
== -1) /* passed via VK_APPS press/release */
3308 /* Windows places the menu at the edit's center in this case */
3309 WIN_GetRectangles( es
->hwndSelf
, COORDS_SCREEN
, NULL
, &rc
);
3310 x
= rc
.left
+ (rc
.right
- rc
.left
) / 2;
3311 y
= rc
.top
+ (rc
.bottom
- rc
.top
) / 2;
3314 if (!(es
->flags
& EF_FOCUSED
))
3315 SetFocus(es
->hwndSelf
);
3317 cmd
= TrackPopupMenu(popup
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
| TPM_RETURNCMD
| TPM_NONOTIFY
,
3318 x
, y
, 0, es
->hwndSelf
, NULL
);
3321 EDIT_ContextMenuCommand(es
, cmd
);
3327 /*********************************************************************
3332 static INT
EDIT_WM_GetText(const EDITSTATE
*es
, INT count
, LPWSTR dst
, BOOL unicode
)
3334 if(!count
) return 0;
3338 lstrcpynW(dst
, es
->text
, count
);
3339 return strlenW(dst
);
3343 LPSTR textA
= (LPSTR
)dst
;
3344 if (!WideCharToMultiByte(CP_ACP
, 0, es
->text
, -1, textA
, count
, NULL
, NULL
))
3345 textA
[count
- 1] = 0; /* ensure 0 termination */
3346 return strlen(textA
);
3350 /*********************************************************************
3355 static BOOL
EDIT_CheckCombo(EDITSTATE
*es
, UINT msg
, INT key
)
3357 HWND hLBox
= es
->hwndListBox
;
3365 hCombo
= GetParent(es
->hwndSelf
);
3369 TRACE_(combo
)("[%p]: handling msg %x (%x)\n", es
->hwndSelf
, msg
, key
);
3371 if (key
== VK_UP
|| key
== VK_DOWN
)
3373 if (SendMessageW(hCombo
, CB_GETEXTENDEDUI
, 0, 0))
3376 if (msg
== WM_KEYDOWN
|| nEUI
)
3377 bDropped
= (BOOL
)SendMessageW(hCombo
, CB_GETDROPPEDSTATE
, 0, 0);
3383 if (!bDropped
&& nEUI
&& (key
== VK_UP
|| key
== VK_DOWN
))
3385 /* make sure ComboLBox pops up */
3386 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, FALSE
, 0);
3391 SendMessageW(hLBox
, WM_KEYDOWN
, key
, 0);
3394 case WM_SYSKEYDOWN
: /* Handle Alt+up/down arrows */
3396 SendMessageW(hCombo
, CB_SHOWDROPDOWN
, !bDropped
, 0);
3398 SendMessageW(hLBox
, WM_KEYDOWN
, VK_F4
, 0);
3403 SendMessageW(hCombo
, CB_SETEXTENDEDUI
, TRUE
, 0);
3409 /*********************************************************************
3413 * Handling of special keys that don't produce a WM_CHAR
3414 * (i.e. non-printable keys) & Backspace & Delete
3417 static LRESULT
EDIT_WM_KeyDown(EDITSTATE
*es
, INT key
)
3422 if (GetKeyState(VK_MENU
) & 0x8000)
3425 shift
= GetKeyState(VK_SHIFT
) & 0x8000;
3426 control
= GetKeyState(VK_CONTROL
) & 0x8000;
3431 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
) || key
== VK_F4
)
3436 if ((es
->style
& ES_MULTILINE
) && (key
== VK_UP
))
3437 EDIT_MoveUp_ML(es
, shift
);
3440 EDIT_MoveWordBackward(es
, shift
);
3442 EDIT_MoveBackward(es
, shift
);
3445 if (EDIT_CheckCombo(es
, WM_KEYDOWN
, key
))
3449 if ((es
->style
& ES_MULTILINE
) && (key
== VK_DOWN
))
3450 EDIT_MoveDown_ML(es
, shift
);
3452 EDIT_MoveWordForward(es
, shift
);
3454 EDIT_MoveForward(es
, shift
);
3457 EDIT_MoveHome(es
, shift
, control
);
3460 EDIT_MoveEnd(es
, shift
, control
);
3463 if (es
->style
& ES_MULTILINE
)
3464 EDIT_MovePageUp_ML(es
, shift
);
3466 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
3469 if (es
->style
& ES_MULTILINE
)
3470 EDIT_MovePageDown_ML(es
, shift
);
3472 EDIT_CheckCombo(es
, WM_KEYDOWN
, key
);
3475 if (!(es
->style
& ES_READONLY
) && !(shift
&& control
)) {
3476 if (es
->selection_start
!= es
->selection_end
) {
3483 /* delete character left of caret */
3484 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3485 EDIT_MoveBackward(es
, TRUE
);
3487 } else if (control
) {
3488 /* delete to end of line */
3489 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3490 EDIT_MoveEnd(es
, TRUE
, FALSE
);
3493 /* delete character right of caret */
3494 EDIT_EM_SetSel(es
, (UINT
)-1, 0, FALSE
);
3495 EDIT_MoveForward(es
, TRUE
);
3503 if (!(es
->style
& ES_READONLY
))
3509 /* If the edit doesn't want the return send a message to the default object */
3510 if(!(es
->style
& ES_MULTILINE
) || !(es
->style
& ES_WANTRETURN
))
3514 if (!EDIT_IsInsideDialog(es
)) break;
3516 dw
= SendMessageW(es
->hwndParent
, DM_GETDEFID
, 0, 0);
3517 if (HIWORD(dw
) == DC_HASDEFID
)
3519 HWND hwDefCtrl
= GetDlgItem(es
->hwndParent
, LOWORD(dw
));
3522 SendMessageW(es
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
3523 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
3529 if ((es
->style
& ES_MULTILINE
) && EDIT_IsInsideDialog(es
))
3530 PostMessageW(es
->hwndParent
, WM_CLOSE
, 0, 0);
3533 if ((es
->style
& ES_MULTILINE
) && EDIT_IsInsideDialog(es
))
3534 SendMessageW(es
->hwndParent
, WM_NEXTDLGCTL
, shift
, 0);
3541 /*********************************************************************
3546 static LRESULT
EDIT_WM_KillFocus(EDITSTATE
*es
)
3548 es
->flags
&= ~EF_FOCUSED
;
3550 if(!(es
->style
& ES_NOHIDESEL
))
3551 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
3552 EDIT_NOTIFY_PARENT(es
, EN_KILLFOCUS
);
3553 /* throw away left over scroll when we lose focus */
3554 es
->wheelDeltaRemainder
= 0;
3559 /*********************************************************************
3563 * The caret position has been set on the WM_LBUTTONDOWN message
3566 static LRESULT
EDIT_WM_LButtonDblClk(EDITSTATE
*es
)
3569 INT e
= es
->selection_end
;
3574 es
->bCaptureState
= TRUE
;
3575 SetCapture(es
->hwndSelf
);
3577 l
= EDIT_EM_LineFromChar(es
, e
);
3578 li
= EDIT_EM_LineIndex(es
, l
);
3579 ll
= EDIT_EM_LineLength(es
, e
);
3580 s
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_LEFT
);
3581 e
= li
+ EDIT_CallWordBreakProc(es
, li
, e
- li
, ll
, WB_RIGHT
);
3582 EDIT_EM_SetSel(es
, s
, e
, FALSE
);
3583 EDIT_EM_ScrollCaret(es
);
3584 es
->region_posx
= es
->region_posy
= 0;
3585 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
3590 /*********************************************************************
3595 static LRESULT
EDIT_WM_LButtonDown(EDITSTATE
*es
, DWORD keys
, INT x
, INT y
)
3600 es
->bCaptureState
= TRUE
;
3601 SetCapture(es
->hwndSelf
);
3602 EDIT_ConfinePoint(es
, &x
, &y
);
3603 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
3604 EDIT_EM_SetSel(es
, (keys
& MK_SHIFT
) ? es
->selection_start
: e
, e
, after_wrap
);
3605 EDIT_EM_ScrollCaret(es
);
3606 es
->region_posx
= es
->region_posy
= 0;
3607 SetTimer(es
->hwndSelf
, 0, 100, NULL
);
3609 if (!(es
->flags
& EF_FOCUSED
))
3610 SetFocus(es
->hwndSelf
);
3616 /*********************************************************************
3621 static LRESULT
EDIT_WM_LButtonUp(EDITSTATE
*es
)
3623 if (es
->bCaptureState
) {
3624 KillTimer(es
->hwndSelf
, 0);
3625 if (GetCapture() == es
->hwndSelf
) ReleaseCapture();
3627 es
->bCaptureState
= FALSE
;
3632 /*********************************************************************
3637 static LRESULT
EDIT_WM_MButtonDown(EDITSTATE
*es
)
3639 SendMessageW(es
->hwndSelf
, WM_PASTE
, 0, 0);
3644 /*********************************************************************
3649 static LRESULT
EDIT_WM_MouseMove(EDITSTATE
*es
, INT x
, INT y
)
3655 /* If the mouse has been captured by process other than the edit control itself,
3656 * the windows edit controls will not select the strings with mouse move.
3658 if (!es
->bCaptureState
|| GetCapture() != es
->hwndSelf
)
3662 * FIXME: gotta do some scrolling if outside client
3663 * area. Maybe reset the timer ?
3666 EDIT_ConfinePoint(es
, &x
, &y
);
3667 es
->region_posx
= (prex
< x
) ? -1 : ((prex
> x
) ? 1 : 0);
3668 es
->region_posy
= (prey
< y
) ? -1 : ((prey
> y
) ? 1 : 0);
3669 e
= EDIT_CharFromPos(es
, x
, y
, &after_wrap
);
3670 EDIT_EM_SetSel(es
, es
->selection_start
, e
, after_wrap
);
3671 EDIT_SetCaretPos(es
,es
->selection_end
,es
->flags
& EF_AFTER_WRAP
);
3676 /*********************************************************************
3681 static void EDIT_WM_Paint(EDITSTATE
*es
, HDC hdc
)
3694 BOOL rev
= es
->bEnableState
&&
3695 ((es
->flags
& EF_FOCUSED
) ||
3696 (es
->style
& ES_NOHIDESEL
));
3697 dc
= hdc
? hdc
: BeginPaint(es
->hwndSelf
, &ps
);
3699 /* The dc we use for calculating may not be the one we paint into.
3700 This is the safest action. */
3701 EDIT_InvalidateUniscribeData(es
);
3702 GetClientRect(es
->hwndSelf
, &rcClient
);
3704 /* get the background brush */
3705 brush
= EDIT_NotifyCtlColor(es
, dc
);
3707 /* paint the border and the background */
3708 IntersectClipRect(dc
, rcClient
.left
, rcClient
.top
, rcClient
.right
, rcClient
.bottom
);
3710 if(es
->style
& WS_BORDER
) {
3711 bw
= GetSystemMetrics(SM_CXBORDER
);
3712 bh
= GetSystemMetrics(SM_CYBORDER
);
3714 if(es
->style
& ES_MULTILINE
) {
3715 if(es
->style
& WS_HSCROLL
) rc
.bottom
+=bh
;
3716 if(es
->style
& WS_VSCROLL
) rc
.right
+=bw
;
3719 /* Draw the frame. Same code as in nonclient.c */
3720 old_brush
= SelectObject(dc
, GetSysColorBrush(COLOR_WINDOWFRAME
));
3721 PatBlt(dc
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, bh
, PATCOPY
);
3722 PatBlt(dc
, rc
.left
, rc
.top
, bw
, rc
.bottom
- rc
.top
, PATCOPY
);
3723 PatBlt(dc
, rc
.left
, rc
.bottom
- 1, rc
.right
- rc
.left
, -bw
, PATCOPY
);
3724 PatBlt(dc
, rc
.right
- 1, rc
.top
, -bw
, rc
.bottom
- rc
.top
, PATCOPY
);
3725 SelectObject(dc
, old_brush
);
3727 /* Keep the border clean */
3728 IntersectClipRect(dc
, rc
.left
+bw
, rc
.top
+bh
,
3729 max(rc
.right
-bw
, rc
.left
+bw
), max(rc
.bottom
-bh
, rc
.top
+bh
));
3732 GetClipBox(dc
, &rc
);
3733 FillRect(dc
, &rc
, brush
);
3735 IntersectClipRect(dc
, es
->format_rect
.left
,
3736 es
->format_rect
.top
,
3737 es
->format_rect
.right
,
3738 es
->format_rect
.bottom
);
3739 if (es
->style
& ES_MULTILINE
) {
3741 IntersectClipRect(dc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
3744 old_font
= SelectObject(dc
, es
->font
);
3746 if (!es
->bEnableState
)
3747 SetTextColor(dc
, GetSysColor(COLOR_GRAYTEXT
));
3748 GetClipBox(dc
, &rcRgn
);
3749 if (es
->style
& ES_MULTILINE
) {
3750 INT vlc
= get_vertical_line_count(es
);
3751 for (i
= es
->y_offset
; i
<= min(es
->y_offset
+ vlc
, es
->y_offset
+ es
->line_count
- 1) ; i
++) {
3752 EDIT_UpdateUniscribeData(es
, dc
, i
);
3753 EDIT_GetLineRect(es
, i
, 0, -1, &rcLine
);
3754 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
3755 EDIT_PaintLine(es
, dc
, i
, rev
);
3758 EDIT_UpdateUniscribeData(es
, dc
, 0);
3759 EDIT_GetLineRect(es
, 0, 0, -1, &rcLine
);
3760 if (IntersectRect(&rc
, &rcRgn
, &rcLine
))
3761 EDIT_PaintLine(es
, dc
, 0, rev
);
3764 SelectObject(dc
, old_font
);
3767 EndPaint(es
->hwndSelf
, &ps
);
3771 /*********************************************************************
3776 static void EDIT_WM_SetFocus(EDITSTATE
*es
)
3778 es
->flags
|= EF_FOCUSED
;
3780 if (!(es
->style
& ES_NOHIDESEL
))
3781 EDIT_InvalidateText(es
, es
->selection_start
, es
->selection_end
);
3783 /* single line edit updates itself */
3784 if (IsWindowVisible(es
->hwndSelf
) && !(es
->style
& ES_MULTILINE
))
3786 HDC hdc
= GetDC(es
->hwndSelf
);
3787 EDIT_WM_Paint(es
, hdc
);
3788 ReleaseDC(es
->hwndSelf
, hdc
);
3791 CreateCaret(es
->hwndSelf
, 0, 1, es
->line_height
);
3792 EDIT_SetCaretPos(es
, es
->selection_end
,
3793 es
->flags
& EF_AFTER_WRAP
);
3794 ShowCaret(es
->hwndSelf
);
3795 EDIT_NOTIFY_PARENT(es
, EN_SETFOCUS
);
3799 /*********************************************************************
3803 * With Win95 look the margins are set to default font value unless
3804 * the system font (font == 0) is being set, in which case they are left
3808 static void EDIT_WM_SetFont(EDITSTATE
*es
, HFONT font
, BOOL redraw
)
3816 EDIT_InvalidateUniscribeData(es
);
3817 dc
= GetDC(es
->hwndSelf
);
3819 old_font
= SelectObject(dc
, font
);
3820 GetTextMetricsW(dc
, &tm
);
3821 es
->line_height
= tm
.tmHeight
;
3822 es
->char_width
= tm
.tmAveCharWidth
;
3824 SelectObject(dc
, old_font
);
3825 ReleaseDC(es
->hwndSelf
, dc
);
3827 /* Reset the format rect and the margins */
3828 GetClientRect(es
->hwndSelf
, &clientRect
);
3829 EDIT_SetRectNP(es
, &clientRect
);
3830 EDIT_EM_SetMargins(es
, EC_LEFTMARGIN
| EC_RIGHTMARGIN
,
3831 EC_USEFONTINFO
, EC_USEFONTINFO
, FALSE
);
3833 if (es
->style
& ES_MULTILINE
)
3834 EDIT_BuildLineDefs_ML(es
, 0, get_text_length(es
), 0, NULL
);
3836 EDIT_CalcLineWidth_SL(es
);
3839 EDIT_UpdateText(es
, NULL
, TRUE
);
3840 if (es
->flags
& EF_FOCUSED
) {
3842 CreateCaret(es
->hwndSelf
, 0, 1, es
->line_height
);
3843 EDIT_SetCaretPos(es
, es
->selection_end
,
3844 es
->flags
& EF_AFTER_WRAP
);
3845 ShowCaret(es
->hwndSelf
);
3850 /*********************************************************************
3855 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3856 * The modified flag is reset. No notifications are sent.
3858 * For single-line controls, reception of WM_SETTEXT triggers:
3859 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3862 static void EDIT_WM_SetText(EDITSTATE
*es
, LPCWSTR text
, BOOL unicode
)
3864 LPWSTR textW
= NULL
;
3865 if (!unicode
&& text
)
3867 LPCSTR textA
= (LPCSTR
)text
;
3868 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
3869 textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
));
3871 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
3875 if (es
->flags
& EF_UPDATE
)
3876 /* fixed this bug once; complain if we see it about to happen again. */
3877 ERR("SetSel may generate UPDATE message whose handler may reset "
3880 EDIT_EM_SetSel(es
, 0, (UINT
)-1, FALSE
);
3883 TRACE("%s\n", debugstr_w(text
));
3884 EDIT_EM_ReplaceSel(es
, FALSE
, text
, strlenW(text
), FALSE
, FALSE
);
3886 HeapFree(GetProcessHeap(), 0, textW
);
3891 EDIT_EM_ReplaceSel(es
, FALSE
, NULL
, 0, FALSE
, FALSE
);
3894 es
->flags
&= ~EF_MODIFIED
;
3895 EDIT_EM_SetSel(es
, 0, 0, FALSE
);
3897 /* Send the notification after the selection start and end have been set
3898 * edit control doesn't send notification on WM_SETTEXT
3899 * if it is multiline, or it is part of combobox
3901 if( !((es
->style
& ES_MULTILINE
) || es
->hwndListBox
))
3903 EDIT_NOTIFY_PARENT(es
, EN_UPDATE
);
3904 EDIT_NOTIFY_PARENT(es
, EN_CHANGE
);
3906 EDIT_EM_ScrollCaret(es
);
3907 EDIT_UpdateScrollInfo(es
);
3908 EDIT_InvalidateUniscribeData(es
);
3912 /*********************************************************************
3917 static void EDIT_WM_Size(EDITSTATE
*es
, UINT action
)
3919 if ((action
== SIZE_MAXIMIZED
) || (action
== SIZE_RESTORED
)) {
3921 GetClientRect(es
->hwndSelf
, &rc
);
3922 EDIT_SetRectNP(es
, &rc
);
3923 EDIT_UpdateText(es
, NULL
, TRUE
);
3928 /*********************************************************************
3932 * This message is sent by SetWindowLong on having changed either the Style
3933 * or the extended style.
3935 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3937 * See also EDIT_WM_NCCreate
3939 * It appears that the Windows version of the edit control allows the style
3940 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3941 * style variable which will generally be different. In this function we
3942 * update the internal style based on what changed in the externally visible
3945 * Much of this content as based upon the MSDN, especially:
3946 * Platform SDK Documentation -> User Interface Services ->
3947 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3948 * Edit Control Styles
3950 static LRESULT
EDIT_WM_StyleChanged ( EDITSTATE
*es
, WPARAM which
, const STYLESTRUCT
*style
)
3952 if (GWL_STYLE
== which
) {
3953 DWORD style_change_mask
;
3955 /* Only a subset of changes can be applied after the control
3958 style_change_mask
= ES_UPPERCASE
| ES_LOWERCASE
|
3960 if (es
->style
& ES_MULTILINE
)
3961 style_change_mask
|= ES_WANTRETURN
;
3963 new_style
= style
->styleNew
& style_change_mask
;
3965 /* Number overrides lowercase overrides uppercase (at least it
3966 * does in Win95). However I'll bet that ES_NUMBER would be
3967 * invalid under Win 3.1.
3969 if (new_style
& ES_NUMBER
) {
3970 ; /* do not override the ES_NUMBER */
3971 } else if (new_style
& ES_LOWERCASE
) {
3972 new_style
&= ~ES_UPPERCASE
;
3975 es
->style
= (es
->style
& ~style_change_mask
) | new_style
;
3976 } else if (GWL_EXSTYLE
== which
) {
3977 ; /* FIXME - what is needed here */
3979 WARN ("Invalid style change %ld\n",which
);
3985 /*********************************************************************
3990 static LRESULT
EDIT_WM_SysKeyDown(EDITSTATE
*es
, INT key
, DWORD key_data
)
3992 if ((key
== VK_BACK
) && (key_data
& 0x2000)) {
3993 if (EDIT_EM_CanUndo(es
))
3996 } else if (key
== VK_UP
|| key
== VK_DOWN
) {
3997 if (EDIT_CheckCombo(es
, WM_SYSKEYDOWN
, key
))
4000 return DefWindowProcW(es
->hwndSelf
, WM_SYSKEYDOWN
, key
, key_data
);
4004 /*********************************************************************
4009 static void EDIT_WM_Timer(EDITSTATE
*es
)
4011 if (es
->region_posx
< 0) {
4012 EDIT_MoveBackward(es
, TRUE
);
4013 } else if (es
->region_posx
> 0) {
4014 EDIT_MoveForward(es
, TRUE
);
4017 * FIXME: gotta do some vertical scrolling here, like
4018 * EDIT_EM_LineScroll(hwnd, 0, 1);
4022 /*********************************************************************
4027 static LRESULT
EDIT_WM_HScroll(EDITSTATE
*es
, INT action
, INT pos
)
4032 if (!(es
->style
& ES_MULTILINE
))
4035 if (!(es
->style
& ES_AUTOHSCROLL
))
4039 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4042 TRACE("SB_LINELEFT\n");
4044 dx
= -es
->char_width
;
4047 TRACE("SB_LINERIGHT\n");
4048 if (es
->x_offset
< es
->text_width
)
4049 dx
= es
->char_width
;
4052 TRACE("SB_PAGELEFT\n");
4054 dx
= -fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4057 TRACE("SB_PAGERIGHT\n");
4058 if (es
->x_offset
< es
->text_width
)
4059 dx
= fw
/ HSCROLL_FRACTION
/ es
->char_width
* es
->char_width
;
4067 TRACE("SB_RIGHT\n");
4068 if (es
->x_offset
< es
->text_width
)
4069 dx
= es
->text_width
- es
->x_offset
;
4072 TRACE("SB_THUMBTRACK %d\n", pos
);
4073 es
->flags
|= EF_HSCROLL_TRACK
;
4074 if(es
->style
& WS_HSCROLL
)
4075 dx
= pos
- es
->x_offset
;
4080 if(pos
< 0 || pos
> 100) return 0;
4081 /* Assume default scroll range 0-100 */
4082 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4083 new_x
= pos
* (es
->text_width
- fw
) / 100;
4084 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4087 case SB_THUMBPOSITION
:
4088 TRACE("SB_THUMBPOSITION %d\n", pos
);
4089 es
->flags
&= ~EF_HSCROLL_TRACK
;
4090 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4091 dx
= pos
- es
->x_offset
;
4096 if(pos
< 0 || pos
> 100) return 0;
4097 /* Assume default scroll range 0-100 */
4098 fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4099 new_x
= pos
* (es
->text_width
- fw
) / 100;
4100 dx
= es
->text_width
? (new_x
- es
->x_offset
) : 0;
4103 /* force scroll info update */
4104 EDIT_UpdateScrollInfo(es
);
4105 EDIT_NOTIFY_PARENT(es
, EN_HSCROLL
);
4109 TRACE("SB_ENDSCROLL\n");
4112 * FIXME : the next two are undocumented !
4113 * Are we doing the right thing ?
4114 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4115 * although it's also a regular control message.
4117 case EM_GETTHUMB
: /* this one is used by NT notepad */
4120 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_HSCROLL
)
4121 ret
= GetScrollPos(es
->hwndSelf
, SB_HORZ
);
4124 /* Assume default scroll range 0-100 */
4125 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4126 ret
= es
->text_width
? es
->x_offset
* 100 / (es
->text_width
- fw
) : 0;
4128 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4132 TRACE("EM_LINESCROLL16\n");
4137 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4143 INT fw
= es
->format_rect
.right
- es
->format_rect
.left
;
4144 /* check if we are going to move too far */
4145 if(es
->x_offset
+ dx
+ fw
> es
->text_width
)
4146 dx
= es
->text_width
- fw
- es
->x_offset
;
4148 EDIT_EM_LineScroll_internal(es
, dx
, 0);
4154 /*********************************************************************
4159 static LRESULT
EDIT_WM_VScroll(EDITSTATE
*es
, INT action
, INT pos
)
4163 if (!(es
->style
& ES_MULTILINE
))
4166 if (!(es
->style
& ES_AUTOVSCROLL
))
4175 TRACE("action %d (%s)\n", action
, (action
== SB_LINEUP
? "SB_LINEUP" :
4176 (action
== SB_LINEDOWN
? "SB_LINEDOWN" :
4177 (action
== SB_PAGEUP
? "SB_PAGEUP" :
4179 EDIT_EM_Scroll(es
, action
);
4186 TRACE("SB_BOTTOM\n");
4187 dy
= es
->line_count
- 1 - es
->y_offset
;
4190 TRACE("SB_THUMBTRACK %d\n", pos
);
4191 es
->flags
|= EF_VSCROLL_TRACK
;
4192 if(es
->style
& WS_VSCROLL
)
4193 dy
= pos
- es
->y_offset
;
4196 /* Assume default scroll range 0-100 */
4199 if(pos
< 0 || pos
> 100) return 0;
4200 vlc
= get_vertical_line_count(es
);
4201 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4202 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4203 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4204 es
->line_count
, es
->y_offset
, pos
, dy
);
4207 case SB_THUMBPOSITION
:
4208 TRACE("SB_THUMBPOSITION %d\n", pos
);
4209 es
->flags
&= ~EF_VSCROLL_TRACK
;
4210 if(es
->style
& WS_VSCROLL
)
4211 dy
= pos
- es
->y_offset
;
4214 /* Assume default scroll range 0-100 */
4217 if(pos
< 0 || pos
> 100) return 0;
4218 vlc
= get_vertical_line_count(es
);
4219 new_y
= pos
* (es
->line_count
- vlc
) / 100;
4220 dy
= es
->line_count
? (new_y
- es
->y_offset
) : 0;
4221 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4222 es
->line_count
, es
->y_offset
, pos
, dy
);
4226 /* force scroll info update */
4227 EDIT_UpdateScrollInfo(es
);
4228 EDIT_NOTIFY_PARENT(es
, EN_VSCROLL
);
4232 TRACE("SB_ENDSCROLL\n");
4235 * FIXME : the next two are undocumented !
4236 * Are we doing the right thing ?
4237 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4238 * although it's also a regular control message.
4240 case EM_GETTHUMB
: /* this one is used by NT notepad */
4243 if(GetWindowLongW( es
->hwndSelf
, GWL_STYLE
) & WS_VSCROLL
)
4244 ret
= GetScrollPos(es
->hwndSelf
, SB_VERT
);
4247 /* Assume default scroll range 0-100 */
4248 INT vlc
= get_vertical_line_count(es
);
4249 ret
= es
->line_count
? es
->y_offset
* 100 / (es
->line_count
- vlc
) : 0;
4251 TRACE("EM_GETTHUMB: returning %ld\n", ret
);
4255 TRACE("EM_LINESCROLL %d\n", pos
);
4260 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4265 EDIT_EM_LineScroll(es
, 0, dy
);
4269 /*********************************************************************
4273 * FIXME: is this right ? (or should it be only VSCROLL)
4274 * (and maybe only for edit controls that really have their
4275 * own scrollbars) (and maybe only for multiline controls ?)
4276 * All in all: very poorly documented
4279 static LRESULT
EDIT_EM_GetThumb(EDITSTATE
*es
)
4281 return MAKELONG(EDIT_WM_VScroll(es
, EM_GETTHUMB
, 0),
4282 EDIT_WM_HScroll(es
, EM_GETTHUMB
, 0));
4286 /********************************************************************
4288 * The Following code is to handle inline editing from IMEs
4291 static void EDIT_GetCompositionStr(HIMC hIMC
, LPARAM CompFlag
, EDITSTATE
*es
)
4295 LPSTR lpCompStrAttr
= NULL
;
4298 buflen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
4305 lpCompStr
= HeapAlloc(GetProcessHeap(),0,buflen
);
4308 ERR("Unable to allocate IME CompositionString\n");
4313 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, buflen
);
4315 if (CompFlag
& GCS_COMPATTR
)
4318 * We do not use the attributes yet. it would tell us what characters
4319 * are in transition and which are converted or decided upon
4321 dwBufLenAttr
= ImmGetCompositionStringW(hIMC
, GCS_COMPATTR
, NULL
, 0);
4325 lpCompStrAttr
= HeapAlloc(GetProcessHeap(),0,dwBufLenAttr
+1);
4328 ERR("Unable to allocate IME Attribute String\n");
4329 HeapFree(GetProcessHeap(),0,lpCompStr
);
4332 ImmGetCompositionStringW(hIMC
,GCS_COMPATTR
, lpCompStrAttr
,
4334 lpCompStrAttr
[dwBufLenAttr
] = 0;
4338 /* check for change in composition start */
4339 if (es
->selection_end
< es
->composition_start
)
4340 es
->composition_start
= es
->selection_end
;
4342 /* replace existing selection string */
4343 es
->selection_start
= es
->composition_start
;
4345 if (es
->composition_len
> 0)
4346 es
->selection_end
= es
->composition_start
+ es
->composition_len
;
4348 es
->selection_end
= es
->selection_start
;
4350 EDIT_EM_ReplaceSel(es
, FALSE
, lpCompStr
, buflen
/ sizeof(WCHAR
), TRUE
, TRUE
);
4351 es
->composition_len
= abs(es
->composition_start
- es
->selection_end
);
4353 es
->selection_start
= es
->composition_start
;
4354 es
->selection_end
= es
->selection_start
+ es
->composition_len
;
4356 HeapFree(GetProcessHeap(),0,lpCompStrAttr
);
4357 HeapFree(GetProcessHeap(),0,lpCompStr
);
4360 static void EDIT_GetResultStr(HIMC hIMC
, EDITSTATE
*es
)
4365 buflen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
4371 lpResultStr
= HeapAlloc(GetProcessHeap(),0, buflen
);
4374 ERR("Unable to alloc buffer for IME string\n");
4378 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpResultStr
, buflen
);
4380 /* check for change in composition start */
4381 if (es
->selection_end
< es
->composition_start
)
4382 es
->composition_start
= es
->selection_end
;
4384 es
->selection_start
= es
->composition_start
;
4385 es
->selection_end
= es
->composition_start
+ es
->composition_len
;
4386 EDIT_EM_ReplaceSel(es
, TRUE
, lpResultStr
, buflen
/ sizeof(WCHAR
), TRUE
, TRUE
);
4387 es
->composition_start
= es
->selection_end
;
4388 es
->composition_len
= 0;
4390 HeapFree(GetProcessHeap(),0,lpResultStr
);
4393 static void EDIT_ImeComposition(HWND hwnd
, LPARAM CompFlag
, EDITSTATE
*es
)
4398 if (es
->composition_len
== 0 && es
->selection_start
!= es
->selection_end
)
4400 EDIT_EM_ReplaceSel(es
, TRUE
, NULL
, 0, TRUE
, TRUE
);
4401 es
->composition_start
= es
->selection_end
;
4404 hIMC
= ImmGetContext(hwnd
);
4408 if (CompFlag
& GCS_RESULTSTR
)
4410 EDIT_GetResultStr(hIMC
, es
);
4415 if (CompFlag
& GCS_COMPSTR
)
4416 EDIT_GetCompositionStr(hIMC
, CompFlag
, es
);
4417 cursor
= ImmGetCompositionStringW(hIMC
, GCS_CURSORPOS
, 0, 0);
4419 ImmReleaseContext(hwnd
, hIMC
);
4420 EDIT_SetCaretPos(es
, es
->selection_start
+ cursor
, es
->flags
& EF_AFTER_WRAP
);
4424 /*********************************************************************
4428 * See also EDIT_WM_StyleChanged
4430 static LRESULT
EDIT_WM_NCCreate(HWND hwnd
, LPCREATESTRUCTW lpcs
, BOOL unicode
)
4435 TRACE("Creating %s edit control, style = %08x\n",
4436 unicode
? "Unicode" : "ANSI", lpcs
->style
);
4438 if (!(es
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*es
))))
4440 SetWindowLongPtrW( hwnd
, 0, (LONG_PTR
)es
);
4443 * Note: since the EDITSTATE has not been fully initialized yet,
4444 * we can't use any API calls that may send
4445 * WM_XXX messages before WM_NCCREATE is completed.
4448 es
->is_unicode
= unicode
;
4449 es
->style
= lpcs
->style
;
4451 es
->bEnableState
= !(es
->style
& WS_DISABLED
);
4453 es
->hwndSelf
= hwnd
;
4454 /* Save parent, which will be notified by EN_* messages */
4455 es
->hwndParent
= lpcs
->hwndParent
;
4457 if (es
->style
& ES_COMBO
)
4458 es
->hwndListBox
= GetDlgItem(es
->hwndParent
, ID_CB_LISTBOX
);
4460 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4461 if (lpcs
->dwExStyle
& WS_EX_RIGHT
) es
->style
|= ES_RIGHT
;
4463 /* Number overrides lowercase overrides uppercase (at least it
4464 * does in Win95). However I'll bet that ES_NUMBER would be
4465 * invalid under Win 3.1.
4467 if (es
->style
& ES_NUMBER
) {
4468 ; /* do not override the ES_NUMBER */
4469 } else if (es
->style
& ES_LOWERCASE
) {
4470 es
->style
&= ~ES_UPPERCASE
;
4472 if (es
->style
& ES_MULTILINE
) {
4473 es
->buffer_limit
= BUFLIMIT_INITIAL
;
4474 if (es
->style
& WS_VSCROLL
)
4475 es
->style
|= ES_AUTOVSCROLL
;
4476 if (es
->style
& WS_HSCROLL
)
4477 es
->style
|= ES_AUTOHSCROLL
;
4478 es
->style
&= ~ES_PASSWORD
;
4479 if ((es
->style
& ES_CENTER
) || (es
->style
& ES_RIGHT
)) {
4480 /* Confirmed - RIGHT overrides CENTER */
4481 if (es
->style
& ES_RIGHT
)
4482 es
->style
&= ~ES_CENTER
;
4483 es
->style
&= ~WS_HSCROLL
;
4484 es
->style
&= ~ES_AUTOHSCROLL
;
4487 es
->buffer_limit
= BUFLIMIT_INITIAL
;
4488 if ((es
->style
& ES_RIGHT
) && (es
->style
& ES_CENTER
))
4489 es
->style
&= ~ES_CENTER
;
4490 es
->style
&= ~WS_HSCROLL
;
4491 es
->style
&= ~WS_VSCROLL
;
4492 if (es
->style
& ES_PASSWORD
)
4493 es
->password_char
= '*';
4496 alloc_size
= ROUND_TO_GROW((es
->buffer_size
+ 1) * sizeof(WCHAR
));
4497 if(!(es
->hloc32W
= LocalAlloc(LMEM_MOVEABLE
| LMEM_ZEROINIT
, alloc_size
)))
4499 es
->buffer_size
= LocalSize(es
->hloc32W
)/sizeof(WCHAR
) - 1;
4501 if (!(es
->undo_text
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (es
->buffer_size
+ 1) * sizeof(WCHAR
))))
4503 es
->undo_buffer_size
= es
->buffer_size
;
4505 if (es
->style
& ES_MULTILINE
)
4506 if (!(es
->first_line_def
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINEDEF
))))
4511 * In Win95 look and feel, the WS_BORDER style is replaced by the
4512 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4513 * control a nonclient area so we don't need to draw the border.
4514 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4515 * a nonclient area and we should handle painting the border ourselves.
4517 * When making modifications please ensure that the code still works
4518 * for edit controls created directly with style 0x50800000, exStyle 0
4519 * (which should have a single pixel border)
4521 if (lpcs
->dwExStyle
& WS_EX_CLIENTEDGE
)
4522 es
->style
&= ~WS_BORDER
;
4523 else if (es
->style
& WS_BORDER
)
4524 SetWindowLongW(hwnd
, GWL_STYLE
, es
->style
& ~WS_BORDER
);
4529 SetWindowLongPtrW(es
->hwndSelf
, 0, 0);
4530 EDIT_InvalidateUniscribeData(es
);
4531 HeapFree(GetProcessHeap(), 0, es
->first_line_def
);
4532 HeapFree(GetProcessHeap(), 0, es
->undo_text
);
4533 if (es
->hloc32W
) LocalFree(es
->hloc32W
);
4534 HeapFree(GetProcessHeap(), 0, es
->logAttr
);
4535 HeapFree(GetProcessHeap(), 0, es
);
4540 /*********************************************************************
4545 static LRESULT
EDIT_WM_Create(EDITSTATE
*es
, LPCWSTR name
)
4549 TRACE("%s\n", debugstr_w(name
));
4551 * To initialize some final structure members, we call some helper
4552 * functions. However, since the EDITSTATE is not consistent (i.e.
4553 * not fully initialized), we should be very careful which
4554 * functions can be called, and in what order.
4556 EDIT_WM_SetFont(es
, 0, FALSE
);
4557 EDIT_EM_EmptyUndoBuffer(es
);
4559 /* We need to calculate the format rect
4560 (applications may send EM_SETMARGINS before the control gets visible) */
4561 GetClientRect(es
->hwndSelf
, &clientRect
);
4562 EDIT_SetRectNP(es
, &clientRect
);
4564 if (name
&& *name
) {
4565 EDIT_EM_ReplaceSel(es
, FALSE
, name
, strlenW(name
), FALSE
, FALSE
);
4566 /* if we insert text to the editline, the text scrolls out
4567 * of the window, as the caret is placed after the insert
4568 * pos normally; thus we reset es->selection... to 0 and
4571 es
->selection_start
= es
->selection_end
= 0;
4572 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4573 * Messages are only to be sent when the USER does something to
4574 * change the contents. So I am removing this EN_CHANGE
4576 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4578 EDIT_EM_ScrollCaret(es
);
4580 /* force scroll info update */
4581 EDIT_UpdateScrollInfo(es
);
4582 /* The rule seems to return 1 here for success */
4583 /* Power Builder masked edit controls will crash */
4585 /* FIXME: is that in all cases so ? */
4590 /*********************************************************************
4595 static LRESULT
EDIT_WM_NCDestroy(EDITSTATE
*es
)
4599 /* The app can own the text buffer handle */
4600 if (es
->hloc32W
&& (es
->hloc32W
!= es
->hlocapp
)) {
4601 LocalFree(es
->hloc32W
);
4603 if (es
->hloc32A
&& (es
->hloc32A
!= es
->hlocapp
)) {
4604 LocalFree(es
->hloc32A
);
4606 EDIT_InvalidateUniscribeData(es
);
4607 pc
= es
->first_line_def
;
4611 HeapFree(GetProcessHeap(), 0, pc
);
4615 SetWindowLongPtrW( es
->hwndSelf
, 0, 0 );
4616 HeapFree(GetProcessHeap(), 0, es
->undo_text
);
4617 HeapFree(GetProcessHeap(), 0, es
);
4623 static inline LRESULT
DefWindowProcT(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
4626 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
4628 return DefWindowProcA(hwnd
, msg
, wParam
, lParam
);
4631 /*********************************************************************
4633 * EditWndProc_common
4635 * The messages are in the order of the actual integer values
4636 * (which can be found in include/windows.h)
4638 LRESULT
EditWndProc_common( HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
4640 EDITSTATE
*es
= (EDITSTATE
*)GetWindowLongPtrW( hwnd
, 0 );
4643 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), wParam
, lParam
);
4645 if (!es
&& msg
!= WM_NCCREATE
)
4646 return DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
4648 if (es
&& (msg
!= WM_NCDESTROY
)) EDIT_LockBuffer(es
);
4652 result
= EDIT_EM_GetSel(es
, (PUINT
)wParam
, (PUINT
)lParam
);
4656 EDIT_EM_SetSel(es
, wParam
, lParam
, FALSE
);
4657 EDIT_EM_ScrollCaret(es
);
4663 *((LPRECT
)lParam
) = es
->format_rect
;
4667 if ((es
->style
& ES_MULTILINE
) && lParam
) {
4668 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
4669 EDIT_UpdateText(es
, NULL
, TRUE
);
4674 if ((es
->style
& ES_MULTILINE
) && lParam
)
4675 EDIT_SetRectNP(es
, (LPRECT
)lParam
);
4679 result
= EDIT_EM_Scroll(es
, (INT
)wParam
);
4683 result
= (LRESULT
)EDIT_EM_LineScroll(es
, (INT
)wParam
, (INT
)lParam
);
4686 case EM_SCROLLCARET
:
4687 EDIT_EM_ScrollCaret(es
);
4692 result
= ((es
->flags
& EF_MODIFIED
) != 0);
4697 es
->flags
|= EF_MODIFIED
;
4699 es
->flags
&= ~(EF_MODIFIED
| EF_UPDATE
); /* reset pending updates */
4702 case EM_GETLINECOUNT
:
4703 result
= (es
->style
& ES_MULTILINE
) ? es
->line_count
: 1;
4707 result
= (LRESULT
)EDIT_EM_LineIndex(es
, (INT
)wParam
);
4711 EDIT_EM_SetHandle(es
, (HLOCAL
)wParam
);
4715 result
= (LRESULT
)EDIT_EM_GetHandle(es
);
4719 result
= EDIT_EM_GetThumb(es
);
4722 /* these messages missing from specs */
4727 FIXME("undocumented message 0x%x, please report\n", msg
);
4728 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
4732 result
= (LRESULT
)EDIT_EM_LineLength(es
, (INT
)wParam
);
4740 textW
= (LPWSTR
)lParam
;
4743 LPSTR textA
= (LPSTR
)lParam
;
4744 INT countW
= MultiByteToWideChar(CP_ACP
, 0, textA
, -1, NULL
, 0);
4745 if (!(textW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
)))) break;
4746 MultiByteToWideChar(CP_ACP
, 0, textA
, -1, textW
, countW
);
4749 EDIT_EM_ReplaceSel(es
, (BOOL
)wParam
, textW
, strlenW(textW
), TRUE
, TRUE
);
4753 HeapFree(GetProcessHeap(), 0, textW
);
4758 result
= (LRESULT
)EDIT_EM_GetLine(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
4761 case EM_SETLIMITTEXT
:
4762 EDIT_EM_SetLimitText(es
, wParam
);
4766 result
= (LRESULT
)EDIT_EM_CanUndo(es
);
4771 result
= (LRESULT
)EDIT_EM_Undo(es
);
4775 result
= (LRESULT
)EDIT_EM_FmtLines(es
, (BOOL
)wParam
);
4778 case EM_LINEFROMCHAR
:
4779 result
= (LRESULT
)EDIT_EM_LineFromChar(es
, (INT
)wParam
);
4782 case EM_SETTABSTOPS
:
4783 result
= (LRESULT
)EDIT_EM_SetTabStops(es
, (INT
)wParam
, (LPINT
)lParam
);
4786 case EM_SETPASSWORDCHAR
:
4791 charW
= (WCHAR
)wParam
;
4794 CHAR charA
= wParam
;
4795 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
4798 EDIT_EM_SetPasswordChar(es
, charW
);
4802 case EM_EMPTYUNDOBUFFER
:
4803 EDIT_EM_EmptyUndoBuffer(es
);
4806 case EM_GETFIRSTVISIBLELINE
:
4807 result
= (es
->style
& ES_MULTILINE
) ? es
->y_offset
: es
->x_offset
;
4810 case EM_SETREADONLY
:
4812 DWORD old_style
= es
->style
;
4815 SetWindowLongW( hwnd
, GWL_STYLE
,
4816 GetWindowLongW( hwnd
, GWL_STYLE
) | ES_READONLY
);
4817 es
->style
|= ES_READONLY
;
4819 SetWindowLongW( hwnd
, GWL_STYLE
,
4820 GetWindowLongW( hwnd
, GWL_STYLE
) & ~ES_READONLY
);
4821 es
->style
&= ~ES_READONLY
;
4824 if (old_style
^ es
->style
)
4825 InvalidateRect(es
->hwndSelf
, NULL
, TRUE
);
4831 case EM_SETWORDBREAKPROC
:
4832 EDIT_EM_SetWordBreakProc(es
, (void *)lParam
);
4836 case EM_GETWORDBREAKPROC
:
4837 result
= (LRESULT
)es
->word_break_proc
;
4840 case EM_GETPASSWORDCHAR
:
4843 result
= es
->password_char
;
4846 WCHAR charW
= es
->password_char
;
4848 WideCharToMultiByte(CP_ACP
, 0, &charW
, 1, &charA
, 1, NULL
, NULL
);
4855 EDIT_EM_SetMargins(es
, (INT
)wParam
, LOWORD(lParam
), HIWORD(lParam
), TRUE
);
4859 result
= MAKELONG(es
->left_margin
, es
->right_margin
);
4862 case EM_GETLIMITTEXT
:
4863 result
= es
->buffer_limit
;
4866 case EM_POSFROMCHAR
:
4867 if ((INT
)wParam
>= get_text_length(es
)) result
= -1;
4868 else result
= EDIT_EM_PosFromChar(es
, (INT
)wParam
, FALSE
);
4871 case EM_CHARFROMPOS
:
4872 result
= EDIT_EM_CharFromPos(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4875 /* End of the EM_ messages which were in numerical order; what order
4876 * are these in? vaguely alphabetical?
4880 result
= EDIT_WM_NCCreate(hwnd
, (LPCREATESTRUCTW
)lParam
, unicode
);
4884 result
= EDIT_WM_NCDestroy(es
);
4889 result
= DLGC_HASSETSEL
| DLGC_WANTCHARS
| DLGC_WANTARROWS
;
4891 if (es
->style
& ES_MULTILINE
)
4892 result
|= DLGC_WANTALLKEYS
;
4896 es
->flags
|=EF_DIALOGMODE
;
4898 if (((LPMSG
)lParam
)->message
== WM_KEYDOWN
)
4900 int vk
= (int)((LPMSG
)lParam
)->wParam
;
4902 if (es
->hwndListBox
)
4904 if (vk
== VK_RETURN
|| vk
== VK_ESCAPE
)
4905 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
4906 result
|= DLGC_WANTMESSAGE
;
4918 strng
[0] = wParam
>> 8;
4919 strng
[1] = wParam
& 0xff;
4920 if (strng
[0]) MultiByteToWideChar(CP_ACP
, 0, strng
, 2, &charW
, 1);
4921 else MultiByteToWideChar(CP_ACP
, 0, &strng
[1], 1, &charW
, 1);
4922 result
= EDIT_WM_Char(es
, charW
);
4934 CHAR charA
= wParam
;
4935 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &charW
, 1);
4938 if (es
->hwndListBox
)
4940 if (charW
== VK_RETURN
|| charW
== VK_ESCAPE
)
4942 if (SendMessageW(GetParent(hwnd
), CB_GETDROPPEDSTATE
, 0, 0))
4943 SendMessageW(GetParent(hwnd
), WM_KEYDOWN
, charW
, 0);
4947 result
= EDIT_WM_Char(es
, charW
);
4954 if (wParam
== UNICODE_NOCHAR
) return TRUE
;
4955 if (wParam
<= 0x000fffff)
4957 if(wParam
> 0xffff) /* convert to surrogates */
4960 EDIT_WM_Char(es
, (wParam
>> 10) + 0xd800);
4961 EDIT_WM_Char(es
, (wParam
& 0x03ff) + 0xdc00);
4963 else EDIT_WM_Char(es
, wParam
);
4973 case WM_CONTEXTMENU
:
4974 EDIT_WM_ContextMenu(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4983 result
= EDIT_WM_Create(es
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
4986 LPCSTR nameA
= ((LPCREATESTRUCTA
)lParam
)->lpszName
;
4987 LPWSTR nameW
= NULL
;
4990 INT countW
= MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, NULL
, 0);
4991 if((nameW
= HeapAlloc(GetProcessHeap(), 0, countW
* sizeof(WCHAR
))))
4992 MultiByteToWideChar(CP_ACP
, 0, nameA
, -1, nameW
, countW
);
4994 result
= EDIT_WM_Create(es
, nameW
);
4995 HeapFree(GetProcessHeap(), 0, nameW
);
5004 es
->bEnableState
= (BOOL
) wParam
;
5005 EDIT_UpdateText(es
, NULL
, TRUE
);
5009 /* we do the proper erase in EDIT_WM_Paint */
5014 result
= (LRESULT
)es
->font
;
5018 result
= (LRESULT
)EDIT_WM_GetText(es
, (INT
)wParam
, (LPWSTR
)lParam
, unicode
);
5021 case WM_GETTEXTLENGTH
:
5022 if (unicode
) result
= get_text_length(es
);
5023 else result
= WideCharToMultiByte( CP_ACP
, 0, es
->text
, get_text_length(es
),
5024 NULL
, 0, NULL
, NULL
);
5028 result
= EDIT_WM_HScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
5032 result
= EDIT_WM_KeyDown(es
, (INT
)wParam
);
5036 result
= EDIT_WM_KillFocus(es
);
5039 case WM_LBUTTONDBLCLK
:
5040 result
= EDIT_WM_LButtonDblClk(es
);
5043 case WM_LBUTTONDOWN
:
5044 result
= EDIT_WM_LButtonDown(es
, wParam
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5048 result
= EDIT_WM_LButtonUp(es
);
5051 case WM_MBUTTONDOWN
:
5052 result
= EDIT_WM_MButtonDown(es
);
5056 result
= EDIT_WM_MouseMove(es
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
5059 case WM_PRINTCLIENT
:
5061 EDIT_WM_Paint(es
, (HDC
)wParam
);
5069 EDIT_WM_SetFocus(es
);
5073 EDIT_WM_SetFont(es
, (HFONT
)wParam
, LOWORD(lParam
) != 0);
5077 /* FIXME: actually set an internal flag and behave accordingly */
5081 EDIT_WM_SetText(es
, (LPCWSTR
)lParam
, unicode
);
5086 EDIT_WM_Size(es
, (UINT
)wParam
);
5089 case WM_STYLECHANGED
:
5090 result
= EDIT_WM_StyleChanged(es
, wParam
, (const STYLESTRUCT
*)lParam
);
5093 case WM_STYLECHANGING
:
5094 result
= 0; /* See EDIT_WM_StyleChanged */
5098 result
= EDIT_WM_SysKeyDown(es
, (INT
)wParam
, (DWORD
)lParam
);
5106 result
= EDIT_WM_VScroll(es
, LOWORD(wParam
), (short)HIWORD(wParam
));
5112 UINT pulScrollLines
= 3;
5113 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
5115 if (wParam
& (MK_SHIFT
| MK_CONTROL
)) {
5116 result
= DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
5119 wheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
5120 /* if scrolling changes direction, ignore left overs */
5121 if ((wheelDelta
< 0 && es
->wheelDeltaRemainder
< 0) ||
5122 (wheelDelta
> 0 && es
->wheelDeltaRemainder
> 0))
5123 es
->wheelDeltaRemainder
+= wheelDelta
;
5125 es
->wheelDeltaRemainder
= wheelDelta
;
5126 if (es
->wheelDeltaRemainder
&& pulScrollLines
)
5129 pulScrollLines
= (int) min((UINT
) es
->line_count
, pulScrollLines
);
5130 cLineScroll
= pulScrollLines
* (float)es
->wheelDeltaRemainder
/ WHEEL_DELTA
;
5131 es
->wheelDeltaRemainder
-= WHEEL_DELTA
* cLineScroll
/ (int)pulScrollLines
;
5132 result
= EDIT_EM_LineScroll(es
, 0, -cLineScroll
);
5138 /* IME messages to make the edit control IME aware */
5139 case WM_IME_SETCONTEXT
:
5142 case WM_IME_STARTCOMPOSITION
:
5143 es
->composition_start
= es
->selection_end
;
5144 es
->composition_len
= 0;
5147 case WM_IME_COMPOSITION
:
5148 EDIT_ImeComposition(hwnd
, lParam
, es
);
5151 case WM_IME_ENDCOMPOSITION
:
5152 if (es
->composition_len
> 0)
5154 EDIT_EM_ReplaceSel(es
, TRUE
, NULL
, 0, TRUE
, TRUE
);
5155 es
->selection_end
= es
->selection_start
;
5156 es
->composition_len
= 0;
5160 case WM_IME_COMPOSITIONFULL
:
5166 case WM_IME_CONTROL
:
5169 case WM_IME_REQUEST
:
5172 case IMR_QUERYCHARPOSITION
:
5175 IMECHARPOSITION
*chpos
= (IMECHARPOSITION
*)lParam
;
5177 pos
= EDIT_EM_PosFromChar(es
, es
->selection_start
+ chpos
->dwCharPos
, FALSE
);
5178 chpos
->pt
.x
= LOWORD(pos
);
5179 chpos
->pt
.y
= HIWORD(pos
);
5180 chpos
->cLineHeight
= es
->line_height
;
5181 chpos
->rcDocument
= es
->format_rect
;
5182 MapWindowPoints(hwnd
, 0, &chpos
->pt
, 1);
5183 MapWindowPoints(hwnd
, 0, (POINT
*)&chpos
->rcDocument
, 2);
5191 result
= DefWindowProcT(hwnd
, msg
, wParam
, lParam
, unicode
);
5195 if (IsWindow(hwnd
) && es
&& msg
!= EM_GETHANDLE
)
5196 EDIT_UnlockBuffer(es
, FALSE
);
5198 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd
, msg
, SPY_GetMsgName(msg
, hwnd
), result
);
5204 /*********************************************************************
5205 * edit class descriptor
5207 static const WCHAR editW
[] = {'E','d','i','t',0};
5208 const struct builtin_class_descr EDIT_builtin_class
=
5211 CS_DBLCLKS
| CS_PARENTDC
, /* style */
5212 WINPROC_EDIT
, /* proc */
5214 sizeof(EDITSTATE
*) + sizeof(WORD
), /* extra */
5216 sizeof(EDITSTATE
*), /* extra */
5218 IDC_IBEAM
, /* cursor */