4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
57 #include "wine/winbase16.h"
58 #include "wine/winuser16.h"
59 #include "wine/unicode.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
69 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
70 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
71 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
74 * extra flags for EDITSTATE.flags field
76 #define EF_MODIFIED 0x0001 /* text has been modified */
77 #define EF_FOCUSED 0x0002 /* we have input focus */
78 #define EF_UPDATE 0x0004 /* notify parent of changed state */
79 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
80 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
82 wrapped line, instead of in front of the next character */
83 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
84 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
85 the text buffer if this is clear. */
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
109 UINT buffer_size; /* the size of the buffer in characters */
110 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
111 HFONT font; /* NULL means standard system font */
112 INT x_offset; /* scroll offset for multi lines this is in pixels
113 for single lines it's in characters */
114 INT line_height; /* height of a screen line in pixels */
115 INT char_width; /* average character width in pixels */
116 DWORD style; /* sane version of wnd->dwStyle */
117 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
118 INT undo_insert_count; /* number of characters inserted in sequence */
119 UINT undo_position; /* character index of the insertion and deletion */
120 LPWSTR undo_text; /* deleted text */
121 UINT undo_buffer_size; /* size of the deleted text buffer */
122 INT selection_start; /* == selection_end if no selection */
123 INT selection_end; /* == current caret position */
124 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
125 INT left_margin; /* in pixels */
126 INT right_margin; /* in pixels */
128 INT text_width; /* width of the widest line in pixels for multi line controls
129 and just line width for single line controls */
130 INT region_posx; /* Position of cursor relative to region: */
131 INT region_posy; /* -1: to left, 0: within, 1: to right */
132 EDITWORDBREAKPROC16 word_break_proc16;
133 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
134 INT line_count; /* number of lines */
135 INT y_offset; /* scroll offset in number of lines */
136 BOOL bCaptureState; /* flag indicating whether mouse was captured */
137 BOOL bEnableState; /* flag keeping the enable state */
138 HWND hwndSelf; /* the our window handle */
139 HWND hwndParent; /* Handle of parent for sending EN_* messages.
140 Even if parent will change, EN_* messages
141 should be sent to the first parent. */
142 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
144 * only for multi line controls
146 INT lock_count; /* amount of re-entries in the EditWndProc */
149 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
150 HLOCAL hloc32W; /* our unicode local memory block */
151 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
153 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
158 UINT composition_len; /* length of composition, 0 == no composition */
159 int composition_start; /* the character position for the composition */
163 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
164 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
166 /* used for disabled or read-only edit control */
167 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
169 { /* Notify parent which has created this edit control */ \
170 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
171 SendMessageW(es->hwndParent, WM_COMMAND, \
172 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
173 (LPARAM)(es->hwndSelf)); \
177 /*********************************************************************
182 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
184 return (es->undo_insert_count || strlenW(es->undo_text));
188 /*********************************************************************
193 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
195 es->undo_insert_count = 0;
196 *es->undo_text = '\0';
200 /**********************************************************************
203 * Returns the window version in case Wine emulates a later version
204 * of windows than the application expects.
206 * In a number of cases when windows runs an application that was
207 * designed for an earlier windows version, windows reverts
208 * to "old" behaviour of that earlier version.
210 * An example is a disabled edit control that needs to be painted.
211 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
212 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
213 * applications with an expected version 0f 4.0 or higher.
216 static DWORD get_app_version(void)
218 static DWORD version;
221 DWORD dwEmulatedVersion;
223 DWORD dwProcVersion = GetProcessVersion(0);
225 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
226 GetVersionExW( &info );
227 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
228 /* FIXME: this may not be 100% correct; see discussion on the
229 * wine developer list in Nov 1999 */
230 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
235 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
240 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
241 msg = WM_CTLCOLORSTATIC;
243 msg = WM_CTLCOLOREDIT;
245 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
246 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
248 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
253 /*********************************************************************
257 * Find the beginning of words.
258 * Note: unlike the specs for a WordBreakProc, this function only
259 * allows to be called without linebreaks between s[0] up to
260 * s[count - 1]. Remember it is only called
261 * internally, so we can decide this for ourselves.
264 static INT EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
268 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
278 if (s[index] == ' ') {
279 while (index && (s[index] == ' '))
282 while (index && (s[index] != ' '))
288 while (index && (s[index] != ' '))
301 while ((index < count) && (s[index] == ' ')) index++;
303 while (s[index] && (s[index] != ' ') && (index < count))
305 while ((s[index] == ' ') && (index < count)) index++;
310 ret = (s[index] == ' ');
313 ERR("unknown action code, please report !\n");
320 /*********************************************************************
322 * EDIT_CallWordBreakProc
324 * Call appropriate WordBreakProc (internal or external).
326 * Note: The "start" argument should always be an index referring
327 * to es->text. The actual wordbreak proc might be
328 * 16 bit, so we can't always pass any 32 bit LPSTR.
329 * Hence we assume that es->text is the buffer that holds
330 * the string under examination (we can decide this for ourselves).
333 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
337 if (es->word_break_proc16) {
344 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
345 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
346 segptr = WOWGlobalLock16(hglob16);
347 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
348 args[4] = SELECTOROF(segptr);
349 args[3] = OFFSETOF(segptr);
353 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
354 ret = LOWORD(result);
355 GlobalUnlock16(hglob16);
356 GlobalFree16(hglob16);
358 else if (es->word_break_proc)
362 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
364 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
365 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
366 ret = wbpW(es->text + start, index, count, action);
370 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
374 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
375 textA = HeapAlloc(GetProcessHeap(), 0, countA);
376 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
377 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
378 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
379 ret = wbpA(textA, index, countA, action);
380 HeapFree(GetProcessHeap(), 0, textA);
384 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
389 /*********************************************************************
391 * EDIT_BuildLineDefs_ML
393 * Build linked list of text lines.
394 * Lines can end with '\0' (last line), a character (if it is wrapped),
395 * a soft return '\r\r\n' or a hard return '\r\n'
398 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
402 LPWSTR current_position, cp;
404 LINEDEF *current_line;
405 LINEDEF *previous_line;
407 INT line_index = 0, nstart_line = 0, nstart_index = 0;
408 INT line_count = es->line_count;
412 if (istart == iend && delta == 0)
415 dc = GetDC(es->hwndSelf);
417 old_font = SelectObject(dc, es->font);
419 previous_line = NULL;
420 current_line = es->first_line_def;
422 /* Find starting line. istart must lie inside an existing line or
423 * at the end of buffer */
425 if (istart < current_line->index + current_line->length ||
426 current_line->ending == END_0)
429 previous_line = current_line;
430 current_line = current_line->next;
432 } while (current_line);
434 if (!current_line) /* Error occurred start is not inside previous buffer */
436 FIXME(" modification occurred outside buffer\n");
437 ReleaseDC(es->hwndSelf, dc);
441 /* Remember start of modifications in order to calculate update region */
442 nstart_line = line_index;
443 nstart_index = current_line->index;
445 /* We must start to reformat from the previous line since the modifications
446 * may have caused the line to wrap upwards. */
447 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
450 current_line = previous_line;
452 start_line = current_line;
454 fw = es->format_rect.right - es->format_rect.left;
455 current_position = es->text + current_line->index;
457 if (current_line != start_line)
459 if (!current_line || current_line->index + delta > current_position - es->text)
461 /* The buffer has been expanded, create a new line and
462 insert it into the link list */
463 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
464 new_line->next = previous_line->next;
465 previous_line->next = new_line;
466 current_line = new_line;
469 else if (current_line->index + delta < current_position - es->text)
471 /* The previous line merged with this line so we delete this extra entry */
472 previous_line->next = current_line->next;
473 HeapFree(GetProcessHeap(), 0, current_line);
474 current_line = previous_line->next;
478 else /* current_line->index + delta == current_position */
480 if (current_position - es->text > iend)
481 break; /* We reached end of line modifications */
482 /* else recalculate this line */
486 current_line->index = current_position - es->text;
487 orig_net_length = current_line->net_length;
489 /* Find end of line */
490 cp = current_position;
492 if (*cp == '\n') break;
493 if ((*cp == '\r') && (*(cp + 1) == '\n'))
498 /* Mark type of line termination */
500 current_line->ending = END_0;
501 current_line->net_length = strlenW(current_position);
502 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
503 current_line->ending = END_SOFT;
504 current_line->net_length = cp - current_position - 1;
505 } else if (*cp == '\n') {
506 current_line->ending = END_RICH;
507 current_line->net_length = cp - current_position;
509 current_line->ending = END_HARD;
510 current_line->net_length = cp - current_position;
513 /* Calculate line width */
514 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
515 current_position, current_line->net_length,
516 es->tabs_count, es->tabs));
518 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
519 if (!(es->style & ES_AUTOHSCROLL)) {
520 if (current_line->width > fw) {
525 next = EDIT_CallWordBreakProc(es, current_position - es->text,
526 prev + 1, current_line->net_length, WB_RIGHT);
527 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
528 current_position, next, es->tabs_count, es->tabs));
529 } while (current_line->width <= fw);
530 if (!prev) { /* Didn't find a line break so force a break */
535 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
536 current_position, next, es->tabs_count, es->tabs));
537 } while (current_line->width <= fw);
542 /* If the first line we are calculating, wrapped before istart, we must
543 * adjust istart in order for this to be reflected in the update region. */
544 if (current_line->index == nstart_index && istart > current_line->index + prev)
545 istart = current_line->index + prev;
546 /* else if we are updating the previous line before the first line we
547 * are re-calculating and it expanded */
548 else if (current_line == start_line &&
549 current_line->index != nstart_index && orig_net_length < prev)
551 /* Line expanded due to an upwards line wrap so we must partially include
552 * previous line in update region */
553 nstart_line = line_index;
554 nstart_index = current_line->index;
555 istart = current_line->index + orig_net_length;
558 current_line->net_length = prev;
559 current_line->ending = END_WRAP;
560 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
561 current_line->net_length, es->tabs_count, es->tabs));
563 else if (current_line == start_line &&
564 current_line->index != nstart_index &&
565 orig_net_length < current_line->net_length) {
566 /* The previous line expanded but it's still not as wide as the client rect */
567 /* The expansion is due to an upwards line wrap so we must partially include
568 it in the update region */
569 nstart_line = line_index;
570 nstart_index = current_line->index;
571 istart = current_line->index + orig_net_length;
576 /* Adjust length to include line termination */
577 switch (current_line->ending) {
579 current_line->length = current_line->net_length + 3;
582 current_line->length = current_line->net_length + 1;
585 current_line->length = current_line->net_length + 2;
589 current_line->length = current_line->net_length;
592 es->text_width = max(es->text_width, current_line->width);
593 current_position += current_line->length;
594 previous_line = current_line;
595 current_line = current_line->next;
597 } while (previous_line->ending != END_0);
599 /* Finish adjusting line indexes by delta or remove hanging lines */
600 if (previous_line->ending == END_0)
602 LINEDEF *pnext = NULL;
604 previous_line->next = NULL;
607 pnext = current_line->next;
608 HeapFree(GetProcessHeap(), 0, current_line);
609 current_line = pnext;
617 current_line->index += delta;
618 current_line = current_line->next;
622 /* Calculate rest of modification rectangle */
627 * We calculate two rectangles. One for the first line which may have
628 * an indent with respect to the format rect. The other is a format-width
629 * rectangle that spans the rest of the lines that changed or moved.
631 rc.top = es->format_rect.top + nstart_line * es->line_height -
632 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
633 rc.bottom = rc.top + es->line_height;
634 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
635 rc.left = es->format_rect.left;
637 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
638 es->text + nstart_index, istart - nstart_index,
639 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
640 rc.right = es->format_rect.right;
641 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
644 rc.left = es->format_rect.left;
645 rc.right = es->format_rect.right;
647 * If lines were added or removed we must re-paint the remainder of the
648 * lines since the remaining lines were either shifted up or down.
650 if (line_count < es->line_count) /* We added lines */
651 rc.bottom = es->line_count * es->line_height;
652 else if (line_count > es->line_count) /* We removed lines */
653 rc.bottom = line_count * es->line_height;
655 rc.bottom = line_index * es->line_height;
656 rc.bottom += es->format_rect.top;
657 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
658 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
659 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
660 DeleteObject(tmphrgn);
664 SelectObject(dc, old_font);
666 ReleaseDC(es->hwndSelf, dc);
670 static inline UINT get_text_length(EDITSTATE *es)
672 if(es->text_length == (UINT)-1)
673 es->text_length = strlenW(es->text);
674 return es->text_length;
677 /*********************************************************************
679 * EDIT_GetPasswordPointer_SL
681 * note: caller should free the (optionally) allocated buffer
684 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
686 if (es->style & ES_PASSWORD) {
687 INT len = get_text_length(es);
688 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
690 while(len) text[--len] = es->password_char;
697 /*********************************************************************
699 * EDIT_CalcLineWidth_SL
702 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
709 text = EDIT_GetPasswordPointer_SL(es);
711 dc = GetDC(es->hwndSelf);
713 old_font = SelectObject(dc, es->font);
715 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
718 SelectObject(dc, old_font);
719 ReleaseDC(es->hwndSelf, dc);
721 if (es->style & ES_PASSWORD)
722 HeapFree(GetProcessHeap(), 0, text);
724 es->text_width = size.cx;
727 /*********************************************************************
731 * Beware: This is not the function called on EM_CHARFROMPOS
732 * The position _can_ be outside the formatting / client
734 * The return value is only the character index
737 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
742 INT x_high = 0, x_low = 0;
744 if (es->style & ES_MULTILINE) {
745 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
747 LINEDEF *line_def = es->first_line_def;
749 while ((line > 0) && line_def->next) {
750 line_index += line_def->length;
751 line_def = line_def->next;
754 x += es->x_offset - es->format_rect.left;
755 if (es->style & ES_RIGHT)
756 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
757 else if (es->style & ES_CENTER)
758 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
759 if (x >= line_def->width) {
761 *after_wrap = (line_def->ending == END_WRAP);
762 return line_index + line_def->net_length;
769 dc = GetDC(es->hwndSelf);
771 old_font = SelectObject(dc, es->font);
773 high = line_index + line_def->net_length + 1;
774 while (low < high - 1)
776 INT mid = (low + high) / 2;
777 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
786 if (abs(x_high - x) + 1 <= abs(x_low - x))
792 *after_wrap = ((index == line_index + line_def->net_length) &&
793 (line_def->ending == END_WRAP));
799 x -= es->format_rect.left;
805 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
806 if (es->style & ES_RIGHT)
808 else if (es->style & ES_CENTER)
812 text = EDIT_GetPasswordPointer_SL(es);
813 dc = GetDC(es->hwndSelf);
815 old_font = SelectObject(dc, es->font);
819 INT high = es->x_offset;
820 while (low < high - 1)
822 INT mid = (low + high) / 2;
823 GetTextExtentPoint32W( dc, text + mid,
824 es->x_offset - mid, &size );
833 if (abs(x_high + x) <= abs(x_low + x) + 1)
840 INT low = es->x_offset;
841 INT high = get_text_length(es) + 1;
842 while (low < high - 1)
844 INT mid = (low + high) / 2;
845 GetTextExtentPoint32W( dc, text + es->x_offset,
846 mid - es->x_offset, &size );
855 if (abs(x_high - x) <= abs(x_low - x) + 1)
860 if (es->style & ES_PASSWORD)
861 HeapFree(GetProcessHeap(), 0, text);
864 SelectObject(dc, old_font);
865 ReleaseDC(es->hwndSelf, dc);
870 /*********************************************************************
874 * adjusts the point to be within the formatting rectangle
875 * (so CharFromPos returns the nearest _visible_ character)
878 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
880 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
881 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
885 /*********************************************************************
890 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
895 if (!(es->style & ES_MULTILINE))
897 if (index > (INT)get_text_length(es))
898 return es->line_count - 1;
900 index = min(es->selection_start, es->selection_end);
903 line_def = es->first_line_def;
904 index -= line_def->length;
905 while ((index >= 0) && line_def->next) {
907 line_def = line_def->next;
908 index -= line_def->length;
914 /*********************************************************************
919 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
922 const LINEDEF *line_def;
924 if (!(es->style & ES_MULTILINE))
926 if (line >= es->line_count)
930 line_def = es->first_line_def;
932 INT index = es->selection_end - line_def->length;
933 while ((index >= 0) && line_def->next) {
934 line_index += line_def->length;
935 line_def = line_def->next;
936 index -= line_def->length;
940 line_index += line_def->length;
941 line_def = line_def->next;
949 /*********************************************************************
954 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
958 if (!(es->style & ES_MULTILINE))
959 return get_text_length(es);
962 /* get the number of remaining non-selected chars of selected lines */
963 INT32 l; /* line number */
964 INT32 li; /* index of first char in line */
966 l = EDIT_EM_LineFromChar(es, es->selection_start);
967 /* # chars before start of selection area */
968 count = es->selection_start - EDIT_EM_LineIndex(es, l);
969 l = EDIT_EM_LineFromChar(es, es->selection_end);
970 /* # chars after end of selection */
971 li = EDIT_EM_LineIndex(es, l);
972 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
975 line_def = es->first_line_def;
976 index -= line_def->length;
977 while ((index >= 0) && line_def->next) {
978 line_def = line_def->next;
979 index -= line_def->length;
981 return line_def->net_length;
985 /*********************************************************************
990 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
992 INT len = get_text_length(es);
1005 index = min(index, len);
1006 dc = GetDC(es->hwndSelf);
1008 old_font = SelectObject(dc, es->font);
1009 if (es->style & ES_MULTILINE) {
1010 l = EDIT_EM_LineFromChar(es, index);
1011 y = (l - es->y_offset) * es->line_height;
1012 li = EDIT_EM_LineIndex(es, l);
1013 if (after_wrap && (li == index) && l) {
1015 line_def = es->first_line_def;
1017 line_def = line_def->next;
1020 if (line_def->ending == END_WRAP) {
1022 y -= es->line_height;
1023 li = EDIT_EM_LineIndex(es, l);
1027 line_def = es->first_line_def;
1028 while (line_def->index != li)
1029 line_def = line_def->next;
1031 ll = line_def->net_length;
1032 lw = line_def->width;
1034 w = es->format_rect.right - es->format_rect.left;
1035 if (es->style & ES_RIGHT)
1037 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
1038 es->tabs_count, es->tabs)) - es->x_offset;
1041 else if (es->style & ES_CENTER)
1043 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1044 es->tabs_count, es->tabs)) - es->x_offset;
1049 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1050 es->tabs_count, es->tabs)) - es->x_offset;
1053 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
1054 if (index < es->x_offset) {
1055 GetTextExtentPoint32W(dc, text + index,
1056 es->x_offset - index, &size);
1059 GetTextExtentPoint32W(dc, text + es->x_offset,
1060 index - es->x_offset, &size);
1063 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1065 w = es->format_rect.right - es->format_rect.left;
1066 if (w > es->text_width)
1068 if (es->style & ES_RIGHT)
1069 x += w - es->text_width;
1070 else if (es->style & ES_CENTER)
1071 x += (w - es->text_width) / 2;
1076 if (es->style & ES_PASSWORD)
1077 HeapFree(GetProcessHeap(), 0, text);
1079 x += es->format_rect.left;
1080 y += es->format_rect.top;
1082 SelectObject(dc, old_font);
1083 ReleaseDC(es->hwndSelf, dc);
1084 return MAKELONG((INT16)x, (INT16)y);
1088 /*********************************************************************
1092 * Calculates the bounding rectangle for a line from a starting
1093 * column to an ending column.
1096 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1098 INT line_index = EDIT_EM_LineIndex(es, line);
1100 if (es->style & ES_MULTILINE)
1101 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1103 rc->top = es->format_rect.top;
1104 rc->bottom = rc->top + es->line_height;
1105 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1106 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1110 static inline void text_buffer_changed(EDITSTATE *es)
1112 es->text_length = (UINT)-1;
1115 /*********************************************************************
1119 * This acts as a LocalLock16(), but it locks only once. This way
1120 * you can call it whenever you like, without unlocking.
1122 * Initially the edit control allocates a HLOCAL32 buffer
1123 * (32 bit linear memory handler). However, 16 bit application
1124 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1125 * handler). From that moment on we have to keep using this 16 bit memory
1126 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1127 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1131 static void EDIT_LockBuffer(EDITSTATE *es)
1133 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1134 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1139 BOOL _16bit = FALSE;
1145 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1146 textA = LocalLock(es->hloc32A);
1147 countA = strlen(textA) + 1;
1151 HANDLE16 oldDS = stack16->ds;
1152 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1153 stack16->ds = hInstance;
1154 textA = MapSL(LocalLock16(es->hloc16));
1155 stack16->ds = oldDS;
1156 countA = strlen(textA) + 1;
1161 ERR("no buffer ... please report\n");
1168 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1169 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1170 if(countW_new > es->buffer_size + 1)
1172 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1173 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1174 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1177 es->hloc32W = hloc32W_new;
1178 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1179 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1182 WARN("FAILED! Will synchronize partially\n");
1186 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1187 es->text = LocalLock(es->hloc32W);
1191 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1194 HANDLE16 oldDS = stack16->ds;
1195 stack16->ds = hInstance;
1196 LocalUnlock16(es->hloc16);
1197 stack16->ds = oldDS;
1200 LocalUnlock(es->hloc32A);
1203 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1208 /*********************************************************************
1213 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1216 /* Edit window might be already destroyed */
1217 if(!IsWindow(es->hwndSelf))
1219 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1223 if (!es->lock_count) {
1224 ERR("lock_count == 0 ... please report\n");
1228 ERR("es->text == 0 ... please report\n");
1232 if (force || (es->lock_count == 1)) {
1236 UINT countW = get_text_length(es) + 1;
1237 STACK16FRAME* stack16 = NULL;
1242 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1243 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1244 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1245 countA = LocalSize(es->hloc32A);
1246 if(countA_new > countA)
1249 UINT alloc_size = ROUND_TO_GROW(countA_new);
1250 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1251 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1254 es->hloc32A = hloc32A_new;
1255 countA = LocalSize(hloc32A_new);
1256 TRACE("Real new size %d bytes\n", countA);
1259 WARN("FAILED! Will synchronize partially\n");
1261 textA = LocalLock(es->hloc32A);
1265 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1267 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1268 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1270 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1271 oldDS = stack16->ds;
1272 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1274 countA = LocalSize16(es->hloc16);
1275 if(countA_new > countA)
1277 HLOCAL16 hloc16_new;
1278 UINT alloc_size = ROUND_TO_GROW(countA_new);
1279 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1280 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1283 es->hloc16 = hloc16_new;
1284 countA = LocalSize16(hloc16_new);
1285 TRACE("Real new size %d bytes\n", countA);
1288 WARN("FAILED! Will synchronize partially\n");
1290 textA = MapSL(LocalLock16(es->hloc16));
1295 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
1297 LocalUnlock16(es->hloc16);
1299 LocalUnlock(es->hloc32A);
1302 if (stack16) stack16->ds = oldDS;
1303 LocalUnlock(es->hloc32W);
1307 ERR("no buffer ... please report\n");
1315 /*********************************************************************
1319 * Try to fit size + 1 characters in the buffer.
1321 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1325 if (size <= es->buffer_size)
1328 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1330 /* Force edit to unlock it's buffer. es->text now NULL */
1331 EDIT_UnlockBuffer(es, TRUE);
1334 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1335 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1336 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1337 es->hloc32W = hNew32W;
1338 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1342 EDIT_LockBuffer(es);
1344 if (es->buffer_size < size) {
1345 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1346 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1349 TRACE("We now have %d+1\n", es->buffer_size);
1355 /*********************************************************************
1359 * Try to fit size + 1 bytes in the undo buffer.
1362 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1366 if (size <= es->undo_buffer_size)
1369 TRACE("trying to ReAlloc to %d+1\n", size);
1371 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1372 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1373 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1378 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1384 /*********************************************************************
1386 * EDIT_UpdateTextRegion
1389 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1391 if (es->flags & EF_UPDATE) {
1392 es->flags &= ~EF_UPDATE;
1393 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1395 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1399 /*********************************************************************
1404 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1406 if (es->flags & EF_UPDATE) {
1407 es->flags &= ~EF_UPDATE;
1408 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1410 InvalidateRect(es->hwndSelf, rc, bErase);
1413 /*********************************************************************
1415 * EDIT_SL_InvalidateText
1417 * Called from EDIT_InvalidateText().
1418 * Does the job for single-line controls only.
1421 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1426 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1427 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1428 EDIT_UpdateText(es, &rc, TRUE);
1432 static inline INT get_vertical_line_count(EDITSTATE *es)
1434 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1438 /*********************************************************************
1440 * EDIT_ML_InvalidateText
1442 * Called from EDIT_InvalidateText().
1443 * Does the job for multi-line controls only.
1446 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1448 INT vlc = get_vertical_line_count(es);
1449 INT sl = EDIT_EM_LineFromChar(es, start);
1450 INT el = EDIT_EM_LineFromChar(es, end);
1459 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1462 sc = start - EDIT_EM_LineIndex(es, sl);
1463 ec = end - EDIT_EM_LineIndex(es, el);
1464 if (sl < es->y_offset) {
1468 if (el > es->y_offset + vlc) {
1469 el = es->y_offset + vlc;
1470 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1472 GetClientRect(es->hwndSelf, &rc1);
1473 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1475 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1476 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1477 EDIT_UpdateText(es, &rcUpdate, TRUE);
1479 EDIT_GetLineRect(es, sl, sc,
1480 EDIT_EM_LineLength(es,
1481 EDIT_EM_LineIndex(es, sl)),
1483 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1484 EDIT_UpdateText(es, &rcUpdate, TRUE);
1485 for (l = sl + 1 ; l < el ; l++) {
1486 EDIT_GetLineRect(es, l, 0,
1487 EDIT_EM_LineLength(es,
1488 EDIT_EM_LineIndex(es, l)),
1490 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1491 EDIT_UpdateText(es, &rcUpdate, TRUE);
1493 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1494 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1495 EDIT_UpdateText(es, &rcUpdate, TRUE);
1500 /*********************************************************************
1502 * EDIT_InvalidateText
1504 * Invalidate the text from offset start up to, but not including,
1505 * offset end. Useful for (re)painting the selection.
1506 * Regions outside the linewidth are not invalidated.
1507 * end == -1 means end == TextLength.
1508 * start and end need not be ordered.
1511 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1517 end = get_text_length(es);
1525 if (es->style & ES_MULTILINE)
1526 EDIT_ML_InvalidateText(es, start, end);
1528 EDIT_SL_InvalidateText(es, start, end);
1532 /*********************************************************************
1536 * note: unlike the specs say: the order of start and end
1537 * _is_ preserved in Windows. (i.e. start can be > end)
1538 * In other words: this handler is OK
1541 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1543 UINT old_start = es->selection_start;
1544 UINT old_end = es->selection_end;
1545 UINT len = get_text_length(es);
1547 if (start == (UINT)-1) {
1548 start = es->selection_end;
1549 end = es->selection_end;
1551 start = min(start, len);
1552 end = min(end, len);
1554 es->selection_start = start;
1555 es->selection_end = end;
1557 es->flags |= EF_AFTER_WRAP;
1559 es->flags &= ~EF_AFTER_WRAP;
1560 /* Compute the necessary invalidation region. */
1561 /* Note that we don't need to invalidate regions which have
1562 * "never" been selected, or those which are "still" selected.
1563 * In fact, every time we hit a selection boundary, we can
1564 * *toggle* whether we need to invalidate. Thus we can optimize by
1565 * *sorting* the interval endpoints. Let's assume that we sort them
1567 * start <= end <= old_start <= old_end
1568 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1569 * in 5 comparisons; i.e. it is impossible to do better than the
1571 ORDER_UINT(end, old_end);
1572 ORDER_UINT(start, old_start);
1573 ORDER_UINT(old_start, old_end);
1574 ORDER_UINT(start, end);
1575 /* Note that at this point 'end' and 'old_start' are not in order, but
1576 * start is definitely the min. and old_end is definitely the max. */
1577 if (end != old_start)
1581 * ORDER_UINT32(end, old_start);
1582 * EDIT_InvalidateText(es, start, end);
1583 * EDIT_InvalidateText(es, old_start, old_end);
1584 * in place of the following if statement.
1585 * (That would complete the optimal five-comparison four-element sort.)
1587 if (old_start > end )
1589 EDIT_InvalidateText(es, start, end);
1590 EDIT_InvalidateText(es, old_start, old_end);
1594 EDIT_InvalidateText(es, start, old_start);
1595 EDIT_InvalidateText(es, end, old_end);
1598 else EDIT_InvalidateText(es, start, old_end);
1602 /*********************************************************************
1604 * EDIT_UpdateScrollInfo
1607 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1609 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1612 si.cbSize = sizeof(SCROLLINFO);
1613 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1615 si.nMax = es->line_count - 1;
1616 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1617 si.nPos = es->y_offset;
1618 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1619 si.nMin, si.nMax, si.nPage, si.nPos);
1620 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1623 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1626 si.cbSize = sizeof(SCROLLINFO);
1627 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1629 si.nMax = es->text_width - 1;
1630 si.nPage = es->format_rect.right - es->format_rect.left;
1631 si.nPos = es->x_offset;
1632 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1633 si.nMin, si.nMax, si.nPage, si.nPos);
1634 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1639 /*********************************************************************
1641 * EDIT_EM_LineScroll_internal
1643 * Version of EDIT_EM_LineScroll for internal use.
1644 * It doesn't refuse if ES_MULTILINE is set and assumes that
1645 * dx is in pixels, dy - in lines.
1648 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1651 INT x_offset_in_pixels;
1652 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1655 if (es->style & ES_MULTILINE)
1657 x_offset_in_pixels = es->x_offset;
1662 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1665 if (-dx > x_offset_in_pixels)
1666 dx = -x_offset_in_pixels;
1667 if (dx > es->text_width - x_offset_in_pixels)
1668 dx = es->text_width - x_offset_in_pixels;
1669 nyoff = max(0, es->y_offset + dy);
1670 if (nyoff >= es->line_count - lines_per_page)
1671 nyoff = max(0, es->line_count - lines_per_page);
1672 dy = (es->y_offset - nyoff) * es->line_height;
1677 es->y_offset = nyoff;
1678 if(es->style & ES_MULTILINE)
1681 es->x_offset += dx / es->char_width;
1683 GetClientRect(es->hwndSelf, &rc1);
1684 IntersectRect(&rc, &rc1, &es->format_rect);
1685 ScrollWindowEx(es->hwndSelf, -dx, dy,
1686 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1687 /* force scroll info update */
1688 EDIT_UpdateScrollInfo(es);
1690 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1691 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1692 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1693 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1697 /*********************************************************************
1701 * NOTE: dx is in average character widths, dy - in lines;
1704 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1706 if (!(es->style & ES_MULTILINE))
1709 dx *= es->char_width;
1710 return EDIT_EM_LineScroll_internal(es, dx, dy);
1714 /*********************************************************************
1719 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1723 if (!(es->style & ES_MULTILINE))
1724 return (LRESULT)FALSE;
1734 if (es->y_offset < es->line_count - 1)
1739 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1742 if (es->y_offset < es->line_count - 1)
1743 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1746 return (LRESULT)FALSE;
1749 INT vlc = get_vertical_line_count(es);
1750 /* check if we are going to move too far */
1751 if(es->y_offset + dy > es->line_count - vlc)
1752 dy = es->line_count - vlc - es->y_offset;
1754 /* Notification is done in EDIT_EM_LineScroll */
1756 EDIT_EM_LineScroll(es, 0, dy);
1758 return MAKELONG((INT16)dy, (BOOL16)TRUE);
1762 /*********************************************************************
1767 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1770 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1771 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1772 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1776 /*********************************************************************
1781 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1783 if (es->style & ES_MULTILINE) {
1787 INT cw = es->char_width;
1792 l = EDIT_EM_LineFromChar(es, es->selection_end);
1793 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1794 vlc = get_vertical_line_count(es);
1795 if (l >= es->y_offset + vlc)
1796 dy = l - vlc + 1 - es->y_offset;
1797 if (l < es->y_offset)
1798 dy = l - es->y_offset;
1799 ww = es->format_rect.right - es->format_rect.left;
1800 if (x < es->format_rect.left)
1801 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1802 if (x > es->format_rect.right)
1803 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1804 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1806 /* check if we are going to move too far */
1807 if(es->x_offset + dx + ww > es->text_width)
1808 dx = es->text_width - ww - es->x_offset;
1809 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1810 EDIT_EM_LineScroll_internal(es, dx, dy);
1817 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1818 format_width = es->format_rect.right - es->format_rect.left;
1819 if (x < es->format_rect.left) {
1820 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1823 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1824 } while ((x < goal) && es->x_offset);
1825 /* FIXME: use ScrollWindow() somehow to improve performance */
1826 EDIT_UpdateText(es, NULL, TRUE);
1827 } else if (x > es->format_rect.right) {
1829 INT len = get_text_length(es);
1830 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1833 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1834 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1835 } while ((x > goal) && (x_last > es->format_rect.right));
1836 /* FIXME: use ScrollWindow() somehow to improve performance */
1837 EDIT_UpdateText(es, NULL, TRUE);
1841 if(es->flags & EF_FOCUSED)
1842 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1846 /*********************************************************************
1851 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1853 INT e = es->selection_end;
1857 if ((es->style & ES_MULTILINE) && e &&
1858 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1860 if (e && (es->text[e - 1] == '\r'))
1864 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1865 EDIT_EM_ScrollCaret(es);
1869 /*********************************************************************
1873 * Only for multi line controls
1874 * Move the caret one line down, on a column with the nearest
1875 * x coordinate on the screen (might be a different column).
1878 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1880 INT s = es->selection_start;
1881 INT e = es->selection_end;
1882 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1883 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1884 INT x = (short)LOWORD(pos);
1885 INT y = (short)HIWORD(pos);
1887 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1890 EDIT_EM_SetSel(es, s, e, after_wrap);
1891 EDIT_EM_ScrollCaret(es);
1895 /*********************************************************************
1900 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1902 BOOL after_wrap = FALSE;
1905 /* Pass a high value in x to make sure of receiving the end of the line */
1906 if (!ctrl && (es->style & ES_MULTILINE))
1907 e = EDIT_CharFromPos(es, 0x3fffffff,
1908 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1910 e = get_text_length(es);
1911 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1912 EDIT_EM_ScrollCaret(es);
1916 /*********************************************************************
1921 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1923 INT e = es->selection_end;
1927 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1928 if (es->text[e] == '\n')
1930 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1934 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1935 EDIT_EM_ScrollCaret(es);
1939 /*********************************************************************
1943 * Home key: move to beginning of line.
1946 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1950 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1951 if (!ctrl && (es->style & ES_MULTILINE))
1952 e = EDIT_CharFromPos(es, -es->x_offset,
1953 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1956 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1957 EDIT_EM_ScrollCaret(es);
1961 /*********************************************************************
1963 * EDIT_MovePageDown_ML
1965 * Only for multi line controls
1966 * Move the caret one page down, on a column with the nearest
1967 * x coordinate on the screen (might be a different column).
1970 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1972 INT s = es->selection_start;
1973 INT e = es->selection_end;
1974 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1975 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1976 INT x = (short)LOWORD(pos);
1977 INT y = (short)HIWORD(pos);
1979 e = EDIT_CharFromPos(es, x,
1980 y + (es->format_rect.bottom - es->format_rect.top),
1984 EDIT_EM_SetSel(es, s, e, after_wrap);
1985 EDIT_EM_ScrollCaret(es);
1989 /*********************************************************************
1991 * EDIT_MovePageUp_ML
1993 * Only for multi line controls
1994 * Move the caret one page up, on a column with the nearest
1995 * x coordinate on the screen (might be a different column).
1998 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2000 INT s = es->selection_start;
2001 INT e = es->selection_end;
2002 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2003 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2004 INT x = (short)LOWORD(pos);
2005 INT y = (short)HIWORD(pos);
2007 e = EDIT_CharFromPos(es, x,
2008 y - (es->format_rect.bottom - es->format_rect.top),
2012 EDIT_EM_SetSel(es, s, e, after_wrap);
2013 EDIT_EM_ScrollCaret(es);
2017 /*********************************************************************
2021 * Only for multi line controls
2022 * Move the caret one line up, on a column with the nearest
2023 * x coordinate on the screen (might be a different column).
2026 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2028 INT s = es->selection_start;
2029 INT e = es->selection_end;
2030 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2031 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2032 INT x = (short)LOWORD(pos);
2033 INT y = (short)HIWORD(pos);
2035 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2038 EDIT_EM_SetSel(es, s, e, after_wrap);
2039 EDIT_EM_ScrollCaret(es);
2043 /*********************************************************************
2045 * EDIT_MoveWordBackward
2048 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2050 INT s = es->selection_start;
2051 INT e = es->selection_end;
2056 l = EDIT_EM_LineFromChar(es, e);
2057 ll = EDIT_EM_LineLength(es, e);
2058 li = EDIT_EM_LineIndex(es, l);
2061 li = EDIT_EM_LineIndex(es, l - 1);
2062 e = li + EDIT_EM_LineLength(es, li);
2065 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2069 EDIT_EM_SetSel(es, s, e, FALSE);
2070 EDIT_EM_ScrollCaret(es);
2074 /*********************************************************************
2076 * EDIT_MoveWordForward
2079 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2081 INT s = es->selection_start;
2082 INT e = es->selection_end;
2087 l = EDIT_EM_LineFromChar(es, e);
2088 ll = EDIT_EM_LineLength(es, e);
2089 li = EDIT_EM_LineIndex(es, l);
2091 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2092 e = EDIT_EM_LineIndex(es, l + 1);
2094 e = li + EDIT_CallWordBreakProc(es,
2095 li, e - li + 1, ll, WB_RIGHT);
2099 EDIT_EM_SetSel(es, s, e, FALSE);
2100 EDIT_EM_ScrollCaret(es);
2104 /*********************************************************************
2109 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2113 LOGFONTW underline_font;
2114 HFONT hUnderline = 0;
2123 BkMode = GetBkMode(dc);
2124 BkColor = GetBkColor(dc);
2125 TextColor = GetTextColor(dc);
2127 if (es->composition_len == 0)
2129 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2130 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2131 SetBkMode( dc, OPAQUE);
2135 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2136 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2137 underline_font.lfUnderline = TRUE;
2138 hUnderline = CreateFontIndirectW(&underline_font);
2139 old_font = SelectObject(dc,hUnderline);
2142 li = EDIT_EM_LineIndex(es, line);
2143 if (es->style & ES_MULTILINE) {
2144 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2145 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2147 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2148 TextOutW(dc, x, y, text + li + col, count);
2149 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2151 if (es->style & ES_PASSWORD)
2152 HeapFree(GetProcessHeap(), 0, text);
2155 if (es->composition_len == 0)
2157 SetBkColor(dc, BkColor);
2158 SetTextColor(dc, TextColor);
2159 SetBkMode( dc, BkMode);
2164 SelectObject(dc,old_font);
2166 DeleteObject(hUnderline);
2173 /*********************************************************************
2178 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2180 INT s = es->selection_start;
2181 INT e = es->selection_end;
2188 if (es->style & ES_MULTILINE) {
2189 INT vlc = get_vertical_line_count(es);
2191 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2196 TRACE("line=%d\n", line);
2198 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2199 x = (short)LOWORD(pos);
2200 y = (short)HIWORD(pos);
2201 li = EDIT_EM_LineIndex(es, line);
2202 ll = EDIT_EM_LineLength(es, li);
2203 s = min(es->selection_start, es->selection_end);
2204 e = max(es->selection_start, es->selection_end);
2205 s = min(li + ll, max(li, s));
2206 e = min(li + ll, max(li, e));
2207 if (rev && (s != e) &&
2208 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2209 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2210 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2211 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2213 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2217 /*********************************************************************
2219 * EDIT_AdjustFormatRect
2221 * Adjusts the format rectangle for the current font and the
2222 * current client rectangle.
2225 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2229 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2230 if (es->style & ES_MULTILINE)
2232 INT fw, vlc, max_x_offset, max_y_offset;
2234 vlc = get_vertical_line_count(es);
2235 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2237 /* correct es->x_offset */
2238 fw = es->format_rect.right - es->format_rect.left;
2239 max_x_offset = es->text_width - fw;
2240 if(max_x_offset < 0) max_x_offset = 0;
2241 if(es->x_offset > max_x_offset)
2242 es->x_offset = max_x_offset;
2244 /* correct es->y_offset */
2245 max_y_offset = es->line_count - vlc;
2246 if(max_y_offset < 0) max_y_offset = 0;
2247 if(es->y_offset > max_y_offset)
2248 es->y_offset = max_y_offset;
2250 /* force scroll info update */
2251 EDIT_UpdateScrollInfo(es);
2254 /* Windows doesn't care to fix text placement for SL controls */
2255 es->format_rect.bottom = es->format_rect.top + es->line_height;
2257 /* Always stay within the client area */
2258 GetClientRect(es->hwndSelf, &ClientRect);
2259 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2261 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2262 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2264 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2268 /*********************************************************************
2272 * note: this is not (exactly) the handler called on EM_SETRECTNP
2273 * it is also used to set the rect of a single line control
2276 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2280 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2282 CopyRect(&es->format_rect, rc);
2284 if (ExStyle & WS_EX_CLIENTEDGE) {
2285 es->format_rect.left++;
2286 es->format_rect.right--;
2288 if (es->format_rect.bottom - es->format_rect.top
2289 >= es->line_height + 2)
2291 es->format_rect.top++;
2292 es->format_rect.bottom--;
2295 else if (es->style & WS_BORDER) {
2296 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2297 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2298 es->format_rect.left += bw;
2299 es->format_rect.right -= bw;
2300 if (es->format_rect.bottom - es->format_rect.top
2301 >= es->line_height + 2 * bh)
2303 es->format_rect.top += bh;
2304 es->format_rect.bottom -= bh;
2308 es->format_rect.left += es->left_margin;
2309 es->format_rect.right -= es->right_margin;
2310 EDIT_AdjustFormatRect(es);
2314 /*********************************************************************
2318 * returns line number (not index) in high-order word of result.
2319 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2320 * to Richedit, not to the edit control. Original documentation is valid.
2321 * FIXME: do the specs mean to return -1 if outside client area or
2322 * if outside formatting rectangle ???
2325 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2333 GetClientRect(es->hwndSelf, &rc);
2334 if (!PtInRect(&rc, pt))
2337 index = EDIT_CharFromPos(es, x, y, NULL);
2338 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2342 /*********************************************************************
2346 * Enable or disable soft breaks.
2348 * This means: insert or remove the soft linebreak character (\r\r\n).
2349 * Take care to check if the text still fits the buffer after insertion.
2350 * If not, notify with EN_ERRSPACE.
2353 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2355 es->flags &= ~EF_USE_SOFTBRK;
2357 es->flags |= EF_USE_SOFTBRK;
2358 FIXME("soft break enabled, not implemented\n");
2364 /*********************************************************************
2368 * Hopefully this won't fire back at us.
2369 * We always start with a fixed buffer in the local heap.
2370 * Despite of the documentation says that the local heap is used
2371 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2372 * buffer on the local heap.
2375 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2379 if (!(es->style & ES_MULTILINE))
2383 hLocal = es->hloc32W;
2389 UINT countA, alloc_size;
2390 TRACE("Allocating 32-bit ANSI alias buffer\n");
2391 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2392 alloc_size = ROUND_TO_GROW(countA);
2393 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2395 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2398 textA = LocalLock(es->hloc32A);
2399 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2400 LocalUnlock(es->hloc32A);
2402 hLocal = es->hloc32A;
2405 es->flags |= EF_APP_HAS_HANDLE;
2406 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2411 /*********************************************************************
2415 * Hopefully this won't fire back at us.
2416 * We always start with a buffer in 32 bit linear memory.
2417 * However, with this message a 16 bit application requests
2418 * a handle of 16 bit local heap memory, where it expects to find
2420 * It's a pitty that from this moment on we have to use this
2421 * local heap, because applications may rely on the handle
2424 * In this function we'll try to switch to local heap.
2426 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2429 UINT countA, alloc_size;
2430 STACK16FRAME* stack16;
2433 if (!(es->style & ES_MULTILINE))
2439 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2440 oldDS = stack16->ds;
2441 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2443 if (!LocalHeapSize16()) {
2445 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2446 ERR("could not initialize local heap\n");
2449 TRACE("local heap initialized\n");
2452 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2453 alloc_size = ROUND_TO_GROW(countA);
2455 TRACE("Allocating 16-bit ANSI alias buffer\n");
2456 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2457 ERR("could not allocate new 16 bit buffer\n");
2461 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2462 ERR("could not lock new 16 bit buffer\n");
2463 LocalFree16(es->hloc16);
2468 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2469 LocalUnlock16(es->hloc16);
2470 es->flags |= EF_APP_HAS_HANDLE;
2472 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2475 stack16->ds = oldDS;
2480 /*********************************************************************
2485 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2488 INT line_len, dst_len;
2491 if (es->style & ES_MULTILINE) {
2492 if (line >= es->line_count)
2496 i = EDIT_EM_LineIndex(es, line);
2498 line_len = EDIT_EM_LineLength(es, i);
2499 dst_len = *(WORD *)dst;
2502 if(dst_len <= line_len)
2504 memcpy(dst, src, dst_len * sizeof(WCHAR));
2507 else /* Append 0 if enough space */
2509 memcpy(dst, src, line_len * sizeof(WCHAR));
2516 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2517 if(!ret && line_len) /* Insufficient buffer size */
2519 if(ret < dst_len) /* Append 0 if enough space */
2520 ((LPSTR)dst)[ret] = 0;
2526 /*********************************************************************
2531 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2533 UINT s = es->selection_start;
2534 UINT e = es->selection_end;
2541 return MAKELONG(s, e);
2545 /*********************************************************************
2549 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2552 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2554 UINT strl = strlenW(lpsz_replace);
2555 UINT tl = get_text_length(es);
2566 TRACE("%s, can_undo %d, send_update %d\n",
2567 debugstr_w(lpsz_replace), can_undo, send_update);
2569 s = es->selection_start;
2570 e = es->selection_end;
2572 if ((s == e) && !strl)
2577 size = tl - (e - s) + strl;
2581 /* Issue the EN_MAXTEXT notification and continue with replacing text
2582 * such that buffer limit is honored. */
2583 if ((honor_limit) && (size > es->buffer_limit)) {
2584 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2585 /* Buffer limit can be smaller than the actual length of text in combobox */
2586 if (es->buffer_limit < (tl - (e-s)))
2589 strl = es->buffer_limit - (tl - (e-s));
2592 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2596 /* there is something to be deleted */
2597 TRACE("deleting stuff.\n");
2599 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2601 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2602 buf[bufl] = 0; /* ensure 0 termination */
2604 strcpyW(es->text + s, es->text + e);
2605 text_buffer_changed(es);
2608 /* there is an insertion */
2609 tl = get_text_length(es);
2610 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));
2611 for (p = es->text + tl ; p >= es->text + s ; p--)
2613 for (i = 0 , p = es->text + s ; i < strl ; i++)
2614 p[i] = lpsz_replace[i];
2615 if(es->style & ES_UPPERCASE)
2616 CharUpperBuffW(p, strl);
2617 else if(es->style & ES_LOWERCASE)
2618 CharLowerBuffW(p, strl);
2619 text_buffer_changed(es);
2621 if (es->style & ES_MULTILINE)
2623 INT st = min(es->selection_start, es->selection_end);
2624 INT vlc = get_vertical_line_count(es);
2626 hrgn = CreateRectRgn(0, 0, 0, 0);
2627 EDIT_BuildLineDefs_ML(es, st, st + strl,
2628 strl - abs(es->selection_end - es->selection_start), hrgn);
2629 /* if text is too long undo all changes */
2630 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2632 strcpyW(es->text + e, es->text + e + strl);
2634 for (i = 0 , p = es->text ; i < e - s ; i++)
2636 text_buffer_changed(es);
2637 EDIT_BuildLineDefs_ML(es, s, e,
2638 abs(es->selection_end - es->selection_start) - strl, hrgn);
2641 hrgn = CreateRectRgn(0, 0, 0, 0);
2642 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2646 INT fw = es->format_rect.right - es->format_rect.left;
2647 EDIT_CalcLineWidth_SL(es);
2648 /* remove chars that don't fit */
2649 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2650 while ((es->text_width > fw) && s + strl >= s) {
2651 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2653 EDIT_CalcLineWidth_SL(es);
2655 text_buffer_changed(es);
2656 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2662 utl = strlenW(es->undo_text);
2663 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2664 /* undo-buffer is extended to the right */
2665 EDIT_MakeUndoFit(es, utl + e - s);
2666 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2667 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2668 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2669 /* undo-buffer is extended to the left */
2670 EDIT_MakeUndoFit(es, utl + e - s);
2671 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2673 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2675 es->undo_position = s;
2677 /* new undo-buffer */
2678 EDIT_MakeUndoFit(es, e - s);
2679 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2680 es->undo_text[e - s] = 0; /* ensure 0 termination */
2681 es->undo_position = s;
2683 /* any deletion makes the old insertion-undo invalid */
2684 es->undo_insert_count = 0;
2686 EDIT_EM_EmptyUndoBuffer(es);
2690 if ((s == es->undo_position) ||
2691 ((es->undo_insert_count) &&
2692 (s == es->undo_position + es->undo_insert_count)))
2694 * insertion is new and at delete position or
2695 * an extension to either left or right
2697 es->undo_insert_count += strl;
2699 /* new insertion undo */
2700 es->undo_position = s;
2701 es->undo_insert_count = strl;
2702 /* new insertion makes old delete-buffer invalid */
2703 *es->undo_text = '\0';
2706 EDIT_EM_EmptyUndoBuffer(es);
2710 HeapFree(GetProcessHeap(), 0, buf);
2714 /* If text has been deleted and we're right or center aligned then scroll rightward */
2715 if (es->style & (ES_RIGHT | ES_CENTER))
2717 INT delta = strl - abs(es->selection_end - es->selection_start);
2719 if (delta < 0 && es->x_offset)
2721 if (abs(delta) > es->x_offset)
2724 es->x_offset += delta;
2728 EDIT_EM_SetSel(es, s, s, FALSE);
2729 es->flags |= EF_MODIFIED;
2730 if (send_update) es->flags |= EF_UPDATE;
2733 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2737 EDIT_UpdateText(es, NULL, TRUE);
2739 EDIT_EM_ScrollCaret(es);
2741 /* force scroll info update */
2742 EDIT_UpdateScrollInfo(es);
2745 if(send_update || (es->flags & EF_UPDATE))
2747 es->flags &= ~EF_UPDATE;
2748 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2753 /*********************************************************************
2757 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2760 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2762 if (!(es->style & ES_MULTILINE))
2766 WARN("called with NULL handle\n");
2770 EDIT_UnlockBuffer(es, TRUE);
2774 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2775 HANDLE16 oldDS = stack16->ds;
2777 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2778 LocalFree16(es->hloc16);
2779 stack16->ds = oldDS;
2787 LocalFree(es->hloc32A);
2799 countA = LocalSize(hloc);
2800 textA = LocalLock(hloc);
2801 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2802 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2804 ERR("Could not allocate new unicode buffer\n");
2807 textW = LocalLock(hloc32W_new);
2808 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2809 LocalUnlock(hloc32W_new);
2813 LocalFree(es->hloc32W);
2815 es->hloc32W = hloc32W_new;
2819 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2821 es->flags |= EF_APP_HAS_HANDLE;
2822 EDIT_LockBuffer(es);
2824 es->x_offset = es->y_offset = 0;
2825 es->selection_start = es->selection_end = 0;
2826 EDIT_EM_EmptyUndoBuffer(es);
2827 es->flags &= ~EF_MODIFIED;
2828 es->flags &= ~EF_UPDATE;
2829 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2830 EDIT_UpdateText(es, NULL, TRUE);
2831 EDIT_EM_ScrollCaret(es);
2832 /* force scroll info update */
2833 EDIT_UpdateScrollInfo(es);
2837 /*********************************************************************
2841 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2844 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
2846 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2847 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2848 HANDLE16 oldDS = stack16->ds;
2854 if (!(es->style & ES_MULTILINE))
2858 WARN("called with NULL handle\n");
2862 EDIT_UnlockBuffer(es, TRUE);
2866 LocalFree(es->hloc32A);
2870 stack16->ds = hInstance;
2871 countA = LocalSize16(hloc);
2872 textA = MapSL(LocalLock16(hloc));
2873 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2874 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2876 ERR("Could not allocate new unicode buffer\n");
2879 textW = LocalLock(hloc32W_new);
2880 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2881 LocalUnlock(hloc32W_new);
2882 LocalUnlock16(hloc);
2883 stack16->ds = oldDS;
2886 LocalFree(es->hloc32W);
2888 es->hloc32W = hloc32W_new;
2891 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2893 es->flags |= EF_APP_HAS_HANDLE;
2894 EDIT_LockBuffer(es);
2896 es->x_offset = es->y_offset = 0;
2897 es->selection_start = es->selection_end = 0;
2898 EDIT_EM_EmptyUndoBuffer(es);
2899 es->flags &= ~EF_MODIFIED;
2900 es->flags &= ~EF_UPDATE;
2901 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2902 EDIT_UpdateText(es, NULL, TRUE);
2903 EDIT_EM_ScrollCaret(es);
2904 /* force scroll info update */
2905 EDIT_UpdateScrollInfo(es);
2909 /*********************************************************************
2913 * NOTE: this version currently implements WinNT limits
2916 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2918 if (!limit) limit = ~0u;
2919 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2920 es->buffer_limit = limit;
2924 /*********************************************************************
2928 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2929 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2930 * margin according to the textmetrics of the current font.
2932 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2933 * of the char's width as the margin, but this is not how Windows handles this.
2934 * For all other fonts Windows sets the margins to zero.
2936 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2937 * edit control is equal to or larger than a certain size.
2938 * Interestingly if one subtracts both the left and right margins from
2939 * this size one always seems to get an even number. The extents of
2940 * the (four character) string "'**'" match this quite closely, so
2941 * we'll use this until we come up with a better idea.
2943 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2945 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2948 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2949 return sz.cx + left + right;
2952 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2953 WORD left, WORD right, BOOL repaint)
2956 INT default_left_margin = 0; /* in pixels */
2957 INT default_right_margin = 0; /* in pixels */
2959 /* Set the default margins depending on the font */
2960 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2961 HDC dc = GetDC(es->hwndSelf);
2962 HFONT old_font = SelectObject(dc, es->font);
2963 GetTextMetricsW(dc, &tm);
2964 /* The default margins are only non zero for TrueType or Vector fonts */
2965 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2968 /* This must be calculated more exactly! But how? */
2969 default_left_margin = tm.tmAveCharWidth / 2;
2970 default_right_margin = tm.tmAveCharWidth / 2;
2971 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2972 GetClientRect(es->hwndSelf, &rc);
2973 if(rc.right - rc.left < min_size) {
2974 default_left_margin = es->left_margin;
2975 default_right_margin = es->right_margin;
2978 SelectObject(dc, old_font);
2979 ReleaseDC(es->hwndSelf, dc);
2982 if (action & EC_LEFTMARGIN) {
2983 es->format_rect.left -= es->left_margin;
2984 if (left != EC_USEFONTINFO)
2985 es->left_margin = left;
2987 es->left_margin = default_left_margin;
2988 es->format_rect.left += es->left_margin;
2991 if (action & EC_RIGHTMARGIN) {
2992 es->format_rect.right += es->right_margin;
2993 if (right != EC_USEFONTINFO)
2994 es->right_margin = right;
2996 es->right_margin = default_right_margin;
2997 es->format_rect.right -= es->right_margin;
3000 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3001 EDIT_AdjustFormatRect(es);
3002 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3005 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3009 /*********************************************************************
3011 * EM_SETPASSWORDCHAR
3014 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3018 if (es->style & ES_MULTILINE)
3021 if (es->password_char == c)
3024 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3025 es->password_char = c;
3027 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3028 es->style |= ES_PASSWORD;
3030 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3031 es->style &= ~ES_PASSWORD;
3033 EDIT_UpdateText(es, NULL, TRUE);
3037 /*********************************************************************
3042 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3044 if (!(es->style & ES_MULTILINE))
3046 HeapFree(GetProcessHeap(), 0, es->tabs);
3047 es->tabs_count = count;
3051 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3052 memcpy(es->tabs, tabs, count * sizeof(INT));
3058 /*********************************************************************
3063 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs)
3065 if (!(es->style & ES_MULTILINE))
3067 HeapFree(GetProcessHeap(), 0, es->tabs);
3068 es->tabs_count = count;
3073 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3074 for (i = 0 ; i < count ; i++)
3075 es->tabs[i] = *tabs++;
3081 /*********************************************************************
3083 * EM_SETWORDBREAKPROC
3086 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3088 if (es->word_break_proc == wbp)
3091 es->word_break_proc = wbp;
3092 es->word_break_proc16 = NULL;
3094 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3095 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3096 EDIT_UpdateText(es, NULL, TRUE);
3101 /*********************************************************************
3103 * EM_SETWORDBREAKPROC16
3106 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3108 if (es->word_break_proc16 == wbp)
3111 es->word_break_proc = NULL;
3112 es->word_break_proc16 = wbp;
3113 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3114 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3115 EDIT_UpdateText(es, NULL, TRUE);
3120 /*********************************************************************
3125 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3130 /* As per MSDN spec, for a single-line edit control,
3131 the return value is always TRUE */
3132 if( es->style & ES_READONLY )
3133 return !(es->style & ES_MULTILINE);
3135 ulength = strlenW(es->undo_text);
3137 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3139 strcpyW(utext, es->undo_text);
3141 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3142 es->undo_insert_count, debugstr_w(utext));
3144 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3145 EDIT_EM_EmptyUndoBuffer(es);
3146 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3147 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3148 /* send the notification after the selection start and end are set */
3149 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3150 EDIT_EM_ScrollCaret(es);
3151 HeapFree(GetProcessHeap(), 0, utext);
3153 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3154 es->undo_insert_count, debugstr_w(es->undo_text));
3159 /* Helper function for WM_CHAR
3161 * According to an MSDN blog article titled "Just because you're a control
3162 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3163 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3164 * a dialog box or not.
3166 static BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3173 pParent = WIN_GetPtr(es->hwndParent);
3174 if (pParent && pParent != WND_OTHER_PROCESS && pParent != WND_DESKTOP)
3176 if (pParent->flags & WIN_ISDIALOG)
3178 WIN_ReleasePtr(pParent);
3185 /*********************************************************************
3190 static void EDIT_WM_Paste(EDITSTATE *es)
3195 /* Protect read-only edit control from modification */
3196 if(es->style & ES_READONLY)
3199 OpenClipboard(es->hwndSelf);
3200 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3201 src = GlobalLock(hsrc);
3202 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
3205 else if (es->style & ES_PASSWORD) {
3206 /* clear selected text in password edit box even with empty clipboard */
3207 const WCHAR empty_strW[] = { 0 };
3208 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
3214 /*********************************************************************
3219 static void EDIT_WM_Copy(EDITSTATE *es)
3221 INT s = min(es->selection_start, es->selection_end);
3222 INT e = max(es->selection_start, es->selection_end);
3230 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3231 dst = GlobalLock(hdst);
3232 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3233 dst[len] = 0; /* ensure 0 termination */
3234 TRACE("%s\n", debugstr_w(dst));
3236 OpenClipboard(es->hwndSelf);
3238 SetClipboardData(CF_UNICODETEXT, hdst);
3243 /*********************************************************************
3248 static inline void EDIT_WM_Clear(EDITSTATE *es)
3250 static const WCHAR empty_stringW[] = {0};
3252 /* Protect read-only edit control from modification */
3253 if(es->style & ES_READONLY)
3256 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
3260 /*********************************************************************
3265 static inline void EDIT_WM_Cut(EDITSTATE *es)
3272 /*********************************************************************
3277 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3281 control = GetKeyState(VK_CONTROL) & 0x8000;
3285 /* If it's not a multiline edit box, it would be ignored below.
3286 * For multiline edit without ES_WANTRETURN, we have to make a
3289 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3290 if (EDIT_IsInsideDialog(es))
3293 if (es->style & ES_MULTILINE) {
3294 if (es->style & ES_READONLY) {
3295 EDIT_MoveHome(es, FALSE, FALSE);
3296 EDIT_MoveDown_ML(es, FALSE);
3298 static const WCHAR cr_lfW[] = {'\r','\n',0};
3299 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3304 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3306 static const WCHAR tabW[] = {'\t',0};
3307 if (EDIT_IsInsideDialog(es))
3309 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3313 if (!(es->style & ES_READONLY) && !control) {
3314 if (es->selection_start != es->selection_end)
3317 /* delete character left of caret */
3318 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3319 EDIT_MoveBackward(es, TRUE);
3325 if (!(es->style & ES_PASSWORD))
3326 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3329 if (!(es->style & ES_READONLY))
3330 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3333 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3334 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3337 if (!(es->style & ES_READONLY))
3338 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3342 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3343 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3346 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3350 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3358 /*********************************************************************
3363 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3365 if (code || control)
3370 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3373 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3376 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3379 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3382 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3385 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3386 EDIT_EM_ScrollCaret(es);
3389 ERR("unknown menu item, please report\n");
3395 /*********************************************************************
3399 * Note: the resource files resource/sysres_??.rc cannot define a
3400 * single popup menu. Hence we use a (dummy) menubar
3401 * containing the single popup menu as its first item.
3403 * FIXME: the message identifiers have been chosen arbitrarily,
3404 * hence we use MF_BYPOSITION.
3405 * We might as well use the "real" values (anybody knows ?)
3406 * The menu definition is in resources/sysres_??.rc.
3407 * Once these are OK, we better use MF_BYCOMMAND here
3408 * (as we do in EDIT_WM_Command()).
3411 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3413 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3414 HMENU popup = GetSubMenu(menu, 0);
3415 UINT start = es->selection_start;
3416 UINT end = es->selection_end;
3418 ORDER_UINT(start, end);
3421 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3423 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3425 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3427 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3429 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3431 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3433 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3436 /* Windows places the menu at the edit's center in this case */
3437 GetClientRect(es->hwndSelf, &rc);
3438 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3439 x = rc.left + (rc.right - rc.left) / 2;
3440 y = rc.top + (rc.bottom - rc.top) / 2;
3443 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3448 /*********************************************************************
3453 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3455 if(!count) return 0;
3459 lstrcpynW(dst, es->text, count);
3460 return strlenW(dst);
3464 LPSTR textA = (LPSTR)dst;
3465 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3466 textA[count - 1] = 0; /* ensure 0 termination */
3467 return strlen(textA);
3471 /*********************************************************************
3476 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3478 HWND hLBox = es->hwndListBox;
3486 hCombo = GetParent(es->hwndSelf);
3490 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3492 if (key == VK_UP || key == VK_DOWN)
3494 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3497 if (msg == WM_KEYDOWN || nEUI)
3498 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3504 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3506 /* make sure ComboLBox pops up */
3507 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3512 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3515 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3517 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3519 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
3524 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3530 /*********************************************************************
3534 * Handling of special keys that don't produce a WM_CHAR
3535 * (i.e. non-printable keys) & Backspace & Delete
3538 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3543 if (GetKeyState(VK_MENU) & 0x8000)
3546 shift = GetKeyState(VK_SHIFT) & 0x8000;
3547 control = GetKeyState(VK_CONTROL) & 0x8000;
3552 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3557 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3558 EDIT_MoveUp_ML(es, shift);
3561 EDIT_MoveWordBackward(es, shift);
3563 EDIT_MoveBackward(es, shift);
3566 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3570 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3571 EDIT_MoveDown_ML(es, shift);
3573 EDIT_MoveWordForward(es, shift);
3575 EDIT_MoveForward(es, shift);
3578 EDIT_MoveHome(es, shift, control);
3581 EDIT_MoveEnd(es, shift, control);
3584 if (es->style & ES_MULTILINE)
3585 EDIT_MovePageUp_ML(es, shift);
3587 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3590 if (es->style & ES_MULTILINE)
3591 EDIT_MovePageDown_ML(es, shift);
3593 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3596 if (!(es->style & ES_READONLY) && !(shift && control)) {
3597 if (es->selection_start != es->selection_end) {
3604 /* delete character left of caret */
3605 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3606 EDIT_MoveBackward(es, TRUE);
3608 } else if (control) {
3609 /* delete to end of line */
3610 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3611 EDIT_MoveEnd(es, TRUE, FALSE);
3614 /* delete character right of caret */
3615 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3616 EDIT_MoveForward(es, TRUE);
3624 if (!(es->style & ES_READONLY))
3630 /* If the edit doesn't want the return send a message to the default object */
3631 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3636 if (!EDIT_IsInsideDialog(es)) return 1;
3638 hwndParent = GetParent(es->hwndSelf);
3639 dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
3640 if (HIWORD(dw) == DC_HASDEFID)
3642 SendMessageW( hwndParent, WM_COMMAND,
3643 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
3644 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
3647 SendMessageW( hwndParent, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndParent, IDOK ) );
3651 if (!(es->style & ES_MULTILINE))
3652 SendMessageW(GetParent(es->hwndSelf), WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( GetParent(es->hwndSelf), IDCANCEL ) );
3655 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3662 /*********************************************************************
3667 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3669 es->flags &= ~EF_FOCUSED;
3671 if(!(es->style & ES_NOHIDESEL))
3672 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3673 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3678 /*********************************************************************
3682 * The caret position has been set on the WM_LBUTTONDOWN message
3685 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3688 INT e = es->selection_end;
3693 es->bCaptureState = TRUE;
3694 SetCapture(es->hwndSelf);
3696 l = EDIT_EM_LineFromChar(es, e);
3697 li = EDIT_EM_LineIndex(es, l);
3698 ll = EDIT_EM_LineLength(es, e);
3699 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3700 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3701 EDIT_EM_SetSel(es, s, e, FALSE);
3702 EDIT_EM_ScrollCaret(es);
3703 es->region_posx = es->region_posy = 0;
3704 SetTimer(es->hwndSelf, 0, 100, NULL);
3709 /*********************************************************************
3714 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3719 es->bCaptureState = TRUE;
3720 SetCapture(es->hwndSelf);
3721 EDIT_ConfinePoint(es, &x, &y);
3722 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3723 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3724 EDIT_EM_ScrollCaret(es);
3725 es->region_posx = es->region_posy = 0;
3726 SetTimer(es->hwndSelf, 0, 100, NULL);
3728 if (!(es->flags & EF_FOCUSED))
3729 SetFocus(es->hwndSelf);
3735 /*********************************************************************
3740 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3742 if (es->bCaptureState) {
3743 KillTimer(es->hwndSelf, 0);
3744 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3746 es->bCaptureState = FALSE;
3751 /*********************************************************************
3756 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3758 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3763 /*********************************************************************
3768 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3774 /* If the mouse has been captured by process other than the edit control itself,
3775 * the windows edit controls will not select the strings with mouse move.
3777 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3781 * FIXME: gotta do some scrolling if outside client
3782 * area. Maybe reset the timer ?
3785 EDIT_ConfinePoint(es, &x, &y);
3786 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3787 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3788 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3789 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3790 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3795 /*********************************************************************
3800 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3813 BOOL rev = es->bEnableState &&
3814 ((es->flags & EF_FOCUSED) ||
3815 (es->style & ES_NOHIDESEL));
3816 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3818 GetClientRect(es->hwndSelf, &rcClient);
3820 /* get the background brush */
3821 brush = EDIT_NotifyCtlColor(es, dc);
3823 /* paint the border and the background */
3824 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3826 if(es->style & WS_BORDER) {
3827 bw = GetSystemMetrics(SM_CXBORDER);
3828 bh = GetSystemMetrics(SM_CYBORDER);
3830 if(es->style & ES_MULTILINE) {
3831 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3832 if(es->style & WS_VSCROLL) rc.right+=bw;
3835 /* Draw the frame. Same code as in nonclient.c */
3836 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3837 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3838 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3839 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3840 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3841 SelectObject(dc, old_brush);
3843 /* Keep the border clean */
3844 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3845 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3848 GetClipBox(dc, &rc);
3849 FillRect(dc, &rc, brush);
3851 IntersectClipRect(dc, es->format_rect.left,
3852 es->format_rect.top,
3853 es->format_rect.right,
3854 es->format_rect.bottom);
3855 if (es->style & ES_MULTILINE) {
3857 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3860 old_font = SelectObject(dc, es->font);
3862 if (!es->bEnableState)
3863 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3864 GetClipBox(dc, &rcRgn);
3865 if (es->style & ES_MULTILINE) {
3866 INT vlc = get_vertical_line_count(es);
3867 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3868 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3869 if (IntersectRect(&rc, &rcRgn, &rcLine))
3870 EDIT_PaintLine(es, dc, i, rev);
3873 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3874 if (IntersectRect(&rc, &rcRgn, &rcLine))
3875 EDIT_PaintLine(es, dc, 0, rev);
3878 SelectObject(dc, old_font);
3881 EndPaint(es->hwndSelf, &ps);
3885 /*********************************************************************
3890 static void EDIT_WM_SetFocus(EDITSTATE *es)
3892 es->flags |= EF_FOCUSED;
3894 if (!(es->style & ES_NOHIDESEL))
3895 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3897 /* single line edit updates itself */
3898 if (!(es->style & ES_MULTILINE))
3900 HDC hdc = GetDC(es->hwndSelf);
3901 EDIT_WM_Paint(es, hdc);
3902 ReleaseDC(es->hwndSelf, hdc);
3905 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3906 EDIT_SetCaretPos(es, es->selection_end,
3907 es->flags & EF_AFTER_WRAP);
3908 ShowCaret(es->hwndSelf);
3909 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3913 /*********************************************************************
3917 * With Win95 look the margins are set to default font value unless
3918 * the system font (font == 0) is being set, in which case they are left
3922 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3930 dc = GetDC(es->hwndSelf);
3932 old_font = SelectObject(dc, font);
3933 GetTextMetricsW(dc, &tm);
3934 es->line_height = tm.tmHeight;
3935 es->char_width = tm.tmAveCharWidth;
3937 SelectObject(dc, old_font);
3938 ReleaseDC(es->hwndSelf, dc);
3940 /* Reset the format rect and the margins */
3941 GetClientRect(es->hwndSelf, &clientRect);
3942 EDIT_SetRectNP(es, &clientRect);
3943 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3944 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3946 if (es->style & ES_MULTILINE)
3947 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3949 EDIT_CalcLineWidth_SL(es);
3952 EDIT_UpdateText(es, NULL, TRUE);
3953 if (es->flags & EF_FOCUSED) {
3955 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3956 EDIT_SetCaretPos(es, es->selection_end,
3957 es->flags & EF_AFTER_WRAP);
3958 ShowCaret(es->hwndSelf);
3963 /*********************************************************************
3968 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3969 * The modified flag is reset. No notifications are sent.
3971 * For single-line controls, reception of WM_SETTEXT triggers:
3972 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3975 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3977 LPWSTR textW = NULL;
3978 if (!unicode && text)
3980 LPCSTR textA = (LPCSTR)text;
3981 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3982 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3984 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3988 if (es->flags & EF_UPDATE)
3989 /* fixed this bug once; complain if we see it about to happen again. */
3990 ERR("SetSel may generate UPDATE message whose handler may reset "
3993 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3996 TRACE("%s\n", debugstr_w(text));
3997 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
3999 HeapFree(GetProcessHeap(), 0, textW);
4003 static const WCHAR empty_stringW[] = {0};
4005 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4008 es->flags &= ~EF_MODIFIED;
4009 EDIT_EM_SetSel(es, 0, 0, FALSE);
4011 /* Send the notification after the selection start and end have been set
4012 * edit control doesn't send notification on WM_SETTEXT
4013 * if it is multiline, or it is part of combobox
4015 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4017 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
4018 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4020 EDIT_EM_ScrollCaret(es);
4021 EDIT_UpdateScrollInfo(es);
4025 /*********************************************************************
4030 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4032 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4034 TRACE("width = %d, height = %d\n", width, height);
4035 SetRect(&rc, 0, 0, width, height);
4036 EDIT_SetRectNP(es, &rc);
4037 EDIT_UpdateText(es, NULL, TRUE);
4042 /*********************************************************************
4046 * This message is sent by SetWindowLong on having changed either the Style
4047 * or the extended style.
4049 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4051 * See also EDIT_WM_NCCreate
4053 * It appears that the Windows version of the edit control allows the style
4054 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4055 * style variable which will generally be different. In this function we
4056 * update the internal style based on what changed in the externally visible
4059 * Much of this content as based upon the MSDN, especially:
4060 * Platform SDK Documentation -> User Interface Services ->
4061 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4062 * Edit Control Styles
4064 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4066 if (GWL_STYLE == which) {
4067 DWORD style_change_mask;
4069 /* Only a subset of changes can be applied after the control
4072 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4074 if (es->style & ES_MULTILINE)
4075 style_change_mask |= ES_WANTRETURN;
4077 new_style = style->styleNew & style_change_mask;
4079 /* Number overrides lowercase overrides uppercase (at least it
4080 * does in Win95). However I'll bet that ES_NUMBER would be
4081 * invalid under Win 3.1.
4083 if (new_style & ES_NUMBER) {
4084 ; /* do not override the ES_NUMBER */
4085 } else if (new_style & ES_LOWERCASE) {
4086 new_style &= ~ES_UPPERCASE;
4089 es->style = (es->style & ~style_change_mask) | new_style;
4090 } else if (GWL_EXSTYLE == which) {
4091 ; /* FIXME - what is needed here */
4093 WARN ("Invalid style change %ld\n",which);
4099 /*********************************************************************
4104 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4106 if ((key == VK_BACK) && (key_data & 0x2000)) {
4107 if (EDIT_EM_CanUndo(es))
4110 } else if (key == VK_UP || key == VK_DOWN) {
4111 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4114 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4118 /*********************************************************************
4123 static void EDIT_WM_Timer(EDITSTATE *es)
4125 if (es->region_posx < 0) {
4126 EDIT_MoveBackward(es, TRUE);
4127 } else if (es->region_posx > 0) {
4128 EDIT_MoveForward(es, TRUE);
4131 * FIXME: gotta do some vertical scrolling here, like
4132 * EDIT_EM_LineScroll(hwnd, 0, 1);
4136 /*********************************************************************
4141 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4146 if (!(es->style & ES_MULTILINE))
4149 if (!(es->style & ES_AUTOHSCROLL))
4153 fw = es->format_rect.right - es->format_rect.left;
4156 TRACE("SB_LINELEFT\n");
4158 dx = -es->char_width;
4161 TRACE("SB_LINERIGHT\n");
4162 if (es->x_offset < es->text_width)
4163 dx = es->char_width;
4166 TRACE("SB_PAGELEFT\n");
4168 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4171 TRACE("SB_PAGERIGHT\n");
4172 if (es->x_offset < es->text_width)
4173 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4181 TRACE("SB_RIGHT\n");
4182 if (es->x_offset < es->text_width)
4183 dx = es->text_width - es->x_offset;
4186 TRACE("SB_THUMBTRACK %d\n", pos);
4187 es->flags |= EF_HSCROLL_TRACK;
4188 if(es->style & WS_HSCROLL)
4189 dx = pos - es->x_offset;
4194 if(pos < 0 || pos > 100) return 0;
4195 /* Assume default scroll range 0-100 */
4196 fw = es->format_rect.right - es->format_rect.left;
4197 new_x = pos * (es->text_width - fw) / 100;
4198 dx = es->text_width ? (new_x - es->x_offset) : 0;
4201 case SB_THUMBPOSITION:
4202 TRACE("SB_THUMBPOSITION %d\n", pos);
4203 es->flags &= ~EF_HSCROLL_TRACK;
4204 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4205 dx = pos - es->x_offset;
4210 if(pos < 0 || pos > 100) return 0;
4211 /* Assume default scroll range 0-100 */
4212 fw = es->format_rect.right - es->format_rect.left;
4213 new_x = pos * (es->text_width - fw) / 100;
4214 dx = es->text_width ? (new_x - es->x_offset) : 0;
4217 /* force scroll info update */
4218 EDIT_UpdateScrollInfo(es);
4219 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4223 TRACE("SB_ENDSCROLL\n");
4226 * FIXME : the next two are undocumented !
4227 * Are we doing the right thing ?
4228 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4229 * although it's also a regular control message.
4231 case EM_GETTHUMB: /* this one is used by NT notepad */
4235 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4236 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4239 /* Assume default scroll range 0-100 */
4240 INT fw = es->format_rect.right - es->format_rect.left;
4241 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4243 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4246 case EM_LINESCROLL16:
4247 TRACE("EM_LINESCROLL16\n");
4252 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4258 INT fw = es->format_rect.right - es->format_rect.left;
4259 /* check if we are going to move too far */
4260 if(es->x_offset + dx + fw > es->text_width)
4261 dx = es->text_width - fw - es->x_offset;
4263 EDIT_EM_LineScroll_internal(es, dx, 0);
4269 /*********************************************************************
4274 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4278 if (!(es->style & ES_MULTILINE))
4281 if (!(es->style & ES_AUTOVSCROLL))
4290 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4291 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4292 (action == SB_PAGEUP ? "SB_PAGEUP" :
4294 EDIT_EM_Scroll(es, action);
4301 TRACE("SB_BOTTOM\n");
4302 dy = es->line_count - 1 - es->y_offset;
4305 TRACE("SB_THUMBTRACK %d\n", pos);
4306 es->flags |= EF_VSCROLL_TRACK;
4307 if(es->style & WS_VSCROLL)
4308 dy = pos - es->y_offset;
4311 /* Assume default scroll range 0-100 */
4314 if(pos < 0 || pos > 100) return 0;
4315 vlc = get_vertical_line_count(es);
4316 new_y = pos * (es->line_count - vlc) / 100;
4317 dy = es->line_count ? (new_y - es->y_offset) : 0;
4318 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4319 es->line_count, es->y_offset, pos, dy);
4322 case SB_THUMBPOSITION:
4323 TRACE("SB_THUMBPOSITION %d\n", pos);
4324 es->flags &= ~EF_VSCROLL_TRACK;
4325 if(es->style & WS_VSCROLL)
4326 dy = pos - es->y_offset;
4329 /* Assume default scroll range 0-100 */
4332 if(pos < 0 || pos > 100) return 0;
4333 vlc = get_vertical_line_count(es);
4334 new_y = pos * (es->line_count - vlc) / 100;
4335 dy = es->line_count ? (new_y - es->y_offset) : 0;
4336 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4337 es->line_count, es->y_offset, pos, dy);
4341 /* force scroll info update */
4342 EDIT_UpdateScrollInfo(es);
4343 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4347 TRACE("SB_ENDSCROLL\n");
4350 * FIXME : the next two are undocumented !
4351 * Are we doing the right thing ?
4352 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4353 * although it's also a regular control message.
4355 case EM_GETTHUMB: /* this one is used by NT notepad */
4359 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4360 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4363 /* Assume default scroll range 0-100 */
4364 INT vlc = get_vertical_line_count(es);
4365 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4367 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4370 case EM_LINESCROLL16:
4371 TRACE("EM_LINESCROLL16 %d\n", pos);
4376 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4381 EDIT_EM_LineScroll(es, 0, dy);
4385 /*********************************************************************
4389 * FIXME: is this right ? (or should it be only VSCROLL)
4390 * (and maybe only for edit controls that really have their
4391 * own scrollbars) (and maybe only for multiline controls ?)
4392 * All in all: very poorly documented
4395 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4397 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
4398 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
4402 /********************************************************************
4404 * The Following code is to handle inline editing from IMEs
4407 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4410 LPWSTR lpCompStr = NULL;
4411 LPSTR lpCompStrAttr = NULL;
4414 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4421 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
4424 ERR("Unable to allocate IME CompositionString\n");
4429 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4430 lpCompStr[buflen/sizeof(WCHAR)] = 0;
4432 if (CompFlag & GCS_COMPATTR)
4435 * We do not use the attributes yet. it would tell us what characters
4436 * are in transition and which are converted or decided upon
4438 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4442 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4445 ERR("Unable to allocate IME Attribute String\n");
4446 HeapFree(GetProcessHeap(),0,lpCompStr);
4449 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4451 lpCompStrAttr[dwBufLenAttr] = 0;
4454 lpCompStrAttr = NULL;
4457 /* check for change in composition start */
4458 if (es->selection_end < es->composition_start)
4459 es->composition_start = es->selection_end;
4461 /* replace existing selection string */
4462 es->selection_start = es->composition_start;
4464 if (es->composition_len > 0)
4465 es->selection_end = es->composition_start + es->composition_len;
4467 es->selection_end = es->selection_start;
4469 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
4470 es->composition_len = abs(es->composition_start - es->selection_end);
4472 es->selection_start = es->composition_start;
4473 es->selection_end = es->selection_start + es->composition_len;
4475 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4476 HeapFree(GetProcessHeap(),0,lpCompStr);
4479 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4484 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4490 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
4493 ERR("Unable to alloc buffer for IME string\n");
4497 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4498 lpResultStr[buflen/sizeof(WCHAR)] = 0;
4500 /* check for change in composition start */
4501 if (es->selection_end < es->composition_start)
4502 es->composition_start = es->selection_end;
4504 es->selection_start = es->composition_start;
4505 es->selection_end = es->composition_start + es->composition_len;
4506 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
4507 es->composition_start = es->selection_end;
4508 es->composition_len = 0;
4510 HeapFree(GetProcessHeap(),0,lpResultStr);
4513 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4518 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4520 static const WCHAR empty_stringW[] = {0};
4521 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4522 es->composition_start = es->selection_end;
4525 hIMC = ImmGetContext(hwnd);
4529 if (CompFlag & GCS_RESULTSTR)
4530 EDIT_GetResultStr(hIMC, es);
4531 if (CompFlag & GCS_COMPSTR)
4532 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4533 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4534 ImmReleaseContext(hwnd, hIMC);
4535 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4539 /*********************************************************************
4543 * See also EDIT_WM_StyleChanged
4545 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4550 TRACE("Creating %s edit control, style = %08x\n",
4551 unicode ? "Unicode" : "ANSI", lpcs->style);
4553 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4555 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4558 * Note: since the EDITSTATE has not been fully initialized yet,
4559 * we can't use any API calls that may send
4560 * WM_XXX messages before WM_NCCREATE is completed.
4563 es->is_unicode = unicode;
4564 es->style = lpcs->style;
4566 es->bEnableState = !(es->style & WS_DISABLED);
4568 es->hwndSelf = hwnd;
4569 /* Save parent, which will be notified by EN_* messages */
4570 es->hwndParent = lpcs->hwndParent;
4572 if (es->style & ES_COMBO)
4573 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4575 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4576 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4578 /* Number overrides lowercase overrides uppercase (at least it
4579 * does in Win95). However I'll bet that ES_NUMBER would be
4580 * invalid under Win 3.1.
4582 if (es->style & ES_NUMBER) {
4583 ; /* do not override the ES_NUMBER */
4584 } else if (es->style & ES_LOWERCASE) {
4585 es->style &= ~ES_UPPERCASE;
4587 if (es->style & ES_MULTILINE) {
4588 es->buffer_limit = BUFLIMIT_INITIAL;
4589 if (es->style & WS_VSCROLL)
4590 es->style |= ES_AUTOVSCROLL;
4591 if (es->style & WS_HSCROLL)
4592 es->style |= ES_AUTOHSCROLL;
4593 es->style &= ~ES_PASSWORD;
4594 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4595 /* Confirmed - RIGHT overrides CENTER */
4596 if (es->style & ES_RIGHT)
4597 es->style &= ~ES_CENTER;
4598 es->style &= ~WS_HSCROLL;
4599 es->style &= ~ES_AUTOHSCROLL;
4602 es->buffer_limit = BUFLIMIT_INITIAL;
4603 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4604 es->style &= ~ES_CENTER;
4605 es->style &= ~WS_HSCROLL;
4606 es->style &= ~WS_VSCROLL;
4607 if (es->style & ES_PASSWORD)
4608 es->password_char = '*';
4611 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4612 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4614 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4616 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4618 es->undo_buffer_size = es->buffer_size;
4620 if (es->style & ES_MULTILINE)
4621 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4626 * In Win95 look and feel, the WS_BORDER style is replaced by the
4627 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4628 * control a nonclient area so we don't need to draw the border.
4629 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4630 * a nonclient area and we should handle painting the border ourselves.
4632 * When making modifications please ensure that the code still works
4633 * for edit controls created directly with style 0x50800000, exStyle 0
4634 * (which should have a single pixel border)
4636 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4637 es->style &= ~WS_BORDER;
4638 else if (es->style & WS_BORDER)
4639 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4645 /*********************************************************************
4650 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4654 TRACE("%s\n", debugstr_w(name));
4656 * To initialize some final structure members, we call some helper
4657 * functions. However, since the EDITSTATE is not consistent (i.e.
4658 * not fully initialized), we should be very careful which
4659 * functions can be called, and in what order.
4661 EDIT_WM_SetFont(es, 0, FALSE);
4662 EDIT_EM_EmptyUndoBuffer(es);
4664 /* We need to calculate the format rect
4665 (applications may send EM_SETMARGINS before the control gets visible) */
4666 GetClientRect(es->hwndSelf, &clientRect);
4667 EDIT_SetRectNP(es, &clientRect);
4669 if (name && *name) {
4670 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4671 /* if we insert text to the editline, the text scrolls out
4672 * of the window, as the caret is placed after the insert
4673 * pos normally; thus we reset es->selection... to 0 and
4676 es->selection_start = es->selection_end = 0;
4677 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4678 * Messages are only to be sent when the USER does something to
4679 * change the contents. So I am removing this EN_CHANGE
4681 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4683 EDIT_EM_ScrollCaret(es);
4685 /* force scroll info update */
4686 EDIT_UpdateScrollInfo(es);
4687 /* The rule seems to return 1 here for success */
4688 /* Power Builder masked edit controls will crash */
4690 /* FIXME: is that in all cases so ? */
4695 /*********************************************************************
4700 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4705 LocalFree(es->hloc32W);
4708 LocalFree(es->hloc32A);
4711 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4712 HANDLE16 oldDS = stack16->ds;
4714 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4715 while (LocalUnlock16(es->hloc16)) ;
4716 LocalFree16(es->hloc16);
4717 stack16->ds = oldDS;
4720 pc = es->first_line_def;
4724 HeapFree(GetProcessHeap(), 0, pc);
4728 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4729 HeapFree(GetProcessHeap(), 0, es);
4735 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4738 return DefWindowProcW(hwnd, msg, wParam, lParam);
4740 return DefWindowProcA(hwnd, msg, wParam, lParam);
4743 /*********************************************************************
4745 * EditWndProc_common
4747 * The messages are in the order of the actual integer values
4748 * (which can be found in include/windows.h)
4749 * Wherever possible the 16 bit versions are converted to
4750 * the 32 bit ones, so that we can 'fall through' to the
4751 * helper functions. These are mostly 32 bit (with a few
4752 * exceptions, clearly indicated by a '16' extension to their
4756 static LRESULT EditWndProc_common( HWND hwnd, UINT msg,
4757 WPARAM wParam, LPARAM lParam, BOOL unicode )
4759 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4762 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4764 if (!es && msg != WM_NCCREATE)
4765 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4767 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
4775 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4779 if ((short)LOWORD(lParam) == -1)
4780 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4782 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
4784 EDIT_EM_ScrollCaret(es);
4788 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4789 EDIT_EM_ScrollCaret(es);
4796 RECT16 *r16 = MapSL(lParam);
4797 r16->left = es->format_rect.left;
4798 r16->top = es->format_rect.top;
4799 r16->right = es->format_rect.right;
4800 r16->bottom = es->format_rect.bottom;
4805 CopyRect((LPRECT)lParam, &es->format_rect);
4809 if ((es->style & ES_MULTILINE) && lParam) {
4811 RECT16 *r16 = MapSL(lParam);
4812 rc.left = r16->left;
4814 rc.right = r16->right;
4815 rc.bottom = r16->bottom;
4816 EDIT_SetRectNP(es, &rc);
4817 EDIT_UpdateText(es, NULL, TRUE);
4821 if ((es->style & ES_MULTILINE) && lParam) {
4822 EDIT_SetRectNP(es, (LPRECT)lParam);
4823 EDIT_UpdateText(es, NULL, TRUE);
4827 case EM_SETRECTNP16:
4828 if ((es->style & ES_MULTILINE) && lParam) {
4830 RECT16 *r16 = MapSL(lParam);
4831 rc.left = r16->left;
4833 rc.right = r16->right;
4834 rc.bottom = r16->bottom;
4835 EDIT_SetRectNP(es, &rc);
4839 if ((es->style & ES_MULTILINE) && lParam)
4840 EDIT_SetRectNP(es, (LPRECT)lParam);
4845 result = EDIT_EM_Scroll(es, (INT)wParam);
4848 case EM_LINESCROLL16:
4849 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
4850 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
4853 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4856 case EM_SCROLLCARET16:
4857 case EM_SCROLLCARET:
4858 EDIT_EM_ScrollCaret(es);
4862 case EM_GETMODIFY16:
4864 result = ((es->flags & EF_MODIFIED) != 0);
4867 case EM_SETMODIFY16:
4870 es->flags |= EF_MODIFIED;
4872 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4875 case EM_GETLINECOUNT16:
4876 case EM_GETLINECOUNT:
4877 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4880 case EM_LINEINDEX16:
4881 if ((INT16)wParam == -1)
4882 wParam = (WPARAM)-1;
4885 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4888 case EM_SETHANDLE16:
4889 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
4892 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4895 case EM_GETHANDLE16:
4896 result = (LRESULT)EDIT_EM_GetHandle16(es);
4899 result = (LRESULT)EDIT_EM_GetHandle(es);
4904 result = EDIT_EM_GetThumb(es);
4907 /* these messages missing from specs */
4916 FIXME("undocumented message 0x%x, please report\n", msg);
4917 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4920 case EM_LINELENGTH16:
4922 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4925 case EM_REPLACESEL16:
4926 lParam = (LPARAM)MapSL(lParam);
4927 unicode = FALSE; /* 16-bit message is always ascii */
4934 textW = (LPWSTR)lParam;
4937 LPSTR textA = (LPSTR)lParam;
4938 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4939 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4940 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4943 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
4947 HeapFree(GetProcessHeap(), 0, textW);
4952 lParam = (LPARAM)MapSL(lParam);
4953 unicode = FALSE; /* 16-bit message is always ascii */
4956 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4959 case EM_LIMITTEXT16:
4960 case EM_SETLIMITTEXT:
4961 EDIT_EM_SetLimitText(es, wParam);
4966 result = (LRESULT)EDIT_EM_CanUndo(es);
4972 result = (LRESULT)EDIT_EM_Undo(es);
4977 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4980 case EM_LINEFROMCHAR16:
4981 case EM_LINEFROMCHAR:
4982 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4985 case EM_SETTABSTOPS16:
4986 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
4988 case EM_SETTABSTOPS:
4989 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4992 case EM_SETPASSWORDCHAR16:
4993 unicode = FALSE; /* 16-bit message is always ascii */
4995 case EM_SETPASSWORDCHAR:
5000 charW = (WCHAR)wParam;
5003 CHAR charA = wParam;
5004 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
5007 EDIT_EM_SetPasswordChar(es, charW);
5011 case EM_EMPTYUNDOBUFFER16:
5012 case EM_EMPTYUNDOBUFFER:
5013 EDIT_EM_EmptyUndoBuffer(es);
5016 case EM_GETFIRSTVISIBLELINE16:
5017 result = es->y_offset;
5019 case EM_GETFIRSTVISIBLELINE:
5020 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
5023 case EM_SETREADONLY16:
5024 case EM_SETREADONLY:
5026 SetWindowLongW( hwnd, GWL_STYLE,
5027 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
5028 es->style |= ES_READONLY;
5030 SetWindowLongW( hwnd, GWL_STYLE,
5031 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
5032 es->style &= ~ES_READONLY;
5037 case EM_SETWORDBREAKPROC16:
5038 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
5040 case EM_SETWORDBREAKPROC:
5041 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
5044 case EM_GETWORDBREAKPROC16:
5045 result = (LRESULT)es->word_break_proc16;
5047 case EM_GETWORDBREAKPROC:
5048 result = (LRESULT)es->word_break_proc;
5051 case EM_GETPASSWORDCHAR16:
5052 unicode = FALSE; /* 16-bit message is always ascii */
5054 case EM_GETPASSWORDCHAR:
5057 result = es->password_char;
5060 WCHAR charW = es->password_char;
5062 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
5068 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
5071 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
5075 result = MAKELONG(es->left_margin, es->right_margin);
5078 case EM_GETLIMITTEXT:
5079 result = es->buffer_limit;
5082 case EM_POSFROMCHAR:
5083 if ((INT)wParam >= get_text_length(es)) result = -1;
5084 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
5087 case EM_CHARFROMPOS:
5088 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5091 /* End of the EM_ messages which were in numerical order; what order
5092 * are these in? vaguely alphabetical?
5096 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
5100 result = EDIT_WM_Destroy(es);
5105 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
5107 if (es->style & ES_MULTILINE)
5108 result |= DLGC_WANTALLKEYS;
5110 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
5112 int vk = (int)((LPMSG)lParam)->wParam;
5114 if (es->hwndListBox)
5116 if (vk == VK_RETURN || vk == VK_ESCAPE)
5117 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
5118 result |= DLGC_WANTMESSAGE;
5125 SendMessageW(GetParent(hwnd), WM_CLOSE, 0, 0);
5140 strng[0] = wParam >> 8;
5141 strng[1] = wParam & 0xff;
5142 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
5143 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
5144 result = EDIT_WM_Char(es, charW);
5156 CHAR charA = wParam;
5157 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
5160 if (es->hwndListBox)
5162 if (charW == VK_RETURN || charW == VK_ESCAPE)
5164 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
5165 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
5169 result = EDIT_WM_Char(es, charW);
5176 if (wParam == UNICODE_NOCHAR) return TRUE;
5177 if (wParam <= 0x000fffff)
5179 if(wParam > 0xffff) /* convert to surrogates */
5182 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
5183 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
5185 else EDIT_WM_Char(es, wParam);
5196 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
5199 case WM_CONTEXTMENU:
5200 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5209 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
5212 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
5213 LPWSTR nameW = NULL;
5216 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5217 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5218 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5220 result = EDIT_WM_Create(es, nameW);
5221 HeapFree(GetProcessHeap(), 0, nameW);
5230 es->bEnableState = (BOOL) wParam;
5231 EDIT_UpdateText(es, NULL, TRUE);
5235 /* we do the proper erase in EDIT_WM_Paint */
5240 result = (LRESULT)es->font;
5244 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5247 case WM_GETTEXTLENGTH:
5248 if (unicode) result = get_text_length(es);
5249 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5250 NULL, 0, NULL, NULL );
5254 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5258 result = EDIT_WM_KeyDown(es, (INT)wParam);
5262 result = EDIT_WM_KillFocus(es);
5265 case WM_LBUTTONDBLCLK:
5266 result = EDIT_WM_LButtonDblClk(es);
5269 case WM_LBUTTONDOWN:
5270 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5274 result = EDIT_WM_LButtonUp(es);
5277 case WM_MBUTTONDOWN:
5278 result = EDIT_WM_MButtonDown(es);
5282 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5285 case WM_PRINTCLIENT:
5287 EDIT_WM_Paint(es, (HDC)wParam);
5295 EDIT_WM_SetFocus(es);
5299 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5303 /* FIXME: actually set an internal flag and behave accordingly */
5307 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5312 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
5315 case WM_STYLECHANGED:
5316 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5319 case WM_STYLECHANGING:
5320 result = 0; /* See EDIT_WM_StyleChanged */
5324 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5332 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5337 int gcWheelDelta = 0;
5338 UINT pulScrollLines = 3;
5339 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5341 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5342 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5345 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
5346 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
5348 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
5349 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
5350 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
5356 /* IME messages to make the edit control IME aware */
5357 case WM_IME_SETCONTEXT:
5360 case WM_IME_STARTCOMPOSITION:
5361 es->composition_start = es->selection_end;
5362 es->composition_len = 0;
5365 case WM_IME_COMPOSITION:
5366 EDIT_ImeComposition(hwnd, lParam, es);
5369 case WM_IME_ENDCOMPOSITION:
5370 if (es->composition_len > 0)
5372 static const WCHAR empty_stringW[] = {0};
5373 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
5374 es->selection_end = es->selection_start;
5375 es->composition_len= 0;
5379 case WM_IME_COMPOSITIONFULL:
5385 case WM_IME_CONTROL:
5389 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5393 if (es) EDIT_UnlockBuffer(es, FALSE);
5395 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5400 /*********************************************************************
5402 * EditWndProc (USER32.@)
5404 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5406 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
5409 /*********************************************************************
5413 static LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5415 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
5418 /*********************************************************************
5419 * edit class descriptor
5421 static const WCHAR editW[] = {'E','d','i','t',0};
5422 const struct builtin_class_descr EDIT_builtin_class =
5425 CS_DBLCLKS | CS_PARENTDC, /* style */
5426 EditWndProcA, /* procA */
5427 EditWndProcW, /* procW */
5428 sizeof(EDITSTATE *), /* extra */
5429 IDC_IBEAM, /* cursor */