windows.devices.enumeration/tests: Make test handlers structure static.
[wine.git] / dlls / user32 / edit.c
blob74411d385e2468131d311bde5f5102d7ab0dfadf
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 * TODO:
24 * - EN_ALIGN_LTR_EC
25 * - EN_ALIGN_RTL_EC
26 * - ES_OEMCONVERT
30 #include <stdlib.h>
31 #include "user_private.h"
32 #include "controls.h"
33 #include "usp10.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(edit);
37 WINE_DECLARE_DEBUG_CHANNEL(combo);
38 WINE_DECLARE_DEBUG_CHANNEL(relay);
40 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
41 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
42 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
43 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
46 * extra flags for EDITSTATE.flags field
48 #define EF_MODIFIED 0x0001 /* text has been modified */
49 #define EF_FOCUSED 0x0002 /* we have input focus */
50 #define EF_UPDATE 0x0004 /* notify parent of changed state */
51 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
52 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
53 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
54 wrapped line, instead of in front of the next character */
55 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
56 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
58 typedef enum
60 END_0 = 0, /* line ends with terminating '\0' character */
61 END_WRAP, /* line is wrapped */
62 END_HARD, /* line ends with a hard return '\r\n' */
63 END_SOFT, /* line ends with a soft return '\r\r\n' */
64 END_RICH /* line ends with a single '\n' */
65 } LINE_END;
67 typedef struct tagLINEDEF {
68 INT length; /* bruto length of a line in bytes */
69 INT net_length; /* netto length of a line in visible characters */
70 LINE_END ending;
71 INT width; /* width of the line in pixels */
72 INT index; /* line index into the buffer */
73 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
74 struct tagLINEDEF *next;
75 } LINEDEF;
77 typedef struct
79 BOOL is_unicode; /* how the control was created */
80 LPWSTR text; /* the actual contents of the control */
81 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
82 UINT buffer_size; /* the size of the buffer in characters */
83 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
84 HFONT font; /* NULL means standard system font */
85 INT x_offset; /* scroll offset for multi lines this is in pixels
86 for single lines it's in characters */
87 INT line_height; /* height of a screen line in pixels */
88 INT char_width; /* average character width in pixels */
89 DWORD style; /* sane version of wnd->dwStyle */
90 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
91 INT undo_insert_count; /* number of characters inserted in sequence */
92 UINT undo_position; /* character index of the insertion and deletion */
93 LPWSTR undo_text; /* deleted text */
94 UINT undo_buffer_size; /* size of the deleted text buffer */
95 INT selection_start; /* == selection_end if no selection */
96 INT selection_end; /* == current caret position */
97 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
98 INT left_margin; /* in pixels */
99 INT right_margin; /* in pixels */
100 RECT format_rect;
101 INT text_width; /* width of the widest line in pixels for multi line controls
102 and just line width for single line controls */
103 INT region_posx; /* Position of cursor relative to region: */
104 INT region_posy; /* -1: to left, 0: within, 1: to right */
105 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
106 INT line_count; /* number of lines */
107 INT y_offset; /* scroll offset in number of lines */
108 BOOL bCaptureState; /* flag indicating whether mouse was captured */
109 BOOL bEnableState; /* flag keeping the enable state */
110 HWND hwndSelf; /* the our window handle */
111 HWND hwndParent; /* Handle of parent for sending EN_* messages.
112 Even if parent will change, EN_* messages
113 should be sent to the first parent. */
114 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
115 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
117 * only for multi line controls
119 INT lock_count; /* amount of re-entries in the EditWndProc */
120 INT tabs_count;
121 LPINT tabs;
122 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
123 HLOCAL hloc32W; /* our unicode local memory block */
124 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
125 or EM_SETHANDLE */
126 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
128 * IME Data
130 UINT composition_len; /* length of composition, 0 == no composition */
131 int composition_start; /* the character position for the composition */
132 UINT ime_status; /* IME status flag */
134 * Uniscribe Data
136 SCRIPT_LOGATTR *logAttr;
137 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
138 } EDITSTATE;
141 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
142 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
144 static inline BOOL notify_parent(const EDITSTATE *es, INT code)
146 HWND hwnd = es->hwndSelf;
147 TRACE("notification %d sent to %p.\n", code, es->hwndParent);
148 SendMessageW(es->hwndParent, WM_COMMAND, MAKEWPARAM(GetWindowLongPtrW(es->hwndSelf, GWLP_ID), code), (LPARAM)es->hwndSelf);
149 return IsWindow(hwnd);
152 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
154 /*********************************************************************
156 * EM_CANUNDO
159 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
161 return (es->undo_insert_count || lstrlenW(es->undo_text));
165 /*********************************************************************
167 * EM_EMPTYUNDOBUFFER
170 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
172 es->undo_insert_count = 0;
173 *es->undo_text = '\0';
177 /**********************************************************************
178 * get_app_version
180 * Returns the window version in case Wine emulates a later version
181 * of windows than the application expects.
183 * In a number of cases when windows runs an application that was
184 * designed for an earlier windows version, windows reverts
185 * to "old" behaviour of that earlier version.
187 * An example is a disabled edit control that needs to be painted.
188 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
189 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
190 * applications with an expected version 0f 4.0 or higher.
193 static DWORD get_app_version(void)
195 static DWORD version;
196 if (!version)
198 DWORD dwEmulatedVersion;
199 OSVERSIONINFOW info;
200 DWORD dwProcVersion = GetProcessVersion(0);
202 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
203 GetVersionExW( &info );
204 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
205 /* FIXME: this may not be 100% correct; see discussion on the
206 * wine developer list in Nov 1999 */
207 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
209 return version;
212 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
214 HBRUSH hbrush;
215 UINT msg;
217 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
218 msg = WM_CTLCOLORSTATIC;
219 else
220 msg = WM_CTLCOLOREDIT;
222 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
223 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
224 if (!hbrush)
225 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
226 return hbrush;
230 static inline UINT get_text_length(EDITSTATE *es)
232 if(es->text_length == (UINT)-1)
233 es->text_length = lstrlenW(es->text);
234 return es->text_length;
238 /*********************************************************************
240 * EDIT_WordBreakProc
242 * Find the beginning of words.
243 * Note: unlike the specs for a WordBreakProc, this function can
244 * only be called without linebreaks between s[0] up to
245 * s[count - 1]. Remember it is only called
246 * internally, so we can decide this for ourselves.
247 * Additionally we will always be breaking the full string.
250 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
252 INT ret = 0;
254 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
256 if(!s) return 0;
258 if (!es->logAttr)
260 SCRIPT_ANALYSIS psa;
262 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
263 psa.eScript = SCRIPT_UNDEFINED;
265 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
266 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
269 switch (action) {
270 case WB_LEFT:
271 if (index)
272 index--;
273 while (index && !es->logAttr[index].fSoftBreak)
274 index--;
275 ret = index;
276 break;
277 case WB_RIGHT:
278 if (!count)
279 break;
280 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
281 index++;
282 ret = index;
283 break;
284 case WB_ISDELIMITER:
285 ret = es->logAttr[index].fWhiteSpace;
286 break;
287 default:
288 ERR("unknown action code, please report !\n");
289 break;
291 return ret;
295 /*********************************************************************
297 * EDIT_CallWordBreakProc
299 * Call appropriate WordBreakProc (internal or external).
301 * Note: The "start" argument should always be an index referring
302 * to es->text. The actual wordbreak proc might be
303 * 16 bit, so we can't always pass any 32 bit LPSTR.
304 * Hence we assume that es->text is the buffer that holds
305 * the string under examination (we can decide this for ourselves).
308 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
310 INT ret;
312 if (es->word_break_proc)
314 if(es->is_unicode)
316 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
318 TRACE_(relay)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
319 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
320 ret = wbpW(es->text + start, index, count, action);
321 TRACE_(relay)("\1Ret wordbreakW %p retval=%d\n", es->word_break_proc, ret );
323 else
325 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
326 INT countA;
327 CHAR *textA;
329 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
330 textA = HeapAlloc(GetProcessHeap(), 0, countA);
331 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
332 TRACE_(relay)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
333 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
334 ret = wbpA(textA, index, countA, action);
335 HeapFree(GetProcessHeap(), 0, textA);
336 TRACE_(relay)("\1Ret wordbreakA %p retval=%d\n", es->word_break_proc, ret );
339 else
340 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
342 return ret;
345 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
347 if (line_def->ssa)
349 ScriptStringFree(&line_def->ssa);
350 line_def->ssa = NULL;
354 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
356 LINEDEF *line_def = es->first_line_def;
357 while (line_def)
359 EDIT_InvalidateUniscribeData_linedef(line_def);
360 line_def = line_def->next;
362 if (es->ssa)
364 ScriptStringFree(&es->ssa);
365 es->ssa = NULL;
369 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
371 if (!line_def)
372 return NULL;
374 if (line_def->net_length && !line_def->ssa)
376 int index = line_def->index;
377 HFONT old_font = NULL;
378 HDC udc = dc;
379 SCRIPT_TABDEF tabdef;
380 HRESULT hr;
382 if (!udc)
383 udc = NtUserGetDC(es->hwndSelf);
384 if (es->font)
385 old_font = SelectObject(udc, es->font);
387 tabdef.cTabStops = es->tabs_count;
388 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
389 tabdef.pTabStops = es->tabs;
390 tabdef.iTabOrigin = 0;
392 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
393 (1.5*line_def->net_length+16), -1,
394 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
395 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
396 if (FAILED(hr))
398 WARN("ScriptStringAnalyse failed (%lx)\n",hr);
399 line_def->ssa = NULL;
402 if (es->font)
403 SelectObject(udc, old_font);
404 if (udc != dc)
405 NtUserReleaseDC( es->hwndSelf, udc );
408 return line_def->ssa;
411 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
413 LINEDEF *line_def;
415 if (!(es->style & ES_MULTILINE))
417 if (!es->ssa)
419 INT length = get_text_length(es);
420 HFONT old_font = NULL;
421 HDC udc = dc;
423 if (!udc)
424 udc = NtUserGetDC(es->hwndSelf);
425 if (es->font)
426 old_font = SelectObject(udc, es->font);
428 if (es->style & ES_PASSWORD)
429 ScriptStringAnalyse(udc, &es->password_char, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_PASSWORD, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
430 else
431 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
433 if (es->font)
434 SelectObject(udc, old_font);
435 if (udc != dc)
436 NtUserReleaseDC( es->hwndSelf, udc );
438 return es->ssa;
440 else
442 line_def = es->first_line_def;
443 while (line_def && line)
445 line_def = line_def->next;
446 line--;
449 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
453 static inline INT get_vertical_line_count(EDITSTATE *es)
455 INT vlc = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
456 return max(1,vlc);
459 /*********************************************************************
461 * EDIT_BuildLineDefs_ML
463 * Build linked list of text lines.
464 * Lines can end with '\0' (last line), a character (if it is wrapped),
465 * a soft return '\r\r\n' or a hard return '\r\n'
468 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
470 LPWSTR current_position, cp;
471 INT fw;
472 LINEDEF *current_line;
473 LINEDEF *previous_line;
474 LINEDEF *start_line;
475 INT line_index = 0, nstart_line, nstart_index;
476 INT line_count = es->line_count;
477 INT orig_net_length;
478 RECT rc;
479 INT vlc;
481 if (istart == iend && delta == 0)
482 return;
484 previous_line = NULL;
485 current_line = es->first_line_def;
487 /* Find starting line. istart must lie inside an existing line or
488 * at the end of buffer */
489 do {
490 if (istart < current_line->index + current_line->length ||
491 current_line->ending == END_0)
492 break;
494 previous_line = current_line;
495 current_line = current_line->next;
496 line_index++;
497 } while (current_line);
499 if (!current_line) /* Error occurred start is not inside previous buffer */
501 FIXME(" modification occurred outside buffer\n");
502 return;
505 /* Remember start of modifications in order to calculate update region */
506 nstart_line = line_index;
507 nstart_index = current_line->index;
509 /* We must start to reformat from the previous line since the modifications
510 * may have caused the line to wrap upwards. */
511 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
513 line_index--;
514 current_line = previous_line;
516 start_line = current_line;
518 fw = es->format_rect.right - es->format_rect.left;
519 current_position = es->text + current_line->index;
520 vlc = get_vertical_line_count(es);
521 do {
522 if (current_line != start_line)
524 if (!current_line || current_line->index + delta > current_position - es->text)
526 /* The buffer has been expanded, create a new line and
527 insert it into the link list */
528 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
529 new_line->next = previous_line->next;
530 previous_line->next = new_line;
531 current_line = new_line;
532 es->line_count++;
534 else if (current_line->index + delta < current_position - es->text)
536 /* The previous line merged with this line so we delete this extra entry */
537 previous_line->next = current_line->next;
538 HeapFree(GetProcessHeap(), 0, current_line);
539 current_line = previous_line->next;
540 es->line_count--;
541 continue;
543 else /* current_line->index + delta == current_position */
545 if (current_position - es->text > iend)
546 break; /* We reached end of line modifications */
547 /* else recalculate this line */
551 current_line->index = current_position - es->text;
552 orig_net_length = current_line->net_length;
554 /* Find end of line */
555 cp = current_position;
556 while (*cp) {
557 if (*cp == '\n') break;
558 if ((*cp == '\r') && (*(cp + 1) == '\n'))
559 break;
560 cp++;
563 /* Mark type of line termination */
564 if (!(*cp)) {
565 current_line->ending = END_0;
566 current_line->net_length = lstrlenW(current_position);
567 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
568 current_line->ending = END_SOFT;
569 current_line->net_length = cp - current_position - 1;
570 } else if (*cp == '\n') {
571 current_line->ending = END_RICH;
572 current_line->net_length = cp - current_position;
573 } else {
574 current_line->ending = END_HARD;
575 current_line->net_length = cp - current_position;
578 if (current_line->net_length)
580 const SIZE *sz;
581 EDIT_InvalidateUniscribeData_linedef(current_line);
582 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
583 if (current_line->ssa)
585 sz = ScriptString_pSize(current_line->ssa);
586 /* Calculate line width */
587 current_line->width = sz->cx;
589 else current_line->width = es->char_width * current_line->net_length;
591 else current_line->width = 0;
593 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
595 /* Line breaks just look back from the end and find the next break and try that. */
597 if (!(es->style & ES_AUTOHSCROLL)) {
598 if (current_line->width > fw && fw > es->char_width) {
600 INT prev, next;
601 int w;
602 const SIZE *sz;
603 float d;
605 prev = current_line->net_length - 1;
606 w = current_line->net_length;
607 d = (float)current_line->width/(float)fw;
608 if (d > 1.2f) d -= 0.2f;
609 next = prev/d;
610 if (next >= prev) next = prev-1;
611 do {
612 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
613 next, current_line->net_length, WB_LEFT);
614 current_line->net_length = prev;
615 EDIT_InvalidateUniscribeData_linedef(current_line);
616 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
617 if (current_line->ssa)
618 sz = ScriptString_pSize(current_line->ssa);
619 else sz = 0;
620 if (sz)
621 current_line->width = sz->cx;
622 else
623 prev = 0;
624 next = prev - 1;
625 } while (prev && current_line->width > fw);
626 current_line->net_length = w;
628 if (prev == 0) { /* Didn't find a line break so force a break */
629 INT *piDx;
630 const INT *count;
632 EDIT_InvalidateUniscribeData_linedef(current_line);
633 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
635 if (current_line->ssa)
637 count = ScriptString_pcOutChars(current_line->ssa);
638 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
639 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
641 prev = current_line->net_length-1;
642 do {
643 current_line->width -= piDx[prev];
644 prev--;
645 } while ( prev > 0 && current_line->width > fw);
646 if (prev<=0)
647 prev = 1;
648 HeapFree(GetProcessHeap(),0,piDx);
650 else
651 prev = (fw / es->char_width);
654 /* If the first line we are calculating, wrapped before istart, we must
655 * adjust istart in order for this to be reflected in the update region. */
656 if (current_line->index == nstart_index && istart > current_line->index + prev)
657 istart = current_line->index + prev;
658 /* else if we are updating the previous line before the first line we
659 * are re-calculating and it expanded */
660 else if (current_line == start_line &&
661 current_line->index != nstart_index && orig_net_length < prev)
663 /* Line expanded due to an upwards line wrap so we must partially include
664 * previous line in update region */
665 nstart_line = line_index;
666 nstart_index = current_line->index;
667 istart = current_line->index + orig_net_length;
670 current_line->net_length = prev;
671 current_line->ending = END_WRAP;
673 if (current_line->net_length > 0)
675 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
676 if (current_line->ssa)
678 sz = ScriptString_pSize(current_line->ssa);
679 current_line->width = sz->cx;
681 else
682 current_line->width = 0;
684 else current_line->width = 0;
686 else if (current_line == start_line &&
687 current_line->index != nstart_index &&
688 orig_net_length < current_line->net_length) {
689 /* The previous line expanded but it's still not as wide as the client rect */
690 /* The expansion is due to an upwards line wrap so we must partially include
691 it in the update region */
692 nstart_line = line_index;
693 nstart_index = current_line->index;
694 istart = current_line->index + orig_net_length;
699 /* Adjust length to include line termination */
700 switch (current_line->ending) {
701 case END_SOFT:
702 current_line->length = current_line->net_length + 3;
703 break;
704 case END_RICH:
705 current_line->length = current_line->net_length + 1;
706 break;
707 case END_HARD:
708 current_line->length = current_line->net_length + 2;
709 break;
710 case END_WRAP:
711 case END_0:
712 current_line->length = current_line->net_length;
713 break;
715 es->text_width = max(es->text_width, current_line->width);
716 current_position += current_line->length;
717 previous_line = current_line;
719 /* Discard data for non-visible lines. It will be calculated as needed */
720 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
721 EDIT_InvalidateUniscribeData_linedef(current_line);
723 current_line = current_line->next;
724 line_index++;
725 } while (previous_line->ending != END_0);
727 /* Finish adjusting line indexes by delta or remove hanging lines */
728 if (previous_line->ending == END_0)
730 LINEDEF *pnext = NULL;
732 previous_line->next = NULL;
733 while (current_line)
735 pnext = current_line->next;
736 EDIT_InvalidateUniscribeData_linedef(current_line);
737 HeapFree(GetProcessHeap(), 0, current_line);
738 current_line = pnext;
739 es->line_count--;
742 else if (delta != 0)
744 while (current_line)
746 current_line->index += delta;
747 current_line = current_line->next;
751 /* Calculate rest of modification rectangle */
752 if (hrgn)
754 HRGN tmphrgn;
756 * We calculate two rectangles. One for the first line which may have
757 * an indent with respect to the format rect. The other is a format-width
758 * rectangle that spans the rest of the lines that changed or moved.
760 rc.top = es->format_rect.top + nstart_line * es->line_height -
761 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
762 rc.bottom = rc.top + es->line_height;
763 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
764 rc.left = es->format_rect.left;
765 else
766 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
767 rc.right = es->format_rect.right;
768 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
770 rc.top = rc.bottom;
771 rc.left = es->format_rect.left;
772 rc.right = es->format_rect.right;
774 * If lines were added or removed we must re-paint the remainder of the
775 * lines since the remaining lines were either shifted up or down.
777 if (line_count < es->line_count) /* We added lines */
778 rc.bottom = es->line_count * es->line_height;
779 else if (line_count > es->line_count) /* We removed lines */
780 rc.bottom = line_count * es->line_height;
781 else
782 rc.bottom = line_index * es->line_height;
783 rc.bottom += es->format_rect.top;
784 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
785 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
786 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
787 DeleteObject(tmphrgn);
791 /*********************************************************************
793 * EDIT_CalcLineWidth_SL
796 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
798 EDIT_UpdateUniscribeData(es, NULL, 0);
799 if (es->ssa)
801 const SIZE *size;
802 size = ScriptString_pSize(es->ssa);
803 es->text_width = size->cx;
805 else
806 es->text_width = 0;
809 /*********************************************************************
811 * EDIT_CharFromPos
813 * Beware: This is not the function called on EM_CHARFROMPOS
814 * The position _can_ be outside the formatting / client
815 * rectangle
816 * The return value is only the character index
819 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
821 INT index;
823 if (es->style & ES_MULTILINE) {
824 int trailing;
825 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
826 INT line_index = 0;
827 LINEDEF *line_def = es->first_line_def;
828 EDIT_UpdateUniscribeData(es, NULL, line);
829 while ((line > 0) && line_def->next) {
830 line_index += line_def->length;
831 line_def = line_def->next;
832 line--;
835 x += es->x_offset - es->format_rect.left;
836 if (es->style & ES_RIGHT)
837 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
838 else if (es->style & ES_CENTER)
839 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
840 if (x >= line_def->width) {
841 if (after_wrap)
842 *after_wrap = (line_def->ending == END_WRAP);
843 return line_index + line_def->net_length;
845 if (x <= 0 || !line_def->ssa) {
846 if (after_wrap)
847 *after_wrap = FALSE;
848 return line_index;
851 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
852 if (trailing) index++;
853 index += line_index;
854 if (after_wrap)
855 *after_wrap = ((index == line_index + line_def->net_length) &&
856 (line_def->ending == END_WRAP));
857 } else {
858 INT xoff = 0;
859 INT trailing;
860 if (after_wrap)
861 *after_wrap = FALSE;
862 x -= es->format_rect.left;
863 if (!x)
864 return es->x_offset;
866 if (!es->x_offset)
868 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
869 if (es->style & ES_RIGHT)
870 x -= indent;
871 else if (es->style & ES_CENTER)
872 x -= indent / 2;
875 EDIT_UpdateUniscribeData(es, NULL, 0);
876 if (es->x_offset)
878 if (es->ssa)
880 if (es->x_offset>= get_text_length(es))
882 const SIZE *size;
883 size = ScriptString_pSize(es->ssa);
884 xoff = size->cx;
886 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
888 else
889 xoff = 0;
891 if (x < 0)
893 if (x + xoff > 0 || !es->ssa)
895 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
896 if (trailing) index++;
898 else
899 index = 0;
901 else
903 if (x)
905 const SIZE *size = NULL;
906 if (es->ssa)
907 size = ScriptString_pSize(es->ssa);
908 if (!size)
909 index = 0;
910 else if (x > size->cx)
911 index = get_text_length(es);
912 else if (es->ssa)
914 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
915 if (trailing) index++;
917 else
918 index = 0;
920 else
921 index = es->x_offset;
924 return index;
928 /*********************************************************************
930 * EDIT_ConfinePoint
932 * adjusts the point to be within the formatting rectangle
933 * (so CharFromPos returns the nearest _visible_ character)
936 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
938 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
939 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
943 /*********************************************************************
945 * EM_LINEFROMCHAR
948 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
950 INT line;
951 LINEDEF *line_def;
953 if (!(es->style & ES_MULTILINE))
954 return 0;
955 if (index > (INT)get_text_length(es))
956 return es->line_count - 1;
957 if (index == -1)
958 index = min(es->selection_start, es->selection_end);
960 line = 0;
961 line_def = es->first_line_def;
962 index -= line_def->length;
963 while ((index >= 0) && line_def->next) {
964 line++;
965 line_def = line_def->next;
966 index -= line_def->length;
968 return line;
972 /*********************************************************************
974 * EM_LINEINDEX
977 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
979 INT line_index;
980 const LINEDEF *line_def;
982 if (!(es->style & ES_MULTILINE))
983 return 0;
984 if (line >= es->line_count)
985 return -1;
987 line_index = 0;
988 line_def = es->first_line_def;
989 if (line == -1) {
990 INT index = es->selection_end - line_def->length;
991 while ((index >= 0) && line_def->next) {
992 line_index += line_def->length;
993 line_def = line_def->next;
994 index -= line_def->length;
996 } else {
997 while (line > 0) {
998 line_index += line_def->length;
999 line_def = line_def->next;
1000 line--;
1003 return line_index;
1007 /*********************************************************************
1009 * EM_LINELENGTH
1012 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1014 LINEDEF *line_def;
1016 if (!(es->style & ES_MULTILINE))
1017 return get_text_length(es);
1019 if (index == -1) {
1020 /* get the number of remaining non-selected chars of selected lines */
1021 INT32 l; /* line number */
1022 INT32 li; /* index of first char in line */
1023 INT32 count;
1024 l = EDIT_EM_LineFromChar(es, es->selection_start);
1025 /* # chars before start of selection area */
1026 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1027 l = EDIT_EM_LineFromChar(es, es->selection_end);
1028 /* # chars after end of selection */
1029 li = EDIT_EM_LineIndex(es, l);
1030 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1031 return count;
1033 line_def = es->first_line_def;
1034 index -= line_def->length;
1035 while ((index >= 0) && line_def->next) {
1036 line_def = line_def->next;
1037 index -= line_def->length;
1039 return line_def->net_length;
1043 /*********************************************************************
1045 * EM_POSFROMCHAR
1048 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1050 INT len = get_text_length(es);
1051 INT l;
1052 INT li;
1053 INT x = 0;
1054 INT y = 0;
1055 INT w;
1056 INT lw;
1057 LINEDEF *line_def;
1059 index = min(index, len);
1060 if (es->style & ES_MULTILINE) {
1061 l = EDIT_EM_LineFromChar(es, index);
1062 EDIT_UpdateUniscribeData(es, NULL, l);
1064 y = (l - es->y_offset) * es->line_height;
1065 li = EDIT_EM_LineIndex(es, l);
1066 if (after_wrap && (li == index) && l) {
1067 INT l2 = l - 1;
1068 line_def = es->first_line_def;
1069 while (l2) {
1070 line_def = line_def->next;
1071 l2--;
1073 if (line_def->ending == END_WRAP) {
1074 l--;
1075 y -= es->line_height;
1076 li = EDIT_EM_LineIndex(es, l);
1080 line_def = es->first_line_def;
1081 while (line_def->index != li)
1082 line_def = line_def->next;
1084 lw = line_def->width;
1085 w = es->format_rect.right - es->format_rect.left;
1086 if (line_def->ssa)
1087 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1088 x -= es->x_offset;
1090 if (es->style & ES_RIGHT)
1091 x = w - (lw - x);
1092 else if (es->style & ES_CENTER)
1093 x += (w - lw) / 2;
1094 } else {
1095 INT xoff = 0;
1096 INT xi = 0;
1097 EDIT_UpdateUniscribeData(es, NULL, 0);
1098 if (es->x_offset)
1100 if (es->ssa)
1102 if (es->x_offset >= get_text_length(es))
1104 int leftover = es->x_offset - get_text_length(es);
1105 if (es->ssa)
1107 const SIZE *size;
1108 size = ScriptString_pSize(es->ssa);
1109 xoff = size->cx;
1111 else
1112 xoff = 0;
1113 xoff += es->char_width * leftover;
1115 else
1116 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1118 else
1119 xoff = 0;
1121 if (index)
1123 if (index >= get_text_length(es))
1125 if (es->ssa)
1127 const SIZE *size;
1128 size = ScriptString_pSize(es->ssa);
1129 xi = size->cx;
1131 else
1132 xi = 0;
1134 else if (es->ssa)
1135 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1136 else
1137 xi = 0;
1139 x = xi - xoff;
1141 if (index >= es->x_offset) {
1142 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1144 w = es->format_rect.right - es->format_rect.left;
1145 if (w > es->text_width)
1147 if (es->style & ES_RIGHT)
1148 x += w - es->text_width;
1149 else if (es->style & ES_CENTER)
1150 x += (w - es->text_width) / 2;
1154 y = 0;
1156 x += es->format_rect.left;
1157 y += es->format_rect.top;
1158 return MAKELONG((INT16)x, (INT16)y);
1162 /*********************************************************************
1164 * EDIT_GetLineRect
1166 * Calculates the bounding rectangle for a line from a starting
1167 * column to an ending column.
1170 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1172 SCRIPT_STRING_ANALYSIS ssa;
1173 INT line_index = 0;
1174 INT pt1, pt2, pt3;
1176 if (es->style & ES_MULTILINE)
1178 const LINEDEF *line_def = NULL;
1179 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1180 if (line >= es->line_count)
1181 return;
1183 line_def = es->first_line_def;
1184 if (line == -1) {
1185 INT index = es->selection_end - line_def->length;
1186 while ((index >= 0) && line_def->next) {
1187 line_index += line_def->length;
1188 line_def = line_def->next;
1189 index -= line_def->length;
1191 } else {
1192 while (line > 0) {
1193 line_index += line_def->length;
1194 line_def = line_def->next;
1195 line--;
1198 ssa = line_def->ssa;
1200 else
1202 line_index = 0;
1203 rc->top = es->format_rect.top;
1204 ssa = es->ssa;
1207 rc->bottom = rc->top + es->line_height;
1208 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1209 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1210 if (ssa)
1212 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1213 pt3+=es->format_rect.left;
1215 else pt3 = pt1;
1216 rc->right = max(max(pt1 , pt2),pt3);
1217 rc->left = min(min(pt1, pt2),pt3);
1221 static inline void text_buffer_changed(EDITSTATE *es)
1223 es->text_length = (UINT)-1;
1225 HeapFree( GetProcessHeap(), 0, es->logAttr );
1226 es->logAttr = NULL;
1227 EDIT_InvalidateUniscribeData(es);
1230 /*********************************************************************
1231 * EDIT_LockBuffer
1234 static void EDIT_LockBuffer(EDITSTATE *es)
1236 if (!es->text) {
1238 if(!es->hloc32W) return;
1240 if(es->hloc32A)
1242 CHAR *textA = LocalLock(es->hloc32A);
1243 HLOCAL hloc32W_new;
1244 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1245 if(countW_new > es->buffer_size + 1)
1247 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1248 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1249 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1250 if(hloc32W_new)
1252 es->hloc32W = hloc32W_new;
1253 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1254 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1256 else
1257 WARN("FAILED! Will synchronize partially\n");
1259 es->text = LocalLock(es->hloc32W);
1260 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1261 LocalUnlock(es->hloc32A);
1263 else es->text = LocalLock(es->hloc32W);
1265 es->lock_count++;
1269 /*********************************************************************
1271 * EDIT_UnlockBuffer
1274 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1276 /* Edit window might be already destroyed */
1277 if(!IsWindow(es->hwndSelf))
1279 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1280 return;
1283 if (!es->lock_count) {
1284 ERR("lock_count == 0 ... please report\n");
1285 return;
1287 if (!es->text) {
1288 ERR("es->text == 0 ... please report\n");
1289 return;
1291 if (force || (es->lock_count == 1)) {
1292 if (es->hloc32W) {
1293 UINT countA = 0;
1294 UINT countW = get_text_length(es) + 1;
1296 if(es->hloc32A)
1298 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1299 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1300 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1301 countA = LocalSize(es->hloc32A);
1302 if(countA_new > countA)
1304 HLOCAL hloc32A_new;
1305 UINT alloc_size = ROUND_TO_GROW(countA_new);
1306 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1307 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1308 if(hloc32A_new)
1310 es->hloc32A = hloc32A_new;
1311 countA = LocalSize(hloc32A_new);
1312 TRACE("Real new size %d bytes\n", countA);
1314 else
1315 WARN("FAILED! Will synchronize partially\n");
1317 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1318 LocalLock(es->hloc32A), countA, NULL, NULL);
1319 LocalUnlock(es->hloc32A);
1322 LocalUnlock(es->hloc32W);
1323 es->text = NULL;
1325 else {
1326 ERR("no buffer ... please report\n");
1327 return;
1330 es->lock_count--;
1334 /*********************************************************************
1336 * EDIT_MakeFit
1338 * Try to fit size + 1 characters in the buffer.
1340 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1342 HLOCAL hNew32W;
1344 if (size <= es->buffer_size)
1345 return TRUE;
1347 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1349 /* Force edit to unlock its buffer. es->text now NULL */
1350 EDIT_UnlockBuffer(es, TRUE);
1352 if (es->hloc32W) {
1353 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1354 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1355 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1356 es->hloc32W = hNew32W;
1357 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1361 EDIT_LockBuffer(es);
1363 if (es->buffer_size < size) {
1364 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1365 notify_parent(es, EN_ERRSPACE);
1366 return FALSE;
1367 } else {
1368 TRACE("We now have %d+1\n", es->buffer_size);
1369 return TRUE;
1374 /*********************************************************************
1376 * EDIT_MakeUndoFit
1378 * Try to fit size + 1 bytes in the undo buffer.
1381 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1383 UINT alloc_size;
1385 if (size <= es->undo_buffer_size)
1386 return TRUE;
1388 TRACE("trying to ReAlloc to %d+1\n", size);
1390 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1391 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1392 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1393 return TRUE;
1395 else
1397 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1398 return FALSE;
1403 /*********************************************************************
1405 * EDIT_UpdateTextRegion
1408 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1410 if (es->flags & EF_UPDATE) {
1411 es->flags &= ~EF_UPDATE;
1412 if (!notify_parent(es, EN_UPDATE)) return;
1414 NtUserInvalidateRgn(es->hwndSelf, hrgn, bErase);
1418 /*********************************************************************
1420 * EDIT_UpdateText
1423 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1425 if (es->flags & EF_UPDATE) {
1426 es->flags &= ~EF_UPDATE;
1427 if (!notify_parent(es, EN_UPDATE)) return;
1429 NtUserInvalidateRect(es->hwndSelf, rc, bErase);
1432 /*********************************************************************
1434 * EDIT_SL_InvalidateText
1436 * Called from EDIT_InvalidateText().
1437 * Does the job for single-line controls only.
1440 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1442 RECT line_rect;
1443 RECT rc;
1445 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1446 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1447 EDIT_UpdateText(es, &rc, TRUE);
1450 /*********************************************************************
1452 * EDIT_ML_InvalidateText
1454 * Called from EDIT_InvalidateText().
1455 * Does the job for multi-line controls only.
1458 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1460 INT vlc = get_vertical_line_count(es);
1461 INT sl = EDIT_EM_LineFromChar(es, start);
1462 INT el = EDIT_EM_LineFromChar(es, end);
1463 INT sc;
1464 INT ec;
1465 RECT rc1;
1466 RECT rcWnd;
1467 RECT rcLine;
1468 RECT rcUpdate;
1469 INT l;
1471 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1472 return;
1474 sc = start - EDIT_EM_LineIndex(es, sl);
1475 ec = end - EDIT_EM_LineIndex(es, el);
1476 if (sl < es->y_offset) {
1477 sl = es->y_offset;
1478 sc = 0;
1480 if (el > es->y_offset + vlc) {
1481 el = es->y_offset + vlc;
1482 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1484 GetClientRect(es->hwndSelf, &rc1);
1485 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1486 if (sl == el) {
1487 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1488 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1489 EDIT_UpdateText(es, &rcUpdate, TRUE);
1490 } else {
1491 EDIT_GetLineRect(es, sl, sc,
1492 EDIT_EM_LineLength(es,
1493 EDIT_EM_LineIndex(es, sl)),
1494 &rcLine);
1495 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1496 EDIT_UpdateText(es, &rcUpdate, TRUE);
1497 for (l = sl + 1 ; l < el ; l++) {
1498 EDIT_GetLineRect(es, l, 0,
1499 EDIT_EM_LineLength(es,
1500 EDIT_EM_LineIndex(es, l)),
1501 &rcLine);
1502 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1503 EDIT_UpdateText(es, &rcUpdate, TRUE);
1505 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1506 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1507 EDIT_UpdateText(es, &rcUpdate, TRUE);
1512 /*********************************************************************
1514 * EDIT_InvalidateText
1516 * Invalidate the text from offset start up to, but not including,
1517 * offset end. Useful for (re)painting the selection.
1518 * Regions outside the linewidth are not invalidated.
1519 * end == -1 means end == TextLength.
1520 * start and end need not be ordered.
1523 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1525 if (end == start)
1526 return;
1528 if (end == -1)
1529 end = get_text_length(es);
1531 if (end < start) {
1532 INT tmp = start;
1533 start = end;
1534 end = tmp;
1537 if (es->style & ES_MULTILINE)
1538 EDIT_ML_InvalidateText(es, start, end);
1539 else
1540 EDIT_SL_InvalidateText(es, start, end);
1544 /*********************************************************************
1546 * EDIT_EM_SetSel
1548 * note: unlike the specs say: the order of start and end
1549 * _is_ preserved in Windows. (i.e. start can be > end)
1550 * In other words: this handler is OK
1553 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1555 UINT old_start = es->selection_start;
1556 UINT old_end = es->selection_end;
1557 UINT len = get_text_length(es);
1559 if (start == (UINT)-1) {
1560 start = es->selection_end;
1561 end = es->selection_end;
1562 } else {
1563 start = min(start, len);
1564 end = min(end, len);
1566 es->selection_start = start;
1567 es->selection_end = end;
1568 if (after_wrap)
1569 es->flags |= EF_AFTER_WRAP;
1570 else
1571 es->flags &= ~EF_AFTER_WRAP;
1572 /* Compute the necessary invalidation region. */
1573 /* Note that we don't need to invalidate regions which have
1574 * "never" been selected, or those which are "still" selected.
1575 * In fact, every time we hit a selection boundary, we can
1576 * *toggle* whether we need to invalidate. Thus we can optimize by
1577 * *sorting* the interval endpoints. Let's assume that we sort them
1578 * in this order:
1579 * start <= end <= old_start <= old_end
1580 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1581 * in 5 comparisons; i.e. it is impossible to do better than the
1582 * following: */
1583 ORDER_UINT(end, old_end);
1584 ORDER_UINT(start, old_start);
1585 ORDER_UINT(old_start, old_end);
1586 ORDER_UINT(start, end);
1587 /* Note that at this point 'end' and 'old_start' are not in order, but
1588 * start is definitely the min. and old_end is definitely the max. */
1589 if (end != old_start)
1592 * One can also do
1593 * ORDER_UINT32(end, old_start);
1594 * EDIT_InvalidateText(es, start, end);
1595 * EDIT_InvalidateText(es, old_start, old_end);
1596 * in place of the following if statement.
1597 * (That would complete the optimal five-comparison four-element sort.)
1599 if (old_start > end )
1601 EDIT_InvalidateText(es, start, end);
1602 EDIT_InvalidateText(es, old_start, old_end);
1604 else
1606 EDIT_InvalidateText(es, start, old_start);
1607 EDIT_InvalidateText(es, end, old_end);
1610 else EDIT_InvalidateText(es, start, old_end);
1614 /*********************************************************************
1616 * EDIT_UpdateScrollInfo
1619 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1621 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1623 SCROLLINFO si;
1624 si.cbSize = sizeof(SCROLLINFO);
1625 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1626 si.nMin = 0;
1627 si.nMax = es->line_count - 1;
1628 si.nPage = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
1629 si.nPos = es->y_offset;
1630 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1631 si.nMin, si.nMax, si.nPage, si.nPos);
1632 NtUserSetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1635 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1637 SCROLLINFO si;
1638 si.cbSize = sizeof(SCROLLINFO);
1639 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1640 si.nMin = 0;
1641 si.nMax = es->text_width - 1;
1642 si.nPage = es->format_rect.right - es->format_rect.left;
1643 si.nPos = es->x_offset;
1644 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1645 si.nMin, si.nMax, si.nPage, si.nPos);
1646 NtUserSetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1651 /*********************************************************************
1653 * EDIT_EM_LineScroll_internal
1655 * Version of EDIT_EM_LineScroll for internal use.
1656 * It doesn't refuse if ES_MULTILINE is set and assumes that
1657 * dx is in pixels, dy - in lines.
1660 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1662 INT nyoff;
1663 INT x_offset_in_pixels;
1664 INT lines_per_page;
1666 if (!es->line_height || !es->char_width)
1667 return TRUE;
1669 lines_per_page = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1671 if (es->style & ES_MULTILINE)
1673 x_offset_in_pixels = es->x_offset;
1675 else
1677 dy = 0;
1678 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1681 if (-dx > x_offset_in_pixels)
1682 dx = -x_offset_in_pixels;
1683 if (dx > es->text_width - x_offset_in_pixels)
1684 dx = es->text_width - x_offset_in_pixels;
1685 nyoff = max(0, es->y_offset + dy);
1686 if (nyoff >= es->line_count - lines_per_page)
1687 nyoff = max(0, es->line_count - lines_per_page);
1688 dy = (es->y_offset - nyoff) * es->line_height;
1689 if (dx || dy) {
1690 RECT rc1;
1691 RECT rc;
1693 es->y_offset = nyoff;
1694 if(es->style & ES_MULTILINE)
1695 es->x_offset += dx;
1696 else
1697 es->x_offset += dx / es->char_width;
1699 GetClientRect(es->hwndSelf, &rc1);
1700 IntersectRect(&rc, &rc1, &es->format_rect);
1701 NtUserScrollWindowEx(es->hwndSelf, -dx, dy,
1702 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1703 /* force scroll info update */
1704 EDIT_UpdateScrollInfo(es);
1706 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1707 notify_parent(es, EN_HSCROLL);
1708 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1709 notify_parent(es, EN_VSCROLL);
1710 return TRUE;
1713 /*********************************************************************
1715 * EM_LINESCROLL
1717 * NOTE: dx is in average character widths, dy - in lines;
1720 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1722 if (!(es->style & ES_MULTILINE))
1723 return FALSE;
1725 dx *= es->char_width;
1726 return EDIT_EM_LineScroll_internal(es, dx, dy);
1730 /*********************************************************************
1732 * EM_SCROLL
1735 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1737 INT dy;
1739 if (!(es->style & ES_MULTILINE))
1740 return (LRESULT)FALSE;
1742 dy = 0;
1744 switch (action) {
1745 case SB_LINEUP:
1746 if (es->y_offset)
1747 dy = -1;
1748 break;
1749 case SB_LINEDOWN:
1750 if (es->y_offset < es->line_count - 1)
1751 dy = 1;
1752 break;
1753 case SB_PAGEUP:
1754 if (es->y_offset)
1755 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1756 break;
1757 case SB_PAGEDOWN:
1758 if (es->y_offset < es->line_count - 1)
1759 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1760 break;
1761 default:
1762 return (LRESULT)FALSE;
1764 if (dy) {
1765 INT vlc = get_vertical_line_count(es);
1766 /* check if we are going to move too far */
1767 if(es->y_offset + dy > es->line_count - vlc)
1768 dy = max(es->line_count - vlc, 0) - es->y_offset;
1770 /* Notification is done in EDIT_EM_LineScroll */
1771 if(dy) {
1772 EDIT_EM_LineScroll(es, 0, dy);
1773 return MAKELONG(dy, TRUE);
1777 return (LRESULT)FALSE;
1781 /*********************************************************************
1783 * EDIT_SetCaretPos
1786 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1787 BOOL after_wrap)
1789 LRESULT res;
1791 if (es->flags & EF_FOCUSED)
1793 res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1794 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1795 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1800 /*********************************************************************
1802 * EM_SCROLLCARET
1805 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1807 if (es->style & ES_MULTILINE) {
1808 INT l;
1809 INT vlc;
1810 INT ww;
1811 INT cw = es->char_width;
1812 INT x;
1813 INT dy = 0;
1814 INT dx = 0;
1816 l = EDIT_EM_LineFromChar(es, es->selection_end);
1817 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1818 vlc = get_vertical_line_count(es);
1819 if (l >= es->y_offset + vlc)
1820 dy = l - vlc + 1 - es->y_offset;
1821 if (l < es->y_offset)
1822 dy = l - es->y_offset;
1823 ww = es->format_rect.right - es->format_rect.left;
1824 if (x < es->format_rect.left)
1825 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1826 if (x > es->format_rect.right)
1827 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1828 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1830 /* check if we are going to move too far */
1831 if(es->x_offset + dx + ww > es->text_width)
1832 dx = es->text_width - ww - es->x_offset;
1833 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1834 EDIT_EM_LineScroll_internal(es, dx, dy);
1836 } else {
1837 INT x;
1838 INT goal;
1839 INT format_width;
1841 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1842 format_width = es->format_rect.right - es->format_rect.left;
1843 if (x < es->format_rect.left) {
1844 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1845 do {
1846 es->x_offset--;
1847 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1848 } while ((x < goal) && es->x_offset);
1849 /* FIXME: use ScrollWindow() somehow to improve performance */
1850 EDIT_UpdateText(es, NULL, TRUE);
1851 } else if (x > es->format_rect.right) {
1852 INT x_last;
1853 INT len = get_text_length(es);
1854 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1855 do {
1856 es->x_offset++;
1857 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1858 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1859 } while ((x > goal) && (x_last > es->format_rect.right));
1860 /* FIXME: use ScrollWindow() somehow to improve performance */
1861 EDIT_UpdateText(es, NULL, TRUE);
1865 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1869 /*********************************************************************
1871 * EDIT_MoveBackward
1874 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1876 INT e = es->selection_end;
1878 if (e) {
1879 e--;
1880 if ((es->style & ES_MULTILINE) && e &&
1881 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1882 e--;
1883 if (e && (es->text[e - 1] == '\r'))
1884 e--;
1887 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1888 EDIT_EM_ScrollCaret(es);
1892 /*********************************************************************
1894 * EDIT_MoveDown_ML
1896 * Only for multi line controls
1897 * Move the caret one line down, on a column with the nearest
1898 * x coordinate on the screen (might be a different column).
1901 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1903 INT s = es->selection_start;
1904 INT e = es->selection_end;
1905 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1906 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1907 INT x = (short)LOWORD(pos);
1908 INT y = (short)HIWORD(pos);
1910 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1911 if (!extend)
1912 s = e;
1913 EDIT_EM_SetSel(es, s, e, after_wrap);
1914 EDIT_EM_ScrollCaret(es);
1918 /*********************************************************************
1920 * EDIT_MoveEnd
1923 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1925 BOOL after_wrap = FALSE;
1926 INT e;
1928 /* Pass a high value in x to make sure of receiving the end of the line */
1929 if (!ctrl && (es->style & ES_MULTILINE))
1930 e = EDIT_CharFromPos(es, 0x3fffffff,
1931 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1932 else
1933 e = get_text_length(es);
1934 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1935 EDIT_EM_ScrollCaret(es);
1939 /*********************************************************************
1941 * EDIT_MoveForward
1944 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1946 INT e = es->selection_end;
1948 if (es->text[e]) {
1949 e++;
1950 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1951 if (es->text[e] == '\n')
1952 e++;
1953 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1954 e += 2;
1957 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1958 EDIT_EM_ScrollCaret(es);
1962 /*********************************************************************
1964 * EDIT_MoveHome
1966 * Home key: move to beginning of line.
1969 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1971 INT e;
1973 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1974 if (!ctrl && (es->style & ES_MULTILINE))
1975 e = EDIT_CharFromPos(es, -es->x_offset,
1976 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1977 else
1978 e = 0;
1979 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1980 EDIT_EM_ScrollCaret(es);
1984 /*********************************************************************
1986 * EDIT_MovePageDown_ML
1988 * Only for multi line controls
1989 * Move the caret one page down, on a column with the nearest
1990 * x coordinate on the screen (might be a different column).
1993 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1995 INT s = es->selection_start;
1996 INT e = es->selection_end;
1997 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1998 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1999 INT x = (short)LOWORD(pos);
2000 INT y = (short)HIWORD(pos);
2002 e = EDIT_CharFromPos(es, x,
2003 y + (es->format_rect.bottom - es->format_rect.top),
2004 &after_wrap);
2005 if (!extend)
2006 s = e;
2007 EDIT_EM_SetSel(es, s, e, after_wrap);
2008 EDIT_EM_ScrollCaret(es);
2012 /*********************************************************************
2014 * EDIT_MovePageUp_ML
2016 * Only for multi line controls
2017 * Move the caret one page up, on a column with the nearest
2018 * x coordinate on the screen (might be a different column).
2021 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2023 INT s = es->selection_start;
2024 INT e = es->selection_end;
2025 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2026 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2027 INT x = (short)LOWORD(pos);
2028 INT y = (short)HIWORD(pos);
2030 e = EDIT_CharFromPos(es, x,
2031 y - (es->format_rect.bottom - es->format_rect.top),
2032 &after_wrap);
2033 if (!extend)
2034 s = e;
2035 EDIT_EM_SetSel(es, s, e, after_wrap);
2036 EDIT_EM_ScrollCaret(es);
2040 /*********************************************************************
2042 * EDIT_MoveUp_ML
2044 * Only for multi line controls
2045 * Move the caret one line up, on a column with the nearest
2046 * x coordinate on the screen (might be a different column).
2049 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2051 INT s = es->selection_start;
2052 INT e = es->selection_end;
2053 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2054 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2055 INT x = (short)LOWORD(pos);
2056 INT y = (short)HIWORD(pos);
2058 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2059 if (!extend)
2060 s = e;
2061 EDIT_EM_SetSel(es, s, e, after_wrap);
2062 EDIT_EM_ScrollCaret(es);
2066 /*********************************************************************
2068 * EDIT_MoveWordBackward
2071 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2073 INT s = es->selection_start;
2074 INT e = es->selection_end;
2075 INT l;
2076 INT ll;
2077 INT li;
2079 l = EDIT_EM_LineFromChar(es, e);
2080 ll = EDIT_EM_LineLength(es, e);
2081 li = EDIT_EM_LineIndex(es, l);
2082 if (e - li == 0) {
2083 if (l) {
2084 li = EDIT_EM_LineIndex(es, l - 1);
2085 e = li + EDIT_EM_LineLength(es, li);
2087 } else {
2088 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2090 if (!extend)
2091 s = e;
2092 EDIT_EM_SetSel(es, s, e, FALSE);
2093 EDIT_EM_ScrollCaret(es);
2097 /*********************************************************************
2099 * EDIT_MoveWordForward
2102 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2104 INT s = es->selection_start;
2105 INT e = es->selection_end;
2106 INT l;
2107 INT ll;
2108 INT li;
2110 l = EDIT_EM_LineFromChar(es, e);
2111 ll = EDIT_EM_LineLength(es, e);
2112 li = EDIT_EM_LineIndex(es, l);
2113 if (e - li == ll) {
2114 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2115 e = EDIT_EM_LineIndex(es, l + 1);
2116 } else {
2117 e = li + EDIT_CallWordBreakProc(es,
2118 li, e - li + 1, ll, WB_RIGHT);
2120 if (!extend)
2121 s = e;
2122 EDIT_EM_SetSel(es, s, e, FALSE);
2123 EDIT_EM_ScrollCaret(es);
2127 /*********************************************************************
2129 * EDIT_PaintText
2132 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2134 COLORREF BkColor;
2135 COLORREF TextColor;
2136 LOGFONTW underline_font;
2137 HFONT hUnderline = 0;
2138 HFONT old_font = 0;
2139 INT ret;
2140 INT li;
2141 INT BkMode;
2142 SIZE size;
2144 if (!count)
2145 return 0;
2146 BkMode = GetBkMode(dc);
2147 BkColor = GetBkColor(dc);
2148 TextColor = GetTextColor(dc);
2149 if (rev) {
2150 if (es->composition_len == 0)
2152 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2153 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2154 SetBkMode( dc, OPAQUE);
2156 else
2158 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2159 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2160 underline_font.lfUnderline = TRUE;
2161 hUnderline = CreateFontIndirectW(&underline_font);
2162 old_font = SelectObject(dc,hUnderline);
2165 li = EDIT_EM_LineIndex(es, line);
2166 if (es->style & ES_MULTILINE) {
2167 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2168 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2169 } else {
2170 TextOutW(dc, x, y, es->text + li + col, count);
2171 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2172 ret = size.cx;
2174 if (rev) {
2175 if (es->composition_len == 0)
2177 SetBkColor(dc, BkColor);
2178 SetTextColor(dc, TextColor);
2179 SetBkMode( dc, BkMode);
2181 else
2183 if (old_font)
2184 SelectObject(dc,old_font);
2185 if (hUnderline)
2186 DeleteObject(hUnderline);
2189 return ret;
2193 /*********************************************************************
2195 * EDIT_PaintLine
2198 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2200 INT s = 0;
2201 INT e = 0;
2202 INT li = 0;
2203 INT ll = 0;
2204 INT x;
2205 INT y;
2206 LRESULT pos;
2207 SCRIPT_STRING_ANALYSIS ssa;
2209 if (es->style & ES_MULTILINE) {
2210 INT vlc = get_vertical_line_count(es);
2212 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2213 return;
2214 } else if (line)
2215 return;
2217 TRACE("line=%d\n", line);
2219 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2220 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2221 x = (short)LOWORD(pos);
2222 y = (short)HIWORD(pos);
2224 if (es->style & ES_MULTILINE)
2226 int line_idx = line;
2227 x = -es->x_offset;
2228 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2230 LINEDEF *line_def = es->first_line_def;
2231 int w, lw;
2233 while (line_def && line_idx)
2235 line_def = line_def->next;
2236 line_idx--;
2238 w = es->format_rect.right - es->format_rect.left;
2239 lw = line_def->width;
2241 if (es->style & ES_RIGHT)
2242 x = w - (lw - x);
2243 else if (es->style & ES_CENTER)
2244 x += (w - lw) / 2;
2246 x += es->format_rect.left;
2249 if (rev)
2251 li = EDIT_EM_LineIndex(es, line);
2252 ll = EDIT_EM_LineLength(es, li);
2253 s = min(es->selection_start, es->selection_end);
2254 e = max(es->selection_start, es->selection_end);
2255 s = min(li + ll, max(li, s));
2256 e = min(li + ll, max(li, e));
2259 if (ssa)
2260 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2261 else if (rev && (s != e) &&
2262 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2263 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2264 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2265 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2266 } else
2267 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2271 /*********************************************************************
2273 * EDIT_AdjustFormatRect
2275 * Adjusts the format rectangle for the current font and the
2276 * current client rectangle.
2279 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2281 RECT ClientRect;
2283 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2284 if (es->style & ES_MULTILINE)
2286 INT fw, vlc, max_x_offset, max_y_offset;
2288 vlc = get_vertical_line_count(es);
2289 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2291 /* correct es->x_offset */
2292 fw = es->format_rect.right - es->format_rect.left;
2293 max_x_offset = es->text_width - fw;
2294 if(max_x_offset < 0) max_x_offset = 0;
2295 if(es->x_offset > max_x_offset)
2296 es->x_offset = max_x_offset;
2298 /* correct es->y_offset */
2299 max_y_offset = es->line_count - vlc;
2300 if(max_y_offset < 0) max_y_offset = 0;
2301 if(es->y_offset > max_y_offset)
2302 es->y_offset = max_y_offset;
2304 /* force scroll info update */
2305 EDIT_UpdateScrollInfo(es);
2307 else
2308 /* Windows doesn't care to fix text placement for SL controls */
2309 es->format_rect.bottom = es->format_rect.top + es->line_height;
2311 /* Always stay within the client area */
2312 GetClientRect(es->hwndSelf, &ClientRect);
2313 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2315 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2316 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2318 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2322 /*********************************************************************
2324 * EDIT_SetRectNP
2326 * note: this is not (exactly) the handler called on EM_SETRECTNP
2327 * it is also used to set the rect of a single line control
2330 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2332 LONG_PTR ExStyle;
2333 INT bw, bh;
2334 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2336 CopyRect(&es->format_rect, rc);
2338 if (ExStyle & WS_EX_CLIENTEDGE) {
2339 es->format_rect.left++;
2340 es->format_rect.right--;
2342 if (es->format_rect.bottom - es->format_rect.top
2343 >= es->line_height + 2)
2345 es->format_rect.top++;
2346 es->format_rect.bottom--;
2349 else if (es->style & WS_BORDER) {
2350 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2351 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2352 InflateRect(&es->format_rect, -bw, 0);
2353 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2354 InflateRect(&es->format_rect, 0, -bh);
2357 es->format_rect.left += es->left_margin;
2358 es->format_rect.right -= es->right_margin;
2359 EDIT_AdjustFormatRect(es);
2363 /*********************************************************************
2365 * EM_CHARFROMPOS
2367 * returns line number (not index) in high-order word of result.
2368 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2369 * to Richedit, not to the edit control. Original documentation is valid.
2370 * FIXME: do the specs mean to return -1 if outside client area or
2371 * if outside formatting rectangle ???
2374 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2376 POINT pt;
2377 RECT rc;
2378 INT index;
2380 pt.x = x;
2381 pt.y = y;
2382 GetClientRect(es->hwndSelf, &rc);
2383 if (!PtInRect(&rc, pt))
2384 return -1;
2386 index = EDIT_CharFromPos(es, x, y, NULL);
2387 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2391 /*********************************************************************
2393 * EM_FMTLINES
2395 * Enable or disable soft breaks.
2397 * This means: insert or remove the soft linebreak character (\r\r\n).
2398 * Take care to check if the text still fits the buffer after insertion.
2399 * If not, notify with EN_ERRSPACE.
2402 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2404 es->flags &= ~EF_USE_SOFTBRK;
2405 if (add_eol) {
2406 es->flags |= EF_USE_SOFTBRK;
2407 FIXME("soft break enabled, not implemented\n");
2409 return add_eol;
2413 /*********************************************************************
2415 * EM_GETHANDLE
2417 * Hopefully this won't fire back at us.
2418 * We always start with a fixed buffer in the local heap.
2419 * Despite of the documentation says that the local heap is used
2420 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2421 * buffer on the local heap.
2424 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2426 HLOCAL hLocal;
2428 if (!(es->style & ES_MULTILINE))
2429 return 0;
2431 if(es->is_unicode)
2432 hLocal = es->hloc32W;
2433 else
2435 if(!es->hloc32A)
2437 CHAR *textA;
2438 UINT countA, alloc_size;
2439 TRACE("Allocating 32-bit ANSI alias buffer\n");
2440 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2441 alloc_size = ROUND_TO_GROW(countA);
2442 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2444 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2445 return 0;
2447 textA = LocalLock(es->hloc32A);
2448 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2449 LocalUnlock(es->hloc32A);
2451 hLocal = es->hloc32A;
2454 EDIT_UnlockBuffer(es, TRUE);
2456 /* The text buffer handle belongs to the app */
2457 es->hlocapp = hLocal;
2459 TRACE("Returning %p, LocalSize() = %Id\n", hLocal, LocalSize(hLocal));
2460 return hLocal;
2464 /*********************************************************************
2466 * EM_GETLINE
2469 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2471 LPWSTR src;
2472 INT line_len, dst_len;
2473 INT i;
2475 if (es->style & ES_MULTILINE) {
2476 if (line >= es->line_count)
2477 return 0;
2478 } else
2479 line = 0;
2480 i = EDIT_EM_LineIndex(es, line);
2481 src = es->text + i;
2482 line_len = EDIT_EM_LineLength(es, i);
2483 dst_len = *(WORD *)dst;
2484 if(unicode)
2486 if(dst_len <= line_len)
2488 memcpy(dst, src, dst_len * sizeof(WCHAR));
2489 return dst_len;
2491 else /* Append 0 if enough space */
2493 memcpy(dst, src, line_len * sizeof(WCHAR));
2494 dst[line_len] = 0;
2495 return line_len;
2498 else
2500 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2501 if(!ret && line_len) /* Insufficient buffer size */
2502 return dst_len;
2503 if(ret < dst_len) /* Append 0 if enough space */
2504 ((LPSTR)dst)[ret] = 0;
2505 return ret;
2510 /*********************************************************************
2512 * EM_GETSEL
2515 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2517 UINT s = es->selection_start;
2518 UINT e = es->selection_end;
2520 ORDER_UINT(s, e);
2521 if (start)
2522 *start = s;
2523 if (end)
2524 *end = e;
2525 return MAKELONG(s, e);
2529 /*********************************************************************
2531 * EM_REPLACESEL
2533 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2536 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2537 BOOL send_update, BOOL honor_limit)
2539 UINT tl = get_text_length(es);
2540 UINT utl;
2541 UINT s;
2542 UINT e;
2543 UINT i;
2544 UINT size;
2545 LPWSTR p;
2546 HRGN hrgn = 0;
2547 LPWSTR buf = NULL;
2548 UINT bufl;
2550 TRACE("%s, can_undo %d, send_update %d\n",
2551 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2553 s = es->selection_start;
2554 e = es->selection_end;
2556 EDIT_InvalidateUniscribeData(es);
2557 if ((s == e) && !strl)
2558 return;
2560 ORDER_UINT(s, e);
2562 size = tl - (e - s) + strl;
2563 if (!size)
2564 es->text_width = 0;
2566 /* Issue the EN_MAXTEXT notification and continue with replacing text
2567 * so that buffer limit is honored. */
2568 if ((honor_limit) && (size > es->buffer_limit))
2570 if (!notify_parent(es, EN_MAXTEXT)) return;
2571 /* Buffer limit can be smaller than the actual length of text in combobox */
2572 if (es->buffer_limit < (tl - (e-s)))
2573 strl = 0;
2574 else
2575 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2578 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2579 return;
2581 if (e != s) {
2582 /* there is something to be deleted */
2583 TRACE("deleting stuff.\n");
2584 bufl = e - s;
2585 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2586 if (!buf) return;
2587 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2588 buf[bufl] = 0; /* ensure 0 termination */
2589 /* now delete */
2590 lstrcpyW(es->text + s, es->text + e);
2591 text_buffer_changed(es);
2593 if (strl) {
2594 /* there is an insertion */
2595 tl = get_text_length(es);
2596 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));
2597 for (p = es->text + tl ; p >= es->text + s ; p--)
2598 p[strl] = p[0];
2599 for (i = 0 , p = es->text + s ; i < strl ; i++)
2600 p[i] = lpsz_replace[i];
2601 if(es->style & ES_UPPERCASE)
2602 CharUpperBuffW(p, strl);
2603 else if(es->style & ES_LOWERCASE)
2604 CharLowerBuffW(p, strl);
2605 text_buffer_changed(es);
2607 if (es->style & ES_MULTILINE)
2609 INT st = min(es->selection_start, es->selection_end);
2610 INT vlc = get_vertical_line_count(es);
2612 hrgn = CreateRectRgn(0, 0, 0, 0);
2613 EDIT_BuildLineDefs_ML(es, st, st + strl,
2614 strl - abs(es->selection_end - es->selection_start), hrgn);
2615 /* if text is too long undo all changes */
2616 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2617 if (strl)
2618 lstrcpyW(es->text + e, es->text + e + strl);
2619 if (e != s)
2620 for (i = 0 , p = es->text ; i < e - s ; i++)
2621 p[i + s] = buf[i];
2622 text_buffer_changed(es);
2623 EDIT_BuildLineDefs_ML(es, s, e,
2624 abs(es->selection_end - es->selection_start) - strl, hrgn);
2625 strl = 0;
2626 e = s;
2627 SetRectRgn(hrgn, 0, 0, 0, 0);
2628 if (!notify_parent(es, EN_MAXTEXT)) return;
2631 else {
2632 INT fw = es->format_rect.right - es->format_rect.left;
2633 EDIT_InvalidateUniscribeData(es);
2634 EDIT_CalcLineWidth_SL(es);
2635 /* remove chars that don't fit */
2636 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2637 while ((es->text_width > fw) && s + strl >= s) {
2638 lstrcpyW(es->text + s + strl - 1, es->text + s + strl);
2639 strl--;
2640 es->text_length = -1;
2641 EDIT_InvalidateUniscribeData(es);
2642 EDIT_CalcLineWidth_SL(es);
2644 text_buffer_changed(es);
2645 if (!notify_parent(es, EN_MAXTEXT)) return;
2649 if (e != s) {
2650 if (can_undo) {
2651 utl = lstrlenW(es->undo_text);
2652 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2653 /* undo-buffer is extended to the right */
2654 EDIT_MakeUndoFit(es, utl + e - s);
2655 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2656 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2657 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2658 /* undo-buffer is extended to the left */
2659 EDIT_MakeUndoFit(es, utl + e - s);
2660 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2661 p[e - s] = p[0];
2662 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2663 p[i] = buf[i];
2664 es->undo_position = s;
2665 } else {
2666 /* new undo-buffer */
2667 EDIT_MakeUndoFit(es, e - s);
2668 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2669 es->undo_text[e - s] = 0; /* ensure 0 termination */
2670 es->undo_position = s;
2672 /* any deletion makes the old insertion-undo invalid */
2673 es->undo_insert_count = 0;
2674 } else
2675 EDIT_EM_EmptyUndoBuffer(es);
2677 if (strl) {
2678 if (can_undo) {
2679 if ((s == es->undo_position) ||
2680 ((es->undo_insert_count) &&
2681 (s == es->undo_position + es->undo_insert_count)))
2683 * insertion is new and at delete position or
2684 * an extension to either left or right
2686 es->undo_insert_count += strl;
2687 else {
2688 /* new insertion undo */
2689 es->undo_position = s;
2690 es->undo_insert_count = strl;
2691 /* new insertion makes old delete-buffer invalid */
2692 *es->undo_text = '\0';
2694 } else
2695 EDIT_EM_EmptyUndoBuffer(es);
2698 HeapFree(GetProcessHeap(), 0, buf);
2700 s += strl;
2702 /* If text has been deleted and we're right or center aligned then scroll rightward */
2703 if (es->style & (ES_RIGHT | ES_CENTER))
2705 INT delta = strl - abs(es->selection_end - es->selection_start);
2707 if (delta < 0 && es->x_offset)
2709 if (abs(delta) > es->x_offset)
2710 es->x_offset = 0;
2711 else
2712 es->x_offset += delta;
2716 EDIT_EM_SetSel(es, s, s, FALSE);
2717 es->flags |= EF_MODIFIED;
2718 if (send_update) es->flags |= EF_UPDATE;
2719 if (hrgn)
2721 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2722 DeleteObject(hrgn);
2724 else
2725 EDIT_UpdateText(es, NULL, TRUE);
2727 EDIT_EM_ScrollCaret(es);
2729 /* force scroll info update */
2730 EDIT_UpdateScrollInfo(es);
2733 if(send_update || (es->flags & EF_UPDATE))
2735 es->flags &= ~EF_UPDATE;
2736 if (!notify_parent(es, EN_CHANGE)) return;
2738 EDIT_InvalidateUniscribeData(es);
2742 /*********************************************************************
2744 * EM_SETHANDLE
2746 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2749 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2751 if (!(es->style & ES_MULTILINE))
2752 return;
2754 if (!hloc) {
2755 WARN("called with NULL handle\n");
2756 return;
2759 EDIT_UnlockBuffer(es, TRUE);
2761 if(es->is_unicode)
2763 if(es->hloc32A)
2765 LocalFree(es->hloc32A);
2766 es->hloc32A = NULL;
2768 es->hloc32W = hloc;
2770 else
2772 INT countW, countA;
2773 HLOCAL hloc32W_new;
2774 WCHAR *textW;
2775 CHAR *textA;
2777 countA = LocalSize(hloc);
2778 textA = LocalLock(hloc);
2779 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2780 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2782 ERR("Could not allocate new unicode buffer\n");
2783 return;
2785 textW = LocalLock(hloc32W_new);
2786 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2787 LocalUnlock(hloc32W_new);
2788 LocalUnlock(hloc);
2790 if(es->hloc32W)
2791 LocalFree(es->hloc32W);
2793 es->hloc32W = hloc32W_new;
2794 es->hloc32A = hloc;
2797 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2799 /* The text buffer handle belongs to the control */
2800 es->hlocapp = NULL;
2802 EDIT_LockBuffer(es);
2803 text_buffer_changed(es);
2805 es->x_offset = es->y_offset = 0;
2806 es->selection_start = es->selection_end = 0;
2807 EDIT_EM_EmptyUndoBuffer(es);
2808 es->flags &= ~EF_MODIFIED;
2809 es->flags &= ~EF_UPDATE;
2810 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2811 EDIT_UpdateText(es, NULL, TRUE);
2812 EDIT_EM_ScrollCaret(es);
2813 /* force scroll info update */
2814 EDIT_UpdateScrollInfo(es);
2818 /*********************************************************************
2820 * EM_SETLIMITTEXT
2822 * NOTE: this version currently implements WinNT limits
2825 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2827 if (!limit) limit = ~0u;
2828 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2829 es->buffer_limit = limit;
2832 static BOOL is_cjk_charset(HDC dc)
2834 switch (GdiGetCodePage(dc)) {
2835 case 932: case 936: case 949: case 950: case 1361:
2836 return TRUE;
2837 default:
2838 return FALSE;
2842 static BOOL is_cjk_font(HDC dc)
2844 const DWORD FS_DBCS_MASK = FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB;
2845 FONTSIGNATURE fs;
2846 return (GetTextCharsetInfo(dc, &fs, 0) != DEFAULT_CHARSET &&
2847 (fs.fsCsb[0] & FS_DBCS_MASK));
2850 static int get_cjk_fontinfo_margin(int width, int side_bearing)
2852 int margin;
2853 if (side_bearing < 0)
2854 margin = min(-side_bearing, width/2);
2855 else
2856 margin = 0;
2857 return margin;
2860 struct char_width_info {
2861 INT min_lsb, min_rsb, unknown;
2864 /* Undocumented gdi32 export */
2865 extern BOOL WINAPI GetCharWidthInfo(HDC, struct char_width_info *);
2867 /*********************************************************************
2869 * EM_SETMARGINS
2871 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2872 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2873 * margin according to the textmetrics of the current font.
2875 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2876 * equal to or larger than a certain size. The empty client rect is treated as
2877 * 80 pixels width.
2879 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2880 WORD left, WORD right, BOOL repaint)
2882 TEXTMETRICW tm;
2883 INT default_left_margin = 0; /* in pixels */
2884 INT default_right_margin = 0; /* in pixels */
2886 /* Set the default margins depending on the font */
2887 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2888 HDC dc = NtUserGetDC(es->hwndSelf);
2889 HFONT old_font = SelectObject(dc, es->font);
2890 LONG width = GdiGetCharDimensions(dc, &tm, NULL), rc_width;
2891 RECT rc;
2893 /* The default margins are only non zero for TrueType or Vector fonts */
2894 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2895 struct char_width_info width_info;
2897 if ((is_cjk_charset(dc) || is_cjk_font(dc)) &&
2898 GetCharWidthInfo(dc, &width_info))
2900 default_left_margin = get_cjk_fontinfo_margin(width, width_info.min_lsb);
2901 default_right_margin = get_cjk_fontinfo_margin(width, width_info.min_rsb);
2903 else
2905 default_left_margin = width / 2;
2906 default_right_margin = width / 2;
2909 GetClientRect(es->hwndSelf, &rc);
2910 rc_width = !IsRectEmpty(&rc) ? rc.right - rc.left : 80;
2911 if (rc_width < default_left_margin + default_right_margin + width * 2) {
2912 default_left_margin = es->left_margin;
2913 default_right_margin = es->right_margin;
2916 SelectObject(dc, old_font);
2917 NtUserReleaseDC( es->hwndSelf, dc );
2920 if (action & EC_LEFTMARGIN) {
2921 es->format_rect.left -= es->left_margin;
2922 if (left != EC_USEFONTINFO)
2923 es->left_margin = left;
2924 else
2925 es->left_margin = default_left_margin;
2926 es->format_rect.left += es->left_margin;
2929 if (action & EC_RIGHTMARGIN) {
2930 es->format_rect.right += es->right_margin;
2931 if (right != EC_USEFONTINFO)
2932 es->right_margin = right;
2933 else
2934 es->right_margin = default_right_margin;
2935 es->format_rect.right -= es->right_margin;
2938 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2939 EDIT_AdjustFormatRect(es);
2940 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2943 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2947 /*********************************************************************
2949 * EM_SETPASSWORDCHAR
2952 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2954 LONG style;
2956 if (es->style & ES_MULTILINE)
2957 return;
2959 if (es->password_char == c)
2960 return;
2962 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2963 es->password_char = c;
2964 if (c) {
2965 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2966 es->style |= ES_PASSWORD;
2967 } else {
2968 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2969 es->style &= ~ES_PASSWORD;
2971 EDIT_InvalidateUniscribeData(es);
2972 EDIT_UpdateText(es, NULL, TRUE);
2976 /*********************************************************************
2978 * EM_SETTABSTOPS
2981 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2983 if (!(es->style & ES_MULTILINE))
2984 return FALSE;
2985 HeapFree(GetProcessHeap(), 0, es->tabs);
2986 es->tabs_count = count;
2987 if (!count)
2988 es->tabs = NULL;
2989 else {
2990 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2991 memcpy(es->tabs, tabs, count * sizeof(INT));
2993 EDIT_InvalidateUniscribeData(es);
2994 return TRUE;
2998 /*********************************************************************
3000 * EM_SETWORDBREAKPROC
3003 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3005 if (es->word_break_proc == wbp)
3006 return;
3008 es->word_break_proc = wbp;
3010 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3011 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3012 EDIT_UpdateText(es, NULL, TRUE);
3017 /*********************************************************************
3019 * EM_UNDO / WM_UNDO
3022 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3024 INT ulength;
3025 LPWSTR utext;
3027 /* As per MSDN spec, for a single-line edit control,
3028 the return value is always TRUE */
3029 if( es->style & ES_READONLY )
3030 return !(es->style & ES_MULTILINE);
3032 ulength = lstrlenW(es->undo_text);
3034 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3036 lstrcpyW(utext, es->undo_text);
3038 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3039 es->undo_insert_count, debugstr_w(utext));
3041 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3042 EDIT_EM_EmptyUndoBuffer(es);
3043 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
3044 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3045 /* send the notification after the selection start and end are set */
3046 if (!notify_parent(es, EN_CHANGE)) return TRUE;
3047 EDIT_EM_ScrollCaret(es);
3048 HeapFree(GetProcessHeap(), 0, utext);
3050 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3051 es->undo_insert_count, debugstr_w(es->undo_text));
3052 return TRUE;
3056 /* Helper function for WM_CHAR
3058 * According to an MSDN blog article titled "Just because you're a control
3059 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3060 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3061 * a dialog box or not.
3063 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3065 return (es->flags & EF_DIALOGMODE);
3069 /*********************************************************************
3071 * WM_PASTE
3074 static void EDIT_WM_Paste(EDITSTATE *es)
3076 HGLOBAL hsrc;
3077 LPWSTR src, ptr;
3078 int len;
3080 /* Protect read-only edit control from modification */
3081 if(es->style & ES_READONLY)
3082 return;
3084 OpenClipboard(es->hwndSelf);
3085 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3086 src = GlobalLock(hsrc);
3087 len = lstrlenW(src);
3088 /* Protect single-line edit against pasting new line character */
3089 if (!(es->style & ES_MULTILINE) && ((ptr = wcschr(src, '\n')))) {
3090 len = ptr - src;
3091 if (len && src[len - 1] == '\r')
3092 --len;
3094 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3095 GlobalUnlock(hsrc);
3097 else if (es->style & ES_PASSWORD) {
3098 /* clear selected text in password edit box even with empty clipboard */
3099 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3101 NtUserCloseClipboard();
3105 /*********************************************************************
3107 * WM_COPY
3110 static void EDIT_WM_Copy(EDITSTATE *es)
3112 INT s = min(es->selection_start, es->selection_end);
3113 INT e = max(es->selection_start, es->selection_end);
3114 HGLOBAL hdst;
3115 LPWSTR dst;
3116 DWORD len;
3118 if (e == s) return;
3120 len = e - s;
3121 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3122 dst = GlobalLock(hdst);
3123 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3124 dst[len] = 0; /* ensure 0 termination */
3125 TRACE("%s\n", debugstr_w(dst));
3126 GlobalUnlock(hdst);
3127 OpenClipboard(es->hwndSelf);
3128 NtUserEmptyClipboard();
3129 SetClipboardData(CF_UNICODETEXT, hdst);
3130 NtUserCloseClipboard();
3134 /*********************************************************************
3136 * WM_CLEAR
3139 static inline void EDIT_WM_Clear(EDITSTATE *es)
3141 /* Protect read-only edit control from modification */
3142 if(es->style & ES_READONLY)
3143 return;
3145 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3149 /*********************************************************************
3151 * WM_CUT
3154 static inline void EDIT_WM_Cut(EDITSTATE *es)
3156 EDIT_WM_Copy(es);
3157 EDIT_WM_Clear(es);
3161 /*********************************************************************
3163 * WM_CHAR
3166 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3168 BOOL control;
3170 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3172 switch (c) {
3173 case '\r':
3174 /* If it's not a multiline edit box, it would be ignored below.
3175 * For multiline edit without ES_WANTRETURN, we have to make a
3176 * special case.
3178 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3179 if (EDIT_IsInsideDialog(es))
3180 break;
3181 case '\n':
3182 if (es->style & ES_MULTILINE) {
3183 if (es->style & ES_READONLY) {
3184 EDIT_MoveHome(es, FALSE, FALSE);
3185 EDIT_MoveDown_ML(es, FALSE);
3186 } else {
3187 EDIT_EM_ReplaceSel(es, TRUE, L"\r\n", 2, TRUE, TRUE);
3190 break;
3191 case '\t':
3192 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3194 if (EDIT_IsInsideDialog(es))
3195 break;
3196 EDIT_EM_ReplaceSel(es, TRUE, L"\t", 1, TRUE, TRUE);
3198 break;
3199 case VK_BACK:
3200 if (!(es->style & ES_READONLY) && !control) {
3201 if (es->selection_start != es->selection_end)
3202 EDIT_WM_Clear(es);
3203 else {
3204 /* delete character left of caret */
3205 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3206 EDIT_MoveBackward(es, TRUE);
3207 EDIT_WM_Clear(es);
3210 break;
3211 case 0x03: /* ^C */
3212 if (!(es->style & ES_PASSWORD))
3213 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3214 break;
3215 case 0x16: /* ^V */
3216 if (!(es->style & ES_READONLY))
3217 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3218 break;
3219 case 0x18: /* ^X */
3220 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3221 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3222 break;
3223 case 0x1A: /* ^Z */
3224 if (!(es->style & ES_READONLY))
3225 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3226 break;
3228 default:
3229 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3230 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3231 break;
3233 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3234 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3235 break;
3237 return 1;
3241 /*********************************************************************
3243 * EDIT_ContextMenuCommand
3246 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3248 switch (id) {
3249 case EM_UNDO:
3250 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3251 break;
3252 case WM_CUT:
3253 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3254 break;
3255 case WM_COPY:
3256 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3257 break;
3258 case WM_PASTE:
3259 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3260 break;
3261 case WM_CLEAR:
3262 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3263 break;
3264 case EM_SETSEL:
3265 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3266 break;
3267 default:
3268 ERR("unknown menu item, please report\n");
3269 break;
3274 /*********************************************************************
3276 * WM_CONTEXTMENU
3278 * Note: the resource files resource/sysres_??.rc cannot define a
3279 * single popup menu. Hence we use a (dummy) menubar
3280 * containing the single popup menu as its first item.
3282 * FIXME: the message identifiers have been chosen arbitrarily,
3283 * hence we use MF_BYPOSITION.
3284 * We might as well use the "real" values (anybody knows ?)
3285 * The menu definition is in resources/sysres_??.rc.
3286 * Once these are OK, we better use MF_BYCOMMAND here
3287 * (as we do in EDIT_WM_Command()).
3290 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3292 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3293 HMENU popup = GetSubMenu(menu, 0);
3294 UINT start = es->selection_start;
3295 UINT end = es->selection_end;
3296 BOOL enabled;
3297 UINT cmd;
3299 ORDER_UINT(start, end);
3301 /* undo */
3302 enabled = EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY);
3303 NtUserEnableMenuItem( popup, 0, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3304 /* cut */
3305 enabled = (end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY);
3306 NtUserEnableMenuItem( popup, 2, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3307 /* copy */
3308 enabled = (end - start) && !(es->style & ES_PASSWORD);
3309 NtUserEnableMenuItem( popup, 3, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3310 /* paste */
3311 enabled = NtUserIsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY);
3312 NtUserEnableMenuItem( popup, 4, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3313 /* delete */
3314 enabled = (end - start) && !(es->style & ES_READONLY);
3315 NtUserEnableMenuItem( popup, 5, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3316 /* select all */
3317 enabled = start || end != get_text_length(es);
3318 NtUserEnableMenuItem( popup, 7, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3320 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3322 POINT pt;
3323 RECT rc;
3325 /* Windows places the menu at the edit's center in this case */
3326 GetClientRect( es->hwndSelf, &rc );
3327 pt.x = rc.right / 2;
3328 pt.y = rc.bottom / 2;
3329 ClientToScreen( es->hwndSelf, &pt );
3330 x = pt.x;
3331 y = pt.y;
3334 if (!(es->flags & EF_FOCUSED))
3335 NtUserSetFocus(es->hwndSelf);
3337 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3338 x, y, 0, es->hwndSelf, NULL);
3340 if (cmd)
3341 EDIT_ContextMenuCommand(es, cmd);
3343 NtUserDestroyMenu(menu);
3347 /*********************************************************************
3349 * WM_GETTEXT
3352 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3354 if(!count) return 0;
3356 if(unicode)
3358 lstrcpynW(dst, es->text, count);
3359 return lstrlenW(dst);
3361 else
3363 LPSTR textA = (LPSTR)dst;
3364 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3365 textA[count - 1] = 0; /* ensure 0 termination */
3366 return strlen(textA);
3370 /*********************************************************************
3372 * EDIT_CheckCombo
3375 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3377 HWND hLBox = es->hwndListBox;
3378 HWND hCombo;
3379 BOOL bDropped;
3380 int nEUI;
3382 if (!hLBox)
3383 return FALSE;
3385 hCombo = GetParent(es->hwndSelf);
3386 bDropped = TRUE;
3387 nEUI = 0;
3389 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3391 if (key == VK_UP || key == VK_DOWN)
3393 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3394 nEUI = 1;
3396 if (msg == WM_KEYDOWN || nEUI)
3397 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3400 switch (msg)
3402 case WM_KEYDOWN:
3403 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3405 /* make sure ComboLBox pops up */
3406 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3407 key = VK_F4;
3408 nEUI = 2;
3411 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3412 break;
3414 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3415 if (nEUI)
3416 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3417 else
3418 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3419 break;
3422 if(nEUI == 2)
3423 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3425 return TRUE;
3429 /*********************************************************************
3431 * WM_KEYDOWN
3433 * Handling of special keys that don't produce a WM_CHAR
3434 * (i.e. non-printable keys) & Backspace & Delete
3437 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3439 BOOL shift;
3440 BOOL control;
3442 if (NtUserGetKeyState(VK_MENU) & 0x8000)
3443 return 0;
3445 shift = NtUserGetKeyState(VK_SHIFT) & 0x8000;
3446 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3448 switch (key) {
3449 case VK_F4:
3450 case VK_UP:
3451 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3452 break;
3454 /* fall through */
3455 case VK_LEFT:
3456 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3457 EDIT_MoveUp_ML(es, shift);
3458 else
3459 if (control)
3460 EDIT_MoveWordBackward(es, shift);
3461 else
3462 EDIT_MoveBackward(es, shift);
3463 break;
3464 case VK_DOWN:
3465 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3466 break;
3467 /* fall through */
3468 case VK_RIGHT:
3469 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3470 EDIT_MoveDown_ML(es, shift);
3471 else if (control)
3472 EDIT_MoveWordForward(es, shift);
3473 else
3474 EDIT_MoveForward(es, shift);
3475 break;
3476 case VK_HOME:
3477 EDIT_MoveHome(es, shift, control);
3478 break;
3479 case VK_END:
3480 EDIT_MoveEnd(es, shift, control);
3481 break;
3482 case VK_PRIOR:
3483 if (es->style & ES_MULTILINE)
3484 EDIT_MovePageUp_ML(es, shift);
3485 else
3486 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3487 break;
3488 case VK_NEXT:
3489 if (es->style & ES_MULTILINE)
3490 EDIT_MovePageDown_ML(es, shift);
3491 else
3492 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3493 break;
3494 case VK_DELETE:
3495 if (!(es->style & ES_READONLY) && !(shift && control)) {
3496 if (es->selection_start != es->selection_end) {
3497 if (shift)
3498 EDIT_WM_Cut(es);
3499 else
3500 EDIT_WM_Clear(es);
3501 } else {
3502 EDIT_EM_SetSel(es, ~0u, 0, FALSE);
3503 if (shift)
3504 /* delete character left of caret */
3505 EDIT_MoveBackward(es, TRUE);
3506 else if (control)
3507 /* delete to end of line */
3508 EDIT_MoveEnd(es, TRUE, FALSE);
3509 else
3510 /* delete character right of caret */
3511 EDIT_MoveForward(es, TRUE);
3512 EDIT_WM_Clear(es);
3515 break;
3516 case VK_INSERT:
3517 if (shift) {
3518 if (!(es->style & ES_READONLY))
3519 EDIT_WM_Paste(es);
3520 } else if (control)
3521 EDIT_WM_Copy(es);
3522 break;
3523 case VK_RETURN:
3524 /* If the edit doesn't want the return send a message to the default object */
3525 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3527 DWORD dw;
3529 if (!EDIT_IsInsideDialog(es)) break;
3530 if (control) break;
3531 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3532 if (HIWORD(dw) == DC_HASDEFID)
3534 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3535 if (hwDefCtrl)
3537 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3538 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3542 break;
3543 case VK_ESCAPE:
3544 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3545 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3546 break;
3547 case VK_TAB:
3548 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3549 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3550 break;
3552 return TRUE;
3556 /*********************************************************************
3558 * WM_KILLFOCUS
3561 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3563 es->flags &= ~EF_FOCUSED;
3564 DestroyCaret();
3565 if(!(es->style & ES_NOHIDESEL))
3566 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3567 if (!notify_parent(es, EN_KILLFOCUS)) return 0;
3568 /* throw away left over scroll when we lose focus */
3569 es->wheelDeltaRemainder = 0;
3570 return 0;
3574 /*********************************************************************
3576 * WM_LBUTTONDBLCLK
3578 * The caret position has been set on the WM_LBUTTONDOWN message
3581 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3583 INT s;
3584 INT e = es->selection_end;
3585 INT l;
3586 INT li;
3587 INT ll;
3589 es->bCaptureState = TRUE;
3590 NtUserSetCapture(es->hwndSelf);
3592 l = EDIT_EM_LineFromChar(es, e);
3593 li = EDIT_EM_LineIndex(es, l);
3594 ll = EDIT_EM_LineLength(es, e);
3595 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3596 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3597 EDIT_EM_SetSel(es, s, e, FALSE);
3598 EDIT_EM_ScrollCaret(es);
3599 es->region_posx = es->region_posy = 0;
3600 SetTimer(es->hwndSelf, 0, 100, NULL);
3601 return 0;
3605 /*********************************************************************
3607 * WM_LBUTTONDOWN
3610 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3612 INT e;
3613 BOOL after_wrap;
3615 es->bCaptureState = TRUE;
3616 NtUserSetCapture(es->hwndSelf);
3617 EDIT_ConfinePoint(es, &x, &y);
3618 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3619 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3620 EDIT_EM_ScrollCaret(es);
3621 es->region_posx = es->region_posy = 0;
3622 SetTimer(es->hwndSelf, 0, 100, NULL);
3624 if (!(es->flags & EF_FOCUSED))
3625 NtUserSetFocus(es->hwndSelf);
3627 return 0;
3631 /*********************************************************************
3633 * WM_LBUTTONUP
3636 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3638 if (es->bCaptureState) {
3639 NtUserKillTimer(es->hwndSelf, 0);
3640 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3642 es->bCaptureState = FALSE;
3643 return 0;
3647 /*********************************************************************
3649 * WM_MBUTTONDOWN
3652 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3654 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3655 return 0;
3659 /*********************************************************************
3661 * WM_MOUSEMOVE
3664 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3666 INT e;
3667 BOOL after_wrap;
3668 INT prex, prey;
3670 /* If the mouse has been captured by process other than the edit control itself,
3671 * the windows edit controls will not select the strings with mouse move.
3673 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3674 return 0;
3677 * FIXME: gotta do some scrolling if outside client
3678 * area. Maybe reset the timer ?
3680 prex = x; prey = y;
3681 EDIT_ConfinePoint(es, &x, &y);
3682 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3683 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3684 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3685 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3686 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3687 return 0;
3691 /*********************************************************************
3693 * WM_PAINT
3696 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3698 PAINTSTRUCT ps;
3699 INT i;
3700 HDC dc;
3701 HFONT old_font = 0;
3702 RECT rc;
3703 RECT rcClient;
3704 RECT rcLine;
3705 RECT rcRgn;
3706 HBRUSH brush;
3707 HBRUSH old_brush;
3708 INT bw, bh;
3709 BOOL rev = es->bEnableState &&
3710 ((es->flags & EF_FOCUSED) ||
3711 (es->style & ES_NOHIDESEL));
3712 dc = hdc ? hdc : NtUserBeginPaint( es->hwndSelf, &ps );
3714 /* The dc we use for calculating may not be the one we paint into.
3715 This is the safest action. */
3716 EDIT_InvalidateUniscribeData(es);
3717 GetClientRect(es->hwndSelf, &rcClient);
3719 /* get the background brush */
3720 brush = EDIT_NotifyCtlColor(es, dc);
3722 /* paint the border and the background */
3723 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3725 if(es->style & WS_BORDER) {
3726 bw = GetSystemMetrics(SM_CXBORDER);
3727 bh = GetSystemMetrics(SM_CYBORDER);
3728 rc = rcClient;
3729 if(es->style & ES_MULTILINE) {
3730 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3731 if(es->style & WS_VSCROLL) rc.right+=bw;
3734 /* Draw the frame. Same code as in nonclient.c */
3735 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3736 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3737 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3738 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3739 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3740 SelectObject(dc, old_brush);
3742 /* Keep the border clean */
3743 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3744 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3747 GetClipBox(dc, &rc);
3748 FillRect(dc, &rc, brush);
3750 IntersectClipRect(dc, es->format_rect.left,
3751 es->format_rect.top,
3752 es->format_rect.right,
3753 es->format_rect.bottom);
3754 if (es->style & ES_MULTILINE) {
3755 rc = rcClient;
3756 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3758 if (es->font)
3759 old_font = SelectObject(dc, es->font);
3761 if (!es->bEnableState)
3762 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3763 GetClipBox(dc, &rcRgn);
3764 if (es->style & ES_MULTILINE) {
3765 INT vlc = get_vertical_line_count(es);
3766 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3767 EDIT_UpdateUniscribeData(es, dc, i);
3768 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3769 if (IntersectRect(&rc, &rcRgn, &rcLine))
3770 EDIT_PaintLine(es, dc, i, rev);
3772 } else {
3773 EDIT_UpdateUniscribeData(es, dc, 0);
3774 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3775 if (IntersectRect(&rc, &rcRgn, &rcLine))
3776 EDIT_PaintLine(es, dc, 0, rev);
3778 if (es->font)
3779 SelectObject(dc, old_font);
3781 if (!hdc)
3782 NtUserEndPaint( es->hwndSelf, &ps );
3786 /*********************************************************************
3788 * WM_SETFOCUS
3791 static void EDIT_WM_SetFocus(EDITSTATE *es)
3793 es->flags |= EF_FOCUSED;
3795 if (!(es->style & ES_NOHIDESEL))
3796 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3798 /* single line edit updates itself */
3799 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3801 HDC hdc = NtUserGetDC(es->hwndSelf);
3802 EDIT_WM_Paint(es, hdc);
3803 NtUserReleaseDC( es->hwndSelf, hdc );
3806 NtUserCreateCaret( es->hwndSelf, 0, 1, es->line_height );
3807 EDIT_SetCaretPos(es, es->selection_end,
3808 es->flags & EF_AFTER_WRAP);
3809 NtUserShowCaret( es->hwndSelf );
3810 notify_parent(es, EN_SETFOCUS);
3813 static DWORD get_font_margins(HDC hdc, const TEXTMETRICW *tm, BOOL unicode)
3815 ABC abc[256];
3816 SHORT left, right;
3817 UINT i;
3819 if (!(tm->tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE)))
3820 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3822 if (unicode) {
3823 if (!is_cjk_charset(hdc) && !is_cjk_font(hdc))
3824 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3825 if (!GetCharABCWidthsW(hdc, 0, 255, abc))
3826 return 0;
3828 else if (is_cjk_charset(hdc)) {
3829 if (!GetCharABCWidthsA(hdc, 0, 255, abc))
3830 return 0;
3832 else
3833 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3835 left = right = 0;
3836 for (i = 0; i < ARRAY_SIZE(abc); i++) {
3837 if (-abc[i].abcA > right) right = -abc[i].abcA;
3838 if (-abc[i].abcC > left ) left = -abc[i].abcC;
3840 return MAKELONG(left, right);
3843 /*********************************************************************
3845 * WM_SETFONT
3847 * With Win95 look the margins are set to default font value unless
3848 * the system font (font == 0) is being set, in which case they are left
3849 * unchanged.
3852 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3854 TEXTMETRICW tm;
3855 HDC dc;
3856 HFONT old_font = 0;
3857 RECT clientRect;
3858 DWORD margins;
3860 es->font = font;
3861 EDIT_InvalidateUniscribeData(es);
3862 dc = NtUserGetDC(es->hwndSelf);
3863 if (font)
3864 old_font = SelectObject(dc, font);
3865 GetTextMetricsW(dc, &tm);
3866 es->line_height = tm.tmHeight;
3867 es->char_width = tm.tmAveCharWidth;
3868 margins = get_font_margins(dc, &tm, es->is_unicode);
3869 if (font)
3870 SelectObject(dc, old_font);
3871 NtUserReleaseDC( es->hwndSelf, dc );
3873 /* Reset the format rect and the margins */
3874 GetClientRect(es->hwndSelf, &clientRect);
3875 EDIT_SetRectNP(es, &clientRect);
3876 if (margins)
3877 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3878 LOWORD(margins), HIWORD(margins), FALSE);
3880 if (es->style & ES_MULTILINE)
3881 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3882 else
3883 EDIT_CalcLineWidth_SL(es);
3885 if (redraw)
3886 EDIT_UpdateText(es, NULL, TRUE);
3887 if (es->flags & EF_FOCUSED) {
3888 DestroyCaret();
3889 NtUserCreateCaret( es->hwndSelf, 0, 1, es->line_height );
3890 EDIT_SetCaretPos(es, es->selection_end,
3891 es->flags & EF_AFTER_WRAP);
3892 NtUserShowCaret( es->hwndSelf );
3897 /*********************************************************************
3899 * WM_SETTEXT
3901 * NOTES
3902 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3903 * The modified flag is reset. No notifications are sent.
3905 * For single-line controls, reception of WM_SETTEXT triggers:
3906 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3909 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3911 LPWSTR textW = NULL;
3912 if (!unicode && text)
3914 LPCSTR textA = (LPCSTR)text;
3915 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3916 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3917 if (textW)
3918 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3919 text = textW;
3922 if (es->flags & EF_UPDATE)
3923 /* fixed this bug once; complain if we see it about to happen again. */
3924 ERR("SetSel may generate UPDATE message whose handler may reset "
3925 "selection.\n");
3927 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3928 if (text)
3930 TRACE("%s\n", debugstr_w(text));
3931 EDIT_EM_ReplaceSel(es, FALSE, text, lstrlenW(text), FALSE, FALSE);
3932 if(!unicode)
3933 HeapFree(GetProcessHeap(), 0, textW);
3935 else
3937 TRACE("<NULL>\n");
3938 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3940 es->x_offset = 0;
3941 es->flags &= ~EF_MODIFIED;
3942 EDIT_EM_SetSel(es, 0, 0, FALSE);
3944 /* Send the notification after the selection start and end have been set
3945 * edit control doesn't send notification on WM_SETTEXT
3946 * if it is multiline, or it is part of combobox
3948 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3950 if (!notify_parent(es, EN_UPDATE)) return;
3951 if (!notify_parent(es, EN_CHANGE)) return;
3953 EDIT_EM_ScrollCaret(es);
3954 EDIT_UpdateScrollInfo(es);
3955 EDIT_InvalidateUniscribeData(es);
3959 /*********************************************************************
3961 * WM_SIZE
3964 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3966 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3967 RECT rc;
3968 GetClientRect(es->hwndSelf, &rc);
3969 EDIT_SetRectNP(es, &rc);
3970 EDIT_UpdateText(es, NULL, TRUE);
3975 /*********************************************************************
3977 * WM_STYLECHANGED
3979 * This message is sent by SetWindowLong on having changed either the Style
3980 * or the extended style.
3982 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3984 * See also EDIT_WM_NCCreate
3986 * It appears that the Windows version of the edit control allows the style
3987 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3988 * style variable which will generally be different. In this function we
3989 * update the internal style based on what changed in the externally visible
3990 * style.
3992 * Much of this content as based upon the MSDN, especially:
3993 * Platform SDK Documentation -> User Interface Services ->
3994 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3995 * Edit Control Styles
3997 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3999 if (GWL_STYLE == which) {
4000 DWORD style_change_mask;
4001 DWORD new_style;
4002 /* Only a subset of changes can be applied after the control
4003 * has been created.
4005 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4006 ES_NUMBER;
4007 if (es->style & ES_MULTILINE)
4008 style_change_mask |= ES_WANTRETURN;
4010 new_style = style->styleNew & style_change_mask;
4012 /* Number overrides lowercase overrides uppercase (at least it
4013 * does in Win95). However I'll bet that ES_NUMBER would be
4014 * invalid under Win 3.1.
4016 if (new_style & ES_NUMBER) {
4017 ; /* do not override the ES_NUMBER */
4018 } else if (new_style & ES_LOWERCASE) {
4019 new_style &= ~ES_UPPERCASE;
4022 es->style = (es->style & ~style_change_mask) | new_style;
4023 } else if (GWL_EXSTYLE == which) {
4024 ; /* FIXME - what is needed here */
4025 } else {
4026 WARN ("Invalid style change %Id\n",which);
4029 return 0;
4032 /*********************************************************************
4034 * WM_SYSKEYDOWN
4037 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4039 if ((key == VK_BACK) && (key_data & 0x2000)) {
4040 if (EDIT_EM_CanUndo(es))
4041 EDIT_EM_Undo(es);
4042 return 0;
4043 } else if (key == VK_UP || key == VK_DOWN) {
4044 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4045 return 0;
4047 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4051 /*********************************************************************
4053 * WM_TIMER
4056 static void EDIT_WM_Timer(EDITSTATE *es)
4058 if (es->region_posx < 0) {
4059 EDIT_MoveBackward(es, TRUE);
4060 } else if (es->region_posx > 0) {
4061 EDIT_MoveForward(es, TRUE);
4064 * FIXME: gotta do some vertical scrolling here, like
4065 * EDIT_EM_LineScroll(hwnd, 0, 1);
4069 /*********************************************************************
4071 * WM_HSCROLL
4074 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4076 INT dx;
4077 INT fw;
4079 if (!(es->style & ES_MULTILINE))
4080 return 0;
4082 if (!(es->style & ES_AUTOHSCROLL))
4083 return 0;
4085 dx = 0;
4086 fw = es->format_rect.right - es->format_rect.left;
4087 switch (action) {
4088 case SB_LINELEFT:
4089 TRACE("SB_LINELEFT\n");
4090 if (es->x_offset)
4091 dx = -es->char_width;
4092 break;
4093 case SB_LINERIGHT:
4094 TRACE("SB_LINERIGHT\n");
4095 if (es->x_offset < es->text_width)
4096 dx = es->char_width;
4097 break;
4098 case SB_PAGELEFT:
4099 TRACE("SB_PAGELEFT\n");
4100 if (es->x_offset)
4101 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4102 break;
4103 case SB_PAGERIGHT:
4104 TRACE("SB_PAGERIGHT\n");
4105 if (es->x_offset < es->text_width)
4106 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4107 break;
4108 case SB_LEFT:
4109 TRACE("SB_LEFT\n");
4110 if (es->x_offset)
4111 dx = -es->x_offset;
4112 break;
4113 case SB_RIGHT:
4114 TRACE("SB_RIGHT\n");
4115 if (es->x_offset < es->text_width)
4116 dx = es->text_width - es->x_offset;
4117 break;
4118 case SB_THUMBTRACK:
4119 TRACE("SB_THUMBTRACK %d\n", pos);
4120 es->flags |= EF_HSCROLL_TRACK;
4121 if(es->style & WS_HSCROLL)
4122 dx = pos - es->x_offset;
4123 else
4125 INT fw, new_x;
4126 /* Sanity check */
4127 if(pos < 0 || pos > 100) return 0;
4128 /* Assume default scroll range 0-100 */
4129 fw = es->format_rect.right - es->format_rect.left;
4130 new_x = pos * (es->text_width - fw) / 100;
4131 dx = es->text_width ? (new_x - es->x_offset) : 0;
4133 break;
4134 case SB_THUMBPOSITION:
4135 TRACE("SB_THUMBPOSITION %d\n", pos);
4136 es->flags &= ~EF_HSCROLL_TRACK;
4137 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4138 dx = pos - es->x_offset;
4139 else
4141 INT fw, new_x;
4142 /* Sanity check */
4143 if(pos < 0 || pos > 100) return 0;
4144 /* Assume default scroll range 0-100 */
4145 fw = es->format_rect.right - es->format_rect.left;
4146 new_x = pos * (es->text_width - fw) / 100;
4147 dx = es->text_width ? (new_x - es->x_offset) : 0;
4149 if (!dx) {
4150 /* force scroll info update */
4151 EDIT_UpdateScrollInfo(es);
4152 notify_parent(es, EN_HSCROLL);
4154 break;
4155 case SB_ENDSCROLL:
4156 TRACE("SB_ENDSCROLL\n");
4157 break;
4159 * FIXME : the next two are undocumented !
4160 * Are we doing the right thing ?
4161 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4162 * although it's also a regular control message.
4164 case EM_GETTHUMB: /* this one is used by NT notepad */
4166 LRESULT ret;
4167 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4168 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4169 else
4171 /* Assume default scroll range 0-100 */
4172 INT fw = es->format_rect.right - es->format_rect.left;
4173 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4175 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4176 return ret;
4178 case EM_LINESCROLL:
4179 TRACE("EM_LINESCROLL16\n");
4180 dx = pos;
4181 break;
4183 default:
4184 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4185 action, action);
4186 return 0;
4188 if (dx)
4190 INT fw = es->format_rect.right - es->format_rect.left;
4191 /* check if we are going to move too far */
4192 if(es->x_offset + dx + fw > es->text_width)
4193 dx = es->text_width - fw - es->x_offset;
4194 if(dx)
4195 EDIT_EM_LineScroll_internal(es, dx, 0);
4197 return 0;
4201 /*********************************************************************
4203 * WM_VSCROLL
4206 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4208 INT dy;
4210 if (!(es->style & ES_MULTILINE))
4211 return 0;
4213 if (!(es->style & ES_AUTOVSCROLL))
4214 return 0;
4216 dy = 0;
4217 switch (action) {
4218 case SB_LINEUP:
4219 case SB_LINEDOWN:
4220 case SB_PAGEUP:
4221 case SB_PAGEDOWN:
4222 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4223 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4224 (action == SB_PAGEUP ? "SB_PAGEUP" :
4225 "SB_PAGEDOWN"))));
4226 EDIT_EM_Scroll(es, action);
4227 return 0;
4228 case SB_TOP:
4229 TRACE("SB_TOP\n");
4230 dy = -es->y_offset;
4231 break;
4232 case SB_BOTTOM:
4233 TRACE("SB_BOTTOM\n");
4234 dy = es->line_count - 1 - es->y_offset;
4235 break;
4236 case SB_THUMBTRACK:
4237 TRACE("SB_THUMBTRACK %d\n", pos);
4238 es->flags |= EF_VSCROLL_TRACK;
4239 if(es->style & WS_VSCROLL)
4240 dy = pos - es->y_offset;
4241 else
4243 /* Assume default scroll range 0-100 */
4244 INT vlc, new_y;
4245 /* Sanity check */
4246 if(pos < 0 || pos > 100) return 0;
4247 vlc = get_vertical_line_count(es);
4248 new_y = pos * (es->line_count - vlc) / 100;
4249 dy = es->line_count ? (new_y - es->y_offset) : 0;
4250 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4251 es->line_count, es->y_offset, pos, dy);
4253 break;
4254 case SB_THUMBPOSITION:
4255 TRACE("SB_THUMBPOSITION %d\n", pos);
4256 es->flags &= ~EF_VSCROLL_TRACK;
4257 if(es->style & WS_VSCROLL)
4258 dy = pos - es->y_offset;
4259 else
4261 /* Assume default scroll range 0-100 */
4262 INT vlc, new_y;
4263 /* Sanity check */
4264 if(pos < 0 || pos > 100) return 0;
4265 vlc = get_vertical_line_count(es);
4266 new_y = pos * (es->line_count - vlc) / 100;
4267 dy = es->line_count ? (new_y - es->y_offset) : 0;
4268 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4269 es->line_count, es->y_offset, pos, dy);
4271 if (!dy)
4273 /* force scroll info update */
4274 EDIT_UpdateScrollInfo(es);
4275 notify_parent(es, EN_VSCROLL);
4277 break;
4278 case SB_ENDSCROLL:
4279 TRACE("SB_ENDSCROLL\n");
4280 break;
4282 * FIXME : the next two are undocumented !
4283 * Are we doing the right thing ?
4284 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4285 * although it's also a regular control message.
4287 case EM_GETTHUMB: /* this one is used by NT notepad */
4289 LRESULT ret;
4290 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4291 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4292 else
4294 /* Assume default scroll range 0-100 */
4295 INT vlc = get_vertical_line_count(es);
4296 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4298 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4299 return ret;
4301 case EM_LINESCROLL:
4302 TRACE("EM_LINESCROLL %d\n", pos);
4303 dy = pos;
4304 break;
4306 default:
4307 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4308 action, action);
4309 return 0;
4311 if (dy)
4312 EDIT_EM_LineScroll(es, 0, dy);
4313 return 0;
4316 /*********************************************************************
4318 * EM_GETTHUMB
4320 * FIXME: is this right ? (or should it be only VSCROLL)
4321 * (and maybe only for edit controls that really have their
4322 * own scrollbars) (and maybe only for multiline controls ?)
4323 * All in all: very poorly documented
4326 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4328 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4329 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4333 /********************************************************************
4335 * The Following code is to handle inline editing from IMEs
4338 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4340 LONG buflen;
4341 LPWSTR lpCompStr;
4342 LPSTR lpCompStrAttr = NULL;
4343 DWORD dwBufLenAttr;
4345 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4347 if (buflen < 0)
4349 return;
4352 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen);
4353 if (!lpCompStr)
4355 ERR("Unable to allocate IME CompositionString\n");
4356 return;
4359 if (buflen)
4360 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4362 if (CompFlag & GCS_COMPATTR)
4365 * We do not use the attributes yet. it would tell us what characters
4366 * are in transition and which are converted or decided upon
4368 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4369 if (dwBufLenAttr)
4371 dwBufLenAttr ++;
4372 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4373 if (!lpCompStrAttr)
4375 ERR("Unable to allocate IME Attribute String\n");
4376 HeapFree(GetProcessHeap(),0,lpCompStr);
4377 return;
4379 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4380 dwBufLenAttr);
4381 lpCompStrAttr[dwBufLenAttr] = 0;
4385 /* check for change in composition start */
4386 if (es->selection_end < es->composition_start)
4387 es->composition_start = es->selection_end;
4389 /* replace existing selection string */
4390 es->selection_start = es->composition_start;
4392 if (es->composition_len > 0)
4393 es->selection_end = es->composition_start + es->composition_len;
4394 else
4395 es->selection_end = es->selection_start;
4397 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4398 es->composition_len = abs(es->composition_start - es->selection_end);
4400 es->selection_start = es->composition_start;
4401 es->selection_end = es->selection_start + es->composition_len;
4403 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4404 HeapFree(GetProcessHeap(),0,lpCompStr);
4407 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4409 LONG buflen;
4410 LPWSTR lpResultStr;
4412 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4413 if (buflen <= 0)
4415 return;
4418 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4419 if (!lpResultStr)
4421 ERR("Unable to alloc buffer for IME string\n");
4422 return;
4425 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4427 /* check for change in composition start */
4428 if (es->selection_end < es->composition_start)
4429 es->composition_start = es->selection_end;
4431 es->selection_start = es->composition_start;
4432 es->selection_end = es->composition_start + es->composition_len;
4433 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4434 es->composition_start = es->selection_end;
4435 es->composition_len = 0;
4437 HeapFree(GetProcessHeap(),0,lpResultStr);
4440 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4442 HIMC hIMC;
4443 int cursor;
4445 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4447 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4448 es->composition_start = es->selection_end;
4451 hIMC = ImmGetContext(hwnd);
4452 if (!hIMC)
4453 return;
4455 if (CompFlag & GCS_RESULTSTR)
4457 EDIT_GetResultStr(hIMC, es);
4458 cursor = 0;
4460 else
4462 if (CompFlag & GCS_COMPSTR)
4463 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4464 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4466 ImmReleaseContext(hwnd, hIMC);
4467 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4471 /*********************************************************************
4473 * WM_NCCREATE
4475 * See also EDIT_WM_StyleChanged
4477 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4479 EDITSTATE *es;
4480 UINT alloc_size;
4482 TRACE("Creating %s edit control, style = %08lx\n",
4483 unicode ? "Unicode" : "ANSI", lpcs->style);
4485 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4486 return FALSE;
4487 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4490 * Note: since the EDITSTATE has not been fully initialized yet,
4491 * we can't use any API calls that may send
4492 * WM_XXX messages before WM_NCCREATE is completed.
4495 es->is_unicode = unicode;
4496 es->style = lpcs->style;
4498 es->bEnableState = !(es->style & WS_DISABLED);
4500 es->hwndSelf = hwnd;
4501 /* Save parent, which will be notified by EN_* messages */
4502 es->hwndParent = lpcs->hwndParent;
4504 if (es->style & ES_COMBO)
4505 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4507 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4508 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4510 /* Number overrides lowercase overrides uppercase (at least it
4511 * does in Win95). However I'll bet that ES_NUMBER would be
4512 * invalid under Win 3.1.
4514 if (es->style & ES_NUMBER) {
4515 ; /* do not override the ES_NUMBER */
4516 } else if (es->style & ES_LOWERCASE) {
4517 es->style &= ~ES_UPPERCASE;
4519 if (es->style & ES_MULTILINE) {
4520 es->buffer_limit = BUFLIMIT_INITIAL;
4521 if (es->style & WS_VSCROLL)
4522 es->style |= ES_AUTOVSCROLL;
4523 if (es->style & WS_HSCROLL)
4524 es->style |= ES_AUTOHSCROLL;
4525 es->style &= ~ES_PASSWORD;
4526 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4527 /* Confirmed - RIGHT overrides CENTER */
4528 if (es->style & ES_RIGHT)
4529 es->style &= ~ES_CENTER;
4530 es->style &= ~WS_HSCROLL;
4531 es->style &= ~ES_AUTOHSCROLL;
4533 } else {
4534 es->buffer_limit = BUFLIMIT_INITIAL;
4535 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4536 es->style &= ~ES_CENTER;
4537 es->style &= ~WS_HSCROLL;
4538 es->style &= ~WS_VSCROLL;
4539 if (es->style & ES_PASSWORD)
4540 es->password_char = '*';
4543 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4544 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4545 goto cleanup;
4546 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4548 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4549 goto cleanup;
4550 es->undo_buffer_size = es->buffer_size;
4552 if (es->style & ES_MULTILINE)
4553 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4554 goto cleanup;
4555 es->line_count = 1;
4558 * In Win95 look and feel, the WS_BORDER style is replaced by the
4559 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4560 * control a nonclient area so we don't need to draw the border.
4561 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4562 * a nonclient area and we should handle painting the border ourselves.
4564 * When making modifications please ensure that the code still works
4565 * for edit controls created directly with style 0x50800000, exStyle 0
4566 * (which should have a single pixel border)
4568 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4569 es->style &= ~WS_BORDER;
4570 else if (es->style & WS_BORDER)
4571 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4573 return TRUE;
4575 cleanup:
4576 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4577 EDIT_InvalidateUniscribeData(es);
4578 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4579 HeapFree(GetProcessHeap(), 0, es->undo_text);
4580 if (es->hloc32W) LocalFree(es->hloc32W);
4581 HeapFree(GetProcessHeap(), 0, es->logAttr);
4582 HeapFree(GetProcessHeap(), 0, es);
4583 return FALSE;
4587 /*********************************************************************
4589 * WM_CREATE
4592 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4594 RECT clientRect;
4596 TRACE("%s\n", debugstr_w(name));
4598 * To initialize some final structure members, we call some helper
4599 * functions. However, since the EDITSTATE is not consistent (i.e.
4600 * not fully initialized), we should be very careful which
4601 * functions can be called, and in what order.
4603 EDIT_WM_SetFont(es, 0, FALSE);
4604 EDIT_EM_EmptyUndoBuffer(es);
4606 /* We need to calculate the format rect
4607 (applications may send EM_SETMARGINS before the control gets visible) */
4608 GetClientRect(es->hwndSelf, &clientRect);
4609 EDIT_SetRectNP(es, &clientRect);
4611 if (name && *name) {
4612 EDIT_EM_ReplaceSel(es, FALSE, name, lstrlenW(name), FALSE, FALSE);
4613 /* if we insert text to the editline, the text scrolls out
4614 * of the window, as the caret is placed after the insert
4615 * pos normally; thus we reset es->selection... to 0 and
4616 * update caret
4618 es->selection_start = es->selection_end = 0;
4619 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4620 * Messages are only to be sent when the USER does something to
4621 * change the contents. So I am removing this EN_CHANGE
4623 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4625 EDIT_EM_ScrollCaret(es);
4627 /* force scroll info update */
4628 EDIT_UpdateScrollInfo(es);
4629 /* The rule seems to return 1 here for success */
4630 /* Power Builder masked edit controls will crash */
4631 /* if not. */
4632 /* FIXME: is that in all cases so ? */
4633 return 1;
4637 /*********************************************************************
4639 * WM_NCDESTROY
4642 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4644 LINEDEF *pc, *pp;
4646 /* The app can own the text buffer handle */
4647 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4648 LocalFree(es->hloc32W);
4650 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4651 LocalFree(es->hloc32A);
4653 EDIT_InvalidateUniscribeData(es);
4654 pc = es->first_line_def;
4655 while (pc)
4657 pp = pc->next;
4658 HeapFree(GetProcessHeap(), 0, pc);
4659 pc = pp;
4662 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4663 HeapFree(GetProcessHeap(), 0, es->undo_text);
4664 HeapFree(GetProcessHeap(), 0, es);
4666 return 0;
4670 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4672 if(unicode)
4673 return DefWindowProcW(hwnd, msg, wParam, lParam);
4674 else
4675 return DefWindowProcA(hwnd, msg, wParam, lParam);
4678 /*********************************************************************
4680 * EditWndProc_common
4682 * The messages are in the order of the actual integer values
4683 * (which can be found in include/windows.h)
4685 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4687 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4688 LRESULT result = 0;
4690 TRACE("hwnd=%p msg=%x (%s) wparam=%Ix lparam=%Ix\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4692 if (!es && msg != WM_NCCREATE)
4693 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4695 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4697 switch (msg) {
4698 case EM_GETSEL:
4699 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4700 break;
4702 case EM_SETSEL:
4703 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4704 EDIT_EM_ScrollCaret(es);
4705 result = 1;
4706 break;
4708 case EM_GETRECT:
4709 if (lParam)
4710 *((LPRECT)lParam) = es->format_rect;
4711 break;
4713 case EM_SETRECT:
4714 if ((es->style & ES_MULTILINE) && lParam) {
4715 EDIT_SetRectNP(es, (LPRECT)lParam);
4716 EDIT_UpdateText(es, NULL, TRUE);
4718 break;
4720 case EM_SETRECTNP:
4721 if ((es->style & ES_MULTILINE) && lParam)
4722 EDIT_SetRectNP(es, (LPRECT)lParam);
4723 break;
4725 case EM_SCROLL:
4726 result = EDIT_EM_Scroll(es, (INT)wParam);
4727 break;
4729 case EM_LINESCROLL:
4730 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4731 break;
4733 case EM_SCROLLCARET:
4734 EDIT_EM_ScrollCaret(es);
4735 result = 1;
4736 break;
4738 case EM_GETMODIFY:
4739 result = ((es->flags & EF_MODIFIED) != 0);
4740 break;
4742 case EM_SETMODIFY:
4743 if (wParam)
4744 es->flags |= EF_MODIFIED;
4745 else
4746 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4747 break;
4749 case EM_GETLINECOUNT:
4750 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4751 break;
4753 case EM_LINEINDEX:
4754 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4755 break;
4757 case EM_SETHANDLE:
4758 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4759 break;
4761 case EM_GETHANDLE:
4762 result = (LRESULT)EDIT_EM_GetHandle(es);
4763 break;
4765 case EM_GETTHUMB:
4766 result = EDIT_EM_GetThumb(es);
4767 break;
4769 /* these messages missing from specs */
4770 case 0x00bf:
4771 case 0x00c0:
4772 case 0x00c3:
4773 case 0x00ca:
4774 FIXME("undocumented message 0x%x, please report\n", msg);
4775 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4776 break;
4778 case EM_LINELENGTH:
4779 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4780 break;
4782 case EM_REPLACESEL:
4784 LPWSTR textW;
4786 if(unicode)
4787 textW = (LPWSTR)lParam;
4788 else
4790 LPSTR textA = (LPSTR)lParam;
4791 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4792 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4793 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4796 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, lstrlenW(textW), TRUE, TRUE);
4797 result = 1;
4799 if(!unicode)
4800 HeapFree(GetProcessHeap(), 0, textW);
4801 break;
4804 case EM_GETLINE:
4805 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4806 break;
4808 case EM_SETLIMITTEXT:
4809 EDIT_EM_SetLimitText(es, wParam);
4810 break;
4812 case EM_CANUNDO:
4813 result = (LRESULT)EDIT_EM_CanUndo(es);
4814 break;
4816 case EM_UNDO:
4817 case WM_UNDO:
4818 result = (LRESULT)EDIT_EM_Undo(es);
4819 break;
4821 case EM_FMTLINES:
4822 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4823 break;
4825 case EM_LINEFROMCHAR:
4826 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4827 break;
4829 case EM_SETTABSTOPS:
4830 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4831 break;
4833 case EM_SETPASSWORDCHAR:
4835 WCHAR charW = 0;
4837 if(unicode)
4838 charW = (WCHAR)wParam;
4839 else
4841 CHAR charA = wParam;
4842 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4845 EDIT_EM_SetPasswordChar(es, charW);
4846 break;
4849 case EM_EMPTYUNDOBUFFER:
4850 EDIT_EM_EmptyUndoBuffer(es);
4851 break;
4853 case EM_GETFIRSTVISIBLELINE:
4854 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4855 break;
4857 case EM_SETREADONLY:
4859 DWORD old_style = es->style;
4861 if (wParam) {
4862 SetWindowLongW( hwnd, GWL_STYLE,
4863 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4864 es->style |= ES_READONLY;
4865 } else {
4866 SetWindowLongW( hwnd, GWL_STYLE,
4867 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4868 es->style &= ~ES_READONLY;
4871 if (old_style ^ es->style)
4872 NtUserInvalidateRect(es->hwndSelf, NULL, TRUE);
4874 result = 1;
4875 break;
4878 case EM_SETWORDBREAKPROC:
4879 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4880 result = 1;
4881 break;
4883 case EM_GETWORDBREAKPROC:
4884 result = (LRESULT)es->word_break_proc;
4885 break;
4887 case EM_GETPASSWORDCHAR:
4889 if(unicode)
4890 result = es->password_char;
4891 else
4893 WCHAR charW = es->password_char;
4894 CHAR charA = 0;
4895 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4896 result = charA;
4898 break;
4901 case EM_SETMARGINS:
4902 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4903 break;
4905 case EM_GETMARGINS:
4906 result = MAKELONG(es->left_margin, es->right_margin);
4907 break;
4909 case EM_GETLIMITTEXT:
4910 result = es->buffer_limit;
4911 break;
4913 case EM_POSFROMCHAR:
4914 if ((INT)wParam >= get_text_length(es)) result = -1;
4915 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4916 break;
4918 case EM_CHARFROMPOS:
4919 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4920 break;
4922 case EM_SETIMESTATUS:
4923 if (wParam == EMSIS_COMPOSITIONSTRING)
4924 es->ime_status = lParam & 0xFFFF;
4926 result = 1;
4927 break;
4929 case EM_GETIMESTATUS:
4930 result = wParam == EMSIS_COMPOSITIONSTRING ? es->ime_status : 1;
4931 break;
4933 /* End of the EM_ messages which were in numerical order; what order
4934 * are these in? vaguely alphabetical?
4937 case WM_NCCREATE:
4938 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4939 break;
4941 case WM_NCDESTROY:
4942 result = EDIT_WM_NCDestroy(es);
4943 es = NULL;
4944 break;
4946 case WM_GETDLGCODE:
4947 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4949 if (es->style & ES_MULTILINE)
4950 result |= DLGC_WANTALLKEYS;
4952 if (lParam)
4954 es->flags|=EF_DIALOGMODE;
4956 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4958 int vk = (int)((LPMSG)lParam)->wParam;
4960 if (es->hwndListBox)
4962 if (vk == VK_RETURN || vk == VK_ESCAPE)
4963 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4964 result |= DLGC_WANTMESSAGE;
4968 break;
4970 case WM_CHAR:
4972 WCHAR charW;
4974 if(unicode)
4975 charW = wParam;
4976 else
4978 CHAR charA = wParam;
4979 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4982 if (es->hwndListBox)
4984 if (charW == VK_RETURN || charW == VK_ESCAPE)
4986 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4987 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4988 break;
4991 result = EDIT_WM_Char(es, charW);
4992 break;
4995 case WM_UNICHAR:
4996 if (unicode)
4998 if (wParam == UNICODE_NOCHAR) return TRUE;
4999 if (wParam <= 0x000fffff)
5001 if(wParam > 0xffff) /* convert to surrogates */
5003 wParam -= 0x10000;
5004 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
5005 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
5007 else EDIT_WM_Char(es, wParam);
5009 return 0;
5011 break;
5013 case WM_CLEAR:
5014 EDIT_WM_Clear(es);
5015 break;
5017 case WM_CONTEXTMENU:
5018 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5019 break;
5021 case WM_COPY:
5022 EDIT_WM_Copy(es);
5023 break;
5025 case WM_CREATE:
5026 if(unicode)
5027 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
5028 else
5030 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
5031 LPWSTR nameW = NULL;
5032 if(nameA)
5034 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5035 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5036 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5038 result = EDIT_WM_Create(es, nameW);
5039 HeapFree(GetProcessHeap(), 0, nameW);
5041 break;
5043 case WM_CUT:
5044 EDIT_WM_Cut(es);
5045 break;
5047 case WM_ENABLE:
5048 es->bEnableState = (BOOL) wParam;
5049 EDIT_UpdateText(es, NULL, TRUE);
5050 break;
5052 case WM_ERASEBKGND:
5053 /* we do the proper erase in EDIT_WM_Paint */
5054 result = 1;
5055 break;
5057 case WM_GETFONT:
5058 result = (LRESULT)es->font;
5059 break;
5061 case WM_GETTEXT:
5062 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5063 break;
5065 case WM_GETTEXTLENGTH:
5066 if (unicode) result = get_text_length(es);
5067 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5068 NULL, 0, NULL, NULL );
5069 break;
5071 case WM_HSCROLL:
5072 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5073 break;
5075 case WM_KEYDOWN:
5076 result = EDIT_WM_KeyDown(es, (INT)wParam);
5077 break;
5079 case WM_KILLFOCUS:
5080 result = EDIT_WM_KillFocus(es);
5081 break;
5083 case WM_LBUTTONDBLCLK:
5084 result = EDIT_WM_LButtonDblClk(es);
5085 break;
5087 case WM_LBUTTONDOWN:
5088 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5089 break;
5091 case WM_LBUTTONUP:
5092 result = EDIT_WM_LButtonUp(es);
5093 break;
5095 case WM_MBUTTONDOWN:
5096 result = EDIT_WM_MButtonDown(es);
5097 break;
5099 case WM_MOUSEMOVE:
5100 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5101 break;
5103 case WM_PRINTCLIENT:
5104 case WM_PAINT:
5105 EDIT_WM_Paint(es, (HDC)wParam);
5106 break;
5108 case WM_PASTE:
5109 EDIT_WM_Paste(es);
5110 break;
5112 case WM_SETFOCUS:
5113 EDIT_WM_SetFocus(es);
5114 break;
5116 case WM_SETFONT:
5117 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5118 break;
5120 case WM_SETREDRAW:
5121 /* FIXME: actually set an internal flag and behave accordingly */
5122 break;
5124 case WM_SETTEXT:
5125 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5126 result = TRUE;
5127 break;
5129 case WM_SIZE:
5130 EDIT_WM_Size(es, (UINT)wParam);
5131 break;
5133 case WM_STYLECHANGED:
5134 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5135 break;
5137 case WM_STYLECHANGING:
5138 result = 0; /* See EDIT_WM_StyleChanged */
5139 break;
5141 case WM_SYSKEYDOWN:
5142 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5143 break;
5145 case WM_TIMER:
5146 EDIT_WM_Timer(es);
5147 break;
5149 case WM_VSCROLL:
5150 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5151 break;
5153 case WM_MOUSEWHEEL:
5155 int wheelDelta;
5156 INT pulScrollLines = 3;
5157 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5159 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5160 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5161 break;
5163 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5164 /* if scrolling changes direction, ignore left overs */
5165 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5166 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5167 es->wheelDeltaRemainder += wheelDelta;
5168 else
5169 es->wheelDeltaRemainder = wheelDelta;
5170 if (es->wheelDeltaRemainder && pulScrollLines)
5172 int cLineScroll;
5173 pulScrollLines = min(es->line_count, pulScrollLines);
5174 cLineScroll = pulScrollLines * es->wheelDeltaRemainder / WHEEL_DELTA;
5175 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
5176 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5179 break;
5182 /* IME messages to make the edit control IME aware */
5183 case WM_IME_SETCONTEXT:
5184 break;
5186 case WM_IME_STARTCOMPOSITION:
5187 es->composition_start = es->selection_end;
5188 es->composition_len = 0;
5189 break;
5191 case WM_IME_COMPOSITION:
5192 if (lParam & GCS_RESULTSTR && !(es->ime_status & EIMES_GETCOMPSTRATONCE))
5194 DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5195 break;
5198 EDIT_ImeComposition(hwnd, lParam, es);
5199 break;
5201 case WM_IME_ENDCOMPOSITION:
5202 if (es->composition_len > 0)
5204 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5205 es->selection_end = es->selection_start;
5206 es->composition_len= 0;
5208 break;
5210 case WM_IME_COMPOSITIONFULL:
5211 break;
5213 case WM_IME_SELECT:
5214 break;
5216 case WM_IME_CONTROL:
5217 break;
5219 case WM_IME_REQUEST:
5220 switch (wParam)
5222 case IMR_QUERYCHARPOSITION:
5224 LRESULT pos;
5225 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5227 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5228 chpos->pt.x = LOWORD(pos);
5229 chpos->pt.y = HIWORD(pos);
5230 chpos->cLineHeight = es->line_height;
5231 chpos->rcDocument = es->format_rect;
5232 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5233 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5234 result = 1;
5235 break;
5238 break;
5240 default:
5241 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5242 break;
5245 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5246 EDIT_UnlockBuffer(es, FALSE);
5248 TRACE("hwnd=%p msg=%x (%s) -- 0x%08Ix\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5250 return result;