secur32: Implement DeleteSecurityContext in the SSP/AP wrapper.
[wine.git] / dlls / user32 / edit.c
blob614a979a390251da75938077b25fc140cecc7a8a
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTES
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_OEMCONVERT
45 #include "config.h"
47 #include <stdarg.h>
48 #include <string.h>
49 #include <stdlib.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnt.h"
54 #include "win.h"
55 #include "imm.h"
56 #include "usp10.h"
57 #include "wine/unicode.h"
58 #include "controls.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(edit);
63 WINE_DECLARE_DEBUG_CHANNEL(combo);
64 WINE_DECLARE_DEBUG_CHANNEL(relay);
66 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
67 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
68 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
69 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
72 * extra flags for EDITSTATE.flags field
74 #define EF_MODIFIED 0x0001 /* text has been modified */
75 #define EF_FOCUSED 0x0002 /* we have input focus */
76 #define EF_UPDATE 0x0004 /* notify parent of changed state */
77 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
79 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
80 wrapped line, instead of in front of the next character */
81 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
82 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
84 typedef enum
86 END_0 = 0, /* line ends with terminating '\0' character */
87 END_WRAP, /* line is wrapped */
88 END_HARD, /* line ends with a hard return '\r\n' */
89 END_SOFT, /* line ends with a soft return '\r\r\n' */
90 END_RICH /* line ends with a single '\n' */
91 } LINE_END;
93 typedef struct tagLINEDEF {
94 INT length; /* bruto length of a line in bytes */
95 INT net_length; /* netto length of a line in visible characters */
96 LINE_END ending;
97 INT width; /* width of the line in pixels */
98 INT index; /* line index into the buffer */
99 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
100 struct tagLINEDEF *next;
101 } LINEDEF;
103 typedef struct
105 BOOL is_unicode; /* how the control was created */
106 LPWSTR text; /* the actual contents of the control */
107 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
108 UINT buffer_size; /* the size of the buffer in characters */
109 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
110 HFONT font; /* NULL means standard system font */
111 INT x_offset; /* scroll offset for multi lines this is in pixels
112 for single lines it's in characters */
113 INT line_height; /* height of a screen line in pixels */
114 INT char_width; /* average character width in pixels */
115 DWORD style; /* sane version of wnd->dwStyle */
116 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
117 INT undo_insert_count; /* number of characters inserted in sequence */
118 UINT undo_position; /* character index of the insertion and deletion */
119 LPWSTR undo_text; /* deleted text */
120 UINT undo_buffer_size; /* size of the deleted text buffer */
121 INT selection_start; /* == selection_end if no selection */
122 INT selection_end; /* == current caret position */
123 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
124 INT left_margin; /* in pixels */
125 INT right_margin; /* in pixels */
126 RECT format_rect;
127 INT text_width; /* width of the widest line in pixels for multi line controls
128 and just line width for single line controls */
129 INT region_posx; /* Position of cursor relative to region: */
130 INT region_posy; /* -1: to left, 0: within, 1: to right */
131 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
132 INT line_count; /* number of lines */
133 INT y_offset; /* scroll offset in number of lines */
134 BOOL bCaptureState; /* flag indicating whether mouse was captured */
135 BOOL bEnableState; /* flag keeping the enable state */
136 HWND hwndSelf; /* the our window handle */
137 HWND hwndParent; /* Handle of parent for sending EN_* messages.
138 Even if parent will change, EN_* messages
139 should be sent to the first parent. */
140 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
141 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
143 * only for multi line controls
145 INT lock_count; /* amount of re-entries in the EditWndProc */
146 INT tabs_count;
147 LPINT tabs;
148 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
149 HLOCAL hloc32W; /* our unicode local memory block */
150 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
151 or EM_SETHANDLE */
152 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
154 * IME Data
156 UINT composition_len; /* length of composition, 0 == no composition */
157 int composition_start; /* the character position for the composition */
159 * Uniscribe Data
161 SCRIPT_LOGATTR *logAttr;
162 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
163 } EDITSTATE;
166 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
167 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
169 /* used for disabled or read-only edit control */
170 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
171 do \
172 { /* Notify parent which has created this edit control */ \
173 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
174 SendMessageW(es->hwndParent, WM_COMMAND, \
175 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
176 (LPARAM)(es->hwndSelf)); \
177 } while(0)
179 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
181 /*********************************************************************
183 * EM_CANUNDO
186 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
188 return (es->undo_insert_count || strlenW(es->undo_text));
192 /*********************************************************************
194 * EM_EMPTYUNDOBUFFER
197 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
199 es->undo_insert_count = 0;
200 *es->undo_text = '\0';
204 /**********************************************************************
205 * get_app_version
207 * Returns the window version in case Wine emulates a later version
208 * of windows than the application expects.
210 * In a number of cases when windows runs an application that was
211 * designed for an earlier windows version, windows reverts
212 * to "old" behaviour of that earlier version.
214 * An example is a disabled edit control that needs to be painted.
215 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
216 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
217 * applications with an expected version 0f 4.0 or higher.
220 static DWORD get_app_version(void)
222 static DWORD version;
223 if (!version)
225 DWORD dwEmulatedVersion;
226 OSVERSIONINFOW info;
227 DWORD dwProcVersion = GetProcessVersion(0);
229 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
230 GetVersionExW( &info );
231 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
232 /* FIXME: this may not be 100% correct; see discussion on the
233 * wine developer list in Nov 1999 */
234 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
236 return version;
239 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
241 HBRUSH hbrush;
242 UINT msg;
244 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
245 msg = WM_CTLCOLORSTATIC;
246 else
247 msg = WM_CTLCOLOREDIT;
249 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
250 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
251 if (!hbrush)
252 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
253 return hbrush;
257 static inline UINT get_text_length(EDITSTATE *es)
259 if(es->text_length == (UINT)-1)
260 es->text_length = strlenW(es->text);
261 return es->text_length;
265 /*********************************************************************
267 * EDIT_WordBreakProc
269 * Find the beginning of words.
270 * Note: unlike the specs for a WordBreakProc, this function can
271 * only be called without linebreaks between s[0] up to
272 * s[count - 1]. Remember it is only called
273 * internally, so we can decide this for ourselves.
274 * Additionally we will always be breaking the full string.
277 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
279 INT ret = 0;
281 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
283 if(!s) return 0;
285 if (!es->logAttr)
287 SCRIPT_ANALYSIS psa;
289 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
290 psa.eScript = SCRIPT_UNDEFINED;
292 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
293 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
296 switch (action) {
297 case WB_LEFT:
298 if (index)
299 index--;
300 while (index && !es->logAttr[index].fSoftBreak)
301 index--;
302 ret = index;
303 break;
304 case WB_RIGHT:
305 if (!count)
306 break;
307 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
308 index++;
309 ret = index;
310 break;
311 case WB_ISDELIMITER:
312 ret = es->logAttr[index].fWhiteSpace;
313 break;
314 default:
315 ERR("unknown action code, please report !\n");
316 break;
318 return ret;
322 /*********************************************************************
324 * EDIT_CallWordBreakProc
326 * Call appropriate WordBreakProc (internal or external).
328 * Note: The "start" argument should always be an index referring
329 * to es->text. The actual wordbreak proc might be
330 * 16 bit, so we can't always pass any 32 bit LPSTR.
331 * Hence we assume that es->text is the buffer that holds
332 * the string under examination (we can decide this for ourselves).
335 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
337 INT ret;
339 if (es->word_break_proc)
341 if(es->is_unicode)
343 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
345 TRACE_(relay)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
346 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
347 ret = wbpW(es->text + start, index, count, action);
348 TRACE_(relay)("\1Ret wordbreakW %p retval=%d\n", es->word_break_proc, ret );
350 else
352 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
353 INT countA;
354 CHAR *textA;
356 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
357 textA = HeapAlloc(GetProcessHeap(), 0, countA);
358 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
359 TRACE_(relay)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
360 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
361 ret = wbpA(textA, index, countA, action);
362 HeapFree(GetProcessHeap(), 0, textA);
363 TRACE_(relay)("\1Ret wordbreakA %p retval=%d\n", es->word_break_proc, ret );
366 else
367 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
369 return ret;
372 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
374 if (line_def->ssa)
376 ScriptStringFree(&line_def->ssa);
377 line_def->ssa = NULL;
381 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
383 LINEDEF *line_def = es->first_line_def;
384 while (line_def)
386 EDIT_InvalidateUniscribeData_linedef(line_def);
387 line_def = line_def->next;
389 if (es->ssa)
391 ScriptStringFree(&es->ssa);
392 es->ssa = NULL;
396 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
398 if (!line_def)
399 return NULL;
401 if (line_def->net_length && !line_def->ssa)
403 int index = line_def->index;
404 HFONT old_font = NULL;
405 HDC udc = dc;
406 SCRIPT_TABDEF tabdef;
407 HRESULT hr;
409 if (!udc)
410 udc = GetDC(es->hwndSelf);
411 if (es->font)
412 old_font = SelectObject(udc, es->font);
414 tabdef.cTabStops = es->tabs_count;
415 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
416 tabdef.pTabStops = es->tabs;
417 tabdef.iTabOrigin = 0;
419 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
420 (1.5*line_def->net_length+16), -1,
421 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
422 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
423 if (FAILED(hr))
425 WARN("ScriptStringAnalyse failed (%x)\n",hr);
426 line_def->ssa = NULL;
429 if (es->font)
430 SelectObject(udc, old_font);
431 if (udc != dc)
432 ReleaseDC(es->hwndSelf, udc);
435 return line_def->ssa;
438 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
440 LINEDEF *line_def;
442 if (!(es->style & ES_MULTILINE))
444 if (!es->ssa)
446 INT length = get_text_length(es);
447 HFONT old_font = NULL;
448 HDC udc = dc;
450 if (!udc)
451 udc = GetDC(es->hwndSelf);
452 if (es->font)
453 old_font = SelectObject(udc, es->font);
455 if (es->style & ES_PASSWORD)
456 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);
457 else
458 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
460 if (es->font)
461 SelectObject(udc, old_font);
462 if (udc != dc)
463 ReleaseDC(es->hwndSelf, udc);
465 return es->ssa;
467 else
469 line_def = es->first_line_def;
470 while (line_def && line)
472 line_def = line_def->next;
473 line--;
476 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
480 static inline INT get_vertical_line_count(EDITSTATE *es)
482 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
483 return max(1,vlc);
486 /*********************************************************************
488 * EDIT_BuildLineDefs_ML
490 * Build linked list of text lines.
491 * Lines can end with '\0' (last line), a character (if it is wrapped),
492 * a soft return '\r\r\n' or a hard return '\r\n'
495 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
497 LPWSTR current_position, cp;
498 INT fw;
499 LINEDEF *current_line;
500 LINEDEF *previous_line;
501 LINEDEF *start_line;
502 INT line_index = 0, nstart_line, nstart_index;
503 INT line_count = es->line_count;
504 INT orig_net_length;
505 RECT rc;
506 INT vlc;
508 if (istart == iend && delta == 0)
509 return;
511 previous_line = NULL;
512 current_line = es->first_line_def;
514 /* Find starting line. istart must lie inside an existing line or
515 * at the end of buffer */
516 do {
517 if (istart < current_line->index + current_line->length ||
518 current_line->ending == END_0)
519 break;
521 previous_line = current_line;
522 current_line = current_line->next;
523 line_index++;
524 } while (current_line);
526 if (!current_line) /* Error occurred start is not inside previous buffer */
528 FIXME(" modification occurred outside buffer\n");
529 return;
532 /* Remember start of modifications in order to calculate update region */
533 nstart_line = line_index;
534 nstart_index = current_line->index;
536 /* We must start to reformat from the previous line since the modifications
537 * may have caused the line to wrap upwards. */
538 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
540 line_index--;
541 current_line = previous_line;
543 start_line = current_line;
545 fw = es->format_rect.right - es->format_rect.left;
546 current_position = es->text + current_line->index;
547 vlc = get_vertical_line_count(es);
548 do {
549 if (current_line != start_line)
551 if (!current_line || current_line->index + delta > current_position - es->text)
553 /* The buffer has been expanded, create a new line and
554 insert it into the link list */
555 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
556 new_line->next = previous_line->next;
557 previous_line->next = new_line;
558 current_line = new_line;
559 es->line_count++;
561 else if (current_line->index + delta < current_position - es->text)
563 /* The previous line merged with this line so we delete this extra entry */
564 previous_line->next = current_line->next;
565 HeapFree(GetProcessHeap(), 0, current_line);
566 current_line = previous_line->next;
567 es->line_count--;
568 continue;
570 else /* current_line->index + delta == current_position */
572 if (current_position - es->text > iend)
573 break; /* We reached end of line modifications */
574 /* else recalculate this line */
578 current_line->index = current_position - es->text;
579 orig_net_length = current_line->net_length;
581 /* Find end of line */
582 cp = current_position;
583 while (*cp) {
584 if (*cp == '\n') break;
585 if ((*cp == '\r') && (*(cp + 1) == '\n'))
586 break;
587 cp++;
590 /* Mark type of line termination */
591 if (!(*cp)) {
592 current_line->ending = END_0;
593 current_line->net_length = strlenW(current_position);
594 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
595 current_line->ending = END_SOFT;
596 current_line->net_length = cp - current_position - 1;
597 } else if (*cp == '\n') {
598 current_line->ending = END_RICH;
599 current_line->net_length = cp - current_position;
600 } else {
601 current_line->ending = END_HARD;
602 current_line->net_length = cp - current_position;
605 if (current_line->net_length)
607 const SIZE *sz;
608 EDIT_InvalidateUniscribeData_linedef(current_line);
609 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
610 if (current_line->ssa)
612 sz = ScriptString_pSize(current_line->ssa);
613 /* Calculate line width */
614 current_line->width = sz->cx;
616 else current_line->width = es->char_width * current_line->net_length;
618 else current_line->width = 0;
620 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
622 /* Line breaks just look back from the end and find the next break and try that. */
624 if (!(es->style & ES_AUTOHSCROLL)) {
625 if (current_line->width > fw && fw > es->char_width) {
627 INT prev, next;
628 int w;
629 const SIZE *sz;
630 float d;
632 prev = current_line->net_length - 1;
633 w = current_line->net_length;
634 d = (float)current_line->width/(float)fw;
635 if (d > 1.2f) d -= 0.2f;
636 next = prev/d;
637 if (next >= prev) next = prev-1;
638 do {
639 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
640 next, current_line->net_length, WB_LEFT);
641 current_line->net_length = prev;
642 EDIT_InvalidateUniscribeData_linedef(current_line);
643 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
644 if (current_line->ssa)
645 sz = ScriptString_pSize(current_line->ssa);
646 else sz = 0;
647 if (sz)
648 current_line->width = sz->cx;
649 else
650 prev = 0;
651 next = prev - 1;
652 } while (prev && current_line->width > fw);
653 current_line->net_length = w;
655 if (prev == 0) { /* Didn't find a line break so force a break */
656 INT *piDx;
657 const INT *count;
659 EDIT_InvalidateUniscribeData_linedef(current_line);
660 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
662 if (current_line->ssa)
664 count = ScriptString_pcOutChars(current_line->ssa);
665 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
666 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
668 prev = current_line->net_length-1;
669 do {
670 current_line->width -= piDx[prev];
671 prev--;
672 } while ( prev > 0 && current_line->width > fw);
673 if (prev<=0)
674 prev = 1;
675 HeapFree(GetProcessHeap(),0,piDx);
677 else
678 prev = (fw / es->char_width);
681 /* If the first line we are calculating, wrapped before istart, we must
682 * adjust istart in order for this to be reflected in the update region. */
683 if (current_line->index == nstart_index && istart > current_line->index + prev)
684 istart = current_line->index + prev;
685 /* else if we are updating the previous line before the first line we
686 * are re-calculating and it expanded */
687 else if (current_line == start_line &&
688 current_line->index != nstart_index && orig_net_length < prev)
690 /* Line expanded due to an upwards line wrap so we must partially include
691 * previous line in update region */
692 nstart_line = line_index;
693 nstart_index = current_line->index;
694 istart = current_line->index + orig_net_length;
697 current_line->net_length = prev;
698 current_line->ending = END_WRAP;
700 if (current_line->net_length > 0)
702 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
703 if (current_line->ssa)
705 sz = ScriptString_pSize(current_line->ssa);
706 current_line->width = sz->cx;
708 else
709 current_line->width = 0;
711 else current_line->width = 0;
713 else if (current_line == start_line &&
714 current_line->index != nstart_index &&
715 orig_net_length < current_line->net_length) {
716 /* The previous line expanded but it's still not as wide as the client rect */
717 /* The expansion is due to an upwards line wrap so we must partially include
718 it in the update region */
719 nstart_line = line_index;
720 nstart_index = current_line->index;
721 istart = current_line->index + orig_net_length;
726 /* Adjust length to include line termination */
727 switch (current_line->ending) {
728 case END_SOFT:
729 current_line->length = current_line->net_length + 3;
730 break;
731 case END_RICH:
732 current_line->length = current_line->net_length + 1;
733 break;
734 case END_HARD:
735 current_line->length = current_line->net_length + 2;
736 break;
737 case END_WRAP:
738 case END_0:
739 current_line->length = current_line->net_length;
740 break;
742 es->text_width = max(es->text_width, current_line->width);
743 current_position += current_line->length;
744 previous_line = current_line;
746 /* Discard data for non-visible lines. It will be calculated as needed */
747 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
748 EDIT_InvalidateUniscribeData_linedef(current_line);
750 current_line = current_line->next;
751 line_index++;
752 } while (previous_line->ending != END_0);
754 /* Finish adjusting line indexes by delta or remove hanging lines */
755 if (previous_line->ending == END_0)
757 LINEDEF *pnext = NULL;
759 previous_line->next = NULL;
760 while (current_line)
762 pnext = current_line->next;
763 EDIT_InvalidateUniscribeData_linedef(current_line);
764 HeapFree(GetProcessHeap(), 0, current_line);
765 current_line = pnext;
766 es->line_count--;
769 else if (delta != 0)
771 while (current_line)
773 current_line->index += delta;
774 current_line = current_line->next;
778 /* Calculate rest of modification rectangle */
779 if (hrgn)
781 HRGN tmphrgn;
783 * We calculate two rectangles. One for the first line which may have
784 * an indent with respect to the format rect. The other is a format-width
785 * rectangle that spans the rest of the lines that changed or moved.
787 rc.top = es->format_rect.top + nstart_line * es->line_height -
788 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
789 rc.bottom = rc.top + es->line_height;
790 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
791 rc.left = es->format_rect.left;
792 else
793 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
794 rc.right = es->format_rect.right;
795 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
797 rc.top = rc.bottom;
798 rc.left = es->format_rect.left;
799 rc.right = es->format_rect.right;
801 * If lines were added or removed we must re-paint the remainder of the
802 * lines since the remaining lines were either shifted up or down.
804 if (line_count < es->line_count) /* We added lines */
805 rc.bottom = es->line_count * es->line_height;
806 else if (line_count > es->line_count) /* We removed lines */
807 rc.bottom = line_count * es->line_height;
808 else
809 rc.bottom = line_index * es->line_height;
810 rc.bottom += es->format_rect.top;
811 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
812 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
813 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
814 DeleteObject(tmphrgn);
818 /*********************************************************************
820 * EDIT_CalcLineWidth_SL
823 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
825 EDIT_UpdateUniscribeData(es, NULL, 0);
826 if (es->ssa)
828 const SIZE *size;
829 size = ScriptString_pSize(es->ssa);
830 es->text_width = size->cx;
832 else
833 es->text_width = 0;
836 /*********************************************************************
838 * EDIT_CharFromPos
840 * Beware: This is not the function called on EM_CHARFROMPOS
841 * The position _can_ be outside the formatting / client
842 * rectangle
843 * The return value is only the character index
846 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
848 INT index;
850 if (es->style & ES_MULTILINE) {
851 int trailing;
852 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
853 INT line_index = 0;
854 LINEDEF *line_def = es->first_line_def;
855 EDIT_UpdateUniscribeData(es, NULL, line);
856 while ((line > 0) && line_def->next) {
857 line_index += line_def->length;
858 line_def = line_def->next;
859 line--;
862 x += es->x_offset - es->format_rect.left;
863 if (es->style & ES_RIGHT)
864 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
865 else if (es->style & ES_CENTER)
866 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
867 if (x >= line_def->width) {
868 if (after_wrap)
869 *after_wrap = (line_def->ending == END_WRAP);
870 return line_index + line_def->net_length;
872 if (x <= 0 || !line_def->ssa) {
873 if (after_wrap)
874 *after_wrap = FALSE;
875 return line_index;
878 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
879 if (trailing) index++;
880 index += line_index;
881 if (after_wrap)
882 *after_wrap = ((index == line_index + line_def->net_length) &&
883 (line_def->ending == END_WRAP));
884 } else {
885 INT xoff = 0;
886 INT trailing;
887 if (after_wrap)
888 *after_wrap = FALSE;
889 x -= es->format_rect.left;
890 if (!x)
891 return es->x_offset;
893 if (!es->x_offset)
895 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
896 if (es->style & ES_RIGHT)
897 x -= indent;
898 else if (es->style & ES_CENTER)
899 x -= indent / 2;
902 EDIT_UpdateUniscribeData(es, NULL, 0);
903 if (es->x_offset)
905 if (es->ssa)
907 if (es->x_offset>= get_text_length(es))
909 const SIZE *size;
910 size = ScriptString_pSize(es->ssa);
911 xoff = size->cx;
913 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
915 else
916 xoff = 0;
918 if (x < 0)
920 if (x + xoff > 0 || !es->ssa)
922 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
923 if (trailing) index++;
925 else
926 index = 0;
928 else
930 if (x)
932 const SIZE *size = NULL;
933 if (es->ssa)
934 size = ScriptString_pSize(es->ssa);
935 if (!size)
936 index = 0;
937 else if (x > size->cx)
938 index = get_text_length(es);
939 else if (es->ssa)
941 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
942 if (trailing) index++;
944 else
945 index = 0;
947 else
948 index = es->x_offset;
951 return index;
955 /*********************************************************************
957 * EDIT_ConfinePoint
959 * adjusts the point to be within the formatting rectangle
960 * (so CharFromPos returns the nearest _visible_ character)
963 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
965 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
966 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
970 /*********************************************************************
972 * EM_LINEFROMCHAR
975 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
977 INT line;
978 LINEDEF *line_def;
980 if (!(es->style & ES_MULTILINE))
981 return 0;
982 if (index > (INT)get_text_length(es))
983 return es->line_count - 1;
984 if (index == -1)
985 index = min(es->selection_start, es->selection_end);
987 line = 0;
988 line_def = es->first_line_def;
989 index -= line_def->length;
990 while ((index >= 0) && line_def->next) {
991 line++;
992 line_def = line_def->next;
993 index -= line_def->length;
995 return line;
999 /*********************************************************************
1001 * EM_LINEINDEX
1004 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
1006 INT line_index;
1007 const LINEDEF *line_def;
1009 if (!(es->style & ES_MULTILINE))
1010 return 0;
1011 if (line >= es->line_count)
1012 return -1;
1014 line_index = 0;
1015 line_def = es->first_line_def;
1016 if (line == -1) {
1017 INT index = es->selection_end - line_def->length;
1018 while ((index >= 0) && line_def->next) {
1019 line_index += line_def->length;
1020 line_def = line_def->next;
1021 index -= line_def->length;
1023 } else {
1024 while (line > 0) {
1025 line_index += line_def->length;
1026 line_def = line_def->next;
1027 line--;
1030 return line_index;
1034 /*********************************************************************
1036 * EM_LINELENGTH
1039 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1041 LINEDEF *line_def;
1043 if (!(es->style & ES_MULTILINE))
1044 return get_text_length(es);
1046 if (index == -1) {
1047 /* get the number of remaining non-selected chars of selected lines */
1048 INT32 l; /* line number */
1049 INT32 li; /* index of first char in line */
1050 INT32 count;
1051 l = EDIT_EM_LineFromChar(es, es->selection_start);
1052 /* # chars before start of selection area */
1053 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1054 l = EDIT_EM_LineFromChar(es, es->selection_end);
1055 /* # chars after end of selection */
1056 li = EDIT_EM_LineIndex(es, l);
1057 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1058 return count;
1060 line_def = es->first_line_def;
1061 index -= line_def->length;
1062 while ((index >= 0) && line_def->next) {
1063 line_def = line_def->next;
1064 index -= line_def->length;
1066 return line_def->net_length;
1070 /*********************************************************************
1072 * EM_POSFROMCHAR
1075 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1077 INT len = get_text_length(es);
1078 INT l;
1079 INT li;
1080 INT x = 0;
1081 INT y = 0;
1082 INT w;
1083 INT lw;
1084 LINEDEF *line_def;
1086 index = min(index, len);
1087 if (es->style & ES_MULTILINE) {
1088 l = EDIT_EM_LineFromChar(es, index);
1089 EDIT_UpdateUniscribeData(es, NULL, l);
1091 y = (l - es->y_offset) * es->line_height;
1092 li = EDIT_EM_LineIndex(es, l);
1093 if (after_wrap && (li == index) && l) {
1094 INT l2 = l - 1;
1095 line_def = es->first_line_def;
1096 while (l2) {
1097 line_def = line_def->next;
1098 l2--;
1100 if (line_def->ending == END_WRAP) {
1101 l--;
1102 y -= es->line_height;
1103 li = EDIT_EM_LineIndex(es, l);
1107 line_def = es->first_line_def;
1108 while (line_def->index != li)
1109 line_def = line_def->next;
1111 lw = line_def->width;
1112 w = es->format_rect.right - es->format_rect.left;
1113 if (line_def->ssa)
1115 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1116 x -= es->x_offset;
1118 else
1119 x = es->x_offset;
1121 if (es->style & ES_RIGHT)
1122 x = w - (lw - x);
1123 else if (es->style & ES_CENTER)
1124 x += (w - lw) / 2;
1125 } else {
1126 INT xoff = 0;
1127 INT xi = 0;
1128 EDIT_UpdateUniscribeData(es, NULL, 0);
1129 if (es->x_offset)
1131 if (es->ssa)
1133 if (es->x_offset >= get_text_length(es))
1135 int leftover = es->x_offset - get_text_length(es);
1136 if (es->ssa)
1138 const SIZE *size;
1139 size = ScriptString_pSize(es->ssa);
1140 xoff = size->cx;
1142 else
1143 xoff = 0;
1144 xoff += es->char_width * leftover;
1146 else
1147 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1149 else
1150 xoff = 0;
1152 if (index)
1154 if (index >= get_text_length(es))
1156 if (es->ssa)
1158 const SIZE *size;
1159 size = ScriptString_pSize(es->ssa);
1160 xi = size->cx;
1162 else
1163 xi = 0;
1165 else if (es->ssa)
1166 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1167 else
1168 xi = 0;
1170 x = xi - xoff;
1172 if (index >= es->x_offset) {
1173 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1175 w = es->format_rect.right - es->format_rect.left;
1176 if (w > es->text_width)
1178 if (es->style & ES_RIGHT)
1179 x += w - es->text_width;
1180 else if (es->style & ES_CENTER)
1181 x += (w - es->text_width) / 2;
1185 y = 0;
1187 x += es->format_rect.left;
1188 y += es->format_rect.top;
1189 return MAKELONG((INT16)x, (INT16)y);
1193 /*********************************************************************
1195 * EDIT_GetLineRect
1197 * Calculates the bounding rectangle for a line from a starting
1198 * column to an ending column.
1201 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1203 SCRIPT_STRING_ANALYSIS ssa;
1204 INT line_index = 0;
1205 INT pt1, pt2, pt3;
1207 if (es->style & ES_MULTILINE)
1209 const LINEDEF *line_def = NULL;
1210 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1211 if (line >= es->line_count)
1212 return;
1214 line_def = es->first_line_def;
1215 if (line == -1) {
1216 INT index = es->selection_end - line_def->length;
1217 while ((index >= 0) && line_def->next) {
1218 line_index += line_def->length;
1219 line_def = line_def->next;
1220 index -= line_def->length;
1222 } else {
1223 while (line > 0) {
1224 line_index += line_def->length;
1225 line_def = line_def->next;
1226 line--;
1229 ssa = line_def->ssa;
1231 else
1233 line_index = 0;
1234 rc->top = es->format_rect.top;
1235 ssa = es->ssa;
1238 rc->bottom = rc->top + es->line_height;
1239 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1240 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1241 if (ssa)
1243 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1244 pt3+=es->format_rect.left;
1246 else pt3 = pt1;
1247 rc->right = max(max(pt1 , pt2),pt3);
1248 rc->left = min(min(pt1, pt2),pt3);
1252 static inline void text_buffer_changed(EDITSTATE *es)
1254 es->text_length = (UINT)-1;
1256 HeapFree( GetProcessHeap(), 0, es->logAttr );
1257 es->logAttr = NULL;
1258 EDIT_InvalidateUniscribeData(es);
1261 /*********************************************************************
1262 * EDIT_LockBuffer
1265 static void EDIT_LockBuffer(EDITSTATE *es)
1267 if (!es->text) {
1269 if(!es->hloc32W) return;
1271 if(es->hloc32A)
1273 CHAR *textA = LocalLock(es->hloc32A);
1274 HLOCAL hloc32W_new;
1275 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1276 if(countW_new > es->buffer_size + 1)
1278 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1279 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1280 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1281 if(hloc32W_new)
1283 es->hloc32W = hloc32W_new;
1284 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1285 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1287 else
1288 WARN("FAILED! Will synchronize partially\n");
1290 es->text = LocalLock(es->hloc32W);
1291 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1292 LocalUnlock(es->hloc32A);
1294 else es->text = LocalLock(es->hloc32W);
1296 es->lock_count++;
1300 /*********************************************************************
1302 * EDIT_UnlockBuffer
1305 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1307 /* Edit window might be already destroyed */
1308 if(!IsWindow(es->hwndSelf))
1310 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1311 return;
1314 if (!es->lock_count) {
1315 ERR("lock_count == 0 ... please report\n");
1316 return;
1318 if (!es->text) {
1319 ERR("es->text == 0 ... please report\n");
1320 return;
1322 if (force || (es->lock_count == 1)) {
1323 if (es->hloc32W) {
1324 UINT countA = 0;
1325 UINT countW = get_text_length(es) + 1;
1327 if(es->hloc32A)
1329 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1330 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1331 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1332 countA = LocalSize(es->hloc32A);
1333 if(countA_new > countA)
1335 HLOCAL hloc32A_new;
1336 UINT alloc_size = ROUND_TO_GROW(countA_new);
1337 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1338 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1339 if(hloc32A_new)
1341 es->hloc32A = hloc32A_new;
1342 countA = LocalSize(hloc32A_new);
1343 TRACE("Real new size %d bytes\n", countA);
1345 else
1346 WARN("FAILED! Will synchronize partially\n");
1348 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1349 LocalLock(es->hloc32A), countA, NULL, NULL);
1350 LocalUnlock(es->hloc32A);
1353 LocalUnlock(es->hloc32W);
1354 es->text = NULL;
1356 else {
1357 ERR("no buffer ... please report\n");
1358 return;
1361 es->lock_count--;
1365 /*********************************************************************
1367 * EDIT_MakeFit
1369 * Try to fit size + 1 characters in the buffer.
1371 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1373 HLOCAL hNew32W;
1375 if (size <= es->buffer_size)
1376 return TRUE;
1378 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1380 /* Force edit to unlock its buffer. es->text now NULL */
1381 EDIT_UnlockBuffer(es, TRUE);
1383 if (es->hloc32W) {
1384 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1385 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1386 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1387 es->hloc32W = hNew32W;
1388 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1392 EDIT_LockBuffer(es);
1394 if (es->buffer_size < size) {
1395 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1396 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1397 return FALSE;
1398 } else {
1399 TRACE("We now have %d+1\n", es->buffer_size);
1400 return TRUE;
1405 /*********************************************************************
1407 * EDIT_MakeUndoFit
1409 * Try to fit size + 1 bytes in the undo buffer.
1412 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1414 UINT alloc_size;
1416 if (size <= es->undo_buffer_size)
1417 return TRUE;
1419 TRACE("trying to ReAlloc to %d+1\n", size);
1421 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1422 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1423 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1424 return TRUE;
1426 else
1428 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1429 return FALSE;
1434 /*********************************************************************
1436 * EDIT_UpdateTextRegion
1439 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1441 if (es->flags & EF_UPDATE) {
1442 es->flags &= ~EF_UPDATE;
1443 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1445 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1449 /*********************************************************************
1451 * EDIT_UpdateText
1454 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1456 if (es->flags & EF_UPDATE) {
1457 es->flags &= ~EF_UPDATE;
1458 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1460 InvalidateRect(es->hwndSelf, rc, bErase);
1463 /*********************************************************************
1465 * EDIT_SL_InvalidateText
1467 * Called from EDIT_InvalidateText().
1468 * Does the job for single-line controls only.
1471 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1473 RECT line_rect;
1474 RECT rc;
1476 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1477 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1478 EDIT_UpdateText(es, &rc, TRUE);
1481 /*********************************************************************
1483 * EDIT_ML_InvalidateText
1485 * Called from EDIT_InvalidateText().
1486 * Does the job for multi-line controls only.
1489 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1491 INT vlc = get_vertical_line_count(es);
1492 INT sl = EDIT_EM_LineFromChar(es, start);
1493 INT el = EDIT_EM_LineFromChar(es, end);
1494 INT sc;
1495 INT ec;
1496 RECT rc1;
1497 RECT rcWnd;
1498 RECT rcLine;
1499 RECT rcUpdate;
1500 INT l;
1502 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1503 return;
1505 sc = start - EDIT_EM_LineIndex(es, sl);
1506 ec = end - EDIT_EM_LineIndex(es, el);
1507 if (sl < es->y_offset) {
1508 sl = es->y_offset;
1509 sc = 0;
1511 if (el > es->y_offset + vlc) {
1512 el = es->y_offset + vlc;
1513 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1515 GetClientRect(es->hwndSelf, &rc1);
1516 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1517 if (sl == el) {
1518 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1519 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1520 EDIT_UpdateText(es, &rcUpdate, TRUE);
1521 } else {
1522 EDIT_GetLineRect(es, sl, sc,
1523 EDIT_EM_LineLength(es,
1524 EDIT_EM_LineIndex(es, sl)),
1525 &rcLine);
1526 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1527 EDIT_UpdateText(es, &rcUpdate, TRUE);
1528 for (l = sl + 1 ; l < el ; l++) {
1529 EDIT_GetLineRect(es, l, 0,
1530 EDIT_EM_LineLength(es,
1531 EDIT_EM_LineIndex(es, l)),
1532 &rcLine);
1533 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1534 EDIT_UpdateText(es, &rcUpdate, TRUE);
1536 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1537 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1538 EDIT_UpdateText(es, &rcUpdate, TRUE);
1543 /*********************************************************************
1545 * EDIT_InvalidateText
1547 * Invalidate the text from offset start up to, but not including,
1548 * offset end. Useful for (re)painting the selection.
1549 * Regions outside the linewidth are not invalidated.
1550 * end == -1 means end == TextLength.
1551 * start and end need not be ordered.
1554 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1556 if (end == start)
1557 return;
1559 if (end == -1)
1560 end = get_text_length(es);
1562 if (end < start) {
1563 INT tmp = start;
1564 start = end;
1565 end = tmp;
1568 if (es->style & ES_MULTILINE)
1569 EDIT_ML_InvalidateText(es, start, end);
1570 else
1571 EDIT_SL_InvalidateText(es, start, end);
1575 /*********************************************************************
1577 * EDIT_EM_SetSel
1579 * note: unlike the specs say: the order of start and end
1580 * _is_ preserved in Windows. (i.e. start can be > end)
1581 * In other words: this handler is OK
1584 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1586 UINT old_start = es->selection_start;
1587 UINT old_end = es->selection_end;
1588 UINT len = get_text_length(es);
1590 if (start == (UINT)-1) {
1591 start = es->selection_end;
1592 end = es->selection_end;
1593 } else {
1594 start = min(start, len);
1595 end = min(end, len);
1597 es->selection_start = start;
1598 es->selection_end = end;
1599 if (after_wrap)
1600 es->flags |= EF_AFTER_WRAP;
1601 else
1602 es->flags &= ~EF_AFTER_WRAP;
1603 /* Compute the necessary invalidation region. */
1604 /* Note that we don't need to invalidate regions which have
1605 * "never" been selected, or those which are "still" selected.
1606 * In fact, every time we hit a selection boundary, we can
1607 * *toggle* whether we need to invalidate. Thus we can optimize by
1608 * *sorting* the interval endpoints. Let's assume that we sort them
1609 * in this order:
1610 * start <= end <= old_start <= old_end
1611 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1612 * in 5 comparisons; i.e. it is impossible to do better than the
1613 * following: */
1614 ORDER_UINT(end, old_end);
1615 ORDER_UINT(start, old_start);
1616 ORDER_UINT(old_start, old_end);
1617 ORDER_UINT(start, end);
1618 /* Note that at this point 'end' and 'old_start' are not in order, but
1619 * start is definitely the min. and old_end is definitely the max. */
1620 if (end != old_start)
1623 * One can also do
1624 * ORDER_UINT32(end, old_start);
1625 * EDIT_InvalidateText(es, start, end);
1626 * EDIT_InvalidateText(es, old_start, old_end);
1627 * in place of the following if statement.
1628 * (That would complete the optimal five-comparison four-element sort.)
1630 if (old_start > end )
1632 EDIT_InvalidateText(es, start, end);
1633 EDIT_InvalidateText(es, old_start, old_end);
1635 else
1637 EDIT_InvalidateText(es, start, old_start);
1638 EDIT_InvalidateText(es, end, old_end);
1641 else EDIT_InvalidateText(es, start, old_end);
1645 /*********************************************************************
1647 * EDIT_UpdateScrollInfo
1650 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1652 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1654 SCROLLINFO si;
1655 si.cbSize = sizeof(SCROLLINFO);
1656 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1657 si.nMin = 0;
1658 si.nMax = es->line_count - 1;
1659 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1660 si.nPos = es->y_offset;
1661 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1662 si.nMin, si.nMax, si.nPage, si.nPos);
1663 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1666 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1668 SCROLLINFO si;
1669 si.cbSize = sizeof(SCROLLINFO);
1670 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1671 si.nMin = 0;
1672 si.nMax = es->text_width - 1;
1673 si.nPage = es->format_rect.right - es->format_rect.left;
1674 si.nPos = es->x_offset;
1675 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1676 si.nMin, si.nMax, si.nPage, si.nPos);
1677 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1682 /*********************************************************************
1684 * EDIT_EM_LineScroll_internal
1686 * Version of EDIT_EM_LineScroll for internal use.
1687 * It doesn't refuse if ES_MULTILINE is set and assumes that
1688 * dx is in pixels, dy - in lines.
1691 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1693 INT nyoff;
1694 INT x_offset_in_pixels;
1695 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1696 es->line_height;
1698 if (es->style & ES_MULTILINE)
1700 x_offset_in_pixels = es->x_offset;
1702 else
1704 dy = 0;
1705 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1708 if (-dx > x_offset_in_pixels)
1709 dx = -x_offset_in_pixels;
1710 if (dx > es->text_width - x_offset_in_pixels)
1711 dx = es->text_width - x_offset_in_pixels;
1712 nyoff = max(0, es->y_offset + dy);
1713 if (nyoff >= es->line_count - lines_per_page)
1714 nyoff = max(0, es->line_count - lines_per_page);
1715 dy = (es->y_offset - nyoff) * es->line_height;
1716 if (dx || dy) {
1717 RECT rc1;
1718 RECT rc;
1720 es->y_offset = nyoff;
1721 if(es->style & ES_MULTILINE)
1722 es->x_offset += dx;
1723 else
1724 es->x_offset += dx / es->char_width;
1726 GetClientRect(es->hwndSelf, &rc1);
1727 IntersectRect(&rc, &rc1, &es->format_rect);
1728 ScrollWindowEx(es->hwndSelf, -dx, dy,
1729 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1730 /* force scroll info update */
1731 EDIT_UpdateScrollInfo(es);
1733 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1734 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1735 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1736 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1737 return TRUE;
1740 /*********************************************************************
1742 * EM_LINESCROLL
1744 * NOTE: dx is in average character widths, dy - in lines;
1747 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1749 if (!(es->style & ES_MULTILINE))
1750 return FALSE;
1752 dx *= es->char_width;
1753 return EDIT_EM_LineScroll_internal(es, dx, dy);
1757 /*********************************************************************
1759 * EM_SCROLL
1762 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1764 INT dy;
1766 if (!(es->style & ES_MULTILINE))
1767 return (LRESULT)FALSE;
1769 dy = 0;
1771 switch (action) {
1772 case SB_LINEUP:
1773 if (es->y_offset)
1774 dy = -1;
1775 break;
1776 case SB_LINEDOWN:
1777 if (es->y_offset < es->line_count - 1)
1778 dy = 1;
1779 break;
1780 case SB_PAGEUP:
1781 if (es->y_offset)
1782 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1783 break;
1784 case SB_PAGEDOWN:
1785 if (es->y_offset < es->line_count - 1)
1786 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1787 break;
1788 default:
1789 return (LRESULT)FALSE;
1791 if (dy) {
1792 INT vlc = get_vertical_line_count(es);
1793 /* check if we are going to move too far */
1794 if(es->y_offset + dy > es->line_count - vlc)
1795 dy = max(es->line_count - vlc, 0) - es->y_offset;
1797 /* Notification is done in EDIT_EM_LineScroll */
1798 if(dy) {
1799 EDIT_EM_LineScroll(es, 0, dy);
1800 return MAKELONG(dy, TRUE);
1804 return (LRESULT)FALSE;
1808 /*********************************************************************
1810 * EDIT_SetCaretPos
1813 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1814 BOOL after_wrap)
1816 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1817 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1818 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1822 /*********************************************************************
1824 * EM_SCROLLCARET
1827 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1829 if (es->style & ES_MULTILINE) {
1830 INT l;
1831 INT vlc;
1832 INT ww;
1833 INT cw = es->char_width;
1834 INT x;
1835 INT dy = 0;
1836 INT dx = 0;
1838 l = EDIT_EM_LineFromChar(es, es->selection_end);
1839 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1840 vlc = get_vertical_line_count(es);
1841 if (l >= es->y_offset + vlc)
1842 dy = l - vlc + 1 - es->y_offset;
1843 if (l < es->y_offset)
1844 dy = l - es->y_offset;
1845 ww = es->format_rect.right - es->format_rect.left;
1846 if (x < es->format_rect.left)
1847 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1848 if (x > es->format_rect.right)
1849 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1850 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1852 /* check if we are going to move too far */
1853 if(es->x_offset + dx + ww > es->text_width)
1854 dx = es->text_width - ww - es->x_offset;
1855 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1856 EDIT_EM_LineScroll_internal(es, dx, dy);
1858 } else {
1859 INT x;
1860 INT goal;
1861 INT format_width;
1863 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1864 format_width = es->format_rect.right - es->format_rect.left;
1865 if (x < es->format_rect.left) {
1866 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1867 do {
1868 es->x_offset--;
1869 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1870 } while ((x < goal) && es->x_offset);
1871 /* FIXME: use ScrollWindow() somehow to improve performance */
1872 EDIT_UpdateText(es, NULL, TRUE);
1873 } else if (x > es->format_rect.right) {
1874 INT x_last;
1875 INT len = get_text_length(es);
1876 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1877 do {
1878 es->x_offset++;
1879 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1880 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1881 } while ((x > goal) && (x_last > es->format_rect.right));
1882 /* FIXME: use ScrollWindow() somehow to improve performance */
1883 EDIT_UpdateText(es, NULL, TRUE);
1887 if(es->flags & EF_FOCUSED)
1888 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1892 /*********************************************************************
1894 * EDIT_MoveBackward
1897 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1899 INT e = es->selection_end;
1901 if (e) {
1902 e--;
1903 if ((es->style & ES_MULTILINE) && e &&
1904 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1905 e--;
1906 if (e && (es->text[e - 1] == '\r'))
1907 e--;
1910 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1911 EDIT_EM_ScrollCaret(es);
1915 /*********************************************************************
1917 * EDIT_MoveDown_ML
1919 * Only for multi line controls
1920 * Move the caret one line down, on a column with the nearest
1921 * x coordinate on the screen (might be a different column).
1924 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1926 INT s = es->selection_start;
1927 INT e = es->selection_end;
1928 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1929 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1930 INT x = (short)LOWORD(pos);
1931 INT y = (short)HIWORD(pos);
1933 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1934 if (!extend)
1935 s = e;
1936 EDIT_EM_SetSel(es, s, e, after_wrap);
1937 EDIT_EM_ScrollCaret(es);
1941 /*********************************************************************
1943 * EDIT_MoveEnd
1946 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1948 BOOL after_wrap = FALSE;
1949 INT e;
1951 /* Pass a high value in x to make sure of receiving the end of the line */
1952 if (!ctrl && (es->style & ES_MULTILINE))
1953 e = EDIT_CharFromPos(es, 0x3fffffff,
1954 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1955 else
1956 e = get_text_length(es);
1957 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1958 EDIT_EM_ScrollCaret(es);
1962 /*********************************************************************
1964 * EDIT_MoveForward
1967 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1969 INT e = es->selection_end;
1971 if (es->text[e]) {
1972 e++;
1973 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1974 if (es->text[e] == '\n')
1975 e++;
1976 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1977 e += 2;
1980 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1981 EDIT_EM_ScrollCaret(es);
1985 /*********************************************************************
1987 * EDIT_MoveHome
1989 * Home key: move to beginning of line.
1992 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1994 INT e;
1996 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1997 if (!ctrl && (es->style & ES_MULTILINE))
1998 e = EDIT_CharFromPos(es, -es->x_offset,
1999 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2000 else
2001 e = 0;
2002 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2003 EDIT_EM_ScrollCaret(es);
2007 /*********************************************************************
2009 * EDIT_MovePageDown_ML
2011 * Only for multi line controls
2012 * Move the caret one page down, on a column with the nearest
2013 * x coordinate on the screen (might be a different column).
2016 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2018 INT s = es->selection_start;
2019 INT e = es->selection_end;
2020 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2021 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2022 INT x = (short)LOWORD(pos);
2023 INT y = (short)HIWORD(pos);
2025 e = EDIT_CharFromPos(es, x,
2026 y + (es->format_rect.bottom - es->format_rect.top),
2027 &after_wrap);
2028 if (!extend)
2029 s = e;
2030 EDIT_EM_SetSel(es, s, e, after_wrap);
2031 EDIT_EM_ScrollCaret(es);
2035 /*********************************************************************
2037 * EDIT_MovePageUp_ML
2039 * Only for multi line controls
2040 * Move the caret one page up, on a column with the nearest
2041 * x coordinate on the screen (might be a different column).
2044 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2046 INT s = es->selection_start;
2047 INT e = es->selection_end;
2048 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2049 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2050 INT x = (short)LOWORD(pos);
2051 INT y = (short)HIWORD(pos);
2053 e = EDIT_CharFromPos(es, x,
2054 y - (es->format_rect.bottom - es->format_rect.top),
2055 &after_wrap);
2056 if (!extend)
2057 s = e;
2058 EDIT_EM_SetSel(es, s, e, after_wrap);
2059 EDIT_EM_ScrollCaret(es);
2063 /*********************************************************************
2065 * EDIT_MoveUp_ML
2067 * Only for multi line controls
2068 * Move the caret one line up, on a column with the nearest
2069 * x coordinate on the screen (might be a different column).
2072 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2074 INT s = es->selection_start;
2075 INT e = es->selection_end;
2076 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2077 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2078 INT x = (short)LOWORD(pos);
2079 INT y = (short)HIWORD(pos);
2081 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2082 if (!extend)
2083 s = e;
2084 EDIT_EM_SetSel(es, s, e, after_wrap);
2085 EDIT_EM_ScrollCaret(es);
2089 /*********************************************************************
2091 * EDIT_MoveWordBackward
2094 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2096 INT s = es->selection_start;
2097 INT e = es->selection_end;
2098 INT l;
2099 INT ll;
2100 INT li;
2102 l = EDIT_EM_LineFromChar(es, e);
2103 ll = EDIT_EM_LineLength(es, e);
2104 li = EDIT_EM_LineIndex(es, l);
2105 if (e - li == 0) {
2106 if (l) {
2107 li = EDIT_EM_LineIndex(es, l - 1);
2108 e = li + EDIT_EM_LineLength(es, li);
2110 } else {
2111 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2113 if (!extend)
2114 s = e;
2115 EDIT_EM_SetSel(es, s, e, FALSE);
2116 EDIT_EM_ScrollCaret(es);
2120 /*********************************************************************
2122 * EDIT_MoveWordForward
2125 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2127 INT s = es->selection_start;
2128 INT e = es->selection_end;
2129 INT l;
2130 INT ll;
2131 INT li;
2133 l = EDIT_EM_LineFromChar(es, e);
2134 ll = EDIT_EM_LineLength(es, e);
2135 li = EDIT_EM_LineIndex(es, l);
2136 if (e - li == ll) {
2137 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2138 e = EDIT_EM_LineIndex(es, l + 1);
2139 } else {
2140 e = li + EDIT_CallWordBreakProc(es,
2141 li, e - li + 1, ll, WB_RIGHT);
2143 if (!extend)
2144 s = e;
2145 EDIT_EM_SetSel(es, s, e, FALSE);
2146 EDIT_EM_ScrollCaret(es);
2150 /*********************************************************************
2152 * EDIT_PaintText
2155 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2157 COLORREF BkColor;
2158 COLORREF TextColor;
2159 LOGFONTW underline_font;
2160 HFONT hUnderline = 0;
2161 HFONT old_font = 0;
2162 INT ret;
2163 INT li;
2164 INT BkMode;
2165 SIZE size;
2167 if (!count)
2168 return 0;
2169 BkMode = GetBkMode(dc);
2170 BkColor = GetBkColor(dc);
2171 TextColor = GetTextColor(dc);
2172 if (rev) {
2173 if (es->composition_len == 0)
2175 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2176 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2177 SetBkMode( dc, OPAQUE);
2179 else
2181 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2182 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2183 underline_font.lfUnderline = TRUE;
2184 hUnderline = CreateFontIndirectW(&underline_font);
2185 old_font = SelectObject(dc,hUnderline);
2188 li = EDIT_EM_LineIndex(es, line);
2189 if (es->style & ES_MULTILINE) {
2190 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2191 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2192 } else {
2193 TextOutW(dc, x, y, es->text + li + col, count);
2194 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2195 ret = size.cx;
2197 if (rev) {
2198 if (es->composition_len == 0)
2200 SetBkColor(dc, BkColor);
2201 SetTextColor(dc, TextColor);
2202 SetBkMode( dc, BkMode);
2204 else
2206 if (old_font)
2207 SelectObject(dc,old_font);
2208 if (hUnderline)
2209 DeleteObject(hUnderline);
2212 return ret;
2216 /*********************************************************************
2218 * EDIT_PaintLine
2221 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2223 INT s = 0;
2224 INT e = 0;
2225 INT li = 0;
2226 INT ll = 0;
2227 INT x;
2228 INT y;
2229 LRESULT pos;
2230 SCRIPT_STRING_ANALYSIS ssa;
2232 if (es->style & ES_MULTILINE) {
2233 INT vlc = get_vertical_line_count(es);
2235 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2236 return;
2237 } else if (line)
2238 return;
2240 TRACE("line=%d\n", line);
2242 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2243 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2244 x = (short)LOWORD(pos);
2245 y = (short)HIWORD(pos);
2247 if (es->style & ES_MULTILINE)
2249 int line_idx = line;
2250 x = -es->x_offset;
2251 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2253 LINEDEF *line_def = es->first_line_def;
2254 int w, lw;
2256 while (line_def && line_idx)
2258 line_def = line_def->next;
2259 line_idx--;
2261 w = es->format_rect.right - es->format_rect.left;
2262 lw = line_def->width;
2264 if (es->style & ES_RIGHT)
2265 x = w - (lw - x);
2266 else if (es->style & ES_CENTER)
2267 x += (w - lw) / 2;
2269 x += es->format_rect.left;
2272 if (rev)
2274 li = EDIT_EM_LineIndex(es, line);
2275 ll = EDIT_EM_LineLength(es, li);
2276 s = min(es->selection_start, es->selection_end);
2277 e = max(es->selection_start, es->selection_end);
2278 s = min(li + ll, max(li, s));
2279 e = min(li + ll, max(li, e));
2282 if (ssa)
2283 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2284 else if (rev && (s != e) &&
2285 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2286 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2287 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2288 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2289 } else
2290 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2294 /*********************************************************************
2296 * EDIT_AdjustFormatRect
2298 * Adjusts the format rectangle for the current font and the
2299 * current client rectangle.
2302 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2304 RECT ClientRect;
2306 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2307 if (es->style & ES_MULTILINE)
2309 INT fw, vlc, max_x_offset, max_y_offset;
2311 vlc = get_vertical_line_count(es);
2312 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2314 /* correct es->x_offset */
2315 fw = es->format_rect.right - es->format_rect.left;
2316 max_x_offset = es->text_width - fw;
2317 if(max_x_offset < 0) max_x_offset = 0;
2318 if(es->x_offset > max_x_offset)
2319 es->x_offset = max_x_offset;
2321 /* correct es->y_offset */
2322 max_y_offset = es->line_count - vlc;
2323 if(max_y_offset < 0) max_y_offset = 0;
2324 if(es->y_offset > max_y_offset)
2325 es->y_offset = max_y_offset;
2327 /* force scroll info update */
2328 EDIT_UpdateScrollInfo(es);
2330 else
2331 /* Windows doesn't care to fix text placement for SL controls */
2332 es->format_rect.bottom = es->format_rect.top + es->line_height;
2334 /* Always stay within the client area */
2335 GetClientRect(es->hwndSelf, &ClientRect);
2336 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2338 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2339 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2341 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2345 /*********************************************************************
2347 * EDIT_SetRectNP
2349 * note: this is not (exactly) the handler called on EM_SETRECTNP
2350 * it is also used to set the rect of a single line control
2353 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2355 LONG_PTR ExStyle;
2356 INT bw, bh;
2357 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2359 CopyRect(&es->format_rect, rc);
2361 if (ExStyle & WS_EX_CLIENTEDGE) {
2362 es->format_rect.left++;
2363 es->format_rect.right--;
2365 if (es->format_rect.bottom - es->format_rect.top
2366 >= es->line_height + 2)
2368 es->format_rect.top++;
2369 es->format_rect.bottom--;
2372 else if (es->style & WS_BORDER) {
2373 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2374 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2375 InflateRect(&es->format_rect, -bw, 0);
2376 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2377 InflateRect(&es->format_rect, 0, -bh);
2380 es->format_rect.left += es->left_margin;
2381 es->format_rect.right -= es->right_margin;
2382 EDIT_AdjustFormatRect(es);
2386 /*********************************************************************
2388 * EM_CHARFROMPOS
2390 * returns line number (not index) in high-order word of result.
2391 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2392 * to Richedit, not to the edit control. Original documentation is valid.
2393 * FIXME: do the specs mean to return -1 if outside client area or
2394 * if outside formatting rectangle ???
2397 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2399 POINT pt;
2400 RECT rc;
2401 INT index;
2403 pt.x = x;
2404 pt.y = y;
2405 GetClientRect(es->hwndSelf, &rc);
2406 if (!PtInRect(&rc, pt))
2407 return -1;
2409 index = EDIT_CharFromPos(es, x, y, NULL);
2410 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2414 /*********************************************************************
2416 * EM_FMTLINES
2418 * Enable or disable soft breaks.
2420 * This means: insert or remove the soft linebreak character (\r\r\n).
2421 * Take care to check if the text still fits the buffer after insertion.
2422 * If not, notify with EN_ERRSPACE.
2425 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2427 es->flags &= ~EF_USE_SOFTBRK;
2428 if (add_eol) {
2429 es->flags |= EF_USE_SOFTBRK;
2430 FIXME("soft break enabled, not implemented\n");
2432 return add_eol;
2436 /*********************************************************************
2438 * EM_GETHANDLE
2440 * Hopefully this won't fire back at us.
2441 * We always start with a fixed buffer in the local heap.
2442 * Despite of the documentation says that the local heap is used
2443 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2444 * buffer on the local heap.
2447 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2449 HLOCAL hLocal;
2451 if (!(es->style & ES_MULTILINE))
2452 return 0;
2454 if(es->is_unicode)
2455 hLocal = es->hloc32W;
2456 else
2458 if(!es->hloc32A)
2460 CHAR *textA;
2461 UINT countA, alloc_size;
2462 TRACE("Allocating 32-bit ANSI alias buffer\n");
2463 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2464 alloc_size = ROUND_TO_GROW(countA);
2465 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2467 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2468 return 0;
2470 textA = LocalLock(es->hloc32A);
2471 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2472 LocalUnlock(es->hloc32A);
2474 hLocal = es->hloc32A;
2477 EDIT_UnlockBuffer(es, TRUE);
2479 /* The text buffer handle belongs to the app */
2480 es->hlocapp = hLocal;
2482 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2483 return hLocal;
2487 /*********************************************************************
2489 * EM_GETLINE
2492 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2494 LPWSTR src;
2495 INT line_len, dst_len;
2496 INT i;
2498 if (es->style & ES_MULTILINE) {
2499 if (line >= es->line_count)
2500 return 0;
2501 } else
2502 line = 0;
2503 i = EDIT_EM_LineIndex(es, line);
2504 src = es->text + i;
2505 line_len = EDIT_EM_LineLength(es, i);
2506 dst_len = *(WORD *)dst;
2507 if(unicode)
2509 if(dst_len <= line_len)
2511 memcpy(dst, src, dst_len * sizeof(WCHAR));
2512 return dst_len;
2514 else /* Append 0 if enough space */
2516 memcpy(dst, src, line_len * sizeof(WCHAR));
2517 dst[line_len] = 0;
2518 return line_len;
2521 else
2523 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2524 if(!ret && line_len) /* Insufficient buffer size */
2525 return dst_len;
2526 if(ret < dst_len) /* Append 0 if enough space */
2527 ((LPSTR)dst)[ret] = 0;
2528 return ret;
2533 /*********************************************************************
2535 * EM_GETSEL
2538 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2540 UINT s = es->selection_start;
2541 UINT e = es->selection_end;
2543 ORDER_UINT(s, e);
2544 if (start)
2545 *start = s;
2546 if (end)
2547 *end = e;
2548 return MAKELONG(s, e);
2552 /*********************************************************************
2554 * EM_REPLACESEL
2556 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2559 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2560 BOOL send_update, BOOL honor_limit)
2562 UINT tl = get_text_length(es);
2563 UINT utl;
2564 UINT s;
2565 UINT e;
2566 UINT i;
2567 UINT size;
2568 LPWSTR p;
2569 HRGN hrgn = 0;
2570 LPWSTR buf = NULL;
2571 UINT bufl;
2573 TRACE("%s, can_undo %d, send_update %d\n",
2574 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2576 s = es->selection_start;
2577 e = es->selection_end;
2579 EDIT_InvalidateUniscribeData(es);
2580 if ((s == e) && !strl)
2581 return;
2583 ORDER_UINT(s, e);
2585 size = tl - (e - s) + strl;
2586 if (!size)
2587 es->text_width = 0;
2589 /* Issue the EN_MAXTEXT notification and continue with replacing text
2590 * so that buffer limit is honored. */
2591 if ((honor_limit) && (size > es->buffer_limit)) {
2592 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2593 /* Buffer limit can be smaller than the actual length of text in combobox */
2594 if (es->buffer_limit < (tl - (e-s)))
2595 strl = 0;
2596 else
2597 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2600 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2601 return;
2603 if (e != s) {
2604 /* there is something to be deleted */
2605 TRACE("deleting stuff.\n");
2606 bufl = e - s;
2607 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2608 if (!buf) return;
2609 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2610 buf[bufl] = 0; /* ensure 0 termination */
2611 /* now delete */
2612 strcpyW(es->text + s, es->text + e);
2613 text_buffer_changed(es);
2615 if (strl) {
2616 /* there is an insertion */
2617 tl = get_text_length(es);
2618 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));
2619 for (p = es->text + tl ; p >= es->text + s ; p--)
2620 p[strl] = p[0];
2621 for (i = 0 , p = es->text + s ; i < strl ; i++)
2622 p[i] = lpsz_replace[i];
2623 if(es->style & ES_UPPERCASE)
2624 CharUpperBuffW(p, strl);
2625 else if(es->style & ES_LOWERCASE)
2626 CharLowerBuffW(p, strl);
2627 text_buffer_changed(es);
2629 if (es->style & ES_MULTILINE)
2631 INT st = min(es->selection_start, es->selection_end);
2632 INT vlc = get_vertical_line_count(es);
2634 hrgn = CreateRectRgn(0, 0, 0, 0);
2635 EDIT_BuildLineDefs_ML(es, st, st + strl,
2636 strl - abs(es->selection_end - es->selection_start), hrgn);
2637 /* if text is too long undo all changes */
2638 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2639 if (strl)
2640 strcpyW(es->text + e, es->text + e + strl);
2641 if (e != s)
2642 for (i = 0 , p = es->text ; i < e - s ; i++)
2643 p[i + s] = buf[i];
2644 text_buffer_changed(es);
2645 EDIT_BuildLineDefs_ML(es, s, e,
2646 abs(es->selection_end - es->selection_start) - strl, hrgn);
2647 strl = 0;
2648 e = s;
2649 hrgn = CreateRectRgn(0, 0, 0, 0);
2650 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2653 else {
2654 INT fw = es->format_rect.right - es->format_rect.left;
2655 EDIT_InvalidateUniscribeData(es);
2656 EDIT_CalcLineWidth_SL(es);
2657 /* remove chars that don't fit */
2658 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2659 while ((es->text_width > fw) && s + strl >= s) {
2660 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2661 strl--;
2662 es->text_length = -1;
2663 EDIT_InvalidateUniscribeData(es);
2664 EDIT_CalcLineWidth_SL(es);
2666 text_buffer_changed(es);
2667 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2671 if (e != s) {
2672 if (can_undo) {
2673 utl = strlenW(es->undo_text);
2674 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2675 /* undo-buffer is extended to the right */
2676 EDIT_MakeUndoFit(es, utl + e - s);
2677 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2678 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2679 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2680 /* undo-buffer is extended to the left */
2681 EDIT_MakeUndoFit(es, utl + e - s);
2682 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2683 p[e - s] = p[0];
2684 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2685 p[i] = buf[i];
2686 es->undo_position = s;
2687 } else {
2688 /* new undo-buffer */
2689 EDIT_MakeUndoFit(es, e - s);
2690 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2691 es->undo_text[e - s] = 0; /* ensure 0 termination */
2692 es->undo_position = s;
2694 /* any deletion makes the old insertion-undo invalid */
2695 es->undo_insert_count = 0;
2696 } else
2697 EDIT_EM_EmptyUndoBuffer(es);
2699 if (strl) {
2700 if (can_undo) {
2701 if ((s == es->undo_position) ||
2702 ((es->undo_insert_count) &&
2703 (s == es->undo_position + es->undo_insert_count)))
2705 * insertion is new and at delete position or
2706 * an extension to either left or right
2708 es->undo_insert_count += strl;
2709 else {
2710 /* new insertion undo */
2711 es->undo_position = s;
2712 es->undo_insert_count = strl;
2713 /* new insertion makes old delete-buffer invalid */
2714 *es->undo_text = '\0';
2716 } else
2717 EDIT_EM_EmptyUndoBuffer(es);
2720 HeapFree(GetProcessHeap(), 0, buf);
2722 s += strl;
2724 /* If text has been deleted and we're right or center aligned then scroll rightward */
2725 if (es->style & (ES_RIGHT | ES_CENTER))
2727 INT delta = strl - abs(es->selection_end - es->selection_start);
2729 if (delta < 0 && es->x_offset)
2731 if (abs(delta) > es->x_offset)
2732 es->x_offset = 0;
2733 else
2734 es->x_offset += delta;
2738 EDIT_EM_SetSel(es, s, s, FALSE);
2739 es->flags |= EF_MODIFIED;
2740 if (send_update) es->flags |= EF_UPDATE;
2741 if (hrgn)
2743 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2744 DeleteObject(hrgn);
2746 else
2747 EDIT_UpdateText(es, NULL, TRUE);
2749 EDIT_EM_ScrollCaret(es);
2751 /* force scroll info update */
2752 EDIT_UpdateScrollInfo(es);
2755 if(send_update || (es->flags & EF_UPDATE))
2757 es->flags &= ~EF_UPDATE;
2758 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2760 EDIT_InvalidateUniscribeData(es);
2764 /*********************************************************************
2766 * EM_SETHANDLE
2768 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2771 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2773 if (!(es->style & ES_MULTILINE))
2774 return;
2776 if (!hloc) {
2777 WARN("called with NULL handle\n");
2778 return;
2781 EDIT_UnlockBuffer(es, TRUE);
2783 if(es->is_unicode)
2785 if(es->hloc32A)
2787 LocalFree(es->hloc32A);
2788 es->hloc32A = NULL;
2790 es->hloc32W = hloc;
2792 else
2794 INT countW, countA;
2795 HLOCAL hloc32W_new;
2796 WCHAR *textW;
2797 CHAR *textA;
2799 countA = LocalSize(hloc);
2800 textA = LocalLock(hloc);
2801 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2802 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2804 ERR("Could not allocate new unicode buffer\n");
2805 return;
2807 textW = LocalLock(hloc32W_new);
2808 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2809 LocalUnlock(hloc32W_new);
2810 LocalUnlock(hloc);
2812 if(es->hloc32W)
2813 LocalFree(es->hloc32W);
2815 es->hloc32W = hloc32W_new;
2816 es->hloc32A = hloc;
2819 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2821 /* The text buffer handle belongs to the control */
2822 es->hlocapp = NULL;
2824 EDIT_LockBuffer(es);
2825 text_buffer_changed(es);
2827 es->x_offset = es->y_offset = 0;
2828 es->selection_start = es->selection_end = 0;
2829 EDIT_EM_EmptyUndoBuffer(es);
2830 es->flags &= ~EF_MODIFIED;
2831 es->flags &= ~EF_UPDATE;
2832 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2833 EDIT_UpdateText(es, NULL, TRUE);
2834 EDIT_EM_ScrollCaret(es);
2835 /* force scroll info update */
2836 EDIT_UpdateScrollInfo(es);
2840 /*********************************************************************
2842 * EM_SETLIMITTEXT
2844 * NOTE: this version currently implements WinNT limits
2847 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2849 if (!limit) limit = ~0u;
2850 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2851 es->buffer_limit = limit;
2855 /*********************************************************************
2857 * EM_SETMARGINS
2859 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2860 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2861 * margin according to the textmetrics of the current font.
2863 * When EC_USEFONTINFO is used in the non_cjk case the margins only
2864 * change if the edit control is equal to or larger than a certain
2865 * size. Though there is an exception for the empty client rect case
2866 * with small font sizes.
2868 static BOOL is_cjk(UINT charset)
2870 switch(charset)
2872 case SHIFTJIS_CHARSET:
2873 case HANGUL_CHARSET:
2874 case GB2312_CHARSET:
2875 case CHINESEBIG5_CHARSET:
2876 return TRUE;
2878 /* HANGUL_CHARSET is strange, though treated as CJK by Win 8, it is
2879 * not by other versions including Win 10. */
2880 return FALSE;
2883 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2884 WORD left, WORD right, BOOL repaint)
2886 TEXTMETRICW tm;
2887 INT default_left_margin = 0; /* in pixels */
2888 INT default_right_margin = 0; /* in pixels */
2890 /* Set the default margins depending on the font */
2891 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2892 HDC dc = GetDC(es->hwndSelf);
2893 HFONT old_font = SelectObject(dc, es->font);
2894 LONG width = GdiGetCharDimensions(dc, &tm, NULL);
2895 RECT rc;
2897 /* The default margins are only non zero for TrueType or Vector fonts */
2898 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2899 if (!is_cjk(tm.tmCharSet)) {
2900 default_left_margin = width / 2;
2901 default_right_margin = width / 2;
2903 GetClientRect(es->hwndSelf, &rc);
2904 if (rc.right - rc.left < (width / 2 + width) * 2 &&
2905 (width >= 28 || !IsRectEmpty(&rc)) ) {
2906 default_left_margin = es->left_margin;
2907 default_right_margin = es->right_margin;
2909 } else {
2910 /* FIXME: figure out the CJK values. They are not affected by the client rect. */
2911 default_left_margin = width / 2;
2912 default_right_margin = width / 2;
2915 SelectObject(dc, old_font);
2916 ReleaseDC(es->hwndSelf, dc);
2919 if (action & EC_LEFTMARGIN) {
2920 es->format_rect.left -= es->left_margin;
2921 if (left != EC_USEFONTINFO)
2922 es->left_margin = left;
2923 else
2924 es->left_margin = default_left_margin;
2925 es->format_rect.left += es->left_margin;
2928 if (action & EC_RIGHTMARGIN) {
2929 es->format_rect.right += es->right_margin;
2930 if (right != EC_USEFONTINFO)
2931 es->right_margin = right;
2932 else
2933 es->right_margin = default_right_margin;
2934 es->format_rect.right -= es->right_margin;
2937 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2938 EDIT_AdjustFormatRect(es);
2939 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2942 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2946 /*********************************************************************
2948 * EM_SETPASSWORDCHAR
2951 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2953 LONG style;
2955 if (es->style & ES_MULTILINE)
2956 return;
2958 if (es->password_char == c)
2959 return;
2961 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2962 es->password_char = c;
2963 if (c) {
2964 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2965 es->style |= ES_PASSWORD;
2966 } else {
2967 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2968 es->style &= ~ES_PASSWORD;
2970 EDIT_InvalidateUniscribeData(es);
2971 EDIT_UpdateText(es, NULL, TRUE);
2975 /*********************************************************************
2977 * EM_SETTABSTOPS
2980 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2982 if (!(es->style & ES_MULTILINE))
2983 return FALSE;
2984 HeapFree(GetProcessHeap(), 0, es->tabs);
2985 es->tabs_count = count;
2986 if (!count)
2987 es->tabs = NULL;
2988 else {
2989 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2990 memcpy(es->tabs, tabs, count * sizeof(INT));
2992 EDIT_InvalidateUniscribeData(es);
2993 return TRUE;
2997 /*********************************************************************
2999 * EM_SETWORDBREAKPROC
3002 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3004 if (es->word_break_proc == wbp)
3005 return;
3007 es->word_break_proc = wbp;
3009 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3010 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3011 EDIT_UpdateText(es, NULL, TRUE);
3016 /*********************************************************************
3018 * EM_UNDO / WM_UNDO
3021 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3023 INT ulength;
3024 LPWSTR utext;
3026 /* As per MSDN spec, for a single-line edit control,
3027 the return value is always TRUE */
3028 if( es->style & ES_READONLY )
3029 return !(es->style & ES_MULTILINE);
3031 ulength = strlenW(es->undo_text);
3033 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3035 strcpyW(utext, es->undo_text);
3037 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3038 es->undo_insert_count, debugstr_w(utext));
3040 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3041 EDIT_EM_EmptyUndoBuffer(es);
3042 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
3043 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3044 /* send the notification after the selection start and end are set */
3045 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3046 EDIT_EM_ScrollCaret(es);
3047 HeapFree(GetProcessHeap(), 0, utext);
3049 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3050 es->undo_insert_count, debugstr_w(es->undo_text));
3051 return TRUE;
3055 /* Helper function for WM_CHAR
3057 * According to an MSDN blog article titled "Just because you're a control
3058 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3059 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3060 * a dialog box or not.
3062 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3064 return (es->flags & EF_DIALOGMODE);
3068 /*********************************************************************
3070 * WM_PASTE
3073 static void EDIT_WM_Paste(EDITSTATE *es)
3075 HGLOBAL hsrc;
3076 LPWSTR src, ptr;
3077 int len;
3079 /* Protect read-only edit control from modification */
3080 if(es->style & ES_READONLY)
3081 return;
3083 OpenClipboard(es->hwndSelf);
3084 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3085 src = GlobalLock(hsrc);
3086 len = strlenW(src);
3087 /* Protect single-line edit against pasting new line character */
3088 if (!(es->style & ES_MULTILINE) && ((ptr = strchrW(src, '\n')))) {
3089 len = ptr - src;
3090 if (len && src[len - 1] == '\r')
3091 --len;
3093 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3094 GlobalUnlock(hsrc);
3096 else if (es->style & ES_PASSWORD) {
3097 /* clear selected text in password edit box even with empty clipboard */
3098 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3100 CloseClipboard();
3104 /*********************************************************************
3106 * WM_COPY
3109 static void EDIT_WM_Copy(EDITSTATE *es)
3111 INT s = min(es->selection_start, es->selection_end);
3112 INT e = max(es->selection_start, es->selection_end);
3113 HGLOBAL hdst;
3114 LPWSTR dst;
3115 DWORD len;
3117 if (e == s) return;
3119 len = e - s;
3120 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3121 dst = GlobalLock(hdst);
3122 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3123 dst[len] = 0; /* ensure 0 termination */
3124 TRACE("%s\n", debugstr_w(dst));
3125 GlobalUnlock(hdst);
3126 OpenClipboard(es->hwndSelf);
3127 EmptyClipboard();
3128 SetClipboardData(CF_UNICODETEXT, hdst);
3129 CloseClipboard();
3133 /*********************************************************************
3135 * WM_CLEAR
3138 static inline void EDIT_WM_Clear(EDITSTATE *es)
3140 /* Protect read-only edit control from modification */
3141 if(es->style & ES_READONLY)
3142 return;
3144 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3148 /*********************************************************************
3150 * WM_CUT
3153 static inline void EDIT_WM_Cut(EDITSTATE *es)
3155 EDIT_WM_Copy(es);
3156 EDIT_WM_Clear(es);
3160 /*********************************************************************
3162 * WM_CHAR
3165 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3167 BOOL control;
3169 control = GetKeyState(VK_CONTROL) & 0x8000;
3171 switch (c) {
3172 case '\r':
3173 /* If it's not a multiline edit box, it would be ignored below.
3174 * For multiline edit without ES_WANTRETURN, we have to make a
3175 * special case.
3177 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3178 if (EDIT_IsInsideDialog(es))
3179 break;
3180 case '\n':
3181 if (es->style & ES_MULTILINE) {
3182 if (es->style & ES_READONLY) {
3183 EDIT_MoveHome(es, FALSE, FALSE);
3184 EDIT_MoveDown_ML(es, FALSE);
3185 } else {
3186 static const WCHAR cr_lfW[] = {'\r','\n'};
3187 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, 2, TRUE, TRUE);
3190 break;
3191 case '\t':
3192 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3194 static const WCHAR tabW[] = {'\t'};
3195 if (EDIT_IsInsideDialog(es))
3196 break;
3197 EDIT_EM_ReplaceSel(es, TRUE, tabW, 1, TRUE, TRUE);
3199 break;
3200 case VK_BACK:
3201 if (!(es->style & ES_READONLY) && !control) {
3202 if (es->selection_start != es->selection_end)
3203 EDIT_WM_Clear(es);
3204 else {
3205 /* delete character left of caret */
3206 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3207 EDIT_MoveBackward(es, TRUE);
3208 EDIT_WM_Clear(es);
3211 break;
3212 case 0x03: /* ^C */
3213 if (!(es->style & ES_PASSWORD))
3214 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3215 break;
3216 case 0x16: /* ^V */
3217 if (!(es->style & ES_READONLY))
3218 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3219 break;
3220 case 0x18: /* ^X */
3221 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3222 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3223 break;
3224 case 0x1A: /* ^Z */
3225 if (!(es->style & ES_READONLY))
3226 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3227 break;
3229 default:
3230 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3231 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3232 break;
3234 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3235 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3236 break;
3238 return 1;
3242 /*********************************************************************
3244 * EDIT_ContextMenuCommand
3247 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3249 switch (id) {
3250 case EM_UNDO:
3251 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3252 break;
3253 case WM_CUT:
3254 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3255 break;
3256 case WM_COPY:
3257 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3258 break;
3259 case WM_PASTE:
3260 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3261 break;
3262 case WM_CLEAR:
3263 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3264 break;
3265 case EM_SETSEL:
3266 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3267 break;
3268 default:
3269 ERR("unknown menu item, please report\n");
3270 break;
3275 /*********************************************************************
3277 * WM_CONTEXTMENU
3279 * Note: the resource files resource/sysres_??.rc cannot define a
3280 * single popup menu. Hence we use a (dummy) menubar
3281 * containing the single popup menu as its first item.
3283 * FIXME: the message identifiers have been chosen arbitrarily,
3284 * hence we use MF_BYPOSITION.
3285 * We might as well use the "real" values (anybody knows ?)
3286 * The menu definition is in resources/sysres_??.rc.
3287 * Once these are OK, we better use MF_BYCOMMAND here
3288 * (as we do in EDIT_WM_Command()).
3291 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3293 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3294 HMENU popup = GetSubMenu(menu, 0);
3295 UINT start = es->selection_start;
3296 UINT end = es->selection_end;
3297 UINT cmd;
3299 ORDER_UINT(start, end);
3301 /* undo */
3302 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3303 /* cut */
3304 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3305 /* copy */
3306 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3307 /* paste */
3308 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3309 /* delete */
3310 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3311 /* select all */
3312 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3314 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3316 RECT rc;
3317 /* Windows places the menu at the edit's center in this case */
3318 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3319 x = rc.left + (rc.right - rc.left) / 2;
3320 y = rc.top + (rc.bottom - rc.top) / 2;
3323 if (!(es->flags & EF_FOCUSED))
3324 SetFocus(es->hwndSelf);
3326 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3327 x, y, 0, es->hwndSelf, NULL);
3329 if (cmd)
3330 EDIT_ContextMenuCommand(es, cmd);
3332 DestroyMenu(menu);
3336 /*********************************************************************
3338 * WM_GETTEXT
3341 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3343 if(!count) return 0;
3345 if(unicode)
3347 lstrcpynW(dst, es->text, count);
3348 return strlenW(dst);
3350 else
3352 LPSTR textA = (LPSTR)dst;
3353 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3354 textA[count - 1] = 0; /* ensure 0 termination */
3355 return strlen(textA);
3359 /*********************************************************************
3361 * EDIT_CheckCombo
3364 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3366 HWND hLBox = es->hwndListBox;
3367 HWND hCombo;
3368 BOOL bDropped;
3369 int nEUI;
3371 if (!hLBox)
3372 return FALSE;
3374 hCombo = GetParent(es->hwndSelf);
3375 bDropped = TRUE;
3376 nEUI = 0;
3378 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3380 if (key == VK_UP || key == VK_DOWN)
3382 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3383 nEUI = 1;
3385 if (msg == WM_KEYDOWN || nEUI)
3386 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3389 switch (msg)
3391 case WM_KEYDOWN:
3392 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3394 /* make sure ComboLBox pops up */
3395 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3396 key = VK_F4;
3397 nEUI = 2;
3400 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3401 break;
3403 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3404 if (nEUI)
3405 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3406 else
3407 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3408 break;
3411 if(nEUI == 2)
3412 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3414 return TRUE;
3418 /*********************************************************************
3420 * WM_KEYDOWN
3422 * Handling of special keys that don't produce a WM_CHAR
3423 * (i.e. non-printable keys) & Backspace & Delete
3426 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3428 BOOL shift;
3429 BOOL control;
3431 if (GetKeyState(VK_MENU) & 0x8000)
3432 return 0;
3434 shift = GetKeyState(VK_SHIFT) & 0x8000;
3435 control = GetKeyState(VK_CONTROL) & 0x8000;
3437 switch (key) {
3438 case VK_F4:
3439 case VK_UP:
3440 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3441 break;
3443 /* fall through */
3444 case VK_LEFT:
3445 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3446 EDIT_MoveUp_ML(es, shift);
3447 else
3448 if (control)
3449 EDIT_MoveWordBackward(es, shift);
3450 else
3451 EDIT_MoveBackward(es, shift);
3452 break;
3453 case VK_DOWN:
3454 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3455 break;
3456 /* fall through */
3457 case VK_RIGHT:
3458 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3459 EDIT_MoveDown_ML(es, shift);
3460 else if (control)
3461 EDIT_MoveWordForward(es, shift);
3462 else
3463 EDIT_MoveForward(es, shift);
3464 break;
3465 case VK_HOME:
3466 EDIT_MoveHome(es, shift, control);
3467 break;
3468 case VK_END:
3469 EDIT_MoveEnd(es, shift, control);
3470 break;
3471 case VK_PRIOR:
3472 if (es->style & ES_MULTILINE)
3473 EDIT_MovePageUp_ML(es, shift);
3474 else
3475 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3476 break;
3477 case VK_NEXT:
3478 if (es->style & ES_MULTILINE)
3479 EDIT_MovePageDown_ML(es, shift);
3480 else
3481 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3482 break;
3483 case VK_DELETE:
3484 if (!(es->style & ES_READONLY) && !(shift && control)) {
3485 if (es->selection_start != es->selection_end) {
3486 if (shift)
3487 EDIT_WM_Cut(es);
3488 else
3489 EDIT_WM_Clear(es);
3490 } else {
3491 if (shift) {
3492 /* delete character left of caret */
3493 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3494 EDIT_MoveBackward(es, TRUE);
3495 EDIT_WM_Clear(es);
3496 } else if (control) {
3497 /* delete to end of line */
3498 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3499 EDIT_MoveEnd(es, TRUE, FALSE);
3500 EDIT_WM_Clear(es);
3501 } else {
3502 /* delete character right of caret */
3503 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3504 EDIT_MoveForward(es, TRUE);
3505 EDIT_WM_Clear(es);
3509 break;
3510 case VK_INSERT:
3511 if (shift) {
3512 if (!(es->style & ES_READONLY))
3513 EDIT_WM_Paste(es);
3514 } else if (control)
3515 EDIT_WM_Copy(es);
3516 break;
3517 case VK_RETURN:
3518 /* If the edit doesn't want the return send a message to the default object */
3519 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3521 DWORD dw;
3523 if (!EDIT_IsInsideDialog(es)) break;
3524 if (control) break;
3525 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3526 if (HIWORD(dw) == DC_HASDEFID)
3528 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3529 if (hwDefCtrl)
3531 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3532 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3536 break;
3537 case VK_ESCAPE:
3538 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3539 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3540 break;
3541 case VK_TAB:
3542 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3543 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3544 break;
3546 return TRUE;
3550 /*********************************************************************
3552 * WM_KILLFOCUS
3555 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3557 es->flags &= ~EF_FOCUSED;
3558 DestroyCaret();
3559 if(!(es->style & ES_NOHIDESEL))
3560 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3561 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3562 /* throw away left over scroll when we lose focus */
3563 es->wheelDeltaRemainder = 0;
3564 return 0;
3568 /*********************************************************************
3570 * WM_LBUTTONDBLCLK
3572 * The caret position has been set on the WM_LBUTTONDOWN message
3575 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3577 INT s;
3578 INT e = es->selection_end;
3579 INT l;
3580 INT li;
3581 INT ll;
3583 es->bCaptureState = TRUE;
3584 SetCapture(es->hwndSelf);
3586 l = EDIT_EM_LineFromChar(es, e);
3587 li = EDIT_EM_LineIndex(es, l);
3588 ll = EDIT_EM_LineLength(es, e);
3589 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3590 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3591 EDIT_EM_SetSel(es, s, e, FALSE);
3592 EDIT_EM_ScrollCaret(es);
3593 es->region_posx = es->region_posy = 0;
3594 SetTimer(es->hwndSelf, 0, 100, NULL);
3595 return 0;
3599 /*********************************************************************
3601 * WM_LBUTTONDOWN
3604 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3606 INT e;
3607 BOOL after_wrap;
3609 es->bCaptureState = TRUE;
3610 SetCapture(es->hwndSelf);
3611 EDIT_ConfinePoint(es, &x, &y);
3612 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3613 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3614 EDIT_EM_ScrollCaret(es);
3615 es->region_posx = es->region_posy = 0;
3616 SetTimer(es->hwndSelf, 0, 100, NULL);
3618 if (!(es->flags & EF_FOCUSED))
3619 SetFocus(es->hwndSelf);
3621 return 0;
3625 /*********************************************************************
3627 * WM_LBUTTONUP
3630 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3632 if (es->bCaptureState) {
3633 KillTimer(es->hwndSelf, 0);
3634 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3636 es->bCaptureState = FALSE;
3637 return 0;
3641 /*********************************************************************
3643 * WM_MBUTTONDOWN
3646 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3648 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3649 return 0;
3653 /*********************************************************************
3655 * WM_MOUSEMOVE
3658 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3660 INT e;
3661 BOOL after_wrap;
3662 INT prex, prey;
3664 /* If the mouse has been captured by process other than the edit control itself,
3665 * the windows edit controls will not select the strings with mouse move.
3667 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3668 return 0;
3671 * FIXME: gotta do some scrolling if outside client
3672 * area. Maybe reset the timer ?
3674 prex = x; prey = y;
3675 EDIT_ConfinePoint(es, &x, &y);
3676 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3677 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3678 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3679 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3680 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3681 return 0;
3685 /*********************************************************************
3687 * WM_PAINT
3690 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3692 PAINTSTRUCT ps;
3693 INT i;
3694 HDC dc;
3695 HFONT old_font = 0;
3696 RECT rc;
3697 RECT rcClient;
3698 RECT rcLine;
3699 RECT rcRgn;
3700 HBRUSH brush;
3701 HBRUSH old_brush;
3702 INT bw, bh;
3703 BOOL rev = es->bEnableState &&
3704 ((es->flags & EF_FOCUSED) ||
3705 (es->style & ES_NOHIDESEL));
3706 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3708 /* The dc we use for calculating may not be the one we paint into.
3709 This is the safest action. */
3710 EDIT_InvalidateUniscribeData(es);
3711 GetClientRect(es->hwndSelf, &rcClient);
3713 /* get the background brush */
3714 brush = EDIT_NotifyCtlColor(es, dc);
3716 /* paint the border and the background */
3717 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3719 if(es->style & WS_BORDER) {
3720 bw = GetSystemMetrics(SM_CXBORDER);
3721 bh = GetSystemMetrics(SM_CYBORDER);
3722 rc = rcClient;
3723 if(es->style & ES_MULTILINE) {
3724 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3725 if(es->style & WS_VSCROLL) rc.right+=bw;
3728 /* Draw the frame. Same code as in nonclient.c */
3729 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3730 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3731 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3732 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3733 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3734 SelectObject(dc, old_brush);
3736 /* Keep the border clean */
3737 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3738 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3741 GetClipBox(dc, &rc);
3742 FillRect(dc, &rc, brush);
3744 IntersectClipRect(dc, es->format_rect.left,
3745 es->format_rect.top,
3746 es->format_rect.right,
3747 es->format_rect.bottom);
3748 if (es->style & ES_MULTILINE) {
3749 rc = rcClient;
3750 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3752 if (es->font)
3753 old_font = SelectObject(dc, es->font);
3755 if (!es->bEnableState)
3756 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3757 GetClipBox(dc, &rcRgn);
3758 if (es->style & ES_MULTILINE) {
3759 INT vlc = get_vertical_line_count(es);
3760 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3761 EDIT_UpdateUniscribeData(es, dc, i);
3762 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3763 if (IntersectRect(&rc, &rcRgn, &rcLine))
3764 EDIT_PaintLine(es, dc, i, rev);
3766 } else {
3767 EDIT_UpdateUniscribeData(es, dc, 0);
3768 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3769 if (IntersectRect(&rc, &rcRgn, &rcLine))
3770 EDIT_PaintLine(es, dc, 0, rev);
3772 if (es->font)
3773 SelectObject(dc, old_font);
3775 if (!hdc)
3776 EndPaint(es->hwndSelf, &ps);
3780 /*********************************************************************
3782 * WM_SETFOCUS
3785 static void EDIT_WM_SetFocus(EDITSTATE *es)
3787 es->flags |= EF_FOCUSED;
3789 if (!(es->style & ES_NOHIDESEL))
3790 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3792 /* single line edit updates itself */
3793 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3795 HDC hdc = GetDC(es->hwndSelf);
3796 EDIT_WM_Paint(es, hdc);
3797 ReleaseDC(es->hwndSelf, hdc);
3800 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3801 EDIT_SetCaretPos(es, es->selection_end,
3802 es->flags & EF_AFTER_WRAP);
3803 ShowCaret(es->hwndSelf);
3804 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3808 /*********************************************************************
3810 * WM_SETFONT
3812 * With Win95 look the margins are set to default font value unless
3813 * the system font (font == 0) is being set, in which case they are left
3814 * unchanged.
3817 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3819 TEXTMETRICW tm;
3820 HDC dc;
3821 HFONT old_font = 0;
3822 RECT clientRect;
3824 es->font = font;
3825 EDIT_InvalidateUniscribeData(es);
3826 dc = GetDC(es->hwndSelf);
3827 if (font)
3828 old_font = SelectObject(dc, font);
3829 GetTextMetricsW(dc, &tm);
3830 es->line_height = tm.tmHeight;
3831 es->char_width = tm.tmAveCharWidth;
3832 if (font)
3833 SelectObject(dc, old_font);
3834 ReleaseDC(es->hwndSelf, dc);
3836 /* Reset the format rect and the margins */
3837 GetClientRect(es->hwndSelf, &clientRect);
3838 EDIT_SetRectNP(es, &clientRect);
3839 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3840 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3842 if (es->style & ES_MULTILINE)
3843 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3844 else
3845 EDIT_CalcLineWidth_SL(es);
3847 if (redraw)
3848 EDIT_UpdateText(es, NULL, TRUE);
3849 if (es->flags & EF_FOCUSED) {
3850 DestroyCaret();
3851 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3852 EDIT_SetCaretPos(es, es->selection_end,
3853 es->flags & EF_AFTER_WRAP);
3854 ShowCaret(es->hwndSelf);
3859 /*********************************************************************
3861 * WM_SETTEXT
3863 * NOTES
3864 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3865 * The modified flag is reset. No notifications are sent.
3867 * For single-line controls, reception of WM_SETTEXT triggers:
3868 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3871 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3873 LPWSTR textW = NULL;
3874 if (!unicode && text)
3876 LPCSTR textA = (LPCSTR)text;
3877 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3878 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3879 if (textW)
3880 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3881 text = textW;
3884 if (es->flags & EF_UPDATE)
3885 /* fixed this bug once; complain if we see it about to happen again. */
3886 ERR("SetSel may generate UPDATE message whose handler may reset "
3887 "selection.\n");
3889 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3890 if (text)
3892 TRACE("%s\n", debugstr_w(text));
3893 EDIT_EM_ReplaceSel(es, FALSE, text, strlenW(text), FALSE, FALSE);
3894 if(!unicode)
3895 HeapFree(GetProcessHeap(), 0, textW);
3897 else
3899 TRACE("<NULL>\n");
3900 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3902 es->x_offset = 0;
3903 es->flags &= ~EF_MODIFIED;
3904 EDIT_EM_SetSel(es, 0, 0, FALSE);
3906 /* Send the notification after the selection start and end have been set
3907 * edit control doesn't send notification on WM_SETTEXT
3908 * if it is multiline, or it is part of combobox
3910 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3912 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3913 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3915 EDIT_EM_ScrollCaret(es);
3916 EDIT_UpdateScrollInfo(es);
3917 EDIT_InvalidateUniscribeData(es);
3921 /*********************************************************************
3923 * WM_SIZE
3926 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3928 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3929 RECT rc;
3930 GetClientRect(es->hwndSelf, &rc);
3931 EDIT_SetRectNP(es, &rc);
3932 EDIT_UpdateText(es, NULL, TRUE);
3937 /*********************************************************************
3939 * WM_STYLECHANGED
3941 * This message is sent by SetWindowLong on having changed either the Style
3942 * or the extended style.
3944 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3946 * See also EDIT_WM_NCCreate
3948 * It appears that the Windows version of the edit control allows the style
3949 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3950 * style variable which will generally be different. In this function we
3951 * update the internal style based on what changed in the externally visible
3952 * style.
3954 * Much of this content as based upon the MSDN, especially:
3955 * Platform SDK Documentation -> User Interface Services ->
3956 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3957 * Edit Control Styles
3959 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3961 if (GWL_STYLE == which) {
3962 DWORD style_change_mask;
3963 DWORD new_style;
3964 /* Only a subset of changes can be applied after the control
3965 * has been created.
3967 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3968 ES_NUMBER;
3969 if (es->style & ES_MULTILINE)
3970 style_change_mask |= ES_WANTRETURN;
3972 new_style = style->styleNew & style_change_mask;
3974 /* Number overrides lowercase overrides uppercase (at least it
3975 * does in Win95). However I'll bet that ES_NUMBER would be
3976 * invalid under Win 3.1.
3978 if (new_style & ES_NUMBER) {
3979 ; /* do not override the ES_NUMBER */
3980 } else if (new_style & ES_LOWERCASE) {
3981 new_style &= ~ES_UPPERCASE;
3984 es->style = (es->style & ~style_change_mask) | new_style;
3985 } else if (GWL_EXSTYLE == which) {
3986 ; /* FIXME - what is needed here */
3987 } else {
3988 WARN ("Invalid style change %ld\n",which);
3991 return 0;
3994 /*********************************************************************
3996 * WM_SYSKEYDOWN
3999 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4001 if ((key == VK_BACK) && (key_data & 0x2000)) {
4002 if (EDIT_EM_CanUndo(es))
4003 EDIT_EM_Undo(es);
4004 return 0;
4005 } else if (key == VK_UP || key == VK_DOWN) {
4006 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4007 return 0;
4009 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4013 /*********************************************************************
4015 * WM_TIMER
4018 static void EDIT_WM_Timer(EDITSTATE *es)
4020 if (es->region_posx < 0) {
4021 EDIT_MoveBackward(es, TRUE);
4022 } else if (es->region_posx > 0) {
4023 EDIT_MoveForward(es, TRUE);
4026 * FIXME: gotta do some vertical scrolling here, like
4027 * EDIT_EM_LineScroll(hwnd, 0, 1);
4031 /*********************************************************************
4033 * WM_HSCROLL
4036 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4038 INT dx;
4039 INT fw;
4041 if (!(es->style & ES_MULTILINE))
4042 return 0;
4044 if (!(es->style & ES_AUTOHSCROLL))
4045 return 0;
4047 dx = 0;
4048 fw = es->format_rect.right - es->format_rect.left;
4049 switch (action) {
4050 case SB_LINELEFT:
4051 TRACE("SB_LINELEFT\n");
4052 if (es->x_offset)
4053 dx = -es->char_width;
4054 break;
4055 case SB_LINERIGHT:
4056 TRACE("SB_LINERIGHT\n");
4057 if (es->x_offset < es->text_width)
4058 dx = es->char_width;
4059 break;
4060 case SB_PAGELEFT:
4061 TRACE("SB_PAGELEFT\n");
4062 if (es->x_offset)
4063 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4064 break;
4065 case SB_PAGERIGHT:
4066 TRACE("SB_PAGERIGHT\n");
4067 if (es->x_offset < es->text_width)
4068 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4069 break;
4070 case SB_LEFT:
4071 TRACE("SB_LEFT\n");
4072 if (es->x_offset)
4073 dx = -es->x_offset;
4074 break;
4075 case SB_RIGHT:
4076 TRACE("SB_RIGHT\n");
4077 if (es->x_offset < es->text_width)
4078 dx = es->text_width - es->x_offset;
4079 break;
4080 case SB_THUMBTRACK:
4081 TRACE("SB_THUMBTRACK %d\n", pos);
4082 es->flags |= EF_HSCROLL_TRACK;
4083 if(es->style & WS_HSCROLL)
4084 dx = pos - es->x_offset;
4085 else
4087 INT fw, new_x;
4088 /* Sanity check */
4089 if(pos < 0 || pos > 100) return 0;
4090 /* Assume default scroll range 0-100 */
4091 fw = es->format_rect.right - es->format_rect.left;
4092 new_x = pos * (es->text_width - fw) / 100;
4093 dx = es->text_width ? (new_x - es->x_offset) : 0;
4095 break;
4096 case SB_THUMBPOSITION:
4097 TRACE("SB_THUMBPOSITION %d\n", pos);
4098 es->flags &= ~EF_HSCROLL_TRACK;
4099 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4100 dx = pos - es->x_offset;
4101 else
4103 INT fw, new_x;
4104 /* Sanity check */
4105 if(pos < 0 || pos > 100) return 0;
4106 /* Assume default scroll range 0-100 */
4107 fw = es->format_rect.right - es->format_rect.left;
4108 new_x = pos * (es->text_width - fw) / 100;
4109 dx = es->text_width ? (new_x - es->x_offset) : 0;
4111 if (!dx) {
4112 /* force scroll info update */
4113 EDIT_UpdateScrollInfo(es);
4114 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4116 break;
4117 case SB_ENDSCROLL:
4118 TRACE("SB_ENDSCROLL\n");
4119 break;
4121 * FIXME : the next two are undocumented !
4122 * Are we doing the right thing ?
4123 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4124 * although it's also a regular control message.
4126 case EM_GETTHUMB: /* this one is used by NT notepad */
4128 LRESULT ret;
4129 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4130 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4131 else
4133 /* Assume default scroll range 0-100 */
4134 INT fw = es->format_rect.right - es->format_rect.left;
4135 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4137 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4138 return ret;
4140 case EM_LINESCROLL:
4141 TRACE("EM_LINESCROLL16\n");
4142 dx = pos;
4143 break;
4145 default:
4146 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4147 action, action);
4148 return 0;
4150 if (dx)
4152 INT fw = es->format_rect.right - es->format_rect.left;
4153 /* check if we are going to move too far */
4154 if(es->x_offset + dx + fw > es->text_width)
4155 dx = es->text_width - fw - es->x_offset;
4156 if(dx)
4157 EDIT_EM_LineScroll_internal(es, dx, 0);
4159 return 0;
4163 /*********************************************************************
4165 * WM_VSCROLL
4168 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4170 INT dy;
4172 if (!(es->style & ES_MULTILINE))
4173 return 0;
4175 if (!(es->style & ES_AUTOVSCROLL))
4176 return 0;
4178 dy = 0;
4179 switch (action) {
4180 case SB_LINEUP:
4181 case SB_LINEDOWN:
4182 case SB_PAGEUP:
4183 case SB_PAGEDOWN:
4184 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4185 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4186 (action == SB_PAGEUP ? "SB_PAGEUP" :
4187 "SB_PAGEDOWN"))));
4188 EDIT_EM_Scroll(es, action);
4189 return 0;
4190 case SB_TOP:
4191 TRACE("SB_TOP\n");
4192 dy = -es->y_offset;
4193 break;
4194 case SB_BOTTOM:
4195 TRACE("SB_BOTTOM\n");
4196 dy = es->line_count - 1 - es->y_offset;
4197 break;
4198 case SB_THUMBTRACK:
4199 TRACE("SB_THUMBTRACK %d\n", pos);
4200 es->flags |= EF_VSCROLL_TRACK;
4201 if(es->style & WS_VSCROLL)
4202 dy = pos - es->y_offset;
4203 else
4205 /* Assume default scroll range 0-100 */
4206 INT vlc, new_y;
4207 /* Sanity check */
4208 if(pos < 0 || pos > 100) return 0;
4209 vlc = get_vertical_line_count(es);
4210 new_y = pos * (es->line_count - vlc) / 100;
4211 dy = es->line_count ? (new_y - es->y_offset) : 0;
4212 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4213 es->line_count, es->y_offset, pos, dy);
4215 break;
4216 case SB_THUMBPOSITION:
4217 TRACE("SB_THUMBPOSITION %d\n", pos);
4218 es->flags &= ~EF_VSCROLL_TRACK;
4219 if(es->style & WS_VSCROLL)
4220 dy = pos - es->y_offset;
4221 else
4223 /* Assume default scroll range 0-100 */
4224 INT vlc, new_y;
4225 /* Sanity check */
4226 if(pos < 0 || pos > 100) return 0;
4227 vlc = get_vertical_line_count(es);
4228 new_y = pos * (es->line_count - vlc) / 100;
4229 dy = es->line_count ? (new_y - es->y_offset) : 0;
4230 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4231 es->line_count, es->y_offset, pos, dy);
4233 if (!dy)
4235 /* force scroll info update */
4236 EDIT_UpdateScrollInfo(es);
4237 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4239 break;
4240 case SB_ENDSCROLL:
4241 TRACE("SB_ENDSCROLL\n");
4242 break;
4244 * FIXME : the next two are undocumented !
4245 * Are we doing the right thing ?
4246 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4247 * although it's also a regular control message.
4249 case EM_GETTHUMB: /* this one is used by NT notepad */
4251 LRESULT ret;
4252 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4253 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4254 else
4256 /* Assume default scroll range 0-100 */
4257 INT vlc = get_vertical_line_count(es);
4258 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4260 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4261 return ret;
4263 case EM_LINESCROLL:
4264 TRACE("EM_LINESCROLL %d\n", pos);
4265 dy = pos;
4266 break;
4268 default:
4269 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4270 action, action);
4271 return 0;
4273 if (dy)
4274 EDIT_EM_LineScroll(es, 0, dy);
4275 return 0;
4278 /*********************************************************************
4280 * EM_GETTHUMB
4282 * FIXME: is this right ? (or should it be only VSCROLL)
4283 * (and maybe only for edit controls that really have their
4284 * own scrollbars) (and maybe only for multiline controls ?)
4285 * All in all: very poorly documented
4288 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4290 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4291 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4295 /********************************************************************
4297 * The Following code is to handle inline editing from IMEs
4300 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4302 LONG buflen;
4303 LPWSTR lpCompStr;
4304 LPSTR lpCompStrAttr = NULL;
4305 DWORD dwBufLenAttr;
4307 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4309 if (buflen < 0)
4311 return;
4314 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen);
4315 if (!lpCompStr)
4317 ERR("Unable to allocate IME CompositionString\n");
4318 return;
4321 if (buflen)
4322 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4324 if (CompFlag & GCS_COMPATTR)
4327 * We do not use the attributes yet. it would tell us what characters
4328 * are in transition and which are converted or decided upon
4330 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4331 if (dwBufLenAttr)
4333 dwBufLenAttr ++;
4334 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4335 if (!lpCompStrAttr)
4337 ERR("Unable to allocate IME Attribute String\n");
4338 HeapFree(GetProcessHeap(),0,lpCompStr);
4339 return;
4341 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4342 dwBufLenAttr);
4343 lpCompStrAttr[dwBufLenAttr] = 0;
4347 /* check for change in composition start */
4348 if (es->selection_end < es->composition_start)
4349 es->composition_start = es->selection_end;
4351 /* replace existing selection string */
4352 es->selection_start = es->composition_start;
4354 if (es->composition_len > 0)
4355 es->selection_end = es->composition_start + es->composition_len;
4356 else
4357 es->selection_end = es->selection_start;
4359 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4360 es->composition_len = abs(es->composition_start - es->selection_end);
4362 es->selection_start = es->composition_start;
4363 es->selection_end = es->selection_start + es->composition_len;
4365 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4366 HeapFree(GetProcessHeap(),0,lpCompStr);
4369 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4371 LONG buflen;
4372 LPWSTR lpResultStr;
4374 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4375 if (buflen <= 0)
4377 return;
4380 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4381 if (!lpResultStr)
4383 ERR("Unable to alloc buffer for IME string\n");
4384 return;
4387 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4389 /* check for change in composition start */
4390 if (es->selection_end < es->composition_start)
4391 es->composition_start = es->selection_end;
4393 es->selection_start = es->composition_start;
4394 es->selection_end = es->composition_start + es->composition_len;
4395 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4396 es->composition_start = es->selection_end;
4397 es->composition_len = 0;
4399 HeapFree(GetProcessHeap(),0,lpResultStr);
4402 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4404 HIMC hIMC;
4405 int cursor;
4407 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4409 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4410 es->composition_start = es->selection_end;
4413 hIMC = ImmGetContext(hwnd);
4414 if (!hIMC)
4415 return;
4417 if (CompFlag & GCS_RESULTSTR)
4419 EDIT_GetResultStr(hIMC, es);
4420 cursor = 0;
4422 else
4424 if (CompFlag & GCS_COMPSTR)
4425 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4426 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4428 ImmReleaseContext(hwnd, hIMC);
4429 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4433 /*********************************************************************
4435 * WM_NCCREATE
4437 * See also EDIT_WM_StyleChanged
4439 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4441 EDITSTATE *es;
4442 UINT alloc_size;
4444 TRACE("Creating %s edit control, style = %08x\n",
4445 unicode ? "Unicode" : "ANSI", lpcs->style);
4447 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4448 return FALSE;
4449 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4452 * Note: since the EDITSTATE has not been fully initialized yet,
4453 * we can't use any API calls that may send
4454 * WM_XXX messages before WM_NCCREATE is completed.
4457 es->is_unicode = unicode;
4458 es->style = lpcs->style;
4460 es->bEnableState = !(es->style & WS_DISABLED);
4462 es->hwndSelf = hwnd;
4463 /* Save parent, which will be notified by EN_* messages */
4464 es->hwndParent = lpcs->hwndParent;
4466 if (es->style & ES_COMBO)
4467 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4469 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4470 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4472 /* Number overrides lowercase overrides uppercase (at least it
4473 * does in Win95). However I'll bet that ES_NUMBER would be
4474 * invalid under Win 3.1.
4476 if (es->style & ES_NUMBER) {
4477 ; /* do not override the ES_NUMBER */
4478 } else if (es->style & ES_LOWERCASE) {
4479 es->style &= ~ES_UPPERCASE;
4481 if (es->style & ES_MULTILINE) {
4482 es->buffer_limit = BUFLIMIT_INITIAL;
4483 if (es->style & WS_VSCROLL)
4484 es->style |= ES_AUTOVSCROLL;
4485 if (es->style & WS_HSCROLL)
4486 es->style |= ES_AUTOHSCROLL;
4487 es->style &= ~ES_PASSWORD;
4488 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4489 /* Confirmed - RIGHT overrides CENTER */
4490 if (es->style & ES_RIGHT)
4491 es->style &= ~ES_CENTER;
4492 es->style &= ~WS_HSCROLL;
4493 es->style &= ~ES_AUTOHSCROLL;
4495 } else {
4496 es->buffer_limit = BUFLIMIT_INITIAL;
4497 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4498 es->style &= ~ES_CENTER;
4499 es->style &= ~WS_HSCROLL;
4500 es->style &= ~WS_VSCROLL;
4501 if (es->style & ES_PASSWORD)
4502 es->password_char = '*';
4505 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4506 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4507 goto cleanup;
4508 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4510 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4511 goto cleanup;
4512 es->undo_buffer_size = es->buffer_size;
4514 if (es->style & ES_MULTILINE)
4515 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4516 goto cleanup;
4517 es->line_count = 1;
4520 * In Win95 look and feel, the WS_BORDER style is replaced by the
4521 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4522 * control a nonclient area so we don't need to draw the border.
4523 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4524 * a nonclient area and we should handle painting the border ourselves.
4526 * When making modifications please ensure that the code still works
4527 * for edit controls created directly with style 0x50800000, exStyle 0
4528 * (which should have a single pixel border)
4530 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4531 es->style &= ~WS_BORDER;
4532 else if (es->style & WS_BORDER)
4533 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4535 return TRUE;
4537 cleanup:
4538 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4539 EDIT_InvalidateUniscribeData(es);
4540 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4541 HeapFree(GetProcessHeap(), 0, es->undo_text);
4542 if (es->hloc32W) LocalFree(es->hloc32W);
4543 HeapFree(GetProcessHeap(), 0, es->logAttr);
4544 HeapFree(GetProcessHeap(), 0, es);
4545 return FALSE;
4549 /*********************************************************************
4551 * WM_CREATE
4554 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4556 RECT clientRect;
4558 TRACE("%s\n", debugstr_w(name));
4560 * To initialize some final structure members, we call some helper
4561 * functions. However, since the EDITSTATE is not consistent (i.e.
4562 * not fully initialized), we should be very careful which
4563 * functions can be called, and in what order.
4565 EDIT_WM_SetFont(es, 0, FALSE);
4566 EDIT_EM_EmptyUndoBuffer(es);
4568 /* We need to calculate the format rect
4569 (applications may send EM_SETMARGINS before the control gets visible) */
4570 GetClientRect(es->hwndSelf, &clientRect);
4571 EDIT_SetRectNP(es, &clientRect);
4573 if (name && *name) {
4574 EDIT_EM_ReplaceSel(es, FALSE, name, strlenW(name), FALSE, FALSE);
4575 /* if we insert text to the editline, the text scrolls out
4576 * of the window, as the caret is placed after the insert
4577 * pos normally; thus we reset es->selection... to 0 and
4578 * update caret
4580 es->selection_start = es->selection_end = 0;
4581 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4582 * Messages are only to be sent when the USER does something to
4583 * change the contents. So I am removing this EN_CHANGE
4585 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4587 EDIT_EM_ScrollCaret(es);
4589 /* force scroll info update */
4590 EDIT_UpdateScrollInfo(es);
4591 /* The rule seems to return 1 here for success */
4592 /* Power Builder masked edit controls will crash */
4593 /* if not. */
4594 /* FIXME: is that in all cases so ? */
4595 return 1;
4599 /*********************************************************************
4601 * WM_NCDESTROY
4604 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4606 LINEDEF *pc, *pp;
4608 /* The app can own the text buffer handle */
4609 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4610 LocalFree(es->hloc32W);
4612 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4613 LocalFree(es->hloc32A);
4615 EDIT_InvalidateUniscribeData(es);
4616 pc = es->first_line_def;
4617 while (pc)
4619 pp = pc->next;
4620 HeapFree(GetProcessHeap(), 0, pc);
4621 pc = pp;
4624 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4625 HeapFree(GetProcessHeap(), 0, es->undo_text);
4626 HeapFree(GetProcessHeap(), 0, es);
4628 return 0;
4632 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4634 if(unicode)
4635 return DefWindowProcW(hwnd, msg, wParam, lParam);
4636 else
4637 return DefWindowProcA(hwnd, msg, wParam, lParam);
4640 /*********************************************************************
4642 * EditWndProc_common
4644 * The messages are in the order of the actual integer values
4645 * (which can be found in include/windows.h)
4647 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4649 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4650 LRESULT result = 0;
4652 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4654 if (!es && msg != WM_NCCREATE)
4655 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4657 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4659 switch (msg) {
4660 case EM_GETSEL:
4661 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4662 break;
4664 case EM_SETSEL:
4665 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4666 EDIT_EM_ScrollCaret(es);
4667 result = 1;
4668 break;
4670 case EM_GETRECT:
4671 if (lParam)
4672 *((LPRECT)lParam) = es->format_rect;
4673 break;
4675 case EM_SETRECT:
4676 if ((es->style & ES_MULTILINE) && lParam) {
4677 EDIT_SetRectNP(es, (LPRECT)lParam);
4678 EDIT_UpdateText(es, NULL, TRUE);
4680 break;
4682 case EM_SETRECTNP:
4683 if ((es->style & ES_MULTILINE) && lParam)
4684 EDIT_SetRectNP(es, (LPRECT)lParam);
4685 break;
4687 case EM_SCROLL:
4688 result = EDIT_EM_Scroll(es, (INT)wParam);
4689 break;
4691 case EM_LINESCROLL:
4692 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4693 break;
4695 case EM_SCROLLCARET:
4696 EDIT_EM_ScrollCaret(es);
4697 result = 1;
4698 break;
4700 case EM_GETMODIFY:
4701 result = ((es->flags & EF_MODIFIED) != 0);
4702 break;
4704 case EM_SETMODIFY:
4705 if (wParam)
4706 es->flags |= EF_MODIFIED;
4707 else
4708 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4709 break;
4711 case EM_GETLINECOUNT:
4712 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4713 break;
4715 case EM_LINEINDEX:
4716 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4717 break;
4719 case EM_SETHANDLE:
4720 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4721 break;
4723 case EM_GETHANDLE:
4724 result = (LRESULT)EDIT_EM_GetHandle(es);
4725 break;
4727 case EM_GETTHUMB:
4728 result = EDIT_EM_GetThumb(es);
4729 break;
4731 /* these messages missing from specs */
4732 case 0x00bf:
4733 case 0x00c0:
4734 case 0x00c3:
4735 case 0x00ca:
4736 FIXME("undocumented message 0x%x, please report\n", msg);
4737 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4738 break;
4740 case EM_LINELENGTH:
4741 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4742 break;
4744 case EM_REPLACESEL:
4746 LPWSTR textW;
4748 if(unicode)
4749 textW = (LPWSTR)lParam;
4750 else
4752 LPSTR textA = (LPSTR)lParam;
4753 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4754 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4755 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4758 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, strlenW(textW), TRUE, TRUE);
4759 result = 1;
4761 if(!unicode)
4762 HeapFree(GetProcessHeap(), 0, textW);
4763 break;
4766 case EM_GETLINE:
4767 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4768 break;
4770 case EM_SETLIMITTEXT:
4771 EDIT_EM_SetLimitText(es, wParam);
4772 break;
4774 case EM_CANUNDO:
4775 result = (LRESULT)EDIT_EM_CanUndo(es);
4776 break;
4778 case EM_UNDO:
4779 case WM_UNDO:
4780 result = (LRESULT)EDIT_EM_Undo(es);
4781 break;
4783 case EM_FMTLINES:
4784 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4785 break;
4787 case EM_LINEFROMCHAR:
4788 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4789 break;
4791 case EM_SETTABSTOPS:
4792 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4793 break;
4795 case EM_SETPASSWORDCHAR:
4797 WCHAR charW = 0;
4799 if(unicode)
4800 charW = (WCHAR)wParam;
4801 else
4803 CHAR charA = wParam;
4804 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4807 EDIT_EM_SetPasswordChar(es, charW);
4808 break;
4811 case EM_EMPTYUNDOBUFFER:
4812 EDIT_EM_EmptyUndoBuffer(es);
4813 break;
4815 case EM_GETFIRSTVISIBLELINE:
4816 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4817 break;
4819 case EM_SETREADONLY:
4821 DWORD old_style = es->style;
4823 if (wParam) {
4824 SetWindowLongW( hwnd, GWL_STYLE,
4825 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4826 es->style |= ES_READONLY;
4827 } else {
4828 SetWindowLongW( hwnd, GWL_STYLE,
4829 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4830 es->style &= ~ES_READONLY;
4833 if (old_style ^ es->style)
4834 InvalidateRect(es->hwndSelf, NULL, TRUE);
4836 result = 1;
4837 break;
4840 case EM_SETWORDBREAKPROC:
4841 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4842 result = 1;
4843 break;
4845 case EM_GETWORDBREAKPROC:
4846 result = (LRESULT)es->word_break_proc;
4847 break;
4849 case EM_GETPASSWORDCHAR:
4851 if(unicode)
4852 result = es->password_char;
4853 else
4855 WCHAR charW = es->password_char;
4856 CHAR charA = 0;
4857 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4858 result = charA;
4860 break;
4863 case EM_SETMARGINS:
4864 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4865 break;
4867 case EM_GETMARGINS:
4868 result = MAKELONG(es->left_margin, es->right_margin);
4869 break;
4871 case EM_GETLIMITTEXT:
4872 result = es->buffer_limit;
4873 break;
4875 case EM_POSFROMCHAR:
4876 if ((INT)wParam >= get_text_length(es)) result = -1;
4877 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4878 break;
4880 case EM_CHARFROMPOS:
4881 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4882 break;
4884 /* End of the EM_ messages which were in numerical order; what order
4885 * are these in? vaguely alphabetical?
4888 case WM_NCCREATE:
4889 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4890 break;
4892 case WM_NCDESTROY:
4893 result = EDIT_WM_NCDestroy(es);
4894 es = NULL;
4895 break;
4897 case WM_GETDLGCODE:
4898 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4900 if (es->style & ES_MULTILINE)
4901 result |= DLGC_WANTALLKEYS;
4903 if (lParam)
4905 es->flags|=EF_DIALOGMODE;
4907 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4909 int vk = (int)((LPMSG)lParam)->wParam;
4911 if (es->hwndListBox)
4913 if (vk == VK_RETURN || vk == VK_ESCAPE)
4914 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4915 result |= DLGC_WANTMESSAGE;
4919 break;
4921 case WM_IME_CHAR:
4922 if (!unicode)
4924 WCHAR charW;
4925 CHAR strng[2];
4927 strng[0] = wParam >> 8;
4928 strng[1] = wParam & 0xff;
4929 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4930 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4931 result = EDIT_WM_Char(es, charW);
4932 break;
4934 /* fall through */
4935 case WM_CHAR:
4937 WCHAR charW;
4939 if(unicode)
4940 charW = wParam;
4941 else
4943 CHAR charA = wParam;
4944 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4947 if (es->hwndListBox)
4949 if (charW == VK_RETURN || charW == VK_ESCAPE)
4951 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4952 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4953 break;
4956 result = EDIT_WM_Char(es, charW);
4957 break;
4960 case WM_UNICHAR:
4961 if (unicode)
4963 if (wParam == UNICODE_NOCHAR) return TRUE;
4964 if (wParam <= 0x000fffff)
4966 if(wParam > 0xffff) /* convert to surrogates */
4968 wParam -= 0x10000;
4969 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4970 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4972 else EDIT_WM_Char(es, wParam);
4974 return 0;
4976 break;
4978 case WM_CLEAR:
4979 EDIT_WM_Clear(es);
4980 break;
4982 case WM_CONTEXTMENU:
4983 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4984 break;
4986 case WM_COPY:
4987 EDIT_WM_Copy(es);
4988 break;
4990 case WM_CREATE:
4991 if(unicode)
4992 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4993 else
4995 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4996 LPWSTR nameW = NULL;
4997 if(nameA)
4999 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5000 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5001 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5003 result = EDIT_WM_Create(es, nameW);
5004 HeapFree(GetProcessHeap(), 0, nameW);
5006 break;
5008 case WM_CUT:
5009 EDIT_WM_Cut(es);
5010 break;
5012 case WM_ENABLE:
5013 es->bEnableState = (BOOL) wParam;
5014 EDIT_UpdateText(es, NULL, TRUE);
5015 break;
5017 case WM_ERASEBKGND:
5018 /* we do the proper erase in EDIT_WM_Paint */
5019 result = 1;
5020 break;
5022 case WM_GETFONT:
5023 result = (LRESULT)es->font;
5024 break;
5026 case WM_GETTEXT:
5027 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5028 break;
5030 case WM_GETTEXTLENGTH:
5031 if (unicode) result = get_text_length(es);
5032 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5033 NULL, 0, NULL, NULL );
5034 break;
5036 case WM_HSCROLL:
5037 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5038 break;
5040 case WM_KEYDOWN:
5041 result = EDIT_WM_KeyDown(es, (INT)wParam);
5042 break;
5044 case WM_KILLFOCUS:
5045 result = EDIT_WM_KillFocus(es);
5046 break;
5048 case WM_LBUTTONDBLCLK:
5049 result = EDIT_WM_LButtonDblClk(es);
5050 break;
5052 case WM_LBUTTONDOWN:
5053 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5054 break;
5056 case WM_LBUTTONUP:
5057 result = EDIT_WM_LButtonUp(es);
5058 break;
5060 case WM_MBUTTONDOWN:
5061 result = EDIT_WM_MButtonDown(es);
5062 break;
5064 case WM_MOUSEMOVE:
5065 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5066 break;
5068 case WM_PRINTCLIENT:
5069 case WM_PAINT:
5070 EDIT_WM_Paint(es, (HDC)wParam);
5071 break;
5073 case WM_PASTE:
5074 EDIT_WM_Paste(es);
5075 break;
5077 case WM_SETFOCUS:
5078 EDIT_WM_SetFocus(es);
5079 break;
5081 case WM_SETFONT:
5082 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5083 break;
5085 case WM_SETREDRAW:
5086 /* FIXME: actually set an internal flag and behave accordingly */
5087 break;
5089 case WM_SETTEXT:
5090 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5091 result = TRUE;
5092 break;
5094 case WM_SIZE:
5095 EDIT_WM_Size(es, (UINT)wParam);
5096 break;
5098 case WM_STYLECHANGED:
5099 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5100 break;
5102 case WM_STYLECHANGING:
5103 result = 0; /* See EDIT_WM_StyleChanged */
5104 break;
5106 case WM_SYSKEYDOWN:
5107 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5108 break;
5110 case WM_TIMER:
5111 EDIT_WM_Timer(es);
5112 break;
5114 case WM_VSCROLL:
5115 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5116 break;
5118 case WM_MOUSEWHEEL:
5120 int wheelDelta;
5121 UINT pulScrollLines = 3;
5122 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5124 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5125 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5126 break;
5128 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5129 /* if scrolling changes direction, ignore left overs */
5130 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5131 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5132 es->wheelDeltaRemainder += wheelDelta;
5133 else
5134 es->wheelDeltaRemainder = wheelDelta;
5135 if (es->wheelDeltaRemainder && pulScrollLines)
5137 int cLineScroll;
5138 pulScrollLines = (int) min((UINT) es->line_count, pulScrollLines);
5139 cLineScroll = pulScrollLines * (float)es->wheelDeltaRemainder / WHEEL_DELTA;
5140 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
5141 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5144 break;
5147 /* IME messages to make the edit control IME aware */
5148 case WM_IME_SETCONTEXT:
5149 break;
5151 case WM_IME_STARTCOMPOSITION:
5152 es->composition_start = es->selection_end;
5153 es->composition_len = 0;
5154 break;
5156 case WM_IME_COMPOSITION:
5157 EDIT_ImeComposition(hwnd, lParam, es);
5158 break;
5160 case WM_IME_ENDCOMPOSITION:
5161 if (es->composition_len > 0)
5163 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5164 es->selection_end = es->selection_start;
5165 es->composition_len= 0;
5167 break;
5169 case WM_IME_COMPOSITIONFULL:
5170 break;
5172 case WM_IME_SELECT:
5173 break;
5175 case WM_IME_CONTROL:
5176 break;
5178 case WM_IME_REQUEST:
5179 switch (wParam)
5181 case IMR_QUERYCHARPOSITION:
5183 LRESULT pos;
5184 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5186 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5187 chpos->pt.x = LOWORD(pos);
5188 chpos->pt.y = HIWORD(pos);
5189 chpos->cLineHeight = es->line_height;
5190 chpos->rcDocument = es->format_rect;
5191 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5192 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5193 result = 1;
5194 break;
5197 break;
5199 default:
5200 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5201 break;
5204 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5205 EDIT_UnlockBuffer(es, FALSE);
5207 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5209 return result;
5213 /*********************************************************************
5214 * edit class descriptor
5216 static const WCHAR editW[] = {'E','d','i','t',0};
5217 const struct builtin_class_descr EDIT_builtin_class =
5219 editW, /* name */
5220 CS_DBLCLKS | CS_PARENTDC, /* style */
5221 WINPROC_EDIT, /* proc */
5222 #ifdef __i386__
5223 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
5224 #else
5225 sizeof(EDITSTATE *), /* extra */
5226 #endif
5227 IDC_IBEAM, /* cursor */
5228 0 /* brush */