d3d9: Remove old debug messages from the tests.
[wine/multimedia.git] / dlls / user32 / edit.c
blob9898bc4ae831b313e23c4a6cbb9b1776c5e3f15e
1 /*
2 * Edit control
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
23 * NOTES
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.
32 * TODO:
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
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_OEMCONVERT
45 #include "config.h"
47 #include <stdarg.h>
48 #include <string.h>
49 #include <stdlib.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnt.h"
54 #include "win.h"
55 #include "imm.h"
56 #include "wine/unicode.h"
57 #include "controls.h"
58 #include "user_private.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(edit);
62 WINE_DECLARE_DEBUG_CHANNEL(combo);
63 WINE_DECLARE_DEBUG_CHANNEL(relay);
65 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
66 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
67 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
68 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
71 * extra flags for EDITSTATE.flags field
73 #define EF_MODIFIED 0x0001 /* text has been modified */
74 #define EF_FOCUSED 0x0002 /* we have input focus */
75 #define EF_UPDATE 0x0004 /* notify parent of changed state */
76 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
77 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
79 wrapped line, instead of in front of the next character */
80 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
81 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
82 the text buffer if this is clear. */
83 #define EF_DIALOGMODE 0x0400 /* Indicates that we are inside a dialog window */
85 typedef enum
87 END_0 = 0, /* line ends with terminating '\0' character */
88 END_WRAP, /* line is wrapped */
89 END_HARD, /* line ends with a hard return '\r\n' */
90 END_SOFT, /* line ends with a soft return '\r\r\n' */
91 END_RICH /* line ends with a single '\n' */
92 } LINE_END;
94 typedef struct tagLINEDEF {
95 INT length; /* bruto length of a line in bytes */
96 INT net_length; /* netto length of a line in visible characters */
97 LINE_END ending;
98 INT width; /* width of the line in pixels */
99 INT index; /* line index into the buffer */
100 struct tagLINEDEF *next;
101 } LINEDEF;
103 typedef struct
105 BOOL is_unicode; /* how the control was created */
106 LPWSTR text; /* the actual contents of the control */
107 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
108 UINT buffer_size; /* the size of the buffer in characters */
109 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
110 HFONT font; /* NULL means standard system font */
111 INT x_offset; /* scroll offset for multi lines this is in pixels
112 for single lines it's in characters */
113 INT line_height; /* height of a screen line in pixels */
114 INT char_width; /* average character width in pixels */
115 DWORD style; /* sane version of wnd->dwStyle */
116 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
117 INT undo_insert_count; /* number of characters inserted in sequence */
118 UINT undo_position; /* character index of the insertion and deletion */
119 LPWSTR undo_text; /* deleted text */
120 UINT undo_buffer_size; /* size of the deleted text buffer */
121 INT selection_start; /* == selection_end if no selection */
122 INT selection_end; /* == current caret position */
123 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
124 INT left_margin; /* in pixels */
125 INT right_margin; /* in pixels */
126 RECT format_rect;
127 INT text_width; /* width of the widest line in pixels for multi line controls
128 and just line width for single line controls */
129 INT region_posx; /* Position of cursor relative to region: */
130 INT region_posy; /* -1: to left, 0: within, 1: to right */
131 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
132 INT line_count; /* number of lines */
133 INT y_offset; /* scroll offset in number of lines */
134 BOOL bCaptureState; /* flag indicating whether mouse was captured */
135 BOOL bEnableState; /* flag keeping the enable state */
136 HWND hwndSelf; /* the our window handle */
137 HWND hwndParent; /* Handle of parent for sending EN_* messages.
138 Even if parent will change, EN_* messages
139 should be sent to the first parent. */
140 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
142 * only for multi line controls
144 INT lock_count; /* amount of re-entries in the EditWndProc */
145 INT tabs_count;
146 LPINT tabs;
147 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
148 HLOCAL hloc32W; /* our unicode local memory block */
149 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
150 or EM_SETHANDLE */
152 * IME Data
154 UINT composition_len; /* length of composition, 0 == no composition */
155 int composition_start; /* the character position for the composition */
156 } EDITSTATE;
159 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
160 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
162 /* used for disabled or read-only edit control */
163 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
164 do \
165 { /* Notify parent which has created this edit control */ \
166 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
167 SendMessageW(es->hwndParent, WM_COMMAND, \
168 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
169 (LPARAM)(es->hwndSelf)); \
170 } while(0)
172 static const WCHAR empty_stringW[] = {0};
174 /*********************************************************************
176 * EM_CANUNDO
179 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
181 return (es->undo_insert_count || strlenW(es->undo_text));
185 /*********************************************************************
187 * EM_EMPTYUNDOBUFFER
190 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
192 es->undo_insert_count = 0;
193 *es->undo_text = '\0';
197 /**********************************************************************
198 * get_app_version
200 * Returns the window version in case Wine emulates a later version
201 * of windows than the application expects.
203 * In a number of cases when windows runs an application that was
204 * designed for an earlier windows version, windows reverts
205 * to "old" behaviour of that earlier version.
207 * An example is a disabled edit control that needs to be painted.
208 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
209 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
210 * applications with an expected version 0f 4.0 or higher.
213 static DWORD get_app_version(void)
215 static DWORD version;
216 if (!version)
218 DWORD dwEmulatedVersion;
219 OSVERSIONINFOW info;
220 DWORD dwProcVersion = GetProcessVersion(0);
222 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
223 GetVersionExW( &info );
224 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
225 /* FIXME: this may not be 100% correct; see discussion on the
226 * wine developer list in Nov 1999 */
227 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
229 return version;
232 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
234 HBRUSH hbrush;
235 UINT msg;
237 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
238 msg = WM_CTLCOLORSTATIC;
239 else
240 msg = WM_CTLCOLOREDIT;
242 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
243 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
244 if (!hbrush)
245 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
246 return hbrush;
250 /*********************************************************************
252 * EDIT_WordBreakProc
254 * Find the beginning of words.
255 * Note: unlike the specs for a WordBreakProc, this function only
256 * allows to be called without linebreaks between s[0] up to
257 * s[count - 1]. Remember it is only called
258 * internally, so we can decide this for ourselves.
261 static INT EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
263 INT ret = 0;
265 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
267 if(!s) return 0;
269 switch (action) {
270 case WB_LEFT:
271 if (!count)
272 break;
273 if (index)
274 index--;
275 if (s[index] == ' ') {
276 while (index && (s[index] == ' '))
277 index--;
278 if (index) {
279 while (index && (s[index] != ' '))
280 index--;
281 if (s[index] == ' ')
282 index++;
284 } else {
285 while (index && (s[index] != ' '))
286 index--;
287 if (s[index] == ' ')
288 index++;
290 ret = index;
291 break;
292 case WB_RIGHT:
293 if (!count)
294 break;
295 if (index)
296 index--;
297 if (s[index] == ' ')
298 while ((index < count) && (s[index] == ' ')) index++;
299 else {
300 while (s[index] && (s[index] != ' ') && (index < count))
301 index++;
302 while ((s[index] == ' ') && (index < count)) index++;
304 ret = index;
305 break;
306 case WB_ISDELIMITER:
307 ret = (s[index] == ' ');
308 break;
309 default:
310 ERR("unknown action code, please report !\n");
311 break;
313 return ret;
317 /*********************************************************************
319 * EDIT_CallWordBreakProc
321 * Call appropriate WordBreakProc (internal or external).
323 * Note: The "start" argument should always be an index referring
324 * to es->text. The actual wordbreak proc might be
325 * 16 bit, so we can't always pass any 32 bit LPSTR.
326 * Hence we assume that es->text is the buffer that holds
327 * the string under examination (we can decide this for ourselves).
330 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
332 INT ret;
334 if (es->word_break_proc)
336 if(es->is_unicode)
338 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
340 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
341 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
342 ret = wbpW(es->text + start, index, count, action);
344 else
346 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
347 INT countA;
348 CHAR *textA;
350 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
351 textA = HeapAlloc(GetProcessHeap(), 0, countA);
352 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
353 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
354 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
355 ret = wbpA(textA, index, countA, action);
356 HeapFree(GetProcessHeap(), 0, textA);
359 else
360 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
362 return ret;
365 /*********************************************************************
367 * EDIT_BuildLineDefs_ML
369 * Build linked list of text lines.
370 * Lines can end with '\0' (last line), a character (if it is wrapped),
371 * a soft return '\r\r\n' or a hard return '\r\n'
374 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
376 HDC dc;
377 HFONT old_font = 0;
378 LPWSTR current_position, cp;
379 INT fw;
380 LINEDEF *current_line;
381 LINEDEF *previous_line;
382 LINEDEF *start_line;
383 INT line_index = 0, nstart_line = 0, nstart_index = 0;
384 INT line_count = es->line_count;
385 INT orig_net_length;
386 RECT rc;
388 if (istart == iend && delta == 0)
389 return;
391 dc = GetDC(es->hwndSelf);
392 if (es->font)
393 old_font = SelectObject(dc, es->font);
395 previous_line = NULL;
396 current_line = es->first_line_def;
398 /* Find starting line. istart must lie inside an existing line or
399 * at the end of buffer */
400 do {
401 if (istart < current_line->index + current_line->length ||
402 current_line->ending == END_0)
403 break;
405 previous_line = current_line;
406 current_line = current_line->next;
407 line_index++;
408 } while (current_line);
410 if (!current_line) /* Error occurred start is not inside previous buffer */
412 FIXME(" modification occurred outside buffer\n");
413 ReleaseDC(es->hwndSelf, dc);
414 return;
417 /* Remember start of modifications in order to calculate update region */
418 nstart_line = line_index;
419 nstart_index = current_line->index;
421 /* We must start to reformat from the previous line since the modifications
422 * may have caused the line to wrap upwards. */
423 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
425 line_index--;
426 current_line = previous_line;
428 start_line = current_line;
430 fw = es->format_rect.right - es->format_rect.left;
431 current_position = es->text + current_line->index;
432 do {
433 if (current_line != start_line)
435 if (!current_line || current_line->index + delta > current_position - es->text)
437 /* The buffer has been expanded, create a new line and
438 insert it into the link list */
439 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
440 new_line->next = previous_line->next;
441 previous_line->next = new_line;
442 current_line = new_line;
443 es->line_count++;
445 else if (current_line->index + delta < current_position - es->text)
447 /* The previous line merged with this line so we delete this extra entry */
448 previous_line->next = current_line->next;
449 HeapFree(GetProcessHeap(), 0, current_line);
450 current_line = previous_line->next;
451 es->line_count--;
452 continue;
454 else /* current_line->index + delta == current_position */
456 if (current_position - es->text > iend)
457 break; /* We reached end of line modifications */
458 /* else recalculate this line */
462 current_line->index = current_position - es->text;
463 orig_net_length = current_line->net_length;
465 /* Find end of line */
466 cp = current_position;
467 while (*cp) {
468 if (*cp == '\n') break;
469 if ((*cp == '\r') && (*(cp + 1) == '\n'))
470 break;
471 cp++;
474 /* Mark type of line termination */
475 if (!(*cp)) {
476 current_line->ending = END_0;
477 current_line->net_length = strlenW(current_position);
478 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
479 current_line->ending = END_SOFT;
480 current_line->net_length = cp - current_position - 1;
481 } else if (*cp == '\n') {
482 current_line->ending = END_RICH;
483 current_line->net_length = cp - current_position;
484 } else {
485 current_line->ending = END_HARD;
486 current_line->net_length = cp - current_position;
489 /* Calculate line width */
490 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
491 current_position, current_line->net_length,
492 es->tabs_count, es->tabs));
494 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
495 if (!(es->style & ES_AUTOHSCROLL)) {
496 if (current_line->width > fw) {
497 INT next = 0;
498 INT prev;
499 do {
500 prev = next;
501 next = EDIT_CallWordBreakProc(es, current_position - es->text,
502 prev + 1, current_line->net_length, WB_RIGHT);
503 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
504 current_position, next, es->tabs_count, es->tabs));
505 } while (current_line->width <= fw);
506 if (!prev) { /* Didn't find a line break so force a break */
507 next = 0;
508 do {
509 prev = next;
510 next++;
511 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
512 current_position, next, es->tabs_count, es->tabs));
513 } while (current_line->width <= fw);
514 if (!prev)
515 prev = 1;
518 /* If the first line we are calculating, wrapped before istart, we must
519 * adjust istart in order for this to be reflected in the update region. */
520 if (current_line->index == nstart_index && istart > current_line->index + prev)
521 istart = current_line->index + prev;
522 /* else if we are updating the previous line before the first line we
523 * are re-calculating and it expanded */
524 else if (current_line == start_line &&
525 current_line->index != nstart_index && orig_net_length < prev)
527 /* Line expanded due to an upwards line wrap so we must partially include
528 * previous line in update region */
529 nstart_line = line_index;
530 nstart_index = current_line->index;
531 istart = current_line->index + orig_net_length;
534 current_line->net_length = prev;
535 current_line->ending = END_WRAP;
536 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
537 current_line->net_length, es->tabs_count, es->tabs));
539 else if (current_line == start_line &&
540 current_line->index != nstart_index &&
541 orig_net_length < current_line->net_length) {
542 /* The previous line expanded but it's still not as wide as the client rect */
543 /* The expansion is due to an upwards line wrap so we must partially include
544 it in the update region */
545 nstart_line = line_index;
546 nstart_index = current_line->index;
547 istart = current_line->index + orig_net_length;
552 /* Adjust length to include line termination */
553 switch (current_line->ending) {
554 case END_SOFT:
555 current_line->length = current_line->net_length + 3;
556 break;
557 case END_RICH:
558 current_line->length = current_line->net_length + 1;
559 break;
560 case END_HARD:
561 current_line->length = current_line->net_length + 2;
562 break;
563 case END_WRAP:
564 case END_0:
565 current_line->length = current_line->net_length;
566 break;
568 es->text_width = max(es->text_width, current_line->width);
569 current_position += current_line->length;
570 previous_line = current_line;
571 current_line = current_line->next;
572 line_index++;
573 } while (previous_line->ending != END_0);
575 /* Finish adjusting line indexes by delta or remove hanging lines */
576 if (previous_line->ending == END_0)
578 LINEDEF *pnext = NULL;
580 previous_line->next = NULL;
581 while (current_line)
583 pnext = current_line->next;
584 HeapFree(GetProcessHeap(), 0, current_line);
585 current_line = pnext;
586 es->line_count--;
589 else if (delta != 0)
591 while (current_line)
593 current_line->index += delta;
594 current_line = current_line->next;
598 /* Calculate rest of modification rectangle */
599 if (hrgn)
601 HRGN tmphrgn;
603 * We calculate two rectangles. One for the first line which may have
604 * an indent with respect to the format rect. The other is a format-width
605 * rectangle that spans the rest of the lines that changed or moved.
607 rc.top = es->format_rect.top + nstart_line * es->line_height -
608 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
609 rc.bottom = rc.top + es->line_height;
610 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
611 rc.left = es->format_rect.left;
612 else
613 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
614 es->text + nstart_index, istart - nstart_index,
615 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
616 rc.right = es->format_rect.right;
617 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
619 rc.top = rc.bottom;
620 rc.left = es->format_rect.left;
621 rc.right = es->format_rect.right;
623 * If lines were added or removed we must re-paint the remainder of the
624 * lines since the remaining lines were either shifted up or down.
626 if (line_count < es->line_count) /* We added lines */
627 rc.bottom = es->line_count * es->line_height;
628 else if (line_count > es->line_count) /* We removed lines */
629 rc.bottom = line_count * es->line_height;
630 else
631 rc.bottom = line_index * es->line_height;
632 rc.bottom += es->format_rect.top;
633 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
634 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
635 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
636 DeleteObject(tmphrgn);
639 if (es->font)
640 SelectObject(dc, old_font);
642 ReleaseDC(es->hwndSelf, dc);
646 static inline UINT get_text_length(EDITSTATE *es)
648 if(es->text_length == (UINT)-1)
649 es->text_length = strlenW(es->text);
650 return es->text_length;
653 /*********************************************************************
655 * EDIT_GetPasswordPointer_SL
657 * note: caller should free the (optionally) allocated buffer
660 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
662 if (es->style & ES_PASSWORD) {
663 INT len = get_text_length(es);
664 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
665 text[len] = '\0';
666 while(len) text[--len] = es->password_char;
667 return text;
668 } else
669 return es->text;
673 /*********************************************************************
675 * EDIT_CalcLineWidth_SL
678 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
680 SIZE size;
681 LPWSTR text;
682 HDC dc;
683 HFONT old_font = 0;
685 text = EDIT_GetPasswordPointer_SL(es);
687 dc = GetDC(es->hwndSelf);
688 if (es->font)
689 old_font = SelectObject(dc, es->font);
691 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
693 if (es->font)
694 SelectObject(dc, old_font);
695 ReleaseDC(es->hwndSelf, dc);
697 if (es->style & ES_PASSWORD)
698 HeapFree(GetProcessHeap(), 0, text);
700 es->text_width = size.cx;
703 /*********************************************************************
705 * EDIT_CharFromPos
707 * Beware: This is not the function called on EM_CHARFROMPOS
708 * The position _can_ be outside the formatting / client
709 * rectangle
710 * The return value is only the character index
713 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
715 INT index;
716 HDC dc;
717 HFONT old_font = 0;
718 INT x_high = 0, x_low = 0;
720 if (es->style & ES_MULTILINE) {
721 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
722 INT line_index = 0;
723 LINEDEF *line_def = es->first_line_def;
724 INT low, high;
725 while ((line > 0) && line_def->next) {
726 line_index += line_def->length;
727 line_def = line_def->next;
728 line--;
730 x += es->x_offset - es->format_rect.left;
731 if (es->style & ES_RIGHT)
732 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
733 else if (es->style & ES_CENTER)
734 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
735 if (x >= line_def->width) {
736 if (after_wrap)
737 *after_wrap = (line_def->ending == END_WRAP);
738 return line_index + line_def->net_length;
740 if (x <= 0) {
741 if (after_wrap)
742 *after_wrap = FALSE;
743 return line_index;
745 dc = GetDC(es->hwndSelf);
746 if (es->font)
747 old_font = SelectObject(dc, es->font);
748 low = line_index;
749 high = line_index + line_def->net_length + 1;
750 while (low < high - 1)
752 INT mid = (low + high) / 2;
753 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
754 if (x_now > x) {
755 high = mid;
756 x_high = x_now;
757 } else {
758 low = mid;
759 x_low = x_now;
762 if (abs(x_high - x) + 1 <= abs(x_low - x))
763 index = high;
764 else
765 index = low;
767 if (after_wrap)
768 *after_wrap = ((index == line_index + line_def->net_length) &&
769 (line_def->ending == END_WRAP));
770 } else {
771 LPWSTR text;
772 SIZE size;
773 if (after_wrap)
774 *after_wrap = FALSE;
775 x -= es->format_rect.left;
776 if (!x)
777 return es->x_offset;
779 if (!es->x_offset)
781 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
782 if (es->style & ES_RIGHT)
783 x -= indent;
784 else if (es->style & ES_CENTER)
785 x -= indent / 2;
788 text = EDIT_GetPasswordPointer_SL(es);
789 dc = GetDC(es->hwndSelf);
790 if (es->font)
791 old_font = SelectObject(dc, es->font);
792 if (x < 0)
794 INT low = 0;
795 INT high = es->x_offset;
796 while (low < high - 1)
798 INT mid = (low + high) / 2;
799 GetTextExtentPoint32W( dc, text + mid,
800 es->x_offset - mid, &size );
801 if (size.cx > -x) {
802 low = mid;
803 x_low = size.cx;
804 } else {
805 high = mid;
806 x_high = size.cx;
809 if (abs(x_high + x) <= abs(x_low + x) + 1)
810 index = high;
811 else
812 index = low;
814 else
816 INT low = es->x_offset;
817 INT high = get_text_length(es) + 1;
818 while (low < high - 1)
820 INT mid = (low + high) / 2;
821 GetTextExtentPoint32W( dc, text + es->x_offset,
822 mid - es->x_offset, &size );
823 if (size.cx > x) {
824 high = mid;
825 x_high = size.cx;
826 } else {
827 low = mid;
828 x_low = size.cx;
831 if (abs(x_high - x) <= abs(x_low - x) + 1)
832 index = high;
833 else
834 index = low;
836 if (es->style & ES_PASSWORD)
837 HeapFree(GetProcessHeap(), 0, text);
839 if (es->font)
840 SelectObject(dc, old_font);
841 ReleaseDC(es->hwndSelf, dc);
842 return index;
846 /*********************************************************************
848 * EDIT_ConfinePoint
850 * adjusts the point to be within the formatting rectangle
851 * (so CharFromPos returns the nearest _visible_ character)
854 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
856 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
857 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
861 /*********************************************************************
863 * EM_LINEFROMCHAR
866 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
868 INT line;
869 LINEDEF *line_def;
871 if (!(es->style & ES_MULTILINE))
872 return 0;
873 if (index > (INT)get_text_length(es))
874 return es->line_count - 1;
875 if (index == -1)
876 index = min(es->selection_start, es->selection_end);
878 line = 0;
879 line_def = es->first_line_def;
880 index -= line_def->length;
881 while ((index >= 0) && line_def->next) {
882 line++;
883 line_def = line_def->next;
884 index -= line_def->length;
886 return line;
890 /*********************************************************************
892 * EM_LINEINDEX
895 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
897 INT line_index;
898 const LINEDEF *line_def;
900 if (!(es->style & ES_MULTILINE))
901 return 0;
902 if (line >= es->line_count)
903 return -1;
905 line_index = 0;
906 line_def = es->first_line_def;
907 if (line == -1) {
908 INT index = es->selection_end - line_def->length;
909 while ((index >= 0) && line_def->next) {
910 line_index += line_def->length;
911 line_def = line_def->next;
912 index -= line_def->length;
914 } else {
915 while (line > 0) {
916 line_index += line_def->length;
917 line_def = line_def->next;
918 line--;
921 return line_index;
925 /*********************************************************************
927 * EM_LINELENGTH
930 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
932 LINEDEF *line_def;
934 if (!(es->style & ES_MULTILINE))
935 return get_text_length(es);
937 if (index == -1) {
938 /* get the number of remaining non-selected chars of selected lines */
939 INT32 l; /* line number */
940 INT32 li; /* index of first char in line */
941 INT32 count;
942 l = EDIT_EM_LineFromChar(es, es->selection_start);
943 /* # chars before start of selection area */
944 count = es->selection_start - EDIT_EM_LineIndex(es, l);
945 l = EDIT_EM_LineFromChar(es, es->selection_end);
946 /* # chars after end of selection */
947 li = EDIT_EM_LineIndex(es, l);
948 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
949 return count;
951 line_def = es->first_line_def;
952 index -= line_def->length;
953 while ((index >= 0) && line_def->next) {
954 line_def = line_def->next;
955 index -= line_def->length;
957 return line_def->net_length;
961 /*********************************************************************
963 * EM_POSFROMCHAR
966 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
968 INT len = get_text_length(es);
969 INT l;
970 INT li;
971 INT x;
972 INT y = 0;
973 INT w;
974 INT lw = 0;
975 INT ll = 0;
976 HDC dc;
977 HFONT old_font = 0;
978 SIZE size;
979 LINEDEF *line_def;
981 index = min(index, len);
982 dc = GetDC(es->hwndSelf);
983 if (es->font)
984 old_font = SelectObject(dc, es->font);
985 if (es->style & ES_MULTILINE) {
986 l = EDIT_EM_LineFromChar(es, index);
987 y = (l - es->y_offset) * es->line_height;
988 li = EDIT_EM_LineIndex(es, l);
989 if (after_wrap && (li == index) && l) {
990 INT l2 = l - 1;
991 line_def = es->first_line_def;
992 while (l2) {
993 line_def = line_def->next;
994 l2--;
996 if (line_def->ending == END_WRAP) {
997 l--;
998 y -= es->line_height;
999 li = EDIT_EM_LineIndex(es, l);
1003 line_def = es->first_line_def;
1004 while (line_def->index != li)
1005 line_def = line_def->next;
1007 ll = line_def->net_length;
1008 lw = line_def->width;
1010 w = es->format_rect.right - es->format_rect.left;
1011 if (es->style & ES_RIGHT)
1013 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
1014 es->tabs_count, es->tabs)) - es->x_offset;
1015 x = w - x;
1017 else if (es->style & ES_CENTER)
1019 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1020 es->tabs_count, es->tabs)) - es->x_offset;
1021 x += (w - lw) / 2;
1023 else /* ES_LEFT */
1025 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1026 es->tabs_count, es->tabs)) - es->x_offset;
1028 } else {
1029 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
1030 if (index < es->x_offset) {
1031 GetTextExtentPoint32W(dc, text + index,
1032 es->x_offset - index, &size);
1033 x = -size.cx;
1034 } else {
1035 GetTextExtentPoint32W(dc, text + es->x_offset,
1036 index - es->x_offset, &size);
1037 x = size.cx;
1039 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1041 w = es->format_rect.right - es->format_rect.left;
1042 if (w > es->text_width)
1044 if (es->style & ES_RIGHT)
1045 x += w - es->text_width;
1046 else if (es->style & ES_CENTER)
1047 x += (w - es->text_width) / 2;
1051 y = 0;
1052 if (es->style & ES_PASSWORD)
1053 HeapFree(GetProcessHeap(), 0, text);
1055 x += es->format_rect.left;
1056 y += es->format_rect.top;
1057 if (es->font)
1058 SelectObject(dc, old_font);
1059 ReleaseDC(es->hwndSelf, dc);
1060 return MAKELONG((INT16)x, (INT16)y);
1064 /*********************************************************************
1066 * EDIT_GetLineRect
1068 * Calculates the bounding rectangle for a line from a starting
1069 * column to an ending column.
1072 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1074 INT line_index = EDIT_EM_LineIndex(es, line);
1076 if (es->style & ES_MULTILINE)
1077 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1078 else
1079 rc->top = es->format_rect.top;
1080 rc->bottom = rc->top + es->line_height;
1081 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1082 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1086 static inline void text_buffer_changed(EDITSTATE *es)
1088 es->text_length = (UINT)-1;
1091 /*********************************************************************
1092 * EDIT_LockBuffer
1095 static void EDIT_LockBuffer(EDITSTATE *es)
1097 if (!es->text) {
1099 if(!es->hloc32W) return;
1101 if(es->hloc32A)
1103 CHAR *textA = LocalLock(es->hloc32A);
1104 HLOCAL hloc32W_new;
1105 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1106 if(countW_new > es->buffer_size + 1)
1108 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1109 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1110 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1111 if(hloc32W_new)
1113 es->hloc32W = hloc32W_new;
1114 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1115 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1117 else
1118 WARN("FAILED! Will synchronize partially\n");
1120 es->text = LocalLock(es->hloc32W);
1121 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1122 LocalUnlock(es->hloc32A);
1124 else es->text = LocalLock(es->hloc32W);
1126 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1127 es->lock_count++;
1131 /*********************************************************************
1133 * EDIT_UnlockBuffer
1136 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1139 /* Edit window might be already destroyed */
1140 if(!IsWindow(es->hwndSelf))
1142 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1143 return;
1146 if (!es->lock_count) {
1147 ERR("lock_count == 0 ... please report\n");
1148 return;
1150 if (!es->text) {
1151 ERR("es->text == 0 ... please report\n");
1152 return;
1155 if (force || (es->lock_count == 1)) {
1156 if (es->hloc32W) {
1157 UINT countA = 0;
1158 UINT countW = get_text_length(es) + 1;
1160 if(es->hloc32A)
1162 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1163 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1164 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1165 countA = LocalSize(es->hloc32A);
1166 if(countA_new > countA)
1168 HLOCAL hloc32A_new;
1169 UINT alloc_size = ROUND_TO_GROW(countA_new);
1170 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1171 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1172 if(hloc32A_new)
1174 es->hloc32A = hloc32A_new;
1175 countA = LocalSize(hloc32A_new);
1176 TRACE("Real new size %d bytes\n", countA);
1178 else
1179 WARN("FAILED! Will synchronize partially\n");
1181 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1182 LocalLock(es->hloc32A), countA, NULL, NULL);
1183 LocalUnlock(es->hloc32A);
1186 LocalUnlock(es->hloc32W);
1187 es->text = NULL;
1189 else {
1190 ERR("no buffer ... please report\n");
1191 return;
1194 es->lock_count--;
1198 /*********************************************************************
1200 * EDIT_MakeFit
1202 * Try to fit size + 1 characters in the buffer.
1204 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1206 HLOCAL hNew32W;
1208 if (size <= es->buffer_size)
1209 return TRUE;
1211 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1213 /* Force edit to unlock it's buffer. es->text now NULL */
1214 EDIT_UnlockBuffer(es, TRUE);
1216 if (es->hloc32W) {
1217 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1218 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1219 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1220 es->hloc32W = hNew32W;
1221 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1225 EDIT_LockBuffer(es);
1227 if (es->buffer_size < size) {
1228 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1229 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1230 return FALSE;
1231 } else {
1232 TRACE("We now have %d+1\n", es->buffer_size);
1233 return TRUE;
1238 /*********************************************************************
1240 * EDIT_MakeUndoFit
1242 * Try to fit size + 1 bytes in the undo buffer.
1245 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1247 UINT alloc_size;
1249 if (size <= es->undo_buffer_size)
1250 return TRUE;
1252 TRACE("trying to ReAlloc to %d+1\n", size);
1254 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1255 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1256 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1257 return TRUE;
1259 else
1261 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1262 return FALSE;
1267 /*********************************************************************
1269 * EDIT_UpdateTextRegion
1272 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1274 if (es->flags & EF_UPDATE) {
1275 es->flags &= ~EF_UPDATE;
1276 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1278 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1282 /*********************************************************************
1284 * EDIT_UpdateText
1287 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1289 if (es->flags & EF_UPDATE) {
1290 es->flags &= ~EF_UPDATE;
1291 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1293 InvalidateRect(es->hwndSelf, rc, bErase);
1296 /*********************************************************************
1298 * EDIT_SL_InvalidateText
1300 * Called from EDIT_InvalidateText().
1301 * Does the job for single-line controls only.
1304 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1306 RECT line_rect;
1307 RECT rc;
1309 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1310 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1311 EDIT_UpdateText(es, &rc, TRUE);
1315 static inline INT get_vertical_line_count(EDITSTATE *es)
1317 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1318 return max(1,vlc);
1321 /*********************************************************************
1323 * EDIT_ML_InvalidateText
1325 * Called from EDIT_InvalidateText().
1326 * Does the job for multi-line controls only.
1329 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1331 INT vlc = get_vertical_line_count(es);
1332 INT sl = EDIT_EM_LineFromChar(es, start);
1333 INT el = EDIT_EM_LineFromChar(es, end);
1334 INT sc;
1335 INT ec;
1336 RECT rc1;
1337 RECT rcWnd;
1338 RECT rcLine;
1339 RECT rcUpdate;
1340 INT l;
1342 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1343 return;
1345 sc = start - EDIT_EM_LineIndex(es, sl);
1346 ec = end - EDIT_EM_LineIndex(es, el);
1347 if (sl < es->y_offset) {
1348 sl = es->y_offset;
1349 sc = 0;
1351 if (el > es->y_offset + vlc) {
1352 el = es->y_offset + vlc;
1353 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1355 GetClientRect(es->hwndSelf, &rc1);
1356 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1357 if (sl == el) {
1358 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1359 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1360 EDIT_UpdateText(es, &rcUpdate, TRUE);
1361 } else {
1362 EDIT_GetLineRect(es, sl, sc,
1363 EDIT_EM_LineLength(es,
1364 EDIT_EM_LineIndex(es, sl)),
1365 &rcLine);
1366 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1367 EDIT_UpdateText(es, &rcUpdate, TRUE);
1368 for (l = sl + 1 ; l < el ; l++) {
1369 EDIT_GetLineRect(es, l, 0,
1370 EDIT_EM_LineLength(es,
1371 EDIT_EM_LineIndex(es, l)),
1372 &rcLine);
1373 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1374 EDIT_UpdateText(es, &rcUpdate, TRUE);
1376 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1377 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1378 EDIT_UpdateText(es, &rcUpdate, TRUE);
1383 /*********************************************************************
1385 * EDIT_InvalidateText
1387 * Invalidate the text from offset start up to, but not including,
1388 * offset end. Useful for (re)painting the selection.
1389 * Regions outside the linewidth are not invalidated.
1390 * end == -1 means end == TextLength.
1391 * start and end need not be ordered.
1394 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1396 if (end == start)
1397 return;
1399 if (end == -1)
1400 end = get_text_length(es);
1402 if (end < start) {
1403 INT tmp = start;
1404 start = end;
1405 end = tmp;
1408 if (es->style & ES_MULTILINE)
1409 EDIT_ML_InvalidateText(es, start, end);
1410 else
1411 EDIT_SL_InvalidateText(es, start, end);
1415 /*********************************************************************
1417 * EDIT_EM_SetSel
1419 * note: unlike the specs say: the order of start and end
1420 * _is_ preserved in Windows. (i.e. start can be > end)
1421 * In other words: this handler is OK
1424 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1426 UINT old_start = es->selection_start;
1427 UINT old_end = es->selection_end;
1428 UINT len = get_text_length(es);
1430 if (start == (UINT)-1) {
1431 start = es->selection_end;
1432 end = es->selection_end;
1433 } else {
1434 start = min(start, len);
1435 end = min(end, len);
1437 es->selection_start = start;
1438 es->selection_end = end;
1439 if (after_wrap)
1440 es->flags |= EF_AFTER_WRAP;
1441 else
1442 es->flags &= ~EF_AFTER_WRAP;
1443 /* Compute the necessary invalidation region. */
1444 /* Note that we don't need to invalidate regions which have
1445 * "never" been selected, or those which are "still" selected.
1446 * In fact, every time we hit a selection boundary, we can
1447 * *toggle* whether we need to invalidate. Thus we can optimize by
1448 * *sorting* the interval endpoints. Let's assume that we sort them
1449 * in this order:
1450 * start <= end <= old_start <= old_end
1451 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1452 * in 5 comparisons; i.e. it is impossible to do better than the
1453 * following: */
1454 ORDER_UINT(end, old_end);
1455 ORDER_UINT(start, old_start);
1456 ORDER_UINT(old_start, old_end);
1457 ORDER_UINT(start, end);
1458 /* Note that at this point 'end' and 'old_start' are not in order, but
1459 * start is definitely the min. and old_end is definitely the max. */
1460 if (end != old_start)
1463 * One can also do
1464 * ORDER_UINT32(end, old_start);
1465 * EDIT_InvalidateText(es, start, end);
1466 * EDIT_InvalidateText(es, old_start, old_end);
1467 * in place of the following if statement.
1468 * (That would complete the optimal five-comparison four-element sort.)
1470 if (old_start > end )
1472 EDIT_InvalidateText(es, start, end);
1473 EDIT_InvalidateText(es, old_start, old_end);
1475 else
1477 EDIT_InvalidateText(es, start, old_start);
1478 EDIT_InvalidateText(es, end, old_end);
1481 else EDIT_InvalidateText(es, start, old_end);
1485 /*********************************************************************
1487 * EDIT_UpdateScrollInfo
1490 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1492 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1494 SCROLLINFO si;
1495 si.cbSize = sizeof(SCROLLINFO);
1496 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1497 si.nMin = 0;
1498 si.nMax = es->line_count - 1;
1499 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1500 si.nPos = es->y_offset;
1501 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1502 si.nMin, si.nMax, si.nPage, si.nPos);
1503 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1506 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1508 SCROLLINFO si;
1509 si.cbSize = sizeof(SCROLLINFO);
1510 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1511 si.nMin = 0;
1512 si.nMax = es->text_width - 1;
1513 si.nPage = es->format_rect.right - es->format_rect.left;
1514 si.nPos = es->x_offset;
1515 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1516 si.nMin, si.nMax, si.nPage, si.nPos);
1517 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1522 /*********************************************************************
1524 * EDIT_EM_LineScroll_internal
1526 * Version of EDIT_EM_LineScroll for internal use.
1527 * It doesn't refuse if ES_MULTILINE is set and assumes that
1528 * dx is in pixels, dy - in lines.
1531 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1533 INT nyoff;
1534 INT x_offset_in_pixels;
1535 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1536 es->line_height;
1538 if (es->style & ES_MULTILINE)
1540 x_offset_in_pixels = es->x_offset;
1542 else
1544 dy = 0;
1545 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1548 if (-dx > x_offset_in_pixels)
1549 dx = -x_offset_in_pixels;
1550 if (dx > es->text_width - x_offset_in_pixels)
1551 dx = es->text_width - x_offset_in_pixels;
1552 nyoff = max(0, es->y_offset + dy);
1553 if (nyoff >= es->line_count - lines_per_page)
1554 nyoff = max(0, es->line_count - lines_per_page);
1555 dy = (es->y_offset - nyoff) * es->line_height;
1556 if (dx || dy) {
1557 RECT rc1;
1558 RECT rc;
1560 es->y_offset = nyoff;
1561 if(es->style & ES_MULTILINE)
1562 es->x_offset += dx;
1563 else
1564 es->x_offset += dx / es->char_width;
1566 GetClientRect(es->hwndSelf, &rc1);
1567 IntersectRect(&rc, &rc1, &es->format_rect);
1568 ScrollWindowEx(es->hwndSelf, -dx, dy,
1569 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1570 /* force scroll info update */
1571 EDIT_UpdateScrollInfo(es);
1573 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1574 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1575 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1576 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1577 return TRUE;
1580 /*********************************************************************
1582 * EM_LINESCROLL
1584 * NOTE: dx is in average character widths, dy - in lines;
1587 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1589 if (!(es->style & ES_MULTILINE))
1590 return FALSE;
1592 dx *= es->char_width;
1593 return EDIT_EM_LineScroll_internal(es, dx, dy);
1597 /*********************************************************************
1599 * EM_SCROLL
1602 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1604 INT dy;
1606 if (!(es->style & ES_MULTILINE))
1607 return (LRESULT)FALSE;
1609 dy = 0;
1611 switch (action) {
1612 case SB_LINEUP:
1613 if (es->y_offset)
1614 dy = -1;
1615 break;
1616 case SB_LINEDOWN:
1617 if (es->y_offset < es->line_count - 1)
1618 dy = 1;
1619 break;
1620 case SB_PAGEUP:
1621 if (es->y_offset)
1622 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1623 break;
1624 case SB_PAGEDOWN:
1625 if (es->y_offset < es->line_count - 1)
1626 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1627 break;
1628 default:
1629 return (LRESULT)FALSE;
1631 if (dy) {
1632 INT vlc = get_vertical_line_count(es);
1633 /* check if we are going to move too far */
1634 if(es->y_offset + dy > es->line_count - vlc)
1635 dy = max(es->line_count - vlc, 0) - es->y_offset;
1637 /* Notification is done in EDIT_EM_LineScroll */
1638 if(dy) {
1639 EDIT_EM_LineScroll(es, 0, dy);
1640 return MAKELONG(dy, TRUE);
1644 return (LRESULT)FALSE;
1648 /*********************************************************************
1650 * EDIT_SetCaretPos
1653 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1654 BOOL after_wrap)
1656 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1657 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1658 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1662 /*********************************************************************
1664 * EM_SCROLLCARET
1667 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1669 if (es->style & ES_MULTILINE) {
1670 INT l;
1671 INT vlc;
1672 INT ww;
1673 INT cw = es->char_width;
1674 INT x;
1675 INT dy = 0;
1676 INT dx = 0;
1678 l = EDIT_EM_LineFromChar(es, es->selection_end);
1679 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1680 vlc = get_vertical_line_count(es);
1681 if (l >= es->y_offset + vlc)
1682 dy = l - vlc + 1 - es->y_offset;
1683 if (l < es->y_offset)
1684 dy = l - es->y_offset;
1685 ww = es->format_rect.right - es->format_rect.left;
1686 if (x < es->format_rect.left)
1687 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1688 if (x > es->format_rect.right)
1689 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1690 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1692 /* check if we are going to move too far */
1693 if(es->x_offset + dx + ww > es->text_width)
1694 dx = es->text_width - ww - es->x_offset;
1695 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1696 EDIT_EM_LineScroll_internal(es, dx, dy);
1698 } else {
1699 INT x;
1700 INT goal;
1701 INT format_width;
1703 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1704 format_width = es->format_rect.right - es->format_rect.left;
1705 if (x < es->format_rect.left) {
1706 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1707 do {
1708 es->x_offset--;
1709 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1710 } while ((x < goal) && es->x_offset);
1711 /* FIXME: use ScrollWindow() somehow to improve performance */
1712 EDIT_UpdateText(es, NULL, TRUE);
1713 } else if (x > es->format_rect.right) {
1714 INT x_last;
1715 INT len = get_text_length(es);
1716 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1717 do {
1718 es->x_offset++;
1719 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1720 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1721 } while ((x > goal) && (x_last > es->format_rect.right));
1722 /* FIXME: use ScrollWindow() somehow to improve performance */
1723 EDIT_UpdateText(es, NULL, TRUE);
1727 if(es->flags & EF_FOCUSED)
1728 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1732 /*********************************************************************
1734 * EDIT_MoveBackward
1737 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1739 INT e = es->selection_end;
1741 if (e) {
1742 e--;
1743 if ((es->style & ES_MULTILINE) && e &&
1744 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1745 e--;
1746 if (e && (es->text[e - 1] == '\r'))
1747 e--;
1750 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1751 EDIT_EM_ScrollCaret(es);
1755 /*********************************************************************
1757 * EDIT_MoveDown_ML
1759 * Only for multi line controls
1760 * Move the caret one line down, on a column with the nearest
1761 * x coordinate on the screen (might be a different column).
1764 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1766 INT s = es->selection_start;
1767 INT e = es->selection_end;
1768 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1769 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1770 INT x = (short)LOWORD(pos);
1771 INT y = (short)HIWORD(pos);
1773 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1774 if (!extend)
1775 s = e;
1776 EDIT_EM_SetSel(es, s, e, after_wrap);
1777 EDIT_EM_ScrollCaret(es);
1781 /*********************************************************************
1783 * EDIT_MoveEnd
1786 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1788 BOOL after_wrap = FALSE;
1789 INT e;
1791 /* Pass a high value in x to make sure of receiving the end of the line */
1792 if (!ctrl && (es->style & ES_MULTILINE))
1793 e = EDIT_CharFromPos(es, 0x3fffffff,
1794 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1795 else
1796 e = get_text_length(es);
1797 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1798 EDIT_EM_ScrollCaret(es);
1802 /*********************************************************************
1804 * EDIT_MoveForward
1807 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1809 INT e = es->selection_end;
1811 if (es->text[e]) {
1812 e++;
1813 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1814 if (es->text[e] == '\n')
1815 e++;
1816 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1817 e += 2;
1820 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1821 EDIT_EM_ScrollCaret(es);
1825 /*********************************************************************
1827 * EDIT_MoveHome
1829 * Home key: move to beginning of line.
1832 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1834 INT e;
1836 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1837 if (!ctrl && (es->style & ES_MULTILINE))
1838 e = EDIT_CharFromPos(es, -es->x_offset,
1839 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1840 else
1841 e = 0;
1842 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1843 EDIT_EM_ScrollCaret(es);
1847 /*********************************************************************
1849 * EDIT_MovePageDown_ML
1851 * Only for multi line controls
1852 * Move the caret one page down, on a column with the nearest
1853 * x coordinate on the screen (might be a different column).
1856 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1858 INT s = es->selection_start;
1859 INT e = es->selection_end;
1860 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1861 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1862 INT x = (short)LOWORD(pos);
1863 INT y = (short)HIWORD(pos);
1865 e = EDIT_CharFromPos(es, x,
1866 y + (es->format_rect.bottom - es->format_rect.top),
1867 &after_wrap);
1868 if (!extend)
1869 s = e;
1870 EDIT_EM_SetSel(es, s, e, after_wrap);
1871 EDIT_EM_ScrollCaret(es);
1875 /*********************************************************************
1877 * EDIT_MovePageUp_ML
1879 * Only for multi line controls
1880 * Move the caret one page up, on a column with the nearest
1881 * x coordinate on the screen (might be a different column).
1884 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1886 INT s = es->selection_start;
1887 INT e = es->selection_end;
1888 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1889 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1890 INT x = (short)LOWORD(pos);
1891 INT y = (short)HIWORD(pos);
1893 e = EDIT_CharFromPos(es, x,
1894 y - (es->format_rect.bottom - es->format_rect.top),
1895 &after_wrap);
1896 if (!extend)
1897 s = e;
1898 EDIT_EM_SetSel(es, s, e, after_wrap);
1899 EDIT_EM_ScrollCaret(es);
1903 /*********************************************************************
1905 * EDIT_MoveUp_ML
1907 * Only for multi line controls
1908 * Move the caret one line up, on a column with the nearest
1909 * x coordinate on the screen (might be a different column).
1912 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1914 INT s = es->selection_start;
1915 INT e = es->selection_end;
1916 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1917 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1918 INT x = (short)LOWORD(pos);
1919 INT y = (short)HIWORD(pos);
1921 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1922 if (!extend)
1923 s = e;
1924 EDIT_EM_SetSel(es, s, e, after_wrap);
1925 EDIT_EM_ScrollCaret(es);
1929 /*********************************************************************
1931 * EDIT_MoveWordBackward
1934 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1936 INT s = es->selection_start;
1937 INT e = es->selection_end;
1938 INT l;
1939 INT ll;
1940 INT li;
1942 l = EDIT_EM_LineFromChar(es, e);
1943 ll = EDIT_EM_LineLength(es, e);
1944 li = EDIT_EM_LineIndex(es, l);
1945 if (e - li == 0) {
1946 if (l) {
1947 li = EDIT_EM_LineIndex(es, l - 1);
1948 e = li + EDIT_EM_LineLength(es, li);
1950 } else {
1951 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
1953 if (!extend)
1954 s = e;
1955 EDIT_EM_SetSel(es, s, e, FALSE);
1956 EDIT_EM_ScrollCaret(es);
1960 /*********************************************************************
1962 * EDIT_MoveWordForward
1965 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
1967 INT s = es->selection_start;
1968 INT e = es->selection_end;
1969 INT l;
1970 INT ll;
1971 INT li;
1973 l = EDIT_EM_LineFromChar(es, e);
1974 ll = EDIT_EM_LineLength(es, e);
1975 li = EDIT_EM_LineIndex(es, l);
1976 if (e - li == ll) {
1977 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1978 e = EDIT_EM_LineIndex(es, l + 1);
1979 } else {
1980 e = li + EDIT_CallWordBreakProc(es,
1981 li, e - li + 1, ll, WB_RIGHT);
1983 if (!extend)
1984 s = e;
1985 EDIT_EM_SetSel(es, s, e, FALSE);
1986 EDIT_EM_ScrollCaret(es);
1990 /*********************************************************************
1992 * EDIT_PaintText
1995 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
1997 COLORREF BkColor;
1998 COLORREF TextColor;
1999 LOGFONTW underline_font;
2000 HFONT hUnderline = 0;
2001 HFONT old_font = 0;
2002 INT ret;
2003 INT li;
2004 INT BkMode;
2005 SIZE size;
2007 if (!count)
2008 return 0;
2009 BkMode = GetBkMode(dc);
2010 BkColor = GetBkColor(dc);
2011 TextColor = GetTextColor(dc);
2012 if (rev) {
2013 if (es->composition_len == 0)
2015 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2016 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2017 SetBkMode( dc, OPAQUE);
2019 else
2021 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2022 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2023 underline_font.lfUnderline = TRUE;
2024 hUnderline = CreateFontIndirectW(&underline_font);
2025 old_font = SelectObject(dc,hUnderline);
2028 li = EDIT_EM_LineIndex(es, line);
2029 if (es->style & ES_MULTILINE) {
2030 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2031 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2032 } else {
2033 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2034 TextOutW(dc, x, y, text + li + col, count);
2035 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2036 ret = size.cx;
2037 if (es->style & ES_PASSWORD)
2038 HeapFree(GetProcessHeap(), 0, text);
2040 if (rev) {
2041 if (es->composition_len == 0)
2043 SetBkColor(dc, BkColor);
2044 SetTextColor(dc, TextColor);
2045 SetBkMode( dc, BkMode);
2047 else
2049 if (old_font)
2050 SelectObject(dc,old_font);
2051 if (hUnderline)
2052 DeleteObject(hUnderline);
2055 return ret;
2059 /*********************************************************************
2061 * EDIT_PaintLine
2064 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2066 INT s = es->selection_start;
2067 INT e = es->selection_end;
2068 INT li;
2069 INT ll;
2070 INT x;
2071 INT y;
2072 LRESULT pos;
2074 if (es->style & ES_MULTILINE) {
2075 INT vlc = get_vertical_line_count(es);
2077 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2078 return;
2079 } else if (line)
2080 return;
2082 TRACE("line=%d\n", line);
2084 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2085 x = (short)LOWORD(pos);
2086 y = (short)HIWORD(pos);
2087 li = EDIT_EM_LineIndex(es, line);
2088 ll = EDIT_EM_LineLength(es, li);
2089 s = min(es->selection_start, es->selection_end);
2090 e = max(es->selection_start, es->selection_end);
2091 s = min(li + ll, max(li, s));
2092 e = min(li + ll, max(li, e));
2093 if (rev && (s != e) &&
2094 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2095 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2096 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2097 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2098 } else
2099 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2103 /*********************************************************************
2105 * EDIT_AdjustFormatRect
2107 * Adjusts the format rectangle for the current font and the
2108 * current client rectangle.
2111 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2113 RECT ClientRect;
2115 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2116 if (es->style & ES_MULTILINE)
2118 INT fw, vlc, max_x_offset, max_y_offset;
2120 vlc = get_vertical_line_count(es);
2121 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2123 /* correct es->x_offset */
2124 fw = es->format_rect.right - es->format_rect.left;
2125 max_x_offset = es->text_width - fw;
2126 if(max_x_offset < 0) max_x_offset = 0;
2127 if(es->x_offset > max_x_offset)
2128 es->x_offset = max_x_offset;
2130 /* correct es->y_offset */
2131 max_y_offset = es->line_count - vlc;
2132 if(max_y_offset < 0) max_y_offset = 0;
2133 if(es->y_offset > max_y_offset)
2134 es->y_offset = max_y_offset;
2136 /* force scroll info update */
2137 EDIT_UpdateScrollInfo(es);
2139 else
2140 /* Windows doesn't care to fix text placement for SL controls */
2141 es->format_rect.bottom = es->format_rect.top + es->line_height;
2143 /* Always stay within the client area */
2144 GetClientRect(es->hwndSelf, &ClientRect);
2145 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2147 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2148 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2150 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2154 /*********************************************************************
2156 * EDIT_SetRectNP
2158 * note: this is not (exactly) the handler called on EM_SETRECTNP
2159 * it is also used to set the rect of a single line control
2162 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2164 LONG_PTR ExStyle;
2165 INT bw, bh;
2166 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2168 CopyRect(&es->format_rect, rc);
2170 if (ExStyle & WS_EX_CLIENTEDGE) {
2171 es->format_rect.left++;
2172 es->format_rect.right--;
2174 if (es->format_rect.bottom - es->format_rect.top
2175 >= es->line_height + 2)
2177 es->format_rect.top++;
2178 es->format_rect.bottom--;
2181 else if (es->style & WS_BORDER) {
2182 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2183 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2184 es->format_rect.left += bw;
2185 es->format_rect.right -= bw;
2186 if (es->format_rect.bottom - es->format_rect.top
2187 >= es->line_height + 2 * bh)
2189 es->format_rect.top += bh;
2190 es->format_rect.bottom -= bh;
2194 es->format_rect.left += es->left_margin;
2195 es->format_rect.right -= es->right_margin;
2196 EDIT_AdjustFormatRect(es);
2200 /*********************************************************************
2202 * EM_CHARFROMPOS
2204 * returns line number (not index) in high-order word of result.
2205 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2206 * to Richedit, not to the edit control. Original documentation is valid.
2207 * FIXME: do the specs mean to return -1 if outside client area or
2208 * if outside formatting rectangle ???
2211 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2213 POINT pt;
2214 RECT rc;
2215 INT index;
2217 pt.x = x;
2218 pt.y = y;
2219 GetClientRect(es->hwndSelf, &rc);
2220 if (!PtInRect(&rc, pt))
2221 return -1;
2223 index = EDIT_CharFromPos(es, x, y, NULL);
2224 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2228 /*********************************************************************
2230 * EM_FMTLINES
2232 * Enable or disable soft breaks.
2234 * This means: insert or remove the soft linebreak character (\r\r\n).
2235 * Take care to check if the text still fits the buffer after insertion.
2236 * If not, notify with EN_ERRSPACE.
2239 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2241 es->flags &= ~EF_USE_SOFTBRK;
2242 if (add_eol) {
2243 es->flags |= EF_USE_SOFTBRK;
2244 FIXME("soft break enabled, not implemented\n");
2246 return add_eol;
2250 /*********************************************************************
2252 * EM_GETHANDLE
2254 * Hopefully this won't fire back at us.
2255 * We always start with a fixed buffer in the local heap.
2256 * Despite of the documentation says that the local heap is used
2257 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2258 * buffer on the local heap.
2261 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2263 HLOCAL hLocal;
2265 if (!(es->style & ES_MULTILINE))
2266 return 0;
2268 if(es->is_unicode)
2269 hLocal = es->hloc32W;
2270 else
2272 if(!es->hloc32A)
2274 CHAR *textA;
2275 UINT countA, alloc_size;
2276 TRACE("Allocating 32-bit ANSI alias buffer\n");
2277 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2278 alloc_size = ROUND_TO_GROW(countA);
2279 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2281 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2282 return 0;
2284 textA = LocalLock(es->hloc32A);
2285 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2286 LocalUnlock(es->hloc32A);
2288 hLocal = es->hloc32A;
2291 es->flags |= EF_APP_HAS_HANDLE;
2292 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2293 return hLocal;
2297 /*********************************************************************
2299 * EM_GETLINE
2302 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2304 LPWSTR src;
2305 INT line_len, dst_len;
2306 INT i;
2308 if (es->style & ES_MULTILINE) {
2309 if (line >= es->line_count)
2310 return 0;
2311 } else
2312 line = 0;
2313 i = EDIT_EM_LineIndex(es, line);
2314 src = es->text + i;
2315 line_len = EDIT_EM_LineLength(es, i);
2316 dst_len = *(WORD *)dst;
2317 if(unicode)
2319 if(dst_len <= line_len)
2321 memcpy(dst, src, dst_len * sizeof(WCHAR));
2322 return dst_len;
2324 else /* Append 0 if enough space */
2326 memcpy(dst, src, line_len * sizeof(WCHAR));
2327 dst[line_len] = 0;
2328 return line_len;
2331 else
2333 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2334 if(!ret && line_len) /* Insufficient buffer size */
2335 return dst_len;
2336 if(ret < dst_len) /* Append 0 if enough space */
2337 ((LPSTR)dst)[ret] = 0;
2338 return ret;
2343 /*********************************************************************
2345 * EM_GETSEL
2348 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2350 UINT s = es->selection_start;
2351 UINT e = es->selection_end;
2353 ORDER_UINT(s, e);
2354 if (start)
2355 *start = s;
2356 if (end)
2357 *end = e;
2358 return MAKELONG(s, e);
2362 /*********************************************************************
2364 * EM_REPLACESEL
2366 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2369 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2371 UINT strl = strlenW(lpsz_replace);
2372 UINT tl = get_text_length(es);
2373 UINT utl;
2374 UINT s;
2375 UINT e;
2376 UINT i;
2377 UINT size;
2378 LPWSTR p;
2379 HRGN hrgn = 0;
2380 LPWSTR buf = NULL;
2381 UINT bufl = 0;
2383 TRACE("%s, can_undo %d, send_update %d\n",
2384 debugstr_w(lpsz_replace), can_undo, send_update);
2386 s = es->selection_start;
2387 e = es->selection_end;
2389 if ((s == e) && !strl)
2390 return;
2392 ORDER_UINT(s, e);
2394 size = tl - (e - s) + strl;
2395 if (!size)
2396 es->text_width = 0;
2398 /* Issue the EN_MAXTEXT notification and continue with replacing text
2399 * such that buffer limit is honored. */
2400 if ((honor_limit) && (size > es->buffer_limit)) {
2401 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2402 /* Buffer limit can be smaller than the actual length of text in combobox */
2403 if (es->buffer_limit < (tl - (e-s)))
2404 strl = 0;
2405 else
2406 strl = es->buffer_limit - (tl - (e-s));
2409 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2410 return;
2412 if (e != s) {
2413 /* there is something to be deleted */
2414 TRACE("deleting stuff.\n");
2415 bufl = e - s;
2416 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2417 if (!buf) return;
2418 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2419 buf[bufl] = 0; /* ensure 0 termination */
2420 /* now delete */
2421 strcpyW(es->text + s, es->text + e);
2422 text_buffer_changed(es);
2424 if (strl) {
2425 /* there is an insertion */
2426 tl = get_text_length(es);
2427 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));
2428 for (p = es->text + tl ; p >= es->text + s ; p--)
2429 p[strl] = p[0];
2430 for (i = 0 , p = es->text + s ; i < strl ; i++)
2431 p[i] = lpsz_replace[i];
2432 if(es->style & ES_UPPERCASE)
2433 CharUpperBuffW(p, strl);
2434 else if(es->style & ES_LOWERCASE)
2435 CharLowerBuffW(p, strl);
2436 text_buffer_changed(es);
2438 if (es->style & ES_MULTILINE)
2440 INT st = min(es->selection_start, es->selection_end);
2441 INT vlc = get_vertical_line_count(es);
2443 hrgn = CreateRectRgn(0, 0, 0, 0);
2444 EDIT_BuildLineDefs_ML(es, st, st + strl,
2445 strl - abs(es->selection_end - es->selection_start), hrgn);
2446 /* if text is too long undo all changes */
2447 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2448 if (strl)
2449 strcpyW(es->text + e, es->text + e + strl);
2450 if (e != s)
2451 for (i = 0 , p = es->text ; i < e - s ; i++)
2452 p[i + s] = buf[i];
2453 text_buffer_changed(es);
2454 EDIT_BuildLineDefs_ML(es, s, e,
2455 abs(es->selection_end - es->selection_start) - strl, hrgn);
2456 strl = 0;
2457 e = s;
2458 hrgn = CreateRectRgn(0, 0, 0, 0);
2459 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2462 else {
2463 INT fw = es->format_rect.right - es->format_rect.left;
2464 EDIT_CalcLineWidth_SL(es);
2465 /* remove chars that don't fit */
2466 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2467 while ((es->text_width > fw) && s + strl >= s) {
2468 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2469 strl--;
2470 EDIT_CalcLineWidth_SL(es);
2472 text_buffer_changed(es);
2473 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2477 if (e != s) {
2478 if (can_undo) {
2479 utl = strlenW(es->undo_text);
2480 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2481 /* undo-buffer is extended to the right */
2482 EDIT_MakeUndoFit(es, utl + e - s);
2483 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2484 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2485 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2486 /* undo-buffer is extended to the left */
2487 EDIT_MakeUndoFit(es, utl + e - s);
2488 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2489 p[e - s] = p[0];
2490 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2491 p[i] = buf[i];
2492 es->undo_position = s;
2493 } else {
2494 /* new undo-buffer */
2495 EDIT_MakeUndoFit(es, e - s);
2496 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2497 es->undo_text[e - s] = 0; /* ensure 0 termination */
2498 es->undo_position = s;
2500 /* any deletion makes the old insertion-undo invalid */
2501 es->undo_insert_count = 0;
2502 } else
2503 EDIT_EM_EmptyUndoBuffer(es);
2505 if (strl) {
2506 if (can_undo) {
2507 if ((s == es->undo_position) ||
2508 ((es->undo_insert_count) &&
2509 (s == es->undo_position + es->undo_insert_count)))
2511 * insertion is new and at delete position or
2512 * an extension to either left or right
2514 es->undo_insert_count += strl;
2515 else {
2516 /* new insertion undo */
2517 es->undo_position = s;
2518 es->undo_insert_count = strl;
2519 /* new insertion makes old delete-buffer invalid */
2520 *es->undo_text = '\0';
2522 } else
2523 EDIT_EM_EmptyUndoBuffer(es);
2526 if (bufl)
2527 HeapFree(GetProcessHeap(), 0, buf);
2529 s += strl;
2531 /* If text has been deleted and we're right or center aligned then scroll rightward */
2532 if (es->style & (ES_RIGHT | ES_CENTER))
2534 INT delta = strl - abs(es->selection_end - es->selection_start);
2536 if (delta < 0 && es->x_offset)
2538 if (abs(delta) > es->x_offset)
2539 es->x_offset = 0;
2540 else
2541 es->x_offset += delta;
2545 EDIT_EM_SetSel(es, s, s, FALSE);
2546 es->flags |= EF_MODIFIED;
2547 if (send_update) es->flags |= EF_UPDATE;
2548 if (hrgn)
2550 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2551 DeleteObject(hrgn);
2553 else
2554 EDIT_UpdateText(es, NULL, TRUE);
2556 EDIT_EM_ScrollCaret(es);
2558 /* force scroll info update */
2559 EDIT_UpdateScrollInfo(es);
2562 if(send_update || (es->flags & EF_UPDATE))
2564 es->flags &= ~EF_UPDATE;
2565 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2570 /*********************************************************************
2572 * EM_SETHANDLE
2574 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2577 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2579 if (!(es->style & ES_MULTILINE))
2580 return;
2582 if (!hloc) {
2583 WARN("called with NULL handle\n");
2584 return;
2587 EDIT_UnlockBuffer(es, TRUE);
2589 if(es->is_unicode)
2591 if(es->hloc32A)
2593 LocalFree(es->hloc32A);
2594 es->hloc32A = NULL;
2596 es->hloc32W = hloc;
2598 else
2600 INT countW, countA;
2601 HLOCAL hloc32W_new;
2602 WCHAR *textW;
2603 CHAR *textA;
2605 countA = LocalSize(hloc);
2606 textA = LocalLock(hloc);
2607 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2608 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2610 ERR("Could not allocate new unicode buffer\n");
2611 return;
2613 textW = LocalLock(hloc32W_new);
2614 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2615 LocalUnlock(hloc32W_new);
2616 LocalUnlock(hloc);
2618 if(es->hloc32W)
2619 LocalFree(es->hloc32W);
2621 es->hloc32W = hloc32W_new;
2622 es->hloc32A = hloc;
2625 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2627 es->flags |= EF_APP_HAS_HANDLE;
2628 EDIT_LockBuffer(es);
2630 es->x_offset = es->y_offset = 0;
2631 es->selection_start = es->selection_end = 0;
2632 EDIT_EM_EmptyUndoBuffer(es);
2633 es->flags &= ~EF_MODIFIED;
2634 es->flags &= ~EF_UPDATE;
2635 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2636 EDIT_UpdateText(es, NULL, TRUE);
2637 EDIT_EM_ScrollCaret(es);
2638 /* force scroll info update */
2639 EDIT_UpdateScrollInfo(es);
2643 /*********************************************************************
2645 * EM_SETLIMITTEXT
2647 * NOTE: this version currently implements WinNT limits
2650 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2652 if (!limit) limit = ~0u;
2653 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2654 es->buffer_limit = limit;
2658 /*********************************************************************
2660 * EM_SETMARGINS
2662 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2663 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2664 * margin according to the textmetrics of the current font.
2666 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2667 * of the char's width as the margin, but this is not how Windows handles this.
2668 * For all other fonts Windows sets the margins to zero.
2670 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2671 * edit control is equal to or larger than a certain size.
2672 * Interestingly if one subtracts both the left and right margins from
2673 * this size one always seems to get an even number. The extents of
2674 * the (four character) string "'**'" match this quite closely, so
2675 * we'll use this until we come up with a better idea.
2677 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2679 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2680 SIZE sz;
2682 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2683 return sz.cx + left + right;
2686 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2687 WORD left, WORD right, BOOL repaint)
2689 TEXTMETRICW tm;
2690 INT default_left_margin = 0; /* in pixels */
2691 INT default_right_margin = 0; /* in pixels */
2693 /* Set the default margins depending on the font */
2694 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2695 HDC dc = GetDC(es->hwndSelf);
2696 HFONT old_font = SelectObject(dc, es->font);
2697 GetTextMetricsW(dc, &tm);
2698 /* The default margins are only non zero for TrueType or Vector fonts */
2699 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2700 int min_size;
2701 RECT rc;
2702 /* This must be calculated more exactly! But how? */
2703 default_left_margin = tm.tmAveCharWidth / 2;
2704 default_right_margin = tm.tmAveCharWidth / 2;
2705 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2706 GetClientRect(es->hwndSelf, &rc);
2707 if(rc.right - rc.left < min_size) {
2708 default_left_margin = es->left_margin;
2709 default_right_margin = es->right_margin;
2712 SelectObject(dc, old_font);
2713 ReleaseDC(es->hwndSelf, dc);
2716 if (action & EC_LEFTMARGIN) {
2717 es->format_rect.left -= es->left_margin;
2718 if (left != EC_USEFONTINFO)
2719 es->left_margin = left;
2720 else
2721 es->left_margin = default_left_margin;
2722 es->format_rect.left += es->left_margin;
2725 if (action & EC_RIGHTMARGIN) {
2726 es->format_rect.right += es->right_margin;
2727 if (right != EC_USEFONTINFO)
2728 es->right_margin = right;
2729 else
2730 es->right_margin = default_right_margin;
2731 es->format_rect.right -= es->right_margin;
2734 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2735 EDIT_AdjustFormatRect(es);
2736 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2739 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2743 /*********************************************************************
2745 * EM_SETPASSWORDCHAR
2748 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2750 LONG style;
2752 if (es->style & ES_MULTILINE)
2753 return;
2755 if (es->password_char == c)
2756 return;
2758 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2759 es->password_char = c;
2760 if (c) {
2761 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2762 es->style |= ES_PASSWORD;
2763 } else {
2764 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2765 es->style &= ~ES_PASSWORD;
2767 EDIT_UpdateText(es, NULL, TRUE);
2771 /*********************************************************************
2773 * EM_SETTABSTOPS
2776 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2778 if (!(es->style & ES_MULTILINE))
2779 return FALSE;
2780 HeapFree(GetProcessHeap(), 0, es->tabs);
2781 es->tabs_count = count;
2782 if (!count)
2783 es->tabs = NULL;
2784 else {
2785 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2786 memcpy(es->tabs, tabs, count * sizeof(INT));
2788 return TRUE;
2792 /*********************************************************************
2794 * EM_SETWORDBREAKPROC
2797 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
2799 if (es->word_break_proc == wbp)
2800 return;
2802 es->word_break_proc = wbp;
2804 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2805 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2806 EDIT_UpdateText(es, NULL, TRUE);
2811 /*********************************************************************
2813 * EM_UNDO / WM_UNDO
2816 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2818 INT ulength;
2819 LPWSTR utext;
2821 /* As per MSDN spec, for a single-line edit control,
2822 the return value is always TRUE */
2823 if( es->style & ES_READONLY )
2824 return !(es->style & ES_MULTILINE);
2826 ulength = strlenW(es->undo_text);
2828 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
2830 strcpyW(utext, es->undo_text);
2832 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2833 es->undo_insert_count, debugstr_w(utext));
2835 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2836 EDIT_EM_EmptyUndoBuffer(es);
2837 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
2838 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2839 /* send the notification after the selection start and end are set */
2840 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2841 EDIT_EM_ScrollCaret(es);
2842 HeapFree(GetProcessHeap(), 0, utext);
2844 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2845 es->undo_insert_count, debugstr_w(es->undo_text));
2846 return TRUE;
2850 /* Helper function for WM_CHAR
2852 * According to an MSDN blog article titled "Just because you're a control
2853 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2854 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2855 * a dialog box or not.
2857 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2859 return (es->flags & EF_DIALOGMODE);
2863 /*********************************************************************
2865 * WM_PASTE
2868 static void EDIT_WM_Paste(EDITSTATE *es)
2870 HGLOBAL hsrc;
2871 LPWSTR src;
2873 /* Protect read-only edit control from modification */
2874 if(es->style & ES_READONLY)
2875 return;
2877 OpenClipboard(es->hwndSelf);
2878 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2879 src = GlobalLock(hsrc);
2880 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
2881 GlobalUnlock(hsrc);
2883 else if (es->style & ES_PASSWORD) {
2884 /* clear selected text in password edit box even with empty clipboard */
2885 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2887 CloseClipboard();
2891 /*********************************************************************
2893 * WM_COPY
2896 static void EDIT_WM_Copy(EDITSTATE *es)
2898 INT s = min(es->selection_start, es->selection_end);
2899 INT e = max(es->selection_start, es->selection_end);
2900 HGLOBAL hdst;
2901 LPWSTR dst;
2902 DWORD len;
2904 if (e == s) return;
2906 len = e - s;
2907 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2908 dst = GlobalLock(hdst);
2909 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2910 dst[len] = 0; /* ensure 0 termination */
2911 TRACE("%s\n", debugstr_w(dst));
2912 GlobalUnlock(hdst);
2913 OpenClipboard(es->hwndSelf);
2914 EmptyClipboard();
2915 SetClipboardData(CF_UNICODETEXT, hdst);
2916 CloseClipboard();
2920 /*********************************************************************
2922 * WM_CLEAR
2925 static inline void EDIT_WM_Clear(EDITSTATE *es)
2927 /* Protect read-only edit control from modification */
2928 if(es->style & ES_READONLY)
2929 return;
2931 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2935 /*********************************************************************
2937 * WM_CUT
2940 static inline void EDIT_WM_Cut(EDITSTATE *es)
2942 EDIT_WM_Copy(es);
2943 EDIT_WM_Clear(es);
2947 /*********************************************************************
2949 * WM_CHAR
2952 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
2954 BOOL control;
2956 control = GetKeyState(VK_CONTROL) & 0x8000;
2958 switch (c) {
2959 case '\r':
2960 /* If it's not a multiline edit box, it would be ignored below.
2961 * For multiline edit without ES_WANTRETURN, we have to make a
2962 * special case.
2964 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
2965 if (EDIT_IsInsideDialog(es))
2966 break;
2967 case '\n':
2968 if (es->style & ES_MULTILINE) {
2969 if (es->style & ES_READONLY) {
2970 EDIT_MoveHome(es, FALSE, FALSE);
2971 EDIT_MoveDown_ML(es, FALSE);
2972 } else {
2973 static const WCHAR cr_lfW[] = {'\r','\n',0};
2974 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
2977 break;
2978 case '\t':
2979 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2981 static const WCHAR tabW[] = {'\t',0};
2982 if (EDIT_IsInsideDialog(es))
2983 break;
2984 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
2986 break;
2987 case VK_BACK:
2988 if (!(es->style & ES_READONLY) && !control) {
2989 if (es->selection_start != es->selection_end)
2990 EDIT_WM_Clear(es);
2991 else {
2992 /* delete character left of caret */
2993 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
2994 EDIT_MoveBackward(es, TRUE);
2995 EDIT_WM_Clear(es);
2998 break;
2999 case 0x03: /* ^C */
3000 if (!(es->style & ES_PASSWORD))
3001 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3002 break;
3003 case 0x16: /* ^V */
3004 if (!(es->style & ES_READONLY))
3005 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3006 break;
3007 case 0x18: /* ^X */
3008 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3009 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3010 break;
3011 case 0x1A: /* ^Z */
3012 if (!(es->style & ES_READONLY))
3013 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3014 break;
3016 default:
3017 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3018 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3019 break;
3021 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3022 WCHAR str[2];
3023 str[0] = c;
3024 str[1] = '\0';
3025 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3027 break;
3029 return 1;
3033 /*********************************************************************
3035 * WM_COMMAND
3038 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3040 if (code || control)
3041 return;
3043 switch (id) {
3044 case EM_UNDO:
3045 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3046 break;
3047 case WM_CUT:
3048 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3049 break;
3050 case WM_COPY:
3051 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3052 break;
3053 case WM_PASTE:
3054 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3055 break;
3056 case WM_CLEAR:
3057 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3058 break;
3059 case EM_SETSEL:
3060 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3061 EDIT_EM_ScrollCaret(es);
3062 break;
3063 default:
3064 ERR("unknown menu item, please report\n");
3065 break;
3070 /*********************************************************************
3072 * WM_CONTEXTMENU
3074 * Note: the resource files resource/sysres_??.rc cannot define a
3075 * single popup menu. Hence we use a (dummy) menubar
3076 * containing the single popup menu as its first item.
3078 * FIXME: the message identifiers have been chosen arbitrarily,
3079 * hence we use MF_BYPOSITION.
3080 * We might as well use the "real" values (anybody knows ?)
3081 * The menu definition is in resources/sysres_??.rc.
3082 * Once these are OK, we better use MF_BYCOMMAND here
3083 * (as we do in EDIT_WM_Command()).
3086 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3088 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3089 HMENU popup = GetSubMenu(menu, 0);
3090 UINT start = es->selection_start;
3091 UINT end = es->selection_end;
3093 ORDER_UINT(start, end);
3095 /* undo */
3096 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3097 /* cut */
3098 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3099 /* copy */
3100 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3101 /* paste */
3102 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3103 /* delete */
3104 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3105 /* select all */
3106 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3108 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3110 RECT rc;
3111 /* Windows places the menu at the edit's center in this case */
3112 GetClientRect(es->hwndSelf, &rc);
3113 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3114 x = rc.left + (rc.right - rc.left) / 2;
3115 y = rc.top + (rc.bottom - rc.top) / 2;
3118 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3119 DestroyMenu(menu);
3123 /*********************************************************************
3125 * WM_GETTEXT
3128 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3130 if(!count) return 0;
3132 if(unicode)
3134 lstrcpynW(dst, es->text, count);
3135 return strlenW(dst);
3137 else
3139 LPSTR textA = (LPSTR)dst;
3140 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3141 textA[count - 1] = 0; /* ensure 0 termination */
3142 return strlen(textA);
3146 /*********************************************************************
3148 * EDIT_CheckCombo
3151 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3153 HWND hLBox = es->hwndListBox;
3154 HWND hCombo;
3155 BOOL bDropped;
3156 int nEUI;
3158 if (!hLBox)
3159 return FALSE;
3161 hCombo = GetParent(es->hwndSelf);
3162 bDropped = TRUE;
3163 nEUI = 0;
3165 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3167 if (key == VK_UP || key == VK_DOWN)
3169 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3170 nEUI = 1;
3172 if (msg == WM_KEYDOWN || nEUI)
3173 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3176 switch (msg)
3178 case WM_KEYDOWN:
3179 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3181 /* make sure ComboLBox pops up */
3182 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3183 key = VK_F4;
3184 nEUI = 2;
3187 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3188 break;
3190 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3191 if (nEUI)
3192 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3193 else
3194 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3195 break;
3198 if(nEUI == 2)
3199 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3201 return TRUE;
3205 /*********************************************************************
3207 * WM_KEYDOWN
3209 * Handling of special keys that don't produce a WM_CHAR
3210 * (i.e. non-printable keys) & Backspace & Delete
3213 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3215 BOOL shift;
3216 BOOL control;
3218 if (GetKeyState(VK_MENU) & 0x8000)
3219 return 0;
3221 shift = GetKeyState(VK_SHIFT) & 0x8000;
3222 control = GetKeyState(VK_CONTROL) & 0x8000;
3224 switch (key) {
3225 case VK_F4:
3226 case VK_UP:
3227 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3228 break;
3230 /* fall through */
3231 case VK_LEFT:
3232 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3233 EDIT_MoveUp_ML(es, shift);
3234 else
3235 if (control)
3236 EDIT_MoveWordBackward(es, shift);
3237 else
3238 EDIT_MoveBackward(es, shift);
3239 break;
3240 case VK_DOWN:
3241 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3242 break;
3243 /* fall through */
3244 case VK_RIGHT:
3245 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3246 EDIT_MoveDown_ML(es, shift);
3247 else if (control)
3248 EDIT_MoveWordForward(es, shift);
3249 else
3250 EDIT_MoveForward(es, shift);
3251 break;
3252 case VK_HOME:
3253 EDIT_MoveHome(es, shift, control);
3254 break;
3255 case VK_END:
3256 EDIT_MoveEnd(es, shift, control);
3257 break;
3258 case VK_PRIOR:
3259 if (es->style & ES_MULTILINE)
3260 EDIT_MovePageUp_ML(es, shift);
3261 else
3262 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3263 break;
3264 case VK_NEXT:
3265 if (es->style & ES_MULTILINE)
3266 EDIT_MovePageDown_ML(es, shift);
3267 else
3268 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3269 break;
3270 case VK_DELETE:
3271 if (!(es->style & ES_READONLY) && !(shift && control)) {
3272 if (es->selection_start != es->selection_end) {
3273 if (shift)
3274 EDIT_WM_Cut(es);
3275 else
3276 EDIT_WM_Clear(es);
3277 } else {
3278 if (shift) {
3279 /* delete character left of caret */
3280 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3281 EDIT_MoveBackward(es, TRUE);
3282 EDIT_WM_Clear(es);
3283 } else if (control) {
3284 /* delete to end of line */
3285 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3286 EDIT_MoveEnd(es, TRUE, FALSE);
3287 EDIT_WM_Clear(es);
3288 } else {
3289 /* delete character right of caret */
3290 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3291 EDIT_MoveForward(es, TRUE);
3292 EDIT_WM_Clear(es);
3296 break;
3297 case VK_INSERT:
3298 if (shift) {
3299 if (!(es->style & ES_READONLY))
3300 EDIT_WM_Paste(es);
3301 } else if (control)
3302 EDIT_WM_Copy(es);
3303 break;
3304 case VK_RETURN:
3305 /* If the edit doesn't want the return send a message to the default object */
3306 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3308 DWORD dw;
3310 if (!EDIT_IsInsideDialog(es)) break;
3311 if (control) break;
3312 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3313 if (HIWORD(dw) == DC_HASDEFID)
3315 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3316 if (hwDefCtrl)
3318 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3319 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3323 break;
3324 case VK_ESCAPE:
3325 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3326 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3327 break;
3328 case VK_TAB:
3329 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3330 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3331 break;
3333 return TRUE;
3337 /*********************************************************************
3339 * WM_KILLFOCUS
3342 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3344 es->flags &= ~EF_FOCUSED;
3345 DestroyCaret();
3346 if(!(es->style & ES_NOHIDESEL))
3347 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3348 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3349 return 0;
3353 /*********************************************************************
3355 * WM_LBUTTONDBLCLK
3357 * The caret position has been set on the WM_LBUTTONDOWN message
3360 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3362 INT s;
3363 INT e = es->selection_end;
3364 INT l;
3365 INT li;
3366 INT ll;
3368 es->bCaptureState = TRUE;
3369 SetCapture(es->hwndSelf);
3371 l = EDIT_EM_LineFromChar(es, e);
3372 li = EDIT_EM_LineIndex(es, l);
3373 ll = EDIT_EM_LineLength(es, e);
3374 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3375 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3376 EDIT_EM_SetSel(es, s, e, FALSE);
3377 EDIT_EM_ScrollCaret(es);
3378 es->region_posx = es->region_posy = 0;
3379 SetTimer(es->hwndSelf, 0, 100, NULL);
3380 return 0;
3384 /*********************************************************************
3386 * WM_LBUTTONDOWN
3389 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3391 INT e;
3392 BOOL after_wrap;
3394 es->bCaptureState = TRUE;
3395 SetCapture(es->hwndSelf);
3396 EDIT_ConfinePoint(es, &x, &y);
3397 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3398 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3399 EDIT_EM_ScrollCaret(es);
3400 es->region_posx = es->region_posy = 0;
3401 SetTimer(es->hwndSelf, 0, 100, NULL);
3403 if (!(es->flags & EF_FOCUSED))
3404 SetFocus(es->hwndSelf);
3406 return 0;
3410 /*********************************************************************
3412 * WM_LBUTTONUP
3415 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3417 if (es->bCaptureState) {
3418 KillTimer(es->hwndSelf, 0);
3419 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3421 es->bCaptureState = FALSE;
3422 return 0;
3426 /*********************************************************************
3428 * WM_MBUTTONDOWN
3431 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3433 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3434 return 0;
3438 /*********************************************************************
3440 * WM_MOUSEMOVE
3443 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3445 INT e;
3446 BOOL after_wrap;
3447 INT prex, prey;
3449 /* If the mouse has been captured by process other than the edit control itself,
3450 * the windows edit controls will not select the strings with mouse move.
3452 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3453 return 0;
3456 * FIXME: gotta do some scrolling if outside client
3457 * area. Maybe reset the timer ?
3459 prex = x; prey = y;
3460 EDIT_ConfinePoint(es, &x, &y);
3461 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3462 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3463 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3464 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3465 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3466 return 0;
3470 /*********************************************************************
3472 * WM_PAINT
3475 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3477 PAINTSTRUCT ps;
3478 INT i;
3479 HDC dc;
3480 HFONT old_font = 0;
3481 RECT rc;
3482 RECT rcClient;
3483 RECT rcLine;
3484 RECT rcRgn;
3485 HBRUSH brush;
3486 HBRUSH old_brush;
3487 INT bw, bh;
3488 BOOL rev = es->bEnableState &&
3489 ((es->flags & EF_FOCUSED) ||
3490 (es->style & ES_NOHIDESEL));
3491 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3493 GetClientRect(es->hwndSelf, &rcClient);
3495 /* get the background brush */
3496 brush = EDIT_NotifyCtlColor(es, dc);
3498 /* paint the border and the background */
3499 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3501 if(es->style & WS_BORDER) {
3502 bw = GetSystemMetrics(SM_CXBORDER);
3503 bh = GetSystemMetrics(SM_CYBORDER);
3504 rc = rcClient;
3505 if(es->style & ES_MULTILINE) {
3506 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3507 if(es->style & WS_VSCROLL) rc.right+=bw;
3510 /* Draw the frame. Same code as in nonclient.c */
3511 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3512 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3513 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3514 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3515 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3516 SelectObject(dc, old_brush);
3518 /* Keep the border clean */
3519 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3520 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3523 GetClipBox(dc, &rc);
3524 FillRect(dc, &rc, brush);
3526 IntersectClipRect(dc, es->format_rect.left,
3527 es->format_rect.top,
3528 es->format_rect.right,
3529 es->format_rect.bottom);
3530 if (es->style & ES_MULTILINE) {
3531 rc = rcClient;
3532 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3534 if (es->font)
3535 old_font = SelectObject(dc, es->font);
3537 if (!es->bEnableState)
3538 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3539 GetClipBox(dc, &rcRgn);
3540 if (es->style & ES_MULTILINE) {
3541 INT vlc = get_vertical_line_count(es);
3542 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3543 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3544 if (IntersectRect(&rc, &rcRgn, &rcLine))
3545 EDIT_PaintLine(es, dc, i, rev);
3547 } else {
3548 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3549 if (IntersectRect(&rc, &rcRgn, &rcLine))
3550 EDIT_PaintLine(es, dc, 0, rev);
3552 if (es->font)
3553 SelectObject(dc, old_font);
3555 if (!hdc)
3556 EndPaint(es->hwndSelf, &ps);
3560 /*********************************************************************
3562 * WM_SETFOCUS
3565 static void EDIT_WM_SetFocus(EDITSTATE *es)
3567 es->flags |= EF_FOCUSED;
3569 if (!(es->style & ES_NOHIDESEL))
3570 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3572 /* single line edit updates itself */
3573 if (!(es->style & ES_MULTILINE))
3575 HDC hdc = GetDC(es->hwndSelf);
3576 EDIT_WM_Paint(es, hdc);
3577 ReleaseDC(es->hwndSelf, hdc);
3580 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3581 EDIT_SetCaretPos(es, es->selection_end,
3582 es->flags & EF_AFTER_WRAP);
3583 ShowCaret(es->hwndSelf);
3584 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3588 /*********************************************************************
3590 * WM_SETFONT
3592 * With Win95 look the margins are set to default font value unless
3593 * the system font (font == 0) is being set, in which case they are left
3594 * unchanged.
3597 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3599 TEXTMETRICW tm;
3600 HDC dc;
3601 HFONT old_font = 0;
3602 RECT clientRect;
3604 es->font = font;
3605 dc = GetDC(es->hwndSelf);
3606 if (font)
3607 old_font = SelectObject(dc, font);
3608 GetTextMetricsW(dc, &tm);
3609 es->line_height = tm.tmHeight;
3610 es->char_width = tm.tmAveCharWidth;
3611 if (font)
3612 SelectObject(dc, old_font);
3613 ReleaseDC(es->hwndSelf, dc);
3615 /* Reset the format rect and the margins */
3616 GetClientRect(es->hwndSelf, &clientRect);
3617 EDIT_SetRectNP(es, &clientRect);
3618 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3619 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3621 if (es->style & ES_MULTILINE)
3622 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3623 else
3624 EDIT_CalcLineWidth_SL(es);
3626 if (redraw)
3627 EDIT_UpdateText(es, NULL, TRUE);
3628 if (es->flags & EF_FOCUSED) {
3629 DestroyCaret();
3630 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3631 EDIT_SetCaretPos(es, es->selection_end,
3632 es->flags & EF_AFTER_WRAP);
3633 ShowCaret(es->hwndSelf);
3638 /*********************************************************************
3640 * WM_SETTEXT
3642 * NOTES
3643 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3644 * The modified flag is reset. No notifications are sent.
3646 * For single-line controls, reception of WM_SETTEXT triggers:
3647 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3650 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3652 LPWSTR textW = NULL;
3653 if (!unicode && text)
3655 LPCSTR textA = (LPCSTR)text;
3656 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3657 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3658 if (textW)
3659 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3660 text = textW;
3663 if (es->flags & EF_UPDATE)
3664 /* fixed this bug once; complain if we see it about to happen again. */
3665 ERR("SetSel may generate UPDATE message whose handler may reset "
3666 "selection.\n");
3668 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3669 if (text)
3671 TRACE("%s\n", debugstr_w(text));
3672 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
3673 if(!unicode)
3674 HeapFree(GetProcessHeap(), 0, textW);
3676 else
3678 TRACE("<NULL>\n");
3679 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
3681 es->x_offset = 0;
3682 es->flags &= ~EF_MODIFIED;
3683 EDIT_EM_SetSel(es, 0, 0, FALSE);
3685 /* Send the notification after the selection start and end have been set
3686 * edit control doesn't send notification on WM_SETTEXT
3687 * if it is multiline, or it is part of combobox
3689 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3691 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3692 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3694 EDIT_EM_ScrollCaret(es);
3695 EDIT_UpdateScrollInfo(es);
3699 /*********************************************************************
3701 * WM_SIZE
3704 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
3706 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3707 RECT rc;
3708 TRACE("width = %d, height = %d\n", width, height);
3709 SetRect(&rc, 0, 0, width, height);
3710 EDIT_SetRectNP(es, &rc);
3711 EDIT_UpdateText(es, NULL, TRUE);
3716 /*********************************************************************
3718 * WM_STYLECHANGED
3720 * This message is sent by SetWindowLong on having changed either the Style
3721 * or the extended style.
3723 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3725 * See also EDIT_WM_NCCreate
3727 * It appears that the Windows version of the edit control allows the style
3728 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3729 * style variable which will generally be different. In this function we
3730 * update the internal style based on what changed in the externally visible
3731 * style.
3733 * Much of this content as based upon the MSDN, especially:
3734 * Platform SDK Documentation -> User Interface Services ->
3735 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3736 * Edit Control Styles
3738 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3740 if (GWL_STYLE == which) {
3741 DWORD style_change_mask;
3742 DWORD new_style;
3743 /* Only a subset of changes can be applied after the control
3744 * has been created.
3746 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3747 ES_NUMBER;
3748 if (es->style & ES_MULTILINE)
3749 style_change_mask |= ES_WANTRETURN;
3751 new_style = style->styleNew & style_change_mask;
3753 /* Number overrides lowercase overrides uppercase (at least it
3754 * does in Win95). However I'll bet that ES_NUMBER would be
3755 * invalid under Win 3.1.
3757 if (new_style & ES_NUMBER) {
3758 ; /* do not override the ES_NUMBER */
3759 } else if (new_style & ES_LOWERCASE) {
3760 new_style &= ~ES_UPPERCASE;
3763 es->style = (es->style & ~style_change_mask) | new_style;
3764 } else if (GWL_EXSTYLE == which) {
3765 ; /* FIXME - what is needed here */
3766 } else {
3767 WARN ("Invalid style change %ld\n",which);
3770 return 0;
3773 /*********************************************************************
3775 * WM_SYSKEYDOWN
3778 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
3780 if ((key == VK_BACK) && (key_data & 0x2000)) {
3781 if (EDIT_EM_CanUndo(es))
3782 EDIT_EM_Undo(es);
3783 return 0;
3784 } else if (key == VK_UP || key == VK_DOWN) {
3785 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
3786 return 0;
3788 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
3792 /*********************************************************************
3794 * WM_TIMER
3797 static void EDIT_WM_Timer(EDITSTATE *es)
3799 if (es->region_posx < 0) {
3800 EDIT_MoveBackward(es, TRUE);
3801 } else if (es->region_posx > 0) {
3802 EDIT_MoveForward(es, TRUE);
3805 * FIXME: gotta do some vertical scrolling here, like
3806 * EDIT_EM_LineScroll(hwnd, 0, 1);
3810 /*********************************************************************
3812 * WM_HSCROLL
3815 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3817 INT dx;
3818 INT fw;
3820 if (!(es->style & ES_MULTILINE))
3821 return 0;
3823 if (!(es->style & ES_AUTOHSCROLL))
3824 return 0;
3826 dx = 0;
3827 fw = es->format_rect.right - es->format_rect.left;
3828 switch (action) {
3829 case SB_LINELEFT:
3830 TRACE("SB_LINELEFT\n");
3831 if (es->x_offset)
3832 dx = -es->char_width;
3833 break;
3834 case SB_LINERIGHT:
3835 TRACE("SB_LINERIGHT\n");
3836 if (es->x_offset < es->text_width)
3837 dx = es->char_width;
3838 break;
3839 case SB_PAGELEFT:
3840 TRACE("SB_PAGELEFT\n");
3841 if (es->x_offset)
3842 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3843 break;
3844 case SB_PAGERIGHT:
3845 TRACE("SB_PAGERIGHT\n");
3846 if (es->x_offset < es->text_width)
3847 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3848 break;
3849 case SB_LEFT:
3850 TRACE("SB_LEFT\n");
3851 if (es->x_offset)
3852 dx = -es->x_offset;
3853 break;
3854 case SB_RIGHT:
3855 TRACE("SB_RIGHT\n");
3856 if (es->x_offset < es->text_width)
3857 dx = es->text_width - es->x_offset;
3858 break;
3859 case SB_THUMBTRACK:
3860 TRACE("SB_THUMBTRACK %d\n", pos);
3861 es->flags |= EF_HSCROLL_TRACK;
3862 if(es->style & WS_HSCROLL)
3863 dx = pos - es->x_offset;
3864 else
3866 INT fw, new_x;
3867 /* Sanity check */
3868 if(pos < 0 || pos > 100) return 0;
3869 /* Assume default scroll range 0-100 */
3870 fw = es->format_rect.right - es->format_rect.left;
3871 new_x = pos * (es->text_width - fw) / 100;
3872 dx = es->text_width ? (new_x - es->x_offset) : 0;
3874 break;
3875 case SB_THUMBPOSITION:
3876 TRACE("SB_THUMBPOSITION %d\n", pos);
3877 es->flags &= ~EF_HSCROLL_TRACK;
3878 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3879 dx = pos - es->x_offset;
3880 else
3882 INT fw, new_x;
3883 /* Sanity check */
3884 if(pos < 0 || pos > 100) return 0;
3885 /* Assume default scroll range 0-100 */
3886 fw = es->format_rect.right - es->format_rect.left;
3887 new_x = pos * (es->text_width - fw) / 100;
3888 dx = es->text_width ? (new_x - es->x_offset) : 0;
3890 if (!dx) {
3891 /* force scroll info update */
3892 EDIT_UpdateScrollInfo(es);
3893 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3895 break;
3896 case SB_ENDSCROLL:
3897 TRACE("SB_ENDSCROLL\n");
3898 break;
3900 * FIXME : the next two are undocumented !
3901 * Are we doing the right thing ?
3902 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3903 * although it's also a regular control message.
3905 case EM_GETTHUMB: /* this one is used by NT notepad */
3907 LRESULT ret;
3908 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3909 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3910 else
3912 /* Assume default scroll range 0-100 */
3913 INT fw = es->format_rect.right - es->format_rect.left;
3914 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
3916 TRACE("EM_GETTHUMB: returning %ld\n", ret);
3917 return ret;
3919 case EM_LINESCROLL:
3920 TRACE("EM_LINESCROLL16\n");
3921 dx = pos;
3922 break;
3924 default:
3925 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
3926 action, action);
3927 return 0;
3929 if (dx)
3931 INT fw = es->format_rect.right - es->format_rect.left;
3932 /* check if we are going to move too far */
3933 if(es->x_offset + dx + fw > es->text_width)
3934 dx = es->text_width - fw - es->x_offset;
3935 if(dx)
3936 EDIT_EM_LineScroll_internal(es, dx, 0);
3938 return 0;
3942 /*********************************************************************
3944 * WM_VSCROLL
3947 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
3949 INT dy;
3951 if (!(es->style & ES_MULTILINE))
3952 return 0;
3954 if (!(es->style & ES_AUTOVSCROLL))
3955 return 0;
3957 dy = 0;
3958 switch (action) {
3959 case SB_LINEUP:
3960 case SB_LINEDOWN:
3961 case SB_PAGEUP:
3962 case SB_PAGEDOWN:
3963 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
3964 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
3965 (action == SB_PAGEUP ? "SB_PAGEUP" :
3966 "SB_PAGEDOWN"))));
3967 EDIT_EM_Scroll(es, action);
3968 return 0;
3969 case SB_TOP:
3970 TRACE("SB_TOP\n");
3971 dy = -es->y_offset;
3972 break;
3973 case SB_BOTTOM:
3974 TRACE("SB_BOTTOM\n");
3975 dy = es->line_count - 1 - es->y_offset;
3976 break;
3977 case SB_THUMBTRACK:
3978 TRACE("SB_THUMBTRACK %d\n", pos);
3979 es->flags |= EF_VSCROLL_TRACK;
3980 if(es->style & WS_VSCROLL)
3981 dy = pos - es->y_offset;
3982 else
3984 /* Assume default scroll range 0-100 */
3985 INT vlc, new_y;
3986 /* Sanity check */
3987 if(pos < 0 || pos > 100) return 0;
3988 vlc = get_vertical_line_count(es);
3989 new_y = pos * (es->line_count - vlc) / 100;
3990 dy = es->line_count ? (new_y - es->y_offset) : 0;
3991 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
3992 es->line_count, es->y_offset, pos, dy);
3994 break;
3995 case SB_THUMBPOSITION:
3996 TRACE("SB_THUMBPOSITION %d\n", pos);
3997 es->flags &= ~EF_VSCROLL_TRACK;
3998 if(es->style & WS_VSCROLL)
3999 dy = pos - es->y_offset;
4000 else
4002 /* Assume default scroll range 0-100 */
4003 INT vlc, new_y;
4004 /* Sanity check */
4005 if(pos < 0 || pos > 100) return 0;
4006 vlc = get_vertical_line_count(es);
4007 new_y = pos * (es->line_count - vlc) / 100;
4008 dy = es->line_count ? (new_y - es->y_offset) : 0;
4009 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4010 es->line_count, es->y_offset, pos, dy);
4012 if (!dy)
4014 /* force scroll info update */
4015 EDIT_UpdateScrollInfo(es);
4016 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4018 break;
4019 case SB_ENDSCROLL:
4020 TRACE("SB_ENDSCROLL\n");
4021 break;
4023 * FIXME : the next two are undocumented !
4024 * Are we doing the right thing ?
4025 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4026 * although it's also a regular control message.
4028 case EM_GETTHUMB: /* this one is used by NT notepad */
4030 LRESULT ret;
4031 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4032 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4033 else
4035 /* Assume default scroll range 0-100 */
4036 INT vlc = get_vertical_line_count(es);
4037 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4039 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4040 return ret;
4042 case EM_LINESCROLL:
4043 TRACE("EM_LINESCROLL %d\n", pos);
4044 dy = pos;
4045 break;
4047 default:
4048 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4049 action, action);
4050 return 0;
4052 if (dy)
4053 EDIT_EM_LineScroll(es, 0, dy);
4054 return 0;
4057 /*********************************************************************
4059 * EM_GETTHUMB
4061 * FIXME: is this right ? (or should it be only VSCROLL)
4062 * (and maybe only for edit controls that really have their
4063 * own scrollbars) (and maybe only for multiline controls ?)
4064 * All in all: very poorly documented
4067 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4069 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4070 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4074 /********************************************************************
4076 * The Following code is to handle inline editing from IMEs
4079 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4081 LONG buflen;
4082 LPWSTR lpCompStr = NULL;
4083 LPSTR lpCompStrAttr = NULL;
4084 DWORD dwBufLenAttr;
4086 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4088 if (buflen < 0)
4090 return;
4093 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
4094 if (!lpCompStr)
4096 ERR("Unable to allocate IME CompositionString\n");
4097 return;
4100 if (buflen)
4101 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4102 lpCompStr[buflen/sizeof(WCHAR)] = 0;
4104 if (CompFlag & GCS_COMPATTR)
4107 * We do not use the attributes yet. it would tell us what characters
4108 * are in transition and which are converted or decided upon
4110 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4111 if (dwBufLenAttr)
4113 dwBufLenAttr ++;
4114 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4115 if (!lpCompStrAttr)
4117 ERR("Unable to allocate IME Attribute String\n");
4118 HeapFree(GetProcessHeap(),0,lpCompStr);
4119 return;
4121 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4122 dwBufLenAttr);
4123 lpCompStrAttr[dwBufLenAttr] = 0;
4125 else
4126 lpCompStrAttr = NULL;
4129 /* check for change in composition start */
4130 if (es->selection_end < es->composition_start)
4131 es->composition_start = es->selection_end;
4133 /* replace existing selection string */
4134 es->selection_start = es->composition_start;
4136 if (es->composition_len > 0)
4137 es->selection_end = es->composition_start + es->composition_len;
4138 else
4139 es->selection_end = es->selection_start;
4141 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
4142 es->composition_len = abs(es->composition_start - es->selection_end);
4144 es->selection_start = es->composition_start;
4145 es->selection_end = es->selection_start + es->composition_len;
4147 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4148 HeapFree(GetProcessHeap(),0,lpCompStr);
4151 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4153 LONG buflen;
4154 LPWSTR lpResultStr;
4156 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4157 if (buflen <= 0)
4159 return;
4162 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
4163 if (!lpResultStr)
4165 ERR("Unable to alloc buffer for IME string\n");
4166 return;
4169 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4170 lpResultStr[buflen/sizeof(WCHAR)] = 0;
4172 /* check for change in composition start */
4173 if (es->selection_end < es->composition_start)
4174 es->composition_start = es->selection_end;
4176 es->selection_start = es->composition_start;
4177 es->selection_end = es->composition_start + es->composition_len;
4178 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
4179 es->composition_start = es->selection_end;
4180 es->composition_len = 0;
4182 HeapFree(GetProcessHeap(),0,lpResultStr);
4185 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4187 HIMC hIMC;
4188 int cursor;
4190 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4192 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4193 es->composition_start = es->selection_end;
4196 hIMC = ImmGetContext(hwnd);
4197 if (!hIMC)
4198 return;
4200 if (CompFlag & GCS_RESULTSTR)
4201 EDIT_GetResultStr(hIMC, es);
4202 if (CompFlag & GCS_COMPSTR)
4203 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4204 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4205 ImmReleaseContext(hwnd, hIMC);
4206 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4210 /*********************************************************************
4212 * WM_NCCREATE
4214 * See also EDIT_WM_StyleChanged
4216 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4218 EDITSTATE *es;
4219 UINT alloc_size;
4221 TRACE("Creating %s edit control, style = %08x\n",
4222 unicode ? "Unicode" : "ANSI", lpcs->style);
4224 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4225 return FALSE;
4226 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4229 * Note: since the EDITSTATE has not been fully initialized yet,
4230 * we can't use any API calls that may send
4231 * WM_XXX messages before WM_NCCREATE is completed.
4234 es->is_unicode = unicode;
4235 es->style = lpcs->style;
4237 es->bEnableState = !(es->style & WS_DISABLED);
4239 es->hwndSelf = hwnd;
4240 /* Save parent, which will be notified by EN_* messages */
4241 es->hwndParent = lpcs->hwndParent;
4243 if (es->style & ES_COMBO)
4244 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4246 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4247 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4249 /* Number overrides lowercase overrides uppercase (at least it
4250 * does in Win95). However I'll bet that ES_NUMBER would be
4251 * invalid under Win 3.1.
4253 if (es->style & ES_NUMBER) {
4254 ; /* do not override the ES_NUMBER */
4255 } else if (es->style & ES_LOWERCASE) {
4256 es->style &= ~ES_UPPERCASE;
4258 if (es->style & ES_MULTILINE) {
4259 es->buffer_limit = BUFLIMIT_INITIAL;
4260 if (es->style & WS_VSCROLL)
4261 es->style |= ES_AUTOVSCROLL;
4262 if (es->style & WS_HSCROLL)
4263 es->style |= ES_AUTOHSCROLL;
4264 es->style &= ~ES_PASSWORD;
4265 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4266 /* Confirmed - RIGHT overrides CENTER */
4267 if (es->style & ES_RIGHT)
4268 es->style &= ~ES_CENTER;
4269 es->style &= ~WS_HSCROLL;
4270 es->style &= ~ES_AUTOHSCROLL;
4272 } else {
4273 es->buffer_limit = BUFLIMIT_INITIAL;
4274 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4275 es->style &= ~ES_CENTER;
4276 es->style &= ~WS_HSCROLL;
4277 es->style &= ~WS_VSCROLL;
4278 if (es->style & ES_PASSWORD)
4279 es->password_char = '*';
4282 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4283 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4284 goto cleanup;
4285 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4287 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4288 goto cleanup;
4289 es->undo_buffer_size = es->buffer_size;
4291 if (es->style & ES_MULTILINE)
4292 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4293 goto cleanup;
4294 es->line_count = 1;
4297 * In Win95 look and feel, the WS_BORDER style is replaced by the
4298 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4299 * control a nonclient area so we don't need to draw the border.
4300 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4301 * a nonclient area and we should handle painting the border ourselves.
4303 * When making modifications please ensure that the code still works
4304 * for edit controls created directly with style 0x50800000, exStyle 0
4305 * (which should have a single pixel border)
4307 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4308 es->style &= ~WS_BORDER;
4309 else if (es->style & WS_BORDER)
4310 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4312 return TRUE;
4314 cleanup:
4315 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4316 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4317 HeapFree(GetProcessHeap(), 0, es->undo_text);
4318 if (es->hloc32W) LocalFree(es->hloc32W);
4319 HeapFree(GetProcessHeap(), 0, es);
4320 return FALSE;
4324 /*********************************************************************
4326 * WM_CREATE
4329 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4331 RECT clientRect;
4333 TRACE("%s\n", debugstr_w(name));
4335 * To initialize some final structure members, we call some helper
4336 * functions. However, since the EDITSTATE is not consistent (i.e.
4337 * not fully initialized), we should be very careful which
4338 * functions can be called, and in what order.
4340 EDIT_WM_SetFont(es, 0, FALSE);
4341 EDIT_EM_EmptyUndoBuffer(es);
4343 /* We need to calculate the format rect
4344 (applications may send EM_SETMARGINS before the control gets visible) */
4345 GetClientRect(es->hwndSelf, &clientRect);
4346 EDIT_SetRectNP(es, &clientRect);
4348 if (name && *name) {
4349 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4350 /* if we insert text to the editline, the text scrolls out
4351 * of the window, as the caret is placed after the insert
4352 * pos normally; thus we reset es->selection... to 0 and
4353 * update caret
4355 es->selection_start = es->selection_end = 0;
4356 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4357 * Messages are only to be sent when the USER does something to
4358 * change the contents. So I am removing this EN_CHANGE
4360 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4362 EDIT_EM_ScrollCaret(es);
4364 /* force scroll info update */
4365 EDIT_UpdateScrollInfo(es);
4366 /* The rule seems to return 1 here for success */
4367 /* Power Builder masked edit controls will crash */
4368 /* if not. */
4369 /* FIXME: is that in all cases so ? */
4370 return 1;
4374 /*********************************************************************
4376 * WM_NCDESTROY
4379 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4381 LINEDEF *pc, *pp;
4383 if (es->hloc32W) {
4384 LocalFree(es->hloc32W);
4386 if (es->hloc32A) {
4387 LocalFree(es->hloc32A);
4389 pc = es->first_line_def;
4390 while (pc)
4392 pp = pc->next;
4393 HeapFree(GetProcessHeap(), 0, pc);
4394 pc = pp;
4397 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4398 HeapFree(GetProcessHeap(), 0, es->undo_text);
4399 HeapFree(GetProcessHeap(), 0, es);
4401 return 0;
4405 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4407 if(unicode)
4408 return DefWindowProcW(hwnd, msg, wParam, lParam);
4409 else
4410 return DefWindowProcA(hwnd, msg, wParam, lParam);
4413 /*********************************************************************
4415 * EditWndProc_common
4417 * The messages are in the order of the actual integer values
4418 * (which can be found in include/windows.h)
4420 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4422 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4423 LRESULT result = 0;
4425 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4427 if (!es && msg != WM_NCCREATE)
4428 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4430 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4432 switch (msg) {
4433 case EM_GETSEL:
4434 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4435 break;
4437 case EM_SETSEL:
4438 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4439 EDIT_EM_ScrollCaret(es);
4440 result = 1;
4441 break;
4443 case EM_GETRECT:
4444 if (lParam)
4445 CopyRect((LPRECT)lParam, &es->format_rect);
4446 break;
4448 case EM_SETRECT:
4449 if ((es->style & ES_MULTILINE) && lParam) {
4450 EDIT_SetRectNP(es, (LPRECT)lParam);
4451 EDIT_UpdateText(es, NULL, TRUE);
4453 break;
4455 case EM_SETRECTNP:
4456 if ((es->style & ES_MULTILINE) && lParam)
4457 EDIT_SetRectNP(es, (LPRECT)lParam);
4458 break;
4460 case EM_SCROLL:
4461 result = EDIT_EM_Scroll(es, (INT)wParam);
4462 break;
4464 case EM_LINESCROLL:
4465 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4466 break;
4468 case EM_SCROLLCARET:
4469 EDIT_EM_ScrollCaret(es);
4470 result = 1;
4471 break;
4473 case EM_GETMODIFY:
4474 result = ((es->flags & EF_MODIFIED) != 0);
4475 break;
4477 case EM_SETMODIFY:
4478 if (wParam)
4479 es->flags |= EF_MODIFIED;
4480 else
4481 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4482 break;
4484 case EM_GETLINECOUNT:
4485 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4486 break;
4488 case EM_LINEINDEX:
4489 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4490 break;
4492 case EM_SETHANDLE:
4493 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4494 break;
4496 case EM_GETHANDLE:
4497 result = (LRESULT)EDIT_EM_GetHandle(es);
4498 break;
4500 case EM_GETTHUMB:
4501 result = EDIT_EM_GetThumb(es);
4502 break;
4504 /* these messages missing from specs */
4505 case 0x00bf:
4506 case 0x00c0:
4507 case 0x00c3:
4508 case 0x00ca:
4509 FIXME("undocumented message 0x%x, please report\n", msg);
4510 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4511 break;
4513 case EM_LINELENGTH:
4514 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4515 break;
4517 case EM_REPLACESEL:
4519 LPWSTR textW;
4521 if(unicode)
4522 textW = (LPWSTR)lParam;
4523 else
4525 LPSTR textA = (LPSTR)lParam;
4526 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4527 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4528 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4531 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
4532 result = 1;
4534 if(!unicode)
4535 HeapFree(GetProcessHeap(), 0, textW);
4536 break;
4539 case EM_GETLINE:
4540 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4541 break;
4543 case EM_SETLIMITTEXT:
4544 EDIT_EM_SetLimitText(es, wParam);
4545 break;
4547 case EM_CANUNDO:
4548 result = (LRESULT)EDIT_EM_CanUndo(es);
4549 break;
4551 case EM_UNDO:
4552 case WM_UNDO:
4553 result = (LRESULT)EDIT_EM_Undo(es);
4554 break;
4556 case EM_FMTLINES:
4557 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4558 break;
4560 case EM_LINEFROMCHAR:
4561 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4562 break;
4564 case EM_SETTABSTOPS:
4565 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4566 break;
4568 case EM_SETPASSWORDCHAR:
4570 WCHAR charW = 0;
4572 if(unicode)
4573 charW = (WCHAR)wParam;
4574 else
4576 CHAR charA = wParam;
4577 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4580 EDIT_EM_SetPasswordChar(es, charW);
4581 break;
4584 case EM_EMPTYUNDOBUFFER:
4585 EDIT_EM_EmptyUndoBuffer(es);
4586 break;
4588 case EM_GETFIRSTVISIBLELINE:
4589 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4590 break;
4592 case EM_SETREADONLY:
4594 DWORD old_style = es->style;
4596 if (wParam) {
4597 SetWindowLongW( hwnd, GWL_STYLE,
4598 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4599 es->style |= ES_READONLY;
4600 } else {
4601 SetWindowLongW( hwnd, GWL_STYLE,
4602 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4603 es->style &= ~ES_READONLY;
4606 if (old_style ^ es->style)
4607 InvalidateRect(es->hwndSelf, NULL, TRUE);
4609 result = 1;
4610 break;
4613 case EM_SETWORDBREAKPROC:
4614 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4615 break;
4617 case EM_GETWORDBREAKPROC:
4618 result = (LRESULT)es->word_break_proc;
4619 break;
4621 case EM_GETPASSWORDCHAR:
4623 if(unicode)
4624 result = es->password_char;
4625 else
4627 WCHAR charW = es->password_char;
4628 CHAR charA = 0;
4629 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4630 result = charA;
4632 break;
4635 case EM_SETMARGINS:
4636 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4637 break;
4639 case EM_GETMARGINS:
4640 result = MAKELONG(es->left_margin, es->right_margin);
4641 break;
4643 case EM_GETLIMITTEXT:
4644 result = es->buffer_limit;
4645 break;
4647 case EM_POSFROMCHAR:
4648 if ((INT)wParam >= get_text_length(es)) result = -1;
4649 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4650 break;
4652 case EM_CHARFROMPOS:
4653 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4654 break;
4656 /* End of the EM_ messages which were in numerical order; what order
4657 * are these in? vaguely alphabetical?
4660 case WM_NCCREATE:
4661 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4662 break;
4664 case WM_NCDESTROY:
4665 result = EDIT_WM_NCDestroy(es);
4666 es = NULL;
4667 break;
4669 case WM_GETDLGCODE:
4670 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4672 if (es->style & ES_MULTILINE)
4673 result |= DLGC_WANTALLKEYS;
4675 if (lParam)
4677 es->flags|=EF_DIALOGMODE;
4679 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4681 int vk = (int)((LPMSG)lParam)->wParam;
4683 if (es->hwndListBox)
4685 if (vk == VK_RETURN || vk == VK_ESCAPE)
4686 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4687 result |= DLGC_WANTMESSAGE;
4691 break;
4693 case WM_IME_CHAR:
4694 if (!unicode)
4696 WCHAR charW;
4697 CHAR strng[2];
4699 strng[0] = wParam >> 8;
4700 strng[1] = wParam & 0xff;
4701 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4702 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4703 result = EDIT_WM_Char(es, charW);
4704 break;
4706 /* fall through */
4707 case WM_CHAR:
4709 WCHAR charW;
4711 if(unicode)
4712 charW = wParam;
4713 else
4715 CHAR charA = wParam;
4716 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4719 if (es->hwndListBox)
4721 if (charW == VK_RETURN || charW == VK_ESCAPE)
4723 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4724 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4725 break;
4728 result = EDIT_WM_Char(es, charW);
4729 break;
4732 case WM_UNICHAR:
4733 if (unicode)
4735 if (wParam == UNICODE_NOCHAR) return TRUE;
4736 if (wParam <= 0x000fffff)
4738 if(wParam > 0xffff) /* convert to surrogates */
4740 wParam -= 0x10000;
4741 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4742 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4744 else EDIT_WM_Char(es, wParam);
4746 return 0;
4748 break;
4750 case WM_CLEAR:
4751 EDIT_WM_Clear(es);
4752 break;
4754 case WM_COMMAND:
4755 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
4756 break;
4758 case WM_CONTEXTMENU:
4759 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4760 break;
4762 case WM_COPY:
4763 EDIT_WM_Copy(es);
4764 break;
4766 case WM_CREATE:
4767 if(unicode)
4768 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4769 else
4771 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4772 LPWSTR nameW = NULL;
4773 if(nameA)
4775 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
4776 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4777 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
4779 result = EDIT_WM_Create(es, nameW);
4780 HeapFree(GetProcessHeap(), 0, nameW);
4782 break;
4784 case WM_CUT:
4785 EDIT_WM_Cut(es);
4786 break;
4788 case WM_ENABLE:
4789 es->bEnableState = (BOOL) wParam;
4790 EDIT_UpdateText(es, NULL, TRUE);
4791 break;
4793 case WM_ERASEBKGND:
4794 /* we do the proper erase in EDIT_WM_Paint */
4795 result = 1;
4796 break;
4798 case WM_GETFONT:
4799 result = (LRESULT)es->font;
4800 break;
4802 case WM_GETTEXT:
4803 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
4804 break;
4806 case WM_GETTEXTLENGTH:
4807 if (unicode) result = get_text_length(es);
4808 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
4809 NULL, 0, NULL, NULL );
4810 break;
4812 case WM_HSCROLL:
4813 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4814 break;
4816 case WM_KEYDOWN:
4817 result = EDIT_WM_KeyDown(es, (INT)wParam);
4818 break;
4820 case WM_KILLFOCUS:
4821 result = EDIT_WM_KillFocus(es);
4822 break;
4824 case WM_LBUTTONDBLCLK:
4825 result = EDIT_WM_LButtonDblClk(es);
4826 break;
4828 case WM_LBUTTONDOWN:
4829 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
4830 break;
4832 case WM_LBUTTONUP:
4833 result = EDIT_WM_LButtonUp(es);
4834 break;
4836 case WM_MBUTTONDOWN:
4837 result = EDIT_WM_MButtonDown(es);
4838 break;
4840 case WM_MOUSEMOVE:
4841 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4842 break;
4844 case WM_PRINTCLIENT:
4845 case WM_PAINT:
4846 EDIT_WM_Paint(es, (HDC)wParam);
4847 break;
4849 case WM_PASTE:
4850 EDIT_WM_Paste(es);
4851 break;
4853 case WM_SETFOCUS:
4854 EDIT_WM_SetFocus(es);
4855 break;
4857 case WM_SETFONT:
4858 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
4859 break;
4861 case WM_SETREDRAW:
4862 /* FIXME: actually set an internal flag and behave accordingly */
4863 break;
4865 case WM_SETTEXT:
4866 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
4867 result = TRUE;
4868 break;
4870 case WM_SIZE:
4871 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
4872 break;
4874 case WM_STYLECHANGED:
4875 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
4876 break;
4878 case WM_STYLECHANGING:
4879 result = 0; /* See EDIT_WM_StyleChanged */
4880 break;
4882 case WM_SYSKEYDOWN:
4883 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
4884 break;
4886 case WM_TIMER:
4887 EDIT_WM_Timer(es);
4888 break;
4890 case WM_VSCROLL:
4891 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4892 break;
4894 case WM_MOUSEWHEEL:
4896 int gcWheelDelta = 0;
4897 UINT pulScrollLines = 3;
4898 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4900 if (wParam & (MK_SHIFT | MK_CONTROL)) {
4901 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4902 break;
4904 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
4905 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4907 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
4908 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
4909 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
4912 break;
4915 /* IME messages to make the edit control IME aware */
4916 case WM_IME_SETCONTEXT:
4917 break;
4919 case WM_IME_STARTCOMPOSITION:
4920 es->composition_start = es->selection_end;
4921 es->composition_len = 0;
4922 break;
4924 case WM_IME_COMPOSITION:
4925 EDIT_ImeComposition(hwnd, lParam, es);
4926 break;
4928 case WM_IME_ENDCOMPOSITION:
4929 if (es->composition_len > 0)
4931 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4932 es->selection_end = es->selection_start;
4933 es->composition_len= 0;
4935 break;
4937 case WM_IME_COMPOSITIONFULL:
4938 break;
4940 case WM_IME_SELECT:
4941 break;
4943 case WM_IME_CONTROL:
4944 break;
4946 default:
4947 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4948 break;
4951 if (IsWindow(hwnd) && es) EDIT_UnlockBuffer(es, FALSE);
4953 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
4955 return result;
4959 /*********************************************************************
4960 * edit class descriptor
4962 static const WCHAR editW[] = {'E','d','i','t',0};
4963 const struct builtin_class_descr EDIT_builtin_class =
4965 editW, /* name */
4966 CS_DBLCLKS | CS_PARENTDC, /* style */
4967 WINPROC_EDIT, /* proc */
4968 #ifdef __i386__
4969 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
4970 #else
4971 sizeof(EDITSTATE *), /* extra */
4972 #endif
4973 IDC_IBEAM, /* cursor */
4974 0 /* brush */