user32/edit: Let DefWindowProcA/W() handle WM_IME_CHAR.
[wine.git] / dlls / user32 / edit.c
blob6593ee6dce2dcd8c436a4675543dae88e0305814
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 <stdarg.h>
31 #include <string.h>
32 #include <stdlib.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnt.h"
37 #include "win.h"
38 #include "imm.h"
39 #include "usp10.h"
40 #include "controls.h"
41 #include "user_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(edit);
45 WINE_DECLARE_DEBUG_CHANNEL(combo);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
49 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
50 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
51 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
54 * extra flags for EDITSTATE.flags field
56 #define EF_MODIFIED 0x0001 /* text has been modified */
57 #define EF_FOCUSED 0x0002 /* we have input focus */
58 #define EF_UPDATE 0x0004 /* notify parent of changed state */
59 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
60 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
61 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
62 wrapped line, instead of in front of the next character */
63 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
64 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
66 typedef enum
68 END_0 = 0, /* line ends with terminating '\0' character */
69 END_WRAP, /* line is wrapped */
70 END_HARD, /* line ends with a hard return '\r\n' */
71 END_SOFT, /* line ends with a soft return '\r\r\n' */
72 END_RICH /* line ends with a single '\n' */
73 } LINE_END;
75 typedef struct tagLINEDEF {
76 INT length; /* bruto length of a line in bytes */
77 INT net_length; /* netto length of a line in visible characters */
78 LINE_END ending;
79 INT width; /* width of the line in pixels */
80 INT index; /* line index into the buffer */
81 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
82 struct tagLINEDEF *next;
83 } LINEDEF;
85 typedef struct
87 BOOL is_unicode; /* how the control was created */
88 LPWSTR text; /* the actual contents of the control */
89 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
90 UINT buffer_size; /* the size of the buffer in characters */
91 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
92 HFONT font; /* NULL means standard system font */
93 INT x_offset; /* scroll offset for multi lines this is in pixels
94 for single lines it's in characters */
95 INT line_height; /* height of a screen line in pixels */
96 INT char_width; /* average character width in pixels */
97 DWORD style; /* sane version of wnd->dwStyle */
98 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
99 INT undo_insert_count; /* number of characters inserted in sequence */
100 UINT undo_position; /* character index of the insertion and deletion */
101 LPWSTR undo_text; /* deleted text */
102 UINT undo_buffer_size; /* size of the deleted text buffer */
103 INT selection_start; /* == selection_end if no selection */
104 INT selection_end; /* == current caret position */
105 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
106 INT left_margin; /* in pixels */
107 INT right_margin; /* in pixels */
108 RECT format_rect;
109 INT text_width; /* width of the widest line in pixels for multi line controls
110 and just line width for single line controls */
111 INT region_posx; /* Position of cursor relative to region: */
112 INT region_posy; /* -1: to left, 0: within, 1: to right */
113 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
114 INT line_count; /* number of lines */
115 INT y_offset; /* scroll offset in number of lines */
116 BOOL bCaptureState; /* flag indicating whether mouse was captured */
117 BOOL bEnableState; /* flag keeping the enable state */
118 HWND hwndSelf; /* the our window handle */
119 HWND hwndParent; /* Handle of parent for sending EN_* messages.
120 Even if parent will change, EN_* messages
121 should be sent to the first parent. */
122 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
123 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
125 * only for multi line controls
127 INT lock_count; /* amount of re-entries in the EditWndProc */
128 INT tabs_count;
129 LPINT tabs;
130 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
131 HLOCAL hloc32W; /* our unicode local memory block */
132 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
133 or EM_SETHANDLE */
134 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
136 * IME Data
138 UINT composition_len; /* length of composition, 0 == no composition */
139 int composition_start; /* the character position for the composition */
140 UINT ime_status; /* IME status flag */
142 * Uniscribe Data
144 SCRIPT_LOGATTR *logAttr;
145 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
146 } EDITSTATE;
149 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
150 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
152 /* used for disabled or read-only edit control */
153 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
154 do \
155 { /* Notify parent which has created this edit control */ \
156 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
157 SendMessageW(es->hwndParent, WM_COMMAND, \
158 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
159 (LPARAM)(es->hwndSelf)); \
160 } while(0)
162 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
164 /*********************************************************************
166 * EM_CANUNDO
169 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
171 return (es->undo_insert_count || lstrlenW(es->undo_text));
175 /*********************************************************************
177 * EM_EMPTYUNDOBUFFER
180 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
182 es->undo_insert_count = 0;
183 *es->undo_text = '\0';
187 /**********************************************************************
188 * get_app_version
190 * Returns the window version in case Wine emulates a later version
191 * of windows than the application expects.
193 * In a number of cases when windows runs an application that was
194 * designed for an earlier windows version, windows reverts
195 * to "old" behaviour of that earlier version.
197 * An example is a disabled edit control that needs to be painted.
198 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
199 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
200 * applications with an expected version 0f 4.0 or higher.
203 static DWORD get_app_version(void)
205 static DWORD version;
206 if (!version)
208 DWORD dwEmulatedVersion;
209 OSVERSIONINFOW info;
210 DWORD dwProcVersion = GetProcessVersion(0);
212 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
213 GetVersionExW( &info );
214 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
215 /* FIXME: this may not be 100% correct; see discussion on the
216 * wine developer list in Nov 1999 */
217 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
219 return version;
222 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
224 HBRUSH hbrush;
225 UINT msg;
227 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
228 msg = WM_CTLCOLORSTATIC;
229 else
230 msg = WM_CTLCOLOREDIT;
232 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
233 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
234 if (!hbrush)
235 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
236 return hbrush;
240 static inline UINT get_text_length(EDITSTATE *es)
242 if(es->text_length == (UINT)-1)
243 es->text_length = lstrlenW(es->text);
244 return es->text_length;
248 /*********************************************************************
250 * EDIT_WordBreakProc
252 * Find the beginning of words.
253 * Note: unlike the specs for a WordBreakProc, this function can
254 * only be called without linebreaks between s[0] up to
255 * s[count - 1]. Remember it is only called
256 * internally, so we can decide this for ourselves.
257 * Additionally we will always be breaking the full string.
260 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
262 INT ret = 0;
264 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
266 if(!s) return 0;
268 if (!es->logAttr)
270 SCRIPT_ANALYSIS psa;
272 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
273 psa.eScript = SCRIPT_UNDEFINED;
275 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
276 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
279 switch (action) {
280 case WB_LEFT:
281 if (index)
282 index--;
283 while (index && !es->logAttr[index].fSoftBreak)
284 index--;
285 ret = index;
286 break;
287 case WB_RIGHT:
288 if (!count)
289 break;
290 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
291 index++;
292 ret = index;
293 break;
294 case WB_ISDELIMITER:
295 ret = es->logAttr[index].fWhiteSpace;
296 break;
297 default:
298 ERR("unknown action code, please report !\n");
299 break;
301 return ret;
305 /*********************************************************************
307 * EDIT_CallWordBreakProc
309 * Call appropriate WordBreakProc (internal or external).
311 * Note: The "start" argument should always be an index referring
312 * to es->text. The actual wordbreak proc might be
313 * 16 bit, so we can't always pass any 32 bit LPSTR.
314 * Hence we assume that es->text is the buffer that holds
315 * the string under examination (we can decide this for ourselves).
318 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
320 INT ret;
322 if (es->word_break_proc)
324 if(es->is_unicode)
326 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
328 TRACE_(relay)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
329 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
330 ret = wbpW(es->text + start, index, count, action);
331 TRACE_(relay)("\1Ret wordbreakW %p retval=%d\n", es->word_break_proc, ret );
333 else
335 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
336 INT countA;
337 CHAR *textA;
339 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
340 textA = HeapAlloc(GetProcessHeap(), 0, countA);
341 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
342 TRACE_(relay)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
343 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
344 ret = wbpA(textA, index, countA, action);
345 HeapFree(GetProcessHeap(), 0, textA);
346 TRACE_(relay)("\1Ret wordbreakA %p retval=%d\n", es->word_break_proc, ret );
349 else
350 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
352 return ret;
355 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
357 if (line_def->ssa)
359 ScriptStringFree(&line_def->ssa);
360 line_def->ssa = NULL;
364 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
366 LINEDEF *line_def = es->first_line_def;
367 while (line_def)
369 EDIT_InvalidateUniscribeData_linedef(line_def);
370 line_def = line_def->next;
372 if (es->ssa)
374 ScriptStringFree(&es->ssa);
375 es->ssa = NULL;
379 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
381 if (!line_def)
382 return NULL;
384 if (line_def->net_length && !line_def->ssa)
386 int index = line_def->index;
387 HFONT old_font = NULL;
388 HDC udc = dc;
389 SCRIPT_TABDEF tabdef;
390 HRESULT hr;
392 if (!udc)
393 udc = GetDC(es->hwndSelf);
394 if (es->font)
395 old_font = SelectObject(udc, es->font);
397 tabdef.cTabStops = es->tabs_count;
398 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
399 tabdef.pTabStops = es->tabs;
400 tabdef.iTabOrigin = 0;
402 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
403 (1.5*line_def->net_length+16), -1,
404 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
405 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
406 if (FAILED(hr))
408 WARN("ScriptStringAnalyse failed (%lx)\n",hr);
409 line_def->ssa = NULL;
412 if (es->font)
413 SelectObject(udc, old_font);
414 if (udc != dc)
415 NtUserReleaseDC( es->hwndSelf, udc );
418 return line_def->ssa;
421 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
423 LINEDEF *line_def;
425 if (!(es->style & ES_MULTILINE))
427 if (!es->ssa)
429 INT length = get_text_length(es);
430 HFONT old_font = NULL;
431 HDC udc = dc;
433 if (!udc)
434 udc = GetDC(es->hwndSelf);
435 if (es->font)
436 old_font = SelectObject(udc, es->font);
438 if (es->style & ES_PASSWORD)
439 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);
440 else
441 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
443 if (es->font)
444 SelectObject(udc, old_font);
445 if (udc != dc)
446 NtUserReleaseDC( es->hwndSelf, udc );
448 return es->ssa;
450 else
452 line_def = es->first_line_def;
453 while (line_def && line)
455 line_def = line_def->next;
456 line--;
459 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
463 static inline INT get_vertical_line_count(EDITSTATE *es)
465 INT vlc = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
466 return max(1,vlc);
469 /*********************************************************************
471 * EDIT_BuildLineDefs_ML
473 * Build linked list of text lines.
474 * Lines can end with '\0' (last line), a character (if it is wrapped),
475 * a soft return '\r\r\n' or a hard return '\r\n'
478 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
480 LPWSTR current_position, cp;
481 INT fw;
482 LINEDEF *current_line;
483 LINEDEF *previous_line;
484 LINEDEF *start_line;
485 INT line_index = 0, nstart_line, nstart_index;
486 INT line_count = es->line_count;
487 INT orig_net_length;
488 RECT rc;
489 INT vlc;
491 if (istart == iend && delta == 0)
492 return;
494 previous_line = NULL;
495 current_line = es->first_line_def;
497 /* Find starting line. istart must lie inside an existing line or
498 * at the end of buffer */
499 do {
500 if (istart < current_line->index + current_line->length ||
501 current_line->ending == END_0)
502 break;
504 previous_line = current_line;
505 current_line = current_line->next;
506 line_index++;
507 } while (current_line);
509 if (!current_line) /* Error occurred start is not inside previous buffer */
511 FIXME(" modification occurred outside buffer\n");
512 return;
515 /* Remember start of modifications in order to calculate update region */
516 nstart_line = line_index;
517 nstart_index = current_line->index;
519 /* We must start to reformat from the previous line since the modifications
520 * may have caused the line to wrap upwards. */
521 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
523 line_index--;
524 current_line = previous_line;
526 start_line = current_line;
528 fw = es->format_rect.right - es->format_rect.left;
529 current_position = es->text + current_line->index;
530 vlc = get_vertical_line_count(es);
531 do {
532 if (current_line != start_line)
534 if (!current_line || current_line->index + delta > current_position - es->text)
536 /* The buffer has been expanded, create a new line and
537 insert it into the link list */
538 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
539 new_line->next = previous_line->next;
540 previous_line->next = new_line;
541 current_line = new_line;
542 es->line_count++;
544 else if (current_line->index + delta < current_position - es->text)
546 /* The previous line merged with this line so we delete this extra entry */
547 previous_line->next = current_line->next;
548 HeapFree(GetProcessHeap(), 0, current_line);
549 current_line = previous_line->next;
550 es->line_count--;
551 continue;
553 else /* current_line->index + delta == current_position */
555 if (current_position - es->text > iend)
556 break; /* We reached end of line modifications */
557 /* else recalculate this line */
561 current_line->index = current_position - es->text;
562 orig_net_length = current_line->net_length;
564 /* Find end of line */
565 cp = current_position;
566 while (*cp) {
567 if (*cp == '\n') break;
568 if ((*cp == '\r') && (*(cp + 1) == '\n'))
569 break;
570 cp++;
573 /* Mark type of line termination */
574 if (!(*cp)) {
575 current_line->ending = END_0;
576 current_line->net_length = lstrlenW(current_position);
577 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
578 current_line->ending = END_SOFT;
579 current_line->net_length = cp - current_position - 1;
580 } else if (*cp == '\n') {
581 current_line->ending = END_RICH;
582 current_line->net_length = cp - current_position;
583 } else {
584 current_line->ending = END_HARD;
585 current_line->net_length = cp - current_position;
588 if (current_line->net_length)
590 const SIZE *sz;
591 EDIT_InvalidateUniscribeData_linedef(current_line);
592 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
593 if (current_line->ssa)
595 sz = ScriptString_pSize(current_line->ssa);
596 /* Calculate line width */
597 current_line->width = sz->cx;
599 else current_line->width = es->char_width * current_line->net_length;
601 else current_line->width = 0;
603 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
605 /* Line breaks just look back from the end and find the next break and try that. */
607 if (!(es->style & ES_AUTOHSCROLL)) {
608 if (current_line->width > fw && fw > es->char_width) {
610 INT prev, next;
611 int w;
612 const SIZE *sz;
613 float d;
615 prev = current_line->net_length - 1;
616 w = current_line->net_length;
617 d = (float)current_line->width/(float)fw;
618 if (d > 1.2f) d -= 0.2f;
619 next = prev/d;
620 if (next >= prev) next = prev-1;
621 do {
622 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
623 next, current_line->net_length, WB_LEFT);
624 current_line->net_length = prev;
625 EDIT_InvalidateUniscribeData_linedef(current_line);
626 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
627 if (current_line->ssa)
628 sz = ScriptString_pSize(current_line->ssa);
629 else sz = 0;
630 if (sz)
631 current_line->width = sz->cx;
632 else
633 prev = 0;
634 next = prev - 1;
635 } while (prev && current_line->width > fw);
636 current_line->net_length = w;
638 if (prev == 0) { /* Didn't find a line break so force a break */
639 INT *piDx;
640 const INT *count;
642 EDIT_InvalidateUniscribeData_linedef(current_line);
643 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
645 if (current_line->ssa)
647 count = ScriptString_pcOutChars(current_line->ssa);
648 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
649 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
651 prev = current_line->net_length-1;
652 do {
653 current_line->width -= piDx[prev];
654 prev--;
655 } while ( prev > 0 && current_line->width > fw);
656 if (prev<=0)
657 prev = 1;
658 HeapFree(GetProcessHeap(),0,piDx);
660 else
661 prev = (fw / es->char_width);
664 /* If the first line we are calculating, wrapped before istart, we must
665 * adjust istart in order for this to be reflected in the update region. */
666 if (current_line->index == nstart_index && istart > current_line->index + prev)
667 istart = current_line->index + prev;
668 /* else if we are updating the previous line before the first line we
669 * are re-calculating and it expanded */
670 else if (current_line == start_line &&
671 current_line->index != nstart_index && orig_net_length < prev)
673 /* Line expanded due to an upwards line wrap so we must partially include
674 * previous line in update region */
675 nstart_line = line_index;
676 nstart_index = current_line->index;
677 istart = current_line->index + orig_net_length;
680 current_line->net_length = prev;
681 current_line->ending = END_WRAP;
683 if (current_line->net_length > 0)
685 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
686 if (current_line->ssa)
688 sz = ScriptString_pSize(current_line->ssa);
689 current_line->width = sz->cx;
691 else
692 current_line->width = 0;
694 else current_line->width = 0;
696 else if (current_line == start_line &&
697 current_line->index != nstart_index &&
698 orig_net_length < current_line->net_length) {
699 /* The previous line expanded but it's still not as wide as the client rect */
700 /* The expansion is due to an upwards line wrap so we must partially include
701 it in the update region */
702 nstart_line = line_index;
703 nstart_index = current_line->index;
704 istart = current_line->index + orig_net_length;
709 /* Adjust length to include line termination */
710 switch (current_line->ending) {
711 case END_SOFT:
712 current_line->length = current_line->net_length + 3;
713 break;
714 case END_RICH:
715 current_line->length = current_line->net_length + 1;
716 break;
717 case END_HARD:
718 current_line->length = current_line->net_length + 2;
719 break;
720 case END_WRAP:
721 case END_0:
722 current_line->length = current_line->net_length;
723 break;
725 es->text_width = max(es->text_width, current_line->width);
726 current_position += current_line->length;
727 previous_line = current_line;
729 /* Discard data for non-visible lines. It will be calculated as needed */
730 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
731 EDIT_InvalidateUniscribeData_linedef(current_line);
733 current_line = current_line->next;
734 line_index++;
735 } while (previous_line->ending != END_0);
737 /* Finish adjusting line indexes by delta or remove hanging lines */
738 if (previous_line->ending == END_0)
740 LINEDEF *pnext = NULL;
742 previous_line->next = NULL;
743 while (current_line)
745 pnext = current_line->next;
746 EDIT_InvalidateUniscribeData_linedef(current_line);
747 HeapFree(GetProcessHeap(), 0, current_line);
748 current_line = pnext;
749 es->line_count--;
752 else if (delta != 0)
754 while (current_line)
756 current_line->index += delta;
757 current_line = current_line->next;
761 /* Calculate rest of modification rectangle */
762 if (hrgn)
764 HRGN tmphrgn;
766 * We calculate two rectangles. One for the first line which may have
767 * an indent with respect to the format rect. The other is a format-width
768 * rectangle that spans the rest of the lines that changed or moved.
770 rc.top = es->format_rect.top + nstart_line * es->line_height -
771 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
772 rc.bottom = rc.top + es->line_height;
773 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
774 rc.left = es->format_rect.left;
775 else
776 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
777 rc.right = es->format_rect.right;
778 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
780 rc.top = rc.bottom;
781 rc.left = es->format_rect.left;
782 rc.right = es->format_rect.right;
784 * If lines were added or removed we must re-paint the remainder of the
785 * lines since the remaining lines were either shifted up or down.
787 if (line_count < es->line_count) /* We added lines */
788 rc.bottom = es->line_count * es->line_height;
789 else if (line_count > es->line_count) /* We removed lines */
790 rc.bottom = line_count * es->line_height;
791 else
792 rc.bottom = line_index * es->line_height;
793 rc.bottom += es->format_rect.top;
794 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
795 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
796 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
797 DeleteObject(tmphrgn);
801 /*********************************************************************
803 * EDIT_CalcLineWidth_SL
806 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
808 EDIT_UpdateUniscribeData(es, NULL, 0);
809 if (es->ssa)
811 const SIZE *size;
812 size = ScriptString_pSize(es->ssa);
813 es->text_width = size->cx;
815 else
816 es->text_width = 0;
819 /*********************************************************************
821 * EDIT_CharFromPos
823 * Beware: This is not the function called on EM_CHARFROMPOS
824 * The position _can_ be outside the formatting / client
825 * rectangle
826 * The return value is only the character index
829 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
831 INT index;
833 if (es->style & ES_MULTILINE) {
834 int trailing;
835 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
836 INT line_index = 0;
837 LINEDEF *line_def = es->first_line_def;
838 EDIT_UpdateUniscribeData(es, NULL, line);
839 while ((line > 0) && line_def->next) {
840 line_index += line_def->length;
841 line_def = line_def->next;
842 line--;
845 x += es->x_offset - es->format_rect.left;
846 if (es->style & ES_RIGHT)
847 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
848 else if (es->style & ES_CENTER)
849 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
850 if (x >= line_def->width) {
851 if (after_wrap)
852 *after_wrap = (line_def->ending == END_WRAP);
853 return line_index + line_def->net_length;
855 if (x <= 0 || !line_def->ssa) {
856 if (after_wrap)
857 *after_wrap = FALSE;
858 return line_index;
861 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
862 if (trailing) index++;
863 index += line_index;
864 if (after_wrap)
865 *after_wrap = ((index == line_index + line_def->net_length) &&
866 (line_def->ending == END_WRAP));
867 } else {
868 INT xoff = 0;
869 INT trailing;
870 if (after_wrap)
871 *after_wrap = FALSE;
872 x -= es->format_rect.left;
873 if (!x)
874 return es->x_offset;
876 if (!es->x_offset)
878 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
879 if (es->style & ES_RIGHT)
880 x -= indent;
881 else if (es->style & ES_CENTER)
882 x -= indent / 2;
885 EDIT_UpdateUniscribeData(es, NULL, 0);
886 if (es->x_offset)
888 if (es->ssa)
890 if (es->x_offset>= get_text_length(es))
892 const SIZE *size;
893 size = ScriptString_pSize(es->ssa);
894 xoff = size->cx;
896 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
898 else
899 xoff = 0;
901 if (x < 0)
903 if (x + xoff > 0 || !es->ssa)
905 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
906 if (trailing) index++;
908 else
909 index = 0;
911 else
913 if (x)
915 const SIZE *size = NULL;
916 if (es->ssa)
917 size = ScriptString_pSize(es->ssa);
918 if (!size)
919 index = 0;
920 else if (x > size->cx)
921 index = get_text_length(es);
922 else if (es->ssa)
924 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
925 if (trailing) index++;
927 else
928 index = 0;
930 else
931 index = es->x_offset;
934 return index;
938 /*********************************************************************
940 * EDIT_ConfinePoint
942 * adjusts the point to be within the formatting rectangle
943 * (so CharFromPos returns the nearest _visible_ character)
946 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
948 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
949 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
953 /*********************************************************************
955 * EM_LINEFROMCHAR
958 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
960 INT line;
961 LINEDEF *line_def;
963 if (!(es->style & ES_MULTILINE))
964 return 0;
965 if (index > (INT)get_text_length(es))
966 return es->line_count - 1;
967 if (index == -1)
968 index = min(es->selection_start, es->selection_end);
970 line = 0;
971 line_def = es->first_line_def;
972 index -= line_def->length;
973 while ((index >= 0) && line_def->next) {
974 line++;
975 line_def = line_def->next;
976 index -= line_def->length;
978 return line;
982 /*********************************************************************
984 * EM_LINEINDEX
987 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
989 INT line_index;
990 const LINEDEF *line_def;
992 if (!(es->style & ES_MULTILINE))
993 return 0;
994 if (line >= es->line_count)
995 return -1;
997 line_index = 0;
998 line_def = es->first_line_def;
999 if (line == -1) {
1000 INT index = es->selection_end - line_def->length;
1001 while ((index >= 0) && line_def->next) {
1002 line_index += line_def->length;
1003 line_def = line_def->next;
1004 index -= line_def->length;
1006 } else {
1007 while (line > 0) {
1008 line_index += line_def->length;
1009 line_def = line_def->next;
1010 line--;
1013 return line_index;
1017 /*********************************************************************
1019 * EM_LINELENGTH
1022 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1024 LINEDEF *line_def;
1026 if (!(es->style & ES_MULTILINE))
1027 return get_text_length(es);
1029 if (index == -1) {
1030 /* get the number of remaining non-selected chars of selected lines */
1031 INT32 l; /* line number */
1032 INT32 li; /* index of first char in line */
1033 INT32 count;
1034 l = EDIT_EM_LineFromChar(es, es->selection_start);
1035 /* # chars before start of selection area */
1036 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1037 l = EDIT_EM_LineFromChar(es, es->selection_end);
1038 /* # chars after end of selection */
1039 li = EDIT_EM_LineIndex(es, l);
1040 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1041 return count;
1043 line_def = es->first_line_def;
1044 index -= line_def->length;
1045 while ((index >= 0) && line_def->next) {
1046 line_def = line_def->next;
1047 index -= line_def->length;
1049 return line_def->net_length;
1053 /*********************************************************************
1055 * EM_POSFROMCHAR
1058 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1060 INT len = get_text_length(es);
1061 INT l;
1062 INT li;
1063 INT x = 0;
1064 INT y = 0;
1065 INT w;
1066 INT lw;
1067 LINEDEF *line_def;
1069 index = min(index, len);
1070 if (es->style & ES_MULTILINE) {
1071 l = EDIT_EM_LineFromChar(es, index);
1072 EDIT_UpdateUniscribeData(es, NULL, l);
1074 y = (l - es->y_offset) * es->line_height;
1075 li = EDIT_EM_LineIndex(es, l);
1076 if (after_wrap && (li == index) && l) {
1077 INT l2 = l - 1;
1078 line_def = es->first_line_def;
1079 while (l2) {
1080 line_def = line_def->next;
1081 l2--;
1083 if (line_def->ending == END_WRAP) {
1084 l--;
1085 y -= es->line_height;
1086 li = EDIT_EM_LineIndex(es, l);
1090 line_def = es->first_line_def;
1091 while (line_def->index != li)
1092 line_def = line_def->next;
1094 lw = line_def->width;
1095 w = es->format_rect.right - es->format_rect.left;
1096 if (line_def->ssa)
1097 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1098 x -= es->x_offset;
1100 if (es->style & ES_RIGHT)
1101 x = w - (lw - x);
1102 else if (es->style & ES_CENTER)
1103 x += (w - lw) / 2;
1104 } else {
1105 INT xoff = 0;
1106 INT xi = 0;
1107 EDIT_UpdateUniscribeData(es, NULL, 0);
1108 if (es->x_offset)
1110 if (es->ssa)
1112 if (es->x_offset >= get_text_length(es))
1114 int leftover = es->x_offset - get_text_length(es);
1115 if (es->ssa)
1117 const SIZE *size;
1118 size = ScriptString_pSize(es->ssa);
1119 xoff = size->cx;
1121 else
1122 xoff = 0;
1123 xoff += es->char_width * leftover;
1125 else
1126 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1128 else
1129 xoff = 0;
1131 if (index)
1133 if (index >= get_text_length(es))
1135 if (es->ssa)
1137 const SIZE *size;
1138 size = ScriptString_pSize(es->ssa);
1139 xi = size->cx;
1141 else
1142 xi = 0;
1144 else if (es->ssa)
1145 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1146 else
1147 xi = 0;
1149 x = xi - xoff;
1151 if (index >= es->x_offset) {
1152 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1154 w = es->format_rect.right - es->format_rect.left;
1155 if (w > es->text_width)
1157 if (es->style & ES_RIGHT)
1158 x += w - es->text_width;
1159 else if (es->style & ES_CENTER)
1160 x += (w - es->text_width) / 2;
1164 y = 0;
1166 x += es->format_rect.left;
1167 y += es->format_rect.top;
1168 return MAKELONG((INT16)x, (INT16)y);
1172 /*********************************************************************
1174 * EDIT_GetLineRect
1176 * Calculates the bounding rectangle for a line from a starting
1177 * column to an ending column.
1180 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1182 SCRIPT_STRING_ANALYSIS ssa;
1183 INT line_index = 0;
1184 INT pt1, pt2, pt3;
1186 if (es->style & ES_MULTILINE)
1188 const LINEDEF *line_def = NULL;
1189 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1190 if (line >= es->line_count)
1191 return;
1193 line_def = es->first_line_def;
1194 if (line == -1) {
1195 INT index = es->selection_end - line_def->length;
1196 while ((index >= 0) && line_def->next) {
1197 line_index += line_def->length;
1198 line_def = line_def->next;
1199 index -= line_def->length;
1201 } else {
1202 while (line > 0) {
1203 line_index += line_def->length;
1204 line_def = line_def->next;
1205 line--;
1208 ssa = line_def->ssa;
1210 else
1212 line_index = 0;
1213 rc->top = es->format_rect.top;
1214 ssa = es->ssa;
1217 rc->bottom = rc->top + es->line_height;
1218 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1219 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1220 if (ssa)
1222 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1223 pt3+=es->format_rect.left;
1225 else pt3 = pt1;
1226 rc->right = max(max(pt1 , pt2),pt3);
1227 rc->left = min(min(pt1, pt2),pt3);
1231 static inline void text_buffer_changed(EDITSTATE *es)
1233 es->text_length = (UINT)-1;
1235 HeapFree( GetProcessHeap(), 0, es->logAttr );
1236 es->logAttr = NULL;
1237 EDIT_InvalidateUniscribeData(es);
1240 /*********************************************************************
1241 * EDIT_LockBuffer
1244 static void EDIT_LockBuffer(EDITSTATE *es)
1246 if (!es->text) {
1248 if(!es->hloc32W) return;
1250 if(es->hloc32A)
1252 CHAR *textA = LocalLock(es->hloc32A);
1253 HLOCAL hloc32W_new;
1254 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1255 if(countW_new > es->buffer_size + 1)
1257 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1258 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1259 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1260 if(hloc32W_new)
1262 es->hloc32W = hloc32W_new;
1263 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1264 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1266 else
1267 WARN("FAILED! Will synchronize partially\n");
1269 es->text = LocalLock(es->hloc32W);
1270 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1271 LocalUnlock(es->hloc32A);
1273 else es->text = LocalLock(es->hloc32W);
1275 es->lock_count++;
1279 /*********************************************************************
1281 * EDIT_UnlockBuffer
1284 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1286 /* Edit window might be already destroyed */
1287 if(!IsWindow(es->hwndSelf))
1289 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1290 return;
1293 if (!es->lock_count) {
1294 ERR("lock_count == 0 ... please report\n");
1295 return;
1297 if (!es->text) {
1298 ERR("es->text == 0 ... please report\n");
1299 return;
1301 if (force || (es->lock_count == 1)) {
1302 if (es->hloc32W) {
1303 UINT countA = 0;
1304 UINT countW = get_text_length(es) + 1;
1306 if(es->hloc32A)
1308 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1309 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1310 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1311 countA = LocalSize(es->hloc32A);
1312 if(countA_new > countA)
1314 HLOCAL hloc32A_new;
1315 UINT alloc_size = ROUND_TO_GROW(countA_new);
1316 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1317 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1318 if(hloc32A_new)
1320 es->hloc32A = hloc32A_new;
1321 countA = LocalSize(hloc32A_new);
1322 TRACE("Real new size %d bytes\n", countA);
1324 else
1325 WARN("FAILED! Will synchronize partially\n");
1327 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1328 LocalLock(es->hloc32A), countA, NULL, NULL);
1329 LocalUnlock(es->hloc32A);
1332 LocalUnlock(es->hloc32W);
1333 es->text = NULL;
1335 else {
1336 ERR("no buffer ... please report\n");
1337 return;
1340 es->lock_count--;
1344 /*********************************************************************
1346 * EDIT_MakeFit
1348 * Try to fit size + 1 characters in the buffer.
1350 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1352 HLOCAL hNew32W;
1354 if (size <= es->buffer_size)
1355 return TRUE;
1357 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1359 /* Force edit to unlock its buffer. es->text now NULL */
1360 EDIT_UnlockBuffer(es, TRUE);
1362 if (es->hloc32W) {
1363 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1364 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1365 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1366 es->hloc32W = hNew32W;
1367 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1371 EDIT_LockBuffer(es);
1373 if (es->buffer_size < size) {
1374 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1375 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1376 return FALSE;
1377 } else {
1378 TRACE("We now have %d+1\n", es->buffer_size);
1379 return TRUE;
1384 /*********************************************************************
1386 * EDIT_MakeUndoFit
1388 * Try to fit size + 1 bytes in the undo buffer.
1391 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1393 UINT alloc_size;
1395 if (size <= es->undo_buffer_size)
1396 return TRUE;
1398 TRACE("trying to ReAlloc to %d+1\n", size);
1400 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1401 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1402 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1403 return TRUE;
1405 else
1407 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1408 return FALSE;
1413 /*********************************************************************
1415 * EDIT_UpdateTextRegion
1418 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1420 if (es->flags & EF_UPDATE) {
1421 es->flags &= ~EF_UPDATE;
1422 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1424 NtUserInvalidateRgn(es->hwndSelf, hrgn, bErase);
1428 /*********************************************************************
1430 * EDIT_UpdateText
1433 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1435 if (es->flags & EF_UPDATE) {
1436 es->flags &= ~EF_UPDATE;
1437 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1439 NtUserInvalidateRect(es->hwndSelf, rc, bErase);
1442 /*********************************************************************
1444 * EDIT_SL_InvalidateText
1446 * Called from EDIT_InvalidateText().
1447 * Does the job for single-line controls only.
1450 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1452 RECT line_rect;
1453 RECT rc;
1455 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1456 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1457 EDIT_UpdateText(es, &rc, TRUE);
1460 /*********************************************************************
1462 * EDIT_ML_InvalidateText
1464 * Called from EDIT_InvalidateText().
1465 * Does the job for multi-line controls only.
1468 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1470 INT vlc = get_vertical_line_count(es);
1471 INT sl = EDIT_EM_LineFromChar(es, start);
1472 INT el = EDIT_EM_LineFromChar(es, end);
1473 INT sc;
1474 INT ec;
1475 RECT rc1;
1476 RECT rcWnd;
1477 RECT rcLine;
1478 RECT rcUpdate;
1479 INT l;
1481 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1482 return;
1484 sc = start - EDIT_EM_LineIndex(es, sl);
1485 ec = end - EDIT_EM_LineIndex(es, el);
1486 if (sl < es->y_offset) {
1487 sl = es->y_offset;
1488 sc = 0;
1490 if (el > es->y_offset + vlc) {
1491 el = es->y_offset + vlc;
1492 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1494 GetClientRect(es->hwndSelf, &rc1);
1495 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1496 if (sl == el) {
1497 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1498 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1499 EDIT_UpdateText(es, &rcUpdate, TRUE);
1500 } else {
1501 EDIT_GetLineRect(es, sl, sc,
1502 EDIT_EM_LineLength(es,
1503 EDIT_EM_LineIndex(es, sl)),
1504 &rcLine);
1505 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1506 EDIT_UpdateText(es, &rcUpdate, TRUE);
1507 for (l = sl + 1 ; l < el ; l++) {
1508 EDIT_GetLineRect(es, l, 0,
1509 EDIT_EM_LineLength(es,
1510 EDIT_EM_LineIndex(es, l)),
1511 &rcLine);
1512 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1513 EDIT_UpdateText(es, &rcUpdate, TRUE);
1515 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1516 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1517 EDIT_UpdateText(es, &rcUpdate, TRUE);
1522 /*********************************************************************
1524 * EDIT_InvalidateText
1526 * Invalidate the text from offset start up to, but not including,
1527 * offset end. Useful for (re)painting the selection.
1528 * Regions outside the linewidth are not invalidated.
1529 * end == -1 means end == TextLength.
1530 * start and end need not be ordered.
1533 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1535 if (end == start)
1536 return;
1538 if (end == -1)
1539 end = get_text_length(es);
1541 if (end < start) {
1542 INT tmp = start;
1543 start = end;
1544 end = tmp;
1547 if (es->style & ES_MULTILINE)
1548 EDIT_ML_InvalidateText(es, start, end);
1549 else
1550 EDIT_SL_InvalidateText(es, start, end);
1554 /*********************************************************************
1556 * EDIT_EM_SetSel
1558 * note: unlike the specs say: the order of start and end
1559 * _is_ preserved in Windows. (i.e. start can be > end)
1560 * In other words: this handler is OK
1563 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1565 UINT old_start = es->selection_start;
1566 UINT old_end = es->selection_end;
1567 UINT len = get_text_length(es);
1569 if (start == (UINT)-1) {
1570 start = es->selection_end;
1571 end = es->selection_end;
1572 } else {
1573 start = min(start, len);
1574 end = min(end, len);
1576 es->selection_start = start;
1577 es->selection_end = end;
1578 if (after_wrap)
1579 es->flags |= EF_AFTER_WRAP;
1580 else
1581 es->flags &= ~EF_AFTER_WRAP;
1582 /* Compute the necessary invalidation region. */
1583 /* Note that we don't need to invalidate regions which have
1584 * "never" been selected, or those which are "still" selected.
1585 * In fact, every time we hit a selection boundary, we can
1586 * *toggle* whether we need to invalidate. Thus we can optimize by
1587 * *sorting* the interval endpoints. Let's assume that we sort them
1588 * in this order:
1589 * start <= end <= old_start <= old_end
1590 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1591 * in 5 comparisons; i.e. it is impossible to do better than the
1592 * following: */
1593 ORDER_UINT(end, old_end);
1594 ORDER_UINT(start, old_start);
1595 ORDER_UINT(old_start, old_end);
1596 ORDER_UINT(start, end);
1597 /* Note that at this point 'end' and 'old_start' are not in order, but
1598 * start is definitely the min. and old_end is definitely the max. */
1599 if (end != old_start)
1602 * One can also do
1603 * ORDER_UINT32(end, old_start);
1604 * EDIT_InvalidateText(es, start, end);
1605 * EDIT_InvalidateText(es, old_start, old_end);
1606 * in place of the following if statement.
1607 * (That would complete the optimal five-comparison four-element sort.)
1609 if (old_start > end )
1611 EDIT_InvalidateText(es, start, end);
1612 EDIT_InvalidateText(es, old_start, old_end);
1614 else
1616 EDIT_InvalidateText(es, start, old_start);
1617 EDIT_InvalidateText(es, end, old_end);
1620 else EDIT_InvalidateText(es, start, old_end);
1624 /*********************************************************************
1626 * EDIT_UpdateScrollInfo
1629 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1631 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1633 SCROLLINFO si;
1634 si.cbSize = sizeof(SCROLLINFO);
1635 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1636 si.nMin = 0;
1637 si.nMax = es->line_count - 1;
1638 si.nPage = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
1639 si.nPos = es->y_offset;
1640 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1641 si.nMin, si.nMax, si.nPage, si.nPos);
1642 NtUserSetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1645 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1647 SCROLLINFO si;
1648 si.cbSize = sizeof(SCROLLINFO);
1649 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1650 si.nMin = 0;
1651 si.nMax = es->text_width - 1;
1652 si.nPage = es->format_rect.right - es->format_rect.left;
1653 si.nPos = es->x_offset;
1654 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1655 si.nMin, si.nMax, si.nPage, si.nPos);
1656 NtUserSetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1661 /*********************************************************************
1663 * EDIT_EM_LineScroll_internal
1665 * Version of EDIT_EM_LineScroll for internal use.
1666 * It doesn't refuse if ES_MULTILINE is set and assumes that
1667 * dx is in pixels, dy - in lines.
1670 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1672 INT nyoff;
1673 INT x_offset_in_pixels;
1674 INT lines_per_page;
1676 if (!es->line_height || !es->char_width)
1677 return TRUE;
1679 lines_per_page = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1681 if (es->style & ES_MULTILINE)
1683 x_offset_in_pixels = es->x_offset;
1685 else
1687 dy = 0;
1688 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1691 if (-dx > x_offset_in_pixels)
1692 dx = -x_offset_in_pixels;
1693 if (dx > es->text_width - x_offset_in_pixels)
1694 dx = es->text_width - x_offset_in_pixels;
1695 nyoff = max(0, es->y_offset + dy);
1696 if (nyoff >= es->line_count - lines_per_page)
1697 nyoff = max(0, es->line_count - lines_per_page);
1698 dy = (es->y_offset - nyoff) * es->line_height;
1699 if (dx || dy) {
1700 RECT rc1;
1701 RECT rc;
1703 es->y_offset = nyoff;
1704 if(es->style & ES_MULTILINE)
1705 es->x_offset += dx;
1706 else
1707 es->x_offset += dx / es->char_width;
1709 GetClientRect(es->hwndSelf, &rc1);
1710 IntersectRect(&rc, &rc1, &es->format_rect);
1711 NtUserScrollWindowEx(es->hwndSelf, -dx, dy,
1712 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1713 /* force scroll info update */
1714 EDIT_UpdateScrollInfo(es);
1716 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1717 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1718 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1719 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1720 return TRUE;
1723 /*********************************************************************
1725 * EM_LINESCROLL
1727 * NOTE: dx is in average character widths, dy - in lines;
1730 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1732 if (!(es->style & ES_MULTILINE))
1733 return FALSE;
1735 dx *= es->char_width;
1736 return EDIT_EM_LineScroll_internal(es, dx, dy);
1740 /*********************************************************************
1742 * EM_SCROLL
1745 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1747 INT dy;
1749 if (!(es->style & ES_MULTILINE))
1750 return (LRESULT)FALSE;
1752 dy = 0;
1754 switch (action) {
1755 case SB_LINEUP:
1756 if (es->y_offset)
1757 dy = -1;
1758 break;
1759 case SB_LINEDOWN:
1760 if (es->y_offset < es->line_count - 1)
1761 dy = 1;
1762 break;
1763 case SB_PAGEUP:
1764 if (es->y_offset)
1765 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1766 break;
1767 case SB_PAGEDOWN:
1768 if (es->y_offset < es->line_count - 1)
1769 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1770 break;
1771 default:
1772 return (LRESULT)FALSE;
1774 if (dy) {
1775 INT vlc = get_vertical_line_count(es);
1776 /* check if we are going to move too far */
1777 if(es->y_offset + dy > es->line_count - vlc)
1778 dy = max(es->line_count - vlc, 0) - es->y_offset;
1780 /* Notification is done in EDIT_EM_LineScroll */
1781 if(dy) {
1782 EDIT_EM_LineScroll(es, 0, dy);
1783 return MAKELONG(dy, TRUE);
1787 return (LRESULT)FALSE;
1791 /*********************************************************************
1793 * EDIT_SetCaretPos
1796 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1797 BOOL after_wrap)
1799 LRESULT res;
1801 if (es->flags & EF_FOCUSED)
1803 res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1804 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1805 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1810 /*********************************************************************
1812 * EM_SCROLLCARET
1815 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1817 if (es->style & ES_MULTILINE) {
1818 INT l;
1819 INT vlc;
1820 INT ww;
1821 INT cw = es->char_width;
1822 INT x;
1823 INT dy = 0;
1824 INT dx = 0;
1826 l = EDIT_EM_LineFromChar(es, es->selection_end);
1827 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1828 vlc = get_vertical_line_count(es);
1829 if (l >= es->y_offset + vlc)
1830 dy = l - vlc + 1 - es->y_offset;
1831 if (l < es->y_offset)
1832 dy = l - es->y_offset;
1833 ww = es->format_rect.right - es->format_rect.left;
1834 if (x < es->format_rect.left)
1835 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1836 if (x > es->format_rect.right)
1837 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1838 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1840 /* check if we are going to move too far */
1841 if(es->x_offset + dx + ww > es->text_width)
1842 dx = es->text_width - ww - es->x_offset;
1843 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1844 EDIT_EM_LineScroll_internal(es, dx, dy);
1846 } else {
1847 INT x;
1848 INT goal;
1849 INT format_width;
1851 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1852 format_width = es->format_rect.right - es->format_rect.left;
1853 if (x < es->format_rect.left) {
1854 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1855 do {
1856 es->x_offset--;
1857 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1858 } while ((x < goal) && es->x_offset);
1859 /* FIXME: use ScrollWindow() somehow to improve performance */
1860 EDIT_UpdateText(es, NULL, TRUE);
1861 } else if (x > es->format_rect.right) {
1862 INT x_last;
1863 INT len = get_text_length(es);
1864 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1865 do {
1866 es->x_offset++;
1867 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1868 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1869 } while ((x > goal) && (x_last > es->format_rect.right));
1870 /* FIXME: use ScrollWindow() somehow to improve performance */
1871 EDIT_UpdateText(es, NULL, TRUE);
1875 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1879 /*********************************************************************
1881 * EDIT_MoveBackward
1884 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1886 INT e = es->selection_end;
1888 if (e) {
1889 e--;
1890 if ((es->style & ES_MULTILINE) && e &&
1891 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1892 e--;
1893 if (e && (es->text[e - 1] == '\r'))
1894 e--;
1897 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1898 EDIT_EM_ScrollCaret(es);
1902 /*********************************************************************
1904 * EDIT_MoveDown_ML
1906 * Only for multi line controls
1907 * Move the caret one line down, on a column with the nearest
1908 * x coordinate on the screen (might be a different column).
1911 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1913 INT s = es->selection_start;
1914 INT e = es->selection_end;
1915 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1916 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1917 INT x = (short)LOWORD(pos);
1918 INT y = (short)HIWORD(pos);
1920 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1921 if (!extend)
1922 s = e;
1923 EDIT_EM_SetSel(es, s, e, after_wrap);
1924 EDIT_EM_ScrollCaret(es);
1928 /*********************************************************************
1930 * EDIT_MoveEnd
1933 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1935 BOOL after_wrap = FALSE;
1936 INT e;
1938 /* Pass a high value in x to make sure of receiving the end of the line */
1939 if (!ctrl && (es->style & ES_MULTILINE))
1940 e = EDIT_CharFromPos(es, 0x3fffffff,
1941 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1942 else
1943 e = get_text_length(es);
1944 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1945 EDIT_EM_ScrollCaret(es);
1949 /*********************************************************************
1951 * EDIT_MoveForward
1954 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1956 INT e = es->selection_end;
1958 if (es->text[e]) {
1959 e++;
1960 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1961 if (es->text[e] == '\n')
1962 e++;
1963 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1964 e += 2;
1967 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1968 EDIT_EM_ScrollCaret(es);
1972 /*********************************************************************
1974 * EDIT_MoveHome
1976 * Home key: move to beginning of line.
1979 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1981 INT e;
1983 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1984 if (!ctrl && (es->style & ES_MULTILINE))
1985 e = EDIT_CharFromPos(es, -es->x_offset,
1986 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1987 else
1988 e = 0;
1989 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1990 EDIT_EM_ScrollCaret(es);
1994 /*********************************************************************
1996 * EDIT_MovePageDown_ML
1998 * Only for multi line controls
1999 * Move the caret one page down, on a column with the nearest
2000 * x coordinate on the screen (might be a different column).
2003 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2005 INT s = es->selection_start;
2006 INT e = es->selection_end;
2007 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2008 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2009 INT x = (short)LOWORD(pos);
2010 INT y = (short)HIWORD(pos);
2012 e = EDIT_CharFromPos(es, x,
2013 y + (es->format_rect.bottom - es->format_rect.top),
2014 &after_wrap);
2015 if (!extend)
2016 s = e;
2017 EDIT_EM_SetSel(es, s, e, after_wrap);
2018 EDIT_EM_ScrollCaret(es);
2022 /*********************************************************************
2024 * EDIT_MovePageUp_ML
2026 * Only for multi line controls
2027 * Move the caret one page up, on a column with the nearest
2028 * x coordinate on the screen (might be a different column).
2031 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2033 INT s = es->selection_start;
2034 INT e = es->selection_end;
2035 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2036 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2037 INT x = (short)LOWORD(pos);
2038 INT y = (short)HIWORD(pos);
2040 e = EDIT_CharFromPos(es, x,
2041 y - (es->format_rect.bottom - es->format_rect.top),
2042 &after_wrap);
2043 if (!extend)
2044 s = e;
2045 EDIT_EM_SetSel(es, s, e, after_wrap);
2046 EDIT_EM_ScrollCaret(es);
2050 /*********************************************************************
2052 * EDIT_MoveUp_ML
2054 * Only for multi line controls
2055 * Move the caret one line up, on a column with the nearest
2056 * x coordinate on the screen (might be a different column).
2059 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2061 INT s = es->selection_start;
2062 INT e = es->selection_end;
2063 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2064 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2065 INT x = (short)LOWORD(pos);
2066 INT y = (short)HIWORD(pos);
2068 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2069 if (!extend)
2070 s = e;
2071 EDIT_EM_SetSel(es, s, e, after_wrap);
2072 EDIT_EM_ScrollCaret(es);
2076 /*********************************************************************
2078 * EDIT_MoveWordBackward
2081 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2083 INT s = es->selection_start;
2084 INT e = es->selection_end;
2085 INT l;
2086 INT ll;
2087 INT li;
2089 l = EDIT_EM_LineFromChar(es, e);
2090 ll = EDIT_EM_LineLength(es, e);
2091 li = EDIT_EM_LineIndex(es, l);
2092 if (e - li == 0) {
2093 if (l) {
2094 li = EDIT_EM_LineIndex(es, l - 1);
2095 e = li + EDIT_EM_LineLength(es, li);
2097 } else {
2098 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2100 if (!extend)
2101 s = e;
2102 EDIT_EM_SetSel(es, s, e, FALSE);
2103 EDIT_EM_ScrollCaret(es);
2107 /*********************************************************************
2109 * EDIT_MoveWordForward
2112 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2114 INT s = es->selection_start;
2115 INT e = es->selection_end;
2116 INT l;
2117 INT ll;
2118 INT li;
2120 l = EDIT_EM_LineFromChar(es, e);
2121 ll = EDIT_EM_LineLength(es, e);
2122 li = EDIT_EM_LineIndex(es, l);
2123 if (e - li == ll) {
2124 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2125 e = EDIT_EM_LineIndex(es, l + 1);
2126 } else {
2127 e = li + EDIT_CallWordBreakProc(es,
2128 li, e - li + 1, ll, WB_RIGHT);
2130 if (!extend)
2131 s = e;
2132 EDIT_EM_SetSel(es, s, e, FALSE);
2133 EDIT_EM_ScrollCaret(es);
2137 /*********************************************************************
2139 * EDIT_PaintText
2142 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2144 COLORREF BkColor;
2145 COLORREF TextColor;
2146 LOGFONTW underline_font;
2147 HFONT hUnderline = 0;
2148 HFONT old_font = 0;
2149 INT ret;
2150 INT li;
2151 INT BkMode;
2152 SIZE size;
2154 if (!count)
2155 return 0;
2156 BkMode = GetBkMode(dc);
2157 BkColor = GetBkColor(dc);
2158 TextColor = GetTextColor(dc);
2159 if (rev) {
2160 if (es->composition_len == 0)
2162 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2163 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2164 SetBkMode( dc, OPAQUE);
2166 else
2168 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2169 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2170 underline_font.lfUnderline = TRUE;
2171 hUnderline = CreateFontIndirectW(&underline_font);
2172 old_font = SelectObject(dc,hUnderline);
2175 li = EDIT_EM_LineIndex(es, line);
2176 if (es->style & ES_MULTILINE) {
2177 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2178 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2179 } else {
2180 TextOutW(dc, x, y, es->text + li + col, count);
2181 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2182 ret = size.cx;
2184 if (rev) {
2185 if (es->composition_len == 0)
2187 SetBkColor(dc, BkColor);
2188 SetTextColor(dc, TextColor);
2189 SetBkMode( dc, BkMode);
2191 else
2193 if (old_font)
2194 SelectObject(dc,old_font);
2195 if (hUnderline)
2196 DeleteObject(hUnderline);
2199 return ret;
2203 /*********************************************************************
2205 * EDIT_PaintLine
2208 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2210 INT s = 0;
2211 INT e = 0;
2212 INT li = 0;
2213 INT ll = 0;
2214 INT x;
2215 INT y;
2216 LRESULT pos;
2217 SCRIPT_STRING_ANALYSIS ssa;
2219 if (es->style & ES_MULTILINE) {
2220 INT vlc = get_vertical_line_count(es);
2222 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2223 return;
2224 } else if (line)
2225 return;
2227 TRACE("line=%d\n", line);
2229 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2230 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2231 x = (short)LOWORD(pos);
2232 y = (short)HIWORD(pos);
2234 if (es->style & ES_MULTILINE)
2236 int line_idx = line;
2237 x = -es->x_offset;
2238 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2240 LINEDEF *line_def = es->first_line_def;
2241 int w, lw;
2243 while (line_def && line_idx)
2245 line_def = line_def->next;
2246 line_idx--;
2248 w = es->format_rect.right - es->format_rect.left;
2249 lw = line_def->width;
2251 if (es->style & ES_RIGHT)
2252 x = w - (lw - x);
2253 else if (es->style & ES_CENTER)
2254 x += (w - lw) / 2;
2256 x += es->format_rect.left;
2259 if (rev)
2261 li = EDIT_EM_LineIndex(es, line);
2262 ll = EDIT_EM_LineLength(es, li);
2263 s = min(es->selection_start, es->selection_end);
2264 e = max(es->selection_start, es->selection_end);
2265 s = min(li + ll, max(li, s));
2266 e = min(li + ll, max(li, e));
2269 if (ssa)
2270 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2271 else if (rev && (s != e) &&
2272 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2273 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2274 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2275 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2276 } else
2277 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2281 /*********************************************************************
2283 * EDIT_AdjustFormatRect
2285 * Adjusts the format rectangle for the current font and the
2286 * current client rectangle.
2289 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2291 RECT ClientRect;
2293 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2294 if (es->style & ES_MULTILINE)
2296 INT fw, vlc, max_x_offset, max_y_offset;
2298 vlc = get_vertical_line_count(es);
2299 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2301 /* correct es->x_offset */
2302 fw = es->format_rect.right - es->format_rect.left;
2303 max_x_offset = es->text_width - fw;
2304 if(max_x_offset < 0) max_x_offset = 0;
2305 if(es->x_offset > max_x_offset)
2306 es->x_offset = max_x_offset;
2308 /* correct es->y_offset */
2309 max_y_offset = es->line_count - vlc;
2310 if(max_y_offset < 0) max_y_offset = 0;
2311 if(es->y_offset > max_y_offset)
2312 es->y_offset = max_y_offset;
2314 /* force scroll info update */
2315 EDIT_UpdateScrollInfo(es);
2317 else
2318 /* Windows doesn't care to fix text placement for SL controls */
2319 es->format_rect.bottom = es->format_rect.top + es->line_height;
2321 /* Always stay within the client area */
2322 GetClientRect(es->hwndSelf, &ClientRect);
2323 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2325 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2326 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2328 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2332 /*********************************************************************
2334 * EDIT_SetRectNP
2336 * note: this is not (exactly) the handler called on EM_SETRECTNP
2337 * it is also used to set the rect of a single line control
2340 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2342 LONG_PTR ExStyle;
2343 INT bw, bh;
2344 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2346 CopyRect(&es->format_rect, rc);
2348 if (ExStyle & WS_EX_CLIENTEDGE) {
2349 es->format_rect.left++;
2350 es->format_rect.right--;
2352 if (es->format_rect.bottom - es->format_rect.top
2353 >= es->line_height + 2)
2355 es->format_rect.top++;
2356 es->format_rect.bottom--;
2359 else if (es->style & WS_BORDER) {
2360 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2361 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2362 InflateRect(&es->format_rect, -bw, 0);
2363 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2364 InflateRect(&es->format_rect, 0, -bh);
2367 es->format_rect.left += es->left_margin;
2368 es->format_rect.right -= es->right_margin;
2369 EDIT_AdjustFormatRect(es);
2373 /*********************************************************************
2375 * EM_CHARFROMPOS
2377 * returns line number (not index) in high-order word of result.
2378 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2379 * to Richedit, not to the edit control. Original documentation is valid.
2380 * FIXME: do the specs mean to return -1 if outside client area or
2381 * if outside formatting rectangle ???
2384 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2386 POINT pt;
2387 RECT rc;
2388 INT index;
2390 pt.x = x;
2391 pt.y = y;
2392 GetClientRect(es->hwndSelf, &rc);
2393 if (!PtInRect(&rc, pt))
2394 return -1;
2396 index = EDIT_CharFromPos(es, x, y, NULL);
2397 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2401 /*********************************************************************
2403 * EM_FMTLINES
2405 * Enable or disable soft breaks.
2407 * This means: insert or remove the soft linebreak character (\r\r\n).
2408 * Take care to check if the text still fits the buffer after insertion.
2409 * If not, notify with EN_ERRSPACE.
2412 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2414 es->flags &= ~EF_USE_SOFTBRK;
2415 if (add_eol) {
2416 es->flags |= EF_USE_SOFTBRK;
2417 FIXME("soft break enabled, not implemented\n");
2419 return add_eol;
2423 /*********************************************************************
2425 * EM_GETHANDLE
2427 * Hopefully this won't fire back at us.
2428 * We always start with a fixed buffer in the local heap.
2429 * Despite of the documentation says that the local heap is used
2430 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2431 * buffer on the local heap.
2434 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2436 HLOCAL hLocal;
2438 if (!(es->style & ES_MULTILINE))
2439 return 0;
2441 if(es->is_unicode)
2442 hLocal = es->hloc32W;
2443 else
2445 if(!es->hloc32A)
2447 CHAR *textA;
2448 UINT countA, alloc_size;
2449 TRACE("Allocating 32-bit ANSI alias buffer\n");
2450 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2451 alloc_size = ROUND_TO_GROW(countA);
2452 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2454 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2455 return 0;
2457 textA = LocalLock(es->hloc32A);
2458 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2459 LocalUnlock(es->hloc32A);
2461 hLocal = es->hloc32A;
2464 EDIT_UnlockBuffer(es, TRUE);
2466 /* The text buffer handle belongs to the app */
2467 es->hlocapp = hLocal;
2469 TRACE("Returning %p, LocalSize() = %Id\n", hLocal, LocalSize(hLocal));
2470 return hLocal;
2474 /*********************************************************************
2476 * EM_GETLINE
2479 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2481 LPWSTR src;
2482 INT line_len, dst_len;
2483 INT i;
2485 if (es->style & ES_MULTILINE) {
2486 if (line >= es->line_count)
2487 return 0;
2488 } else
2489 line = 0;
2490 i = EDIT_EM_LineIndex(es, line);
2491 src = es->text + i;
2492 line_len = EDIT_EM_LineLength(es, i);
2493 dst_len = *(WORD *)dst;
2494 if(unicode)
2496 if(dst_len <= line_len)
2498 memcpy(dst, src, dst_len * sizeof(WCHAR));
2499 return dst_len;
2501 else /* Append 0 if enough space */
2503 memcpy(dst, src, line_len * sizeof(WCHAR));
2504 dst[line_len] = 0;
2505 return line_len;
2508 else
2510 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2511 if(!ret && line_len) /* Insufficient buffer size */
2512 return dst_len;
2513 if(ret < dst_len) /* Append 0 if enough space */
2514 ((LPSTR)dst)[ret] = 0;
2515 return ret;
2520 /*********************************************************************
2522 * EM_GETSEL
2525 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2527 UINT s = es->selection_start;
2528 UINT e = es->selection_end;
2530 ORDER_UINT(s, e);
2531 if (start)
2532 *start = s;
2533 if (end)
2534 *end = e;
2535 return MAKELONG(s, e);
2539 /*********************************************************************
2541 * EM_REPLACESEL
2543 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2546 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2547 BOOL send_update, BOOL honor_limit)
2549 UINT tl = get_text_length(es);
2550 UINT utl;
2551 UINT s;
2552 UINT e;
2553 UINT i;
2554 UINT size;
2555 LPWSTR p;
2556 HRGN hrgn = 0;
2557 LPWSTR buf = NULL;
2558 UINT bufl;
2560 TRACE("%s, can_undo %d, send_update %d\n",
2561 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2563 s = es->selection_start;
2564 e = es->selection_end;
2566 EDIT_InvalidateUniscribeData(es);
2567 if ((s == e) && !strl)
2568 return;
2570 ORDER_UINT(s, e);
2572 size = tl - (e - s) + strl;
2573 if (!size)
2574 es->text_width = 0;
2576 /* Issue the EN_MAXTEXT notification and continue with replacing text
2577 * so that buffer limit is honored. */
2578 if ((honor_limit) && (size > es->buffer_limit)) {
2579 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2580 /* Buffer limit can be smaller than the actual length of text in combobox */
2581 if (es->buffer_limit < (tl - (e-s)))
2582 strl = 0;
2583 else
2584 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2587 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2588 return;
2590 if (e != s) {
2591 /* there is something to be deleted */
2592 TRACE("deleting stuff.\n");
2593 bufl = e - s;
2594 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2595 if (!buf) return;
2596 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2597 buf[bufl] = 0; /* ensure 0 termination */
2598 /* now delete */
2599 lstrcpyW(es->text + s, es->text + e);
2600 text_buffer_changed(es);
2602 if (strl) {
2603 /* there is an insertion */
2604 tl = get_text_length(es);
2605 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));
2606 for (p = es->text + tl ; p >= es->text + s ; p--)
2607 p[strl] = p[0];
2608 for (i = 0 , p = es->text + s ; i < strl ; i++)
2609 p[i] = lpsz_replace[i];
2610 if(es->style & ES_UPPERCASE)
2611 CharUpperBuffW(p, strl);
2612 else if(es->style & ES_LOWERCASE)
2613 CharLowerBuffW(p, strl);
2614 text_buffer_changed(es);
2616 if (es->style & ES_MULTILINE)
2618 INT st = min(es->selection_start, es->selection_end);
2619 INT vlc = get_vertical_line_count(es);
2621 hrgn = CreateRectRgn(0, 0, 0, 0);
2622 EDIT_BuildLineDefs_ML(es, st, st + strl,
2623 strl - abs(es->selection_end - es->selection_start), hrgn);
2624 /* if text is too long undo all changes */
2625 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2626 if (strl)
2627 lstrcpyW(es->text + e, es->text + e + strl);
2628 if (e != s)
2629 for (i = 0 , p = es->text ; i < e - s ; i++)
2630 p[i + s] = buf[i];
2631 text_buffer_changed(es);
2632 EDIT_BuildLineDefs_ML(es, s, e,
2633 abs(es->selection_end - es->selection_start) - strl, hrgn);
2634 strl = 0;
2635 e = s;
2636 SetRectRgn(hrgn, 0, 0, 0, 0);
2637 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2640 else {
2641 INT fw = es->format_rect.right - es->format_rect.left;
2642 EDIT_InvalidateUniscribeData(es);
2643 EDIT_CalcLineWidth_SL(es);
2644 /* remove chars that don't fit */
2645 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2646 while ((es->text_width > fw) && s + strl >= s) {
2647 lstrcpyW(es->text + s + strl - 1, es->text + s + strl);
2648 strl--;
2649 es->text_length = -1;
2650 EDIT_InvalidateUniscribeData(es);
2651 EDIT_CalcLineWidth_SL(es);
2653 text_buffer_changed(es);
2654 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2658 if (e != s) {
2659 if (can_undo) {
2660 utl = lstrlenW(es->undo_text);
2661 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2662 /* undo-buffer is extended to the right */
2663 EDIT_MakeUndoFit(es, utl + e - s);
2664 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2665 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2666 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2667 /* undo-buffer is extended to the left */
2668 EDIT_MakeUndoFit(es, utl + e - s);
2669 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2670 p[e - s] = p[0];
2671 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2672 p[i] = buf[i];
2673 es->undo_position = s;
2674 } else {
2675 /* new undo-buffer */
2676 EDIT_MakeUndoFit(es, e - s);
2677 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2678 es->undo_text[e - s] = 0; /* ensure 0 termination */
2679 es->undo_position = s;
2681 /* any deletion makes the old insertion-undo invalid */
2682 es->undo_insert_count = 0;
2683 } else
2684 EDIT_EM_EmptyUndoBuffer(es);
2686 if (strl) {
2687 if (can_undo) {
2688 if ((s == es->undo_position) ||
2689 ((es->undo_insert_count) &&
2690 (s == es->undo_position + es->undo_insert_count)))
2692 * insertion is new and at delete position or
2693 * an extension to either left or right
2695 es->undo_insert_count += strl;
2696 else {
2697 /* new insertion undo */
2698 es->undo_position = s;
2699 es->undo_insert_count = strl;
2700 /* new insertion makes old delete-buffer invalid */
2701 *es->undo_text = '\0';
2703 } else
2704 EDIT_EM_EmptyUndoBuffer(es);
2707 HeapFree(GetProcessHeap(), 0, buf);
2709 s += strl;
2711 /* If text has been deleted and we're right or center aligned then scroll rightward */
2712 if (es->style & (ES_RIGHT | ES_CENTER))
2714 INT delta = strl - abs(es->selection_end - es->selection_start);
2716 if (delta < 0 && es->x_offset)
2718 if (abs(delta) > es->x_offset)
2719 es->x_offset = 0;
2720 else
2721 es->x_offset += delta;
2725 EDIT_EM_SetSel(es, s, s, FALSE);
2726 es->flags |= EF_MODIFIED;
2727 if (send_update) es->flags |= EF_UPDATE;
2728 if (hrgn)
2730 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2731 DeleteObject(hrgn);
2733 else
2734 EDIT_UpdateText(es, NULL, TRUE);
2736 EDIT_EM_ScrollCaret(es);
2738 /* force scroll info update */
2739 EDIT_UpdateScrollInfo(es);
2742 if(send_update || (es->flags & EF_UPDATE))
2744 es->flags &= ~EF_UPDATE;
2745 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2747 EDIT_InvalidateUniscribeData(es);
2751 /*********************************************************************
2753 * EM_SETHANDLE
2755 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2758 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2760 if (!(es->style & ES_MULTILINE))
2761 return;
2763 if (!hloc) {
2764 WARN("called with NULL handle\n");
2765 return;
2768 EDIT_UnlockBuffer(es, TRUE);
2770 if(es->is_unicode)
2772 if(es->hloc32A)
2774 LocalFree(es->hloc32A);
2775 es->hloc32A = NULL;
2777 es->hloc32W = hloc;
2779 else
2781 INT countW, countA;
2782 HLOCAL hloc32W_new;
2783 WCHAR *textW;
2784 CHAR *textA;
2786 countA = LocalSize(hloc);
2787 textA = LocalLock(hloc);
2788 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2789 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2791 ERR("Could not allocate new unicode buffer\n");
2792 return;
2794 textW = LocalLock(hloc32W_new);
2795 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2796 LocalUnlock(hloc32W_new);
2797 LocalUnlock(hloc);
2799 if(es->hloc32W)
2800 LocalFree(es->hloc32W);
2802 es->hloc32W = hloc32W_new;
2803 es->hloc32A = hloc;
2806 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2808 /* The text buffer handle belongs to the control */
2809 es->hlocapp = NULL;
2811 EDIT_LockBuffer(es);
2812 text_buffer_changed(es);
2814 es->x_offset = es->y_offset = 0;
2815 es->selection_start = es->selection_end = 0;
2816 EDIT_EM_EmptyUndoBuffer(es);
2817 es->flags &= ~EF_MODIFIED;
2818 es->flags &= ~EF_UPDATE;
2819 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2820 EDIT_UpdateText(es, NULL, TRUE);
2821 EDIT_EM_ScrollCaret(es);
2822 /* force scroll info update */
2823 EDIT_UpdateScrollInfo(es);
2827 /*********************************************************************
2829 * EM_SETLIMITTEXT
2831 * NOTE: this version currently implements WinNT limits
2834 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2836 if (!limit) limit = ~0u;
2837 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2838 es->buffer_limit = limit;
2841 static BOOL is_cjk_charset(HDC dc)
2843 switch (GdiGetCodePage(dc)) {
2844 case 932: case 936: case 949: case 950: case 1361:
2845 return TRUE;
2846 default:
2847 return FALSE;
2851 static BOOL is_cjk_font(HDC dc)
2853 const DWORD FS_DBCS_MASK = FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB;
2854 FONTSIGNATURE fs;
2855 return (GetTextCharsetInfo(dc, &fs, 0) != DEFAULT_CHARSET &&
2856 (fs.fsCsb[0] & FS_DBCS_MASK));
2859 static int get_cjk_fontinfo_margin(int width, int side_bearing)
2861 int margin;
2862 if (side_bearing < 0)
2863 margin = min(-side_bearing, width/2);
2864 else
2865 margin = 0;
2866 return margin;
2869 struct char_width_info {
2870 INT min_lsb, min_rsb, unknown;
2873 /* Undocumented gdi32 export */
2874 extern BOOL WINAPI GetCharWidthInfo(HDC, struct char_width_info *);
2876 /*********************************************************************
2878 * EM_SETMARGINS
2880 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2881 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2882 * margin according to the textmetrics of the current font.
2884 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2885 * equal to or larger than a certain size. The empty client rect is treated as
2886 * 80 pixels width.
2888 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2889 WORD left, WORD right, BOOL repaint)
2891 TEXTMETRICW tm;
2892 INT default_left_margin = 0; /* in pixels */
2893 INT default_right_margin = 0; /* in pixels */
2895 /* Set the default margins depending on the font */
2896 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2897 HDC dc = GetDC(es->hwndSelf);
2898 HFONT old_font = SelectObject(dc, es->font);
2899 LONG width = GdiGetCharDimensions(dc, &tm, NULL), rc_width;
2900 RECT rc;
2902 /* The default margins are only non zero for TrueType or Vector fonts */
2903 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2904 struct char_width_info width_info;
2906 if ((is_cjk_charset(dc) || is_cjk_font(dc)) &&
2907 GetCharWidthInfo(dc, &width_info))
2909 default_left_margin = get_cjk_fontinfo_margin(width, width_info.min_lsb);
2910 default_right_margin = get_cjk_fontinfo_margin(width, width_info.min_rsb);
2912 else
2914 default_left_margin = width / 2;
2915 default_right_margin = width / 2;
2918 GetClientRect(es->hwndSelf, &rc);
2919 rc_width = !IsRectEmpty(&rc) ? rc.right - rc.left : 80;
2920 if (rc_width < default_left_margin + default_right_margin + width * 2) {
2921 default_left_margin = es->left_margin;
2922 default_right_margin = es->right_margin;
2925 SelectObject(dc, old_font);
2926 NtUserReleaseDC( es->hwndSelf, dc );
2929 if (action & EC_LEFTMARGIN) {
2930 es->format_rect.left -= es->left_margin;
2931 if (left != EC_USEFONTINFO)
2932 es->left_margin = left;
2933 else
2934 es->left_margin = default_left_margin;
2935 es->format_rect.left += es->left_margin;
2938 if (action & EC_RIGHTMARGIN) {
2939 es->format_rect.right += es->right_margin;
2940 if (right != EC_USEFONTINFO)
2941 es->right_margin = right;
2942 else
2943 es->right_margin = default_right_margin;
2944 es->format_rect.right -= es->right_margin;
2947 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2948 EDIT_AdjustFormatRect(es);
2949 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2952 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2956 /*********************************************************************
2958 * EM_SETPASSWORDCHAR
2961 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2963 LONG style;
2965 if (es->style & ES_MULTILINE)
2966 return;
2968 if (es->password_char == c)
2969 return;
2971 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2972 es->password_char = c;
2973 if (c) {
2974 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2975 es->style |= ES_PASSWORD;
2976 } else {
2977 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2978 es->style &= ~ES_PASSWORD;
2980 EDIT_InvalidateUniscribeData(es);
2981 EDIT_UpdateText(es, NULL, TRUE);
2985 /*********************************************************************
2987 * EM_SETTABSTOPS
2990 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2992 if (!(es->style & ES_MULTILINE))
2993 return FALSE;
2994 HeapFree(GetProcessHeap(), 0, es->tabs);
2995 es->tabs_count = count;
2996 if (!count)
2997 es->tabs = NULL;
2998 else {
2999 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3000 memcpy(es->tabs, tabs, count * sizeof(INT));
3002 EDIT_InvalidateUniscribeData(es);
3003 return TRUE;
3007 /*********************************************************************
3009 * EM_SETWORDBREAKPROC
3012 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3014 if (es->word_break_proc == wbp)
3015 return;
3017 es->word_break_proc = wbp;
3019 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3020 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3021 EDIT_UpdateText(es, NULL, TRUE);
3026 /*********************************************************************
3028 * EM_UNDO / WM_UNDO
3031 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3033 INT ulength;
3034 LPWSTR utext;
3036 /* As per MSDN spec, for a single-line edit control,
3037 the return value is always TRUE */
3038 if( es->style & ES_READONLY )
3039 return !(es->style & ES_MULTILINE);
3041 ulength = lstrlenW(es->undo_text);
3043 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3045 lstrcpyW(utext, es->undo_text);
3047 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3048 es->undo_insert_count, debugstr_w(utext));
3050 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3051 EDIT_EM_EmptyUndoBuffer(es);
3052 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
3053 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3054 /* send the notification after the selection start and end are set */
3055 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3056 EDIT_EM_ScrollCaret(es);
3057 HeapFree(GetProcessHeap(), 0, utext);
3059 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3060 es->undo_insert_count, debugstr_w(es->undo_text));
3061 return TRUE;
3065 /* Helper function for WM_CHAR
3067 * According to an MSDN blog article titled "Just because you're a control
3068 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3069 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3070 * a dialog box or not.
3072 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3074 return (es->flags & EF_DIALOGMODE);
3078 /*********************************************************************
3080 * WM_PASTE
3083 static void EDIT_WM_Paste(EDITSTATE *es)
3085 HGLOBAL hsrc;
3086 LPWSTR src, ptr;
3087 int len;
3089 /* Protect read-only edit control from modification */
3090 if(es->style & ES_READONLY)
3091 return;
3093 OpenClipboard(es->hwndSelf);
3094 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3095 src = GlobalLock(hsrc);
3096 len = lstrlenW(src);
3097 /* Protect single-line edit against pasting new line character */
3098 if (!(es->style & ES_MULTILINE) && ((ptr = wcschr(src, '\n')))) {
3099 len = ptr - src;
3100 if (len && src[len - 1] == '\r')
3101 --len;
3103 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3104 GlobalUnlock(hsrc);
3106 else if (es->style & ES_PASSWORD) {
3107 /* clear selected text in password edit box even with empty clipboard */
3108 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3110 NtUserCloseClipboard();
3114 /*********************************************************************
3116 * WM_COPY
3119 static void EDIT_WM_Copy(EDITSTATE *es)
3121 INT s = min(es->selection_start, es->selection_end);
3122 INT e = max(es->selection_start, es->selection_end);
3123 HGLOBAL hdst;
3124 LPWSTR dst;
3125 DWORD len;
3127 if (e == s) return;
3129 len = e - s;
3130 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3131 dst = GlobalLock(hdst);
3132 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3133 dst[len] = 0; /* ensure 0 termination */
3134 TRACE("%s\n", debugstr_w(dst));
3135 GlobalUnlock(hdst);
3136 OpenClipboard(es->hwndSelf);
3137 NtUserEmptyClipboard();
3138 SetClipboardData(CF_UNICODETEXT, hdst);
3139 NtUserCloseClipboard();
3143 /*********************************************************************
3145 * WM_CLEAR
3148 static inline void EDIT_WM_Clear(EDITSTATE *es)
3150 /* Protect read-only edit control from modification */
3151 if(es->style & ES_READONLY)
3152 return;
3154 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3158 /*********************************************************************
3160 * WM_CUT
3163 static inline void EDIT_WM_Cut(EDITSTATE *es)
3165 EDIT_WM_Copy(es);
3166 EDIT_WM_Clear(es);
3170 /*********************************************************************
3172 * WM_CHAR
3175 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3177 BOOL control;
3179 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3181 switch (c) {
3182 case '\r':
3183 /* If it's not a multiline edit box, it would be ignored below.
3184 * For multiline edit without ES_WANTRETURN, we have to make a
3185 * special case.
3187 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3188 if (EDIT_IsInsideDialog(es))
3189 break;
3190 case '\n':
3191 if (es->style & ES_MULTILINE) {
3192 if (es->style & ES_READONLY) {
3193 EDIT_MoveHome(es, FALSE, FALSE);
3194 EDIT_MoveDown_ML(es, FALSE);
3195 } else {
3196 EDIT_EM_ReplaceSel(es, TRUE, L"\r\n", 2, TRUE, TRUE);
3199 break;
3200 case '\t':
3201 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3203 if (EDIT_IsInsideDialog(es))
3204 break;
3205 EDIT_EM_ReplaceSel(es, TRUE, L"\t", 1, TRUE, TRUE);
3207 break;
3208 case VK_BACK:
3209 if (!(es->style & ES_READONLY) && !control) {
3210 if (es->selection_start != es->selection_end)
3211 EDIT_WM_Clear(es);
3212 else {
3213 /* delete character left of caret */
3214 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3215 EDIT_MoveBackward(es, TRUE);
3216 EDIT_WM_Clear(es);
3219 break;
3220 case 0x03: /* ^C */
3221 if (!(es->style & ES_PASSWORD))
3222 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3223 break;
3224 case 0x16: /* ^V */
3225 if (!(es->style & ES_READONLY))
3226 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3227 break;
3228 case 0x18: /* ^X */
3229 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3230 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3231 break;
3232 case 0x1A: /* ^Z */
3233 if (!(es->style & ES_READONLY))
3234 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3235 break;
3237 default:
3238 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3239 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3240 break;
3242 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3243 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3244 break;
3246 return 1;
3250 /*********************************************************************
3252 * EDIT_ContextMenuCommand
3255 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3257 switch (id) {
3258 case EM_UNDO:
3259 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3260 break;
3261 case WM_CUT:
3262 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3263 break;
3264 case WM_COPY:
3265 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3266 break;
3267 case WM_PASTE:
3268 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3269 break;
3270 case WM_CLEAR:
3271 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3272 break;
3273 case EM_SETSEL:
3274 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3275 break;
3276 default:
3277 ERR("unknown menu item, please report\n");
3278 break;
3283 /*********************************************************************
3285 * WM_CONTEXTMENU
3287 * Note: the resource files resource/sysres_??.rc cannot define a
3288 * single popup menu. Hence we use a (dummy) menubar
3289 * containing the single popup menu as its first item.
3291 * FIXME: the message identifiers have been chosen arbitrarily,
3292 * hence we use MF_BYPOSITION.
3293 * We might as well use the "real" values (anybody knows ?)
3294 * The menu definition is in resources/sysres_??.rc.
3295 * Once these are OK, we better use MF_BYCOMMAND here
3296 * (as we do in EDIT_WM_Command()).
3299 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3301 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3302 HMENU popup = GetSubMenu(menu, 0);
3303 UINT start = es->selection_start;
3304 UINT end = es->selection_end;
3305 BOOL enabled;
3306 UINT cmd;
3308 ORDER_UINT(start, end);
3310 /* undo */
3311 enabled = EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY);
3312 NtUserEnableMenuItem( popup, 0, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3313 /* cut */
3314 enabled = (end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY);
3315 NtUserEnableMenuItem( popup, 2, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3316 /* copy */
3317 enabled = (end - start) && !(es->style & ES_PASSWORD);
3318 NtUserEnableMenuItem( popup, 3, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3319 /* paste */
3320 enabled = NtUserIsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY);
3321 NtUserEnableMenuItem( popup, 4, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3322 /* delete */
3323 enabled = (end - start) && !(es->style & ES_READONLY);
3324 NtUserEnableMenuItem( popup, 5, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3325 /* select all */
3326 enabled = start || end != get_text_length(es);
3327 NtUserEnableMenuItem( popup, 7, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3329 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3331 POINT pt;
3332 RECT rc;
3334 /* Windows places the menu at the edit's center in this case */
3335 GetClientRect( es->hwndSelf, &rc );
3336 pt.x = rc.right / 2;
3337 pt.y = rc.bottom / 2;
3338 ClientToScreen( es->hwndSelf, &pt );
3339 x = pt.x;
3340 y = pt.y;
3343 if (!(es->flags & EF_FOCUSED))
3344 NtUserSetFocus(es->hwndSelf);
3346 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3347 x, y, 0, es->hwndSelf, NULL);
3349 if (cmd)
3350 EDIT_ContextMenuCommand(es, cmd);
3352 NtUserDestroyMenu(menu);
3356 /*********************************************************************
3358 * WM_GETTEXT
3361 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3363 if(!count) return 0;
3365 if(unicode)
3367 lstrcpynW(dst, es->text, count);
3368 return lstrlenW(dst);
3370 else
3372 LPSTR textA = (LPSTR)dst;
3373 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3374 textA[count - 1] = 0; /* ensure 0 termination */
3375 return strlen(textA);
3379 /*********************************************************************
3381 * EDIT_CheckCombo
3384 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3386 HWND hLBox = es->hwndListBox;
3387 HWND hCombo;
3388 BOOL bDropped;
3389 int nEUI;
3391 if (!hLBox)
3392 return FALSE;
3394 hCombo = GetParent(es->hwndSelf);
3395 bDropped = TRUE;
3396 nEUI = 0;
3398 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3400 if (key == VK_UP || key == VK_DOWN)
3402 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3403 nEUI = 1;
3405 if (msg == WM_KEYDOWN || nEUI)
3406 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3409 switch (msg)
3411 case WM_KEYDOWN:
3412 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3414 /* make sure ComboLBox pops up */
3415 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3416 key = VK_F4;
3417 nEUI = 2;
3420 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3421 break;
3423 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3424 if (nEUI)
3425 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3426 else
3427 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3428 break;
3431 if(nEUI == 2)
3432 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3434 return TRUE;
3438 /*********************************************************************
3440 * WM_KEYDOWN
3442 * Handling of special keys that don't produce a WM_CHAR
3443 * (i.e. non-printable keys) & Backspace & Delete
3446 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3448 BOOL shift;
3449 BOOL control;
3451 if (NtUserGetKeyState(VK_MENU) & 0x8000)
3452 return 0;
3454 shift = NtUserGetKeyState(VK_SHIFT) & 0x8000;
3455 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3457 switch (key) {
3458 case VK_F4:
3459 case VK_UP:
3460 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3461 break;
3463 /* fall through */
3464 case VK_LEFT:
3465 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3466 EDIT_MoveUp_ML(es, shift);
3467 else
3468 if (control)
3469 EDIT_MoveWordBackward(es, shift);
3470 else
3471 EDIT_MoveBackward(es, shift);
3472 break;
3473 case VK_DOWN:
3474 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3475 break;
3476 /* fall through */
3477 case VK_RIGHT:
3478 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3479 EDIT_MoveDown_ML(es, shift);
3480 else if (control)
3481 EDIT_MoveWordForward(es, shift);
3482 else
3483 EDIT_MoveForward(es, shift);
3484 break;
3485 case VK_HOME:
3486 EDIT_MoveHome(es, shift, control);
3487 break;
3488 case VK_END:
3489 EDIT_MoveEnd(es, shift, control);
3490 break;
3491 case VK_PRIOR:
3492 if (es->style & ES_MULTILINE)
3493 EDIT_MovePageUp_ML(es, shift);
3494 else
3495 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3496 break;
3497 case VK_NEXT:
3498 if (es->style & ES_MULTILINE)
3499 EDIT_MovePageDown_ML(es, shift);
3500 else
3501 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3502 break;
3503 case VK_DELETE:
3504 if (!(es->style & ES_READONLY) && !(shift && control)) {
3505 if (es->selection_start != es->selection_end) {
3506 if (shift)
3507 EDIT_WM_Cut(es);
3508 else
3509 EDIT_WM_Clear(es);
3510 } else {
3511 EDIT_EM_SetSel(es, ~0u, 0, FALSE);
3512 if (shift)
3513 /* delete character left of caret */
3514 EDIT_MoveBackward(es, TRUE);
3515 else if (control)
3516 /* delete to end of line */
3517 EDIT_MoveEnd(es, TRUE, FALSE);
3518 else
3519 /* delete character right of caret */
3520 EDIT_MoveForward(es, TRUE);
3521 EDIT_WM_Clear(es);
3524 break;
3525 case VK_INSERT:
3526 if (shift) {
3527 if (!(es->style & ES_READONLY))
3528 EDIT_WM_Paste(es);
3529 } else if (control)
3530 EDIT_WM_Copy(es);
3531 break;
3532 case VK_RETURN:
3533 /* If the edit doesn't want the return send a message to the default object */
3534 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3536 DWORD dw;
3538 if (!EDIT_IsInsideDialog(es)) break;
3539 if (control) break;
3540 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3541 if (HIWORD(dw) == DC_HASDEFID)
3543 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3544 if (hwDefCtrl)
3546 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3547 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3551 break;
3552 case VK_ESCAPE:
3553 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3554 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3555 break;
3556 case VK_TAB:
3557 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3558 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3559 break;
3561 return TRUE;
3565 /*********************************************************************
3567 * WM_KILLFOCUS
3570 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3572 es->flags &= ~EF_FOCUSED;
3573 DestroyCaret();
3574 if(!(es->style & ES_NOHIDESEL))
3575 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3576 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3577 /* throw away left over scroll when we lose focus */
3578 es->wheelDeltaRemainder = 0;
3579 return 0;
3583 /*********************************************************************
3585 * WM_LBUTTONDBLCLK
3587 * The caret position has been set on the WM_LBUTTONDOWN message
3590 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3592 INT s;
3593 INT e = es->selection_end;
3594 INT l;
3595 INT li;
3596 INT ll;
3598 es->bCaptureState = TRUE;
3599 NtUserSetCapture(es->hwndSelf);
3601 l = EDIT_EM_LineFromChar(es, e);
3602 li = EDIT_EM_LineIndex(es, l);
3603 ll = EDIT_EM_LineLength(es, e);
3604 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3605 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3606 EDIT_EM_SetSel(es, s, e, FALSE);
3607 EDIT_EM_ScrollCaret(es);
3608 es->region_posx = es->region_posy = 0;
3609 SetTimer(es->hwndSelf, 0, 100, NULL);
3610 return 0;
3614 /*********************************************************************
3616 * WM_LBUTTONDOWN
3619 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3621 INT e;
3622 BOOL after_wrap;
3624 es->bCaptureState = TRUE;
3625 NtUserSetCapture(es->hwndSelf);
3626 EDIT_ConfinePoint(es, &x, &y);
3627 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3628 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3629 EDIT_EM_ScrollCaret(es);
3630 es->region_posx = es->region_posy = 0;
3631 SetTimer(es->hwndSelf, 0, 100, NULL);
3633 if (!(es->flags & EF_FOCUSED))
3634 NtUserSetFocus(es->hwndSelf);
3636 return 0;
3640 /*********************************************************************
3642 * WM_LBUTTONUP
3645 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3647 if (es->bCaptureState) {
3648 NtUserKillTimer(es->hwndSelf, 0);
3649 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3651 es->bCaptureState = FALSE;
3652 return 0;
3656 /*********************************************************************
3658 * WM_MBUTTONDOWN
3661 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3663 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3664 return 0;
3668 /*********************************************************************
3670 * WM_MOUSEMOVE
3673 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3675 INT e;
3676 BOOL after_wrap;
3677 INT prex, prey;
3679 /* If the mouse has been captured by process other than the edit control itself,
3680 * the windows edit controls will not select the strings with mouse move.
3682 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3683 return 0;
3686 * FIXME: gotta do some scrolling if outside client
3687 * area. Maybe reset the timer ?
3689 prex = x; prey = y;
3690 EDIT_ConfinePoint(es, &x, &y);
3691 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3692 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3693 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3694 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3695 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3696 return 0;
3700 /*********************************************************************
3702 * WM_PAINT
3705 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3707 PAINTSTRUCT ps;
3708 INT i;
3709 HDC dc;
3710 HFONT old_font = 0;
3711 RECT rc;
3712 RECT rcClient;
3713 RECT rcLine;
3714 RECT rcRgn;
3715 HBRUSH brush;
3716 HBRUSH old_brush;
3717 INT bw, bh;
3718 BOOL rev = es->bEnableState &&
3719 ((es->flags & EF_FOCUSED) ||
3720 (es->style & ES_NOHIDESEL));
3721 dc = hdc ? hdc : NtUserBeginPaint( es->hwndSelf, &ps );
3723 /* The dc we use for calculating may not be the one we paint into.
3724 This is the safest action. */
3725 EDIT_InvalidateUniscribeData(es);
3726 GetClientRect(es->hwndSelf, &rcClient);
3728 /* get the background brush */
3729 brush = EDIT_NotifyCtlColor(es, dc);
3731 /* paint the border and the background */
3732 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3734 if(es->style & WS_BORDER) {
3735 bw = GetSystemMetrics(SM_CXBORDER);
3736 bh = GetSystemMetrics(SM_CYBORDER);
3737 rc = rcClient;
3738 if(es->style & ES_MULTILINE) {
3739 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3740 if(es->style & WS_VSCROLL) rc.right+=bw;
3743 /* Draw the frame. Same code as in nonclient.c */
3744 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3745 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3746 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3747 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3748 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3749 SelectObject(dc, old_brush);
3751 /* Keep the border clean */
3752 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3753 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3756 GetClipBox(dc, &rc);
3757 FillRect(dc, &rc, brush);
3759 IntersectClipRect(dc, es->format_rect.left,
3760 es->format_rect.top,
3761 es->format_rect.right,
3762 es->format_rect.bottom);
3763 if (es->style & ES_MULTILINE) {
3764 rc = rcClient;
3765 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3767 if (es->font)
3768 old_font = SelectObject(dc, es->font);
3770 if (!es->bEnableState)
3771 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3772 GetClipBox(dc, &rcRgn);
3773 if (es->style & ES_MULTILINE) {
3774 INT vlc = get_vertical_line_count(es);
3775 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3776 EDIT_UpdateUniscribeData(es, dc, i);
3777 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3778 if (IntersectRect(&rc, &rcRgn, &rcLine))
3779 EDIT_PaintLine(es, dc, i, rev);
3781 } else {
3782 EDIT_UpdateUniscribeData(es, dc, 0);
3783 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3784 if (IntersectRect(&rc, &rcRgn, &rcLine))
3785 EDIT_PaintLine(es, dc, 0, rev);
3787 if (es->font)
3788 SelectObject(dc, old_font);
3790 if (!hdc)
3791 NtUserEndPaint( es->hwndSelf, &ps );
3795 /*********************************************************************
3797 * WM_SETFOCUS
3800 static void EDIT_WM_SetFocus(EDITSTATE *es)
3802 es->flags |= EF_FOCUSED;
3804 if (!(es->style & ES_NOHIDESEL))
3805 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3807 /* single line edit updates itself */
3808 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3810 HDC hdc = GetDC(es->hwndSelf);
3811 EDIT_WM_Paint(es, hdc);
3812 NtUserReleaseDC( es->hwndSelf, hdc );
3815 NtUserCreateCaret( es->hwndSelf, 0, 1, es->line_height );
3816 EDIT_SetCaretPos(es, es->selection_end,
3817 es->flags & EF_AFTER_WRAP);
3818 NtUserShowCaret( es->hwndSelf );
3819 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3822 static DWORD get_font_margins(HDC hdc, const TEXTMETRICW *tm, BOOL unicode)
3824 ABC abc[256];
3825 SHORT left, right;
3826 UINT i;
3828 if (!(tm->tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE)))
3829 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3831 if (unicode) {
3832 if (!is_cjk_charset(hdc) && !is_cjk_font(hdc))
3833 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3834 if (!GetCharABCWidthsW(hdc, 0, 255, abc))
3835 return 0;
3837 else if (is_cjk_charset(hdc)) {
3838 if (!GetCharABCWidthsA(hdc, 0, 255, abc))
3839 return 0;
3841 else
3842 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3844 left = right = 0;
3845 for (i = 0; i < ARRAY_SIZE(abc); i++) {
3846 if (-abc[i].abcA > right) right = -abc[i].abcA;
3847 if (-abc[i].abcC > left ) left = -abc[i].abcC;
3849 return MAKELONG(left, right);
3852 /*********************************************************************
3854 * WM_SETFONT
3856 * With Win95 look the margins are set to default font value unless
3857 * the system font (font == 0) is being set, in which case they are left
3858 * unchanged.
3861 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3863 TEXTMETRICW tm;
3864 HDC dc;
3865 HFONT old_font = 0;
3866 RECT clientRect;
3867 DWORD margins;
3869 es->font = font;
3870 EDIT_InvalidateUniscribeData(es);
3871 dc = GetDC(es->hwndSelf);
3872 if (font)
3873 old_font = SelectObject(dc, font);
3874 GetTextMetricsW(dc, &tm);
3875 es->line_height = tm.tmHeight;
3876 es->char_width = tm.tmAveCharWidth;
3877 margins = get_font_margins(dc, &tm, es->is_unicode);
3878 if (font)
3879 SelectObject(dc, old_font);
3880 NtUserReleaseDC( es->hwndSelf, dc );
3882 /* Reset the format rect and the margins */
3883 GetClientRect(es->hwndSelf, &clientRect);
3884 EDIT_SetRectNP(es, &clientRect);
3885 if (margins)
3886 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3887 LOWORD(margins), HIWORD(margins), FALSE);
3889 if (es->style & ES_MULTILINE)
3890 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3891 else
3892 EDIT_CalcLineWidth_SL(es);
3894 if (redraw)
3895 EDIT_UpdateText(es, NULL, TRUE);
3896 if (es->flags & EF_FOCUSED) {
3897 DestroyCaret();
3898 NtUserCreateCaret( es->hwndSelf, 0, 1, es->line_height );
3899 EDIT_SetCaretPos(es, es->selection_end,
3900 es->flags & EF_AFTER_WRAP);
3901 NtUserShowCaret( es->hwndSelf );
3906 /*********************************************************************
3908 * WM_SETTEXT
3910 * NOTES
3911 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3912 * The modified flag is reset. No notifications are sent.
3914 * For single-line controls, reception of WM_SETTEXT triggers:
3915 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3918 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3920 LPWSTR textW = NULL;
3921 if (!unicode && text)
3923 LPCSTR textA = (LPCSTR)text;
3924 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3925 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3926 if (textW)
3927 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3928 text = textW;
3931 if (es->flags & EF_UPDATE)
3932 /* fixed this bug once; complain if we see it about to happen again. */
3933 ERR("SetSel may generate UPDATE message whose handler may reset "
3934 "selection.\n");
3936 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3937 if (text)
3939 TRACE("%s\n", debugstr_w(text));
3940 EDIT_EM_ReplaceSel(es, FALSE, text, lstrlenW(text), FALSE, FALSE);
3941 if(!unicode)
3942 HeapFree(GetProcessHeap(), 0, textW);
3944 else
3946 TRACE("<NULL>\n");
3947 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3949 es->x_offset = 0;
3950 es->flags &= ~EF_MODIFIED;
3951 EDIT_EM_SetSel(es, 0, 0, FALSE);
3953 /* Send the notification after the selection start and end have been set
3954 * edit control doesn't send notification on WM_SETTEXT
3955 * if it is multiline, or it is part of combobox
3957 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3959 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3960 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3962 EDIT_EM_ScrollCaret(es);
3963 EDIT_UpdateScrollInfo(es);
3964 EDIT_InvalidateUniscribeData(es);
3968 /*********************************************************************
3970 * WM_SIZE
3973 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3975 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3976 RECT rc;
3977 GetClientRect(es->hwndSelf, &rc);
3978 EDIT_SetRectNP(es, &rc);
3979 EDIT_UpdateText(es, NULL, TRUE);
3984 /*********************************************************************
3986 * WM_STYLECHANGED
3988 * This message is sent by SetWindowLong on having changed either the Style
3989 * or the extended style.
3991 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3993 * See also EDIT_WM_NCCreate
3995 * It appears that the Windows version of the edit control allows the style
3996 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3997 * style variable which will generally be different. In this function we
3998 * update the internal style based on what changed in the externally visible
3999 * style.
4001 * Much of this content as based upon the MSDN, especially:
4002 * Platform SDK Documentation -> User Interface Services ->
4003 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4004 * Edit Control Styles
4006 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4008 if (GWL_STYLE == which) {
4009 DWORD style_change_mask;
4010 DWORD new_style;
4011 /* Only a subset of changes can be applied after the control
4012 * has been created.
4014 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4015 ES_NUMBER;
4016 if (es->style & ES_MULTILINE)
4017 style_change_mask |= ES_WANTRETURN;
4019 new_style = style->styleNew & style_change_mask;
4021 /* Number overrides lowercase overrides uppercase (at least it
4022 * does in Win95). However I'll bet that ES_NUMBER would be
4023 * invalid under Win 3.1.
4025 if (new_style & ES_NUMBER) {
4026 ; /* do not override the ES_NUMBER */
4027 } else if (new_style & ES_LOWERCASE) {
4028 new_style &= ~ES_UPPERCASE;
4031 es->style = (es->style & ~style_change_mask) | new_style;
4032 } else if (GWL_EXSTYLE == which) {
4033 ; /* FIXME - what is needed here */
4034 } else {
4035 WARN ("Invalid style change %Id\n",which);
4038 return 0;
4041 /*********************************************************************
4043 * WM_SYSKEYDOWN
4046 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4048 if ((key == VK_BACK) && (key_data & 0x2000)) {
4049 if (EDIT_EM_CanUndo(es))
4050 EDIT_EM_Undo(es);
4051 return 0;
4052 } else if (key == VK_UP || key == VK_DOWN) {
4053 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4054 return 0;
4056 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4060 /*********************************************************************
4062 * WM_TIMER
4065 static void EDIT_WM_Timer(EDITSTATE *es)
4067 if (es->region_posx < 0) {
4068 EDIT_MoveBackward(es, TRUE);
4069 } else if (es->region_posx > 0) {
4070 EDIT_MoveForward(es, TRUE);
4073 * FIXME: gotta do some vertical scrolling here, like
4074 * EDIT_EM_LineScroll(hwnd, 0, 1);
4078 /*********************************************************************
4080 * WM_HSCROLL
4083 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4085 INT dx;
4086 INT fw;
4088 if (!(es->style & ES_MULTILINE))
4089 return 0;
4091 if (!(es->style & ES_AUTOHSCROLL))
4092 return 0;
4094 dx = 0;
4095 fw = es->format_rect.right - es->format_rect.left;
4096 switch (action) {
4097 case SB_LINELEFT:
4098 TRACE("SB_LINELEFT\n");
4099 if (es->x_offset)
4100 dx = -es->char_width;
4101 break;
4102 case SB_LINERIGHT:
4103 TRACE("SB_LINERIGHT\n");
4104 if (es->x_offset < es->text_width)
4105 dx = es->char_width;
4106 break;
4107 case SB_PAGELEFT:
4108 TRACE("SB_PAGELEFT\n");
4109 if (es->x_offset)
4110 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4111 break;
4112 case SB_PAGERIGHT:
4113 TRACE("SB_PAGERIGHT\n");
4114 if (es->x_offset < es->text_width)
4115 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4116 break;
4117 case SB_LEFT:
4118 TRACE("SB_LEFT\n");
4119 if (es->x_offset)
4120 dx = -es->x_offset;
4121 break;
4122 case SB_RIGHT:
4123 TRACE("SB_RIGHT\n");
4124 if (es->x_offset < es->text_width)
4125 dx = es->text_width - es->x_offset;
4126 break;
4127 case SB_THUMBTRACK:
4128 TRACE("SB_THUMBTRACK %d\n", pos);
4129 es->flags |= EF_HSCROLL_TRACK;
4130 if(es->style & WS_HSCROLL)
4131 dx = pos - es->x_offset;
4132 else
4134 INT fw, new_x;
4135 /* Sanity check */
4136 if(pos < 0 || pos > 100) return 0;
4137 /* Assume default scroll range 0-100 */
4138 fw = es->format_rect.right - es->format_rect.left;
4139 new_x = pos * (es->text_width - fw) / 100;
4140 dx = es->text_width ? (new_x - es->x_offset) : 0;
4142 break;
4143 case SB_THUMBPOSITION:
4144 TRACE("SB_THUMBPOSITION %d\n", pos);
4145 es->flags &= ~EF_HSCROLL_TRACK;
4146 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4147 dx = pos - es->x_offset;
4148 else
4150 INT fw, new_x;
4151 /* Sanity check */
4152 if(pos < 0 || pos > 100) return 0;
4153 /* Assume default scroll range 0-100 */
4154 fw = es->format_rect.right - es->format_rect.left;
4155 new_x = pos * (es->text_width - fw) / 100;
4156 dx = es->text_width ? (new_x - es->x_offset) : 0;
4158 if (!dx) {
4159 /* force scroll info update */
4160 EDIT_UpdateScrollInfo(es);
4161 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4163 break;
4164 case SB_ENDSCROLL:
4165 TRACE("SB_ENDSCROLL\n");
4166 break;
4168 * FIXME : the next two are undocumented !
4169 * Are we doing the right thing ?
4170 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4171 * although it's also a regular control message.
4173 case EM_GETTHUMB: /* this one is used by NT notepad */
4175 LRESULT ret;
4176 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4177 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4178 else
4180 /* Assume default scroll range 0-100 */
4181 INT fw = es->format_rect.right - es->format_rect.left;
4182 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4184 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4185 return ret;
4187 case EM_LINESCROLL:
4188 TRACE("EM_LINESCROLL16\n");
4189 dx = pos;
4190 break;
4192 default:
4193 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4194 action, action);
4195 return 0;
4197 if (dx)
4199 INT fw = es->format_rect.right - es->format_rect.left;
4200 /* check if we are going to move too far */
4201 if(es->x_offset + dx + fw > es->text_width)
4202 dx = es->text_width - fw - es->x_offset;
4203 if(dx)
4204 EDIT_EM_LineScroll_internal(es, dx, 0);
4206 return 0;
4210 /*********************************************************************
4212 * WM_VSCROLL
4215 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4217 INT dy;
4219 if (!(es->style & ES_MULTILINE))
4220 return 0;
4222 if (!(es->style & ES_AUTOVSCROLL))
4223 return 0;
4225 dy = 0;
4226 switch (action) {
4227 case SB_LINEUP:
4228 case SB_LINEDOWN:
4229 case SB_PAGEUP:
4230 case SB_PAGEDOWN:
4231 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4232 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4233 (action == SB_PAGEUP ? "SB_PAGEUP" :
4234 "SB_PAGEDOWN"))));
4235 EDIT_EM_Scroll(es, action);
4236 return 0;
4237 case SB_TOP:
4238 TRACE("SB_TOP\n");
4239 dy = -es->y_offset;
4240 break;
4241 case SB_BOTTOM:
4242 TRACE("SB_BOTTOM\n");
4243 dy = es->line_count - 1 - es->y_offset;
4244 break;
4245 case SB_THUMBTRACK:
4246 TRACE("SB_THUMBTRACK %d\n", pos);
4247 es->flags |= EF_VSCROLL_TRACK;
4248 if(es->style & WS_VSCROLL)
4249 dy = pos - es->y_offset;
4250 else
4252 /* Assume default scroll range 0-100 */
4253 INT vlc, new_y;
4254 /* Sanity check */
4255 if(pos < 0 || pos > 100) return 0;
4256 vlc = get_vertical_line_count(es);
4257 new_y = pos * (es->line_count - vlc) / 100;
4258 dy = es->line_count ? (new_y - es->y_offset) : 0;
4259 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4260 es->line_count, es->y_offset, pos, dy);
4262 break;
4263 case SB_THUMBPOSITION:
4264 TRACE("SB_THUMBPOSITION %d\n", pos);
4265 es->flags &= ~EF_VSCROLL_TRACK;
4266 if(es->style & WS_VSCROLL)
4267 dy = pos - es->y_offset;
4268 else
4270 /* Assume default scroll range 0-100 */
4271 INT vlc, new_y;
4272 /* Sanity check */
4273 if(pos < 0 || pos > 100) return 0;
4274 vlc = get_vertical_line_count(es);
4275 new_y = pos * (es->line_count - vlc) / 100;
4276 dy = es->line_count ? (new_y - es->y_offset) : 0;
4277 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4278 es->line_count, es->y_offset, pos, dy);
4280 if (!dy)
4282 /* force scroll info update */
4283 EDIT_UpdateScrollInfo(es);
4284 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4286 break;
4287 case SB_ENDSCROLL:
4288 TRACE("SB_ENDSCROLL\n");
4289 break;
4291 * FIXME : the next two are undocumented !
4292 * Are we doing the right thing ?
4293 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4294 * although it's also a regular control message.
4296 case EM_GETTHUMB: /* this one is used by NT notepad */
4298 LRESULT ret;
4299 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4300 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4301 else
4303 /* Assume default scroll range 0-100 */
4304 INT vlc = get_vertical_line_count(es);
4305 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4307 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4308 return ret;
4310 case EM_LINESCROLL:
4311 TRACE("EM_LINESCROLL %d\n", pos);
4312 dy = pos;
4313 break;
4315 default:
4316 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4317 action, action);
4318 return 0;
4320 if (dy)
4321 EDIT_EM_LineScroll(es, 0, dy);
4322 return 0;
4325 /*********************************************************************
4327 * EM_GETTHUMB
4329 * FIXME: is this right ? (or should it be only VSCROLL)
4330 * (and maybe only for edit controls that really have their
4331 * own scrollbars) (and maybe only for multiline controls ?)
4332 * All in all: very poorly documented
4335 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4337 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4338 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4342 /********************************************************************
4344 * The Following code is to handle inline editing from IMEs
4347 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4349 LONG buflen;
4350 LPWSTR lpCompStr;
4351 LPSTR lpCompStrAttr = NULL;
4352 DWORD dwBufLenAttr;
4354 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4356 if (buflen < 0)
4358 return;
4361 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen);
4362 if (!lpCompStr)
4364 ERR("Unable to allocate IME CompositionString\n");
4365 return;
4368 if (buflen)
4369 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4371 if (CompFlag & GCS_COMPATTR)
4374 * We do not use the attributes yet. it would tell us what characters
4375 * are in transition and which are converted or decided upon
4377 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4378 if (dwBufLenAttr)
4380 dwBufLenAttr ++;
4381 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4382 if (!lpCompStrAttr)
4384 ERR("Unable to allocate IME Attribute String\n");
4385 HeapFree(GetProcessHeap(),0,lpCompStr);
4386 return;
4388 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4389 dwBufLenAttr);
4390 lpCompStrAttr[dwBufLenAttr] = 0;
4394 /* check for change in composition start */
4395 if (es->selection_end < es->composition_start)
4396 es->composition_start = es->selection_end;
4398 /* replace existing selection string */
4399 es->selection_start = es->composition_start;
4401 if (es->composition_len > 0)
4402 es->selection_end = es->composition_start + es->composition_len;
4403 else
4404 es->selection_end = es->selection_start;
4406 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4407 es->composition_len = abs(es->composition_start - es->selection_end);
4409 es->selection_start = es->composition_start;
4410 es->selection_end = es->selection_start + es->composition_len;
4412 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4413 HeapFree(GetProcessHeap(),0,lpCompStr);
4416 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4418 LONG buflen;
4419 LPWSTR lpResultStr;
4421 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4422 if (buflen <= 0)
4424 return;
4427 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4428 if (!lpResultStr)
4430 ERR("Unable to alloc buffer for IME string\n");
4431 return;
4434 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4436 /* check for change in composition start */
4437 if (es->selection_end < es->composition_start)
4438 es->composition_start = es->selection_end;
4440 es->selection_start = es->composition_start;
4441 es->selection_end = es->composition_start + es->composition_len;
4442 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4443 es->composition_start = es->selection_end;
4444 es->composition_len = 0;
4446 HeapFree(GetProcessHeap(),0,lpResultStr);
4449 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4451 HIMC hIMC;
4452 int cursor;
4454 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4456 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4457 es->composition_start = es->selection_end;
4460 hIMC = ImmGetContext(hwnd);
4461 if (!hIMC)
4462 return;
4464 if (CompFlag & GCS_RESULTSTR)
4466 EDIT_GetResultStr(hIMC, es);
4467 cursor = 0;
4469 else
4471 if (CompFlag & GCS_COMPSTR)
4472 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4473 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4475 ImmReleaseContext(hwnd, hIMC);
4476 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4480 /*********************************************************************
4482 * WM_NCCREATE
4484 * See also EDIT_WM_StyleChanged
4486 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4488 EDITSTATE *es;
4489 UINT alloc_size;
4491 TRACE("Creating %s edit control, style = %08lx\n",
4492 unicode ? "Unicode" : "ANSI", lpcs->style);
4494 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4495 return FALSE;
4496 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4499 * Note: since the EDITSTATE has not been fully initialized yet,
4500 * we can't use any API calls that may send
4501 * WM_XXX messages before WM_NCCREATE is completed.
4504 es->is_unicode = unicode;
4505 es->style = lpcs->style;
4507 es->bEnableState = !(es->style & WS_DISABLED);
4509 es->hwndSelf = hwnd;
4510 /* Save parent, which will be notified by EN_* messages */
4511 es->hwndParent = lpcs->hwndParent;
4513 if (es->style & ES_COMBO)
4514 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4516 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4517 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4519 /* Number overrides lowercase overrides uppercase (at least it
4520 * does in Win95). However I'll bet that ES_NUMBER would be
4521 * invalid under Win 3.1.
4523 if (es->style & ES_NUMBER) {
4524 ; /* do not override the ES_NUMBER */
4525 } else if (es->style & ES_LOWERCASE) {
4526 es->style &= ~ES_UPPERCASE;
4528 if (es->style & ES_MULTILINE) {
4529 es->buffer_limit = BUFLIMIT_INITIAL;
4530 if (es->style & WS_VSCROLL)
4531 es->style |= ES_AUTOVSCROLL;
4532 if (es->style & WS_HSCROLL)
4533 es->style |= ES_AUTOHSCROLL;
4534 es->style &= ~ES_PASSWORD;
4535 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4536 /* Confirmed - RIGHT overrides CENTER */
4537 if (es->style & ES_RIGHT)
4538 es->style &= ~ES_CENTER;
4539 es->style &= ~WS_HSCROLL;
4540 es->style &= ~ES_AUTOHSCROLL;
4542 } else {
4543 es->buffer_limit = BUFLIMIT_INITIAL;
4544 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4545 es->style &= ~ES_CENTER;
4546 es->style &= ~WS_HSCROLL;
4547 es->style &= ~WS_VSCROLL;
4548 if (es->style & ES_PASSWORD)
4549 es->password_char = '*';
4552 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4553 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4554 goto cleanup;
4555 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4557 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4558 goto cleanup;
4559 es->undo_buffer_size = es->buffer_size;
4561 if (es->style & ES_MULTILINE)
4562 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4563 goto cleanup;
4564 es->line_count = 1;
4567 * In Win95 look and feel, the WS_BORDER style is replaced by the
4568 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4569 * control a nonclient area so we don't need to draw the border.
4570 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4571 * a nonclient area and we should handle painting the border ourselves.
4573 * When making modifications please ensure that the code still works
4574 * for edit controls created directly with style 0x50800000, exStyle 0
4575 * (which should have a single pixel border)
4577 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4578 es->style &= ~WS_BORDER;
4579 else if (es->style & WS_BORDER)
4580 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4582 return TRUE;
4584 cleanup:
4585 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4586 EDIT_InvalidateUniscribeData(es);
4587 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4588 HeapFree(GetProcessHeap(), 0, es->undo_text);
4589 if (es->hloc32W) LocalFree(es->hloc32W);
4590 HeapFree(GetProcessHeap(), 0, es->logAttr);
4591 HeapFree(GetProcessHeap(), 0, es);
4592 return FALSE;
4596 /*********************************************************************
4598 * WM_CREATE
4601 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4603 RECT clientRect;
4605 TRACE("%s\n", debugstr_w(name));
4607 * To initialize some final structure members, we call some helper
4608 * functions. However, since the EDITSTATE is not consistent (i.e.
4609 * not fully initialized), we should be very careful which
4610 * functions can be called, and in what order.
4612 EDIT_WM_SetFont(es, 0, FALSE);
4613 EDIT_EM_EmptyUndoBuffer(es);
4615 /* We need to calculate the format rect
4616 (applications may send EM_SETMARGINS before the control gets visible) */
4617 GetClientRect(es->hwndSelf, &clientRect);
4618 EDIT_SetRectNP(es, &clientRect);
4620 if (name && *name) {
4621 EDIT_EM_ReplaceSel(es, FALSE, name, lstrlenW(name), FALSE, FALSE);
4622 /* if we insert text to the editline, the text scrolls out
4623 * of the window, as the caret is placed after the insert
4624 * pos normally; thus we reset es->selection... to 0 and
4625 * update caret
4627 es->selection_start = es->selection_end = 0;
4628 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4629 * Messages are only to be sent when the USER does something to
4630 * change the contents. So I am removing this EN_CHANGE
4632 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4634 EDIT_EM_ScrollCaret(es);
4636 /* force scroll info update */
4637 EDIT_UpdateScrollInfo(es);
4638 /* The rule seems to return 1 here for success */
4639 /* Power Builder masked edit controls will crash */
4640 /* if not. */
4641 /* FIXME: is that in all cases so ? */
4642 return 1;
4646 /*********************************************************************
4648 * WM_NCDESTROY
4651 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4653 LINEDEF *pc, *pp;
4655 /* The app can own the text buffer handle */
4656 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4657 LocalFree(es->hloc32W);
4659 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4660 LocalFree(es->hloc32A);
4662 EDIT_InvalidateUniscribeData(es);
4663 pc = es->first_line_def;
4664 while (pc)
4666 pp = pc->next;
4667 HeapFree(GetProcessHeap(), 0, pc);
4668 pc = pp;
4671 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4672 HeapFree(GetProcessHeap(), 0, es->undo_text);
4673 HeapFree(GetProcessHeap(), 0, es);
4675 return 0;
4679 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4681 if(unicode)
4682 return DefWindowProcW(hwnd, msg, wParam, lParam);
4683 else
4684 return DefWindowProcA(hwnd, msg, wParam, lParam);
4687 /*********************************************************************
4689 * EditWndProc_common
4691 * The messages are in the order of the actual integer values
4692 * (which can be found in include/windows.h)
4694 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4696 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4697 LRESULT result = 0;
4699 TRACE("hwnd=%p msg=%x (%s) wparam=%Ix lparam=%Ix\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4701 if (!es && msg != WM_NCCREATE)
4702 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4704 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4706 switch (msg) {
4707 case EM_GETSEL:
4708 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4709 break;
4711 case EM_SETSEL:
4712 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4713 EDIT_EM_ScrollCaret(es);
4714 result = 1;
4715 break;
4717 case EM_GETRECT:
4718 if (lParam)
4719 *((LPRECT)lParam) = es->format_rect;
4720 break;
4722 case EM_SETRECT:
4723 if ((es->style & ES_MULTILINE) && lParam) {
4724 EDIT_SetRectNP(es, (LPRECT)lParam);
4725 EDIT_UpdateText(es, NULL, TRUE);
4727 break;
4729 case EM_SETRECTNP:
4730 if ((es->style & ES_MULTILINE) && lParam)
4731 EDIT_SetRectNP(es, (LPRECT)lParam);
4732 break;
4734 case EM_SCROLL:
4735 result = EDIT_EM_Scroll(es, (INT)wParam);
4736 break;
4738 case EM_LINESCROLL:
4739 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4740 break;
4742 case EM_SCROLLCARET:
4743 EDIT_EM_ScrollCaret(es);
4744 result = 1;
4745 break;
4747 case EM_GETMODIFY:
4748 result = ((es->flags & EF_MODIFIED) != 0);
4749 break;
4751 case EM_SETMODIFY:
4752 if (wParam)
4753 es->flags |= EF_MODIFIED;
4754 else
4755 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4756 break;
4758 case EM_GETLINECOUNT:
4759 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4760 break;
4762 case EM_LINEINDEX:
4763 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4764 break;
4766 case EM_SETHANDLE:
4767 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4768 break;
4770 case EM_GETHANDLE:
4771 result = (LRESULT)EDIT_EM_GetHandle(es);
4772 break;
4774 case EM_GETTHUMB:
4775 result = EDIT_EM_GetThumb(es);
4776 break;
4778 /* these messages missing from specs */
4779 case 0x00bf:
4780 case 0x00c0:
4781 case 0x00c3:
4782 case 0x00ca:
4783 FIXME("undocumented message 0x%x, please report\n", msg);
4784 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4785 break;
4787 case EM_LINELENGTH:
4788 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4789 break;
4791 case EM_REPLACESEL:
4793 LPWSTR textW;
4795 if(unicode)
4796 textW = (LPWSTR)lParam;
4797 else
4799 LPSTR textA = (LPSTR)lParam;
4800 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4801 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4802 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4805 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, lstrlenW(textW), TRUE, TRUE);
4806 result = 1;
4808 if(!unicode)
4809 HeapFree(GetProcessHeap(), 0, textW);
4810 break;
4813 case EM_GETLINE:
4814 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4815 break;
4817 case EM_SETLIMITTEXT:
4818 EDIT_EM_SetLimitText(es, wParam);
4819 break;
4821 case EM_CANUNDO:
4822 result = (LRESULT)EDIT_EM_CanUndo(es);
4823 break;
4825 case EM_UNDO:
4826 case WM_UNDO:
4827 result = (LRESULT)EDIT_EM_Undo(es);
4828 break;
4830 case EM_FMTLINES:
4831 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4832 break;
4834 case EM_LINEFROMCHAR:
4835 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4836 break;
4838 case EM_SETTABSTOPS:
4839 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4840 break;
4842 case EM_SETPASSWORDCHAR:
4844 WCHAR charW = 0;
4846 if(unicode)
4847 charW = (WCHAR)wParam;
4848 else
4850 CHAR charA = wParam;
4851 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4854 EDIT_EM_SetPasswordChar(es, charW);
4855 break;
4858 case EM_EMPTYUNDOBUFFER:
4859 EDIT_EM_EmptyUndoBuffer(es);
4860 break;
4862 case EM_GETFIRSTVISIBLELINE:
4863 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4864 break;
4866 case EM_SETREADONLY:
4868 DWORD old_style = es->style;
4870 if (wParam) {
4871 SetWindowLongW( hwnd, GWL_STYLE,
4872 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4873 es->style |= ES_READONLY;
4874 } else {
4875 SetWindowLongW( hwnd, GWL_STYLE,
4876 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4877 es->style &= ~ES_READONLY;
4880 if (old_style ^ es->style)
4881 NtUserInvalidateRect(es->hwndSelf, NULL, TRUE);
4883 result = 1;
4884 break;
4887 case EM_SETWORDBREAKPROC:
4888 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4889 result = 1;
4890 break;
4892 case EM_GETWORDBREAKPROC:
4893 result = (LRESULT)es->word_break_proc;
4894 break;
4896 case EM_GETPASSWORDCHAR:
4898 if(unicode)
4899 result = es->password_char;
4900 else
4902 WCHAR charW = es->password_char;
4903 CHAR charA = 0;
4904 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4905 result = charA;
4907 break;
4910 case EM_SETMARGINS:
4911 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4912 break;
4914 case EM_GETMARGINS:
4915 result = MAKELONG(es->left_margin, es->right_margin);
4916 break;
4918 case EM_GETLIMITTEXT:
4919 result = es->buffer_limit;
4920 break;
4922 case EM_POSFROMCHAR:
4923 if ((INT)wParam >= get_text_length(es)) result = -1;
4924 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4925 break;
4927 case EM_CHARFROMPOS:
4928 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4929 break;
4931 case EM_SETIMESTATUS:
4932 if (wParam == EMSIS_COMPOSITIONSTRING)
4933 es->ime_status = lParam & 0xFFFF;
4935 result = 1;
4936 break;
4938 case EM_GETIMESTATUS:
4939 result = wParam == EMSIS_COMPOSITIONSTRING ? es->ime_status : 1;
4940 break;
4942 /* End of the EM_ messages which were in numerical order; what order
4943 * are these in? vaguely alphabetical?
4946 case WM_NCCREATE:
4947 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4948 break;
4950 case WM_NCDESTROY:
4951 result = EDIT_WM_NCDestroy(es);
4952 es = NULL;
4953 break;
4955 case WM_GETDLGCODE:
4956 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4958 if (es->style & ES_MULTILINE)
4959 result |= DLGC_WANTALLKEYS;
4961 if (lParam)
4963 es->flags|=EF_DIALOGMODE;
4965 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4967 int vk = (int)((LPMSG)lParam)->wParam;
4969 if (es->hwndListBox)
4971 if (vk == VK_RETURN || vk == VK_ESCAPE)
4972 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4973 result |= DLGC_WANTMESSAGE;
4977 break;
4979 case WM_CHAR:
4981 WCHAR charW;
4983 if(unicode)
4984 charW = wParam;
4985 else
4987 CHAR charA = wParam;
4988 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4991 if (es->hwndListBox)
4993 if (charW == VK_RETURN || charW == VK_ESCAPE)
4995 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4996 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4997 break;
5000 result = EDIT_WM_Char(es, charW);
5001 break;
5004 case WM_UNICHAR:
5005 if (unicode)
5007 if (wParam == UNICODE_NOCHAR) return TRUE;
5008 if (wParam <= 0x000fffff)
5010 if(wParam > 0xffff) /* convert to surrogates */
5012 wParam -= 0x10000;
5013 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
5014 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
5016 else EDIT_WM_Char(es, wParam);
5018 return 0;
5020 break;
5022 case WM_CLEAR:
5023 EDIT_WM_Clear(es);
5024 break;
5026 case WM_CONTEXTMENU:
5027 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5028 break;
5030 case WM_COPY:
5031 EDIT_WM_Copy(es);
5032 break;
5034 case WM_CREATE:
5035 if(unicode)
5036 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
5037 else
5039 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
5040 LPWSTR nameW = NULL;
5041 if(nameA)
5043 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5044 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5045 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5047 result = EDIT_WM_Create(es, nameW);
5048 HeapFree(GetProcessHeap(), 0, nameW);
5050 break;
5052 case WM_CUT:
5053 EDIT_WM_Cut(es);
5054 break;
5056 case WM_ENABLE:
5057 es->bEnableState = (BOOL) wParam;
5058 EDIT_UpdateText(es, NULL, TRUE);
5059 break;
5061 case WM_ERASEBKGND:
5062 /* we do the proper erase in EDIT_WM_Paint */
5063 result = 1;
5064 break;
5066 case WM_GETFONT:
5067 result = (LRESULT)es->font;
5068 break;
5070 case WM_GETTEXT:
5071 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5072 break;
5074 case WM_GETTEXTLENGTH:
5075 if (unicode) result = get_text_length(es);
5076 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5077 NULL, 0, NULL, NULL );
5078 break;
5080 case WM_HSCROLL:
5081 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5082 break;
5084 case WM_KEYDOWN:
5085 result = EDIT_WM_KeyDown(es, (INT)wParam);
5086 break;
5088 case WM_KILLFOCUS:
5089 result = EDIT_WM_KillFocus(es);
5090 break;
5092 case WM_LBUTTONDBLCLK:
5093 result = EDIT_WM_LButtonDblClk(es);
5094 break;
5096 case WM_LBUTTONDOWN:
5097 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5098 break;
5100 case WM_LBUTTONUP:
5101 result = EDIT_WM_LButtonUp(es);
5102 break;
5104 case WM_MBUTTONDOWN:
5105 result = EDIT_WM_MButtonDown(es);
5106 break;
5108 case WM_MOUSEMOVE:
5109 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5110 break;
5112 case WM_PRINTCLIENT:
5113 case WM_PAINT:
5114 EDIT_WM_Paint(es, (HDC)wParam);
5115 break;
5117 case WM_PASTE:
5118 EDIT_WM_Paste(es);
5119 break;
5121 case WM_SETFOCUS:
5122 EDIT_WM_SetFocus(es);
5123 break;
5125 case WM_SETFONT:
5126 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5127 break;
5129 case WM_SETREDRAW:
5130 /* FIXME: actually set an internal flag and behave accordingly */
5131 break;
5133 case WM_SETTEXT:
5134 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5135 result = TRUE;
5136 break;
5138 case WM_SIZE:
5139 EDIT_WM_Size(es, (UINT)wParam);
5140 break;
5142 case WM_STYLECHANGED:
5143 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5144 break;
5146 case WM_STYLECHANGING:
5147 result = 0; /* See EDIT_WM_StyleChanged */
5148 break;
5150 case WM_SYSKEYDOWN:
5151 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5152 break;
5154 case WM_TIMER:
5155 EDIT_WM_Timer(es);
5156 break;
5158 case WM_VSCROLL:
5159 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5160 break;
5162 case WM_MOUSEWHEEL:
5164 int wheelDelta;
5165 INT pulScrollLines = 3;
5166 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5168 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5169 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5170 break;
5172 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5173 /* if scrolling changes direction, ignore left overs */
5174 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5175 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5176 es->wheelDeltaRemainder += wheelDelta;
5177 else
5178 es->wheelDeltaRemainder = wheelDelta;
5179 if (es->wheelDeltaRemainder && pulScrollLines)
5181 int cLineScroll;
5182 pulScrollLines = min(es->line_count, pulScrollLines);
5183 cLineScroll = pulScrollLines * es->wheelDeltaRemainder / WHEEL_DELTA;
5184 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
5185 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5188 break;
5191 /* IME messages to make the edit control IME aware */
5192 case WM_IME_SETCONTEXT:
5193 break;
5195 case WM_IME_STARTCOMPOSITION:
5196 es->composition_start = es->selection_end;
5197 es->composition_len = 0;
5198 break;
5200 case WM_IME_COMPOSITION:
5201 EDIT_ImeComposition(hwnd, lParam, es);
5202 break;
5204 case WM_IME_ENDCOMPOSITION:
5205 if (es->composition_len > 0)
5207 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5208 es->selection_end = es->selection_start;
5209 es->composition_len= 0;
5211 break;
5213 case WM_IME_COMPOSITIONFULL:
5214 break;
5216 case WM_IME_SELECT:
5217 break;
5219 case WM_IME_CONTROL:
5220 break;
5222 case WM_IME_REQUEST:
5223 switch (wParam)
5225 case IMR_QUERYCHARPOSITION:
5227 LRESULT pos;
5228 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5230 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5231 chpos->pt.x = LOWORD(pos);
5232 chpos->pt.y = HIWORD(pos);
5233 chpos->cLineHeight = es->line_height;
5234 chpos->rcDocument = es->format_rect;
5235 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5236 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5237 result = 1;
5238 break;
5241 break;
5243 default:
5244 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5245 break;
5248 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5249 EDIT_UnlockBuffer(es, FALSE);
5251 TRACE("hwnd=%p msg=%x (%s) -- 0x%08Ix\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5253 return result;