wevtapi: Add EvtOpenSession stub.
[wine.git] / dlls / user32 / edit.c
blobc8903e26f6cc55bd7d46060e948ad74e68911b9c
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->hlocapp) return;
1269 if (!es->text) {
1271 if(!es->hloc32W) return;
1273 if(es->hloc32A)
1275 CHAR *textA = LocalLock(es->hloc32A);
1276 HLOCAL hloc32W_new;
1277 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1278 if(countW_new > es->buffer_size + 1)
1280 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1281 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1282 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1283 if(hloc32W_new)
1285 es->hloc32W = hloc32W_new;
1286 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1287 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1289 else
1290 WARN("FAILED! Will synchronize partially\n");
1292 es->text = LocalLock(es->hloc32W);
1293 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1294 LocalUnlock(es->hloc32A);
1296 else es->text = LocalLock(es->hloc32W);
1298 es->lock_count++;
1302 /*********************************************************************
1304 * EDIT_UnlockBuffer
1307 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1309 if (es->hlocapp) return;
1311 /* Edit window might be already destroyed */
1312 if(!IsWindow(es->hwndSelf))
1314 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1315 return;
1318 if (!es->lock_count) {
1319 ERR("lock_count == 0 ... please report\n");
1320 return;
1322 if (!es->text) {
1323 ERR("es->text == 0 ... please report\n");
1324 return;
1327 if (force || (es->lock_count == 1)) {
1328 if (es->hloc32W) {
1329 UINT countA = 0;
1330 UINT countW = get_text_length(es) + 1;
1332 if(es->hloc32A)
1334 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1335 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1336 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1337 countA = LocalSize(es->hloc32A);
1338 if(countA_new > countA)
1340 HLOCAL hloc32A_new;
1341 UINT alloc_size = ROUND_TO_GROW(countA_new);
1342 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1343 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1344 if(hloc32A_new)
1346 es->hloc32A = hloc32A_new;
1347 countA = LocalSize(hloc32A_new);
1348 TRACE("Real new size %d bytes\n", countA);
1350 else
1351 WARN("FAILED! Will synchronize partially\n");
1353 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1354 LocalLock(es->hloc32A), countA, NULL, NULL);
1355 LocalUnlock(es->hloc32A);
1358 LocalUnlock(es->hloc32W);
1359 es->text = NULL;
1361 else {
1362 ERR("no buffer ... please report\n");
1363 return;
1366 es->lock_count--;
1370 /*********************************************************************
1372 * EDIT_MakeFit
1374 * Try to fit size + 1 characters in the buffer.
1376 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1378 HLOCAL hNew32W;
1380 if (size <= es->buffer_size)
1381 return TRUE;
1383 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1385 /* Force edit to unlock its buffer. es->text now NULL */
1386 EDIT_UnlockBuffer(es, TRUE);
1388 if (es->hloc32W) {
1389 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1390 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1391 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1392 es->hloc32W = hNew32W;
1393 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1397 EDIT_LockBuffer(es);
1399 if (es->buffer_size < size) {
1400 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1401 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1402 return FALSE;
1403 } else {
1404 TRACE("We now have %d+1\n", es->buffer_size);
1405 return TRUE;
1410 /*********************************************************************
1412 * EDIT_MakeUndoFit
1414 * Try to fit size + 1 bytes in the undo buffer.
1417 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1419 UINT alloc_size;
1421 if (size <= es->undo_buffer_size)
1422 return TRUE;
1424 TRACE("trying to ReAlloc to %d+1\n", size);
1426 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1427 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1428 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1429 return TRUE;
1431 else
1433 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1434 return FALSE;
1439 /*********************************************************************
1441 * EDIT_UpdateTextRegion
1444 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1446 if (es->flags & EF_UPDATE) {
1447 es->flags &= ~EF_UPDATE;
1448 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1450 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1454 /*********************************************************************
1456 * EDIT_UpdateText
1459 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1461 if (es->flags & EF_UPDATE) {
1462 es->flags &= ~EF_UPDATE;
1463 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1465 InvalidateRect(es->hwndSelf, rc, bErase);
1468 /*********************************************************************
1470 * EDIT_SL_InvalidateText
1472 * Called from EDIT_InvalidateText().
1473 * Does the job for single-line controls only.
1476 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1478 RECT line_rect;
1479 RECT rc;
1481 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1482 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1483 EDIT_UpdateText(es, &rc, TRUE);
1486 /*********************************************************************
1488 * EDIT_ML_InvalidateText
1490 * Called from EDIT_InvalidateText().
1491 * Does the job for multi-line controls only.
1494 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1496 INT vlc = get_vertical_line_count(es);
1497 INT sl = EDIT_EM_LineFromChar(es, start);
1498 INT el = EDIT_EM_LineFromChar(es, end);
1499 INT sc;
1500 INT ec;
1501 RECT rc1;
1502 RECT rcWnd;
1503 RECT rcLine;
1504 RECT rcUpdate;
1505 INT l;
1507 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1508 return;
1510 sc = start - EDIT_EM_LineIndex(es, sl);
1511 ec = end - EDIT_EM_LineIndex(es, el);
1512 if (sl < es->y_offset) {
1513 sl = es->y_offset;
1514 sc = 0;
1516 if (el > es->y_offset + vlc) {
1517 el = es->y_offset + vlc;
1518 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1520 GetClientRect(es->hwndSelf, &rc1);
1521 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1522 if (sl == el) {
1523 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1524 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1525 EDIT_UpdateText(es, &rcUpdate, TRUE);
1526 } else {
1527 EDIT_GetLineRect(es, sl, sc,
1528 EDIT_EM_LineLength(es,
1529 EDIT_EM_LineIndex(es, sl)),
1530 &rcLine);
1531 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1532 EDIT_UpdateText(es, &rcUpdate, TRUE);
1533 for (l = sl + 1 ; l < el ; l++) {
1534 EDIT_GetLineRect(es, l, 0,
1535 EDIT_EM_LineLength(es,
1536 EDIT_EM_LineIndex(es, l)),
1537 &rcLine);
1538 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1539 EDIT_UpdateText(es, &rcUpdate, TRUE);
1541 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1542 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1543 EDIT_UpdateText(es, &rcUpdate, TRUE);
1548 /*********************************************************************
1550 * EDIT_InvalidateText
1552 * Invalidate the text from offset start up to, but not including,
1553 * offset end. Useful for (re)painting the selection.
1554 * Regions outside the linewidth are not invalidated.
1555 * end == -1 means end == TextLength.
1556 * start and end need not be ordered.
1559 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1561 if (end == start)
1562 return;
1564 if (end == -1)
1565 end = get_text_length(es);
1567 if (end < start) {
1568 INT tmp = start;
1569 start = end;
1570 end = tmp;
1573 if (es->style & ES_MULTILINE)
1574 EDIT_ML_InvalidateText(es, start, end);
1575 else
1576 EDIT_SL_InvalidateText(es, start, end);
1580 /*********************************************************************
1582 * EDIT_EM_SetSel
1584 * note: unlike the specs say: the order of start and end
1585 * _is_ preserved in Windows. (i.e. start can be > end)
1586 * In other words: this handler is OK
1589 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1591 UINT old_start = es->selection_start;
1592 UINT old_end = es->selection_end;
1593 UINT len = get_text_length(es);
1595 if (start == (UINT)-1) {
1596 start = es->selection_end;
1597 end = es->selection_end;
1598 } else {
1599 start = min(start, len);
1600 end = min(end, len);
1602 es->selection_start = start;
1603 es->selection_end = end;
1604 if (after_wrap)
1605 es->flags |= EF_AFTER_WRAP;
1606 else
1607 es->flags &= ~EF_AFTER_WRAP;
1608 /* Compute the necessary invalidation region. */
1609 /* Note that we don't need to invalidate regions which have
1610 * "never" been selected, or those which are "still" selected.
1611 * In fact, every time we hit a selection boundary, we can
1612 * *toggle* whether we need to invalidate. Thus we can optimize by
1613 * *sorting* the interval endpoints. Let's assume that we sort them
1614 * in this order:
1615 * start <= end <= old_start <= old_end
1616 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1617 * in 5 comparisons; i.e. it is impossible to do better than the
1618 * following: */
1619 ORDER_UINT(end, old_end);
1620 ORDER_UINT(start, old_start);
1621 ORDER_UINT(old_start, old_end);
1622 ORDER_UINT(start, end);
1623 /* Note that at this point 'end' and 'old_start' are not in order, but
1624 * start is definitely the min. and old_end is definitely the max. */
1625 if (end != old_start)
1628 * One can also do
1629 * ORDER_UINT32(end, old_start);
1630 * EDIT_InvalidateText(es, start, end);
1631 * EDIT_InvalidateText(es, old_start, old_end);
1632 * in place of the following if statement.
1633 * (That would complete the optimal five-comparison four-element sort.)
1635 if (old_start > end )
1637 EDIT_InvalidateText(es, start, end);
1638 EDIT_InvalidateText(es, old_start, old_end);
1640 else
1642 EDIT_InvalidateText(es, start, old_start);
1643 EDIT_InvalidateText(es, end, old_end);
1646 else EDIT_InvalidateText(es, start, old_end);
1650 /*********************************************************************
1652 * EDIT_UpdateScrollInfo
1655 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1657 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1659 SCROLLINFO si;
1660 si.cbSize = sizeof(SCROLLINFO);
1661 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1662 si.nMin = 0;
1663 si.nMax = es->line_count - 1;
1664 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1665 si.nPos = es->y_offset;
1666 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1667 si.nMin, si.nMax, si.nPage, si.nPos);
1668 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1671 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1673 SCROLLINFO si;
1674 si.cbSize = sizeof(SCROLLINFO);
1675 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1676 si.nMin = 0;
1677 si.nMax = es->text_width - 1;
1678 si.nPage = es->format_rect.right - es->format_rect.left;
1679 si.nPos = es->x_offset;
1680 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1681 si.nMin, si.nMax, si.nPage, si.nPos);
1682 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1687 /*********************************************************************
1689 * EDIT_EM_LineScroll_internal
1691 * Version of EDIT_EM_LineScroll for internal use.
1692 * It doesn't refuse if ES_MULTILINE is set and assumes that
1693 * dx is in pixels, dy - in lines.
1696 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1698 INT nyoff;
1699 INT x_offset_in_pixels;
1700 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1701 es->line_height;
1703 if (es->style & ES_MULTILINE)
1705 x_offset_in_pixels = es->x_offset;
1707 else
1709 dy = 0;
1710 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1713 if (-dx > x_offset_in_pixels)
1714 dx = -x_offset_in_pixels;
1715 if (dx > es->text_width - x_offset_in_pixels)
1716 dx = es->text_width - x_offset_in_pixels;
1717 nyoff = max(0, es->y_offset + dy);
1718 if (nyoff >= es->line_count - lines_per_page)
1719 nyoff = max(0, es->line_count - lines_per_page);
1720 dy = (es->y_offset - nyoff) * es->line_height;
1721 if (dx || dy) {
1722 RECT rc1;
1723 RECT rc;
1725 es->y_offset = nyoff;
1726 if(es->style & ES_MULTILINE)
1727 es->x_offset += dx;
1728 else
1729 es->x_offset += dx / es->char_width;
1731 GetClientRect(es->hwndSelf, &rc1);
1732 IntersectRect(&rc, &rc1, &es->format_rect);
1733 ScrollWindowEx(es->hwndSelf, -dx, dy,
1734 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1735 /* force scroll info update */
1736 EDIT_UpdateScrollInfo(es);
1738 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1739 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1740 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1741 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1742 return TRUE;
1745 /*********************************************************************
1747 * EM_LINESCROLL
1749 * NOTE: dx is in average character widths, dy - in lines;
1752 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1754 if (!(es->style & ES_MULTILINE))
1755 return FALSE;
1757 dx *= es->char_width;
1758 return EDIT_EM_LineScroll_internal(es, dx, dy);
1762 /*********************************************************************
1764 * EM_SCROLL
1767 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1769 INT dy;
1771 if (!(es->style & ES_MULTILINE))
1772 return (LRESULT)FALSE;
1774 dy = 0;
1776 switch (action) {
1777 case SB_LINEUP:
1778 if (es->y_offset)
1779 dy = -1;
1780 break;
1781 case SB_LINEDOWN:
1782 if (es->y_offset < es->line_count - 1)
1783 dy = 1;
1784 break;
1785 case SB_PAGEUP:
1786 if (es->y_offset)
1787 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1788 break;
1789 case SB_PAGEDOWN:
1790 if (es->y_offset < es->line_count - 1)
1791 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1792 break;
1793 default:
1794 return (LRESULT)FALSE;
1796 if (dy) {
1797 INT vlc = get_vertical_line_count(es);
1798 /* check if we are going to move too far */
1799 if(es->y_offset + dy > es->line_count - vlc)
1800 dy = max(es->line_count - vlc, 0) - es->y_offset;
1802 /* Notification is done in EDIT_EM_LineScroll */
1803 if(dy) {
1804 EDIT_EM_LineScroll(es, 0, dy);
1805 return MAKELONG(dy, TRUE);
1809 return (LRESULT)FALSE;
1813 /*********************************************************************
1815 * EDIT_SetCaretPos
1818 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1819 BOOL after_wrap)
1821 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1822 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1823 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1827 /*********************************************************************
1829 * EM_SCROLLCARET
1832 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1834 if (es->style & ES_MULTILINE) {
1835 INT l;
1836 INT vlc;
1837 INT ww;
1838 INT cw = es->char_width;
1839 INT x;
1840 INT dy = 0;
1841 INT dx = 0;
1843 l = EDIT_EM_LineFromChar(es, es->selection_end);
1844 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1845 vlc = get_vertical_line_count(es);
1846 if (l >= es->y_offset + vlc)
1847 dy = l - vlc + 1 - es->y_offset;
1848 if (l < es->y_offset)
1849 dy = l - es->y_offset;
1850 ww = es->format_rect.right - es->format_rect.left;
1851 if (x < es->format_rect.left)
1852 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1853 if (x > es->format_rect.right)
1854 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1855 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1857 /* check if we are going to move too far */
1858 if(es->x_offset + dx + ww > es->text_width)
1859 dx = es->text_width - ww - es->x_offset;
1860 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1861 EDIT_EM_LineScroll_internal(es, dx, dy);
1863 } else {
1864 INT x;
1865 INT goal;
1866 INT format_width;
1868 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1869 format_width = es->format_rect.right - es->format_rect.left;
1870 if (x < es->format_rect.left) {
1871 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1872 do {
1873 es->x_offset--;
1874 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1875 } while ((x < goal) && es->x_offset);
1876 /* FIXME: use ScrollWindow() somehow to improve performance */
1877 EDIT_UpdateText(es, NULL, TRUE);
1878 } else if (x > es->format_rect.right) {
1879 INT x_last;
1880 INT len = get_text_length(es);
1881 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1882 do {
1883 es->x_offset++;
1884 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1885 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1886 } while ((x > goal) && (x_last > es->format_rect.right));
1887 /* FIXME: use ScrollWindow() somehow to improve performance */
1888 EDIT_UpdateText(es, NULL, TRUE);
1892 if(es->flags & EF_FOCUSED)
1893 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1897 /*********************************************************************
1899 * EDIT_MoveBackward
1902 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1904 INT e = es->selection_end;
1906 if (e) {
1907 e--;
1908 if ((es->style & ES_MULTILINE) && e &&
1909 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1910 e--;
1911 if (e && (es->text[e - 1] == '\r'))
1912 e--;
1915 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1916 EDIT_EM_ScrollCaret(es);
1920 /*********************************************************************
1922 * EDIT_MoveDown_ML
1924 * Only for multi line controls
1925 * Move the caret one line down, on a column with the nearest
1926 * x coordinate on the screen (might be a different column).
1929 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1931 INT s = es->selection_start;
1932 INT e = es->selection_end;
1933 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1934 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1935 INT x = (short)LOWORD(pos);
1936 INT y = (short)HIWORD(pos);
1938 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1939 if (!extend)
1940 s = e;
1941 EDIT_EM_SetSel(es, s, e, after_wrap);
1942 EDIT_EM_ScrollCaret(es);
1946 /*********************************************************************
1948 * EDIT_MoveEnd
1951 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1953 BOOL after_wrap = FALSE;
1954 INT e;
1956 /* Pass a high value in x to make sure of receiving the end of the line */
1957 if (!ctrl && (es->style & ES_MULTILINE))
1958 e = EDIT_CharFromPos(es, 0x3fffffff,
1959 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1960 else
1961 e = get_text_length(es);
1962 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1963 EDIT_EM_ScrollCaret(es);
1967 /*********************************************************************
1969 * EDIT_MoveForward
1972 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1974 INT e = es->selection_end;
1976 if (es->text[e]) {
1977 e++;
1978 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1979 if (es->text[e] == '\n')
1980 e++;
1981 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1982 e += 2;
1985 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1986 EDIT_EM_ScrollCaret(es);
1990 /*********************************************************************
1992 * EDIT_MoveHome
1994 * Home key: move to beginning of line.
1997 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1999 INT e;
2001 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2002 if (!ctrl && (es->style & ES_MULTILINE))
2003 e = EDIT_CharFromPos(es, -es->x_offset,
2004 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2005 else
2006 e = 0;
2007 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2008 EDIT_EM_ScrollCaret(es);
2012 /*********************************************************************
2014 * EDIT_MovePageDown_ML
2016 * Only for multi line controls
2017 * Move the caret one page down, on a column with the nearest
2018 * x coordinate on the screen (might be a different column).
2021 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2023 INT s = es->selection_start;
2024 INT e = es->selection_end;
2025 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2026 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2027 INT x = (short)LOWORD(pos);
2028 INT y = (short)HIWORD(pos);
2030 e = EDIT_CharFromPos(es, x,
2031 y + (es->format_rect.bottom - es->format_rect.top),
2032 &after_wrap);
2033 if (!extend)
2034 s = e;
2035 EDIT_EM_SetSel(es, s, e, after_wrap);
2036 EDIT_EM_ScrollCaret(es);
2040 /*********************************************************************
2042 * EDIT_MovePageUp_ML
2044 * Only for multi line controls
2045 * Move the caret one page up, on a column with the nearest
2046 * x coordinate on the screen (might be a different column).
2049 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2051 INT s = es->selection_start;
2052 INT e = es->selection_end;
2053 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2054 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2055 INT x = (short)LOWORD(pos);
2056 INT y = (short)HIWORD(pos);
2058 e = EDIT_CharFromPos(es, x,
2059 y - (es->format_rect.bottom - es->format_rect.top),
2060 &after_wrap);
2061 if (!extend)
2062 s = e;
2063 EDIT_EM_SetSel(es, s, e, after_wrap);
2064 EDIT_EM_ScrollCaret(es);
2068 /*********************************************************************
2070 * EDIT_MoveUp_ML
2072 * Only for multi line controls
2073 * Move the caret one line up, on a column with the nearest
2074 * x coordinate on the screen (might be a different column).
2077 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2079 INT s = es->selection_start;
2080 INT e = es->selection_end;
2081 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2082 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2083 INT x = (short)LOWORD(pos);
2084 INT y = (short)HIWORD(pos);
2086 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2087 if (!extend)
2088 s = e;
2089 EDIT_EM_SetSel(es, s, e, after_wrap);
2090 EDIT_EM_ScrollCaret(es);
2094 /*********************************************************************
2096 * EDIT_MoveWordBackward
2099 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2101 INT s = es->selection_start;
2102 INT e = es->selection_end;
2103 INT l;
2104 INT ll;
2105 INT li;
2107 l = EDIT_EM_LineFromChar(es, e);
2108 ll = EDIT_EM_LineLength(es, e);
2109 li = EDIT_EM_LineIndex(es, l);
2110 if (e - li == 0) {
2111 if (l) {
2112 li = EDIT_EM_LineIndex(es, l - 1);
2113 e = li + EDIT_EM_LineLength(es, li);
2115 } else {
2116 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2118 if (!extend)
2119 s = e;
2120 EDIT_EM_SetSel(es, s, e, FALSE);
2121 EDIT_EM_ScrollCaret(es);
2125 /*********************************************************************
2127 * EDIT_MoveWordForward
2130 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2132 INT s = es->selection_start;
2133 INT e = es->selection_end;
2134 INT l;
2135 INT ll;
2136 INT li;
2138 l = EDIT_EM_LineFromChar(es, e);
2139 ll = EDIT_EM_LineLength(es, e);
2140 li = EDIT_EM_LineIndex(es, l);
2141 if (e - li == ll) {
2142 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2143 e = EDIT_EM_LineIndex(es, l + 1);
2144 } else {
2145 e = li + EDIT_CallWordBreakProc(es,
2146 li, e - li + 1, ll, WB_RIGHT);
2148 if (!extend)
2149 s = e;
2150 EDIT_EM_SetSel(es, s, e, FALSE);
2151 EDIT_EM_ScrollCaret(es);
2155 /*********************************************************************
2157 * EDIT_PaintText
2160 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2162 COLORREF BkColor;
2163 COLORREF TextColor;
2164 LOGFONTW underline_font;
2165 HFONT hUnderline = 0;
2166 HFONT old_font = 0;
2167 INT ret;
2168 INT li;
2169 INT BkMode;
2170 SIZE size;
2172 if (!count)
2173 return 0;
2174 BkMode = GetBkMode(dc);
2175 BkColor = GetBkColor(dc);
2176 TextColor = GetTextColor(dc);
2177 if (rev) {
2178 if (es->composition_len == 0)
2180 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2181 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2182 SetBkMode( dc, OPAQUE);
2184 else
2186 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2187 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2188 underline_font.lfUnderline = TRUE;
2189 hUnderline = CreateFontIndirectW(&underline_font);
2190 old_font = SelectObject(dc,hUnderline);
2193 li = EDIT_EM_LineIndex(es, line);
2194 if (es->style & ES_MULTILINE) {
2195 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2196 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2197 } else {
2198 TextOutW(dc, x, y, es->text + li + col, count);
2199 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2200 ret = size.cx;
2202 if (rev) {
2203 if (es->composition_len == 0)
2205 SetBkColor(dc, BkColor);
2206 SetTextColor(dc, TextColor);
2207 SetBkMode( dc, BkMode);
2209 else
2211 if (old_font)
2212 SelectObject(dc,old_font);
2213 if (hUnderline)
2214 DeleteObject(hUnderline);
2217 return ret;
2221 /*********************************************************************
2223 * EDIT_PaintLine
2226 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2228 INT s = 0;
2229 INT e = 0;
2230 INT li = 0;
2231 INT ll = 0;
2232 INT x;
2233 INT y;
2234 LRESULT pos;
2235 SCRIPT_STRING_ANALYSIS ssa;
2237 if (es->style & ES_MULTILINE) {
2238 INT vlc = get_vertical_line_count(es);
2240 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2241 return;
2242 } else if (line)
2243 return;
2245 TRACE("line=%d\n", line);
2247 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2248 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2249 x = (short)LOWORD(pos);
2250 y = (short)HIWORD(pos);
2252 if (es->style & ES_MULTILINE)
2254 int line_idx = line;
2255 x = -es->x_offset;
2256 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2258 LINEDEF *line_def = es->first_line_def;
2259 int w, lw;
2261 while (line_def && line_idx)
2263 line_def = line_def->next;
2264 line_idx--;
2266 w = es->format_rect.right - es->format_rect.left;
2267 lw = line_def->width;
2269 if (es->style & ES_RIGHT)
2270 x = w - (lw - x);
2271 else if (es->style & ES_CENTER)
2272 x += (w - lw) / 2;
2274 x += es->format_rect.left;
2277 if (rev)
2279 li = EDIT_EM_LineIndex(es, line);
2280 ll = EDIT_EM_LineLength(es, li);
2281 s = min(es->selection_start, es->selection_end);
2282 e = max(es->selection_start, es->selection_end);
2283 s = min(li + ll, max(li, s));
2284 e = min(li + ll, max(li, e));
2287 if (ssa)
2288 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2289 else if (rev && (s != e) &&
2290 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2291 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2292 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2293 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2294 } else
2295 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2299 /*********************************************************************
2301 * EDIT_AdjustFormatRect
2303 * Adjusts the format rectangle for the current font and the
2304 * current client rectangle.
2307 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2309 RECT ClientRect;
2311 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2312 if (es->style & ES_MULTILINE)
2314 INT fw, vlc, max_x_offset, max_y_offset;
2316 vlc = get_vertical_line_count(es);
2317 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2319 /* correct es->x_offset */
2320 fw = es->format_rect.right - es->format_rect.left;
2321 max_x_offset = es->text_width - fw;
2322 if(max_x_offset < 0) max_x_offset = 0;
2323 if(es->x_offset > max_x_offset)
2324 es->x_offset = max_x_offset;
2326 /* correct es->y_offset */
2327 max_y_offset = es->line_count - vlc;
2328 if(max_y_offset < 0) max_y_offset = 0;
2329 if(es->y_offset > max_y_offset)
2330 es->y_offset = max_y_offset;
2332 /* force scroll info update */
2333 EDIT_UpdateScrollInfo(es);
2335 else
2336 /* Windows doesn't care to fix text placement for SL controls */
2337 es->format_rect.bottom = es->format_rect.top + es->line_height;
2339 /* Always stay within the client area */
2340 GetClientRect(es->hwndSelf, &ClientRect);
2341 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2343 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2344 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2346 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2350 /*********************************************************************
2352 * EDIT_SetRectNP
2354 * note: this is not (exactly) the handler called on EM_SETRECTNP
2355 * it is also used to set the rect of a single line control
2358 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2360 LONG_PTR ExStyle;
2361 INT bw, bh;
2362 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2364 CopyRect(&es->format_rect, rc);
2366 if (ExStyle & WS_EX_CLIENTEDGE) {
2367 es->format_rect.left++;
2368 es->format_rect.right--;
2370 if (es->format_rect.bottom - es->format_rect.top
2371 >= es->line_height + 2)
2373 es->format_rect.top++;
2374 es->format_rect.bottom--;
2377 else if (es->style & WS_BORDER) {
2378 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2379 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2380 InflateRect(&es->format_rect, -bw, 0);
2381 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2382 InflateRect(&es->format_rect, 0, -bh);
2385 es->format_rect.left += es->left_margin;
2386 es->format_rect.right -= es->right_margin;
2387 EDIT_AdjustFormatRect(es);
2391 /*********************************************************************
2393 * EM_CHARFROMPOS
2395 * returns line number (not index) in high-order word of result.
2396 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2397 * to Richedit, not to the edit control. Original documentation is valid.
2398 * FIXME: do the specs mean to return -1 if outside client area or
2399 * if outside formatting rectangle ???
2402 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2404 POINT pt;
2405 RECT rc;
2406 INT index;
2408 pt.x = x;
2409 pt.y = y;
2410 GetClientRect(es->hwndSelf, &rc);
2411 if (!PtInRect(&rc, pt))
2412 return -1;
2414 index = EDIT_CharFromPos(es, x, y, NULL);
2415 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2419 /*********************************************************************
2421 * EM_FMTLINES
2423 * Enable or disable soft breaks.
2425 * This means: insert or remove the soft linebreak character (\r\r\n).
2426 * Take care to check if the text still fits the buffer after insertion.
2427 * If not, notify with EN_ERRSPACE.
2430 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2432 es->flags &= ~EF_USE_SOFTBRK;
2433 if (add_eol) {
2434 es->flags |= EF_USE_SOFTBRK;
2435 FIXME("soft break enabled, not implemented\n");
2437 return add_eol;
2441 /*********************************************************************
2443 * EM_GETHANDLE
2445 * Hopefully this won't fire back at us.
2446 * We always start with a fixed buffer in the local heap.
2447 * Despite of the documentation says that the local heap is used
2448 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2449 * buffer on the local heap.
2452 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2454 HLOCAL hLocal;
2456 if (!(es->style & ES_MULTILINE))
2457 return 0;
2459 if(es->is_unicode)
2460 hLocal = es->hloc32W;
2461 else
2463 if(!es->hloc32A)
2465 CHAR *textA;
2466 UINT countA, alloc_size;
2467 TRACE("Allocating 32-bit ANSI alias buffer\n");
2468 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2469 alloc_size = ROUND_TO_GROW(countA);
2470 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2472 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2473 return 0;
2475 textA = LocalLock(es->hloc32A);
2476 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2477 LocalUnlock(es->hloc32A);
2479 hLocal = es->hloc32A;
2482 EDIT_UnlockBuffer(es, TRUE);
2484 /* The text buffer handle belongs to the app */
2485 es->hlocapp = hLocal;
2487 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2488 return hLocal;
2492 /*********************************************************************
2494 * EM_GETLINE
2497 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2499 LPWSTR src;
2500 INT line_len, dst_len;
2501 INT i;
2503 if (es->style & ES_MULTILINE) {
2504 if (line >= es->line_count)
2505 return 0;
2506 } else
2507 line = 0;
2508 i = EDIT_EM_LineIndex(es, line);
2509 src = es->text + i;
2510 line_len = EDIT_EM_LineLength(es, i);
2511 dst_len = *(WORD *)dst;
2512 if(unicode)
2514 if(dst_len <= line_len)
2516 memcpy(dst, src, dst_len * sizeof(WCHAR));
2517 return dst_len;
2519 else /* Append 0 if enough space */
2521 memcpy(dst, src, line_len * sizeof(WCHAR));
2522 dst[line_len] = 0;
2523 return line_len;
2526 else
2528 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2529 if(!ret && line_len) /* Insufficient buffer size */
2530 return dst_len;
2531 if(ret < dst_len) /* Append 0 if enough space */
2532 ((LPSTR)dst)[ret] = 0;
2533 return ret;
2538 /*********************************************************************
2540 * EM_GETSEL
2543 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2545 UINT s = es->selection_start;
2546 UINT e = es->selection_end;
2548 ORDER_UINT(s, e);
2549 if (start)
2550 *start = s;
2551 if (end)
2552 *end = e;
2553 return MAKELONG(s, e);
2557 /*********************************************************************
2559 * EM_REPLACESEL
2561 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2564 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2565 BOOL send_update, BOOL honor_limit)
2567 UINT tl = get_text_length(es);
2568 UINT utl;
2569 UINT s;
2570 UINT e;
2571 UINT i;
2572 UINT size;
2573 LPWSTR p;
2574 HRGN hrgn = 0;
2575 LPWSTR buf = NULL;
2576 UINT bufl;
2578 TRACE("%s, can_undo %d, send_update %d\n",
2579 debugstr_w(lpsz_replace), can_undo, send_update);
2581 s = es->selection_start;
2582 e = es->selection_end;
2584 EDIT_InvalidateUniscribeData(es);
2585 if ((s == e) && !strl)
2586 return;
2588 ORDER_UINT(s, e);
2590 size = tl - (e - s) + strl;
2591 if (!size)
2592 es->text_width = 0;
2594 /* Issue the EN_MAXTEXT notification and continue with replacing text
2595 * so that buffer limit is honored. */
2596 if ((honor_limit) && (size > es->buffer_limit)) {
2597 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2598 /* Buffer limit can be smaller than the actual length of text in combobox */
2599 if (es->buffer_limit < (tl - (e-s)))
2600 strl = 0;
2601 else
2602 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2605 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2606 return;
2608 if (e != s) {
2609 /* there is something to be deleted */
2610 TRACE("deleting stuff.\n");
2611 bufl = e - s;
2612 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2613 if (!buf) return;
2614 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2615 buf[bufl] = 0; /* ensure 0 termination */
2616 /* now delete */
2617 strcpyW(es->text + s, es->text + e);
2618 text_buffer_changed(es);
2620 if (strl) {
2621 /* there is an insertion */
2622 tl = get_text_length(es);
2623 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));
2624 for (p = es->text + tl ; p >= es->text + s ; p--)
2625 p[strl] = p[0];
2626 for (i = 0 , p = es->text + s ; i < strl ; i++)
2627 p[i] = lpsz_replace[i];
2628 if(es->style & ES_UPPERCASE)
2629 CharUpperBuffW(p, strl);
2630 else if(es->style & ES_LOWERCASE)
2631 CharLowerBuffW(p, strl);
2632 text_buffer_changed(es);
2634 if (es->style & ES_MULTILINE)
2636 INT st = min(es->selection_start, es->selection_end);
2637 INT vlc = get_vertical_line_count(es);
2639 hrgn = CreateRectRgn(0, 0, 0, 0);
2640 EDIT_BuildLineDefs_ML(es, st, st + strl,
2641 strl - abs(es->selection_end - es->selection_start), hrgn);
2642 /* if text is too long undo all changes */
2643 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2644 if (strl)
2645 strcpyW(es->text + e, es->text + e + strl);
2646 if (e != s)
2647 for (i = 0 , p = es->text ; i < e - s ; i++)
2648 p[i + s] = buf[i];
2649 text_buffer_changed(es);
2650 EDIT_BuildLineDefs_ML(es, s, e,
2651 abs(es->selection_end - es->selection_start) - strl, hrgn);
2652 strl = 0;
2653 e = s;
2654 hrgn = CreateRectRgn(0, 0, 0, 0);
2655 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2658 else {
2659 INT fw = es->format_rect.right - es->format_rect.left;
2660 EDIT_InvalidateUniscribeData(es);
2661 EDIT_CalcLineWidth_SL(es);
2662 /* remove chars that don't fit */
2663 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2664 while ((es->text_width > fw) && s + strl >= s) {
2665 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2666 strl--;
2667 es->text_length = -1;
2668 EDIT_InvalidateUniscribeData(es);
2669 EDIT_CalcLineWidth_SL(es);
2671 text_buffer_changed(es);
2672 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2676 if (e != s) {
2677 if (can_undo) {
2678 utl = strlenW(es->undo_text);
2679 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2680 /* undo-buffer is extended to the right */
2681 EDIT_MakeUndoFit(es, utl + e - s);
2682 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2683 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2684 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2685 /* undo-buffer is extended to the left */
2686 EDIT_MakeUndoFit(es, utl + e - s);
2687 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2688 p[e - s] = p[0];
2689 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2690 p[i] = buf[i];
2691 es->undo_position = s;
2692 } else {
2693 /* new undo-buffer */
2694 EDIT_MakeUndoFit(es, e - s);
2695 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2696 es->undo_text[e - s] = 0; /* ensure 0 termination */
2697 es->undo_position = s;
2699 /* any deletion makes the old insertion-undo invalid */
2700 es->undo_insert_count = 0;
2701 } else
2702 EDIT_EM_EmptyUndoBuffer(es);
2704 if (strl) {
2705 if (can_undo) {
2706 if ((s == es->undo_position) ||
2707 ((es->undo_insert_count) &&
2708 (s == es->undo_position + es->undo_insert_count)))
2710 * insertion is new and at delete position or
2711 * an extension to either left or right
2713 es->undo_insert_count += strl;
2714 else {
2715 /* new insertion undo */
2716 es->undo_position = s;
2717 es->undo_insert_count = strl;
2718 /* new insertion makes old delete-buffer invalid */
2719 *es->undo_text = '\0';
2721 } else
2722 EDIT_EM_EmptyUndoBuffer(es);
2725 HeapFree(GetProcessHeap(), 0, buf);
2727 s += strl;
2729 /* If text has been deleted and we're right or center aligned then scroll rightward */
2730 if (es->style & (ES_RIGHT | ES_CENTER))
2732 INT delta = strl - abs(es->selection_end - es->selection_start);
2734 if (delta < 0 && es->x_offset)
2736 if (abs(delta) > es->x_offset)
2737 es->x_offset = 0;
2738 else
2739 es->x_offset += delta;
2743 EDIT_EM_SetSel(es, s, s, FALSE);
2744 es->flags |= EF_MODIFIED;
2745 if (send_update) es->flags |= EF_UPDATE;
2746 if (hrgn)
2748 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2749 DeleteObject(hrgn);
2751 else
2752 EDIT_UpdateText(es, NULL, TRUE);
2754 EDIT_EM_ScrollCaret(es);
2756 /* force scroll info update */
2757 EDIT_UpdateScrollInfo(es);
2760 if(send_update || (es->flags & EF_UPDATE))
2762 es->flags &= ~EF_UPDATE;
2763 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2765 EDIT_InvalidateUniscribeData(es);
2769 /*********************************************************************
2771 * EM_SETHANDLE
2773 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2776 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2778 if (!(es->style & ES_MULTILINE))
2779 return;
2781 if (!hloc) {
2782 WARN("called with NULL handle\n");
2783 return;
2786 EDIT_UnlockBuffer(es, TRUE);
2788 if(es->is_unicode)
2790 if(es->hloc32A)
2792 LocalFree(es->hloc32A);
2793 es->hloc32A = NULL;
2795 es->hloc32W = hloc;
2797 else
2799 INT countW, countA;
2800 HLOCAL hloc32W_new;
2801 WCHAR *textW;
2802 CHAR *textA;
2804 countA = LocalSize(hloc);
2805 textA = LocalLock(hloc);
2806 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2807 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2809 ERR("Could not allocate new unicode buffer\n");
2810 return;
2812 textW = LocalLock(hloc32W_new);
2813 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2814 LocalUnlock(hloc32W_new);
2815 LocalUnlock(hloc);
2817 if(es->hloc32W)
2818 LocalFree(es->hloc32W);
2820 es->hloc32W = hloc32W_new;
2821 es->hloc32A = hloc;
2824 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2826 /* The text buffer handle belongs to the control */
2827 es->hlocapp = NULL;
2829 EDIT_LockBuffer(es);
2830 text_buffer_changed(es);
2832 es->x_offset = es->y_offset = 0;
2833 es->selection_start = es->selection_end = 0;
2834 EDIT_EM_EmptyUndoBuffer(es);
2835 es->flags &= ~EF_MODIFIED;
2836 es->flags &= ~EF_UPDATE;
2837 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2838 EDIT_UpdateText(es, NULL, TRUE);
2839 EDIT_EM_ScrollCaret(es);
2840 /* force scroll info update */
2841 EDIT_UpdateScrollInfo(es);
2845 /*********************************************************************
2847 * EM_SETLIMITTEXT
2849 * NOTE: this version currently implements WinNT limits
2852 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2854 if (!limit) limit = ~0u;
2855 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2856 es->buffer_limit = limit;
2860 /*********************************************************************
2862 * EM_SETMARGINS
2864 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2865 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2866 * margin according to the textmetrics of the current font.
2868 * When EC_USEFONTINFO is used in the non_cjk case the margins only
2869 * change if the edit control is equal to or larger than a certain
2870 * size. Though there is an exception for the empty client rect case
2871 * with small font sizes.
2873 static BOOL is_cjk(UINT charset)
2875 switch(charset)
2877 case SHIFTJIS_CHARSET:
2878 case HANGUL_CHARSET:
2879 case GB2312_CHARSET:
2880 case CHINESEBIG5_CHARSET:
2881 return TRUE;
2883 /* HANGUL_CHARSET is strange, though treated as CJK by Win 8, it is
2884 * not by other versions including Win 10. */
2885 return FALSE;
2888 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2889 WORD left, WORD right, BOOL repaint)
2891 TEXTMETRICW tm;
2892 INT default_left_margin = 0; /* in pixels */
2893 INT default_right_margin = 0; /* in pixels */
2895 /* Set the default margins depending on the font */
2896 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2897 HDC dc = GetDC(es->hwndSelf);
2898 HFONT old_font = SelectObject(dc, es->font);
2899 LONG width = GdiGetCharDimensions(dc, &tm, NULL);
2900 RECT rc;
2902 /* The default margins are only non zero for TrueType or Vector fonts */
2903 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2904 if (!is_cjk(tm.tmCharSet)) {
2905 default_left_margin = width / 2;
2906 default_right_margin = width / 2;
2908 GetClientRect(es->hwndSelf, &rc);
2909 if (rc.right - rc.left < (width / 2 + width) * 2 &&
2910 (width >= 28 || !IsRectEmpty(&rc)) ) {
2911 default_left_margin = es->left_margin;
2912 default_right_margin = es->right_margin;
2914 } else {
2915 /* FIXME: figure out the CJK values. They are not affected by the client rect. */
2916 default_left_margin = width / 2;
2917 default_right_margin = width / 2;
2920 SelectObject(dc, old_font);
2921 ReleaseDC(es->hwndSelf, dc);
2924 if (action & EC_LEFTMARGIN) {
2925 es->format_rect.left -= es->left_margin;
2926 if (left != EC_USEFONTINFO)
2927 es->left_margin = left;
2928 else
2929 es->left_margin = default_left_margin;
2930 es->format_rect.left += es->left_margin;
2933 if (action & EC_RIGHTMARGIN) {
2934 es->format_rect.right += es->right_margin;
2935 if (right != EC_USEFONTINFO)
2936 es->right_margin = right;
2937 else
2938 es->right_margin = default_right_margin;
2939 es->format_rect.right -= es->right_margin;
2942 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2943 EDIT_AdjustFormatRect(es);
2944 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2947 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2951 /*********************************************************************
2953 * EM_SETPASSWORDCHAR
2956 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2958 LONG style;
2960 if (es->style & ES_MULTILINE)
2961 return;
2963 if (es->password_char == c)
2964 return;
2966 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2967 es->password_char = c;
2968 if (c) {
2969 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2970 es->style |= ES_PASSWORD;
2971 } else {
2972 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2973 es->style &= ~ES_PASSWORD;
2975 EDIT_InvalidateUniscribeData(es);
2976 EDIT_UpdateText(es, NULL, TRUE);
2980 /*********************************************************************
2982 * EM_SETTABSTOPS
2985 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2987 if (!(es->style & ES_MULTILINE))
2988 return FALSE;
2989 HeapFree(GetProcessHeap(), 0, es->tabs);
2990 es->tabs_count = count;
2991 if (!count)
2992 es->tabs = NULL;
2993 else {
2994 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2995 memcpy(es->tabs, tabs, count * sizeof(INT));
2997 EDIT_InvalidateUniscribeData(es);
2998 return TRUE;
3002 /*********************************************************************
3004 * EM_SETWORDBREAKPROC
3007 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3009 if (es->word_break_proc == wbp)
3010 return;
3012 es->word_break_proc = wbp;
3014 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3015 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3016 EDIT_UpdateText(es, NULL, TRUE);
3021 /*********************************************************************
3023 * EM_UNDO / WM_UNDO
3026 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3028 INT ulength;
3029 LPWSTR utext;
3031 /* As per MSDN spec, for a single-line edit control,
3032 the return value is always TRUE */
3033 if( es->style & ES_READONLY )
3034 return !(es->style & ES_MULTILINE);
3036 ulength = strlenW(es->undo_text);
3038 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3040 strcpyW(utext, es->undo_text);
3042 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3043 es->undo_insert_count, debugstr_w(utext));
3045 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3046 EDIT_EM_EmptyUndoBuffer(es);
3047 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
3048 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3049 /* send the notification after the selection start and end are set */
3050 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3051 EDIT_EM_ScrollCaret(es);
3052 HeapFree(GetProcessHeap(), 0, utext);
3054 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3055 es->undo_insert_count, debugstr_w(es->undo_text));
3056 return TRUE;
3060 /* Helper function for WM_CHAR
3062 * According to an MSDN blog article titled "Just because you're a control
3063 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3064 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3065 * a dialog box or not.
3067 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3069 return (es->flags & EF_DIALOGMODE);
3073 /*********************************************************************
3075 * WM_PASTE
3078 static void EDIT_WM_Paste(EDITSTATE *es)
3080 HGLOBAL hsrc;
3081 LPWSTR src, ptr;
3082 int len;
3084 /* Protect read-only edit control from modification */
3085 if(es->style & ES_READONLY)
3086 return;
3088 OpenClipboard(es->hwndSelf);
3089 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3090 src = GlobalLock(hsrc);
3091 len = strlenW(src);
3092 /* Protect single-line edit against pasting new line character */
3093 if (!(es->style & ES_MULTILINE) && ((ptr = strchrW(src, '\n')))) {
3094 len = ptr - src;
3095 if (len && src[len - 1] == '\r')
3096 --len;
3098 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3099 GlobalUnlock(hsrc);
3101 else if (es->style & ES_PASSWORD) {
3102 /* clear selected text in password edit box even with empty clipboard */
3103 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3105 CloseClipboard();
3109 /*********************************************************************
3111 * WM_COPY
3114 static void EDIT_WM_Copy(EDITSTATE *es)
3116 INT s = min(es->selection_start, es->selection_end);
3117 INT e = max(es->selection_start, es->selection_end);
3118 HGLOBAL hdst;
3119 LPWSTR dst;
3120 DWORD len;
3122 if (e == s) return;
3124 len = e - s;
3125 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3126 dst = GlobalLock(hdst);
3127 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3128 dst[len] = 0; /* ensure 0 termination */
3129 TRACE("%s\n", debugstr_w(dst));
3130 GlobalUnlock(hdst);
3131 OpenClipboard(es->hwndSelf);
3132 EmptyClipboard();
3133 SetClipboardData(CF_UNICODETEXT, hdst);
3134 CloseClipboard();
3138 /*********************************************************************
3140 * WM_CLEAR
3143 static inline void EDIT_WM_Clear(EDITSTATE *es)
3145 /* Protect read-only edit control from modification */
3146 if(es->style & ES_READONLY)
3147 return;
3149 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3153 /*********************************************************************
3155 * WM_CUT
3158 static inline void EDIT_WM_Cut(EDITSTATE *es)
3160 EDIT_WM_Copy(es);
3161 EDIT_WM_Clear(es);
3165 /*********************************************************************
3167 * WM_CHAR
3170 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3172 BOOL control;
3174 control = GetKeyState(VK_CONTROL) & 0x8000;
3176 switch (c) {
3177 case '\r':
3178 /* If it's not a multiline edit box, it would be ignored below.
3179 * For multiline edit without ES_WANTRETURN, we have to make a
3180 * special case.
3182 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3183 if (EDIT_IsInsideDialog(es))
3184 break;
3185 case '\n':
3186 if (es->style & ES_MULTILINE) {
3187 if (es->style & ES_READONLY) {
3188 EDIT_MoveHome(es, FALSE, FALSE);
3189 EDIT_MoveDown_ML(es, FALSE);
3190 } else {
3191 static const WCHAR cr_lfW[] = {'\r','\n'};
3192 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, 2, TRUE, TRUE);
3195 break;
3196 case '\t':
3197 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3199 static const WCHAR tabW[] = {'\t'};
3200 if (EDIT_IsInsideDialog(es))
3201 break;
3202 EDIT_EM_ReplaceSel(es, TRUE, tabW, 1, TRUE, TRUE);
3204 break;
3205 case VK_BACK:
3206 if (!(es->style & ES_READONLY) && !control) {
3207 if (es->selection_start != es->selection_end)
3208 EDIT_WM_Clear(es);
3209 else {
3210 /* delete character left of caret */
3211 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3212 EDIT_MoveBackward(es, TRUE);
3213 EDIT_WM_Clear(es);
3216 break;
3217 case 0x03: /* ^C */
3218 if (!(es->style & ES_PASSWORD))
3219 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3220 break;
3221 case 0x16: /* ^V */
3222 if (!(es->style & ES_READONLY))
3223 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3224 break;
3225 case 0x18: /* ^X */
3226 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3227 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3228 break;
3229 case 0x1A: /* ^Z */
3230 if (!(es->style & ES_READONLY))
3231 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3232 break;
3234 default:
3235 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3236 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3237 break;
3239 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3240 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3241 break;
3243 return 1;
3247 /*********************************************************************
3249 * EDIT_ContextMenuCommand
3252 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3254 switch (id) {
3255 case EM_UNDO:
3256 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3257 break;
3258 case WM_CUT:
3259 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3260 break;
3261 case WM_COPY:
3262 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3263 break;
3264 case WM_PASTE:
3265 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3266 break;
3267 case WM_CLEAR:
3268 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3269 break;
3270 case EM_SETSEL:
3271 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3272 break;
3273 default:
3274 ERR("unknown menu item, please report\n");
3275 break;
3280 /*********************************************************************
3282 * WM_CONTEXTMENU
3284 * Note: the resource files resource/sysres_??.rc cannot define a
3285 * single popup menu. Hence we use a (dummy) menubar
3286 * containing the single popup menu as its first item.
3288 * FIXME: the message identifiers have been chosen arbitrarily,
3289 * hence we use MF_BYPOSITION.
3290 * We might as well use the "real" values (anybody knows ?)
3291 * The menu definition is in resources/sysres_??.rc.
3292 * Once these are OK, we better use MF_BYCOMMAND here
3293 * (as we do in EDIT_WM_Command()).
3296 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3298 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3299 HMENU popup = GetSubMenu(menu, 0);
3300 UINT start = es->selection_start;
3301 UINT end = es->selection_end;
3302 UINT cmd;
3304 ORDER_UINT(start, end);
3306 /* undo */
3307 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3308 /* cut */
3309 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3310 /* copy */
3311 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3312 /* paste */
3313 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3314 /* delete */
3315 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3316 /* select all */
3317 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3319 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3321 RECT rc;
3322 /* Windows places the menu at the edit's center in this case */
3323 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3324 x = rc.left + (rc.right - rc.left) / 2;
3325 y = rc.top + (rc.bottom - rc.top) / 2;
3328 if (!(es->flags & EF_FOCUSED))
3329 SetFocus(es->hwndSelf);
3331 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3332 x, y, 0, es->hwndSelf, NULL);
3334 if (cmd)
3335 EDIT_ContextMenuCommand(es, cmd);
3337 DestroyMenu(menu);
3341 /*********************************************************************
3343 * WM_GETTEXT
3346 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3348 if(!count) return 0;
3350 if(unicode)
3352 lstrcpynW(dst, es->text, count);
3353 return strlenW(dst);
3355 else
3357 LPSTR textA = (LPSTR)dst;
3358 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3359 textA[count - 1] = 0; /* ensure 0 termination */
3360 return strlen(textA);
3364 /*********************************************************************
3366 * EDIT_CheckCombo
3369 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3371 HWND hLBox = es->hwndListBox;
3372 HWND hCombo;
3373 BOOL bDropped;
3374 int nEUI;
3376 if (!hLBox)
3377 return FALSE;
3379 hCombo = GetParent(es->hwndSelf);
3380 bDropped = TRUE;
3381 nEUI = 0;
3383 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3385 if (key == VK_UP || key == VK_DOWN)
3387 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3388 nEUI = 1;
3390 if (msg == WM_KEYDOWN || nEUI)
3391 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3394 switch (msg)
3396 case WM_KEYDOWN:
3397 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3399 /* make sure ComboLBox pops up */
3400 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3401 key = VK_F4;
3402 nEUI = 2;
3405 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3406 break;
3408 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3409 if (nEUI)
3410 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3411 else
3412 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3413 break;
3416 if(nEUI == 2)
3417 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3419 return TRUE;
3423 /*********************************************************************
3425 * WM_KEYDOWN
3427 * Handling of special keys that don't produce a WM_CHAR
3428 * (i.e. non-printable keys) & Backspace & Delete
3431 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3433 BOOL shift;
3434 BOOL control;
3436 if (GetKeyState(VK_MENU) & 0x8000)
3437 return 0;
3439 shift = GetKeyState(VK_SHIFT) & 0x8000;
3440 control = GetKeyState(VK_CONTROL) & 0x8000;
3442 switch (key) {
3443 case VK_F4:
3444 case VK_UP:
3445 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3446 break;
3448 /* fall through */
3449 case VK_LEFT:
3450 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3451 EDIT_MoveUp_ML(es, shift);
3452 else
3453 if (control)
3454 EDIT_MoveWordBackward(es, shift);
3455 else
3456 EDIT_MoveBackward(es, shift);
3457 break;
3458 case VK_DOWN:
3459 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3460 break;
3461 /* fall through */
3462 case VK_RIGHT:
3463 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3464 EDIT_MoveDown_ML(es, shift);
3465 else if (control)
3466 EDIT_MoveWordForward(es, shift);
3467 else
3468 EDIT_MoveForward(es, shift);
3469 break;
3470 case VK_HOME:
3471 EDIT_MoveHome(es, shift, control);
3472 break;
3473 case VK_END:
3474 EDIT_MoveEnd(es, shift, control);
3475 break;
3476 case VK_PRIOR:
3477 if (es->style & ES_MULTILINE)
3478 EDIT_MovePageUp_ML(es, shift);
3479 else
3480 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3481 break;
3482 case VK_NEXT:
3483 if (es->style & ES_MULTILINE)
3484 EDIT_MovePageDown_ML(es, shift);
3485 else
3486 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3487 break;
3488 case VK_DELETE:
3489 if (!(es->style & ES_READONLY) && !(shift && control)) {
3490 if (es->selection_start != es->selection_end) {
3491 if (shift)
3492 EDIT_WM_Cut(es);
3493 else
3494 EDIT_WM_Clear(es);
3495 } else {
3496 if (shift) {
3497 /* delete character left of caret */
3498 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3499 EDIT_MoveBackward(es, TRUE);
3500 EDIT_WM_Clear(es);
3501 } else if (control) {
3502 /* delete to end of line */
3503 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3504 EDIT_MoveEnd(es, TRUE, FALSE);
3505 EDIT_WM_Clear(es);
3506 } else {
3507 /* delete character right of caret */
3508 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3509 EDIT_MoveForward(es, TRUE);
3510 EDIT_WM_Clear(es);
3514 break;
3515 case VK_INSERT:
3516 if (shift) {
3517 if (!(es->style & ES_READONLY))
3518 EDIT_WM_Paste(es);
3519 } else if (control)
3520 EDIT_WM_Copy(es);
3521 break;
3522 case VK_RETURN:
3523 /* If the edit doesn't want the return send a message to the default object */
3524 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3526 DWORD dw;
3528 if (!EDIT_IsInsideDialog(es)) break;
3529 if (control) break;
3530 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3531 if (HIWORD(dw) == DC_HASDEFID)
3533 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3534 if (hwDefCtrl)
3536 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3537 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3541 break;
3542 case VK_ESCAPE:
3543 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3544 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3545 break;
3546 case VK_TAB:
3547 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3548 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3549 break;
3551 return TRUE;
3555 /*********************************************************************
3557 * WM_KILLFOCUS
3560 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3562 es->flags &= ~EF_FOCUSED;
3563 DestroyCaret();
3564 if(!(es->style & ES_NOHIDESEL))
3565 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3566 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3567 /* throw away left over scroll when we lose focus */
3568 es->wheelDeltaRemainder = 0;
3569 return 0;
3573 /*********************************************************************
3575 * WM_LBUTTONDBLCLK
3577 * The caret position has been set on the WM_LBUTTONDOWN message
3580 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3582 INT s;
3583 INT e = es->selection_end;
3584 INT l;
3585 INT li;
3586 INT ll;
3588 es->bCaptureState = TRUE;
3589 SetCapture(es->hwndSelf);
3591 l = EDIT_EM_LineFromChar(es, e);
3592 li = EDIT_EM_LineIndex(es, l);
3593 ll = EDIT_EM_LineLength(es, e);
3594 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3595 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3596 EDIT_EM_SetSel(es, s, e, FALSE);
3597 EDIT_EM_ScrollCaret(es);
3598 es->region_posx = es->region_posy = 0;
3599 SetTimer(es->hwndSelf, 0, 100, NULL);
3600 return 0;
3604 /*********************************************************************
3606 * WM_LBUTTONDOWN
3609 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3611 INT e;
3612 BOOL after_wrap;
3614 es->bCaptureState = TRUE;
3615 SetCapture(es->hwndSelf);
3616 EDIT_ConfinePoint(es, &x, &y);
3617 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3618 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3619 EDIT_EM_ScrollCaret(es);
3620 es->region_posx = es->region_posy = 0;
3621 SetTimer(es->hwndSelf, 0, 100, NULL);
3623 if (!(es->flags & EF_FOCUSED))
3624 SetFocus(es->hwndSelf);
3626 return 0;
3630 /*********************************************************************
3632 * WM_LBUTTONUP
3635 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3637 if (es->bCaptureState) {
3638 KillTimer(es->hwndSelf, 0);
3639 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3641 es->bCaptureState = FALSE;
3642 return 0;
3646 /*********************************************************************
3648 * WM_MBUTTONDOWN
3651 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3653 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3654 return 0;
3658 /*********************************************************************
3660 * WM_MOUSEMOVE
3663 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3665 INT e;
3666 BOOL after_wrap;
3667 INT prex, prey;
3669 /* If the mouse has been captured by process other than the edit control itself,
3670 * the windows edit controls will not select the strings with mouse move.
3672 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3673 return 0;
3676 * FIXME: gotta do some scrolling if outside client
3677 * area. Maybe reset the timer ?
3679 prex = x; prey = y;
3680 EDIT_ConfinePoint(es, &x, &y);
3681 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3682 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3683 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3684 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3685 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3686 return 0;
3690 /*********************************************************************
3692 * WM_PAINT
3695 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3697 PAINTSTRUCT ps;
3698 INT i;
3699 HDC dc;
3700 HFONT old_font = 0;
3701 RECT rc;
3702 RECT rcClient;
3703 RECT rcLine;
3704 RECT rcRgn;
3705 HBRUSH brush;
3706 HBRUSH old_brush;
3707 INT bw, bh;
3708 BOOL rev = es->bEnableState &&
3709 ((es->flags & EF_FOCUSED) ||
3710 (es->style & ES_NOHIDESEL));
3711 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3713 /* The dc we use for calculating may not be the one we paint into.
3714 This is the safest action. */
3715 EDIT_InvalidateUniscribeData(es);
3716 GetClientRect(es->hwndSelf, &rcClient);
3718 /* get the background brush */
3719 brush = EDIT_NotifyCtlColor(es, dc);
3721 /* paint the border and the background */
3722 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3724 if(es->style & WS_BORDER) {
3725 bw = GetSystemMetrics(SM_CXBORDER);
3726 bh = GetSystemMetrics(SM_CYBORDER);
3727 rc = rcClient;
3728 if(es->style & ES_MULTILINE) {
3729 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3730 if(es->style & WS_VSCROLL) rc.right+=bw;
3733 /* Draw the frame. Same code as in nonclient.c */
3734 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3735 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3736 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3737 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3738 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3739 SelectObject(dc, old_brush);
3741 /* Keep the border clean */
3742 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3743 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3746 GetClipBox(dc, &rc);
3747 FillRect(dc, &rc, brush);
3749 IntersectClipRect(dc, es->format_rect.left,
3750 es->format_rect.top,
3751 es->format_rect.right,
3752 es->format_rect.bottom);
3753 if (es->style & ES_MULTILINE) {
3754 rc = rcClient;
3755 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3757 if (es->font)
3758 old_font = SelectObject(dc, es->font);
3760 if (!es->bEnableState)
3761 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3762 GetClipBox(dc, &rcRgn);
3763 if (es->style & ES_MULTILINE) {
3764 INT vlc = get_vertical_line_count(es);
3765 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3766 EDIT_UpdateUniscribeData(es, dc, i);
3767 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3768 if (IntersectRect(&rc, &rcRgn, &rcLine))
3769 EDIT_PaintLine(es, dc, i, rev);
3771 } else {
3772 EDIT_UpdateUniscribeData(es, dc, 0);
3773 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3774 if (IntersectRect(&rc, &rcRgn, &rcLine))
3775 EDIT_PaintLine(es, dc, 0, rev);
3777 if (es->font)
3778 SelectObject(dc, old_font);
3780 if (!hdc)
3781 EndPaint(es->hwndSelf, &ps);
3785 /*********************************************************************
3787 * WM_SETFOCUS
3790 static void EDIT_WM_SetFocus(EDITSTATE *es)
3792 es->flags |= EF_FOCUSED;
3794 if (!(es->style & ES_NOHIDESEL))
3795 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3797 /* single line edit updates itself */
3798 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3800 HDC hdc = GetDC(es->hwndSelf);
3801 EDIT_WM_Paint(es, hdc);
3802 ReleaseDC(es->hwndSelf, hdc);
3805 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3806 EDIT_SetCaretPos(es, es->selection_end,
3807 es->flags & EF_AFTER_WRAP);
3808 ShowCaret(es->hwndSelf);
3809 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3813 /*********************************************************************
3815 * WM_SETFONT
3817 * With Win95 look the margins are set to default font value unless
3818 * the system font (font == 0) is being set, in which case they are left
3819 * unchanged.
3822 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3824 TEXTMETRICW tm;
3825 HDC dc;
3826 HFONT old_font = 0;
3827 RECT clientRect;
3829 es->font = font;
3830 EDIT_InvalidateUniscribeData(es);
3831 dc = GetDC(es->hwndSelf);
3832 if (font)
3833 old_font = SelectObject(dc, font);
3834 GetTextMetricsW(dc, &tm);
3835 es->line_height = tm.tmHeight;
3836 es->char_width = tm.tmAveCharWidth;
3837 if (font)
3838 SelectObject(dc, old_font);
3839 ReleaseDC(es->hwndSelf, dc);
3841 /* Reset the format rect and the margins */
3842 GetClientRect(es->hwndSelf, &clientRect);
3843 EDIT_SetRectNP(es, &clientRect);
3844 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3845 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3847 if (es->style & ES_MULTILINE)
3848 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3849 else
3850 EDIT_CalcLineWidth_SL(es);
3852 if (redraw)
3853 EDIT_UpdateText(es, NULL, TRUE);
3854 if (es->flags & EF_FOCUSED) {
3855 DestroyCaret();
3856 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3857 EDIT_SetCaretPos(es, es->selection_end,
3858 es->flags & EF_AFTER_WRAP);
3859 ShowCaret(es->hwndSelf);
3864 /*********************************************************************
3866 * WM_SETTEXT
3868 * NOTES
3869 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3870 * The modified flag is reset. No notifications are sent.
3872 * For single-line controls, reception of WM_SETTEXT triggers:
3873 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3876 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3878 LPWSTR textW = NULL;
3879 if (!unicode && text)
3881 LPCSTR textA = (LPCSTR)text;
3882 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3883 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3884 if (textW)
3885 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3886 text = textW;
3889 if (es->flags & EF_UPDATE)
3890 /* fixed this bug once; complain if we see it about to happen again. */
3891 ERR("SetSel may generate UPDATE message whose handler may reset "
3892 "selection.\n");
3894 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3895 if (text)
3897 TRACE("%s\n", debugstr_w(text));
3898 EDIT_EM_ReplaceSel(es, FALSE, text, strlenW(text), FALSE, FALSE);
3899 if(!unicode)
3900 HeapFree(GetProcessHeap(), 0, textW);
3902 else
3904 TRACE("<NULL>\n");
3905 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3907 es->x_offset = 0;
3908 es->flags &= ~EF_MODIFIED;
3909 EDIT_EM_SetSel(es, 0, 0, FALSE);
3911 /* Send the notification after the selection start and end have been set
3912 * edit control doesn't send notification on WM_SETTEXT
3913 * if it is multiline, or it is part of combobox
3915 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3917 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3918 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3920 EDIT_EM_ScrollCaret(es);
3921 EDIT_UpdateScrollInfo(es);
3922 EDIT_InvalidateUniscribeData(es);
3926 /*********************************************************************
3928 * WM_SIZE
3931 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3933 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3934 RECT rc;
3935 GetClientRect(es->hwndSelf, &rc);
3936 EDIT_SetRectNP(es, &rc);
3937 EDIT_UpdateText(es, NULL, TRUE);
3942 /*********************************************************************
3944 * WM_STYLECHANGED
3946 * This message is sent by SetWindowLong on having changed either the Style
3947 * or the extended style.
3949 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3951 * See also EDIT_WM_NCCreate
3953 * It appears that the Windows version of the edit control allows the style
3954 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3955 * style variable which will generally be different. In this function we
3956 * update the internal style based on what changed in the externally visible
3957 * style.
3959 * Much of this content as based upon the MSDN, especially:
3960 * Platform SDK Documentation -> User Interface Services ->
3961 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3962 * Edit Control Styles
3964 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3966 if (GWL_STYLE == which) {
3967 DWORD style_change_mask;
3968 DWORD new_style;
3969 /* Only a subset of changes can be applied after the control
3970 * has been created.
3972 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3973 ES_NUMBER;
3974 if (es->style & ES_MULTILINE)
3975 style_change_mask |= ES_WANTRETURN;
3977 new_style = style->styleNew & style_change_mask;
3979 /* Number overrides lowercase overrides uppercase (at least it
3980 * does in Win95). However I'll bet that ES_NUMBER would be
3981 * invalid under Win 3.1.
3983 if (new_style & ES_NUMBER) {
3984 ; /* do not override the ES_NUMBER */
3985 } else if (new_style & ES_LOWERCASE) {
3986 new_style &= ~ES_UPPERCASE;
3989 es->style = (es->style & ~style_change_mask) | new_style;
3990 } else if (GWL_EXSTYLE == which) {
3991 ; /* FIXME - what is needed here */
3992 } else {
3993 WARN ("Invalid style change %ld\n",which);
3996 return 0;
3999 /*********************************************************************
4001 * WM_SYSKEYDOWN
4004 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4006 if ((key == VK_BACK) && (key_data & 0x2000)) {
4007 if (EDIT_EM_CanUndo(es))
4008 EDIT_EM_Undo(es);
4009 return 0;
4010 } else if (key == VK_UP || key == VK_DOWN) {
4011 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4012 return 0;
4014 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4018 /*********************************************************************
4020 * WM_TIMER
4023 static void EDIT_WM_Timer(EDITSTATE *es)
4025 if (es->region_posx < 0) {
4026 EDIT_MoveBackward(es, TRUE);
4027 } else if (es->region_posx > 0) {
4028 EDIT_MoveForward(es, TRUE);
4031 * FIXME: gotta do some vertical scrolling here, like
4032 * EDIT_EM_LineScroll(hwnd, 0, 1);
4036 /*********************************************************************
4038 * WM_HSCROLL
4041 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4043 INT dx;
4044 INT fw;
4046 if (!(es->style & ES_MULTILINE))
4047 return 0;
4049 if (!(es->style & ES_AUTOHSCROLL))
4050 return 0;
4052 dx = 0;
4053 fw = es->format_rect.right - es->format_rect.left;
4054 switch (action) {
4055 case SB_LINELEFT:
4056 TRACE("SB_LINELEFT\n");
4057 if (es->x_offset)
4058 dx = -es->char_width;
4059 break;
4060 case SB_LINERIGHT:
4061 TRACE("SB_LINERIGHT\n");
4062 if (es->x_offset < es->text_width)
4063 dx = es->char_width;
4064 break;
4065 case SB_PAGELEFT:
4066 TRACE("SB_PAGELEFT\n");
4067 if (es->x_offset)
4068 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4069 break;
4070 case SB_PAGERIGHT:
4071 TRACE("SB_PAGERIGHT\n");
4072 if (es->x_offset < es->text_width)
4073 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4074 break;
4075 case SB_LEFT:
4076 TRACE("SB_LEFT\n");
4077 if (es->x_offset)
4078 dx = -es->x_offset;
4079 break;
4080 case SB_RIGHT:
4081 TRACE("SB_RIGHT\n");
4082 if (es->x_offset < es->text_width)
4083 dx = es->text_width - es->x_offset;
4084 break;
4085 case SB_THUMBTRACK:
4086 TRACE("SB_THUMBTRACK %d\n", pos);
4087 es->flags |= EF_HSCROLL_TRACK;
4088 if(es->style & WS_HSCROLL)
4089 dx = pos - es->x_offset;
4090 else
4092 INT fw, new_x;
4093 /* Sanity check */
4094 if(pos < 0 || pos > 100) return 0;
4095 /* Assume default scroll range 0-100 */
4096 fw = es->format_rect.right - es->format_rect.left;
4097 new_x = pos * (es->text_width - fw) / 100;
4098 dx = es->text_width ? (new_x - es->x_offset) : 0;
4100 break;
4101 case SB_THUMBPOSITION:
4102 TRACE("SB_THUMBPOSITION %d\n", pos);
4103 es->flags &= ~EF_HSCROLL_TRACK;
4104 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4105 dx = pos - es->x_offset;
4106 else
4108 INT fw, new_x;
4109 /* Sanity check */
4110 if(pos < 0 || pos > 100) return 0;
4111 /* Assume default scroll range 0-100 */
4112 fw = es->format_rect.right - es->format_rect.left;
4113 new_x = pos * (es->text_width - fw) / 100;
4114 dx = es->text_width ? (new_x - es->x_offset) : 0;
4116 if (!dx) {
4117 /* force scroll info update */
4118 EDIT_UpdateScrollInfo(es);
4119 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4121 break;
4122 case SB_ENDSCROLL:
4123 TRACE("SB_ENDSCROLL\n");
4124 break;
4126 * FIXME : the next two are undocumented !
4127 * Are we doing the right thing ?
4128 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4129 * although it's also a regular control message.
4131 case EM_GETTHUMB: /* this one is used by NT notepad */
4133 LRESULT ret;
4134 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4135 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4136 else
4138 /* Assume default scroll range 0-100 */
4139 INT fw = es->format_rect.right - es->format_rect.left;
4140 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4142 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4143 return ret;
4145 case EM_LINESCROLL:
4146 TRACE("EM_LINESCROLL16\n");
4147 dx = pos;
4148 break;
4150 default:
4151 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4152 action, action);
4153 return 0;
4155 if (dx)
4157 INT fw = es->format_rect.right - es->format_rect.left;
4158 /* check if we are going to move too far */
4159 if(es->x_offset + dx + fw > es->text_width)
4160 dx = es->text_width - fw - es->x_offset;
4161 if(dx)
4162 EDIT_EM_LineScroll_internal(es, dx, 0);
4164 return 0;
4168 /*********************************************************************
4170 * WM_VSCROLL
4173 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4175 INT dy;
4177 if (!(es->style & ES_MULTILINE))
4178 return 0;
4180 if (!(es->style & ES_AUTOVSCROLL))
4181 return 0;
4183 dy = 0;
4184 switch (action) {
4185 case SB_LINEUP:
4186 case SB_LINEDOWN:
4187 case SB_PAGEUP:
4188 case SB_PAGEDOWN:
4189 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4190 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4191 (action == SB_PAGEUP ? "SB_PAGEUP" :
4192 "SB_PAGEDOWN"))));
4193 EDIT_EM_Scroll(es, action);
4194 return 0;
4195 case SB_TOP:
4196 TRACE("SB_TOP\n");
4197 dy = -es->y_offset;
4198 break;
4199 case SB_BOTTOM:
4200 TRACE("SB_BOTTOM\n");
4201 dy = es->line_count - 1 - es->y_offset;
4202 break;
4203 case SB_THUMBTRACK:
4204 TRACE("SB_THUMBTRACK %d\n", pos);
4205 es->flags |= EF_VSCROLL_TRACK;
4206 if(es->style & WS_VSCROLL)
4207 dy = pos - es->y_offset;
4208 else
4210 /* Assume default scroll range 0-100 */
4211 INT vlc, new_y;
4212 /* Sanity check */
4213 if(pos < 0 || pos > 100) return 0;
4214 vlc = get_vertical_line_count(es);
4215 new_y = pos * (es->line_count - vlc) / 100;
4216 dy = es->line_count ? (new_y - es->y_offset) : 0;
4217 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4218 es->line_count, es->y_offset, pos, dy);
4220 break;
4221 case SB_THUMBPOSITION:
4222 TRACE("SB_THUMBPOSITION %d\n", pos);
4223 es->flags &= ~EF_VSCROLL_TRACK;
4224 if(es->style & WS_VSCROLL)
4225 dy = pos - es->y_offset;
4226 else
4228 /* Assume default scroll range 0-100 */
4229 INT vlc, new_y;
4230 /* Sanity check */
4231 if(pos < 0 || pos > 100) return 0;
4232 vlc = get_vertical_line_count(es);
4233 new_y = pos * (es->line_count - vlc) / 100;
4234 dy = es->line_count ? (new_y - es->y_offset) : 0;
4235 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4236 es->line_count, es->y_offset, pos, dy);
4238 if (!dy)
4240 /* force scroll info update */
4241 EDIT_UpdateScrollInfo(es);
4242 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4244 break;
4245 case SB_ENDSCROLL:
4246 TRACE("SB_ENDSCROLL\n");
4247 break;
4249 * FIXME : the next two are undocumented !
4250 * Are we doing the right thing ?
4251 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4252 * although it's also a regular control message.
4254 case EM_GETTHUMB: /* this one is used by NT notepad */
4256 LRESULT ret;
4257 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4258 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4259 else
4261 /* Assume default scroll range 0-100 */
4262 INT vlc = get_vertical_line_count(es);
4263 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4265 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4266 return ret;
4268 case EM_LINESCROLL:
4269 TRACE("EM_LINESCROLL %d\n", pos);
4270 dy = pos;
4271 break;
4273 default:
4274 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4275 action, action);
4276 return 0;
4278 if (dy)
4279 EDIT_EM_LineScroll(es, 0, dy);
4280 return 0;
4283 /*********************************************************************
4285 * EM_GETTHUMB
4287 * FIXME: is this right ? (or should it be only VSCROLL)
4288 * (and maybe only for edit controls that really have their
4289 * own scrollbars) (and maybe only for multiline controls ?)
4290 * All in all: very poorly documented
4293 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4295 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4296 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4300 /********************************************************************
4302 * The Following code is to handle inline editing from IMEs
4305 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4307 LONG buflen;
4308 LPWSTR lpCompStr;
4309 LPSTR lpCompStrAttr = NULL;
4310 DWORD dwBufLenAttr;
4312 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4314 if (buflen < 0)
4316 return;
4319 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen);
4320 if (!lpCompStr)
4322 ERR("Unable to allocate IME CompositionString\n");
4323 return;
4326 if (buflen)
4327 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4329 if (CompFlag & GCS_COMPATTR)
4332 * We do not use the attributes yet. it would tell us what characters
4333 * are in transition and which are converted or decided upon
4335 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4336 if (dwBufLenAttr)
4338 dwBufLenAttr ++;
4339 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4340 if (!lpCompStrAttr)
4342 ERR("Unable to allocate IME Attribute String\n");
4343 HeapFree(GetProcessHeap(),0,lpCompStr);
4344 return;
4346 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4347 dwBufLenAttr);
4348 lpCompStrAttr[dwBufLenAttr] = 0;
4352 /* check for change in composition start */
4353 if (es->selection_end < es->composition_start)
4354 es->composition_start = es->selection_end;
4356 /* replace existing selection string */
4357 es->selection_start = es->composition_start;
4359 if (es->composition_len > 0)
4360 es->selection_end = es->composition_start + es->composition_len;
4361 else
4362 es->selection_end = es->selection_start;
4364 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4365 es->composition_len = abs(es->composition_start - es->selection_end);
4367 es->selection_start = es->composition_start;
4368 es->selection_end = es->selection_start + es->composition_len;
4370 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4371 HeapFree(GetProcessHeap(),0,lpCompStr);
4374 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4376 LONG buflen;
4377 LPWSTR lpResultStr;
4379 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4380 if (buflen <= 0)
4382 return;
4385 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4386 if (!lpResultStr)
4388 ERR("Unable to alloc buffer for IME string\n");
4389 return;
4392 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4394 /* check for change in composition start */
4395 if (es->selection_end < es->composition_start)
4396 es->composition_start = es->selection_end;
4398 es->selection_start = es->composition_start;
4399 es->selection_end = es->composition_start + es->composition_len;
4400 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4401 es->composition_start = es->selection_end;
4402 es->composition_len = 0;
4404 HeapFree(GetProcessHeap(),0,lpResultStr);
4407 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4409 HIMC hIMC;
4410 int cursor;
4412 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4414 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4415 es->composition_start = es->selection_end;
4418 hIMC = ImmGetContext(hwnd);
4419 if (!hIMC)
4420 return;
4422 if (CompFlag & GCS_RESULTSTR)
4424 EDIT_GetResultStr(hIMC, es);
4425 cursor = 0;
4427 else
4429 if (CompFlag & GCS_COMPSTR)
4430 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4431 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4433 ImmReleaseContext(hwnd, hIMC);
4434 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4438 /*********************************************************************
4440 * WM_NCCREATE
4442 * See also EDIT_WM_StyleChanged
4444 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4446 EDITSTATE *es;
4447 UINT alloc_size;
4449 TRACE("Creating %s edit control, style = %08x\n",
4450 unicode ? "Unicode" : "ANSI", lpcs->style);
4452 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4453 return FALSE;
4454 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4457 * Note: since the EDITSTATE has not been fully initialized yet,
4458 * we can't use any API calls that may send
4459 * WM_XXX messages before WM_NCCREATE is completed.
4462 es->is_unicode = unicode;
4463 es->style = lpcs->style;
4465 es->bEnableState = !(es->style & WS_DISABLED);
4467 es->hwndSelf = hwnd;
4468 /* Save parent, which will be notified by EN_* messages */
4469 es->hwndParent = lpcs->hwndParent;
4471 if (es->style & ES_COMBO)
4472 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4474 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4475 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4477 /* Number overrides lowercase overrides uppercase (at least it
4478 * does in Win95). However I'll bet that ES_NUMBER would be
4479 * invalid under Win 3.1.
4481 if (es->style & ES_NUMBER) {
4482 ; /* do not override the ES_NUMBER */
4483 } else if (es->style & ES_LOWERCASE) {
4484 es->style &= ~ES_UPPERCASE;
4486 if (es->style & ES_MULTILINE) {
4487 es->buffer_limit = BUFLIMIT_INITIAL;
4488 if (es->style & WS_VSCROLL)
4489 es->style |= ES_AUTOVSCROLL;
4490 if (es->style & WS_HSCROLL)
4491 es->style |= ES_AUTOHSCROLL;
4492 es->style &= ~ES_PASSWORD;
4493 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4494 /* Confirmed - RIGHT overrides CENTER */
4495 if (es->style & ES_RIGHT)
4496 es->style &= ~ES_CENTER;
4497 es->style &= ~WS_HSCROLL;
4498 es->style &= ~ES_AUTOHSCROLL;
4500 } else {
4501 es->buffer_limit = BUFLIMIT_INITIAL;
4502 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4503 es->style &= ~ES_CENTER;
4504 es->style &= ~WS_HSCROLL;
4505 es->style &= ~WS_VSCROLL;
4506 if (es->style & ES_PASSWORD)
4507 es->password_char = '*';
4510 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4511 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4512 goto cleanup;
4513 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4515 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4516 goto cleanup;
4517 es->undo_buffer_size = es->buffer_size;
4519 if (es->style & ES_MULTILINE)
4520 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4521 goto cleanup;
4522 es->line_count = 1;
4525 * In Win95 look and feel, the WS_BORDER style is replaced by the
4526 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4527 * control a nonclient area so we don't need to draw the border.
4528 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4529 * a nonclient area and we should handle painting the border ourselves.
4531 * When making modifications please ensure that the code still works
4532 * for edit controls created directly with style 0x50800000, exStyle 0
4533 * (which should have a single pixel border)
4535 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4536 es->style &= ~WS_BORDER;
4537 else if (es->style & WS_BORDER)
4538 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4540 return TRUE;
4542 cleanup:
4543 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4544 EDIT_InvalidateUniscribeData(es);
4545 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4546 HeapFree(GetProcessHeap(), 0, es->undo_text);
4547 if (es->hloc32W) LocalFree(es->hloc32W);
4548 HeapFree(GetProcessHeap(), 0, es->logAttr);
4549 HeapFree(GetProcessHeap(), 0, es);
4550 return FALSE;
4554 /*********************************************************************
4556 * WM_CREATE
4559 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4561 RECT clientRect;
4563 TRACE("%s\n", debugstr_w(name));
4565 * To initialize some final structure members, we call some helper
4566 * functions. However, since the EDITSTATE is not consistent (i.e.
4567 * not fully initialized), we should be very careful which
4568 * functions can be called, and in what order.
4570 EDIT_WM_SetFont(es, 0, FALSE);
4571 EDIT_EM_EmptyUndoBuffer(es);
4573 /* We need to calculate the format rect
4574 (applications may send EM_SETMARGINS before the control gets visible) */
4575 GetClientRect(es->hwndSelf, &clientRect);
4576 EDIT_SetRectNP(es, &clientRect);
4578 if (name && *name) {
4579 EDIT_EM_ReplaceSel(es, FALSE, name, strlenW(name), FALSE, FALSE);
4580 /* if we insert text to the editline, the text scrolls out
4581 * of the window, as the caret is placed after the insert
4582 * pos normally; thus we reset es->selection... to 0 and
4583 * update caret
4585 es->selection_start = es->selection_end = 0;
4586 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4587 * Messages are only to be sent when the USER does something to
4588 * change the contents. So I am removing this EN_CHANGE
4590 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4592 EDIT_EM_ScrollCaret(es);
4594 /* force scroll info update */
4595 EDIT_UpdateScrollInfo(es);
4596 /* The rule seems to return 1 here for success */
4597 /* Power Builder masked edit controls will crash */
4598 /* if not. */
4599 /* FIXME: is that in all cases so ? */
4600 return 1;
4604 /*********************************************************************
4606 * WM_NCDESTROY
4609 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4611 LINEDEF *pc, *pp;
4613 /* The app can own the text buffer handle */
4614 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4615 LocalFree(es->hloc32W);
4617 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4618 LocalFree(es->hloc32A);
4620 EDIT_InvalidateUniscribeData(es);
4621 pc = es->first_line_def;
4622 while (pc)
4624 pp = pc->next;
4625 HeapFree(GetProcessHeap(), 0, pc);
4626 pc = pp;
4629 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4630 HeapFree(GetProcessHeap(), 0, es->undo_text);
4631 HeapFree(GetProcessHeap(), 0, es);
4633 return 0;
4637 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4639 if(unicode)
4640 return DefWindowProcW(hwnd, msg, wParam, lParam);
4641 else
4642 return DefWindowProcA(hwnd, msg, wParam, lParam);
4645 /*********************************************************************
4647 * EditWndProc_common
4649 * The messages are in the order of the actual integer values
4650 * (which can be found in include/windows.h)
4652 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4654 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4655 LRESULT result = 0;
4657 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4659 if (!es && msg != WM_NCCREATE)
4660 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4662 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4664 switch (msg) {
4665 case EM_GETSEL:
4666 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4667 break;
4669 case EM_SETSEL:
4670 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4671 EDIT_EM_ScrollCaret(es);
4672 result = 1;
4673 break;
4675 case EM_GETRECT:
4676 if (lParam)
4677 *((LPRECT)lParam) = es->format_rect;
4678 break;
4680 case EM_SETRECT:
4681 if ((es->style & ES_MULTILINE) && lParam) {
4682 EDIT_SetRectNP(es, (LPRECT)lParam);
4683 EDIT_UpdateText(es, NULL, TRUE);
4685 break;
4687 case EM_SETRECTNP:
4688 if ((es->style & ES_MULTILINE) && lParam)
4689 EDIT_SetRectNP(es, (LPRECT)lParam);
4690 break;
4692 case EM_SCROLL:
4693 result = EDIT_EM_Scroll(es, (INT)wParam);
4694 break;
4696 case EM_LINESCROLL:
4697 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4698 break;
4700 case EM_SCROLLCARET:
4701 EDIT_EM_ScrollCaret(es);
4702 result = 1;
4703 break;
4705 case EM_GETMODIFY:
4706 result = ((es->flags & EF_MODIFIED) != 0);
4707 break;
4709 case EM_SETMODIFY:
4710 if (wParam)
4711 es->flags |= EF_MODIFIED;
4712 else
4713 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4714 break;
4716 case EM_GETLINECOUNT:
4717 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4718 break;
4720 case EM_LINEINDEX:
4721 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4722 break;
4724 case EM_SETHANDLE:
4725 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4726 break;
4728 case EM_GETHANDLE:
4729 result = (LRESULT)EDIT_EM_GetHandle(es);
4730 break;
4732 case EM_GETTHUMB:
4733 result = EDIT_EM_GetThumb(es);
4734 break;
4736 /* these messages missing from specs */
4737 case 0x00bf:
4738 case 0x00c0:
4739 case 0x00c3:
4740 case 0x00ca:
4741 FIXME("undocumented message 0x%x, please report\n", msg);
4742 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4743 break;
4745 case EM_LINELENGTH:
4746 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4747 break;
4749 case EM_REPLACESEL:
4751 LPWSTR textW;
4753 if(unicode)
4754 textW = (LPWSTR)lParam;
4755 else
4757 LPSTR textA = (LPSTR)lParam;
4758 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4759 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4760 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4763 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, strlenW(textW), TRUE, TRUE);
4764 result = 1;
4766 if(!unicode)
4767 HeapFree(GetProcessHeap(), 0, textW);
4768 break;
4771 case EM_GETLINE:
4772 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4773 break;
4775 case EM_SETLIMITTEXT:
4776 EDIT_EM_SetLimitText(es, wParam);
4777 break;
4779 case EM_CANUNDO:
4780 result = (LRESULT)EDIT_EM_CanUndo(es);
4781 break;
4783 case EM_UNDO:
4784 case WM_UNDO:
4785 result = (LRESULT)EDIT_EM_Undo(es);
4786 break;
4788 case EM_FMTLINES:
4789 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4790 break;
4792 case EM_LINEFROMCHAR:
4793 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4794 break;
4796 case EM_SETTABSTOPS:
4797 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4798 break;
4800 case EM_SETPASSWORDCHAR:
4802 WCHAR charW = 0;
4804 if(unicode)
4805 charW = (WCHAR)wParam;
4806 else
4808 CHAR charA = wParam;
4809 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4812 EDIT_EM_SetPasswordChar(es, charW);
4813 break;
4816 case EM_EMPTYUNDOBUFFER:
4817 EDIT_EM_EmptyUndoBuffer(es);
4818 break;
4820 case EM_GETFIRSTVISIBLELINE:
4821 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4822 break;
4824 case EM_SETREADONLY:
4826 DWORD old_style = es->style;
4828 if (wParam) {
4829 SetWindowLongW( hwnd, GWL_STYLE,
4830 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4831 es->style |= ES_READONLY;
4832 } else {
4833 SetWindowLongW( hwnd, GWL_STYLE,
4834 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4835 es->style &= ~ES_READONLY;
4838 if (old_style ^ es->style)
4839 InvalidateRect(es->hwndSelf, NULL, TRUE);
4841 result = 1;
4842 break;
4845 case EM_SETWORDBREAKPROC:
4846 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4847 break;
4849 case EM_GETWORDBREAKPROC:
4850 result = (LRESULT)es->word_break_proc;
4851 break;
4853 case EM_GETPASSWORDCHAR:
4855 if(unicode)
4856 result = es->password_char;
4857 else
4859 WCHAR charW = es->password_char;
4860 CHAR charA = 0;
4861 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4862 result = charA;
4864 break;
4867 case EM_SETMARGINS:
4868 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4869 break;
4871 case EM_GETMARGINS:
4872 result = MAKELONG(es->left_margin, es->right_margin);
4873 break;
4875 case EM_GETLIMITTEXT:
4876 result = es->buffer_limit;
4877 break;
4879 case EM_POSFROMCHAR:
4880 if ((INT)wParam >= get_text_length(es)) result = -1;
4881 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4882 break;
4884 case EM_CHARFROMPOS:
4885 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4886 break;
4888 /* End of the EM_ messages which were in numerical order; what order
4889 * are these in? vaguely alphabetical?
4892 case WM_NCCREATE:
4893 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4894 break;
4896 case WM_NCDESTROY:
4897 result = EDIT_WM_NCDestroy(es);
4898 es = NULL;
4899 break;
4901 case WM_GETDLGCODE:
4902 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4904 if (es->style & ES_MULTILINE)
4905 result |= DLGC_WANTALLKEYS;
4907 if (lParam)
4909 es->flags|=EF_DIALOGMODE;
4911 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4913 int vk = (int)((LPMSG)lParam)->wParam;
4915 if (es->hwndListBox)
4917 if (vk == VK_RETURN || vk == VK_ESCAPE)
4918 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4919 result |= DLGC_WANTMESSAGE;
4923 break;
4925 case WM_IME_CHAR:
4926 if (!unicode)
4928 WCHAR charW;
4929 CHAR strng[2];
4931 strng[0] = wParam >> 8;
4932 strng[1] = wParam & 0xff;
4933 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4934 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4935 result = EDIT_WM_Char(es, charW);
4936 break;
4938 /* fall through */
4939 case WM_CHAR:
4941 WCHAR charW;
4943 if(unicode)
4944 charW = wParam;
4945 else
4947 CHAR charA = wParam;
4948 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4951 if (es->hwndListBox)
4953 if (charW == VK_RETURN || charW == VK_ESCAPE)
4955 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4956 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4957 break;
4960 result = EDIT_WM_Char(es, charW);
4961 break;
4964 case WM_UNICHAR:
4965 if (unicode)
4967 if (wParam == UNICODE_NOCHAR) return TRUE;
4968 if (wParam <= 0x000fffff)
4970 if(wParam > 0xffff) /* convert to surrogates */
4972 wParam -= 0x10000;
4973 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4974 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4976 else EDIT_WM_Char(es, wParam);
4978 return 0;
4980 break;
4982 case WM_CLEAR:
4983 EDIT_WM_Clear(es);
4984 break;
4986 case WM_CONTEXTMENU:
4987 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4988 break;
4990 case WM_COPY:
4991 EDIT_WM_Copy(es);
4992 break;
4994 case WM_CREATE:
4995 if(unicode)
4996 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4997 else
4999 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
5000 LPWSTR nameW = NULL;
5001 if(nameA)
5003 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5004 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5005 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5007 result = EDIT_WM_Create(es, nameW);
5008 HeapFree(GetProcessHeap(), 0, nameW);
5010 break;
5012 case WM_CUT:
5013 EDIT_WM_Cut(es);
5014 break;
5016 case WM_ENABLE:
5017 es->bEnableState = (BOOL) wParam;
5018 EDIT_UpdateText(es, NULL, TRUE);
5019 break;
5021 case WM_ERASEBKGND:
5022 /* we do the proper erase in EDIT_WM_Paint */
5023 result = 1;
5024 break;
5026 case WM_GETFONT:
5027 result = (LRESULT)es->font;
5028 break;
5030 case WM_GETTEXT:
5031 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5032 break;
5034 case WM_GETTEXTLENGTH:
5035 if (unicode) result = get_text_length(es);
5036 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5037 NULL, 0, NULL, NULL );
5038 break;
5040 case WM_HSCROLL:
5041 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5042 break;
5044 case WM_KEYDOWN:
5045 result = EDIT_WM_KeyDown(es, (INT)wParam);
5046 break;
5048 case WM_KILLFOCUS:
5049 result = EDIT_WM_KillFocus(es);
5050 break;
5052 case WM_LBUTTONDBLCLK:
5053 result = EDIT_WM_LButtonDblClk(es);
5054 break;
5056 case WM_LBUTTONDOWN:
5057 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5058 break;
5060 case WM_LBUTTONUP:
5061 result = EDIT_WM_LButtonUp(es);
5062 break;
5064 case WM_MBUTTONDOWN:
5065 result = EDIT_WM_MButtonDown(es);
5066 break;
5068 case WM_MOUSEMOVE:
5069 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5070 break;
5072 case WM_PRINTCLIENT:
5073 case WM_PAINT:
5074 EDIT_WM_Paint(es, (HDC)wParam);
5075 break;
5077 case WM_PASTE:
5078 EDIT_WM_Paste(es);
5079 break;
5081 case WM_SETFOCUS:
5082 EDIT_WM_SetFocus(es);
5083 break;
5085 case WM_SETFONT:
5086 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5087 break;
5089 case WM_SETREDRAW:
5090 /* FIXME: actually set an internal flag and behave accordingly */
5091 break;
5093 case WM_SETTEXT:
5094 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5095 result = TRUE;
5096 break;
5098 case WM_SIZE:
5099 EDIT_WM_Size(es, (UINT)wParam);
5100 break;
5102 case WM_STYLECHANGED:
5103 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5104 break;
5106 case WM_STYLECHANGING:
5107 result = 0; /* See EDIT_WM_StyleChanged */
5108 break;
5110 case WM_SYSKEYDOWN:
5111 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5112 break;
5114 case WM_TIMER:
5115 EDIT_WM_Timer(es);
5116 break;
5118 case WM_VSCROLL:
5119 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5120 break;
5122 case WM_MOUSEWHEEL:
5124 int wheelDelta;
5125 UINT pulScrollLines = 3;
5126 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5128 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5129 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5130 break;
5132 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5133 /* if scrolling changes direction, ignore left overs */
5134 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5135 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5136 es->wheelDeltaRemainder += wheelDelta;
5137 else
5138 es->wheelDeltaRemainder = wheelDelta;
5139 if (es->wheelDeltaRemainder && pulScrollLines)
5141 int cLineScroll;
5142 pulScrollLines = (int) min((UINT) es->line_count, pulScrollLines);
5143 cLineScroll = pulScrollLines * (float)es->wheelDeltaRemainder / WHEEL_DELTA;
5144 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
5145 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5148 break;
5151 /* IME messages to make the edit control IME aware */
5152 case WM_IME_SETCONTEXT:
5153 break;
5155 case WM_IME_STARTCOMPOSITION:
5156 es->composition_start = es->selection_end;
5157 es->composition_len = 0;
5158 break;
5160 case WM_IME_COMPOSITION:
5161 EDIT_ImeComposition(hwnd, lParam, es);
5162 break;
5164 case WM_IME_ENDCOMPOSITION:
5165 if (es->composition_len > 0)
5167 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5168 es->selection_end = es->selection_start;
5169 es->composition_len= 0;
5171 break;
5173 case WM_IME_COMPOSITIONFULL:
5174 break;
5176 case WM_IME_SELECT:
5177 break;
5179 case WM_IME_CONTROL:
5180 break;
5182 case WM_IME_REQUEST:
5183 switch (wParam)
5185 case IMR_QUERYCHARPOSITION:
5187 LRESULT pos;
5188 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5190 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5191 chpos->pt.x = LOWORD(pos);
5192 chpos->pt.y = HIWORD(pos);
5193 chpos->cLineHeight = es->line_height;
5194 chpos->rcDocument = es->format_rect;
5195 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5196 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5197 result = 1;
5198 break;
5201 break;
5203 default:
5204 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5205 break;
5208 if (IsWindow(hwnd) && es) EDIT_UnlockBuffer(es, FALSE);
5210 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5212 return result;
5216 /*********************************************************************
5217 * edit class descriptor
5219 static const WCHAR editW[] = {'E','d','i','t',0};
5220 const struct builtin_class_descr EDIT_builtin_class =
5222 editW, /* name */
5223 CS_DBLCLKS | CS_PARENTDC, /* style */
5224 WINPROC_EDIT, /* proc */
5225 #ifdef __i386__
5226 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
5227 #else
5228 sizeof(EDITSTATE *), /* extra */
5229 #endif
5230 IDC_IBEAM, /* cursor */
5231 0 /* brush */