user32: Use uniscribe ScriptBreak to handle edit control linebreaking.
[wine/multimedia.git] / dlls / user32 / edit.c
blobe02a2683941b29af28d7de4198c04fce82150d31
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 "usp10.h"
57 #include "wine/unicode.h"
58 #include "controls.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(edit);
63 WINE_DECLARE_DEBUG_CHANNEL(combo);
64 WINE_DECLARE_DEBUG_CHANNEL(relay);
66 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
67 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
68 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
69 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
72 * extra flags for EDITSTATE.flags field
74 #define EF_MODIFIED 0x0001 /* text has been modified */
75 #define EF_FOCUSED 0x0002 /* we have input focus */
76 #define EF_UPDATE 0x0004 /* notify parent of changed state */
77 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
79 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
80 wrapped line, instead of in front of the next character */
81 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
82 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
83 the text buffer if this is clear. */
84 #define EF_DIALOGMODE 0x0400 /* Indicates that we are inside a dialog window */
86 typedef enum
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
93 } LINE_END;
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
98 LINE_END ending;
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
102 } LINEDEF;
104 typedef struct
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
109 UINT buffer_size; /* the size of the buffer in characters */
110 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
111 HFONT font; /* NULL means standard system font */
112 INT x_offset; /* scroll offset for multi lines this is in pixels
113 for single lines it's in characters */
114 INT line_height; /* height of a screen line in pixels */
115 INT char_width; /* average character width in pixels */
116 DWORD style; /* sane version of wnd->dwStyle */
117 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
118 INT undo_insert_count; /* number of characters inserted in sequence */
119 UINT undo_position; /* character index of the insertion and deletion */
120 LPWSTR undo_text; /* deleted text */
121 UINT undo_buffer_size; /* size of the deleted text buffer */
122 INT selection_start; /* == selection_end if no selection */
123 INT selection_end; /* == current caret position */
124 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
125 INT left_margin; /* in pixels */
126 INT right_margin; /* in pixels */
127 RECT format_rect;
128 INT text_width; /* width of the widest line in pixels for multi line controls
129 and just line width for single line controls */
130 INT region_posx; /* Position of cursor relative to region: */
131 INT region_posy; /* -1: to left, 0: within, 1: to right */
132 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
133 INT line_count; /* number of lines */
134 INT y_offset; /* scroll offset in number of lines */
135 BOOL bCaptureState; /* flag indicating whether mouse was captured */
136 BOOL bEnableState; /* flag keeping the enable state */
137 HWND hwndSelf; /* the our window handle */
138 HWND hwndParent; /* Handle of parent for sending EN_* messages.
139 Even if parent will change, EN_* messages
140 should be sent to the first parent. */
141 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
143 * only for multi line controls
145 INT lock_count; /* amount of re-entries in the EditWndProc */
146 INT tabs_count;
147 LPINT tabs;
148 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
149 HLOCAL hloc32W; /* our unicode local memory block */
150 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
151 or EM_SETHANDLE */
153 * IME Data
155 UINT composition_len; /* length of composition, 0 == no composition */
156 int composition_start; /* the character position for the composition */
158 * Uniscribe Data
160 SCRIPT_LOGATTR *logAttr;
161 } EDITSTATE;
164 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
165 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
167 /* used for disabled or read-only edit control */
168 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
169 do \
170 { /* Notify parent which has created this edit control */ \
171 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
172 SendMessageW(es->hwndParent, WM_COMMAND, \
173 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
174 (LPARAM)(es->hwndSelf)); \
175 } while(0)
177 static const WCHAR empty_stringW[] = {0};
179 /*********************************************************************
181 * EM_CANUNDO
184 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
186 return (es->undo_insert_count || strlenW(es->undo_text));
190 /*********************************************************************
192 * EM_EMPTYUNDOBUFFER
195 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
197 es->undo_insert_count = 0;
198 *es->undo_text = '\0';
202 /**********************************************************************
203 * get_app_version
205 * Returns the window version in case Wine emulates a later version
206 * of windows than the application expects.
208 * In a number of cases when windows runs an application that was
209 * designed for an earlier windows version, windows reverts
210 * to "old" behaviour of that earlier version.
212 * An example is a disabled edit control that needs to be painted.
213 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
214 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
215 * applications with an expected version 0f 4.0 or higher.
218 static DWORD get_app_version(void)
220 static DWORD version;
221 if (!version)
223 DWORD dwEmulatedVersion;
224 OSVERSIONINFOW info;
225 DWORD dwProcVersion = GetProcessVersion(0);
227 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
228 GetVersionExW( &info );
229 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
230 /* FIXME: this may not be 100% correct; see discussion on the
231 * wine developer list in Nov 1999 */
232 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
234 return version;
237 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
239 HBRUSH hbrush;
240 UINT msg;
242 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
243 msg = WM_CTLCOLORSTATIC;
244 else
245 msg = WM_CTLCOLOREDIT;
247 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
248 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
249 if (!hbrush)
250 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
251 return hbrush;
255 static inline UINT get_text_length(EDITSTATE *es)
257 if(es->text_length == (UINT)-1)
258 es->text_length = strlenW(es->text);
259 return es->text_length;
263 /*********************************************************************
265 * EDIT_WordBreakProc
267 * Find the beginning of words.
268 * Note: unlike the specs for a WordBreakProc, this function only
269 * allows to be called without linebreaks between s[0] up to
270 * s[count - 1]. Remember it is only called
271 * internally, so we can decide this for ourselves.
272 * Additional we will always be breaking the full string.
275 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
277 INT ret = 0;
279 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
281 if(!s) return 0;
283 if (!es->logAttr)
285 SCRIPT_ANALYSIS psa;
287 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
288 psa.eScript = SCRIPT_UNDEFINED;
290 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
291 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
294 switch (action) {
295 case WB_LEFT:
296 if (index)
297 index--;
298 while (index && !es->logAttr[index].fSoftBreak)
299 index--;
300 ret = index;
301 break;
302 case WB_RIGHT:
303 if (!count)
304 break;
305 while (s[index] && index < count && !es->logAttr[index].fSoftBreak)
306 index++;
307 ret = index;
308 break;
309 case WB_ISDELIMITER:
310 ret = es->logAttr[index].fWhiteSpace;
311 break;
312 default:
313 ERR("unknown action code, please report !\n");
314 break;
316 return ret;
320 /*********************************************************************
322 * EDIT_CallWordBreakProc
324 * Call appropriate WordBreakProc (internal or external).
326 * Note: The "start" argument should always be an index referring
327 * to es->text. The actual wordbreak proc might be
328 * 16 bit, so we can't always pass any 32 bit LPSTR.
329 * Hence we assume that es->text is the buffer that holds
330 * the string under examination (we can decide this for ourselves).
333 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
335 INT ret;
337 if (es->word_break_proc)
339 if(es->is_unicode)
341 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
343 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
344 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
345 ret = wbpW(es->text + start, index, count, action);
347 else
349 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
350 INT countA;
351 CHAR *textA;
353 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
354 textA = HeapAlloc(GetProcessHeap(), 0, countA);
355 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
356 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
357 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
358 ret = wbpA(textA, index, countA, action);
359 HeapFree(GetProcessHeap(), 0, textA);
362 else
363 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
365 return ret;
368 /*********************************************************************
370 * EDIT_BuildLineDefs_ML
372 * Build linked list of text lines.
373 * Lines can end with '\0' (last line), a character (if it is wrapped),
374 * a soft return '\r\r\n' or a hard return '\r\n'
377 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
379 HDC dc;
380 HFONT old_font = 0;
381 LPWSTR current_position, cp;
382 INT fw;
383 LINEDEF *current_line;
384 LINEDEF *previous_line;
385 LINEDEF *start_line;
386 INT line_index = 0, nstart_line = 0, nstart_index = 0;
387 INT line_count = es->line_count;
388 INT orig_net_length;
389 RECT rc;
391 if (istart == iend && delta == 0)
392 return;
394 dc = GetDC(es->hwndSelf);
395 if (es->font)
396 old_font = SelectObject(dc, es->font);
398 previous_line = NULL;
399 current_line = es->first_line_def;
401 /* Find starting line. istart must lie inside an existing line or
402 * at the end of buffer */
403 do {
404 if (istart < current_line->index + current_line->length ||
405 current_line->ending == END_0)
406 break;
408 previous_line = current_line;
409 current_line = current_line->next;
410 line_index++;
411 } while (current_line);
413 if (!current_line) /* Error occurred start is not inside previous buffer */
415 FIXME(" modification occurred outside buffer\n");
416 ReleaseDC(es->hwndSelf, dc);
417 return;
420 /* Remember start of modifications in order to calculate update region */
421 nstart_line = line_index;
422 nstart_index = current_line->index;
424 /* We must start to reformat from the previous line since the modifications
425 * may have caused the line to wrap upwards. */
426 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
428 line_index--;
429 current_line = previous_line;
431 start_line = current_line;
433 fw = es->format_rect.right - es->format_rect.left;
434 current_position = es->text + current_line->index;
435 do {
436 if (current_line != start_line)
438 if (!current_line || current_line->index + delta > current_position - es->text)
440 /* The buffer has been expanded, create a new line and
441 insert it into the link list */
442 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
443 new_line->next = previous_line->next;
444 previous_line->next = new_line;
445 current_line = new_line;
446 es->line_count++;
448 else if (current_line->index + delta < current_position - es->text)
450 /* The previous line merged with this line so we delete this extra entry */
451 previous_line->next = current_line->next;
452 HeapFree(GetProcessHeap(), 0, current_line);
453 current_line = previous_line->next;
454 es->line_count--;
455 continue;
457 else /* current_line->index + delta == current_position */
459 if (current_position - es->text > iend)
460 break; /* We reached end of line modifications */
461 /* else recalculate this line */
465 current_line->index = current_position - es->text;
466 orig_net_length = current_line->net_length;
468 /* Find end of line */
469 cp = current_position;
470 while (*cp) {
471 if (*cp == '\n') break;
472 if ((*cp == '\r') && (*(cp + 1) == '\n'))
473 break;
474 cp++;
477 /* Mark type of line termination */
478 if (!(*cp)) {
479 current_line->ending = END_0;
480 current_line->net_length = strlenW(current_position);
481 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
482 current_line->ending = END_SOFT;
483 current_line->net_length = cp - current_position - 1;
484 } else if (*cp == '\n') {
485 current_line->ending = END_RICH;
486 current_line->net_length = cp - current_position;
487 } else {
488 current_line->ending = END_HARD;
489 current_line->net_length = cp - current_position;
492 /* Calculate line width */
493 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
494 current_position, current_line->net_length,
495 es->tabs_count, es->tabs));
497 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
498 if (!(es->style & ES_AUTOHSCROLL)) {
499 if (current_line->width > fw) {
500 INT next = 0;
501 INT prev;
502 do {
503 prev = next;
504 next = EDIT_CallWordBreakProc(es, current_position - es->text,
505 prev + 1, current_line->net_length, WB_RIGHT);
506 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
507 current_position, next, es->tabs_count, es->tabs));
508 } while (current_line->width <= fw);
509 if (!prev) { /* Didn't find a line break so force a break */
510 next = 0;
511 do {
512 prev = next;
513 next++;
514 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
515 current_position, next, es->tabs_count, es->tabs));
516 } while (current_line->width <= fw);
517 if (!prev)
518 prev = 1;
521 /* If the first line we are calculating, wrapped before istart, we must
522 * adjust istart in order for this to be reflected in the update region. */
523 if (current_line->index == nstart_index && istart > current_line->index + prev)
524 istart = current_line->index + prev;
525 /* else if we are updating the previous line before the first line we
526 * are re-calculating and it expanded */
527 else if (current_line == start_line &&
528 current_line->index != nstart_index && orig_net_length < prev)
530 /* Line expanded due to an upwards line wrap so we must partially include
531 * previous line in update region */
532 nstart_line = line_index;
533 nstart_index = current_line->index;
534 istart = current_line->index + orig_net_length;
537 current_line->net_length = prev;
538 current_line->ending = END_WRAP;
539 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
540 current_line->net_length, es->tabs_count, es->tabs));
542 else if (current_line == start_line &&
543 current_line->index != nstart_index &&
544 orig_net_length < current_line->net_length) {
545 /* The previous line expanded but it's still not as wide as the client rect */
546 /* The expansion is due to an upwards line wrap so we must partially include
547 it in the update region */
548 nstart_line = line_index;
549 nstart_index = current_line->index;
550 istart = current_line->index + orig_net_length;
555 /* Adjust length to include line termination */
556 switch (current_line->ending) {
557 case END_SOFT:
558 current_line->length = current_line->net_length + 3;
559 break;
560 case END_RICH:
561 current_line->length = current_line->net_length + 1;
562 break;
563 case END_HARD:
564 current_line->length = current_line->net_length + 2;
565 break;
566 case END_WRAP:
567 case END_0:
568 current_line->length = current_line->net_length;
569 break;
571 es->text_width = max(es->text_width, current_line->width);
572 current_position += current_line->length;
573 previous_line = current_line;
574 current_line = current_line->next;
575 line_index++;
576 } while (previous_line->ending != END_0);
578 /* Finish adjusting line indexes by delta or remove hanging lines */
579 if (previous_line->ending == END_0)
581 LINEDEF *pnext = NULL;
583 previous_line->next = NULL;
584 while (current_line)
586 pnext = current_line->next;
587 HeapFree(GetProcessHeap(), 0, current_line);
588 current_line = pnext;
589 es->line_count--;
592 else if (delta != 0)
594 while (current_line)
596 current_line->index += delta;
597 current_line = current_line->next;
601 /* Calculate rest of modification rectangle */
602 if (hrgn)
604 HRGN tmphrgn;
606 * We calculate two rectangles. One for the first line which may have
607 * an indent with respect to the format rect. The other is a format-width
608 * rectangle that spans the rest of the lines that changed or moved.
610 rc.top = es->format_rect.top + nstart_line * es->line_height -
611 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
612 rc.bottom = rc.top + es->line_height;
613 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
614 rc.left = es->format_rect.left;
615 else
616 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
617 es->text + nstart_index, istart - nstart_index,
618 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
619 rc.right = es->format_rect.right;
620 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
622 rc.top = rc.bottom;
623 rc.left = es->format_rect.left;
624 rc.right = es->format_rect.right;
626 * If lines were added or removed we must re-paint the remainder of the
627 * lines since the remaining lines were either shifted up or down.
629 if (line_count < es->line_count) /* We added lines */
630 rc.bottom = es->line_count * es->line_height;
631 else if (line_count > es->line_count) /* We removed lines */
632 rc.bottom = line_count * es->line_height;
633 else
634 rc.bottom = line_index * es->line_height;
635 rc.bottom += es->format_rect.top;
636 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
637 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
638 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
639 DeleteObject(tmphrgn);
642 if (es->font)
643 SelectObject(dc, old_font);
645 ReleaseDC(es->hwndSelf, dc);
648 /*********************************************************************
650 * EDIT_GetPasswordPointer_SL
652 * note: caller should free the (optionally) allocated buffer
655 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
657 if (es->style & ES_PASSWORD) {
658 INT len = get_text_length(es);
659 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
660 text[len] = '\0';
661 while(len) text[--len] = es->password_char;
662 return text;
663 } else
664 return es->text;
668 /*********************************************************************
670 * EDIT_CalcLineWidth_SL
673 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
675 SIZE size;
676 LPWSTR text;
677 HDC dc;
678 HFONT old_font = 0;
680 text = EDIT_GetPasswordPointer_SL(es);
682 dc = GetDC(es->hwndSelf);
683 if (es->font)
684 old_font = SelectObject(dc, es->font);
686 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
688 if (es->font)
689 SelectObject(dc, old_font);
690 ReleaseDC(es->hwndSelf, dc);
692 if (es->style & ES_PASSWORD)
693 HeapFree(GetProcessHeap(), 0, text);
695 es->text_width = size.cx;
698 /*********************************************************************
700 * EDIT_CharFromPos
702 * Beware: This is not the function called on EM_CHARFROMPOS
703 * The position _can_ be outside the formatting / client
704 * rectangle
705 * The return value is only the character index
708 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
710 INT index;
711 HDC dc;
712 HFONT old_font = 0;
713 INT x_high = 0, x_low = 0;
715 if (es->style & ES_MULTILINE) {
716 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
717 INT line_index = 0;
718 LINEDEF *line_def = es->first_line_def;
719 INT low, high;
720 while ((line > 0) && line_def->next) {
721 line_index += line_def->length;
722 line_def = line_def->next;
723 line--;
725 x += es->x_offset - es->format_rect.left;
726 if (es->style & ES_RIGHT)
727 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
728 else if (es->style & ES_CENTER)
729 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
730 if (x >= line_def->width) {
731 if (after_wrap)
732 *after_wrap = (line_def->ending == END_WRAP);
733 return line_index + line_def->net_length;
735 if (x <= 0) {
736 if (after_wrap)
737 *after_wrap = FALSE;
738 return line_index;
740 dc = GetDC(es->hwndSelf);
741 if (es->font)
742 old_font = SelectObject(dc, es->font);
743 low = line_index;
744 high = line_index + line_def->net_length + 1;
745 while (low < high - 1)
747 INT mid = (low + high) / 2;
748 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
749 if (x_now > x) {
750 high = mid;
751 x_high = x_now;
752 } else {
753 low = mid;
754 x_low = x_now;
757 if (abs(x_high - x) + 1 <= abs(x_low - x))
758 index = high;
759 else
760 index = low;
762 if (after_wrap)
763 *after_wrap = ((index == line_index + line_def->net_length) &&
764 (line_def->ending == END_WRAP));
765 } else {
766 LPWSTR text;
767 SIZE size;
768 if (after_wrap)
769 *after_wrap = FALSE;
770 x -= es->format_rect.left;
771 if (!x)
772 return es->x_offset;
774 if (!es->x_offset)
776 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
777 if (es->style & ES_RIGHT)
778 x -= indent;
779 else if (es->style & ES_CENTER)
780 x -= indent / 2;
783 text = EDIT_GetPasswordPointer_SL(es);
784 dc = GetDC(es->hwndSelf);
785 if (es->font)
786 old_font = SelectObject(dc, es->font);
787 if (x < 0)
789 INT low = 0;
790 INT high = es->x_offset;
791 while (low < high - 1)
793 INT mid = (low + high) / 2;
794 GetTextExtentPoint32W( dc, text + mid,
795 es->x_offset - mid, &size );
796 if (size.cx > -x) {
797 low = mid;
798 x_low = size.cx;
799 } else {
800 high = mid;
801 x_high = size.cx;
804 if (abs(x_high + x) <= abs(x_low + x) + 1)
805 index = high;
806 else
807 index = low;
809 else
811 INT low = es->x_offset;
812 INT high = get_text_length(es) + 1;
813 while (low < high - 1)
815 INT mid = (low + high) / 2;
816 GetTextExtentPoint32W( dc, text + es->x_offset,
817 mid - es->x_offset, &size );
818 if (size.cx > x) {
819 high = mid;
820 x_high = size.cx;
821 } else {
822 low = mid;
823 x_low = size.cx;
826 if (abs(x_high - x) <= abs(x_low - x) + 1)
827 index = high;
828 else
829 index = low;
831 if (es->style & ES_PASSWORD)
832 HeapFree(GetProcessHeap(), 0, text);
834 if (es->font)
835 SelectObject(dc, old_font);
836 ReleaseDC(es->hwndSelf, dc);
837 return index;
841 /*********************************************************************
843 * EDIT_ConfinePoint
845 * adjusts the point to be within the formatting rectangle
846 * (so CharFromPos returns the nearest _visible_ character)
849 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
851 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
852 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
856 /*********************************************************************
858 * EM_LINEFROMCHAR
861 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
863 INT line;
864 LINEDEF *line_def;
866 if (!(es->style & ES_MULTILINE))
867 return 0;
868 if (index > (INT)get_text_length(es))
869 return es->line_count - 1;
870 if (index == -1)
871 index = min(es->selection_start, es->selection_end);
873 line = 0;
874 line_def = es->first_line_def;
875 index -= line_def->length;
876 while ((index >= 0) && line_def->next) {
877 line++;
878 line_def = line_def->next;
879 index -= line_def->length;
881 return line;
885 /*********************************************************************
887 * EM_LINEINDEX
890 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
892 INT line_index;
893 const LINEDEF *line_def;
895 if (!(es->style & ES_MULTILINE))
896 return 0;
897 if (line >= es->line_count)
898 return -1;
900 line_index = 0;
901 line_def = es->first_line_def;
902 if (line == -1) {
903 INT index = es->selection_end - line_def->length;
904 while ((index >= 0) && line_def->next) {
905 line_index += line_def->length;
906 line_def = line_def->next;
907 index -= line_def->length;
909 } else {
910 while (line > 0) {
911 line_index += line_def->length;
912 line_def = line_def->next;
913 line--;
916 return line_index;
920 /*********************************************************************
922 * EM_LINELENGTH
925 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
927 LINEDEF *line_def;
929 if (!(es->style & ES_MULTILINE))
930 return get_text_length(es);
932 if (index == -1) {
933 /* get the number of remaining non-selected chars of selected lines */
934 INT32 l; /* line number */
935 INT32 li; /* index of first char in line */
936 INT32 count;
937 l = EDIT_EM_LineFromChar(es, es->selection_start);
938 /* # chars before start of selection area */
939 count = es->selection_start - EDIT_EM_LineIndex(es, l);
940 l = EDIT_EM_LineFromChar(es, es->selection_end);
941 /* # chars after end of selection */
942 li = EDIT_EM_LineIndex(es, l);
943 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
944 return count;
946 line_def = es->first_line_def;
947 index -= line_def->length;
948 while ((index >= 0) && line_def->next) {
949 line_def = line_def->next;
950 index -= line_def->length;
952 return line_def->net_length;
956 /*********************************************************************
958 * EM_POSFROMCHAR
961 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
963 INT len = get_text_length(es);
964 INT l;
965 INT li;
966 INT x;
967 INT y = 0;
968 INT w;
969 INT lw = 0;
970 INT ll = 0;
971 HDC dc;
972 HFONT old_font = 0;
973 SIZE size;
974 LINEDEF *line_def;
976 index = min(index, len);
977 dc = GetDC(es->hwndSelf);
978 if (es->font)
979 old_font = SelectObject(dc, es->font);
980 if (es->style & ES_MULTILINE) {
981 l = EDIT_EM_LineFromChar(es, index);
982 y = (l - es->y_offset) * es->line_height;
983 li = EDIT_EM_LineIndex(es, l);
984 if (after_wrap && (li == index) && l) {
985 INT l2 = l - 1;
986 line_def = es->first_line_def;
987 while (l2) {
988 line_def = line_def->next;
989 l2--;
991 if (line_def->ending == END_WRAP) {
992 l--;
993 y -= es->line_height;
994 li = EDIT_EM_LineIndex(es, l);
998 line_def = es->first_line_def;
999 while (line_def->index != li)
1000 line_def = line_def->next;
1002 ll = line_def->net_length;
1003 lw = line_def->width;
1005 w = es->format_rect.right - es->format_rect.left;
1006 if (es->style & ES_RIGHT)
1008 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
1009 es->tabs_count, es->tabs)) - es->x_offset;
1010 x = w - x;
1012 else if (es->style & ES_CENTER)
1014 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1015 es->tabs_count, es->tabs)) - es->x_offset;
1016 x += (w - lw) / 2;
1018 else /* ES_LEFT */
1020 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1021 es->tabs_count, es->tabs)) - es->x_offset;
1023 } else {
1024 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
1025 if (index < es->x_offset) {
1026 GetTextExtentPoint32W(dc, text + index,
1027 es->x_offset - index, &size);
1028 x = -size.cx;
1029 } else {
1030 GetTextExtentPoint32W(dc, text + es->x_offset,
1031 index - es->x_offset, &size);
1032 x = size.cx;
1034 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1036 w = es->format_rect.right - es->format_rect.left;
1037 if (w > es->text_width)
1039 if (es->style & ES_RIGHT)
1040 x += w - es->text_width;
1041 else if (es->style & ES_CENTER)
1042 x += (w - es->text_width) / 2;
1046 y = 0;
1047 if (es->style & ES_PASSWORD)
1048 HeapFree(GetProcessHeap(), 0, text);
1050 x += es->format_rect.left;
1051 y += es->format_rect.top;
1052 if (es->font)
1053 SelectObject(dc, old_font);
1054 ReleaseDC(es->hwndSelf, dc);
1055 return MAKELONG((INT16)x, (INT16)y);
1059 /*********************************************************************
1061 * EDIT_GetLineRect
1063 * Calculates the bounding rectangle for a line from a starting
1064 * column to an ending column.
1067 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1069 INT line_index = EDIT_EM_LineIndex(es, line);
1071 if (es->style & ES_MULTILINE)
1072 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1073 else
1074 rc->top = es->format_rect.top;
1075 rc->bottom = rc->top + es->line_height;
1076 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1077 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1081 static inline void text_buffer_changed(EDITSTATE *es)
1083 es->text_length = (UINT)-1;
1085 HeapFree( GetProcessHeap(), 0, es->logAttr );
1086 es->logAttr = NULL;
1089 /*********************************************************************
1090 * EDIT_LockBuffer
1093 static void EDIT_LockBuffer(EDITSTATE *es)
1095 if (!es->text) {
1097 if(!es->hloc32W) return;
1099 if(es->hloc32A)
1101 CHAR *textA = LocalLock(es->hloc32A);
1102 HLOCAL hloc32W_new;
1103 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1104 if(countW_new > es->buffer_size + 1)
1106 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1107 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1108 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1109 if(hloc32W_new)
1111 es->hloc32W = hloc32W_new;
1112 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1113 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1115 else
1116 WARN("FAILED! Will synchronize partially\n");
1118 es->text = LocalLock(es->hloc32W);
1119 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1120 LocalUnlock(es->hloc32A);
1122 else es->text = LocalLock(es->hloc32W);
1124 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1125 es->lock_count++;
1129 /*********************************************************************
1131 * EDIT_UnlockBuffer
1134 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1137 /* Edit window might be already destroyed */
1138 if(!IsWindow(es->hwndSelf))
1140 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1141 return;
1144 if (!es->lock_count) {
1145 ERR("lock_count == 0 ... please report\n");
1146 return;
1148 if (!es->text) {
1149 ERR("es->text == 0 ... please report\n");
1150 return;
1153 if (force || (es->lock_count == 1)) {
1154 if (es->hloc32W) {
1155 UINT countA = 0;
1156 UINT countW = get_text_length(es) + 1;
1158 if(es->hloc32A)
1160 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1161 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1162 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1163 countA = LocalSize(es->hloc32A);
1164 if(countA_new > countA)
1166 HLOCAL hloc32A_new;
1167 UINT alloc_size = ROUND_TO_GROW(countA_new);
1168 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1169 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1170 if(hloc32A_new)
1172 es->hloc32A = hloc32A_new;
1173 countA = LocalSize(hloc32A_new);
1174 TRACE("Real new size %d bytes\n", countA);
1176 else
1177 WARN("FAILED! Will synchronize partially\n");
1179 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1180 LocalLock(es->hloc32A), countA, NULL, NULL);
1181 LocalUnlock(es->hloc32A);
1184 LocalUnlock(es->hloc32W);
1185 es->text = NULL;
1187 else {
1188 ERR("no buffer ... please report\n");
1189 return;
1192 es->lock_count--;
1196 /*********************************************************************
1198 * EDIT_MakeFit
1200 * Try to fit size + 1 characters in the buffer.
1202 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1204 HLOCAL hNew32W;
1206 if (size <= es->buffer_size)
1207 return TRUE;
1209 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1211 /* Force edit to unlock it's buffer. es->text now NULL */
1212 EDIT_UnlockBuffer(es, TRUE);
1214 if (es->hloc32W) {
1215 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1216 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1217 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1218 es->hloc32W = hNew32W;
1219 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1223 EDIT_LockBuffer(es);
1225 if (es->buffer_size < size) {
1226 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1227 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1228 return FALSE;
1229 } else {
1230 TRACE("We now have %d+1\n", es->buffer_size);
1231 return TRUE;
1236 /*********************************************************************
1238 * EDIT_MakeUndoFit
1240 * Try to fit size + 1 bytes in the undo buffer.
1243 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1245 UINT alloc_size;
1247 if (size <= es->undo_buffer_size)
1248 return TRUE;
1250 TRACE("trying to ReAlloc to %d+1\n", size);
1252 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1253 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1254 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1255 return TRUE;
1257 else
1259 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1260 return FALSE;
1265 /*********************************************************************
1267 * EDIT_UpdateTextRegion
1270 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1272 if (es->flags & EF_UPDATE) {
1273 es->flags &= ~EF_UPDATE;
1274 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1276 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1280 /*********************************************************************
1282 * EDIT_UpdateText
1285 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1287 if (es->flags & EF_UPDATE) {
1288 es->flags &= ~EF_UPDATE;
1289 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1291 InvalidateRect(es->hwndSelf, rc, bErase);
1294 /*********************************************************************
1296 * EDIT_SL_InvalidateText
1298 * Called from EDIT_InvalidateText().
1299 * Does the job for single-line controls only.
1302 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1304 RECT line_rect;
1305 RECT rc;
1307 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1308 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1309 EDIT_UpdateText(es, &rc, TRUE);
1313 static inline INT get_vertical_line_count(EDITSTATE *es)
1315 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1316 return max(1,vlc);
1319 /*********************************************************************
1321 * EDIT_ML_InvalidateText
1323 * Called from EDIT_InvalidateText().
1324 * Does the job for multi-line controls only.
1327 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1329 INT vlc = get_vertical_line_count(es);
1330 INT sl = EDIT_EM_LineFromChar(es, start);
1331 INT el = EDIT_EM_LineFromChar(es, end);
1332 INT sc;
1333 INT ec;
1334 RECT rc1;
1335 RECT rcWnd;
1336 RECT rcLine;
1337 RECT rcUpdate;
1338 INT l;
1340 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1341 return;
1343 sc = start - EDIT_EM_LineIndex(es, sl);
1344 ec = end - EDIT_EM_LineIndex(es, el);
1345 if (sl < es->y_offset) {
1346 sl = es->y_offset;
1347 sc = 0;
1349 if (el > es->y_offset + vlc) {
1350 el = es->y_offset + vlc;
1351 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1353 GetClientRect(es->hwndSelf, &rc1);
1354 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1355 if (sl == el) {
1356 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1357 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1358 EDIT_UpdateText(es, &rcUpdate, TRUE);
1359 } else {
1360 EDIT_GetLineRect(es, sl, sc,
1361 EDIT_EM_LineLength(es,
1362 EDIT_EM_LineIndex(es, sl)),
1363 &rcLine);
1364 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1365 EDIT_UpdateText(es, &rcUpdate, TRUE);
1366 for (l = sl + 1 ; l < el ; l++) {
1367 EDIT_GetLineRect(es, l, 0,
1368 EDIT_EM_LineLength(es,
1369 EDIT_EM_LineIndex(es, l)),
1370 &rcLine);
1371 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1372 EDIT_UpdateText(es, &rcUpdate, TRUE);
1374 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1375 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1376 EDIT_UpdateText(es, &rcUpdate, TRUE);
1381 /*********************************************************************
1383 * EDIT_InvalidateText
1385 * Invalidate the text from offset start up to, but not including,
1386 * offset end. Useful for (re)painting the selection.
1387 * Regions outside the linewidth are not invalidated.
1388 * end == -1 means end == TextLength.
1389 * start and end need not be ordered.
1392 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1394 if (end == start)
1395 return;
1397 if (end == -1)
1398 end = get_text_length(es);
1400 if (end < start) {
1401 INT tmp = start;
1402 start = end;
1403 end = tmp;
1406 if (es->style & ES_MULTILINE)
1407 EDIT_ML_InvalidateText(es, start, end);
1408 else
1409 EDIT_SL_InvalidateText(es, start, end);
1413 /*********************************************************************
1415 * EDIT_EM_SetSel
1417 * note: unlike the specs say: the order of start and end
1418 * _is_ preserved in Windows. (i.e. start can be > end)
1419 * In other words: this handler is OK
1422 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1424 UINT old_start = es->selection_start;
1425 UINT old_end = es->selection_end;
1426 UINT len = get_text_length(es);
1428 if (start == (UINT)-1) {
1429 start = es->selection_end;
1430 end = es->selection_end;
1431 } else {
1432 start = min(start, len);
1433 end = min(end, len);
1435 es->selection_start = start;
1436 es->selection_end = end;
1437 if (after_wrap)
1438 es->flags |= EF_AFTER_WRAP;
1439 else
1440 es->flags &= ~EF_AFTER_WRAP;
1441 /* Compute the necessary invalidation region. */
1442 /* Note that we don't need to invalidate regions which have
1443 * "never" been selected, or those which are "still" selected.
1444 * In fact, every time we hit a selection boundary, we can
1445 * *toggle* whether we need to invalidate. Thus we can optimize by
1446 * *sorting* the interval endpoints. Let's assume that we sort them
1447 * in this order:
1448 * start <= end <= old_start <= old_end
1449 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1450 * in 5 comparisons; i.e. it is impossible to do better than the
1451 * following: */
1452 ORDER_UINT(end, old_end);
1453 ORDER_UINT(start, old_start);
1454 ORDER_UINT(old_start, old_end);
1455 ORDER_UINT(start, end);
1456 /* Note that at this point 'end' and 'old_start' are not in order, but
1457 * start is definitely the min. and old_end is definitely the max. */
1458 if (end != old_start)
1461 * One can also do
1462 * ORDER_UINT32(end, old_start);
1463 * EDIT_InvalidateText(es, start, end);
1464 * EDIT_InvalidateText(es, old_start, old_end);
1465 * in place of the following if statement.
1466 * (That would complete the optimal five-comparison four-element sort.)
1468 if (old_start > end )
1470 EDIT_InvalidateText(es, start, end);
1471 EDIT_InvalidateText(es, old_start, old_end);
1473 else
1475 EDIT_InvalidateText(es, start, old_start);
1476 EDIT_InvalidateText(es, end, old_end);
1479 else EDIT_InvalidateText(es, start, old_end);
1483 /*********************************************************************
1485 * EDIT_UpdateScrollInfo
1488 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1490 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1492 SCROLLINFO si;
1493 si.cbSize = sizeof(SCROLLINFO);
1494 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1495 si.nMin = 0;
1496 si.nMax = es->line_count - 1;
1497 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1498 si.nPos = es->y_offset;
1499 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1500 si.nMin, si.nMax, si.nPage, si.nPos);
1501 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1504 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1506 SCROLLINFO si;
1507 si.cbSize = sizeof(SCROLLINFO);
1508 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1509 si.nMin = 0;
1510 si.nMax = es->text_width - 1;
1511 si.nPage = es->format_rect.right - es->format_rect.left;
1512 si.nPos = es->x_offset;
1513 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1514 si.nMin, si.nMax, si.nPage, si.nPos);
1515 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1520 /*********************************************************************
1522 * EDIT_EM_LineScroll_internal
1524 * Version of EDIT_EM_LineScroll for internal use.
1525 * It doesn't refuse if ES_MULTILINE is set and assumes that
1526 * dx is in pixels, dy - in lines.
1529 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1531 INT nyoff;
1532 INT x_offset_in_pixels;
1533 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1534 es->line_height;
1536 if (es->style & ES_MULTILINE)
1538 x_offset_in_pixels = es->x_offset;
1540 else
1542 dy = 0;
1543 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1546 if (-dx > x_offset_in_pixels)
1547 dx = -x_offset_in_pixels;
1548 if (dx > es->text_width - x_offset_in_pixels)
1549 dx = es->text_width - x_offset_in_pixels;
1550 nyoff = max(0, es->y_offset + dy);
1551 if (nyoff >= es->line_count - lines_per_page)
1552 nyoff = max(0, es->line_count - lines_per_page);
1553 dy = (es->y_offset - nyoff) * es->line_height;
1554 if (dx || dy) {
1555 RECT rc1;
1556 RECT rc;
1558 es->y_offset = nyoff;
1559 if(es->style & ES_MULTILINE)
1560 es->x_offset += dx;
1561 else
1562 es->x_offset += dx / es->char_width;
1564 GetClientRect(es->hwndSelf, &rc1);
1565 IntersectRect(&rc, &rc1, &es->format_rect);
1566 ScrollWindowEx(es->hwndSelf, -dx, dy,
1567 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1568 /* force scroll info update */
1569 EDIT_UpdateScrollInfo(es);
1571 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1572 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1573 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1574 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1575 return TRUE;
1578 /*********************************************************************
1580 * EM_LINESCROLL
1582 * NOTE: dx is in average character widths, dy - in lines;
1585 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1587 if (!(es->style & ES_MULTILINE))
1588 return FALSE;
1590 dx *= es->char_width;
1591 return EDIT_EM_LineScroll_internal(es, dx, dy);
1595 /*********************************************************************
1597 * EM_SCROLL
1600 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1602 INT dy;
1604 if (!(es->style & ES_MULTILINE))
1605 return (LRESULT)FALSE;
1607 dy = 0;
1609 switch (action) {
1610 case SB_LINEUP:
1611 if (es->y_offset)
1612 dy = -1;
1613 break;
1614 case SB_LINEDOWN:
1615 if (es->y_offset < es->line_count - 1)
1616 dy = 1;
1617 break;
1618 case SB_PAGEUP:
1619 if (es->y_offset)
1620 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1621 break;
1622 case SB_PAGEDOWN:
1623 if (es->y_offset < es->line_count - 1)
1624 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1625 break;
1626 default:
1627 return (LRESULT)FALSE;
1629 if (dy) {
1630 INT vlc = get_vertical_line_count(es);
1631 /* check if we are going to move too far */
1632 if(es->y_offset + dy > es->line_count - vlc)
1633 dy = max(es->line_count - vlc, 0) - es->y_offset;
1635 /* Notification is done in EDIT_EM_LineScroll */
1636 if(dy) {
1637 EDIT_EM_LineScroll(es, 0, dy);
1638 return MAKELONG(dy, TRUE);
1642 return (LRESULT)FALSE;
1646 /*********************************************************************
1648 * EDIT_SetCaretPos
1651 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1652 BOOL after_wrap)
1654 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1655 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1656 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1660 /*********************************************************************
1662 * EM_SCROLLCARET
1665 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1667 if (es->style & ES_MULTILINE) {
1668 INT l;
1669 INT vlc;
1670 INT ww;
1671 INT cw = es->char_width;
1672 INT x;
1673 INT dy = 0;
1674 INT dx = 0;
1676 l = EDIT_EM_LineFromChar(es, es->selection_end);
1677 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1678 vlc = get_vertical_line_count(es);
1679 if (l >= es->y_offset + vlc)
1680 dy = l - vlc + 1 - es->y_offset;
1681 if (l < es->y_offset)
1682 dy = l - es->y_offset;
1683 ww = es->format_rect.right - es->format_rect.left;
1684 if (x < es->format_rect.left)
1685 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1686 if (x > es->format_rect.right)
1687 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1688 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1690 /* check if we are going to move too far */
1691 if(es->x_offset + dx + ww > es->text_width)
1692 dx = es->text_width - ww - es->x_offset;
1693 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1694 EDIT_EM_LineScroll_internal(es, dx, dy);
1696 } else {
1697 INT x;
1698 INT goal;
1699 INT format_width;
1701 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1702 format_width = es->format_rect.right - es->format_rect.left;
1703 if (x < es->format_rect.left) {
1704 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1705 do {
1706 es->x_offset--;
1707 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1708 } while ((x < goal) && es->x_offset);
1709 /* FIXME: use ScrollWindow() somehow to improve performance */
1710 EDIT_UpdateText(es, NULL, TRUE);
1711 } else if (x > es->format_rect.right) {
1712 INT x_last;
1713 INT len = get_text_length(es);
1714 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1715 do {
1716 es->x_offset++;
1717 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1718 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1719 } while ((x > goal) && (x_last > es->format_rect.right));
1720 /* FIXME: use ScrollWindow() somehow to improve performance */
1721 EDIT_UpdateText(es, NULL, TRUE);
1725 if(es->flags & EF_FOCUSED)
1726 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1730 /*********************************************************************
1732 * EDIT_MoveBackward
1735 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1737 INT e = es->selection_end;
1739 if (e) {
1740 e--;
1741 if ((es->style & ES_MULTILINE) && e &&
1742 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1743 e--;
1744 if (e && (es->text[e - 1] == '\r'))
1745 e--;
1748 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1749 EDIT_EM_ScrollCaret(es);
1753 /*********************************************************************
1755 * EDIT_MoveDown_ML
1757 * Only for multi line controls
1758 * Move the caret one line down, on a column with the nearest
1759 * x coordinate on the screen (might be a different column).
1762 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1764 INT s = es->selection_start;
1765 INT e = es->selection_end;
1766 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1767 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1768 INT x = (short)LOWORD(pos);
1769 INT y = (short)HIWORD(pos);
1771 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1772 if (!extend)
1773 s = e;
1774 EDIT_EM_SetSel(es, s, e, after_wrap);
1775 EDIT_EM_ScrollCaret(es);
1779 /*********************************************************************
1781 * EDIT_MoveEnd
1784 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1786 BOOL after_wrap = FALSE;
1787 INT e;
1789 /* Pass a high value in x to make sure of receiving the end of the line */
1790 if (!ctrl && (es->style & ES_MULTILINE))
1791 e = EDIT_CharFromPos(es, 0x3fffffff,
1792 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1793 else
1794 e = get_text_length(es);
1795 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1796 EDIT_EM_ScrollCaret(es);
1800 /*********************************************************************
1802 * EDIT_MoveForward
1805 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1807 INT e = es->selection_end;
1809 if (es->text[e]) {
1810 e++;
1811 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1812 if (es->text[e] == '\n')
1813 e++;
1814 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1815 e += 2;
1818 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1819 EDIT_EM_ScrollCaret(es);
1823 /*********************************************************************
1825 * EDIT_MoveHome
1827 * Home key: move to beginning of line.
1830 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1832 INT e;
1834 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1835 if (!ctrl && (es->style & ES_MULTILINE))
1836 e = EDIT_CharFromPos(es, -es->x_offset,
1837 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1838 else
1839 e = 0;
1840 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1841 EDIT_EM_ScrollCaret(es);
1845 /*********************************************************************
1847 * EDIT_MovePageDown_ML
1849 * Only for multi line controls
1850 * Move the caret one page down, on a column with the nearest
1851 * x coordinate on the screen (might be a different column).
1854 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1856 INT s = es->selection_start;
1857 INT e = es->selection_end;
1858 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1859 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1860 INT x = (short)LOWORD(pos);
1861 INT y = (short)HIWORD(pos);
1863 e = EDIT_CharFromPos(es, x,
1864 y + (es->format_rect.bottom - es->format_rect.top),
1865 &after_wrap);
1866 if (!extend)
1867 s = e;
1868 EDIT_EM_SetSel(es, s, e, after_wrap);
1869 EDIT_EM_ScrollCaret(es);
1873 /*********************************************************************
1875 * EDIT_MovePageUp_ML
1877 * Only for multi line controls
1878 * Move the caret one page up, on a column with the nearest
1879 * x coordinate on the screen (might be a different column).
1882 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1884 INT s = es->selection_start;
1885 INT e = es->selection_end;
1886 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1887 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1888 INT x = (short)LOWORD(pos);
1889 INT y = (short)HIWORD(pos);
1891 e = EDIT_CharFromPos(es, x,
1892 y - (es->format_rect.bottom - es->format_rect.top),
1893 &after_wrap);
1894 if (!extend)
1895 s = e;
1896 EDIT_EM_SetSel(es, s, e, after_wrap);
1897 EDIT_EM_ScrollCaret(es);
1901 /*********************************************************************
1903 * EDIT_MoveUp_ML
1905 * Only for multi line controls
1906 * Move the caret one line up, on a column with the nearest
1907 * x coordinate on the screen (might be a different column).
1910 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1912 INT s = es->selection_start;
1913 INT e = es->selection_end;
1914 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1915 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1916 INT x = (short)LOWORD(pos);
1917 INT y = (short)HIWORD(pos);
1919 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1920 if (!extend)
1921 s = e;
1922 EDIT_EM_SetSel(es, s, e, after_wrap);
1923 EDIT_EM_ScrollCaret(es);
1927 /*********************************************************************
1929 * EDIT_MoveWordBackward
1932 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1934 INT s = es->selection_start;
1935 INT e = es->selection_end;
1936 INT l;
1937 INT ll;
1938 INT li;
1940 l = EDIT_EM_LineFromChar(es, e);
1941 ll = EDIT_EM_LineLength(es, e);
1942 li = EDIT_EM_LineIndex(es, l);
1943 if (e - li == 0) {
1944 if (l) {
1945 li = EDIT_EM_LineIndex(es, l - 1);
1946 e = li + EDIT_EM_LineLength(es, li);
1948 } else {
1949 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
1951 if (!extend)
1952 s = e;
1953 EDIT_EM_SetSel(es, s, e, FALSE);
1954 EDIT_EM_ScrollCaret(es);
1958 /*********************************************************************
1960 * EDIT_MoveWordForward
1963 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
1965 INT s = es->selection_start;
1966 INT e = es->selection_end;
1967 INT l;
1968 INT ll;
1969 INT li;
1971 l = EDIT_EM_LineFromChar(es, e);
1972 ll = EDIT_EM_LineLength(es, e);
1973 li = EDIT_EM_LineIndex(es, l);
1974 if (e - li == ll) {
1975 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1976 e = EDIT_EM_LineIndex(es, l + 1);
1977 } else {
1978 e = li + EDIT_CallWordBreakProc(es,
1979 li, e - li + 1, ll, WB_RIGHT);
1981 if (!extend)
1982 s = e;
1983 EDIT_EM_SetSel(es, s, e, FALSE);
1984 EDIT_EM_ScrollCaret(es);
1988 /*********************************************************************
1990 * EDIT_PaintText
1993 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
1995 COLORREF BkColor;
1996 COLORREF TextColor;
1997 LOGFONTW underline_font;
1998 HFONT hUnderline = 0;
1999 HFONT old_font = 0;
2000 INT ret;
2001 INT li;
2002 INT BkMode;
2003 SIZE size;
2005 if (!count)
2006 return 0;
2007 BkMode = GetBkMode(dc);
2008 BkColor = GetBkColor(dc);
2009 TextColor = GetTextColor(dc);
2010 if (rev) {
2011 if (es->composition_len == 0)
2013 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2014 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2015 SetBkMode( dc, OPAQUE);
2017 else
2019 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2020 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2021 underline_font.lfUnderline = TRUE;
2022 hUnderline = CreateFontIndirectW(&underline_font);
2023 old_font = SelectObject(dc,hUnderline);
2026 li = EDIT_EM_LineIndex(es, line);
2027 if (es->style & ES_MULTILINE) {
2028 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2029 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2030 } else {
2031 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2032 TextOutW(dc, x, y, text + li + col, count);
2033 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2034 ret = size.cx;
2035 if (es->style & ES_PASSWORD)
2036 HeapFree(GetProcessHeap(), 0, text);
2038 if (rev) {
2039 if (es->composition_len == 0)
2041 SetBkColor(dc, BkColor);
2042 SetTextColor(dc, TextColor);
2043 SetBkMode( dc, BkMode);
2045 else
2047 if (old_font)
2048 SelectObject(dc,old_font);
2049 if (hUnderline)
2050 DeleteObject(hUnderline);
2053 return ret;
2057 /*********************************************************************
2059 * EDIT_PaintLine
2062 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2064 INT s;
2065 INT e;
2066 INT li;
2067 INT ll;
2068 INT x;
2069 INT y;
2070 LRESULT pos;
2072 if (es->style & ES_MULTILINE) {
2073 INT vlc = get_vertical_line_count(es);
2075 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2076 return;
2077 } else if (line)
2078 return;
2080 TRACE("line=%d\n", line);
2082 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2083 x = (short)LOWORD(pos);
2084 y = (short)HIWORD(pos);
2085 li = EDIT_EM_LineIndex(es, line);
2086 ll = EDIT_EM_LineLength(es, li);
2087 s = min(es->selection_start, es->selection_end);
2088 e = max(es->selection_start, es->selection_end);
2089 s = min(li + ll, max(li, s));
2090 e = min(li + ll, max(li, e));
2091 if (rev && (s != e) &&
2092 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2093 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2094 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2095 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2096 } else
2097 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2101 /*********************************************************************
2103 * EDIT_AdjustFormatRect
2105 * Adjusts the format rectangle for the current font and the
2106 * current client rectangle.
2109 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2111 RECT ClientRect;
2113 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2114 if (es->style & ES_MULTILINE)
2116 INT fw, vlc, max_x_offset, max_y_offset;
2118 vlc = get_vertical_line_count(es);
2119 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2121 /* correct es->x_offset */
2122 fw = es->format_rect.right - es->format_rect.left;
2123 max_x_offset = es->text_width - fw;
2124 if(max_x_offset < 0) max_x_offset = 0;
2125 if(es->x_offset > max_x_offset)
2126 es->x_offset = max_x_offset;
2128 /* correct es->y_offset */
2129 max_y_offset = es->line_count - vlc;
2130 if(max_y_offset < 0) max_y_offset = 0;
2131 if(es->y_offset > max_y_offset)
2132 es->y_offset = max_y_offset;
2134 /* force scroll info update */
2135 EDIT_UpdateScrollInfo(es);
2137 else
2138 /* Windows doesn't care to fix text placement for SL controls */
2139 es->format_rect.bottom = es->format_rect.top + es->line_height;
2141 /* Always stay within the client area */
2142 GetClientRect(es->hwndSelf, &ClientRect);
2143 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2145 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2146 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2148 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2152 /*********************************************************************
2154 * EDIT_SetRectNP
2156 * note: this is not (exactly) the handler called on EM_SETRECTNP
2157 * it is also used to set the rect of a single line control
2160 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2162 LONG_PTR ExStyle;
2163 INT bw, bh;
2164 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2166 CopyRect(&es->format_rect, rc);
2168 if (ExStyle & WS_EX_CLIENTEDGE) {
2169 es->format_rect.left++;
2170 es->format_rect.right--;
2172 if (es->format_rect.bottom - es->format_rect.top
2173 >= es->line_height + 2)
2175 es->format_rect.top++;
2176 es->format_rect.bottom--;
2179 else if (es->style & WS_BORDER) {
2180 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2181 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2182 es->format_rect.left += bw;
2183 es->format_rect.right -= bw;
2184 if (es->format_rect.bottom - es->format_rect.top
2185 >= es->line_height + 2 * bh)
2187 es->format_rect.top += bh;
2188 es->format_rect.bottom -= bh;
2192 es->format_rect.left += es->left_margin;
2193 es->format_rect.right -= es->right_margin;
2194 EDIT_AdjustFormatRect(es);
2198 /*********************************************************************
2200 * EM_CHARFROMPOS
2202 * returns line number (not index) in high-order word of result.
2203 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2204 * to Richedit, not to the edit control. Original documentation is valid.
2205 * FIXME: do the specs mean to return -1 if outside client area or
2206 * if outside formatting rectangle ???
2209 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2211 POINT pt;
2212 RECT rc;
2213 INT index;
2215 pt.x = x;
2216 pt.y = y;
2217 GetClientRect(es->hwndSelf, &rc);
2218 if (!PtInRect(&rc, pt))
2219 return -1;
2221 index = EDIT_CharFromPos(es, x, y, NULL);
2222 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2226 /*********************************************************************
2228 * EM_FMTLINES
2230 * Enable or disable soft breaks.
2232 * This means: insert or remove the soft linebreak character (\r\r\n).
2233 * Take care to check if the text still fits the buffer after insertion.
2234 * If not, notify with EN_ERRSPACE.
2237 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2239 es->flags &= ~EF_USE_SOFTBRK;
2240 if (add_eol) {
2241 es->flags |= EF_USE_SOFTBRK;
2242 FIXME("soft break enabled, not implemented\n");
2244 return add_eol;
2248 /*********************************************************************
2250 * EM_GETHANDLE
2252 * Hopefully this won't fire back at us.
2253 * We always start with a fixed buffer in the local heap.
2254 * Despite of the documentation says that the local heap is used
2255 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2256 * buffer on the local heap.
2259 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2261 HLOCAL hLocal;
2263 if (!(es->style & ES_MULTILINE))
2264 return 0;
2266 if(es->is_unicode)
2267 hLocal = es->hloc32W;
2268 else
2270 if(!es->hloc32A)
2272 CHAR *textA;
2273 UINT countA, alloc_size;
2274 TRACE("Allocating 32-bit ANSI alias buffer\n");
2275 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2276 alloc_size = ROUND_TO_GROW(countA);
2277 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2279 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2280 return 0;
2282 textA = LocalLock(es->hloc32A);
2283 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2284 LocalUnlock(es->hloc32A);
2286 hLocal = es->hloc32A;
2289 es->flags |= EF_APP_HAS_HANDLE;
2290 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2291 return hLocal;
2295 /*********************************************************************
2297 * EM_GETLINE
2300 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2302 LPWSTR src;
2303 INT line_len, dst_len;
2304 INT i;
2306 if (es->style & ES_MULTILINE) {
2307 if (line >= es->line_count)
2308 return 0;
2309 } else
2310 line = 0;
2311 i = EDIT_EM_LineIndex(es, line);
2312 src = es->text + i;
2313 line_len = EDIT_EM_LineLength(es, i);
2314 dst_len = *(WORD *)dst;
2315 if(unicode)
2317 if(dst_len <= line_len)
2319 memcpy(dst, src, dst_len * sizeof(WCHAR));
2320 return dst_len;
2322 else /* Append 0 if enough space */
2324 memcpy(dst, src, line_len * sizeof(WCHAR));
2325 dst[line_len] = 0;
2326 return line_len;
2329 else
2331 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2332 if(!ret && line_len) /* Insufficient buffer size */
2333 return dst_len;
2334 if(ret < dst_len) /* Append 0 if enough space */
2335 ((LPSTR)dst)[ret] = 0;
2336 return ret;
2341 /*********************************************************************
2343 * EM_GETSEL
2346 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2348 UINT s = es->selection_start;
2349 UINT e = es->selection_end;
2351 ORDER_UINT(s, e);
2352 if (start)
2353 *start = s;
2354 if (end)
2355 *end = e;
2356 return MAKELONG(s, e);
2360 /*********************************************************************
2362 * EM_REPLACESEL
2364 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2367 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2369 UINT strl = strlenW(lpsz_replace);
2370 UINT tl = get_text_length(es);
2371 UINT utl;
2372 UINT s;
2373 UINT e;
2374 UINT i;
2375 UINT size;
2376 LPWSTR p;
2377 HRGN hrgn = 0;
2378 LPWSTR buf = NULL;
2379 UINT bufl = 0;
2381 TRACE("%s, can_undo %d, send_update %d\n",
2382 debugstr_w(lpsz_replace), can_undo, send_update);
2384 s = es->selection_start;
2385 e = es->selection_end;
2387 if ((s == e) && !strl)
2388 return;
2390 ORDER_UINT(s, e);
2392 size = tl - (e - s) + strl;
2393 if (!size)
2394 es->text_width = 0;
2396 /* Issue the EN_MAXTEXT notification and continue with replacing text
2397 * such that buffer limit is honored. */
2398 if ((honor_limit) && (size > es->buffer_limit)) {
2399 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2400 /* Buffer limit can be smaller than the actual length of text in combobox */
2401 if (es->buffer_limit < (tl - (e-s)))
2402 strl = 0;
2403 else
2404 strl = es->buffer_limit - (tl - (e-s));
2407 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2408 return;
2410 if (e != s) {
2411 /* there is something to be deleted */
2412 TRACE("deleting stuff.\n");
2413 bufl = e - s;
2414 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2415 if (!buf) return;
2416 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2417 buf[bufl] = 0; /* ensure 0 termination */
2418 /* now delete */
2419 strcpyW(es->text + s, es->text + e);
2420 text_buffer_changed(es);
2422 if (strl) {
2423 /* there is an insertion */
2424 tl = get_text_length(es);
2425 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));
2426 for (p = es->text + tl ; p >= es->text + s ; p--)
2427 p[strl] = p[0];
2428 for (i = 0 , p = es->text + s ; i < strl ; i++)
2429 p[i] = lpsz_replace[i];
2430 if(es->style & ES_UPPERCASE)
2431 CharUpperBuffW(p, strl);
2432 else if(es->style & ES_LOWERCASE)
2433 CharLowerBuffW(p, strl);
2434 text_buffer_changed(es);
2436 if (es->style & ES_MULTILINE)
2438 INT st = min(es->selection_start, es->selection_end);
2439 INT vlc = get_vertical_line_count(es);
2441 hrgn = CreateRectRgn(0, 0, 0, 0);
2442 EDIT_BuildLineDefs_ML(es, st, st + strl,
2443 strl - abs(es->selection_end - es->selection_start), hrgn);
2444 /* if text is too long undo all changes */
2445 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2446 if (strl)
2447 strcpyW(es->text + e, es->text + e + strl);
2448 if (e != s)
2449 for (i = 0 , p = es->text ; i < e - s ; i++)
2450 p[i + s] = buf[i];
2451 text_buffer_changed(es);
2452 EDIT_BuildLineDefs_ML(es, s, e,
2453 abs(es->selection_end - es->selection_start) - strl, hrgn);
2454 strl = 0;
2455 e = s;
2456 hrgn = CreateRectRgn(0, 0, 0, 0);
2457 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2460 else {
2461 INT fw = es->format_rect.right - es->format_rect.left;
2462 EDIT_CalcLineWidth_SL(es);
2463 /* remove chars that don't fit */
2464 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2465 while ((es->text_width > fw) && s + strl >= s) {
2466 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2467 strl--;
2468 EDIT_CalcLineWidth_SL(es);
2470 text_buffer_changed(es);
2471 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2475 if (e != s) {
2476 if (can_undo) {
2477 utl = strlenW(es->undo_text);
2478 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2479 /* undo-buffer is extended to the right */
2480 EDIT_MakeUndoFit(es, utl + e - s);
2481 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2482 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2483 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2484 /* undo-buffer is extended to the left */
2485 EDIT_MakeUndoFit(es, utl + e - s);
2486 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2487 p[e - s] = p[0];
2488 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2489 p[i] = buf[i];
2490 es->undo_position = s;
2491 } else {
2492 /* new undo-buffer */
2493 EDIT_MakeUndoFit(es, e - s);
2494 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2495 es->undo_text[e - s] = 0; /* ensure 0 termination */
2496 es->undo_position = s;
2498 /* any deletion makes the old insertion-undo invalid */
2499 es->undo_insert_count = 0;
2500 } else
2501 EDIT_EM_EmptyUndoBuffer(es);
2503 if (strl) {
2504 if (can_undo) {
2505 if ((s == es->undo_position) ||
2506 ((es->undo_insert_count) &&
2507 (s == es->undo_position + es->undo_insert_count)))
2509 * insertion is new and at delete position or
2510 * an extension to either left or right
2512 es->undo_insert_count += strl;
2513 else {
2514 /* new insertion undo */
2515 es->undo_position = s;
2516 es->undo_insert_count = strl;
2517 /* new insertion makes old delete-buffer invalid */
2518 *es->undo_text = '\0';
2520 } else
2521 EDIT_EM_EmptyUndoBuffer(es);
2524 if (bufl)
2525 HeapFree(GetProcessHeap(), 0, buf);
2527 s += strl;
2529 /* If text has been deleted and we're right or center aligned then scroll rightward */
2530 if (es->style & (ES_RIGHT | ES_CENTER))
2532 INT delta = strl - abs(es->selection_end - es->selection_start);
2534 if (delta < 0 && es->x_offset)
2536 if (abs(delta) > es->x_offset)
2537 es->x_offset = 0;
2538 else
2539 es->x_offset += delta;
2543 EDIT_EM_SetSel(es, s, s, FALSE);
2544 es->flags |= EF_MODIFIED;
2545 if (send_update) es->flags |= EF_UPDATE;
2546 if (hrgn)
2548 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2549 DeleteObject(hrgn);
2551 else
2552 EDIT_UpdateText(es, NULL, TRUE);
2554 EDIT_EM_ScrollCaret(es);
2556 /* force scroll info update */
2557 EDIT_UpdateScrollInfo(es);
2560 if(send_update || (es->flags & EF_UPDATE))
2562 es->flags &= ~EF_UPDATE;
2563 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2568 /*********************************************************************
2570 * EM_SETHANDLE
2572 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2575 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2577 if (!(es->style & ES_MULTILINE))
2578 return;
2580 if (!hloc) {
2581 WARN("called with NULL handle\n");
2582 return;
2585 EDIT_UnlockBuffer(es, TRUE);
2587 if(es->is_unicode)
2589 if(es->hloc32A)
2591 LocalFree(es->hloc32A);
2592 es->hloc32A = NULL;
2594 es->hloc32W = hloc;
2596 else
2598 INT countW, countA;
2599 HLOCAL hloc32W_new;
2600 WCHAR *textW;
2601 CHAR *textA;
2603 countA = LocalSize(hloc);
2604 textA = LocalLock(hloc);
2605 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2606 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2608 ERR("Could not allocate new unicode buffer\n");
2609 return;
2611 textW = LocalLock(hloc32W_new);
2612 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2613 LocalUnlock(hloc32W_new);
2614 LocalUnlock(hloc);
2616 if(es->hloc32W)
2617 LocalFree(es->hloc32W);
2619 es->hloc32W = hloc32W_new;
2620 es->hloc32A = hloc;
2623 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2625 es->flags |= EF_APP_HAS_HANDLE;
2626 EDIT_LockBuffer(es);
2628 es->x_offset = es->y_offset = 0;
2629 es->selection_start = es->selection_end = 0;
2630 EDIT_EM_EmptyUndoBuffer(es);
2631 es->flags &= ~EF_MODIFIED;
2632 es->flags &= ~EF_UPDATE;
2633 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2634 EDIT_UpdateText(es, NULL, TRUE);
2635 EDIT_EM_ScrollCaret(es);
2636 /* force scroll info update */
2637 EDIT_UpdateScrollInfo(es);
2641 /*********************************************************************
2643 * EM_SETLIMITTEXT
2645 * NOTE: this version currently implements WinNT limits
2648 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2650 if (!limit) limit = ~0u;
2651 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2652 es->buffer_limit = limit;
2656 /*********************************************************************
2658 * EM_SETMARGINS
2660 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2661 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2662 * margin according to the textmetrics of the current font.
2664 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2665 * of the char's width as the margin, but this is not how Windows handles this.
2666 * For all other fonts Windows sets the margins to zero.
2668 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2669 * edit control is equal to or larger than a certain size.
2670 * Interestingly if one subtracts both the left and right margins from
2671 * this size one always seems to get an even number. The extents of
2672 * the (four character) string "'**'" match this quite closely, so
2673 * we'll use this until we come up with a better idea.
2675 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2677 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2678 SIZE sz;
2680 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2681 return sz.cx + left + right;
2684 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2685 WORD left, WORD right, BOOL repaint)
2687 TEXTMETRICW tm;
2688 INT default_left_margin = 0; /* in pixels */
2689 INT default_right_margin = 0; /* in pixels */
2691 /* Set the default margins depending on the font */
2692 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2693 HDC dc = GetDC(es->hwndSelf);
2694 HFONT old_font = SelectObject(dc, es->font);
2695 GetTextMetricsW(dc, &tm);
2696 /* The default margins are only non zero for TrueType or Vector fonts */
2697 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2698 int min_size;
2699 RECT rc;
2700 /* This must be calculated more exactly! But how? */
2701 default_left_margin = tm.tmAveCharWidth / 2;
2702 default_right_margin = tm.tmAveCharWidth / 2;
2703 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2704 GetClientRect(es->hwndSelf, &rc);
2705 if(rc.right - rc.left < min_size) {
2706 default_left_margin = es->left_margin;
2707 default_right_margin = es->right_margin;
2710 SelectObject(dc, old_font);
2711 ReleaseDC(es->hwndSelf, dc);
2714 if (action & EC_LEFTMARGIN) {
2715 es->format_rect.left -= es->left_margin;
2716 if (left != EC_USEFONTINFO)
2717 es->left_margin = left;
2718 else
2719 es->left_margin = default_left_margin;
2720 es->format_rect.left += es->left_margin;
2723 if (action & EC_RIGHTMARGIN) {
2724 es->format_rect.right += es->right_margin;
2725 if (right != EC_USEFONTINFO)
2726 es->right_margin = right;
2727 else
2728 es->right_margin = default_right_margin;
2729 es->format_rect.right -= es->right_margin;
2732 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2733 EDIT_AdjustFormatRect(es);
2734 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2737 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2741 /*********************************************************************
2743 * EM_SETPASSWORDCHAR
2746 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2748 LONG style;
2750 if (es->style & ES_MULTILINE)
2751 return;
2753 if (es->password_char == c)
2754 return;
2756 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2757 es->password_char = c;
2758 if (c) {
2759 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2760 es->style |= ES_PASSWORD;
2761 } else {
2762 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2763 es->style &= ~ES_PASSWORD;
2765 EDIT_UpdateText(es, NULL, TRUE);
2769 /*********************************************************************
2771 * EM_SETTABSTOPS
2774 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2776 if (!(es->style & ES_MULTILINE))
2777 return FALSE;
2778 HeapFree(GetProcessHeap(), 0, es->tabs);
2779 es->tabs_count = count;
2780 if (!count)
2781 es->tabs = NULL;
2782 else {
2783 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2784 memcpy(es->tabs, tabs, count * sizeof(INT));
2786 return TRUE;
2790 /*********************************************************************
2792 * EM_SETWORDBREAKPROC
2795 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
2797 if (es->word_break_proc == wbp)
2798 return;
2800 es->word_break_proc = wbp;
2802 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2803 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2804 EDIT_UpdateText(es, NULL, TRUE);
2809 /*********************************************************************
2811 * EM_UNDO / WM_UNDO
2814 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2816 INT ulength;
2817 LPWSTR utext;
2819 /* As per MSDN spec, for a single-line edit control,
2820 the return value is always TRUE */
2821 if( es->style & ES_READONLY )
2822 return !(es->style & ES_MULTILINE);
2824 ulength = strlenW(es->undo_text);
2826 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
2828 strcpyW(utext, es->undo_text);
2830 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2831 es->undo_insert_count, debugstr_w(utext));
2833 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2834 EDIT_EM_EmptyUndoBuffer(es);
2835 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
2836 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2837 /* send the notification after the selection start and end are set */
2838 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2839 EDIT_EM_ScrollCaret(es);
2840 HeapFree(GetProcessHeap(), 0, utext);
2842 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2843 es->undo_insert_count, debugstr_w(es->undo_text));
2844 return TRUE;
2848 /* Helper function for WM_CHAR
2850 * According to an MSDN blog article titled "Just because you're a control
2851 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2852 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2853 * a dialog box or not.
2855 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2857 return (es->flags & EF_DIALOGMODE);
2861 /*********************************************************************
2863 * WM_PASTE
2866 static void EDIT_WM_Paste(EDITSTATE *es)
2868 HGLOBAL hsrc;
2869 LPWSTR src;
2871 /* Protect read-only edit control from modification */
2872 if(es->style & ES_READONLY)
2873 return;
2875 OpenClipboard(es->hwndSelf);
2876 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2877 src = GlobalLock(hsrc);
2878 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
2879 GlobalUnlock(hsrc);
2881 else if (es->style & ES_PASSWORD) {
2882 /* clear selected text in password edit box even with empty clipboard */
2883 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2885 CloseClipboard();
2889 /*********************************************************************
2891 * WM_COPY
2894 static void EDIT_WM_Copy(EDITSTATE *es)
2896 INT s = min(es->selection_start, es->selection_end);
2897 INT e = max(es->selection_start, es->selection_end);
2898 HGLOBAL hdst;
2899 LPWSTR dst;
2900 DWORD len;
2902 if (e == s) return;
2904 len = e - s;
2905 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2906 dst = GlobalLock(hdst);
2907 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2908 dst[len] = 0; /* ensure 0 termination */
2909 TRACE("%s\n", debugstr_w(dst));
2910 GlobalUnlock(hdst);
2911 OpenClipboard(es->hwndSelf);
2912 EmptyClipboard();
2913 SetClipboardData(CF_UNICODETEXT, hdst);
2914 CloseClipboard();
2918 /*********************************************************************
2920 * WM_CLEAR
2923 static inline void EDIT_WM_Clear(EDITSTATE *es)
2925 /* Protect read-only edit control from modification */
2926 if(es->style & ES_READONLY)
2927 return;
2929 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2933 /*********************************************************************
2935 * WM_CUT
2938 static inline void EDIT_WM_Cut(EDITSTATE *es)
2940 EDIT_WM_Copy(es);
2941 EDIT_WM_Clear(es);
2945 /*********************************************************************
2947 * WM_CHAR
2950 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
2952 BOOL control;
2954 control = GetKeyState(VK_CONTROL) & 0x8000;
2956 switch (c) {
2957 case '\r':
2958 /* If it's not a multiline edit box, it would be ignored below.
2959 * For multiline edit without ES_WANTRETURN, we have to make a
2960 * special case.
2962 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
2963 if (EDIT_IsInsideDialog(es))
2964 break;
2965 case '\n':
2966 if (es->style & ES_MULTILINE) {
2967 if (es->style & ES_READONLY) {
2968 EDIT_MoveHome(es, FALSE, FALSE);
2969 EDIT_MoveDown_ML(es, FALSE);
2970 } else {
2971 static const WCHAR cr_lfW[] = {'\r','\n',0};
2972 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
2975 break;
2976 case '\t':
2977 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2979 static const WCHAR tabW[] = {'\t',0};
2980 if (EDIT_IsInsideDialog(es))
2981 break;
2982 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
2984 break;
2985 case VK_BACK:
2986 if (!(es->style & ES_READONLY) && !control) {
2987 if (es->selection_start != es->selection_end)
2988 EDIT_WM_Clear(es);
2989 else {
2990 /* delete character left of caret */
2991 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
2992 EDIT_MoveBackward(es, TRUE);
2993 EDIT_WM_Clear(es);
2996 break;
2997 case 0x03: /* ^C */
2998 if (!(es->style & ES_PASSWORD))
2999 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3000 break;
3001 case 0x16: /* ^V */
3002 if (!(es->style & ES_READONLY))
3003 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3004 break;
3005 case 0x18: /* ^X */
3006 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3007 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3008 break;
3009 case 0x1A: /* ^Z */
3010 if (!(es->style & ES_READONLY))
3011 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3012 break;
3014 default:
3015 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3016 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3017 break;
3019 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3020 WCHAR str[2];
3021 str[0] = c;
3022 str[1] = '\0';
3023 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3025 break;
3027 return 1;
3031 /*********************************************************************
3033 * WM_COMMAND
3036 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3038 if (code || control)
3039 return;
3041 switch (id) {
3042 case EM_UNDO:
3043 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3044 break;
3045 case WM_CUT:
3046 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3047 break;
3048 case WM_COPY:
3049 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3050 break;
3051 case WM_PASTE:
3052 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3053 break;
3054 case WM_CLEAR:
3055 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3056 break;
3057 case EM_SETSEL:
3058 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3059 EDIT_EM_ScrollCaret(es);
3060 break;
3061 default:
3062 ERR("unknown menu item, please report\n");
3063 break;
3068 /*********************************************************************
3070 * WM_CONTEXTMENU
3072 * Note: the resource files resource/sysres_??.rc cannot define a
3073 * single popup menu. Hence we use a (dummy) menubar
3074 * containing the single popup menu as its first item.
3076 * FIXME: the message identifiers have been chosen arbitrarily,
3077 * hence we use MF_BYPOSITION.
3078 * We might as well use the "real" values (anybody knows ?)
3079 * The menu definition is in resources/sysres_??.rc.
3080 * Once these are OK, we better use MF_BYCOMMAND here
3081 * (as we do in EDIT_WM_Command()).
3084 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3086 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3087 HMENU popup = GetSubMenu(menu, 0);
3088 UINT start = es->selection_start;
3089 UINT end = es->selection_end;
3091 ORDER_UINT(start, end);
3093 /* undo */
3094 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3095 /* cut */
3096 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3097 /* copy */
3098 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3099 /* paste */
3100 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3101 /* delete */
3102 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3103 /* select all */
3104 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3106 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3108 RECT rc;
3109 /* Windows places the menu at the edit's center in this case */
3110 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3111 x = rc.left + (rc.right - rc.left) / 2;
3112 y = rc.top + (rc.bottom - rc.top) / 2;
3115 if (!(es->flags & EF_FOCUSED))
3116 SetFocus(es->hwndSelf);
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->logAttr);
4320 HeapFree(GetProcessHeap(), 0, es);
4321 return FALSE;
4325 /*********************************************************************
4327 * WM_CREATE
4330 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4332 RECT clientRect;
4334 TRACE("%s\n", debugstr_w(name));
4336 * To initialize some final structure members, we call some helper
4337 * functions. However, since the EDITSTATE is not consistent (i.e.
4338 * not fully initialized), we should be very careful which
4339 * functions can be called, and in what order.
4341 EDIT_WM_SetFont(es, 0, FALSE);
4342 EDIT_EM_EmptyUndoBuffer(es);
4344 /* We need to calculate the format rect
4345 (applications may send EM_SETMARGINS before the control gets visible) */
4346 GetClientRect(es->hwndSelf, &clientRect);
4347 EDIT_SetRectNP(es, &clientRect);
4349 if (name && *name) {
4350 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4351 /* if we insert text to the editline, the text scrolls out
4352 * of the window, as the caret is placed after the insert
4353 * pos normally; thus we reset es->selection... to 0 and
4354 * update caret
4356 es->selection_start = es->selection_end = 0;
4357 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4358 * Messages are only to be sent when the USER does something to
4359 * change the contents. So I am removing this EN_CHANGE
4361 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4363 EDIT_EM_ScrollCaret(es);
4365 /* force scroll info update */
4366 EDIT_UpdateScrollInfo(es);
4367 /* The rule seems to return 1 here for success */
4368 /* Power Builder masked edit controls will crash */
4369 /* if not. */
4370 /* FIXME: is that in all cases so ? */
4371 return 1;
4375 /*********************************************************************
4377 * WM_NCDESTROY
4380 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4382 LINEDEF *pc, *pp;
4384 if (es->hloc32W) {
4385 LocalFree(es->hloc32W);
4387 if (es->hloc32A) {
4388 LocalFree(es->hloc32A);
4390 pc = es->first_line_def;
4391 while (pc)
4393 pp = pc->next;
4394 HeapFree(GetProcessHeap(), 0, pc);
4395 pc = pp;
4398 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4399 HeapFree(GetProcessHeap(), 0, es->undo_text);
4400 HeapFree(GetProcessHeap(), 0, es);
4402 return 0;
4406 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4408 if(unicode)
4409 return DefWindowProcW(hwnd, msg, wParam, lParam);
4410 else
4411 return DefWindowProcA(hwnd, msg, wParam, lParam);
4414 /*********************************************************************
4416 * EditWndProc_common
4418 * The messages are in the order of the actual integer values
4419 * (which can be found in include/windows.h)
4421 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4423 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4424 LRESULT result = 0;
4426 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4428 if (!es && msg != WM_NCCREATE)
4429 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4431 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4433 switch (msg) {
4434 case EM_GETSEL:
4435 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4436 break;
4438 case EM_SETSEL:
4439 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4440 EDIT_EM_ScrollCaret(es);
4441 result = 1;
4442 break;
4444 case EM_GETRECT:
4445 if (lParam)
4446 CopyRect((LPRECT)lParam, &es->format_rect);
4447 break;
4449 case EM_SETRECT:
4450 if ((es->style & ES_MULTILINE) && lParam) {
4451 EDIT_SetRectNP(es, (LPRECT)lParam);
4452 EDIT_UpdateText(es, NULL, TRUE);
4454 break;
4456 case EM_SETRECTNP:
4457 if ((es->style & ES_MULTILINE) && lParam)
4458 EDIT_SetRectNP(es, (LPRECT)lParam);
4459 break;
4461 case EM_SCROLL:
4462 result = EDIT_EM_Scroll(es, (INT)wParam);
4463 break;
4465 case EM_LINESCROLL:
4466 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4467 break;
4469 case EM_SCROLLCARET:
4470 EDIT_EM_ScrollCaret(es);
4471 result = 1;
4472 break;
4474 case EM_GETMODIFY:
4475 result = ((es->flags & EF_MODIFIED) != 0);
4476 break;
4478 case EM_SETMODIFY:
4479 if (wParam)
4480 es->flags |= EF_MODIFIED;
4481 else
4482 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4483 break;
4485 case EM_GETLINECOUNT:
4486 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4487 break;
4489 case EM_LINEINDEX:
4490 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4491 break;
4493 case EM_SETHANDLE:
4494 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4495 break;
4497 case EM_GETHANDLE:
4498 result = (LRESULT)EDIT_EM_GetHandle(es);
4499 break;
4501 case EM_GETTHUMB:
4502 result = EDIT_EM_GetThumb(es);
4503 break;
4505 /* these messages missing from specs */
4506 case 0x00bf:
4507 case 0x00c0:
4508 case 0x00c3:
4509 case 0x00ca:
4510 FIXME("undocumented message 0x%x, please report\n", msg);
4511 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4512 break;
4514 case EM_LINELENGTH:
4515 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4516 break;
4518 case EM_REPLACESEL:
4520 LPWSTR textW;
4522 if(unicode)
4523 textW = (LPWSTR)lParam;
4524 else
4526 LPSTR textA = (LPSTR)lParam;
4527 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4528 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4529 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4532 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
4533 result = 1;
4535 if(!unicode)
4536 HeapFree(GetProcessHeap(), 0, textW);
4537 break;
4540 case EM_GETLINE:
4541 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4542 break;
4544 case EM_SETLIMITTEXT:
4545 EDIT_EM_SetLimitText(es, wParam);
4546 break;
4548 case EM_CANUNDO:
4549 result = (LRESULT)EDIT_EM_CanUndo(es);
4550 break;
4552 case EM_UNDO:
4553 case WM_UNDO:
4554 result = (LRESULT)EDIT_EM_Undo(es);
4555 break;
4557 case EM_FMTLINES:
4558 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4559 break;
4561 case EM_LINEFROMCHAR:
4562 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4563 break;
4565 case EM_SETTABSTOPS:
4566 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4567 break;
4569 case EM_SETPASSWORDCHAR:
4571 WCHAR charW = 0;
4573 if(unicode)
4574 charW = (WCHAR)wParam;
4575 else
4577 CHAR charA = wParam;
4578 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4581 EDIT_EM_SetPasswordChar(es, charW);
4582 break;
4585 case EM_EMPTYUNDOBUFFER:
4586 EDIT_EM_EmptyUndoBuffer(es);
4587 break;
4589 case EM_GETFIRSTVISIBLELINE:
4590 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4591 break;
4593 case EM_SETREADONLY:
4595 DWORD old_style = es->style;
4597 if (wParam) {
4598 SetWindowLongW( hwnd, GWL_STYLE,
4599 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4600 es->style |= ES_READONLY;
4601 } else {
4602 SetWindowLongW( hwnd, GWL_STYLE,
4603 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4604 es->style &= ~ES_READONLY;
4607 if (old_style ^ es->style)
4608 InvalidateRect(es->hwndSelf, NULL, TRUE);
4610 result = 1;
4611 break;
4614 case EM_SETWORDBREAKPROC:
4615 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4616 break;
4618 case EM_GETWORDBREAKPROC:
4619 result = (LRESULT)es->word_break_proc;
4620 break;
4622 case EM_GETPASSWORDCHAR:
4624 if(unicode)
4625 result = es->password_char;
4626 else
4628 WCHAR charW = es->password_char;
4629 CHAR charA = 0;
4630 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4631 result = charA;
4633 break;
4636 case EM_SETMARGINS:
4637 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4638 break;
4640 case EM_GETMARGINS:
4641 result = MAKELONG(es->left_margin, es->right_margin);
4642 break;
4644 case EM_GETLIMITTEXT:
4645 result = es->buffer_limit;
4646 break;
4648 case EM_POSFROMCHAR:
4649 if ((INT)wParam >= get_text_length(es)) result = -1;
4650 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4651 break;
4653 case EM_CHARFROMPOS:
4654 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4655 break;
4657 /* End of the EM_ messages which were in numerical order; what order
4658 * are these in? vaguely alphabetical?
4661 case WM_NCCREATE:
4662 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4663 break;
4665 case WM_NCDESTROY:
4666 result = EDIT_WM_NCDestroy(es);
4667 es = NULL;
4668 break;
4670 case WM_GETDLGCODE:
4671 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4673 if (es->style & ES_MULTILINE)
4674 result |= DLGC_WANTALLKEYS;
4676 if (lParam)
4678 es->flags|=EF_DIALOGMODE;
4680 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4682 int vk = (int)((LPMSG)lParam)->wParam;
4684 if (es->hwndListBox)
4686 if (vk == VK_RETURN || vk == VK_ESCAPE)
4687 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4688 result |= DLGC_WANTMESSAGE;
4692 break;
4694 case WM_IME_CHAR:
4695 if (!unicode)
4697 WCHAR charW;
4698 CHAR strng[2];
4700 strng[0] = wParam >> 8;
4701 strng[1] = wParam & 0xff;
4702 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4703 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4704 result = EDIT_WM_Char(es, charW);
4705 break;
4707 /* fall through */
4708 case WM_CHAR:
4710 WCHAR charW;
4712 if(unicode)
4713 charW = wParam;
4714 else
4716 CHAR charA = wParam;
4717 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4720 if (es->hwndListBox)
4722 if (charW == VK_RETURN || charW == VK_ESCAPE)
4724 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4725 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4726 break;
4729 result = EDIT_WM_Char(es, charW);
4730 break;
4733 case WM_UNICHAR:
4734 if (unicode)
4736 if (wParam == UNICODE_NOCHAR) return TRUE;
4737 if (wParam <= 0x000fffff)
4739 if(wParam > 0xffff) /* convert to surrogates */
4741 wParam -= 0x10000;
4742 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4743 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4745 else EDIT_WM_Char(es, wParam);
4747 return 0;
4749 break;
4751 case WM_CLEAR:
4752 EDIT_WM_Clear(es);
4753 break;
4755 case WM_COMMAND:
4756 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
4757 break;
4759 case WM_CONTEXTMENU:
4760 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4761 break;
4763 case WM_COPY:
4764 EDIT_WM_Copy(es);
4765 break;
4767 case WM_CREATE:
4768 if(unicode)
4769 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4770 else
4772 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4773 LPWSTR nameW = NULL;
4774 if(nameA)
4776 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
4777 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4778 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
4780 result = EDIT_WM_Create(es, nameW);
4781 HeapFree(GetProcessHeap(), 0, nameW);
4783 break;
4785 case WM_CUT:
4786 EDIT_WM_Cut(es);
4787 break;
4789 case WM_ENABLE:
4790 es->bEnableState = (BOOL) wParam;
4791 EDIT_UpdateText(es, NULL, TRUE);
4792 break;
4794 case WM_ERASEBKGND:
4795 /* we do the proper erase in EDIT_WM_Paint */
4796 result = 1;
4797 break;
4799 case WM_GETFONT:
4800 result = (LRESULT)es->font;
4801 break;
4803 case WM_GETTEXT:
4804 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
4805 break;
4807 case WM_GETTEXTLENGTH:
4808 if (unicode) result = get_text_length(es);
4809 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
4810 NULL, 0, NULL, NULL );
4811 break;
4813 case WM_HSCROLL:
4814 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4815 break;
4817 case WM_KEYDOWN:
4818 result = EDIT_WM_KeyDown(es, (INT)wParam);
4819 break;
4821 case WM_KILLFOCUS:
4822 result = EDIT_WM_KillFocus(es);
4823 break;
4825 case WM_LBUTTONDBLCLK:
4826 result = EDIT_WM_LButtonDblClk(es);
4827 break;
4829 case WM_LBUTTONDOWN:
4830 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
4831 break;
4833 case WM_LBUTTONUP:
4834 result = EDIT_WM_LButtonUp(es);
4835 break;
4837 case WM_MBUTTONDOWN:
4838 result = EDIT_WM_MButtonDown(es);
4839 break;
4841 case WM_MOUSEMOVE:
4842 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4843 break;
4845 case WM_PRINTCLIENT:
4846 case WM_PAINT:
4847 EDIT_WM_Paint(es, (HDC)wParam);
4848 break;
4850 case WM_PASTE:
4851 EDIT_WM_Paste(es);
4852 break;
4854 case WM_SETFOCUS:
4855 EDIT_WM_SetFocus(es);
4856 break;
4858 case WM_SETFONT:
4859 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
4860 break;
4862 case WM_SETREDRAW:
4863 /* FIXME: actually set an internal flag and behave accordingly */
4864 break;
4866 case WM_SETTEXT:
4867 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
4868 result = TRUE;
4869 break;
4871 case WM_SIZE:
4872 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
4873 break;
4875 case WM_STYLECHANGED:
4876 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
4877 break;
4879 case WM_STYLECHANGING:
4880 result = 0; /* See EDIT_WM_StyleChanged */
4881 break;
4883 case WM_SYSKEYDOWN:
4884 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
4885 break;
4887 case WM_TIMER:
4888 EDIT_WM_Timer(es);
4889 break;
4891 case WM_VSCROLL:
4892 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4893 break;
4895 case WM_MOUSEWHEEL:
4897 int gcWheelDelta = 0;
4898 UINT pulScrollLines = 3;
4899 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4901 if (wParam & (MK_SHIFT | MK_CONTROL)) {
4902 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4903 break;
4905 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
4906 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4908 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
4909 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
4910 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
4913 break;
4916 /* IME messages to make the edit control IME aware */
4917 case WM_IME_SETCONTEXT:
4918 break;
4920 case WM_IME_STARTCOMPOSITION:
4921 es->composition_start = es->selection_end;
4922 es->composition_len = 0;
4923 break;
4925 case WM_IME_COMPOSITION:
4926 EDIT_ImeComposition(hwnd, lParam, es);
4927 break;
4929 case WM_IME_ENDCOMPOSITION:
4930 if (es->composition_len > 0)
4932 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4933 es->selection_end = es->selection_start;
4934 es->composition_len= 0;
4936 break;
4938 case WM_IME_COMPOSITIONFULL:
4939 break;
4941 case WM_IME_SELECT:
4942 break;
4944 case WM_IME_CONTROL:
4945 break;
4947 default:
4948 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4949 break;
4952 if (IsWindow(hwnd) && es) EDIT_UnlockBuffer(es, FALSE);
4954 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
4956 return result;
4960 /*********************************************************************
4961 * edit class descriptor
4963 static const WCHAR editW[] = {'E','d','i','t',0};
4964 const struct builtin_class_descr EDIT_builtin_class =
4966 editW, /* name */
4967 CS_DBLCLKS | CS_PARENTDC, /* style */
4968 WINPROC_EDIT, /* proc */
4969 #ifdef __i386__
4970 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
4971 #else
4972 sizeof(EDITSTATE *), /* extra */
4973 #endif
4974 IDC_IBEAM, /* cursor */
4975 0 /* brush */