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
56 #include "wine/unicode.h"
58 #include "user_private.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(edit);
62 WINE_DECLARE_DEBUG_CHANNEL(combo);
63 WINE_DECLARE_DEBUG_CHANNEL(relay);
65 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
66 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
67 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
68 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
71 * extra flags for EDITSTATE.flags field
73 #define EF_MODIFIED 0x0001 /* text has been modified */
74 #define EF_FOCUSED 0x0002 /* we have input focus */
75 #define EF_UPDATE 0x0004 /* notify parent of changed state */
76 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
77 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
79 wrapped line, instead of in front of the next character */
80 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
81 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
82 the text buffer if this is clear. */
83 #define EF_DIALOGMODE 0x0400 /* Indicates that we are inside a dialog window */
87 END_0 = 0, /* line ends with terminating '\0' character */
88 END_WRAP, /* line is wrapped */
89 END_HARD, /* line ends with a hard return '\r\n' */
90 END_SOFT, /* line ends with a soft return '\r\r\n' */
91 END_RICH /* line ends with a single '\n' */
94 typedef struct tagLINEDEF {
95 INT length; /* bruto length of a line in bytes */
96 INT net_length; /* netto length of a line in visible characters */
98 INT width; /* width of the line in pixels */
99 INT index; /* line index into the buffer */
100 struct tagLINEDEF *next;
105 BOOL is_unicode; /* how the control was created */
106 LPWSTR text; /* the actual contents of the control */
107 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
108 UINT buffer_size; /* the size of the buffer in characters */
109 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
110 HFONT font; /* NULL means standard system font */
111 INT x_offset; /* scroll offset for multi lines this is in pixels
112 for single lines it's in characters */
113 INT line_height; /* height of a screen line in pixels */
114 INT char_width; /* average character width in pixels */
115 DWORD style; /* sane version of wnd->dwStyle */
116 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
117 INT undo_insert_count; /* number of characters inserted in sequence */
118 UINT undo_position; /* character index of the insertion and deletion */
119 LPWSTR undo_text; /* deleted text */
120 UINT undo_buffer_size; /* size of the deleted text buffer */
121 INT selection_start; /* == selection_end if no selection */
122 INT selection_end; /* == current caret position */
123 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
124 INT left_margin; /* in pixels */
125 INT right_margin; /* in pixels */
127 INT text_width; /* width of the widest line in pixels for multi line controls
128 and just line width for single line controls */
129 INT region_posx; /* Position of cursor relative to region: */
130 INT region_posy; /* -1: to left, 0: within, 1: to right */
131 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
132 INT line_count; /* number of lines */
133 INT y_offset; /* scroll offset in number of lines */
134 BOOL bCaptureState; /* flag indicating whether mouse was captured */
135 BOOL bEnableState; /* flag keeping the enable state */
136 HWND hwndSelf; /* the our window handle */
137 HWND hwndParent; /* Handle of parent for sending EN_* messages.
138 Even if parent will change, EN_* messages
139 should be sent to the first parent. */
140 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
142 * only for multi line controls
144 INT lock_count; /* amount of re-entries in the EditWndProc */
147 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
148 HLOCAL hloc32W; /* our unicode local memory block */
149 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
154 UINT composition_len; /* length of composition, 0 == no composition */
155 int composition_start; /* the character position for the composition */
159 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
160 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
162 /* used for disabled or read-only edit control */
163 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
165 { /* Notify parent which has created this edit control */ \
166 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
167 SendMessageW(es->hwndParent, WM_COMMAND, \
168 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
169 (LPARAM)(es->hwndSelf)); \
173 /*********************************************************************
178 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
180 return (es->undo_insert_count || strlenW(es->undo_text));
184 /*********************************************************************
189 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
191 es->undo_insert_count = 0;
192 *es->undo_text = '\0';
196 /**********************************************************************
199 * Returns the window version in case Wine emulates a later version
200 * of windows than the application expects.
202 * In a number of cases when windows runs an application that was
203 * designed for an earlier windows version, windows reverts
204 * to "old" behaviour of that earlier version.
206 * An example is a disabled edit control that needs to be painted.
207 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
208 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
209 * applications with an expected version 0f 4.0 or higher.
212 static DWORD get_app_version(void)
214 static DWORD version;
217 DWORD dwEmulatedVersion;
219 DWORD dwProcVersion = GetProcessVersion(0);
221 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
222 GetVersionExW( &info );
223 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
224 /* FIXME: this may not be 100% correct; see discussion on the
225 * wine developer list in Nov 1999 */
226 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
231 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
236 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
237 msg = WM_CTLCOLORSTATIC;
239 msg = WM_CTLCOLOREDIT;
241 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
242 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
244 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
249 /*********************************************************************
253 * Find the beginning of words.
254 * Note: unlike the specs for a WordBreakProc, this function only
255 * allows to be called without linebreaks between s[0] up to
256 * s[count - 1]. Remember it is only called
257 * internally, so we can decide this for ourselves.
260 static INT EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
264 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
274 if (s[index] == ' ') {
275 while (index && (s[index] == ' '))
278 while (index && (s[index] != ' '))
284 while (index && (s[index] != ' '))
297 while ((index < count) && (s[index] == ' ')) index++;
299 while (s[index] && (s[index] != ' ') && (index < count))
301 while ((s[index] == ' ') && (index < count)) index++;
306 ret = (s[index] == ' ');
309 ERR("unknown action code, please report !\n");
316 /*********************************************************************
318 * EDIT_CallWordBreakProc
320 * Call appropriate WordBreakProc (internal or external).
322 * Note: The "start" argument should always be an index referring
323 * to es->text. The actual wordbreak proc might be
324 * 16 bit, so we can't always pass any 32 bit LPSTR.
325 * Hence we assume that es->text is the buffer that holds
326 * the string under examination (we can decide this for ourselves).
329 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
333 if (es->word_break_proc)
337 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
339 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
340 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
341 ret = wbpW(es->text + start, index, count, action);
345 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
349 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
350 textA = HeapAlloc(GetProcessHeap(), 0, countA);
351 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
352 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
353 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
354 ret = wbpA(textA, index, countA, action);
355 HeapFree(GetProcessHeap(), 0, textA);
359 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
364 /*********************************************************************
366 * EDIT_BuildLineDefs_ML
368 * Build linked list of text lines.
369 * Lines can end with '\0' (last line), a character (if it is wrapped),
370 * a soft return '\r\r\n' or a hard return '\r\n'
373 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
377 LPWSTR current_position, cp;
379 LINEDEF *current_line;
380 LINEDEF *previous_line;
382 INT line_index = 0, nstart_line = 0, nstart_index = 0;
383 INT line_count = es->line_count;
387 if (istart == iend && delta == 0)
390 dc = GetDC(es->hwndSelf);
392 old_font = SelectObject(dc, es->font);
394 previous_line = NULL;
395 current_line = es->first_line_def;
397 /* Find starting line. istart must lie inside an existing line or
398 * at the end of buffer */
400 if (istart < current_line->index + current_line->length ||
401 current_line->ending == END_0)
404 previous_line = current_line;
405 current_line = current_line->next;
407 } while (current_line);
409 if (!current_line) /* Error occurred start is not inside previous buffer */
411 FIXME(" modification occurred outside buffer\n");
412 ReleaseDC(es->hwndSelf, dc);
416 /* Remember start of modifications in order to calculate update region */
417 nstart_line = line_index;
418 nstart_index = current_line->index;
420 /* We must start to reformat from the previous line since the modifications
421 * may have caused the line to wrap upwards. */
422 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
425 current_line = previous_line;
427 start_line = current_line;
429 fw = es->format_rect.right - es->format_rect.left;
430 current_position = es->text + current_line->index;
432 if (current_line != start_line)
434 if (!current_line || current_line->index + delta > current_position - es->text)
436 /* The buffer has been expanded, create a new line and
437 insert it into the link list */
438 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
439 new_line->next = previous_line->next;
440 previous_line->next = new_line;
441 current_line = new_line;
444 else if (current_line->index + delta < current_position - es->text)
446 /* The previous line merged with this line so we delete this extra entry */
447 previous_line->next = current_line->next;
448 HeapFree(GetProcessHeap(), 0, current_line);
449 current_line = previous_line->next;
453 else /* current_line->index + delta == current_position */
455 if (current_position - es->text > iend)
456 break; /* We reached end of line modifications */
457 /* else recalculate this line */
461 current_line->index = current_position - es->text;
462 orig_net_length = current_line->net_length;
464 /* Find end of line */
465 cp = current_position;
467 if (*cp == '\n') break;
468 if ((*cp == '\r') && (*(cp + 1) == '\n'))
473 /* Mark type of line termination */
475 current_line->ending = END_0;
476 current_line->net_length = strlenW(current_position);
477 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
478 current_line->ending = END_SOFT;
479 current_line->net_length = cp - current_position - 1;
480 } else if (*cp == '\n') {
481 current_line->ending = END_RICH;
482 current_line->net_length = cp - current_position;
484 current_line->ending = END_HARD;
485 current_line->net_length = cp - current_position;
488 /* Calculate line width */
489 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
490 current_position, current_line->net_length,
491 es->tabs_count, es->tabs));
493 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
494 if (!(es->style & ES_AUTOHSCROLL)) {
495 if (current_line->width > fw) {
500 next = EDIT_CallWordBreakProc(es, current_position - es->text,
501 prev + 1, current_line->net_length, WB_RIGHT);
502 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
503 current_position, next, es->tabs_count, es->tabs));
504 } while (current_line->width <= fw);
505 if (!prev) { /* Didn't find a line break so force a break */
510 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
511 current_position, next, es->tabs_count, es->tabs));
512 } while (current_line->width <= fw);
517 /* If the first line we are calculating, wrapped before istart, we must
518 * adjust istart in order for this to be reflected in the update region. */
519 if (current_line->index == nstart_index && istart > current_line->index + prev)
520 istart = current_line->index + prev;
521 /* else if we are updating the previous line before the first line we
522 * are re-calculating and it expanded */
523 else if (current_line == start_line &&
524 current_line->index != nstart_index && orig_net_length < prev)
526 /* Line expanded due to an upwards line wrap so we must partially include
527 * previous line in update region */
528 nstart_line = line_index;
529 nstart_index = current_line->index;
530 istart = current_line->index + orig_net_length;
533 current_line->net_length = prev;
534 current_line->ending = END_WRAP;
535 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
536 current_line->net_length, es->tabs_count, es->tabs));
538 else if (current_line == start_line &&
539 current_line->index != nstart_index &&
540 orig_net_length < current_line->net_length) {
541 /* The previous line expanded but it's still not as wide as the client rect */
542 /* The expansion is due to an upwards line wrap so we must partially include
543 it in the update region */
544 nstart_line = line_index;
545 nstart_index = current_line->index;
546 istart = current_line->index + orig_net_length;
551 /* Adjust length to include line termination */
552 switch (current_line->ending) {
554 current_line->length = current_line->net_length + 3;
557 current_line->length = current_line->net_length + 1;
560 current_line->length = current_line->net_length + 2;
564 current_line->length = current_line->net_length;
567 es->text_width = max(es->text_width, current_line->width);
568 current_position += current_line->length;
569 previous_line = current_line;
570 current_line = current_line->next;
572 } while (previous_line->ending != END_0);
574 /* Finish adjusting line indexes by delta or remove hanging lines */
575 if (previous_line->ending == END_0)
577 LINEDEF *pnext = NULL;
579 previous_line->next = NULL;
582 pnext = current_line->next;
583 HeapFree(GetProcessHeap(), 0, current_line);
584 current_line = pnext;
592 current_line->index += delta;
593 current_line = current_line->next;
597 /* Calculate rest of modification rectangle */
602 * We calculate two rectangles. One for the first line which may have
603 * an indent with respect to the format rect. The other is a format-width
604 * rectangle that spans the rest of the lines that changed or moved.
606 rc.top = es->format_rect.top + nstart_line * es->line_height -
607 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
608 rc.bottom = rc.top + es->line_height;
609 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
610 rc.left = es->format_rect.left;
612 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
613 es->text + nstart_index, istart - nstart_index,
614 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
615 rc.right = es->format_rect.right;
616 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
619 rc.left = es->format_rect.left;
620 rc.right = es->format_rect.right;
622 * If lines were added or removed we must re-paint the remainder of the
623 * lines since the remaining lines were either shifted up or down.
625 if (line_count < es->line_count) /* We added lines */
626 rc.bottom = es->line_count * es->line_height;
627 else if (line_count > es->line_count) /* We removed lines */
628 rc.bottom = line_count * es->line_height;
630 rc.bottom = line_index * es->line_height;
631 rc.bottom += es->format_rect.top;
632 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
633 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
634 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
635 DeleteObject(tmphrgn);
639 SelectObject(dc, old_font);
641 ReleaseDC(es->hwndSelf, dc);
645 static inline UINT get_text_length(EDITSTATE *es)
647 if(es->text_length == (UINT)-1)
648 es->text_length = strlenW(es->text);
649 return es->text_length;
652 /*********************************************************************
654 * EDIT_GetPasswordPointer_SL
656 * note: caller should free the (optionally) allocated buffer
659 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
661 if (es->style & ES_PASSWORD) {
662 INT len = get_text_length(es);
663 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
665 while(len) text[--len] = es->password_char;
672 /*********************************************************************
674 * EDIT_CalcLineWidth_SL
677 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
684 text = EDIT_GetPasswordPointer_SL(es);
686 dc = GetDC(es->hwndSelf);
688 old_font = SelectObject(dc, es->font);
690 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
693 SelectObject(dc, old_font);
694 ReleaseDC(es->hwndSelf, dc);
696 if (es->style & ES_PASSWORD)
697 HeapFree(GetProcessHeap(), 0, text);
699 es->text_width = size.cx;
702 /*********************************************************************
706 * Beware: This is not the function called on EM_CHARFROMPOS
707 * The position _can_ be outside the formatting / client
709 * The return value is only the character index
712 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
717 INT x_high = 0, x_low = 0;
719 if (es->style & ES_MULTILINE) {
720 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
722 LINEDEF *line_def = es->first_line_def;
724 while ((line > 0) && line_def->next) {
725 line_index += line_def->length;
726 line_def = line_def->next;
729 x += es->x_offset - es->format_rect.left;
730 if (es->style & ES_RIGHT)
731 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
732 else if (es->style & ES_CENTER)
733 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
734 if (x >= line_def->width) {
736 *after_wrap = (line_def->ending == END_WRAP);
737 return line_index + line_def->net_length;
744 dc = GetDC(es->hwndSelf);
746 old_font = SelectObject(dc, es->font);
748 high = line_index + line_def->net_length + 1;
749 while (low < high - 1)
751 INT mid = (low + high) / 2;
752 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
761 if (abs(x_high - x) + 1 <= abs(x_low - x))
767 *after_wrap = ((index == line_index + line_def->net_length) &&
768 (line_def->ending == END_WRAP));
774 x -= es->format_rect.left;
780 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
781 if (es->style & ES_RIGHT)
783 else if (es->style & ES_CENTER)
787 text = EDIT_GetPasswordPointer_SL(es);
788 dc = GetDC(es->hwndSelf);
790 old_font = SelectObject(dc, es->font);
794 INT high = es->x_offset;
795 while (low < high - 1)
797 INT mid = (low + high) / 2;
798 GetTextExtentPoint32W( dc, text + mid,
799 es->x_offset - mid, &size );
808 if (abs(x_high + x) <= abs(x_low + x) + 1)
815 INT low = es->x_offset;
816 INT high = get_text_length(es) + 1;
817 while (low < high - 1)
819 INT mid = (low + high) / 2;
820 GetTextExtentPoint32W( dc, text + es->x_offset,
821 mid - es->x_offset, &size );
830 if (abs(x_high - x) <= abs(x_low - x) + 1)
835 if (es->style & ES_PASSWORD)
836 HeapFree(GetProcessHeap(), 0, text);
839 SelectObject(dc, old_font);
840 ReleaseDC(es->hwndSelf, dc);
845 /*********************************************************************
849 * adjusts the point to be within the formatting rectangle
850 * (so CharFromPos returns the nearest _visible_ character)
853 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
855 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
856 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
860 /*********************************************************************
865 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
870 if (!(es->style & ES_MULTILINE))
872 if (index > (INT)get_text_length(es))
873 return es->line_count - 1;
875 index = min(es->selection_start, es->selection_end);
878 line_def = es->first_line_def;
879 index -= line_def->length;
880 while ((index >= 0) && line_def->next) {
882 line_def = line_def->next;
883 index -= line_def->length;
889 /*********************************************************************
894 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
897 const LINEDEF *line_def;
899 if (!(es->style & ES_MULTILINE))
901 if (line >= es->line_count)
905 line_def = es->first_line_def;
907 INT index = es->selection_end - line_def->length;
908 while ((index >= 0) && line_def->next) {
909 line_index += line_def->length;
910 line_def = line_def->next;
911 index -= line_def->length;
915 line_index += line_def->length;
916 line_def = line_def->next;
924 /*********************************************************************
929 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
933 if (!(es->style & ES_MULTILINE))
934 return get_text_length(es);
937 /* get the number of remaining non-selected chars of selected lines */
938 INT32 l; /* line number */
939 INT32 li; /* index of first char in line */
941 l = EDIT_EM_LineFromChar(es, es->selection_start);
942 /* # chars before start of selection area */
943 count = es->selection_start - EDIT_EM_LineIndex(es, l);
944 l = EDIT_EM_LineFromChar(es, es->selection_end);
945 /* # chars after end of selection */
946 li = EDIT_EM_LineIndex(es, l);
947 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
950 line_def = es->first_line_def;
951 index -= line_def->length;
952 while ((index >= 0) && line_def->next) {
953 line_def = line_def->next;
954 index -= line_def->length;
956 return line_def->net_length;
960 /*********************************************************************
965 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
967 INT len = get_text_length(es);
980 index = min(index, len);
981 dc = GetDC(es->hwndSelf);
983 old_font = SelectObject(dc, es->font);
984 if (es->style & ES_MULTILINE) {
985 l = EDIT_EM_LineFromChar(es, index);
986 y = (l - es->y_offset) * es->line_height;
987 li = EDIT_EM_LineIndex(es, l);
988 if (after_wrap && (li == index) && l) {
990 line_def = es->first_line_def;
992 line_def = line_def->next;
995 if (line_def->ending == END_WRAP) {
997 y -= es->line_height;
998 li = EDIT_EM_LineIndex(es, l);
1002 line_def = es->first_line_def;
1003 while (line_def->index != li)
1004 line_def = line_def->next;
1006 ll = line_def->net_length;
1007 lw = line_def->width;
1009 w = es->format_rect.right - es->format_rect.left;
1010 if (es->style & ES_RIGHT)
1012 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
1013 es->tabs_count, es->tabs)) - es->x_offset;
1016 else if (es->style & ES_CENTER)
1018 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1019 es->tabs_count, es->tabs)) - es->x_offset;
1024 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1025 es->tabs_count, es->tabs)) - es->x_offset;
1028 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
1029 if (index < es->x_offset) {
1030 GetTextExtentPoint32W(dc, text + index,
1031 es->x_offset - index, &size);
1034 GetTextExtentPoint32W(dc, text + es->x_offset,
1035 index - es->x_offset, &size);
1038 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1040 w = es->format_rect.right - es->format_rect.left;
1041 if (w > es->text_width)
1043 if (es->style & ES_RIGHT)
1044 x += w - es->text_width;
1045 else if (es->style & ES_CENTER)
1046 x += (w - es->text_width) / 2;
1051 if (es->style & ES_PASSWORD)
1052 HeapFree(GetProcessHeap(), 0, text);
1054 x += es->format_rect.left;
1055 y += es->format_rect.top;
1057 SelectObject(dc, old_font);
1058 ReleaseDC(es->hwndSelf, dc);
1059 return MAKELONG((INT16)x, (INT16)y);
1063 /*********************************************************************
1067 * Calculates the bounding rectangle for a line from a starting
1068 * column to an ending column.
1071 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1073 INT line_index = EDIT_EM_LineIndex(es, line);
1075 if (es->style & ES_MULTILINE)
1076 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1078 rc->top = es->format_rect.top;
1079 rc->bottom = rc->top + es->line_height;
1080 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1081 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1085 static inline void text_buffer_changed(EDITSTATE *es)
1087 es->text_length = (UINT)-1;
1090 /*********************************************************************
1094 static void EDIT_LockBuffer(EDITSTATE *es)
1098 if(!es->hloc32W) return;
1102 CHAR *textA = LocalLock(es->hloc32A);
1104 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1105 if(countW_new > es->buffer_size + 1)
1107 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1108 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1109 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1112 es->hloc32W = hloc32W_new;
1113 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1114 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1117 WARN("FAILED! Will synchronize partially\n");
1119 es->text = LocalLock(es->hloc32W);
1120 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1121 LocalUnlock(es->hloc32A);
1123 else es->text = LocalLock(es->hloc32W);
1125 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1130 /*********************************************************************
1135 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1138 /* Edit window might be already destroyed */
1139 if(!IsWindow(es->hwndSelf))
1141 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1145 if (!es->lock_count) {
1146 ERR("lock_count == 0 ... please report\n");
1150 ERR("es->text == 0 ... please report\n");
1154 if (force || (es->lock_count == 1)) {
1157 UINT countW = get_text_length(es) + 1;
1161 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1162 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1163 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1164 countA = LocalSize(es->hloc32A);
1165 if(countA_new > countA)
1168 UINT alloc_size = ROUND_TO_GROW(countA_new);
1169 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1170 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1173 es->hloc32A = hloc32A_new;
1174 countA = LocalSize(hloc32A_new);
1175 TRACE("Real new size %d bytes\n", countA);
1178 WARN("FAILED! Will synchronize partially\n");
1180 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1181 LocalLock(es->hloc32A), countA, NULL, NULL);
1182 LocalUnlock(es->hloc32A);
1185 LocalUnlock(es->hloc32W);
1189 ERR("no buffer ... please report\n");
1197 /*********************************************************************
1201 * Try to fit size + 1 characters in the buffer.
1203 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1207 if (size <= es->buffer_size)
1210 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1212 /* Force edit to unlock it's buffer. es->text now NULL */
1213 EDIT_UnlockBuffer(es, TRUE);
1216 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1217 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1218 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1219 es->hloc32W = hNew32W;
1220 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1224 EDIT_LockBuffer(es);
1226 if (es->buffer_size < size) {
1227 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1228 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1231 TRACE("We now have %d+1\n", es->buffer_size);
1237 /*********************************************************************
1241 * Try to fit size + 1 bytes in the undo buffer.
1244 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1248 if (size <= es->undo_buffer_size)
1251 TRACE("trying to ReAlloc to %d+1\n", size);
1253 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1254 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1255 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1260 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1266 /*********************************************************************
1268 * EDIT_UpdateTextRegion
1271 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1273 if (es->flags & EF_UPDATE) {
1274 es->flags &= ~EF_UPDATE;
1275 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1277 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1281 /*********************************************************************
1286 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1288 if (es->flags & EF_UPDATE) {
1289 es->flags &= ~EF_UPDATE;
1290 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1292 InvalidateRect(es->hwndSelf, rc, bErase);
1295 /*********************************************************************
1297 * EDIT_SL_InvalidateText
1299 * Called from EDIT_InvalidateText().
1300 * Does the job for single-line controls only.
1303 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1308 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1309 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1310 EDIT_UpdateText(es, &rc, TRUE);
1314 static inline INT get_vertical_line_count(EDITSTATE *es)
1316 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1320 /*********************************************************************
1322 * EDIT_ML_InvalidateText
1324 * Called from EDIT_InvalidateText().
1325 * Does the job for multi-line controls only.
1328 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1330 INT vlc = get_vertical_line_count(es);
1331 INT sl = EDIT_EM_LineFromChar(es, start);
1332 INT el = EDIT_EM_LineFromChar(es, end);
1341 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1344 sc = start - EDIT_EM_LineIndex(es, sl);
1345 ec = end - EDIT_EM_LineIndex(es, el);
1346 if (sl < es->y_offset) {
1350 if (el > es->y_offset + vlc) {
1351 el = es->y_offset + vlc;
1352 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1354 GetClientRect(es->hwndSelf, &rc1);
1355 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1357 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1358 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1359 EDIT_UpdateText(es, &rcUpdate, TRUE);
1361 EDIT_GetLineRect(es, sl, sc,
1362 EDIT_EM_LineLength(es,
1363 EDIT_EM_LineIndex(es, sl)),
1365 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1366 EDIT_UpdateText(es, &rcUpdate, TRUE);
1367 for (l = sl + 1 ; l < el ; l++) {
1368 EDIT_GetLineRect(es, l, 0,
1369 EDIT_EM_LineLength(es,
1370 EDIT_EM_LineIndex(es, l)),
1372 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1373 EDIT_UpdateText(es, &rcUpdate, TRUE);
1375 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1376 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1377 EDIT_UpdateText(es, &rcUpdate, TRUE);
1382 /*********************************************************************
1384 * EDIT_InvalidateText
1386 * Invalidate the text from offset start up to, but not including,
1387 * offset end. Useful for (re)painting the selection.
1388 * Regions outside the linewidth are not invalidated.
1389 * end == -1 means end == TextLength.
1390 * start and end need not be ordered.
1393 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1399 end = get_text_length(es);
1407 if (es->style & ES_MULTILINE)
1408 EDIT_ML_InvalidateText(es, start, end);
1410 EDIT_SL_InvalidateText(es, start, end);
1414 /*********************************************************************
1418 * note: unlike the specs say: the order of start and end
1419 * _is_ preserved in Windows. (i.e. start can be > end)
1420 * In other words: this handler is OK
1423 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1425 UINT old_start = es->selection_start;
1426 UINT old_end = es->selection_end;
1427 UINT len = get_text_length(es);
1429 if (start == (UINT)-1) {
1430 start = es->selection_end;
1431 end = es->selection_end;
1433 start = min(start, len);
1434 end = min(end, len);
1436 es->selection_start = start;
1437 es->selection_end = end;
1439 es->flags |= EF_AFTER_WRAP;
1441 es->flags &= ~EF_AFTER_WRAP;
1442 /* Compute the necessary invalidation region. */
1443 /* Note that we don't need to invalidate regions which have
1444 * "never" been selected, or those which are "still" selected.
1445 * In fact, every time we hit a selection boundary, we can
1446 * *toggle* whether we need to invalidate. Thus we can optimize by
1447 * *sorting* the interval endpoints. Let's assume that we sort them
1449 * start <= end <= old_start <= old_end
1450 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1451 * in 5 comparisons; i.e. it is impossible to do better than the
1453 ORDER_UINT(end, old_end);
1454 ORDER_UINT(start, old_start);
1455 ORDER_UINT(old_start, old_end);
1456 ORDER_UINT(start, end);
1457 /* Note that at this point 'end' and 'old_start' are not in order, but
1458 * start is definitely the min. and old_end is definitely the max. */
1459 if (end != old_start)
1463 * ORDER_UINT32(end, old_start);
1464 * EDIT_InvalidateText(es, start, end);
1465 * EDIT_InvalidateText(es, old_start, old_end);
1466 * in place of the following if statement.
1467 * (That would complete the optimal five-comparison four-element sort.)
1469 if (old_start > end )
1471 EDIT_InvalidateText(es, start, end);
1472 EDIT_InvalidateText(es, old_start, old_end);
1476 EDIT_InvalidateText(es, start, old_start);
1477 EDIT_InvalidateText(es, end, old_end);
1480 else EDIT_InvalidateText(es, start, old_end);
1484 /*********************************************************************
1486 * EDIT_UpdateScrollInfo
1489 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1491 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1494 si.cbSize = sizeof(SCROLLINFO);
1495 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1497 si.nMax = es->line_count - 1;
1498 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1499 si.nPos = es->y_offset;
1500 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1501 si.nMin, si.nMax, si.nPage, si.nPos);
1502 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1505 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1508 si.cbSize = sizeof(SCROLLINFO);
1509 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1511 si.nMax = es->text_width - 1;
1512 si.nPage = es->format_rect.right - es->format_rect.left;
1513 si.nPos = es->x_offset;
1514 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1515 si.nMin, si.nMax, si.nPage, si.nPos);
1516 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1521 /*********************************************************************
1523 * EDIT_EM_LineScroll_internal
1525 * Version of EDIT_EM_LineScroll for internal use.
1526 * It doesn't refuse if ES_MULTILINE is set and assumes that
1527 * dx is in pixels, dy - in lines.
1530 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1533 INT x_offset_in_pixels;
1534 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1537 if (es->style & ES_MULTILINE)
1539 x_offset_in_pixels = es->x_offset;
1544 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1547 if (-dx > x_offset_in_pixels)
1548 dx = -x_offset_in_pixels;
1549 if (dx > es->text_width - x_offset_in_pixels)
1550 dx = es->text_width - x_offset_in_pixels;
1551 nyoff = max(0, es->y_offset + dy);
1552 if (nyoff >= es->line_count - lines_per_page)
1553 nyoff = max(0, es->line_count - lines_per_page);
1554 dy = (es->y_offset - nyoff) * es->line_height;
1559 es->y_offset = nyoff;
1560 if(es->style & ES_MULTILINE)
1563 es->x_offset += dx / es->char_width;
1565 GetClientRect(es->hwndSelf, &rc1);
1566 IntersectRect(&rc, &rc1, &es->format_rect);
1567 ScrollWindowEx(es->hwndSelf, -dx, dy,
1568 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1569 /* force scroll info update */
1570 EDIT_UpdateScrollInfo(es);
1572 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1573 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1574 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1575 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1579 /*********************************************************************
1583 * NOTE: dx is in average character widths, dy - in lines;
1586 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1588 if (!(es->style & ES_MULTILINE))
1591 dx *= es->char_width;
1592 return EDIT_EM_LineScroll_internal(es, dx, dy);
1596 /*********************************************************************
1601 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1605 if (!(es->style & ES_MULTILINE))
1606 return (LRESULT)FALSE;
1616 if (es->y_offset < es->line_count - 1)
1621 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1624 if (es->y_offset < es->line_count - 1)
1625 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1628 return (LRESULT)FALSE;
1631 INT vlc = get_vertical_line_count(es);
1632 /* check if we are going to move too far */
1633 if(es->y_offset + dy > es->line_count - vlc)
1634 dy = es->line_count - vlc - es->y_offset;
1636 /* Notification is done in EDIT_EM_LineScroll */
1638 EDIT_EM_LineScroll(es, 0, dy);
1640 return MAKELONG(dy, TRUE);
1644 /*********************************************************************
1649 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1652 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1653 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1654 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1658 /*********************************************************************
1663 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1665 if (es->style & ES_MULTILINE) {
1669 INT cw = es->char_width;
1674 l = EDIT_EM_LineFromChar(es, es->selection_end);
1675 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1676 vlc = get_vertical_line_count(es);
1677 if (l >= es->y_offset + vlc)
1678 dy = l - vlc + 1 - es->y_offset;
1679 if (l < es->y_offset)
1680 dy = l - es->y_offset;
1681 ww = es->format_rect.right - es->format_rect.left;
1682 if (x < es->format_rect.left)
1683 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1684 if (x > es->format_rect.right)
1685 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1686 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1688 /* check if we are going to move too far */
1689 if(es->x_offset + dx + ww > es->text_width)
1690 dx = es->text_width - ww - es->x_offset;
1691 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1692 EDIT_EM_LineScroll_internal(es, dx, dy);
1699 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1700 format_width = es->format_rect.right - es->format_rect.left;
1701 if (x < es->format_rect.left) {
1702 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1705 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1706 } while ((x < goal) && es->x_offset);
1707 /* FIXME: use ScrollWindow() somehow to improve performance */
1708 EDIT_UpdateText(es, NULL, TRUE);
1709 } else if (x > es->format_rect.right) {
1711 INT len = get_text_length(es);
1712 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1715 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1716 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1717 } while ((x > goal) && (x_last > es->format_rect.right));
1718 /* FIXME: use ScrollWindow() somehow to improve performance */
1719 EDIT_UpdateText(es, NULL, TRUE);
1723 if(es->flags & EF_FOCUSED)
1724 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1728 /*********************************************************************
1733 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1735 INT e = es->selection_end;
1739 if ((es->style & ES_MULTILINE) && e &&
1740 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1742 if (e && (es->text[e - 1] == '\r'))
1746 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1747 EDIT_EM_ScrollCaret(es);
1751 /*********************************************************************
1755 * Only for multi line controls
1756 * Move the caret one line down, on a column with the nearest
1757 * x coordinate on the screen (might be a different column).
1760 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1762 INT s = es->selection_start;
1763 INT e = es->selection_end;
1764 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1765 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1766 INT x = (short)LOWORD(pos);
1767 INT y = (short)HIWORD(pos);
1769 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1772 EDIT_EM_SetSel(es, s, e, after_wrap);
1773 EDIT_EM_ScrollCaret(es);
1777 /*********************************************************************
1782 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1784 BOOL after_wrap = FALSE;
1787 /* Pass a high value in x to make sure of receiving the end of the line */
1788 if (!ctrl && (es->style & ES_MULTILINE))
1789 e = EDIT_CharFromPos(es, 0x3fffffff,
1790 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1792 e = get_text_length(es);
1793 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1794 EDIT_EM_ScrollCaret(es);
1798 /*********************************************************************
1803 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1805 INT e = es->selection_end;
1809 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1810 if (es->text[e] == '\n')
1812 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1816 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1817 EDIT_EM_ScrollCaret(es);
1821 /*********************************************************************
1825 * Home key: move to beginning of line.
1828 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1832 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1833 if (!ctrl && (es->style & ES_MULTILINE))
1834 e = EDIT_CharFromPos(es, -es->x_offset,
1835 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1838 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1839 EDIT_EM_ScrollCaret(es);
1843 /*********************************************************************
1845 * EDIT_MovePageDown_ML
1847 * Only for multi line controls
1848 * Move the caret one page down, on a column with the nearest
1849 * x coordinate on the screen (might be a different column).
1852 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1854 INT s = es->selection_start;
1855 INT e = es->selection_end;
1856 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1857 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1858 INT x = (short)LOWORD(pos);
1859 INT y = (short)HIWORD(pos);
1861 e = EDIT_CharFromPos(es, x,
1862 y + (es->format_rect.bottom - es->format_rect.top),
1866 EDIT_EM_SetSel(es, s, e, after_wrap);
1867 EDIT_EM_ScrollCaret(es);
1871 /*********************************************************************
1873 * EDIT_MovePageUp_ML
1875 * Only for multi line controls
1876 * Move the caret one page up, on a column with the nearest
1877 * x coordinate on the screen (might be a different column).
1880 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1882 INT s = es->selection_start;
1883 INT e = es->selection_end;
1884 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1885 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1886 INT x = (short)LOWORD(pos);
1887 INT y = (short)HIWORD(pos);
1889 e = EDIT_CharFromPos(es, x,
1890 y - (es->format_rect.bottom - es->format_rect.top),
1894 EDIT_EM_SetSel(es, s, e, after_wrap);
1895 EDIT_EM_ScrollCaret(es);
1899 /*********************************************************************
1903 * Only for multi line controls
1904 * Move the caret one line up, on a column with the nearest
1905 * x coordinate on the screen (might be a different column).
1908 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1910 INT s = es->selection_start;
1911 INT e = es->selection_end;
1912 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1913 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1914 INT x = (short)LOWORD(pos);
1915 INT y = (short)HIWORD(pos);
1917 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1920 EDIT_EM_SetSel(es, s, e, after_wrap);
1921 EDIT_EM_ScrollCaret(es);
1925 /*********************************************************************
1927 * EDIT_MoveWordBackward
1930 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1932 INT s = es->selection_start;
1933 INT e = es->selection_end;
1938 l = EDIT_EM_LineFromChar(es, e);
1939 ll = EDIT_EM_LineLength(es, e);
1940 li = EDIT_EM_LineIndex(es, l);
1943 li = EDIT_EM_LineIndex(es, l - 1);
1944 e = li + EDIT_EM_LineLength(es, li);
1947 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
1951 EDIT_EM_SetSel(es, s, e, FALSE);
1952 EDIT_EM_ScrollCaret(es);
1956 /*********************************************************************
1958 * EDIT_MoveWordForward
1961 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
1963 INT s = es->selection_start;
1964 INT e = es->selection_end;
1969 l = EDIT_EM_LineFromChar(es, e);
1970 ll = EDIT_EM_LineLength(es, e);
1971 li = EDIT_EM_LineIndex(es, l);
1973 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1974 e = EDIT_EM_LineIndex(es, l + 1);
1976 e = li + EDIT_CallWordBreakProc(es,
1977 li, e - li + 1, ll, WB_RIGHT);
1981 EDIT_EM_SetSel(es, s, e, FALSE);
1982 EDIT_EM_ScrollCaret(es);
1986 /*********************************************************************
1991 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
1995 LOGFONTW underline_font;
1996 HFONT hUnderline = 0;
2005 BkMode = GetBkMode(dc);
2006 BkColor = GetBkColor(dc);
2007 TextColor = GetTextColor(dc);
2009 if (es->composition_len == 0)
2011 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2012 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2013 SetBkMode( dc, OPAQUE);
2017 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2018 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2019 underline_font.lfUnderline = TRUE;
2020 hUnderline = CreateFontIndirectW(&underline_font);
2021 old_font = SelectObject(dc,hUnderline);
2024 li = EDIT_EM_LineIndex(es, line);
2025 if (es->style & ES_MULTILINE) {
2026 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2027 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2029 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2030 TextOutW(dc, x, y, text + li + col, count);
2031 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2033 if (es->style & ES_PASSWORD)
2034 HeapFree(GetProcessHeap(), 0, text);
2037 if (es->composition_len == 0)
2039 SetBkColor(dc, BkColor);
2040 SetTextColor(dc, TextColor);
2041 SetBkMode( dc, BkMode);
2046 SelectObject(dc,old_font);
2048 DeleteObject(hUnderline);
2055 /*********************************************************************
2060 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2062 INT s = es->selection_start;
2063 INT e = es->selection_end;
2070 if (es->style & ES_MULTILINE) {
2071 INT vlc = get_vertical_line_count(es);
2073 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2078 TRACE("line=%d\n", line);
2080 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2081 x = (short)LOWORD(pos);
2082 y = (short)HIWORD(pos);
2083 li = EDIT_EM_LineIndex(es, line);
2084 ll = EDIT_EM_LineLength(es, li);
2085 s = min(es->selection_start, es->selection_end);
2086 e = max(es->selection_start, es->selection_end);
2087 s = min(li + ll, max(li, s));
2088 e = min(li + ll, max(li, e));
2089 if (rev && (s != e) &&
2090 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2091 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2092 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2093 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2095 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2099 /*********************************************************************
2101 * EDIT_AdjustFormatRect
2103 * Adjusts the format rectangle for the current font and the
2104 * current client rectangle.
2107 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2111 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2112 if (es->style & ES_MULTILINE)
2114 INT fw, vlc, max_x_offset, max_y_offset;
2116 vlc = get_vertical_line_count(es);
2117 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2119 /* correct es->x_offset */
2120 fw = es->format_rect.right - es->format_rect.left;
2121 max_x_offset = es->text_width - fw;
2122 if(max_x_offset < 0) max_x_offset = 0;
2123 if(es->x_offset > max_x_offset)
2124 es->x_offset = max_x_offset;
2126 /* correct es->y_offset */
2127 max_y_offset = es->line_count - vlc;
2128 if(max_y_offset < 0) max_y_offset = 0;
2129 if(es->y_offset > max_y_offset)
2130 es->y_offset = max_y_offset;
2132 /* force scroll info update */
2133 EDIT_UpdateScrollInfo(es);
2136 /* Windows doesn't care to fix text placement for SL controls */
2137 es->format_rect.bottom = es->format_rect.top + es->line_height;
2139 /* Always stay within the client area */
2140 GetClientRect(es->hwndSelf, &ClientRect);
2141 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2143 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2144 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2146 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2150 /*********************************************************************
2154 * note: this is not (exactly) the handler called on EM_SETRECTNP
2155 * it is also used to set the rect of a single line control
2158 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2162 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2164 CopyRect(&es->format_rect, rc);
2166 if (ExStyle & WS_EX_CLIENTEDGE) {
2167 es->format_rect.left++;
2168 es->format_rect.right--;
2170 if (es->format_rect.bottom - es->format_rect.top
2171 >= es->line_height + 2)
2173 es->format_rect.top++;
2174 es->format_rect.bottom--;
2177 else if (es->style & WS_BORDER) {
2178 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2179 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2180 es->format_rect.left += bw;
2181 es->format_rect.right -= bw;
2182 if (es->format_rect.bottom - es->format_rect.top
2183 >= es->line_height + 2 * bh)
2185 es->format_rect.top += bh;
2186 es->format_rect.bottom -= bh;
2190 es->format_rect.left += es->left_margin;
2191 es->format_rect.right -= es->right_margin;
2192 EDIT_AdjustFormatRect(es);
2196 /*********************************************************************
2200 * returns line number (not index) in high-order word of result.
2201 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2202 * to Richedit, not to the edit control. Original documentation is valid.
2203 * FIXME: do the specs mean to return -1 if outside client area or
2204 * if outside formatting rectangle ???
2207 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2215 GetClientRect(es->hwndSelf, &rc);
2216 if (!PtInRect(&rc, pt))
2219 index = EDIT_CharFromPos(es, x, y, NULL);
2220 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2224 /*********************************************************************
2228 * Enable or disable soft breaks.
2230 * This means: insert or remove the soft linebreak character (\r\r\n).
2231 * Take care to check if the text still fits the buffer after insertion.
2232 * If not, notify with EN_ERRSPACE.
2235 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2237 es->flags &= ~EF_USE_SOFTBRK;
2239 es->flags |= EF_USE_SOFTBRK;
2240 FIXME("soft break enabled, not implemented\n");
2246 /*********************************************************************
2250 * Hopefully this won't fire back at us.
2251 * We always start with a fixed buffer in the local heap.
2252 * Despite of the documentation says that the local heap is used
2253 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2254 * buffer on the local heap.
2257 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2261 if (!(es->style & ES_MULTILINE))
2265 hLocal = es->hloc32W;
2271 UINT countA, alloc_size;
2272 TRACE("Allocating 32-bit ANSI alias buffer\n");
2273 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2274 alloc_size = ROUND_TO_GROW(countA);
2275 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2277 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2280 textA = LocalLock(es->hloc32A);
2281 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2282 LocalUnlock(es->hloc32A);
2284 hLocal = es->hloc32A;
2287 es->flags |= EF_APP_HAS_HANDLE;
2288 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2293 /*********************************************************************
2298 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2301 INT line_len, dst_len;
2304 if (es->style & ES_MULTILINE) {
2305 if (line >= es->line_count)
2309 i = EDIT_EM_LineIndex(es, line);
2311 line_len = EDIT_EM_LineLength(es, i);
2312 dst_len = *(WORD *)dst;
2315 if(dst_len <= line_len)
2317 memcpy(dst, src, dst_len * sizeof(WCHAR));
2320 else /* Append 0 if enough space */
2322 memcpy(dst, src, line_len * sizeof(WCHAR));
2329 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2330 if(!ret && line_len) /* Insufficient buffer size */
2332 if(ret < dst_len) /* Append 0 if enough space */
2333 ((LPSTR)dst)[ret] = 0;
2339 /*********************************************************************
2344 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2346 UINT s = es->selection_start;
2347 UINT e = es->selection_end;
2354 return MAKELONG(s, e);
2358 /*********************************************************************
2362 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2365 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2367 UINT strl = strlenW(lpsz_replace);
2368 UINT tl = get_text_length(es);
2379 TRACE("%s, can_undo %d, send_update %d\n",
2380 debugstr_w(lpsz_replace), can_undo, send_update);
2382 s = es->selection_start;
2383 e = es->selection_end;
2385 if ((s == e) && !strl)
2390 size = tl - (e - s) + strl;
2394 /* Issue the EN_MAXTEXT notification and continue with replacing text
2395 * such that buffer limit is honored. */
2396 if ((honor_limit) && (size > es->buffer_limit)) {
2397 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2398 /* Buffer limit can be smaller than the actual length of text in combobox */
2399 if (es->buffer_limit < (tl - (e-s)))
2402 strl = es->buffer_limit - (tl - (e-s));
2405 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2409 /* there is something to be deleted */
2410 TRACE("deleting stuff.\n");
2412 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2414 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2415 buf[bufl] = 0; /* ensure 0 termination */
2417 strcpyW(es->text + s, es->text + e);
2418 text_buffer_changed(es);
2421 /* there is an insertion */
2422 tl = get_text_length(es);
2423 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));
2424 for (p = es->text + tl ; p >= es->text + s ; p--)
2426 for (i = 0 , p = es->text + s ; i < strl ; i++)
2427 p[i] = lpsz_replace[i];
2428 if(es->style & ES_UPPERCASE)
2429 CharUpperBuffW(p, strl);
2430 else if(es->style & ES_LOWERCASE)
2431 CharLowerBuffW(p, strl);
2432 text_buffer_changed(es);
2434 if (es->style & ES_MULTILINE)
2436 INT st = min(es->selection_start, es->selection_end);
2437 INT vlc = get_vertical_line_count(es);
2439 hrgn = CreateRectRgn(0, 0, 0, 0);
2440 EDIT_BuildLineDefs_ML(es, st, st + strl,
2441 strl - abs(es->selection_end - es->selection_start), hrgn);
2442 /* if text is too long undo all changes */
2443 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2445 strcpyW(es->text + e, es->text + e + strl);
2447 for (i = 0 , p = es->text ; i < e - s ; i++)
2449 text_buffer_changed(es);
2450 EDIT_BuildLineDefs_ML(es, s, e,
2451 abs(es->selection_end - es->selection_start) - strl, hrgn);
2454 hrgn = CreateRectRgn(0, 0, 0, 0);
2455 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2459 INT fw = es->format_rect.right - es->format_rect.left;
2460 EDIT_CalcLineWidth_SL(es);
2461 /* remove chars that don't fit */
2462 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2463 while ((es->text_width > fw) && s + strl >= s) {
2464 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2466 EDIT_CalcLineWidth_SL(es);
2468 text_buffer_changed(es);
2469 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2475 utl = strlenW(es->undo_text);
2476 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2477 /* undo-buffer is extended to the right */
2478 EDIT_MakeUndoFit(es, utl + e - s);
2479 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2480 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2481 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2482 /* undo-buffer is extended to the left */
2483 EDIT_MakeUndoFit(es, utl + e - s);
2484 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2486 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2488 es->undo_position = s;
2490 /* new undo-buffer */
2491 EDIT_MakeUndoFit(es, e - s);
2492 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2493 es->undo_text[e - s] = 0; /* ensure 0 termination */
2494 es->undo_position = s;
2496 /* any deletion makes the old insertion-undo invalid */
2497 es->undo_insert_count = 0;
2499 EDIT_EM_EmptyUndoBuffer(es);
2503 if ((s == es->undo_position) ||
2504 ((es->undo_insert_count) &&
2505 (s == es->undo_position + es->undo_insert_count)))
2507 * insertion is new and at delete position or
2508 * an extension to either left or right
2510 es->undo_insert_count += strl;
2512 /* new insertion undo */
2513 es->undo_position = s;
2514 es->undo_insert_count = strl;
2515 /* new insertion makes old delete-buffer invalid */
2516 *es->undo_text = '\0';
2519 EDIT_EM_EmptyUndoBuffer(es);
2523 HeapFree(GetProcessHeap(), 0, buf);
2527 /* If text has been deleted and we're right or center aligned then scroll rightward */
2528 if (es->style & (ES_RIGHT | ES_CENTER))
2530 INT delta = strl - abs(es->selection_end - es->selection_start);
2532 if (delta < 0 && es->x_offset)
2534 if (abs(delta) > es->x_offset)
2537 es->x_offset += delta;
2541 EDIT_EM_SetSel(es, s, s, FALSE);
2542 es->flags |= EF_MODIFIED;
2543 if (send_update) es->flags |= EF_UPDATE;
2546 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2550 EDIT_UpdateText(es, NULL, TRUE);
2552 EDIT_EM_ScrollCaret(es);
2554 /* force scroll info update */
2555 EDIT_UpdateScrollInfo(es);
2558 if(send_update || (es->flags & EF_UPDATE))
2560 es->flags &= ~EF_UPDATE;
2561 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2566 /*********************************************************************
2570 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2573 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2575 if (!(es->style & ES_MULTILINE))
2579 WARN("called with NULL handle\n");
2583 EDIT_UnlockBuffer(es, TRUE);
2589 LocalFree(es->hloc32A);
2601 countA = LocalSize(hloc);
2602 textA = LocalLock(hloc);
2603 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2604 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2606 ERR("Could not allocate new unicode buffer\n");
2609 textW = LocalLock(hloc32W_new);
2610 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2611 LocalUnlock(hloc32W_new);
2615 LocalFree(es->hloc32W);
2617 es->hloc32W = hloc32W_new;
2621 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2623 es->flags |= EF_APP_HAS_HANDLE;
2624 EDIT_LockBuffer(es);
2626 es->x_offset = es->y_offset = 0;
2627 es->selection_start = es->selection_end = 0;
2628 EDIT_EM_EmptyUndoBuffer(es);
2629 es->flags &= ~EF_MODIFIED;
2630 es->flags &= ~EF_UPDATE;
2631 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2632 EDIT_UpdateText(es, NULL, TRUE);
2633 EDIT_EM_ScrollCaret(es);
2634 /* force scroll info update */
2635 EDIT_UpdateScrollInfo(es);
2639 /*********************************************************************
2643 * NOTE: this version currently implements WinNT limits
2646 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2648 if (!limit) limit = ~0u;
2649 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2650 es->buffer_limit = limit;
2654 /*********************************************************************
2658 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2659 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2660 * margin according to the textmetrics of the current font.
2662 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2663 * of the char's width as the margin, but this is not how Windows handles this.
2664 * For all other fonts Windows sets the margins to zero.
2666 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2667 * edit control is equal to or larger than a certain size.
2668 * Interestingly if one subtracts both the left and right margins from
2669 * this size one always seems to get an even number. The extents of
2670 * the (four character) string "'**'" match this quite closely, so
2671 * we'll use this until we come up with a better idea.
2673 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2675 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2678 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2679 return sz.cx + left + right;
2682 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2683 WORD left, WORD right, BOOL repaint)
2686 INT default_left_margin = 0; /* in pixels */
2687 INT default_right_margin = 0; /* in pixels */
2689 /* Set the default margins depending on the font */
2690 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2691 HDC dc = GetDC(es->hwndSelf);
2692 HFONT old_font = SelectObject(dc, es->font);
2693 GetTextMetricsW(dc, &tm);
2694 /* The default margins are only non zero for TrueType or Vector fonts */
2695 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2698 /* This must be calculated more exactly! But how? */
2699 default_left_margin = tm.tmAveCharWidth / 2;
2700 default_right_margin = tm.tmAveCharWidth / 2;
2701 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2702 GetClientRect(es->hwndSelf, &rc);
2703 if(rc.right - rc.left < min_size) {
2704 default_left_margin = es->left_margin;
2705 default_right_margin = es->right_margin;
2708 SelectObject(dc, old_font);
2709 ReleaseDC(es->hwndSelf, dc);
2712 if (action & EC_LEFTMARGIN) {
2713 es->format_rect.left -= es->left_margin;
2714 if (left != EC_USEFONTINFO)
2715 es->left_margin = left;
2717 es->left_margin = default_left_margin;
2718 es->format_rect.left += es->left_margin;
2721 if (action & EC_RIGHTMARGIN) {
2722 es->format_rect.right += es->right_margin;
2723 if (right != EC_USEFONTINFO)
2724 es->right_margin = right;
2726 es->right_margin = default_right_margin;
2727 es->format_rect.right -= es->right_margin;
2730 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2731 EDIT_AdjustFormatRect(es);
2732 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2735 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2739 /*********************************************************************
2741 * EM_SETPASSWORDCHAR
2744 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2748 if (es->style & ES_MULTILINE)
2751 if (es->password_char == c)
2754 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2755 es->password_char = c;
2757 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2758 es->style |= ES_PASSWORD;
2760 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2761 es->style &= ~ES_PASSWORD;
2763 EDIT_UpdateText(es, NULL, TRUE);
2767 /*********************************************************************
2772 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2774 if (!(es->style & ES_MULTILINE))
2776 HeapFree(GetProcessHeap(), 0, es->tabs);
2777 es->tabs_count = count;
2781 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2782 memcpy(es->tabs, tabs, count * sizeof(INT));
2788 /*********************************************************************
2790 * EM_SETWORDBREAKPROC
2793 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
2795 if (es->word_break_proc == wbp)
2798 es->word_break_proc = wbp;
2800 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2801 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2802 EDIT_UpdateText(es, NULL, TRUE);
2807 /*********************************************************************
2812 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2817 /* As per MSDN spec, for a single-line edit control,
2818 the return value is always TRUE */
2819 if( es->style & ES_READONLY )
2820 return !(es->style & ES_MULTILINE);
2822 ulength = strlenW(es->undo_text);
2824 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
2826 strcpyW(utext, es->undo_text);
2828 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2829 es->undo_insert_count, debugstr_w(utext));
2831 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2832 EDIT_EM_EmptyUndoBuffer(es);
2833 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
2834 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2835 /* send the notification after the selection start and end are set */
2836 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2837 EDIT_EM_ScrollCaret(es);
2838 HeapFree(GetProcessHeap(), 0, utext);
2840 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2841 es->undo_insert_count, debugstr_w(es->undo_text));
2846 /* Helper function for WM_CHAR
2848 * According to an MSDN blog article titled "Just because you're a control
2849 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2850 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2851 * a dialog box or not.
2853 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2855 return (es->flags & EF_DIALOGMODE);
2859 /*********************************************************************
2864 static void EDIT_WM_Paste(EDITSTATE *es)
2869 /* Protect read-only edit control from modification */
2870 if(es->style & ES_READONLY)
2873 OpenClipboard(es->hwndSelf);
2874 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2875 src = GlobalLock(hsrc);
2876 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
2879 else if (es->style & ES_PASSWORD) {
2880 /* clear selected text in password edit box even with empty clipboard */
2881 const WCHAR empty_strW[] = { 0 };
2882 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
2888 /*********************************************************************
2893 static void EDIT_WM_Copy(EDITSTATE *es)
2895 INT s = min(es->selection_start, es->selection_end);
2896 INT e = max(es->selection_start, es->selection_end);
2904 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2905 dst = GlobalLock(hdst);
2906 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2907 dst[len] = 0; /* ensure 0 termination */
2908 TRACE("%s\n", debugstr_w(dst));
2910 OpenClipboard(es->hwndSelf);
2912 SetClipboardData(CF_UNICODETEXT, hdst);
2917 /*********************************************************************
2922 static inline void EDIT_WM_Clear(EDITSTATE *es)
2924 static const WCHAR empty_stringW[] = {0};
2926 /* Protect read-only edit control from modification */
2927 if(es->style & ES_READONLY)
2930 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2934 /*********************************************************************
2939 static inline void EDIT_WM_Cut(EDITSTATE *es)
2946 /*********************************************************************
2951 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
2955 control = GetKeyState(VK_CONTROL) & 0x8000;
2959 /* If it's not a multiline edit box, it would be ignored below.
2960 * For multiline edit without ES_WANTRETURN, we have to make a
2963 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
2964 if (EDIT_IsInsideDialog(es))
2967 if (es->style & ES_MULTILINE) {
2968 if (es->style & ES_READONLY) {
2969 EDIT_MoveHome(es, FALSE, FALSE);
2970 EDIT_MoveDown_ML(es, FALSE);
2972 static const WCHAR cr_lfW[] = {'\r','\n',0};
2973 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
2978 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2980 static const WCHAR tabW[] = {'\t',0};
2981 if (EDIT_IsInsideDialog(es))
2983 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
2987 if (!(es->style & ES_READONLY) && !control) {
2988 if (es->selection_start != es->selection_end)
2991 /* delete character left of caret */
2992 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
2993 EDIT_MoveBackward(es, TRUE);
2999 if (!(es->style & ES_PASSWORD))
3000 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3003 if (!(es->style & ES_READONLY))
3004 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3007 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3008 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3011 if (!(es->style & ES_READONLY))
3012 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3016 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3017 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3020 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3024 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3032 /*********************************************************************
3037 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3039 if (code || control)
3044 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3047 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3050 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3053 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3056 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3059 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3060 EDIT_EM_ScrollCaret(es);
3063 ERR("unknown menu item, please report\n");
3069 /*********************************************************************
3073 * Note: the resource files resource/sysres_??.rc cannot define a
3074 * single popup menu. Hence we use a (dummy) menubar
3075 * containing the single popup menu as its first item.
3077 * FIXME: the message identifiers have been chosen arbitrarily,
3078 * hence we use MF_BYPOSITION.
3079 * We might as well use the "real" values (anybody knows ?)
3080 * The menu definition is in resources/sysres_??.rc.
3081 * Once these are OK, we better use MF_BYCOMMAND here
3082 * (as we do in EDIT_WM_Command()).
3085 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3087 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3088 HMENU popup = GetSubMenu(menu, 0);
3089 UINT start = es->selection_start;
3090 UINT end = es->selection_end;
3092 ORDER_UINT(start, end);
3095 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3097 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3099 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3101 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3103 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3105 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3107 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3110 /* Windows places the menu at the edit's center in this case */
3111 GetClientRect(es->hwndSelf, &rc);
3112 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3113 x = rc.left + (rc.right - rc.left) / 2;
3114 y = rc.top + (rc.bottom - rc.top) / 2;
3117 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3122 /*********************************************************************
3127 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3129 if(!count) return 0;
3133 lstrcpynW(dst, es->text, count);
3134 return strlenW(dst);
3138 LPSTR textA = (LPSTR)dst;
3139 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3140 textA[count - 1] = 0; /* ensure 0 termination */
3141 return strlen(textA);
3145 /*********************************************************************
3150 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3152 HWND hLBox = es->hwndListBox;
3160 hCombo = GetParent(es->hwndSelf);
3164 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3166 if (key == VK_UP || key == VK_DOWN)
3168 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3171 if (msg == WM_KEYDOWN || nEUI)
3172 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3178 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3180 /* make sure ComboLBox pops up */
3181 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3186 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3189 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3191 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3193 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3198 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3204 /*********************************************************************
3208 * Handling of special keys that don't produce a WM_CHAR
3209 * (i.e. non-printable keys) & Backspace & Delete
3212 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3217 if (GetKeyState(VK_MENU) & 0x8000)
3220 shift = GetKeyState(VK_SHIFT) & 0x8000;
3221 control = GetKeyState(VK_CONTROL) & 0x8000;
3226 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3231 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3232 EDIT_MoveUp_ML(es, shift);
3235 EDIT_MoveWordBackward(es, shift);
3237 EDIT_MoveBackward(es, shift);
3240 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3244 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3245 EDIT_MoveDown_ML(es, shift);
3247 EDIT_MoveWordForward(es, shift);
3249 EDIT_MoveForward(es, shift);
3252 EDIT_MoveHome(es, shift, control);
3255 EDIT_MoveEnd(es, shift, control);
3258 if (es->style & ES_MULTILINE)
3259 EDIT_MovePageUp_ML(es, shift);
3261 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3264 if (es->style & ES_MULTILINE)
3265 EDIT_MovePageDown_ML(es, shift);
3267 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3270 if (!(es->style & ES_READONLY) && !(shift && control)) {
3271 if (es->selection_start != es->selection_end) {
3278 /* delete character left of caret */
3279 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3280 EDIT_MoveBackward(es, TRUE);
3282 } else if (control) {
3283 /* delete to end of line */
3284 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3285 EDIT_MoveEnd(es, TRUE, FALSE);
3288 /* delete character right of caret */
3289 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3290 EDIT_MoveForward(es, TRUE);
3298 if (!(es->style & ES_READONLY))
3304 /* If the edit doesn't want the return send a message to the default object */
3305 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3309 if (!EDIT_IsInsideDialog(es)) break;
3311 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3312 if (HIWORD(dw) == DC_HASDEFID)
3314 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3317 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3318 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3324 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3325 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3328 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3329 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3336 /*********************************************************************
3341 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3343 es->flags &= ~EF_FOCUSED;
3345 if(!(es->style & ES_NOHIDESEL))
3346 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3347 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3352 /*********************************************************************
3356 * The caret position has been set on the WM_LBUTTONDOWN message
3359 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3362 INT e = es->selection_end;
3367 es->bCaptureState = TRUE;
3368 SetCapture(es->hwndSelf);
3370 l = EDIT_EM_LineFromChar(es, e);
3371 li = EDIT_EM_LineIndex(es, l);
3372 ll = EDIT_EM_LineLength(es, e);
3373 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3374 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3375 EDIT_EM_SetSel(es, s, e, FALSE);
3376 EDIT_EM_ScrollCaret(es);
3377 es->region_posx = es->region_posy = 0;
3378 SetTimer(es->hwndSelf, 0, 100, NULL);
3383 /*********************************************************************
3388 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3393 es->bCaptureState = TRUE;
3394 SetCapture(es->hwndSelf);
3395 EDIT_ConfinePoint(es, &x, &y);
3396 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3397 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3398 EDIT_EM_ScrollCaret(es);
3399 es->region_posx = es->region_posy = 0;
3400 SetTimer(es->hwndSelf, 0, 100, NULL);
3402 if (!(es->flags & EF_FOCUSED))
3403 SetFocus(es->hwndSelf);
3409 /*********************************************************************
3414 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3416 if (es->bCaptureState) {
3417 KillTimer(es->hwndSelf, 0);
3418 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3420 es->bCaptureState = FALSE;
3425 /*********************************************************************
3430 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3432 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3437 /*********************************************************************
3442 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3448 /* If the mouse has been captured by process other than the edit control itself,
3449 * the windows edit controls will not select the strings with mouse move.
3451 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3455 * FIXME: gotta do some scrolling if outside client
3456 * area. Maybe reset the timer ?
3459 EDIT_ConfinePoint(es, &x, &y);
3460 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3461 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3462 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3463 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3464 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3469 /*********************************************************************
3474 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3487 BOOL rev = es->bEnableState &&
3488 ((es->flags & EF_FOCUSED) ||
3489 (es->style & ES_NOHIDESEL));
3490 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3492 GetClientRect(es->hwndSelf, &rcClient);
3494 /* get the background brush */
3495 brush = EDIT_NotifyCtlColor(es, dc);
3497 /* paint the border and the background */
3498 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3500 if(es->style & WS_BORDER) {
3501 bw = GetSystemMetrics(SM_CXBORDER);
3502 bh = GetSystemMetrics(SM_CYBORDER);
3504 if(es->style & ES_MULTILINE) {
3505 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3506 if(es->style & WS_VSCROLL) rc.right+=bw;
3509 /* Draw the frame. Same code as in nonclient.c */
3510 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3511 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3512 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3513 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3514 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3515 SelectObject(dc, old_brush);
3517 /* Keep the border clean */
3518 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3519 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3522 GetClipBox(dc, &rc);
3523 FillRect(dc, &rc, brush);
3525 IntersectClipRect(dc, es->format_rect.left,
3526 es->format_rect.top,
3527 es->format_rect.right,
3528 es->format_rect.bottom);
3529 if (es->style & ES_MULTILINE) {
3531 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3534 old_font = SelectObject(dc, es->font);
3536 if (!es->bEnableState)
3537 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3538 GetClipBox(dc, &rcRgn);
3539 if (es->style & ES_MULTILINE) {
3540 INT vlc = get_vertical_line_count(es);
3541 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3542 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3543 if (IntersectRect(&rc, &rcRgn, &rcLine))
3544 EDIT_PaintLine(es, dc, i, rev);
3547 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3548 if (IntersectRect(&rc, &rcRgn, &rcLine))
3549 EDIT_PaintLine(es, dc, 0, rev);
3552 SelectObject(dc, old_font);
3555 EndPaint(es->hwndSelf, &ps);
3559 /*********************************************************************
3564 static void EDIT_WM_SetFocus(EDITSTATE *es)
3566 es->flags |= EF_FOCUSED;
3568 if (!(es->style & ES_NOHIDESEL))
3569 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3571 /* single line edit updates itself */
3572 if (!(es->style & ES_MULTILINE))
3574 HDC hdc = GetDC(es->hwndSelf);
3575 EDIT_WM_Paint(es, hdc);
3576 ReleaseDC(es->hwndSelf, hdc);
3579 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3580 EDIT_SetCaretPos(es, es->selection_end,
3581 es->flags & EF_AFTER_WRAP);
3582 ShowCaret(es->hwndSelf);
3583 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3587 /*********************************************************************
3591 * With Win95 look the margins are set to default font value unless
3592 * the system font (font == 0) is being set, in which case they are left
3596 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3604 dc = GetDC(es->hwndSelf);
3606 old_font = SelectObject(dc, font);
3607 GetTextMetricsW(dc, &tm);
3608 es->line_height = tm.tmHeight;
3609 es->char_width = tm.tmAveCharWidth;
3611 SelectObject(dc, old_font);
3612 ReleaseDC(es->hwndSelf, dc);
3614 /* Reset the format rect and the margins */
3615 GetClientRect(es->hwndSelf, &clientRect);
3616 EDIT_SetRectNP(es, &clientRect);
3617 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3618 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3620 if (es->style & ES_MULTILINE)
3621 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3623 EDIT_CalcLineWidth_SL(es);
3626 EDIT_UpdateText(es, NULL, TRUE);
3627 if (es->flags & EF_FOCUSED) {
3629 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3630 EDIT_SetCaretPos(es, es->selection_end,
3631 es->flags & EF_AFTER_WRAP);
3632 ShowCaret(es->hwndSelf);
3637 /*********************************************************************
3642 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3643 * The modified flag is reset. No notifications are sent.
3645 * For single-line controls, reception of WM_SETTEXT triggers:
3646 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3649 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3651 LPWSTR textW = NULL;
3652 if (!unicode && text)
3654 LPCSTR textA = (LPCSTR)text;
3655 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3656 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3658 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3662 if (es->flags & EF_UPDATE)
3663 /* fixed this bug once; complain if we see it about to happen again. */
3664 ERR("SetSel may generate UPDATE message whose handler may reset "
3667 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3670 TRACE("%s\n", debugstr_w(text));
3671 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
3673 HeapFree(GetProcessHeap(), 0, textW);
3677 static const WCHAR empty_stringW[] = {0};
3679 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
3682 es->flags &= ~EF_MODIFIED;
3683 EDIT_EM_SetSel(es, 0, 0, FALSE);
3685 /* Send the notification after the selection start and end have been set
3686 * edit control doesn't send notification on WM_SETTEXT
3687 * if it is multiline, or it is part of combobox
3689 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3691 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3692 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3694 EDIT_EM_ScrollCaret(es);
3695 EDIT_UpdateScrollInfo(es);
3699 /*********************************************************************
3704 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
3706 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3708 TRACE("width = %d, height = %d\n", width, height);
3709 SetRect(&rc, 0, 0, width, height);
3710 EDIT_SetRectNP(es, &rc);
3711 EDIT_UpdateText(es, NULL, TRUE);
3716 /*********************************************************************
3720 * This message is sent by SetWindowLong on having changed either the Style
3721 * or the extended style.
3723 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3725 * See also EDIT_WM_NCCreate
3727 * It appears that the Windows version of the edit control allows the style
3728 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3729 * style variable which will generally be different. In this function we
3730 * update the internal style based on what changed in the externally visible
3733 * Much of this content as based upon the MSDN, especially:
3734 * Platform SDK Documentation -> User Interface Services ->
3735 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3736 * Edit Control Styles
3738 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3740 if (GWL_STYLE == which) {
3741 DWORD style_change_mask;
3743 /* Only a subset of changes can be applied after the control
3746 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3748 if (es->style & ES_MULTILINE)
3749 style_change_mask |= ES_WANTRETURN;
3751 new_style = style->styleNew & style_change_mask;
3753 /* Number overrides lowercase overrides uppercase (at least it
3754 * does in Win95). However I'll bet that ES_NUMBER would be
3755 * invalid under Win 3.1.
3757 if (new_style & ES_NUMBER) {
3758 ; /* do not override the ES_NUMBER */
3759 } else if (new_style & ES_LOWERCASE) {
3760 new_style &= ~ES_UPPERCASE;
3763 es->style = (es->style & ~style_change_mask) | new_style;
3764 } else if (GWL_EXSTYLE == which) {
3765 ; /* FIXME - what is needed here */
3767 WARN ("Invalid style change %ld\n",which);
3773 /*********************************************************************
3778 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
3780 if ((key == VK_BACK) && (key_data & 0x2000)) {
3781 if (EDIT_EM_CanUndo(es))
3784 } else if (key == VK_UP || key == VK_DOWN) {
3785 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
3788 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
3792 /*********************************************************************
3797 static void EDIT_WM_Timer(EDITSTATE *es)
3799 if (es->region_posx < 0) {
3800 EDIT_MoveBackward(es, TRUE);
3801 } else if (es->region_posx > 0) {
3802 EDIT_MoveForward(es, TRUE);
3805 * FIXME: gotta do some vertical scrolling here, like
3806 * EDIT_EM_LineScroll(hwnd, 0, 1);
3810 /*********************************************************************
3815 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3820 if (!(es->style & ES_MULTILINE))
3823 if (!(es->style & ES_AUTOHSCROLL))
3827 fw = es->format_rect.right - es->format_rect.left;
3830 TRACE("SB_LINELEFT\n");
3832 dx = -es->char_width;
3835 TRACE("SB_LINERIGHT\n");
3836 if (es->x_offset < es->text_width)
3837 dx = es->char_width;
3840 TRACE("SB_PAGELEFT\n");
3842 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3845 TRACE("SB_PAGERIGHT\n");
3846 if (es->x_offset < es->text_width)
3847 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3855 TRACE("SB_RIGHT\n");
3856 if (es->x_offset < es->text_width)
3857 dx = es->text_width - es->x_offset;
3860 TRACE("SB_THUMBTRACK %d\n", pos);
3861 es->flags |= EF_HSCROLL_TRACK;
3862 if(es->style & WS_HSCROLL)
3863 dx = pos - es->x_offset;
3868 if(pos < 0 || pos > 100) return 0;
3869 /* Assume default scroll range 0-100 */
3870 fw = es->format_rect.right - es->format_rect.left;
3871 new_x = pos * (es->text_width - fw) / 100;
3872 dx = es->text_width ? (new_x - es->x_offset) : 0;
3875 case SB_THUMBPOSITION:
3876 TRACE("SB_THUMBPOSITION %d\n", pos);
3877 es->flags &= ~EF_HSCROLL_TRACK;
3878 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3879 dx = pos - es->x_offset;
3884 if(pos < 0 || pos > 100) return 0;
3885 /* Assume default scroll range 0-100 */
3886 fw = es->format_rect.right - es->format_rect.left;
3887 new_x = pos * (es->text_width - fw) / 100;
3888 dx = es->text_width ? (new_x - es->x_offset) : 0;
3891 /* force scroll info update */
3892 EDIT_UpdateScrollInfo(es);
3893 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3897 TRACE("SB_ENDSCROLL\n");
3900 * FIXME : the next two are undocumented !
3901 * Are we doing the right thing ?
3902 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3903 * although it's also a regular control message.
3905 case EM_GETTHUMB: /* this one is used by NT notepad */
3908 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3909 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3912 /* Assume default scroll range 0-100 */
3913 INT fw = es->format_rect.right - es->format_rect.left;
3914 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
3916 TRACE("EM_GETTHUMB: returning %ld\n", ret);
3920 TRACE("EM_LINESCROLL16\n");
3925 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
3931 INT fw = es->format_rect.right - es->format_rect.left;
3932 /* check if we are going to move too far */
3933 if(es->x_offset + dx + fw > es->text_width)
3934 dx = es->text_width - fw - es->x_offset;
3936 EDIT_EM_LineScroll_internal(es, dx, 0);
3942 /*********************************************************************
3947 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
3951 if (!(es->style & ES_MULTILINE))
3954 if (!(es->style & ES_AUTOVSCROLL))
3963 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
3964 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
3965 (action == SB_PAGEUP ? "SB_PAGEUP" :
3967 EDIT_EM_Scroll(es, action);
3974 TRACE("SB_BOTTOM\n");
3975 dy = es->line_count - 1 - es->y_offset;
3978 TRACE("SB_THUMBTRACK %d\n", pos);
3979 es->flags |= EF_VSCROLL_TRACK;
3980 if(es->style & WS_VSCROLL)
3981 dy = pos - es->y_offset;
3984 /* Assume default scroll range 0-100 */
3987 if(pos < 0 || pos > 100) return 0;
3988 vlc = get_vertical_line_count(es);
3989 new_y = pos * (es->line_count - vlc) / 100;
3990 dy = es->line_count ? (new_y - es->y_offset) : 0;
3991 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
3992 es->line_count, es->y_offset, pos, dy);
3995 case SB_THUMBPOSITION:
3996 TRACE("SB_THUMBPOSITION %d\n", pos);
3997 es->flags &= ~EF_VSCROLL_TRACK;
3998 if(es->style & WS_VSCROLL)
3999 dy = pos - es->y_offset;
4002 /* Assume default scroll range 0-100 */
4005 if(pos < 0 || pos > 100) return 0;
4006 vlc = get_vertical_line_count(es);
4007 new_y = pos * (es->line_count - vlc) / 100;
4008 dy = es->line_count ? (new_y - es->y_offset) : 0;
4009 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4010 es->line_count, es->y_offset, pos, dy);
4014 /* force scroll info update */
4015 EDIT_UpdateScrollInfo(es);
4016 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4020 TRACE("SB_ENDSCROLL\n");
4023 * FIXME : the next two are undocumented !
4024 * Are we doing the right thing ?
4025 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4026 * although it's also a regular control message.
4028 case EM_GETTHUMB: /* this one is used by NT notepad */
4031 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4032 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4035 /* Assume default scroll range 0-100 */
4036 INT vlc = get_vertical_line_count(es);
4037 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4039 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4043 TRACE("EM_LINESCROLL %d\n", pos);
4048 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4053 EDIT_EM_LineScroll(es, 0, dy);
4057 /*********************************************************************
4061 * FIXME: is this right ? (or should it be only VSCROLL)
4062 * (and maybe only for edit controls that really have their
4063 * own scrollbars) (and maybe only for multiline controls ?)
4064 * All in all: very poorly documented
4067 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4069 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4070 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4074 /********************************************************************
4076 * The Following code is to handle inline editing from IMEs
4079 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4082 LPWSTR lpCompStr = NULL;
4083 LPSTR lpCompStrAttr = NULL;
4086 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4093 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
4096 ERR("Unable to allocate IME CompositionString\n");
4101 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4102 lpCompStr[buflen/sizeof(WCHAR)] = 0;
4104 if (CompFlag & GCS_COMPATTR)
4107 * We do not use the attributes yet. it would tell us what characters
4108 * are in transition and which are converted or decided upon
4110 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4114 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4117 ERR("Unable to allocate IME Attribute String\n");
4118 HeapFree(GetProcessHeap(),0,lpCompStr);
4121 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4123 lpCompStrAttr[dwBufLenAttr] = 0;
4126 lpCompStrAttr = NULL;
4129 /* check for change in composition start */
4130 if (es->selection_end < es->composition_start)
4131 es->composition_start = es->selection_end;
4133 /* replace existing selection string */
4134 es->selection_start = es->composition_start;
4136 if (es->composition_len > 0)
4137 es->selection_end = es->composition_start + es->composition_len;
4139 es->selection_end = es->selection_start;
4141 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
4142 es->composition_len = abs(es->composition_start - es->selection_end);
4144 es->selection_start = es->composition_start;
4145 es->selection_end = es->selection_start + es->composition_len;
4147 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4148 HeapFree(GetProcessHeap(),0,lpCompStr);
4151 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4156 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4162 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
4165 ERR("Unable to alloc buffer for IME string\n");
4169 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4170 lpResultStr[buflen/sizeof(WCHAR)] = 0;
4172 /* check for change in composition start */
4173 if (es->selection_end < es->composition_start)
4174 es->composition_start = es->selection_end;
4176 es->selection_start = es->composition_start;
4177 es->selection_end = es->composition_start + es->composition_len;
4178 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
4179 es->composition_start = es->selection_end;
4180 es->composition_len = 0;
4182 HeapFree(GetProcessHeap(),0,lpResultStr);
4185 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4190 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4192 static const WCHAR empty_stringW[] = {0};
4193 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4194 es->composition_start = es->selection_end;
4197 hIMC = ImmGetContext(hwnd);
4201 if (CompFlag & GCS_RESULTSTR)
4202 EDIT_GetResultStr(hIMC, es);
4203 if (CompFlag & GCS_COMPSTR)
4204 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4205 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4206 ImmReleaseContext(hwnd, hIMC);
4207 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4211 /*********************************************************************
4215 * See also EDIT_WM_StyleChanged
4217 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4222 TRACE("Creating %s edit control, style = %08x\n",
4223 unicode ? "Unicode" : "ANSI", lpcs->style);
4225 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4227 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4230 * Note: since the EDITSTATE has not been fully initialized yet,
4231 * we can't use any API calls that may send
4232 * WM_XXX messages before WM_NCCREATE is completed.
4235 es->is_unicode = unicode;
4236 es->style = lpcs->style;
4238 es->bEnableState = !(es->style & WS_DISABLED);
4240 es->hwndSelf = hwnd;
4241 /* Save parent, which will be notified by EN_* messages */
4242 es->hwndParent = lpcs->hwndParent;
4244 if (es->style & ES_COMBO)
4245 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4247 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4248 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4250 /* Number overrides lowercase overrides uppercase (at least it
4251 * does in Win95). However I'll bet that ES_NUMBER would be
4252 * invalid under Win 3.1.
4254 if (es->style & ES_NUMBER) {
4255 ; /* do not override the ES_NUMBER */
4256 } else if (es->style & ES_LOWERCASE) {
4257 es->style &= ~ES_UPPERCASE;
4259 if (es->style & ES_MULTILINE) {
4260 es->buffer_limit = BUFLIMIT_INITIAL;
4261 if (es->style & WS_VSCROLL)
4262 es->style |= ES_AUTOVSCROLL;
4263 if (es->style & WS_HSCROLL)
4264 es->style |= ES_AUTOHSCROLL;
4265 es->style &= ~ES_PASSWORD;
4266 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4267 /* Confirmed - RIGHT overrides CENTER */
4268 if (es->style & ES_RIGHT)
4269 es->style &= ~ES_CENTER;
4270 es->style &= ~WS_HSCROLL;
4271 es->style &= ~ES_AUTOHSCROLL;
4274 es->buffer_limit = BUFLIMIT_INITIAL;
4275 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4276 es->style &= ~ES_CENTER;
4277 es->style &= ~WS_HSCROLL;
4278 es->style &= ~WS_VSCROLL;
4279 if (es->style & ES_PASSWORD)
4280 es->password_char = '*';
4283 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4284 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4286 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4288 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4290 es->undo_buffer_size = es->buffer_size;
4292 if (es->style & ES_MULTILINE)
4293 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4298 * In Win95 look and feel, the WS_BORDER style is replaced by the
4299 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4300 * control a nonclient area so we don't need to draw the border.
4301 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4302 * a nonclient area and we should handle painting the border ourselves.
4304 * When making modifications please ensure that the code still works
4305 * for edit controls created directly with style 0x50800000, exStyle 0
4306 * (which should have a single pixel border)
4308 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4309 es->style &= ~WS_BORDER;
4310 else if (es->style & WS_BORDER)
4311 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4316 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4317 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4318 HeapFree(GetProcessHeap(), 0, es->undo_text);
4319 if (es->hloc32W) LocalFree(es->hloc32W);
4320 HeapFree(GetProcessHeap(), 0, es);
4325 /*********************************************************************
4330 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4334 TRACE("%s\n", debugstr_w(name));
4336 * To initialize some final structure members, we call some helper
4337 * functions. However, since the EDITSTATE is not consistent (i.e.
4338 * not fully initialized), we should be very careful which
4339 * functions can be called, and in what order.
4341 EDIT_WM_SetFont(es, 0, FALSE);
4342 EDIT_EM_EmptyUndoBuffer(es);
4344 /* We need to calculate the format rect
4345 (applications may send EM_SETMARGINS before the control gets visible) */
4346 GetClientRect(es->hwndSelf, &clientRect);
4347 EDIT_SetRectNP(es, &clientRect);
4349 if (name && *name) {
4350 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4351 /* if we insert text to the editline, the text scrolls out
4352 * of the window, as the caret is placed after the insert
4353 * pos normally; thus we reset es->selection... to 0 and
4356 es->selection_start = es->selection_end = 0;
4357 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4358 * Messages are only to be sent when the USER does something to
4359 * change the contents. So I am removing this EN_CHANGE
4361 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4363 EDIT_EM_ScrollCaret(es);
4365 /* force scroll info update */
4366 EDIT_UpdateScrollInfo(es);
4367 /* The rule seems to return 1 here for success */
4368 /* Power Builder masked edit controls will crash */
4370 /* FIXME: is that in all cases so ? */
4375 /*********************************************************************
4380 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4385 LocalFree(es->hloc32W);
4388 LocalFree(es->hloc32A);
4390 pc = es->first_line_def;
4394 HeapFree(GetProcessHeap(), 0, pc);
4398 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4399 HeapFree(GetProcessHeap(), 0, es->undo_text);
4400 HeapFree(GetProcessHeap(), 0, es);
4406 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4409 return DefWindowProcW(hwnd, msg, wParam, lParam);
4411 return DefWindowProcA(hwnd, msg, wParam, lParam);
4414 /*********************************************************************
4416 * EditWndProc_common
4418 * The messages are in the order of the actual integer values
4419 * (which can be found in include/windows.h)
4421 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4423 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4426 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4428 if (!es && msg != WM_NCCREATE)
4429 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4431 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4435 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4439 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4440 EDIT_EM_ScrollCaret(es);
4446 CopyRect((LPRECT)lParam, &es->format_rect);
4450 if ((es->style & ES_MULTILINE) && lParam) {
4451 EDIT_SetRectNP(es, (LPRECT)lParam);
4452 EDIT_UpdateText(es, NULL, TRUE);
4457 if ((es->style & ES_MULTILINE) && lParam)
4458 EDIT_SetRectNP(es, (LPRECT)lParam);
4462 result = EDIT_EM_Scroll(es, (INT)wParam);
4466 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4469 case EM_SCROLLCARET:
4470 EDIT_EM_ScrollCaret(es);
4475 result = ((es->flags & EF_MODIFIED) != 0);
4480 es->flags |= EF_MODIFIED;
4482 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4485 case EM_GETLINECOUNT:
4486 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4490 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4494 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4498 result = (LRESULT)EDIT_EM_GetHandle(es);
4502 result = EDIT_EM_GetThumb(es);
4505 /* these messages missing from specs */
4510 FIXME("undocumented message 0x%x, please report\n", msg);
4511 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4515 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4523 textW = (LPWSTR)lParam;
4526 LPSTR textA = (LPSTR)lParam;
4527 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4528 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4529 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4532 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
4536 HeapFree(GetProcessHeap(), 0, textW);
4541 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4544 case EM_SETLIMITTEXT:
4545 EDIT_EM_SetLimitText(es, wParam);
4549 result = (LRESULT)EDIT_EM_CanUndo(es);
4554 result = (LRESULT)EDIT_EM_Undo(es);
4558 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4561 case EM_LINEFROMCHAR:
4562 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4565 case EM_SETTABSTOPS:
4566 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4569 case EM_SETPASSWORDCHAR:
4574 charW = (WCHAR)wParam;
4577 CHAR charA = wParam;
4578 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4581 EDIT_EM_SetPasswordChar(es, charW);
4585 case EM_EMPTYUNDOBUFFER:
4586 EDIT_EM_EmptyUndoBuffer(es);
4589 case EM_GETFIRSTVISIBLELINE:
4590 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4593 case EM_SETREADONLY:
4595 DWORD old_style = es->style;
4598 SetWindowLongW( hwnd, GWL_STYLE,
4599 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4600 es->style |= ES_READONLY;
4602 SetWindowLongW( hwnd, GWL_STYLE,
4603 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4604 es->style &= ~ES_READONLY;
4607 if (old_style ^ es->style)
4608 InvalidateRect(es->hwndSelf, NULL, TRUE);
4614 case EM_SETWORDBREAKPROC:
4615 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4618 case EM_GETWORDBREAKPROC:
4619 result = (LRESULT)es->word_break_proc;
4622 case EM_GETPASSWORDCHAR:
4625 result = es->password_char;
4628 WCHAR charW = es->password_char;
4630 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4637 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4641 result = MAKELONG(es->left_margin, es->right_margin);
4644 case EM_GETLIMITTEXT:
4645 result = es->buffer_limit;
4648 case EM_POSFROMCHAR:
4649 if ((INT)wParam >= get_text_length(es)) result = -1;
4650 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4653 case EM_CHARFROMPOS:
4654 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4657 /* End of the EM_ messages which were in numerical order; what order
4658 * are these in? vaguely alphabetical?
4662 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4666 result = EDIT_WM_NCDestroy(es);
4671 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4673 if (es->style & ES_MULTILINE)
4674 result |= DLGC_WANTALLKEYS;
4678 es->flags|=EF_DIALOGMODE;
4680 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4682 int vk = (int)((LPMSG)lParam)->wParam;
4684 if (es->hwndListBox)
4686 if (vk == VK_RETURN || vk == VK_ESCAPE)
4687 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4688 result |= DLGC_WANTMESSAGE;
4700 strng[0] = wParam >> 8;
4701 strng[1] = wParam & 0xff;
4702 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4703 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4704 result = EDIT_WM_Char(es, charW);
4716 CHAR charA = wParam;
4717 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4720 if (es->hwndListBox)
4722 if (charW == VK_RETURN || charW == VK_ESCAPE)
4724 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4725 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4729 result = EDIT_WM_Char(es, charW);
4736 if (wParam == UNICODE_NOCHAR) return TRUE;
4737 if (wParam <= 0x000fffff)
4739 if(wParam > 0xffff) /* convert to surrogates */
4742 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4743 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4745 else EDIT_WM_Char(es, wParam);
4756 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
4759 case WM_CONTEXTMENU:
4760 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4769 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4772 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4773 LPWSTR nameW = NULL;
4776 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
4777 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4778 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
4780 result = EDIT_WM_Create(es, nameW);
4781 HeapFree(GetProcessHeap(), 0, nameW);
4790 es->bEnableState = (BOOL) wParam;
4791 EDIT_UpdateText(es, NULL, TRUE);
4795 /* we do the proper erase in EDIT_WM_Paint */
4800 result = (LRESULT)es->font;
4804 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
4807 case WM_GETTEXTLENGTH:
4808 if (unicode) result = get_text_length(es);
4809 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
4810 NULL, 0, NULL, NULL );
4814 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4818 result = EDIT_WM_KeyDown(es, (INT)wParam);
4822 result = EDIT_WM_KillFocus(es);
4825 case WM_LBUTTONDBLCLK:
4826 result = EDIT_WM_LButtonDblClk(es);
4829 case WM_LBUTTONDOWN:
4830 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
4834 result = EDIT_WM_LButtonUp(es);
4837 case WM_MBUTTONDOWN:
4838 result = EDIT_WM_MButtonDown(es);
4842 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4845 case WM_PRINTCLIENT:
4847 EDIT_WM_Paint(es, (HDC)wParam);
4855 EDIT_WM_SetFocus(es);
4859 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
4863 /* FIXME: actually set an internal flag and behave accordingly */
4867 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
4872 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
4875 case WM_STYLECHANGED:
4876 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
4879 case WM_STYLECHANGING:
4880 result = 0; /* See EDIT_WM_StyleChanged */
4884 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
4892 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4897 int gcWheelDelta = 0;
4898 UINT pulScrollLines = 3;
4899 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4901 if (wParam & (MK_SHIFT | MK_CONTROL)) {
4902 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4905 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
4906 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4908 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
4909 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
4910 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
4916 /* IME messages to make the edit control IME aware */
4917 case WM_IME_SETCONTEXT:
4920 case WM_IME_STARTCOMPOSITION:
4921 es->composition_start = es->selection_end;
4922 es->composition_len = 0;
4925 case WM_IME_COMPOSITION:
4926 EDIT_ImeComposition(hwnd, lParam, es);
4929 case WM_IME_ENDCOMPOSITION:
4930 if (es->composition_len > 0)
4932 static const WCHAR empty_stringW[] = {0};
4933 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4934 es->selection_end = es->selection_start;
4935 es->composition_len= 0;
4939 case WM_IME_COMPOSITIONFULL:
4945 case WM_IME_CONTROL:
4949 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4953 if (IsWindow(hwnd) && es) EDIT_UnlockBuffer(es, FALSE);
4955 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
4961 /*********************************************************************
4962 * edit class descriptor
4964 static const WCHAR editW[] = {'E','d','i','t',0};
4965 const struct builtin_class_descr EDIT_builtin_class =
4968 CS_DBLCLKS | CS_PARENTDC, /* style */
4969 WINPROC_EDIT, /* proc */
4971 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
4973 sizeof(EDITSTATE *), /* extra */
4975 IDC_IBEAM, /* cursor */