4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
57 #include "wine/winbase16.h"
58 #include "wine/winuser16.h"
59 #include "wine/unicode.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
69 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
70 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
71 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
74 * extra flags for EDITSTATE.flags field
76 #define EF_MODIFIED 0x0001 /* text has been modified */
77 #define EF_FOCUSED 0x0002 /* we have input focus */
78 #define EF_UPDATE 0x0004 /* notify parent of changed state */
79 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
80 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
82 wrapped line, instead of in front of the next character */
83 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
84 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
85 the text buffer if this is clear. */
86 #define EF_DIALOGMODE 0x0400 /* Indicates that we are inside a dialog window */
90 END_0 = 0, /* line ends with terminating '\0' character */
91 END_WRAP, /* line is wrapped */
92 END_HARD, /* line ends with a hard return '\r\n' */
93 END_SOFT, /* line ends with a soft return '\r\r\n' */
94 END_RICH /* line ends with a single '\n' */
97 typedef struct tagLINEDEF {
98 INT length; /* bruto length of a line in bytes */
99 INT net_length; /* netto length of a line in visible characters */
101 INT width; /* width of the line in pixels */
102 INT index; /* line index into the buffer */
103 struct tagLINEDEF *next;
108 BOOL is_unicode; /* how the control was created */
109 LPWSTR text; /* the actual contents of the control */
110 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
111 UINT buffer_size; /* the size of the buffer in characters */
112 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
113 HFONT font; /* NULL means standard system font */
114 INT x_offset; /* scroll offset for multi lines this is in pixels
115 for single lines it's in characters */
116 INT line_height; /* height of a screen line in pixels */
117 INT char_width; /* average character width in pixels */
118 DWORD style; /* sane version of wnd->dwStyle */
119 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
120 INT undo_insert_count; /* number of characters inserted in sequence */
121 UINT undo_position; /* character index of the insertion and deletion */
122 LPWSTR undo_text; /* deleted text */
123 UINT undo_buffer_size; /* size of the deleted text buffer */
124 INT selection_start; /* == selection_end if no selection */
125 INT selection_end; /* == current caret position */
126 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
127 INT left_margin; /* in pixels */
128 INT right_margin; /* in pixels */
130 INT text_width; /* width of the widest line in pixels for multi line controls
131 and just line width for single line controls */
132 INT region_posx; /* Position of cursor relative to region: */
133 INT region_posy; /* -1: to left, 0: within, 1: to right */
134 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
135 INT line_count; /* number of lines */
136 INT y_offset; /* scroll offset in number of lines */
137 BOOL bCaptureState; /* flag indicating whether mouse was captured */
138 BOOL bEnableState; /* flag keeping the enable state */
139 HWND hwndSelf; /* the our window handle */
140 HWND hwndParent; /* Handle of parent for sending EN_* messages.
141 Even if parent will change, EN_* messages
142 should be sent to the first parent. */
143 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
145 * only for multi line controls
147 INT lock_count; /* amount of re-entries in the EditWndProc */
150 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
151 HLOCAL hloc32W; /* our unicode local memory block */
152 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
157 UINT composition_len; /* length of composition, 0 == no composition */
158 int composition_start; /* the character position for the composition */
162 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
163 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
165 /* used for disabled or read-only edit control */
166 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
168 { /* Notify parent which has created this edit control */ \
169 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
170 SendMessageW(es->hwndParent, WM_COMMAND, \
171 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
172 (LPARAM)(es->hwndSelf)); \
176 /*********************************************************************
181 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
183 return (es->undo_insert_count || strlenW(es->undo_text));
187 /*********************************************************************
192 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
194 es->undo_insert_count = 0;
195 *es->undo_text = '\0';
199 /**********************************************************************
202 * Returns the window version in case Wine emulates a later version
203 * of windows than the application expects.
205 * In a number of cases when windows runs an application that was
206 * designed for an earlier windows version, windows reverts
207 * to "old" behaviour of that earlier version.
209 * An example is a disabled edit control that needs to be painted.
210 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
211 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
212 * applications with an expected version 0f 4.0 or higher.
215 static DWORD get_app_version(void)
217 static DWORD version;
220 DWORD dwEmulatedVersion;
222 DWORD dwProcVersion = GetProcessVersion(0);
224 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
225 GetVersionExW( &info );
226 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
227 /* FIXME: this may not be 100% correct; see discussion on the
228 * wine developer list in Nov 1999 */
229 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
234 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
239 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
240 msg = WM_CTLCOLORSTATIC;
242 msg = WM_CTLCOLOREDIT;
244 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
245 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
247 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
252 /**********************************************************************
253 * Support for word break proc thunks
256 #define MAX_THUNKS 32
258 #include <pshpack1.h>
259 static struct word_break_thunk
261 BYTE popl_eax; /* popl %eax (return address) */
262 BYTE pushl_proc16; /* pushl proc16 */
263 EDITWORDBREAKPROC16 proc16;
264 BYTE pushl_eax; /* pushl %eax */
265 BYTE jmp; /* ljmp call_word_break_proc16 */
267 } *word_break_thunks;
270 /**********************************************************************
271 * call_word_break_proc16
273 static INT16 CALLBACK call_word_break_proc16( SEGPTR proc16, LPSTR text, INT index, INT count, INT action )
279 segptr = MapLS( text );
280 args[4] = SELECTOROF(segptr);
281 args[3] = OFFSETOF(segptr);
285 WOWCallback16Ex( proc16, WCB16_PASCAL, sizeof(args), args, &result );
287 return LOWORD(result);
290 /******************************************************************
291 * add_word_break_thunk
293 static struct word_break_thunk *add_word_break_thunk( EDITWORDBREAKPROC16 proc16 )
295 struct word_break_thunk *thunk;
297 if (!word_break_thunks)
299 word_break_thunks = VirtualAlloc( NULL, MAX_THUNKS * sizeof(*thunk),
300 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
301 if (!word_break_thunks) return NULL;
303 for (thunk = word_break_thunks; thunk < &word_break_thunks[MAX_THUNKS]; thunk++)
305 thunk->popl_eax = 0x58; /* popl %eax */
306 thunk->pushl_proc16 = 0x68; /* pushl proc16 */
307 thunk->pushl_eax = 0x50; /* pushl %eax */
308 thunk->jmp = 0xe9; /* jmp call_word_break_proc16 */
309 thunk->callback = (char *)call_word_break_proc16 - (char *)(&thunk->callback + 1);
312 for (thunk = word_break_thunks; thunk < &word_break_thunks[MAX_THUNKS]; thunk++)
313 if (thunk->proc16 == proc16) return thunk;
315 for (thunk = word_break_thunks; thunk < &word_break_thunks[MAX_THUNKS]; thunk++)
317 if (thunk->proc16) continue;
318 thunk->proc16 = proc16;
321 FIXME("Out of word break thunks\n");
325 /******************************************************************
326 * get_word_break_thunk
328 static EDITWORDBREAKPROC16 get_word_break_thunk( EDITWORDBREAKPROCA proc )
330 struct word_break_thunk *thunk = (struct word_break_thunk *)proc;
331 if (word_break_thunks && thunk >= word_break_thunks && thunk < &word_break_thunks[MAX_THUNKS])
332 return thunk->proc16;
336 /*********************************************************************
340 * Find the beginning of words.
341 * Note: unlike the specs for a WordBreakProc, this function only
342 * allows to be called without linebreaks between s[0] up to
343 * s[count - 1]. Remember it is only called
344 * internally, so we can decide this for ourselves.
347 static INT EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
351 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
361 if (s[index] == ' ') {
362 while (index && (s[index] == ' '))
365 while (index && (s[index] != ' '))
371 while (index && (s[index] != ' '))
384 while ((index < count) && (s[index] == ' ')) index++;
386 while (s[index] && (s[index] != ' ') && (index < count))
388 while ((s[index] == ' ') && (index < count)) index++;
393 ret = (s[index] == ' ');
396 ERR("unknown action code, please report !\n");
403 /*********************************************************************
405 * EDIT_CallWordBreakProc
407 * Call appropriate WordBreakProc (internal or external).
409 * Note: The "start" argument should always be an index referring
410 * to es->text. The actual wordbreak proc might be
411 * 16 bit, so we can't always pass any 32 bit LPSTR.
412 * Hence we assume that es->text is the buffer that holds
413 * the string under examination (we can decide this for ourselves).
416 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
420 if (es->word_break_proc)
424 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
426 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
427 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
428 ret = wbpW(es->text + start, index, count, action);
432 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
436 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
437 textA = HeapAlloc(GetProcessHeap(), 0, countA);
438 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
439 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
440 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
441 ret = wbpA(textA, index, countA, action);
442 HeapFree(GetProcessHeap(), 0, textA);
446 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
451 /*********************************************************************
453 * EDIT_BuildLineDefs_ML
455 * Build linked list of text lines.
456 * Lines can end with '\0' (last line), a character (if it is wrapped),
457 * a soft return '\r\r\n' or a hard return '\r\n'
460 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
464 LPWSTR current_position, cp;
466 LINEDEF *current_line;
467 LINEDEF *previous_line;
469 INT line_index = 0, nstart_line = 0, nstart_index = 0;
470 INT line_count = es->line_count;
474 if (istart == iend && delta == 0)
477 dc = GetDC(es->hwndSelf);
479 old_font = SelectObject(dc, es->font);
481 previous_line = NULL;
482 current_line = es->first_line_def;
484 /* Find starting line. istart must lie inside an existing line or
485 * at the end of buffer */
487 if (istart < current_line->index + current_line->length ||
488 current_line->ending == END_0)
491 previous_line = current_line;
492 current_line = current_line->next;
494 } while (current_line);
496 if (!current_line) /* Error occurred start is not inside previous buffer */
498 FIXME(" modification occurred outside buffer\n");
499 ReleaseDC(es->hwndSelf, dc);
503 /* Remember start of modifications in order to calculate update region */
504 nstart_line = line_index;
505 nstart_index = current_line->index;
507 /* We must start to reformat from the previous line since the modifications
508 * may have caused the line to wrap upwards. */
509 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
512 current_line = previous_line;
514 start_line = current_line;
516 fw = es->format_rect.right - es->format_rect.left;
517 current_position = es->text + current_line->index;
519 if (current_line != start_line)
521 if (!current_line || current_line->index + delta > current_position - es->text)
523 /* The buffer has been expanded, create a new line and
524 insert it into the link list */
525 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
526 new_line->next = previous_line->next;
527 previous_line->next = new_line;
528 current_line = new_line;
531 else if (current_line->index + delta < current_position - es->text)
533 /* The previous line merged with this line so we delete this extra entry */
534 previous_line->next = current_line->next;
535 HeapFree(GetProcessHeap(), 0, current_line);
536 current_line = previous_line->next;
540 else /* current_line->index + delta == current_position */
542 if (current_position - es->text > iend)
543 break; /* We reached end of line modifications */
544 /* else recalculate this line */
548 current_line->index = current_position - es->text;
549 orig_net_length = current_line->net_length;
551 /* Find end of line */
552 cp = current_position;
554 if (*cp == '\n') break;
555 if ((*cp == '\r') && (*(cp + 1) == '\n'))
560 /* Mark type of line termination */
562 current_line->ending = END_0;
563 current_line->net_length = strlenW(current_position);
564 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
565 current_line->ending = END_SOFT;
566 current_line->net_length = cp - current_position - 1;
567 } else if (*cp == '\n') {
568 current_line->ending = END_RICH;
569 current_line->net_length = cp - current_position;
571 current_line->ending = END_HARD;
572 current_line->net_length = cp - current_position;
575 /* Calculate line width */
576 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
577 current_position, current_line->net_length,
578 es->tabs_count, es->tabs));
580 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
581 if (!(es->style & ES_AUTOHSCROLL)) {
582 if (current_line->width > fw) {
587 next = EDIT_CallWordBreakProc(es, current_position - es->text,
588 prev + 1, current_line->net_length, WB_RIGHT);
589 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
590 current_position, next, es->tabs_count, es->tabs));
591 } while (current_line->width <= fw);
592 if (!prev) { /* Didn't find a line break so force a break */
597 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
598 current_position, next, es->tabs_count, es->tabs));
599 } while (current_line->width <= fw);
604 /* If the first line we are calculating, wrapped before istart, we must
605 * adjust istart in order for this to be reflected in the update region. */
606 if (current_line->index == nstart_index && istart > current_line->index + prev)
607 istart = current_line->index + prev;
608 /* else if we are updating the previous line before the first line we
609 * are re-calculating and it expanded */
610 else if (current_line == start_line &&
611 current_line->index != nstart_index && orig_net_length < prev)
613 /* Line expanded due to an upwards line wrap so we must partially include
614 * previous line in update region */
615 nstart_line = line_index;
616 nstart_index = current_line->index;
617 istart = current_line->index + orig_net_length;
620 current_line->net_length = prev;
621 current_line->ending = END_WRAP;
622 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
623 current_line->net_length, es->tabs_count, es->tabs));
625 else if (current_line == start_line &&
626 current_line->index != nstart_index &&
627 orig_net_length < current_line->net_length) {
628 /* The previous line expanded but it's still not as wide as the client rect */
629 /* The expansion is due to an upwards line wrap so we must partially include
630 it in the update region */
631 nstart_line = line_index;
632 nstart_index = current_line->index;
633 istart = current_line->index + orig_net_length;
638 /* Adjust length to include line termination */
639 switch (current_line->ending) {
641 current_line->length = current_line->net_length + 3;
644 current_line->length = current_line->net_length + 1;
647 current_line->length = current_line->net_length + 2;
651 current_line->length = current_line->net_length;
654 es->text_width = max(es->text_width, current_line->width);
655 current_position += current_line->length;
656 previous_line = current_line;
657 current_line = current_line->next;
659 } while (previous_line->ending != END_0);
661 /* Finish adjusting line indexes by delta or remove hanging lines */
662 if (previous_line->ending == END_0)
664 LINEDEF *pnext = NULL;
666 previous_line->next = NULL;
669 pnext = current_line->next;
670 HeapFree(GetProcessHeap(), 0, current_line);
671 current_line = pnext;
679 current_line->index += delta;
680 current_line = current_line->next;
684 /* Calculate rest of modification rectangle */
689 * We calculate two rectangles. One for the first line which may have
690 * an indent with respect to the format rect. The other is a format-width
691 * rectangle that spans the rest of the lines that changed or moved.
693 rc.top = es->format_rect.top + nstart_line * es->line_height -
694 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
695 rc.bottom = rc.top + es->line_height;
696 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
697 rc.left = es->format_rect.left;
699 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
700 es->text + nstart_index, istart - nstart_index,
701 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
702 rc.right = es->format_rect.right;
703 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
706 rc.left = es->format_rect.left;
707 rc.right = es->format_rect.right;
709 * If lines were added or removed we must re-paint the remainder of the
710 * lines since the remaining lines were either shifted up or down.
712 if (line_count < es->line_count) /* We added lines */
713 rc.bottom = es->line_count * es->line_height;
714 else if (line_count > es->line_count) /* We removed lines */
715 rc.bottom = line_count * es->line_height;
717 rc.bottom = line_index * es->line_height;
718 rc.bottom += es->format_rect.top;
719 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
720 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
721 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
722 DeleteObject(tmphrgn);
726 SelectObject(dc, old_font);
728 ReleaseDC(es->hwndSelf, dc);
732 static inline UINT get_text_length(EDITSTATE *es)
734 if(es->text_length == (UINT)-1)
735 es->text_length = strlenW(es->text);
736 return es->text_length;
739 /*********************************************************************
741 * EDIT_GetPasswordPointer_SL
743 * note: caller should free the (optionally) allocated buffer
746 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
748 if (es->style & ES_PASSWORD) {
749 INT len = get_text_length(es);
750 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
752 while(len) text[--len] = es->password_char;
759 /*********************************************************************
761 * EDIT_CalcLineWidth_SL
764 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
771 text = EDIT_GetPasswordPointer_SL(es);
773 dc = GetDC(es->hwndSelf);
775 old_font = SelectObject(dc, es->font);
777 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
780 SelectObject(dc, old_font);
781 ReleaseDC(es->hwndSelf, dc);
783 if (es->style & ES_PASSWORD)
784 HeapFree(GetProcessHeap(), 0, text);
786 es->text_width = size.cx;
789 /*********************************************************************
793 * Beware: This is not the function called on EM_CHARFROMPOS
794 * The position _can_ be outside the formatting / client
796 * The return value is only the character index
799 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
804 INT x_high = 0, x_low = 0;
806 if (es->style & ES_MULTILINE) {
807 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
809 LINEDEF *line_def = es->first_line_def;
811 while ((line > 0) && line_def->next) {
812 line_index += line_def->length;
813 line_def = line_def->next;
816 x += es->x_offset - es->format_rect.left;
817 if (es->style & ES_RIGHT)
818 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
819 else if (es->style & ES_CENTER)
820 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
821 if (x >= line_def->width) {
823 *after_wrap = (line_def->ending == END_WRAP);
824 return line_index + line_def->net_length;
831 dc = GetDC(es->hwndSelf);
833 old_font = SelectObject(dc, es->font);
835 high = line_index + line_def->net_length + 1;
836 while (low < high - 1)
838 INT mid = (low + high) / 2;
839 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
848 if (abs(x_high - x) + 1 <= abs(x_low - x))
854 *after_wrap = ((index == line_index + line_def->net_length) &&
855 (line_def->ending == END_WRAP));
861 x -= es->format_rect.left;
867 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
868 if (es->style & ES_RIGHT)
870 else if (es->style & ES_CENTER)
874 text = EDIT_GetPasswordPointer_SL(es);
875 dc = GetDC(es->hwndSelf);
877 old_font = SelectObject(dc, es->font);
881 INT high = es->x_offset;
882 while (low < high - 1)
884 INT mid = (low + high) / 2;
885 GetTextExtentPoint32W( dc, text + mid,
886 es->x_offset - mid, &size );
895 if (abs(x_high + x) <= abs(x_low + x) + 1)
902 INT low = es->x_offset;
903 INT high = get_text_length(es) + 1;
904 while (low < high - 1)
906 INT mid = (low + high) / 2;
907 GetTextExtentPoint32W( dc, text + es->x_offset,
908 mid - es->x_offset, &size );
917 if (abs(x_high - x) <= abs(x_low - x) + 1)
922 if (es->style & ES_PASSWORD)
923 HeapFree(GetProcessHeap(), 0, text);
926 SelectObject(dc, old_font);
927 ReleaseDC(es->hwndSelf, dc);
932 /*********************************************************************
936 * adjusts the point to be within the formatting rectangle
937 * (so CharFromPos returns the nearest _visible_ character)
940 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
942 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
943 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
947 /*********************************************************************
952 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
957 if (!(es->style & ES_MULTILINE))
959 if (index > (INT)get_text_length(es))
960 return es->line_count - 1;
962 index = min(es->selection_start, es->selection_end);
965 line_def = es->first_line_def;
966 index -= line_def->length;
967 while ((index >= 0) && line_def->next) {
969 line_def = line_def->next;
970 index -= line_def->length;
976 /*********************************************************************
981 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
984 const LINEDEF *line_def;
986 if (!(es->style & ES_MULTILINE))
988 if (line >= es->line_count)
992 line_def = es->first_line_def;
994 INT index = es->selection_end - line_def->length;
995 while ((index >= 0) && line_def->next) {
996 line_index += line_def->length;
997 line_def = line_def->next;
998 index -= line_def->length;
1002 line_index += line_def->length;
1003 line_def = line_def->next;
1011 /*********************************************************************
1016 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1020 if (!(es->style & ES_MULTILINE))
1021 return get_text_length(es);
1024 /* get the number of remaining non-selected chars of selected lines */
1025 INT32 l; /* line number */
1026 INT32 li; /* index of first char in line */
1028 l = EDIT_EM_LineFromChar(es, es->selection_start);
1029 /* # chars before start of selection area */
1030 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1031 l = EDIT_EM_LineFromChar(es, es->selection_end);
1032 /* # chars after end of selection */
1033 li = EDIT_EM_LineIndex(es, l);
1034 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1037 line_def = es->first_line_def;
1038 index -= line_def->length;
1039 while ((index >= 0) && line_def->next) {
1040 line_def = line_def->next;
1041 index -= line_def->length;
1043 return line_def->net_length;
1047 /*********************************************************************
1052 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1054 INT len = get_text_length(es);
1067 index = min(index, len);
1068 dc = GetDC(es->hwndSelf);
1070 old_font = SelectObject(dc, es->font);
1071 if (es->style & ES_MULTILINE) {
1072 l = EDIT_EM_LineFromChar(es, index);
1073 y = (l - es->y_offset) * es->line_height;
1074 li = EDIT_EM_LineIndex(es, l);
1075 if (after_wrap && (li == index) && l) {
1077 line_def = es->first_line_def;
1079 line_def = line_def->next;
1082 if (line_def->ending == END_WRAP) {
1084 y -= es->line_height;
1085 li = EDIT_EM_LineIndex(es, l);
1089 line_def = es->first_line_def;
1090 while (line_def->index != li)
1091 line_def = line_def->next;
1093 ll = line_def->net_length;
1094 lw = line_def->width;
1096 w = es->format_rect.right - es->format_rect.left;
1097 if (es->style & ES_RIGHT)
1099 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
1100 es->tabs_count, es->tabs)) - es->x_offset;
1103 else if (es->style & ES_CENTER)
1105 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1106 es->tabs_count, es->tabs)) - es->x_offset;
1111 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1112 es->tabs_count, es->tabs)) - es->x_offset;
1115 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
1116 if (index < es->x_offset) {
1117 GetTextExtentPoint32W(dc, text + index,
1118 es->x_offset - index, &size);
1121 GetTextExtentPoint32W(dc, text + es->x_offset,
1122 index - es->x_offset, &size);
1125 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1127 w = es->format_rect.right - es->format_rect.left;
1128 if (w > es->text_width)
1130 if (es->style & ES_RIGHT)
1131 x += w - es->text_width;
1132 else if (es->style & ES_CENTER)
1133 x += (w - es->text_width) / 2;
1138 if (es->style & ES_PASSWORD)
1139 HeapFree(GetProcessHeap(), 0, text);
1141 x += es->format_rect.left;
1142 y += es->format_rect.top;
1144 SelectObject(dc, old_font);
1145 ReleaseDC(es->hwndSelf, dc);
1146 return MAKELONG((INT16)x, (INT16)y);
1150 /*********************************************************************
1154 * Calculates the bounding rectangle for a line from a starting
1155 * column to an ending column.
1158 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1160 INT line_index = EDIT_EM_LineIndex(es, line);
1162 if (es->style & ES_MULTILINE)
1163 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1165 rc->top = es->format_rect.top;
1166 rc->bottom = rc->top + es->line_height;
1167 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1168 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1172 static inline void text_buffer_changed(EDITSTATE *es)
1174 es->text_length = (UINT)-1;
1177 #define GWW_HANDLE16 sizeof(EDITSTATE*)
1179 /*********************************************************************
1182 static void EDIT_LockBuffer16(EDITSTATE *es)
1184 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1185 HLOCAL16 hloc16 = GetWindowWord( es->hwndSelf, GWW_HANDLE16 );
1190 if (!hloc16) return;
1191 if (!(hloc32 = es->hloc32A)) return;
1193 oldDS = stack16->ds;
1194 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1195 size = LocalSize16(hloc16);
1196 if (LocalReAlloc( hloc32, size, LMEM_MOVEABLE ))
1198 char *text = MapSL( LocalLock16( hloc16 ));
1199 char *dest = LocalLock( hloc32 );
1200 memcpy( dest, text, size );
1201 LocalUnlock( hloc32 );
1202 LocalUnlock16( hloc16 );
1204 stack16->ds = oldDS;
1208 /*********************************************************************
1209 * EDIT_UnlockBuffer16
1212 static void EDIT_UnlockBuffer16(EDITSTATE *es)
1214 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1215 HLOCAL16 hloc16 = GetWindowWord( es->hwndSelf, GWW_HANDLE16 );
1220 if (!hloc16) return;
1221 if (!(hloc32 = es->hloc32A)) return;
1222 size = LocalSize( hloc32 );
1224 oldDS = stack16->ds;
1225 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1226 if (LocalReAlloc16( hloc16, size, LMEM_MOVEABLE ))
1228 char *text = LocalLock( hloc32 );
1229 char *dest = MapSL( LocalLock16( hloc16 ));
1230 memcpy( dest, text, size );
1231 LocalUnlock( hloc32 );
1232 LocalUnlock16( hloc16 );
1234 stack16->ds = oldDS;
1237 /*********************************************************************
1241 * This acts as a LocalLock16(), but it locks only once. This way
1242 * you can call it whenever you like, without unlocking.
1244 * Initially the edit control allocates a HLOCAL32 buffer
1245 * (32 bit linear memory handler). However, 16 bit application
1246 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1247 * handler). From that moment on we have to keep using this 16 bit memory
1248 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1249 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1253 static void EDIT_LockBuffer(EDITSTATE *es)
1257 if(!es->hloc32W) return;
1259 EDIT_LockBuffer16(es);
1263 CHAR *textA = LocalLock(es->hloc32A);
1265 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1266 if(countW_new > es->buffer_size + 1)
1268 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1269 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1270 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1273 es->hloc32W = hloc32W_new;
1274 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1275 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1278 WARN("FAILED! Will synchronize partially\n");
1280 es->text = LocalLock(es->hloc32W);
1281 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1282 LocalUnlock(es->hloc32A);
1284 else es->text = LocalLock(es->hloc32W);
1286 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1291 /*********************************************************************
1296 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1299 /* Edit window might be already destroyed */
1300 if(!IsWindow(es->hwndSelf))
1302 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1306 if (!es->lock_count) {
1307 ERR("lock_count == 0 ... please report\n");
1311 ERR("es->text == 0 ... please report\n");
1315 if (force || (es->lock_count == 1)) {
1318 UINT countW = get_text_length(es) + 1;
1322 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1323 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1324 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1325 countA = LocalSize(es->hloc32A);
1326 if(countA_new > countA)
1329 UINT alloc_size = ROUND_TO_GROW(countA_new);
1330 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1331 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1334 es->hloc32A = hloc32A_new;
1335 countA = LocalSize(hloc32A_new);
1336 TRACE("Real new size %d bytes\n", countA);
1339 WARN("FAILED! Will synchronize partially\n");
1341 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1342 LocalLock(es->hloc32A), countA, NULL, NULL);
1343 LocalUnlock(es->hloc32A);
1346 LocalUnlock(es->hloc32W);
1348 EDIT_UnlockBuffer16(es);
1351 ERR("no buffer ... please report\n");
1359 /*********************************************************************
1363 * Try to fit size + 1 characters in the buffer.
1365 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1369 if (size <= es->buffer_size)
1372 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1374 /* Force edit to unlock it's buffer. es->text now NULL */
1375 EDIT_UnlockBuffer(es, TRUE);
1378 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1379 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1380 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1381 es->hloc32W = hNew32W;
1382 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1386 EDIT_LockBuffer(es);
1388 if (es->buffer_size < size) {
1389 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1390 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1393 TRACE("We now have %d+1\n", es->buffer_size);
1399 /*********************************************************************
1403 * Try to fit size + 1 bytes in the undo buffer.
1406 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1410 if (size <= es->undo_buffer_size)
1413 TRACE("trying to ReAlloc to %d+1\n", size);
1415 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1416 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1417 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1422 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1428 /*********************************************************************
1430 * EDIT_UpdateTextRegion
1433 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1435 if (es->flags & EF_UPDATE) {
1436 es->flags &= ~EF_UPDATE;
1437 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1439 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1443 /*********************************************************************
1448 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1450 if (es->flags & EF_UPDATE) {
1451 es->flags &= ~EF_UPDATE;
1452 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1454 InvalidateRect(es->hwndSelf, rc, bErase);
1457 /*********************************************************************
1459 * EDIT_SL_InvalidateText
1461 * Called from EDIT_InvalidateText().
1462 * Does the job for single-line controls only.
1465 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1470 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1471 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1472 EDIT_UpdateText(es, &rc, TRUE);
1476 static inline INT get_vertical_line_count(EDITSTATE *es)
1478 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1482 /*********************************************************************
1484 * EDIT_ML_InvalidateText
1486 * Called from EDIT_InvalidateText().
1487 * Does the job for multi-line controls only.
1490 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1492 INT vlc = get_vertical_line_count(es);
1493 INT sl = EDIT_EM_LineFromChar(es, start);
1494 INT el = EDIT_EM_LineFromChar(es, end);
1503 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1506 sc = start - EDIT_EM_LineIndex(es, sl);
1507 ec = end - EDIT_EM_LineIndex(es, el);
1508 if (sl < es->y_offset) {
1512 if (el > es->y_offset + vlc) {
1513 el = es->y_offset + vlc;
1514 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1516 GetClientRect(es->hwndSelf, &rc1);
1517 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1519 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1520 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1521 EDIT_UpdateText(es, &rcUpdate, TRUE);
1523 EDIT_GetLineRect(es, sl, sc,
1524 EDIT_EM_LineLength(es,
1525 EDIT_EM_LineIndex(es, sl)),
1527 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1528 EDIT_UpdateText(es, &rcUpdate, TRUE);
1529 for (l = sl + 1 ; l < el ; l++) {
1530 EDIT_GetLineRect(es, l, 0,
1531 EDIT_EM_LineLength(es,
1532 EDIT_EM_LineIndex(es, l)),
1534 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1535 EDIT_UpdateText(es, &rcUpdate, TRUE);
1537 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1538 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1539 EDIT_UpdateText(es, &rcUpdate, TRUE);
1544 /*********************************************************************
1546 * EDIT_InvalidateText
1548 * Invalidate the text from offset start up to, but not including,
1549 * offset end. Useful for (re)painting the selection.
1550 * Regions outside the linewidth are not invalidated.
1551 * end == -1 means end == TextLength.
1552 * start and end need not be ordered.
1555 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1561 end = get_text_length(es);
1569 if (es->style & ES_MULTILINE)
1570 EDIT_ML_InvalidateText(es, start, end);
1572 EDIT_SL_InvalidateText(es, start, end);
1576 /*********************************************************************
1580 * note: unlike the specs say: the order of start and end
1581 * _is_ preserved in Windows. (i.e. start can be > end)
1582 * In other words: this handler is OK
1585 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1587 UINT old_start = es->selection_start;
1588 UINT old_end = es->selection_end;
1589 UINT len = get_text_length(es);
1591 if (start == (UINT)-1) {
1592 start = es->selection_end;
1593 end = es->selection_end;
1595 start = min(start, len);
1596 end = min(end, len);
1598 es->selection_start = start;
1599 es->selection_end = end;
1601 es->flags |= EF_AFTER_WRAP;
1603 es->flags &= ~EF_AFTER_WRAP;
1604 /* Compute the necessary invalidation region. */
1605 /* Note that we don't need to invalidate regions which have
1606 * "never" been selected, or those which are "still" selected.
1607 * In fact, every time we hit a selection boundary, we can
1608 * *toggle* whether we need to invalidate. Thus we can optimize by
1609 * *sorting* the interval endpoints. Let's assume that we sort them
1611 * start <= end <= old_start <= old_end
1612 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1613 * in 5 comparisons; i.e. it is impossible to do better than the
1615 ORDER_UINT(end, old_end);
1616 ORDER_UINT(start, old_start);
1617 ORDER_UINT(old_start, old_end);
1618 ORDER_UINT(start, end);
1619 /* Note that at this point 'end' and 'old_start' are not in order, but
1620 * start is definitely the min. and old_end is definitely the max. */
1621 if (end != old_start)
1625 * ORDER_UINT32(end, old_start);
1626 * EDIT_InvalidateText(es, start, end);
1627 * EDIT_InvalidateText(es, old_start, old_end);
1628 * in place of the following if statement.
1629 * (That would complete the optimal five-comparison four-element sort.)
1631 if (old_start > end )
1633 EDIT_InvalidateText(es, start, end);
1634 EDIT_InvalidateText(es, old_start, old_end);
1638 EDIT_InvalidateText(es, start, old_start);
1639 EDIT_InvalidateText(es, end, old_end);
1642 else EDIT_InvalidateText(es, start, old_end);
1646 /*********************************************************************
1648 * EDIT_UpdateScrollInfo
1651 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1653 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1656 si.cbSize = sizeof(SCROLLINFO);
1657 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1659 si.nMax = es->line_count - 1;
1660 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1661 si.nPos = es->y_offset;
1662 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1663 si.nMin, si.nMax, si.nPage, si.nPos);
1664 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1667 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1670 si.cbSize = sizeof(SCROLLINFO);
1671 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1673 si.nMax = es->text_width - 1;
1674 si.nPage = es->format_rect.right - es->format_rect.left;
1675 si.nPos = es->x_offset;
1676 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1677 si.nMin, si.nMax, si.nPage, si.nPos);
1678 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1683 /*********************************************************************
1685 * EDIT_EM_LineScroll_internal
1687 * Version of EDIT_EM_LineScroll for internal use.
1688 * It doesn't refuse if ES_MULTILINE is set and assumes that
1689 * dx is in pixels, dy - in lines.
1692 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1695 INT x_offset_in_pixels;
1696 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1699 if (es->style & ES_MULTILINE)
1701 x_offset_in_pixels = es->x_offset;
1706 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1709 if (-dx > x_offset_in_pixels)
1710 dx = -x_offset_in_pixels;
1711 if (dx > es->text_width - x_offset_in_pixels)
1712 dx = es->text_width - x_offset_in_pixels;
1713 nyoff = max(0, es->y_offset + dy);
1714 if (nyoff >= es->line_count - lines_per_page)
1715 nyoff = max(0, es->line_count - lines_per_page);
1716 dy = (es->y_offset - nyoff) * es->line_height;
1721 es->y_offset = nyoff;
1722 if(es->style & ES_MULTILINE)
1725 es->x_offset += dx / es->char_width;
1727 GetClientRect(es->hwndSelf, &rc1);
1728 IntersectRect(&rc, &rc1, &es->format_rect);
1729 ScrollWindowEx(es->hwndSelf, -dx, dy,
1730 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1731 /* force scroll info update */
1732 EDIT_UpdateScrollInfo(es);
1734 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1735 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1736 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1737 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1741 /*********************************************************************
1745 * NOTE: dx is in average character widths, dy - in lines;
1748 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1750 if (!(es->style & ES_MULTILINE))
1753 dx *= es->char_width;
1754 return EDIT_EM_LineScroll_internal(es, dx, dy);
1758 /*********************************************************************
1763 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1767 if (!(es->style & ES_MULTILINE))
1768 return (LRESULT)FALSE;
1778 if (es->y_offset < es->line_count - 1)
1783 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1786 if (es->y_offset < es->line_count - 1)
1787 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1790 return (LRESULT)FALSE;
1793 INT vlc = get_vertical_line_count(es);
1794 /* check if we are going to move too far */
1795 if(es->y_offset + dy > es->line_count - vlc)
1796 dy = es->line_count - vlc - es->y_offset;
1798 /* Notification is done in EDIT_EM_LineScroll */
1800 EDIT_EM_LineScroll(es, 0, dy);
1802 return MAKELONG((INT16)dy, (BOOL16)TRUE);
1806 /*********************************************************************
1811 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1814 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1815 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1816 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1820 /*********************************************************************
1825 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1827 if (es->style & ES_MULTILINE) {
1831 INT cw = es->char_width;
1836 l = EDIT_EM_LineFromChar(es, es->selection_end);
1837 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1838 vlc = get_vertical_line_count(es);
1839 if (l >= es->y_offset + vlc)
1840 dy = l - vlc + 1 - es->y_offset;
1841 if (l < es->y_offset)
1842 dy = l - es->y_offset;
1843 ww = es->format_rect.right - es->format_rect.left;
1844 if (x < es->format_rect.left)
1845 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1846 if (x > es->format_rect.right)
1847 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1848 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1850 /* check if we are going to move too far */
1851 if(es->x_offset + dx + ww > es->text_width)
1852 dx = es->text_width - ww - es->x_offset;
1853 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1854 EDIT_EM_LineScroll_internal(es, dx, dy);
1861 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1862 format_width = es->format_rect.right - es->format_rect.left;
1863 if (x < es->format_rect.left) {
1864 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1867 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1868 } while ((x < goal) && es->x_offset);
1869 /* FIXME: use ScrollWindow() somehow to improve performance */
1870 EDIT_UpdateText(es, NULL, TRUE);
1871 } else if (x > es->format_rect.right) {
1873 INT len = get_text_length(es);
1874 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1877 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1878 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1879 } while ((x > goal) && (x_last > es->format_rect.right));
1880 /* FIXME: use ScrollWindow() somehow to improve performance */
1881 EDIT_UpdateText(es, NULL, TRUE);
1885 if(es->flags & EF_FOCUSED)
1886 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1890 /*********************************************************************
1895 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1897 INT e = es->selection_end;
1901 if ((es->style & ES_MULTILINE) && e &&
1902 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1904 if (e && (es->text[e - 1] == '\r'))
1908 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1909 EDIT_EM_ScrollCaret(es);
1913 /*********************************************************************
1917 * Only for multi line controls
1918 * Move the caret one line down, on a column with the nearest
1919 * x coordinate on the screen (might be a different column).
1922 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1924 INT s = es->selection_start;
1925 INT e = es->selection_end;
1926 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1927 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1928 INT x = (short)LOWORD(pos);
1929 INT y = (short)HIWORD(pos);
1931 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1934 EDIT_EM_SetSel(es, s, e, after_wrap);
1935 EDIT_EM_ScrollCaret(es);
1939 /*********************************************************************
1944 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1946 BOOL after_wrap = FALSE;
1949 /* Pass a high value in x to make sure of receiving the end of the line */
1950 if (!ctrl && (es->style & ES_MULTILINE))
1951 e = EDIT_CharFromPos(es, 0x3fffffff,
1952 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1954 e = get_text_length(es);
1955 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1956 EDIT_EM_ScrollCaret(es);
1960 /*********************************************************************
1965 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1967 INT e = es->selection_end;
1971 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1972 if (es->text[e] == '\n')
1974 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1978 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1979 EDIT_EM_ScrollCaret(es);
1983 /*********************************************************************
1987 * Home key: move to beginning of line.
1990 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1994 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1995 if (!ctrl && (es->style & ES_MULTILINE))
1996 e = EDIT_CharFromPos(es, -es->x_offset,
1997 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2000 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2001 EDIT_EM_ScrollCaret(es);
2005 /*********************************************************************
2007 * EDIT_MovePageDown_ML
2009 * Only for multi line controls
2010 * Move the caret one page down, on a column with the nearest
2011 * x coordinate on the screen (might be a different column).
2014 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2016 INT s = es->selection_start;
2017 INT e = es->selection_end;
2018 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2019 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2020 INT x = (short)LOWORD(pos);
2021 INT y = (short)HIWORD(pos);
2023 e = EDIT_CharFromPos(es, x,
2024 y + (es->format_rect.bottom - es->format_rect.top),
2028 EDIT_EM_SetSel(es, s, e, after_wrap);
2029 EDIT_EM_ScrollCaret(es);
2033 /*********************************************************************
2035 * EDIT_MovePageUp_ML
2037 * Only for multi line controls
2038 * Move the caret one page up, on a column with the nearest
2039 * x coordinate on the screen (might be a different column).
2042 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2044 INT s = es->selection_start;
2045 INT e = es->selection_end;
2046 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2047 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2048 INT x = (short)LOWORD(pos);
2049 INT y = (short)HIWORD(pos);
2051 e = EDIT_CharFromPos(es, x,
2052 y - (es->format_rect.bottom - es->format_rect.top),
2056 EDIT_EM_SetSel(es, s, e, after_wrap);
2057 EDIT_EM_ScrollCaret(es);
2061 /*********************************************************************
2065 * Only for multi line controls
2066 * Move the caret one line up, on a column with the nearest
2067 * x coordinate on the screen (might be a different column).
2070 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2072 INT s = es->selection_start;
2073 INT e = es->selection_end;
2074 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2075 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2076 INT x = (short)LOWORD(pos);
2077 INT y = (short)HIWORD(pos);
2079 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2082 EDIT_EM_SetSel(es, s, e, after_wrap);
2083 EDIT_EM_ScrollCaret(es);
2087 /*********************************************************************
2089 * EDIT_MoveWordBackward
2092 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2094 INT s = es->selection_start;
2095 INT e = es->selection_end;
2100 l = EDIT_EM_LineFromChar(es, e);
2101 ll = EDIT_EM_LineLength(es, e);
2102 li = EDIT_EM_LineIndex(es, l);
2105 li = EDIT_EM_LineIndex(es, l - 1);
2106 e = li + EDIT_EM_LineLength(es, li);
2109 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2113 EDIT_EM_SetSel(es, s, e, FALSE);
2114 EDIT_EM_ScrollCaret(es);
2118 /*********************************************************************
2120 * EDIT_MoveWordForward
2123 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2125 INT s = es->selection_start;
2126 INT e = es->selection_end;
2131 l = EDIT_EM_LineFromChar(es, e);
2132 ll = EDIT_EM_LineLength(es, e);
2133 li = EDIT_EM_LineIndex(es, l);
2135 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2136 e = EDIT_EM_LineIndex(es, l + 1);
2138 e = li + EDIT_CallWordBreakProc(es,
2139 li, e - li + 1, ll, WB_RIGHT);
2143 EDIT_EM_SetSel(es, s, e, FALSE);
2144 EDIT_EM_ScrollCaret(es);
2148 /*********************************************************************
2153 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2157 LOGFONTW underline_font;
2158 HFONT hUnderline = 0;
2167 BkMode = GetBkMode(dc);
2168 BkColor = GetBkColor(dc);
2169 TextColor = GetTextColor(dc);
2171 if (es->composition_len == 0)
2173 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2174 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2175 SetBkMode( dc, OPAQUE);
2179 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2180 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2181 underline_font.lfUnderline = TRUE;
2182 hUnderline = CreateFontIndirectW(&underline_font);
2183 old_font = SelectObject(dc,hUnderline);
2186 li = EDIT_EM_LineIndex(es, line);
2187 if (es->style & ES_MULTILINE) {
2188 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2189 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2191 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2192 TextOutW(dc, x, y, text + li + col, count);
2193 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2195 if (es->style & ES_PASSWORD)
2196 HeapFree(GetProcessHeap(), 0, text);
2199 if (es->composition_len == 0)
2201 SetBkColor(dc, BkColor);
2202 SetTextColor(dc, TextColor);
2203 SetBkMode( dc, BkMode);
2208 SelectObject(dc,old_font);
2210 DeleteObject(hUnderline);
2217 /*********************************************************************
2222 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2224 INT s = es->selection_start;
2225 INT e = es->selection_end;
2232 if (es->style & ES_MULTILINE) {
2233 INT vlc = get_vertical_line_count(es);
2235 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2240 TRACE("line=%d\n", line);
2242 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2243 x = (short)LOWORD(pos);
2244 y = (short)HIWORD(pos);
2245 li = EDIT_EM_LineIndex(es, line);
2246 ll = EDIT_EM_LineLength(es, li);
2247 s = min(es->selection_start, es->selection_end);
2248 e = max(es->selection_start, es->selection_end);
2249 s = min(li + ll, max(li, s));
2250 e = min(li + ll, max(li, e));
2251 if (rev && (s != e) &&
2252 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2253 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2254 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2255 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2257 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2261 /*********************************************************************
2263 * EDIT_AdjustFormatRect
2265 * Adjusts the format rectangle for the current font and the
2266 * current client rectangle.
2269 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2273 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2274 if (es->style & ES_MULTILINE)
2276 INT fw, vlc, max_x_offset, max_y_offset;
2278 vlc = get_vertical_line_count(es);
2279 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2281 /* correct es->x_offset */
2282 fw = es->format_rect.right - es->format_rect.left;
2283 max_x_offset = es->text_width - fw;
2284 if(max_x_offset < 0) max_x_offset = 0;
2285 if(es->x_offset > max_x_offset)
2286 es->x_offset = max_x_offset;
2288 /* correct es->y_offset */
2289 max_y_offset = es->line_count - vlc;
2290 if(max_y_offset < 0) max_y_offset = 0;
2291 if(es->y_offset > max_y_offset)
2292 es->y_offset = max_y_offset;
2294 /* force scroll info update */
2295 EDIT_UpdateScrollInfo(es);
2298 /* Windows doesn't care to fix text placement for SL controls */
2299 es->format_rect.bottom = es->format_rect.top + es->line_height;
2301 /* Always stay within the client area */
2302 GetClientRect(es->hwndSelf, &ClientRect);
2303 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2305 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2306 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2308 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2312 /*********************************************************************
2316 * note: this is not (exactly) the handler called on EM_SETRECTNP
2317 * it is also used to set the rect of a single line control
2320 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2324 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2326 CopyRect(&es->format_rect, rc);
2328 if (ExStyle & WS_EX_CLIENTEDGE) {
2329 es->format_rect.left++;
2330 es->format_rect.right--;
2332 if (es->format_rect.bottom - es->format_rect.top
2333 >= es->line_height + 2)
2335 es->format_rect.top++;
2336 es->format_rect.bottom--;
2339 else if (es->style & WS_BORDER) {
2340 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2341 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2342 es->format_rect.left += bw;
2343 es->format_rect.right -= bw;
2344 if (es->format_rect.bottom - es->format_rect.top
2345 >= es->line_height + 2 * bh)
2347 es->format_rect.top += bh;
2348 es->format_rect.bottom -= bh;
2352 es->format_rect.left += es->left_margin;
2353 es->format_rect.right -= es->right_margin;
2354 EDIT_AdjustFormatRect(es);
2358 /*********************************************************************
2362 * returns line number (not index) in high-order word of result.
2363 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2364 * to Richedit, not to the edit control. Original documentation is valid.
2365 * FIXME: do the specs mean to return -1 if outside client area or
2366 * if outside formatting rectangle ???
2369 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2377 GetClientRect(es->hwndSelf, &rc);
2378 if (!PtInRect(&rc, pt))
2381 index = EDIT_CharFromPos(es, x, y, NULL);
2382 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2386 /*********************************************************************
2390 * Enable or disable soft breaks.
2392 * This means: insert or remove the soft linebreak character (\r\r\n).
2393 * Take care to check if the text still fits the buffer after insertion.
2394 * If not, notify with EN_ERRSPACE.
2397 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2399 es->flags &= ~EF_USE_SOFTBRK;
2401 es->flags |= EF_USE_SOFTBRK;
2402 FIXME("soft break enabled, not implemented\n");
2408 /*********************************************************************
2412 * Hopefully this won't fire back at us.
2413 * We always start with a fixed buffer in the local heap.
2414 * Despite of the documentation says that the local heap is used
2415 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2416 * buffer on the local heap.
2419 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2423 if (!(es->style & ES_MULTILINE))
2427 hLocal = es->hloc32W;
2433 UINT countA, alloc_size;
2434 TRACE("Allocating 32-bit ANSI alias buffer\n");
2435 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2436 alloc_size = ROUND_TO_GROW(countA);
2437 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2439 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2442 textA = LocalLock(es->hloc32A);
2443 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2444 LocalUnlock(es->hloc32A);
2446 hLocal = es->hloc32A;
2449 es->flags |= EF_APP_HAS_HANDLE;
2450 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2455 /*********************************************************************
2459 * Hopefully this won't fire back at us.
2460 * We always start with a buffer in 32 bit linear memory.
2461 * However, with this message a 16 bit application requests
2462 * a handle of 16 bit local heap memory, where it expects to find
2464 * It's a pitty that from this moment on we have to use this
2465 * local heap, because applications may rely on the handle
2468 * In this function we'll try to switch to local heap.
2470 static HLOCAL16 EDIT_EM_GetHandle16( HWND hwnd )
2475 STACK16FRAME* stack16;
2477 HLOCAL16 hloc16 = GetWindowWord( hwnd, GWW_HANDLE16 );
2479 if (hloc16) return hloc16;
2481 if (!(hloc = (HLOCAL)SendMessageA( hwnd, EM_GETHANDLE, 0, 0 ))) return 0;
2482 alloc_size = LocalSize( hloc );
2484 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2485 oldDS = stack16->ds;
2486 stack16->ds = GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
2488 if (!LocalHeapSize16()) {
2490 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2491 ERR("could not initialize local heap\n");
2494 TRACE("local heap initialized\n");
2497 TRACE("Allocating 16-bit ANSI alias buffer\n");
2498 if (!(hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2499 ERR("could not allocate new 16 bit buffer\n");
2503 if (!(textA = MapSL(LocalLock16( hloc16)))) {
2504 ERR("could not lock new 16 bit buffer\n");
2505 LocalFree16(hloc16);
2509 memcpy( textA, LocalLock( hloc ), alloc_size );
2510 LocalUnlock( hloc );
2511 LocalUnlock16( hloc16 );
2512 SetWindowWord( hwnd, GWW_HANDLE16, hloc16 );
2514 TRACE("Returning %04X, LocalSize() = %d\n", hloc16, alloc_size);
2517 stack16->ds = oldDS;
2522 /*********************************************************************
2527 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2530 INT line_len, dst_len;
2533 if (es->style & ES_MULTILINE) {
2534 if (line >= es->line_count)
2538 i = EDIT_EM_LineIndex(es, line);
2540 line_len = EDIT_EM_LineLength(es, i);
2541 dst_len = *(WORD *)dst;
2544 if(dst_len <= line_len)
2546 memcpy(dst, src, dst_len * sizeof(WCHAR));
2549 else /* Append 0 if enough space */
2551 memcpy(dst, src, line_len * sizeof(WCHAR));
2558 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2559 if(!ret && line_len) /* Insufficient buffer size */
2561 if(ret < dst_len) /* Append 0 if enough space */
2562 ((LPSTR)dst)[ret] = 0;
2568 /*********************************************************************
2573 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2575 UINT s = es->selection_start;
2576 UINT e = es->selection_end;
2583 return MAKELONG(s, e);
2587 /*********************************************************************
2591 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2594 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2596 UINT strl = strlenW(lpsz_replace);
2597 UINT tl = get_text_length(es);
2608 TRACE("%s, can_undo %d, send_update %d\n",
2609 debugstr_w(lpsz_replace), can_undo, send_update);
2611 s = es->selection_start;
2612 e = es->selection_end;
2614 if ((s == e) && !strl)
2619 size = tl - (e - s) + strl;
2623 /* Issue the EN_MAXTEXT notification and continue with replacing text
2624 * such that buffer limit is honored. */
2625 if ((honor_limit) && (size > es->buffer_limit)) {
2626 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2627 /* Buffer limit can be smaller than the actual length of text in combobox */
2628 if (es->buffer_limit < (tl - (e-s)))
2631 strl = es->buffer_limit - (tl - (e-s));
2634 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2638 /* there is something to be deleted */
2639 TRACE("deleting stuff.\n");
2641 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2643 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2644 buf[bufl] = 0; /* ensure 0 termination */
2646 strcpyW(es->text + s, es->text + e);
2647 text_buffer_changed(es);
2650 /* there is an insertion */
2651 tl = get_text_length(es);
2652 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));
2653 for (p = es->text + tl ; p >= es->text + s ; p--)
2655 for (i = 0 , p = es->text + s ; i < strl ; i++)
2656 p[i] = lpsz_replace[i];
2657 if(es->style & ES_UPPERCASE)
2658 CharUpperBuffW(p, strl);
2659 else if(es->style & ES_LOWERCASE)
2660 CharLowerBuffW(p, strl);
2661 text_buffer_changed(es);
2663 if (es->style & ES_MULTILINE)
2665 INT st = min(es->selection_start, es->selection_end);
2666 INT vlc = get_vertical_line_count(es);
2668 hrgn = CreateRectRgn(0, 0, 0, 0);
2669 EDIT_BuildLineDefs_ML(es, st, st + strl,
2670 strl - abs(es->selection_end - es->selection_start), hrgn);
2671 /* if text is too long undo all changes */
2672 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2674 strcpyW(es->text + e, es->text + e + strl);
2676 for (i = 0 , p = es->text ; i < e - s ; i++)
2678 text_buffer_changed(es);
2679 EDIT_BuildLineDefs_ML(es, s, e,
2680 abs(es->selection_end - es->selection_start) - strl, hrgn);
2683 hrgn = CreateRectRgn(0, 0, 0, 0);
2684 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2688 INT fw = es->format_rect.right - es->format_rect.left;
2689 EDIT_CalcLineWidth_SL(es);
2690 /* remove chars that don't fit */
2691 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2692 while ((es->text_width > fw) && s + strl >= s) {
2693 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2695 EDIT_CalcLineWidth_SL(es);
2697 text_buffer_changed(es);
2698 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2704 utl = strlenW(es->undo_text);
2705 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2706 /* undo-buffer is extended to the right */
2707 EDIT_MakeUndoFit(es, utl + e - s);
2708 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2709 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2710 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2711 /* undo-buffer is extended to the left */
2712 EDIT_MakeUndoFit(es, utl + e - s);
2713 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2715 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2717 es->undo_position = s;
2719 /* new undo-buffer */
2720 EDIT_MakeUndoFit(es, e - s);
2721 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2722 es->undo_text[e - s] = 0; /* ensure 0 termination */
2723 es->undo_position = s;
2725 /* any deletion makes the old insertion-undo invalid */
2726 es->undo_insert_count = 0;
2728 EDIT_EM_EmptyUndoBuffer(es);
2732 if ((s == es->undo_position) ||
2733 ((es->undo_insert_count) &&
2734 (s == es->undo_position + es->undo_insert_count)))
2736 * insertion is new and at delete position or
2737 * an extension to either left or right
2739 es->undo_insert_count += strl;
2741 /* new insertion undo */
2742 es->undo_position = s;
2743 es->undo_insert_count = strl;
2744 /* new insertion makes old delete-buffer invalid */
2745 *es->undo_text = '\0';
2748 EDIT_EM_EmptyUndoBuffer(es);
2752 HeapFree(GetProcessHeap(), 0, buf);
2756 /* If text has been deleted and we're right or center aligned then scroll rightward */
2757 if (es->style & (ES_RIGHT | ES_CENTER))
2759 INT delta = strl - abs(es->selection_end - es->selection_start);
2761 if (delta < 0 && es->x_offset)
2763 if (abs(delta) > es->x_offset)
2766 es->x_offset += delta;
2770 EDIT_EM_SetSel(es, s, s, FALSE);
2771 es->flags |= EF_MODIFIED;
2772 if (send_update) es->flags |= EF_UPDATE;
2775 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2779 EDIT_UpdateText(es, NULL, TRUE);
2781 EDIT_EM_ScrollCaret(es);
2783 /* force scroll info update */
2784 EDIT_UpdateScrollInfo(es);
2787 if(send_update || (es->flags & EF_UPDATE))
2789 es->flags &= ~EF_UPDATE;
2790 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2795 /*********************************************************************
2799 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2802 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2804 if (!(es->style & ES_MULTILINE))
2808 WARN("called with NULL handle\n");
2812 EDIT_UnlockBuffer(es, TRUE);
2818 LocalFree(es->hloc32A);
2830 countA = LocalSize(hloc);
2831 textA = LocalLock(hloc);
2832 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2833 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2835 ERR("Could not allocate new unicode buffer\n");
2838 textW = LocalLock(hloc32W_new);
2839 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2840 LocalUnlock(hloc32W_new);
2844 LocalFree(es->hloc32W);
2846 es->hloc32W = hloc32W_new;
2850 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2852 es->flags |= EF_APP_HAS_HANDLE;
2853 EDIT_LockBuffer(es);
2855 es->x_offset = es->y_offset = 0;
2856 es->selection_start = es->selection_end = 0;
2857 EDIT_EM_EmptyUndoBuffer(es);
2858 es->flags &= ~EF_MODIFIED;
2859 es->flags &= ~EF_UPDATE;
2860 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2861 EDIT_UpdateText(es, NULL, TRUE);
2862 EDIT_EM_ScrollCaret(es);
2863 /* force scroll info update */
2864 EDIT_UpdateScrollInfo(es);
2868 /*********************************************************************
2872 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2875 static void EDIT_EM_SetHandle16( HWND hwnd, HLOCAL16 hloc16 )
2877 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2878 HINSTANCE16 hInstance = GetWindowLongPtrW( hwnd, GWLP_HINSTANCE );
2879 HANDLE16 oldDS = stack16->ds;
2884 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & ES_MULTILINE)) return;
2887 WARN("called with NULL handle\n");
2891 stack16->ds = hInstance;
2892 count = LocalSize16(hloc16);
2893 text = MapSL(LocalLock16(hloc16));
2894 if ((hloc32 = LocalAlloc(LMEM_MOVEABLE, count)))
2896 memcpy( LocalLock(hloc32), text, count );
2897 LocalUnlock(hloc32);
2898 LocalUnlock16(hloc16);
2899 SetWindowWord( hwnd, GWW_HANDLE16, hloc16 );
2901 stack16->ds = oldDS;
2903 if (hloc32) SendMessageA( hwnd, EM_SETHANDLE, (WPARAM)hloc32, 0 );
2904 else ERR("Could not allocate new buffer\n");
2908 /*********************************************************************
2912 * NOTE: this version currently implements WinNT limits
2915 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2917 if (!limit) limit = ~0u;
2918 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2919 es->buffer_limit = limit;
2923 /*********************************************************************
2927 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2928 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2929 * margin according to the textmetrics of the current font.
2931 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2932 * of the char's width as the margin, but this is not how Windows handles this.
2933 * For all other fonts Windows sets the margins to zero.
2935 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2936 * edit control is equal to or larger than a certain size.
2937 * Interestingly if one subtracts both the left and right margins from
2938 * this size one always seems to get an even number. The extents of
2939 * the (four character) string "'**'" match this quite closely, so
2940 * we'll use this until we come up with a better idea.
2942 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2944 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2947 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2948 return sz.cx + left + right;
2951 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2952 WORD left, WORD right, BOOL repaint)
2955 INT default_left_margin = 0; /* in pixels */
2956 INT default_right_margin = 0; /* in pixels */
2958 /* Set the default margins depending on the font */
2959 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2960 HDC dc = GetDC(es->hwndSelf);
2961 HFONT old_font = SelectObject(dc, es->font);
2962 GetTextMetricsW(dc, &tm);
2963 /* The default margins are only non zero for TrueType or Vector fonts */
2964 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2967 /* This must be calculated more exactly! But how? */
2968 default_left_margin = tm.tmAveCharWidth / 2;
2969 default_right_margin = tm.tmAveCharWidth / 2;
2970 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2971 GetClientRect(es->hwndSelf, &rc);
2972 if(rc.right - rc.left < min_size) {
2973 default_left_margin = es->left_margin;
2974 default_right_margin = es->right_margin;
2977 SelectObject(dc, old_font);
2978 ReleaseDC(es->hwndSelf, dc);
2981 if (action & EC_LEFTMARGIN) {
2982 es->format_rect.left -= es->left_margin;
2983 if (left != EC_USEFONTINFO)
2984 es->left_margin = left;
2986 es->left_margin = default_left_margin;
2987 es->format_rect.left += es->left_margin;
2990 if (action & EC_RIGHTMARGIN) {
2991 es->format_rect.right += es->right_margin;
2992 if (right != EC_USEFONTINFO)
2993 es->right_margin = right;
2995 es->right_margin = default_right_margin;
2996 es->format_rect.right -= es->right_margin;
2999 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3000 EDIT_AdjustFormatRect(es);
3001 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3004 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3008 /*********************************************************************
3010 * EM_SETPASSWORDCHAR
3013 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3017 if (es->style & ES_MULTILINE)
3020 if (es->password_char == c)
3023 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3024 es->password_char = c;
3026 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3027 es->style |= ES_PASSWORD;
3029 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3030 es->style &= ~ES_PASSWORD;
3032 EDIT_UpdateText(es, NULL, TRUE);
3036 /*********************************************************************
3041 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3043 if (!(es->style & ES_MULTILINE))
3045 HeapFree(GetProcessHeap(), 0, es->tabs);
3046 es->tabs_count = count;
3050 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3051 memcpy(es->tabs, tabs, count * sizeof(INT));
3057 /*********************************************************************
3059 * EM_SETWORDBREAKPROC
3062 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3064 if (es->word_break_proc == wbp)
3067 es->word_break_proc = wbp;
3069 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3070 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3071 EDIT_UpdateText(es, NULL, TRUE);
3076 /*********************************************************************
3081 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3086 /* As per MSDN spec, for a single-line edit control,
3087 the return value is always TRUE */
3088 if( es->style & ES_READONLY )
3089 return !(es->style & ES_MULTILINE);
3091 ulength = strlenW(es->undo_text);
3093 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3095 strcpyW(utext, es->undo_text);
3097 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3098 es->undo_insert_count, debugstr_w(utext));
3100 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3101 EDIT_EM_EmptyUndoBuffer(es);
3102 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3103 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3104 /* send the notification after the selection start and end are set */
3105 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3106 EDIT_EM_ScrollCaret(es);
3107 HeapFree(GetProcessHeap(), 0, utext);
3109 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3110 es->undo_insert_count, debugstr_w(es->undo_text));
3115 /* Helper function for WM_CHAR
3117 * According to an MSDN blog article titled "Just because you're a control
3118 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3119 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3120 * a dialog box or not.
3122 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3124 return (es->flags & EF_DIALOGMODE);
3128 /*********************************************************************
3133 static void EDIT_WM_Paste(EDITSTATE *es)
3138 /* Protect read-only edit control from modification */
3139 if(es->style & ES_READONLY)
3142 OpenClipboard(es->hwndSelf);
3143 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3144 src = GlobalLock(hsrc);
3145 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
3148 else if (es->style & ES_PASSWORD) {
3149 /* clear selected text in password edit box even with empty clipboard */
3150 const WCHAR empty_strW[] = { 0 };
3151 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
3157 /*********************************************************************
3162 static void EDIT_WM_Copy(EDITSTATE *es)
3164 INT s = min(es->selection_start, es->selection_end);
3165 INT e = max(es->selection_start, es->selection_end);
3173 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3174 dst = GlobalLock(hdst);
3175 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3176 dst[len] = 0; /* ensure 0 termination */
3177 TRACE("%s\n", debugstr_w(dst));
3179 OpenClipboard(es->hwndSelf);
3181 SetClipboardData(CF_UNICODETEXT, hdst);
3186 /*********************************************************************
3191 static inline void EDIT_WM_Clear(EDITSTATE *es)
3193 static const WCHAR empty_stringW[] = {0};
3195 /* Protect read-only edit control from modification */
3196 if(es->style & ES_READONLY)
3199 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
3203 /*********************************************************************
3208 static inline void EDIT_WM_Cut(EDITSTATE *es)
3215 /*********************************************************************
3220 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3224 control = GetKeyState(VK_CONTROL) & 0x8000;
3228 /* If it's not a multiline edit box, it would be ignored below.
3229 * For multiline edit without ES_WANTRETURN, we have to make a
3232 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3233 if (EDIT_IsInsideDialog(es))
3236 if (es->style & ES_MULTILINE) {
3237 if (es->style & ES_READONLY) {
3238 EDIT_MoveHome(es, FALSE, FALSE);
3239 EDIT_MoveDown_ML(es, FALSE);
3241 static const WCHAR cr_lfW[] = {'\r','\n',0};
3242 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3247 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3249 static const WCHAR tabW[] = {'\t',0};
3250 if (EDIT_IsInsideDialog(es))
3252 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3256 if (!(es->style & ES_READONLY) && !control) {
3257 if (es->selection_start != es->selection_end)
3260 /* delete character left of caret */
3261 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3262 EDIT_MoveBackward(es, TRUE);
3268 if (!(es->style & ES_PASSWORD))
3269 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3272 if (!(es->style & ES_READONLY))
3273 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3276 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3277 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3280 if (!(es->style & ES_READONLY))
3281 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3285 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3286 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3289 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3293 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3301 /*********************************************************************
3306 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3308 if (code || control)
3313 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3316 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3319 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3322 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3325 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3328 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3329 EDIT_EM_ScrollCaret(es);
3332 ERR("unknown menu item, please report\n");
3338 /*********************************************************************
3342 * Note: the resource files resource/sysres_??.rc cannot define a
3343 * single popup menu. Hence we use a (dummy) menubar
3344 * containing the single popup menu as its first item.
3346 * FIXME: the message identifiers have been chosen arbitrarily,
3347 * hence we use MF_BYPOSITION.
3348 * We might as well use the "real" values (anybody knows ?)
3349 * The menu definition is in resources/sysres_??.rc.
3350 * Once these are OK, we better use MF_BYCOMMAND here
3351 * (as we do in EDIT_WM_Command()).
3354 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3356 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3357 HMENU popup = GetSubMenu(menu, 0);
3358 UINT start = es->selection_start;
3359 UINT end = es->selection_end;
3361 ORDER_UINT(start, end);
3364 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3366 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3368 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3370 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3372 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3374 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3376 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3379 /* Windows places the menu at the edit's center in this case */
3380 GetClientRect(es->hwndSelf, &rc);
3381 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3382 x = rc.left + (rc.right - rc.left) / 2;
3383 y = rc.top + (rc.bottom - rc.top) / 2;
3386 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3391 /*********************************************************************
3396 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3398 if(!count) return 0;
3402 lstrcpynW(dst, es->text, count);
3403 return strlenW(dst);
3407 LPSTR textA = (LPSTR)dst;
3408 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3409 textA[count - 1] = 0; /* ensure 0 termination */
3410 return strlen(textA);
3414 /*********************************************************************
3419 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3421 HWND hLBox = es->hwndListBox;
3429 hCombo = GetParent(es->hwndSelf);
3433 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3435 if (key == VK_UP || key == VK_DOWN)
3437 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3440 if (msg == WM_KEYDOWN || nEUI)
3441 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3447 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3449 /* make sure ComboLBox pops up */
3450 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3455 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3458 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3460 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3462 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
3467 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3473 /*********************************************************************
3477 * Handling of special keys that don't produce a WM_CHAR
3478 * (i.e. non-printable keys) & Backspace & Delete
3481 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3486 if (GetKeyState(VK_MENU) & 0x8000)
3489 shift = GetKeyState(VK_SHIFT) & 0x8000;
3490 control = GetKeyState(VK_CONTROL) & 0x8000;
3495 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3500 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3501 EDIT_MoveUp_ML(es, shift);
3504 EDIT_MoveWordBackward(es, shift);
3506 EDIT_MoveBackward(es, shift);
3509 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3513 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3514 EDIT_MoveDown_ML(es, shift);
3516 EDIT_MoveWordForward(es, shift);
3518 EDIT_MoveForward(es, shift);
3521 EDIT_MoveHome(es, shift, control);
3524 EDIT_MoveEnd(es, shift, control);
3527 if (es->style & ES_MULTILINE)
3528 EDIT_MovePageUp_ML(es, shift);
3530 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3533 if (es->style & ES_MULTILINE)
3534 EDIT_MovePageDown_ML(es, shift);
3536 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3539 if (!(es->style & ES_READONLY) && !(shift && control)) {
3540 if (es->selection_start != es->selection_end) {
3547 /* delete character left of caret */
3548 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3549 EDIT_MoveBackward(es, TRUE);
3551 } else if (control) {
3552 /* delete to end of line */
3553 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3554 EDIT_MoveEnd(es, TRUE, FALSE);
3557 /* delete character right of caret */
3558 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3559 EDIT_MoveForward(es, TRUE);
3567 if (!(es->style & ES_READONLY))
3573 /* If the edit doesn't want the return send a message to the default object */
3574 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3578 if (!EDIT_IsInsideDialog(es)) break;
3580 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3581 if (HIWORD(dw) == DC_HASDEFID)
3583 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3586 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, (LPARAM)TRUE);
3587 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3593 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3594 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3597 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3598 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3605 /*********************************************************************
3610 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3612 es->flags &= ~EF_FOCUSED;
3614 if(!(es->style & ES_NOHIDESEL))
3615 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3616 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3621 /*********************************************************************
3625 * The caret position has been set on the WM_LBUTTONDOWN message
3628 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3631 INT e = es->selection_end;
3636 es->bCaptureState = TRUE;
3637 SetCapture(es->hwndSelf);
3639 l = EDIT_EM_LineFromChar(es, e);
3640 li = EDIT_EM_LineIndex(es, l);
3641 ll = EDIT_EM_LineLength(es, e);
3642 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3643 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3644 EDIT_EM_SetSel(es, s, e, FALSE);
3645 EDIT_EM_ScrollCaret(es);
3646 es->region_posx = es->region_posy = 0;
3647 SetTimer(es->hwndSelf, 0, 100, NULL);
3652 /*********************************************************************
3657 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3662 es->bCaptureState = TRUE;
3663 SetCapture(es->hwndSelf);
3664 EDIT_ConfinePoint(es, &x, &y);
3665 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3666 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3667 EDIT_EM_ScrollCaret(es);
3668 es->region_posx = es->region_posy = 0;
3669 SetTimer(es->hwndSelf, 0, 100, NULL);
3671 if (!(es->flags & EF_FOCUSED))
3672 SetFocus(es->hwndSelf);
3678 /*********************************************************************
3683 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3685 if (es->bCaptureState) {
3686 KillTimer(es->hwndSelf, 0);
3687 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3689 es->bCaptureState = FALSE;
3694 /*********************************************************************
3699 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3701 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3706 /*********************************************************************
3711 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3717 /* If the mouse has been captured by process other than the edit control itself,
3718 * the windows edit controls will not select the strings with mouse move.
3720 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3724 * FIXME: gotta do some scrolling if outside client
3725 * area. Maybe reset the timer ?
3728 EDIT_ConfinePoint(es, &x, &y);
3729 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3730 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3731 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3732 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3733 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3738 /*********************************************************************
3743 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3756 BOOL rev = es->bEnableState &&
3757 ((es->flags & EF_FOCUSED) ||
3758 (es->style & ES_NOHIDESEL));
3759 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3761 GetClientRect(es->hwndSelf, &rcClient);
3763 /* get the background brush */
3764 brush = EDIT_NotifyCtlColor(es, dc);
3766 /* paint the border and the background */
3767 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3769 if(es->style & WS_BORDER) {
3770 bw = GetSystemMetrics(SM_CXBORDER);
3771 bh = GetSystemMetrics(SM_CYBORDER);
3773 if(es->style & ES_MULTILINE) {
3774 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3775 if(es->style & WS_VSCROLL) rc.right+=bw;
3778 /* Draw the frame. Same code as in nonclient.c */
3779 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3780 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3781 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3782 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3783 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3784 SelectObject(dc, old_brush);
3786 /* Keep the border clean */
3787 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3788 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3791 GetClipBox(dc, &rc);
3792 FillRect(dc, &rc, brush);
3794 IntersectClipRect(dc, es->format_rect.left,
3795 es->format_rect.top,
3796 es->format_rect.right,
3797 es->format_rect.bottom);
3798 if (es->style & ES_MULTILINE) {
3800 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3803 old_font = SelectObject(dc, es->font);
3805 if (!es->bEnableState)
3806 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3807 GetClipBox(dc, &rcRgn);
3808 if (es->style & ES_MULTILINE) {
3809 INT vlc = get_vertical_line_count(es);
3810 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3811 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3812 if (IntersectRect(&rc, &rcRgn, &rcLine))
3813 EDIT_PaintLine(es, dc, i, rev);
3816 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3817 if (IntersectRect(&rc, &rcRgn, &rcLine))
3818 EDIT_PaintLine(es, dc, 0, rev);
3821 SelectObject(dc, old_font);
3824 EndPaint(es->hwndSelf, &ps);
3828 /*********************************************************************
3833 static void EDIT_WM_SetFocus(EDITSTATE *es)
3835 es->flags |= EF_FOCUSED;
3837 if (!(es->style & ES_NOHIDESEL))
3838 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3840 /* single line edit updates itself */
3841 if (!(es->style & ES_MULTILINE))
3843 HDC hdc = GetDC(es->hwndSelf);
3844 EDIT_WM_Paint(es, hdc);
3845 ReleaseDC(es->hwndSelf, hdc);
3848 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3849 EDIT_SetCaretPos(es, es->selection_end,
3850 es->flags & EF_AFTER_WRAP);
3851 ShowCaret(es->hwndSelf);
3852 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3856 /*********************************************************************
3860 * With Win95 look the margins are set to default font value unless
3861 * the system font (font == 0) is being set, in which case they are left
3865 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3873 dc = GetDC(es->hwndSelf);
3875 old_font = SelectObject(dc, font);
3876 GetTextMetricsW(dc, &tm);
3877 es->line_height = tm.tmHeight;
3878 es->char_width = tm.tmAveCharWidth;
3880 SelectObject(dc, old_font);
3881 ReleaseDC(es->hwndSelf, dc);
3883 /* Reset the format rect and the margins */
3884 GetClientRect(es->hwndSelf, &clientRect);
3885 EDIT_SetRectNP(es, &clientRect);
3886 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3887 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3889 if (es->style & ES_MULTILINE)
3890 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3892 EDIT_CalcLineWidth_SL(es);
3895 EDIT_UpdateText(es, NULL, TRUE);
3896 if (es->flags & EF_FOCUSED) {
3898 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3899 EDIT_SetCaretPos(es, es->selection_end,
3900 es->flags & EF_AFTER_WRAP);
3901 ShowCaret(es->hwndSelf);
3906 /*********************************************************************
3911 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3912 * The modified flag is reset. No notifications are sent.
3914 * For single-line controls, reception of WM_SETTEXT triggers:
3915 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3918 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3920 LPWSTR textW = NULL;
3921 if (!unicode && text)
3923 LPCSTR textA = (LPCSTR)text;
3924 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3925 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3927 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3931 if (es->flags & EF_UPDATE)
3932 /* fixed this bug once; complain if we see it about to happen again. */
3933 ERR("SetSel may generate UPDATE message whose handler may reset "
3936 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3939 TRACE("%s\n", debugstr_w(text));
3940 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
3942 HeapFree(GetProcessHeap(), 0, textW);
3946 static const WCHAR empty_stringW[] = {0};
3948 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
3951 es->flags &= ~EF_MODIFIED;
3952 EDIT_EM_SetSel(es, 0, 0, FALSE);
3954 /* Send the notification after the selection start and end have been set
3955 * edit control doesn't send notification on WM_SETTEXT
3956 * if it is multiline, or it is part of combobox
3958 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3960 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3961 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3963 EDIT_EM_ScrollCaret(es);
3964 EDIT_UpdateScrollInfo(es);
3968 /*********************************************************************
3973 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
3975 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3977 TRACE("width = %d, height = %d\n", width, height);
3978 SetRect(&rc, 0, 0, width, height);
3979 EDIT_SetRectNP(es, &rc);
3980 EDIT_UpdateText(es, NULL, TRUE);
3985 /*********************************************************************
3989 * This message is sent by SetWindowLong on having changed either the Style
3990 * or the extended style.
3992 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3994 * See also EDIT_WM_NCCreate
3996 * It appears that the Windows version of the edit control allows the style
3997 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3998 * style variable which will generally be different. In this function we
3999 * update the internal style based on what changed in the externally visible
4002 * Much of this content as based upon the MSDN, especially:
4003 * Platform SDK Documentation -> User Interface Services ->
4004 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4005 * Edit Control Styles
4007 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4009 if (GWL_STYLE == which) {
4010 DWORD style_change_mask;
4012 /* Only a subset of changes can be applied after the control
4015 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4017 if (es->style & ES_MULTILINE)
4018 style_change_mask |= ES_WANTRETURN;
4020 new_style = style->styleNew & style_change_mask;
4022 /* Number overrides lowercase overrides uppercase (at least it
4023 * does in Win95). However I'll bet that ES_NUMBER would be
4024 * invalid under Win 3.1.
4026 if (new_style & ES_NUMBER) {
4027 ; /* do not override the ES_NUMBER */
4028 } else if (new_style & ES_LOWERCASE) {
4029 new_style &= ~ES_UPPERCASE;
4032 es->style = (es->style & ~style_change_mask) | new_style;
4033 } else if (GWL_EXSTYLE == which) {
4034 ; /* FIXME - what is needed here */
4036 WARN ("Invalid style change %ld\n",which);
4042 /*********************************************************************
4047 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4049 if ((key == VK_BACK) && (key_data & 0x2000)) {
4050 if (EDIT_EM_CanUndo(es))
4053 } else if (key == VK_UP || key == VK_DOWN) {
4054 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4057 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4061 /*********************************************************************
4066 static void EDIT_WM_Timer(EDITSTATE *es)
4068 if (es->region_posx < 0) {
4069 EDIT_MoveBackward(es, TRUE);
4070 } else if (es->region_posx > 0) {
4071 EDIT_MoveForward(es, TRUE);
4074 * FIXME: gotta do some vertical scrolling here, like
4075 * EDIT_EM_LineScroll(hwnd, 0, 1);
4079 /*********************************************************************
4084 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4089 if (!(es->style & ES_MULTILINE))
4092 if (!(es->style & ES_AUTOHSCROLL))
4096 fw = es->format_rect.right - es->format_rect.left;
4099 TRACE("SB_LINELEFT\n");
4101 dx = -es->char_width;
4104 TRACE("SB_LINERIGHT\n");
4105 if (es->x_offset < es->text_width)
4106 dx = es->char_width;
4109 TRACE("SB_PAGELEFT\n");
4111 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4114 TRACE("SB_PAGERIGHT\n");
4115 if (es->x_offset < es->text_width)
4116 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4124 TRACE("SB_RIGHT\n");
4125 if (es->x_offset < es->text_width)
4126 dx = es->text_width - es->x_offset;
4129 TRACE("SB_THUMBTRACK %d\n", pos);
4130 es->flags |= EF_HSCROLL_TRACK;
4131 if(es->style & WS_HSCROLL)
4132 dx = pos - es->x_offset;
4137 if(pos < 0 || pos > 100) return 0;
4138 /* Assume default scroll range 0-100 */
4139 fw = es->format_rect.right - es->format_rect.left;
4140 new_x = pos * (es->text_width - fw) / 100;
4141 dx = es->text_width ? (new_x - es->x_offset) : 0;
4144 case SB_THUMBPOSITION:
4145 TRACE("SB_THUMBPOSITION %d\n", pos);
4146 es->flags &= ~EF_HSCROLL_TRACK;
4147 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4148 dx = pos - es->x_offset;
4153 if(pos < 0 || pos > 100) return 0;
4154 /* Assume default scroll range 0-100 */
4155 fw = es->format_rect.right - es->format_rect.left;
4156 new_x = pos * (es->text_width - fw) / 100;
4157 dx = es->text_width ? (new_x - es->x_offset) : 0;
4160 /* force scroll info update */
4161 EDIT_UpdateScrollInfo(es);
4162 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4166 TRACE("SB_ENDSCROLL\n");
4169 * FIXME : the next two are undocumented !
4170 * Are we doing the right thing ?
4171 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4172 * although it's also a regular control message.
4174 case EM_GETTHUMB: /* this one is used by NT notepad */
4178 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4179 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4182 /* Assume default scroll range 0-100 */
4183 INT fw = es->format_rect.right - es->format_rect.left;
4184 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4186 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4189 case EM_LINESCROLL16:
4190 TRACE("EM_LINESCROLL16\n");
4195 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4201 INT fw = es->format_rect.right - es->format_rect.left;
4202 /* check if we are going to move too far */
4203 if(es->x_offset + dx + fw > es->text_width)
4204 dx = es->text_width - fw - es->x_offset;
4206 EDIT_EM_LineScroll_internal(es, dx, 0);
4212 /*********************************************************************
4217 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4221 if (!(es->style & ES_MULTILINE))
4224 if (!(es->style & ES_AUTOVSCROLL))
4233 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4234 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4235 (action == SB_PAGEUP ? "SB_PAGEUP" :
4237 EDIT_EM_Scroll(es, action);
4244 TRACE("SB_BOTTOM\n");
4245 dy = es->line_count - 1 - es->y_offset;
4248 TRACE("SB_THUMBTRACK %d\n", pos);
4249 es->flags |= EF_VSCROLL_TRACK;
4250 if(es->style & WS_VSCROLL)
4251 dy = pos - es->y_offset;
4254 /* Assume default scroll range 0-100 */
4257 if(pos < 0 || pos > 100) return 0;
4258 vlc = get_vertical_line_count(es);
4259 new_y = pos * (es->line_count - vlc) / 100;
4260 dy = es->line_count ? (new_y - es->y_offset) : 0;
4261 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4262 es->line_count, es->y_offset, pos, dy);
4265 case SB_THUMBPOSITION:
4266 TRACE("SB_THUMBPOSITION %d\n", pos);
4267 es->flags &= ~EF_VSCROLL_TRACK;
4268 if(es->style & WS_VSCROLL)
4269 dy = pos - es->y_offset;
4272 /* Assume default scroll range 0-100 */
4275 if(pos < 0 || pos > 100) return 0;
4276 vlc = get_vertical_line_count(es);
4277 new_y = pos * (es->line_count - vlc) / 100;
4278 dy = es->line_count ? (new_y - es->y_offset) : 0;
4279 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4280 es->line_count, es->y_offset, pos, dy);
4284 /* force scroll info update */
4285 EDIT_UpdateScrollInfo(es);
4286 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4290 TRACE("SB_ENDSCROLL\n");
4293 * FIXME : the next two are undocumented !
4294 * Are we doing the right thing ?
4295 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4296 * although it's also a regular control message.
4298 case EM_GETTHUMB: /* this one is used by NT notepad */
4302 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4303 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4306 /* Assume default scroll range 0-100 */
4307 INT vlc = get_vertical_line_count(es);
4308 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4310 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4313 case EM_LINESCROLL16:
4314 TRACE("EM_LINESCROLL16 %d\n", pos);
4319 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4324 EDIT_EM_LineScroll(es, 0, dy);
4328 /*********************************************************************
4332 * FIXME: is this right ? (or should it be only VSCROLL)
4333 * (and maybe only for edit controls that really have their
4334 * own scrollbars) (and maybe only for multiline controls ?)
4335 * All in all: very poorly documented
4338 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4340 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
4341 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
4345 /********************************************************************
4347 * The Following code is to handle inline editing from IMEs
4350 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4353 LPWSTR lpCompStr = NULL;
4354 LPSTR lpCompStrAttr = NULL;
4357 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4364 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
4367 ERR("Unable to allocate IME CompositionString\n");
4372 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4373 lpCompStr[buflen/sizeof(WCHAR)] = 0;
4375 if (CompFlag & GCS_COMPATTR)
4378 * We do not use the attributes yet. it would tell us what characters
4379 * are in transition and which are converted or decided upon
4381 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4385 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4388 ERR("Unable to allocate IME Attribute String\n");
4389 HeapFree(GetProcessHeap(),0,lpCompStr);
4392 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4394 lpCompStrAttr[dwBufLenAttr] = 0;
4397 lpCompStrAttr = NULL;
4400 /* check for change in composition start */
4401 if (es->selection_end < es->composition_start)
4402 es->composition_start = es->selection_end;
4404 /* replace existing selection string */
4405 es->selection_start = es->composition_start;
4407 if (es->composition_len > 0)
4408 es->selection_end = es->composition_start + es->composition_len;
4410 es->selection_end = es->selection_start;
4412 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
4413 es->composition_len = abs(es->composition_start - es->selection_end);
4415 es->selection_start = es->composition_start;
4416 es->selection_end = es->selection_start + es->composition_len;
4418 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4419 HeapFree(GetProcessHeap(),0,lpCompStr);
4422 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4427 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4433 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
4436 ERR("Unable to alloc buffer for IME string\n");
4440 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4441 lpResultStr[buflen/sizeof(WCHAR)] = 0;
4443 /* check for change in composition start */
4444 if (es->selection_end < es->composition_start)
4445 es->composition_start = es->selection_end;
4447 es->selection_start = es->composition_start;
4448 es->selection_end = es->composition_start + es->composition_len;
4449 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
4450 es->composition_start = es->selection_end;
4451 es->composition_len = 0;
4453 HeapFree(GetProcessHeap(),0,lpResultStr);
4456 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4461 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4463 static const WCHAR empty_stringW[] = {0};
4464 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4465 es->composition_start = es->selection_end;
4468 hIMC = ImmGetContext(hwnd);
4472 if (CompFlag & GCS_RESULTSTR)
4473 EDIT_GetResultStr(hIMC, es);
4474 if (CompFlag & GCS_COMPSTR)
4475 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4476 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4477 ImmReleaseContext(hwnd, hIMC);
4478 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4482 /*********************************************************************
4486 * See also EDIT_WM_StyleChanged
4488 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4493 TRACE("Creating %s edit control, style = %08x\n",
4494 unicode ? "Unicode" : "ANSI", lpcs->style);
4496 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4498 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4501 * Note: since the EDITSTATE has not been fully initialized yet,
4502 * we can't use any API calls that may send
4503 * WM_XXX messages before WM_NCCREATE is completed.
4506 es->is_unicode = unicode;
4507 es->style = lpcs->style;
4509 es->bEnableState = !(es->style & WS_DISABLED);
4511 es->hwndSelf = hwnd;
4512 /* Save parent, which will be notified by EN_* messages */
4513 es->hwndParent = lpcs->hwndParent;
4515 if (es->style & ES_COMBO)
4516 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4518 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4519 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4521 /* Number overrides lowercase overrides uppercase (at least it
4522 * does in Win95). However I'll bet that ES_NUMBER would be
4523 * invalid under Win 3.1.
4525 if (es->style & ES_NUMBER) {
4526 ; /* do not override the ES_NUMBER */
4527 } else if (es->style & ES_LOWERCASE) {
4528 es->style &= ~ES_UPPERCASE;
4530 if (es->style & ES_MULTILINE) {
4531 es->buffer_limit = BUFLIMIT_INITIAL;
4532 if (es->style & WS_VSCROLL)
4533 es->style |= ES_AUTOVSCROLL;
4534 if (es->style & WS_HSCROLL)
4535 es->style |= ES_AUTOHSCROLL;
4536 es->style &= ~ES_PASSWORD;
4537 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4538 /* Confirmed - RIGHT overrides CENTER */
4539 if (es->style & ES_RIGHT)
4540 es->style &= ~ES_CENTER;
4541 es->style &= ~WS_HSCROLL;
4542 es->style &= ~ES_AUTOHSCROLL;
4545 es->buffer_limit = BUFLIMIT_INITIAL;
4546 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4547 es->style &= ~ES_CENTER;
4548 es->style &= ~WS_HSCROLL;
4549 es->style &= ~WS_VSCROLL;
4550 if (es->style & ES_PASSWORD)
4551 es->password_char = '*';
4554 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4555 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4557 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4559 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4561 es->undo_buffer_size = es->buffer_size;
4563 if (es->style & ES_MULTILINE)
4564 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4569 * In Win95 look and feel, the WS_BORDER style is replaced by the
4570 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4571 * control a nonclient area so we don't need to draw the border.
4572 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4573 * a nonclient area and we should handle painting the border ourselves.
4575 * When making modifications please ensure that the code still works
4576 * for edit controls created directly with style 0x50800000, exStyle 0
4577 * (which should have a single pixel border)
4579 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4580 es->style &= ~WS_BORDER;
4581 else if (es->style & WS_BORDER)
4582 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4587 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4588 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4589 HeapFree(GetProcessHeap(), 0, es->undo_text);
4590 if (es->hloc32W) LocalFree(es->hloc32W);
4591 HeapFree(GetProcessHeap(), 0, es);
4596 /*********************************************************************
4601 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4605 TRACE("%s\n", debugstr_w(name));
4607 * To initialize some final structure members, we call some helper
4608 * functions. However, since the EDITSTATE is not consistent (i.e.
4609 * not fully initialized), we should be very careful which
4610 * functions can be called, and in what order.
4612 EDIT_WM_SetFont(es, 0, FALSE);
4613 EDIT_EM_EmptyUndoBuffer(es);
4615 /* We need to calculate the format rect
4616 (applications may send EM_SETMARGINS before the control gets visible) */
4617 GetClientRect(es->hwndSelf, &clientRect);
4618 EDIT_SetRectNP(es, &clientRect);
4620 if (name && *name) {
4621 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4622 /* if we insert text to the editline, the text scrolls out
4623 * of the window, as the caret is placed after the insert
4624 * pos normally; thus we reset es->selection... to 0 and
4627 es->selection_start = es->selection_end = 0;
4628 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4629 * Messages are only to be sent when the USER does something to
4630 * change the contents. So I am removing this EN_CHANGE
4632 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4634 EDIT_EM_ScrollCaret(es);
4636 /* force scroll info update */
4637 EDIT_UpdateScrollInfo(es);
4638 /* The rule seems to return 1 here for success */
4639 /* Power Builder masked edit controls will crash */
4641 /* FIXME: is that in all cases so ? */
4646 /*********************************************************************
4651 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4654 HLOCAL16 hloc16 = GetWindowWord( es->hwndSelf, GWW_HANDLE16 );
4657 LocalFree(es->hloc32W);
4660 LocalFree(es->hloc32A);
4663 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4664 HANDLE16 oldDS = stack16->ds;
4666 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4667 while (LocalUnlock16(hloc16)) ;
4668 LocalFree16(hloc16);
4669 stack16->ds = oldDS;
4670 SetWindowWord( es->hwndSelf, GWW_HANDLE16, 0 );
4673 pc = es->first_line_def;
4677 HeapFree(GetProcessHeap(), 0, pc);
4681 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4682 HeapFree(GetProcessHeap(), 0, es->undo_text);
4683 HeapFree(GetProcessHeap(), 0, es);
4689 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4692 return DefWindowProcW(hwnd, msg, wParam, lParam);
4694 return DefWindowProcA(hwnd, msg, wParam, lParam);
4697 /*********************************************************************
4699 * EditWndProc_common
4701 * The messages are in the order of the actual integer values
4702 * (which can be found in include/windows.h)
4704 static LRESULT EditWndProc_common( HWND hwnd, UINT msg,
4705 WPARAM wParam, LPARAM lParam, BOOL unicode )
4707 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4710 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4712 if (!es && msg != WM_NCCREATE)
4713 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4715 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4719 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4723 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4724 EDIT_EM_ScrollCaret(es);
4730 CopyRect((LPRECT)lParam, &es->format_rect);
4734 if ((es->style & ES_MULTILINE) && lParam) {
4735 EDIT_SetRectNP(es, (LPRECT)lParam);
4736 EDIT_UpdateText(es, NULL, TRUE);
4741 if ((es->style & ES_MULTILINE) && lParam)
4742 EDIT_SetRectNP(es, (LPRECT)lParam);
4746 result = EDIT_EM_Scroll(es, (INT)wParam);
4750 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4753 case EM_SCROLLCARET:
4754 EDIT_EM_ScrollCaret(es);
4759 result = ((es->flags & EF_MODIFIED) != 0);
4764 es->flags |= EF_MODIFIED;
4766 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4769 case EM_GETLINECOUNT:
4770 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4774 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4778 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4782 result = (LRESULT)EDIT_EM_GetHandle(es);
4786 result = EDIT_EM_GetThumb(es);
4789 /* these messages missing from specs */
4794 FIXME("undocumented message 0x%x, please report\n", msg);
4795 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4799 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4807 textW = (LPWSTR)lParam;
4810 LPSTR textA = (LPSTR)lParam;
4811 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4812 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4813 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4816 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
4820 HeapFree(GetProcessHeap(), 0, textW);
4825 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4828 case EM_SETLIMITTEXT:
4829 EDIT_EM_SetLimitText(es, wParam);
4833 result = (LRESULT)EDIT_EM_CanUndo(es);
4838 result = (LRESULT)EDIT_EM_Undo(es);
4842 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4845 case EM_LINEFROMCHAR:
4846 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4849 case EM_SETTABSTOPS:
4850 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4853 case EM_SETPASSWORDCHAR:
4858 charW = (WCHAR)wParam;
4861 CHAR charA = wParam;
4862 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4865 EDIT_EM_SetPasswordChar(es, charW);
4869 case EM_EMPTYUNDOBUFFER:
4870 EDIT_EM_EmptyUndoBuffer(es);
4873 case EM_GETFIRSTVISIBLELINE:
4874 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4877 case EM_SETREADONLY:
4879 DWORD old_style = es->style;
4882 SetWindowLongW( hwnd, GWL_STYLE,
4883 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4884 es->style |= ES_READONLY;
4886 SetWindowLongW( hwnd, GWL_STYLE,
4887 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4888 es->style &= ~ES_READONLY;
4891 if (old_style ^ es->style)
4892 InvalidateRect(es->hwndSelf, NULL, TRUE);
4898 case EM_SETWORDBREAKPROC:
4899 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4902 case EM_GETWORDBREAKPROC:
4903 result = (LRESULT)es->word_break_proc;
4906 case EM_GETPASSWORDCHAR:
4909 result = es->password_char;
4912 WCHAR charW = es->password_char;
4914 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4921 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4925 result = MAKELONG(es->left_margin, es->right_margin);
4928 case EM_GETLIMITTEXT:
4929 result = es->buffer_limit;
4932 case EM_POSFROMCHAR:
4933 if ((INT)wParam >= get_text_length(es)) result = -1;
4934 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4937 case EM_CHARFROMPOS:
4938 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4941 /* End of the EM_ messages which were in numerical order; what order
4942 * are these in? vaguely alphabetical?
4946 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4950 result = EDIT_WM_NCDestroy(es);
4955 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4957 if (es->style & ES_MULTILINE)
4958 result |= DLGC_WANTALLKEYS;
4962 es->flags|=EF_DIALOGMODE;
4964 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4966 int vk = (int)((LPMSG)lParam)->wParam;
4968 if (es->hwndListBox)
4970 if (vk == VK_RETURN || vk == VK_ESCAPE)
4971 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4972 result |= DLGC_WANTMESSAGE;
4984 strng[0] = wParam >> 8;
4985 strng[1] = wParam & 0xff;
4986 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4987 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4988 result = EDIT_WM_Char(es, charW);
5000 CHAR charA = wParam;
5001 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
5004 if (es->hwndListBox)
5006 if (charW == VK_RETURN || charW == VK_ESCAPE)
5008 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
5009 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
5013 result = EDIT_WM_Char(es, charW);
5020 if (wParam == UNICODE_NOCHAR) return TRUE;
5021 if (wParam <= 0x000fffff)
5023 if(wParam > 0xffff) /* convert to surrogates */
5026 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
5027 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
5029 else EDIT_WM_Char(es, wParam);
5040 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
5043 case WM_CONTEXTMENU:
5044 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5053 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
5056 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
5057 LPWSTR nameW = NULL;
5060 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5061 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5062 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5064 result = EDIT_WM_Create(es, nameW);
5065 HeapFree(GetProcessHeap(), 0, nameW);
5074 es->bEnableState = (BOOL) wParam;
5075 EDIT_UpdateText(es, NULL, TRUE);
5079 /* we do the proper erase in EDIT_WM_Paint */
5084 result = (LRESULT)es->font;
5088 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5091 case WM_GETTEXTLENGTH:
5092 if (unicode) result = get_text_length(es);
5093 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5094 NULL, 0, NULL, NULL );
5098 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5102 result = EDIT_WM_KeyDown(es, (INT)wParam);
5106 result = EDIT_WM_KillFocus(es);
5109 case WM_LBUTTONDBLCLK:
5110 result = EDIT_WM_LButtonDblClk(es);
5113 case WM_LBUTTONDOWN:
5114 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5118 result = EDIT_WM_LButtonUp(es);
5121 case WM_MBUTTONDOWN:
5122 result = EDIT_WM_MButtonDown(es);
5126 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5129 case WM_PRINTCLIENT:
5131 EDIT_WM_Paint(es, (HDC)wParam);
5139 EDIT_WM_SetFocus(es);
5143 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5147 /* FIXME: actually set an internal flag and behave accordingly */
5151 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5156 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
5159 case WM_STYLECHANGED:
5160 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5163 case WM_STYLECHANGING:
5164 result = 0; /* See EDIT_WM_StyleChanged */
5168 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5176 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5181 int gcWheelDelta = 0;
5182 UINT pulScrollLines = 3;
5183 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5185 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5186 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5189 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
5190 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
5192 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
5193 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
5194 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
5200 /* IME messages to make the edit control IME aware */
5201 case WM_IME_SETCONTEXT:
5204 case WM_IME_STARTCOMPOSITION:
5205 es->composition_start = es->selection_end;
5206 es->composition_len = 0;
5209 case WM_IME_COMPOSITION:
5210 EDIT_ImeComposition(hwnd, lParam, es);
5213 case WM_IME_ENDCOMPOSITION:
5214 if (es->composition_len > 0)
5216 static const WCHAR empty_stringW[] = {0};
5217 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
5218 es->selection_end = es->selection_start;
5219 es->composition_len= 0;
5223 case WM_IME_COMPOSITIONFULL:
5229 case WM_IME_CONTROL:
5233 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5237 if (IsWindow(hwnd) && es) EDIT_UnlockBuffer(es, FALSE);
5239 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5245 /*********************************************************************
5246 * EditWndProc_wrapper16
5248 static LRESULT EditWndProc_wrapper16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
5250 static const UINT msg16_offset = EM_GETSEL16 - EM_GETSEL;
5256 case EM_SCROLLCARET16:
5257 case EM_GETMODIFY16:
5258 case EM_SETMODIFY16:
5259 case EM_GETLINECOUNT16:
5261 case EM_LINELENGTH16:
5262 case EM_LIMITTEXT16:
5266 case EM_LINEFROMCHAR16:
5267 case EM_SETPASSWORDCHAR16:
5268 case EM_EMPTYUNDOBUFFER16:
5269 case EM_SETREADONLY16:
5270 case EM_GETPASSWORDCHAR16:
5271 /* these messages missing from specs */
5276 msg -= msg16_offset;
5281 msg -= msg16_offset;
5283 case EM_REPLACESEL16:
5285 lParam = (LPARAM)MapSL(lParam);
5286 msg -= msg16_offset;
5288 case EM_LINESCROLL16:
5289 wParam = (INT)(SHORT)HIWORD(lParam);
5290 lParam = (INT)(SHORT)LOWORD(lParam);
5291 msg -= msg16_offset;
5293 case EM_LINEINDEX16:
5294 if ((INT16)wParam == -1) wParam = (WPARAM)-1;
5295 msg -= msg16_offset;
5298 if ((short)LOWORD(lParam) == -1)
5305 wParam = LOWORD(lParam);
5306 lParam = HIWORD(lParam);
5308 msg -= msg16_offset;
5314 RECT16 *r16 = MapSL(lParam);
5315 EditWndProc_common( hwnd, msg - msg16_offset, wParam, (LPARAM)&rect, FALSE );
5316 r16->left = rect.left;
5317 r16->top = rect.top;
5318 r16->right = rect.right;
5319 r16->bottom = rect.bottom;
5323 case EM_SETRECTNP16:
5327 RECT16 *r16 = MapSL(lParam);
5328 rect.left = r16->left;
5329 rect.top = r16->top;
5330 rect.right = r16->right;
5331 rect.bottom = r16->bottom;
5332 EditWndProc_common( hwnd, msg - msg16_offset, wParam, (LPARAM)&rect, FALSE );
5335 case EM_SETHANDLE16:
5336 EDIT_EM_SetHandle16( hwnd, (HLOCAL16)wParam );
5338 case EM_GETHANDLE16:
5339 result = EDIT_EM_GetHandle16( hwnd );
5341 case EM_SETTABSTOPS16:
5343 INT16 *tabs16 = MapSL(lParam);
5344 INT i, count = wParam, *tabs = NULL;
5347 if (!(tabs = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*tabs) ))) return 0;
5348 for (i = 0; i < count; i++) tabs[i] = tabs16[i];
5350 result = EditWndProc_common( hwnd, msg - msg16_offset, count, (LPARAM)tabs, FALSE );
5351 HeapFree( GetProcessHeap(), 0, tabs );
5354 case EM_GETFIRSTVISIBLELINE16:
5355 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & ES_MULTILINE)) return 0;
5356 msg -= msg16_offset;
5358 case EM_SETWORDBREAKPROC16:
5360 struct word_break_thunk *thunk = add_word_break_thunk( (EDITWORDBREAKPROC16)lParam );
5361 return EditWndProc_common( hwnd, EM_SETWORDBREAKPROC, wParam, (LPARAM)thunk, FALSE );
5363 case EM_GETWORDBREAKPROC16:
5364 result = EditWndProc_common( hwnd, EM_GETWORDBREAKPROC, wParam, lParam, FALSE );
5365 return (LRESULT)get_word_break_thunk( (EDITWORDBREAKPROCA)result );
5367 return EditWndProc_common( hwnd, msg, wParam, lParam, unicode );
5369 return EditWndProc_common( hwnd, msg, wParam, lParam, FALSE );
5372 /*********************************************************************
5374 * EditWndProc (USER32.@)
5376 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5378 return EditWndProc_wrapper16(hWnd, uMsg, wParam, lParam, FALSE);
5381 /*********************************************************************
5385 static LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5387 return EditWndProc_wrapper16(hWnd, uMsg, wParam, lParam, TRUE);
5390 /*********************************************************************
5391 * edit class descriptor
5393 static const WCHAR editW[] = {'E','d','i','t',0};
5394 const struct builtin_class_descr EDIT_builtin_class =
5397 CS_DBLCLKS | CS_PARENTDC, /* style */
5398 EditWndProcA, /* procA */
5399 EditWndProcW, /* procW */
5401 sizeof(EDITSTATE *) + sizeof(HLOCAL16), /* extra */
5403 sizeof(EDITSTATE *), /* extra */
5405 IDC_IBEAM, /* cursor */