explorerframe: Clear pointer when nothing is selected.
[wine.git] / dlls / user32 / edit.c
blob74e0ea460e6918514953e6a921a059fa47810115
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTES
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_OEMCONVERT
45 #include "config.h"
47 #include <stdarg.h>
48 #include <string.h>
49 #include <stdlib.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnt.h"
54 #include "win.h"
55 #include "imm.h"
56 #include "usp10.h"
57 #include "wine/unicode.h"
58 #include "controls.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(edit);
63 WINE_DECLARE_DEBUG_CHANNEL(combo);
64 WINE_DECLARE_DEBUG_CHANNEL(relay);
66 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
67 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
68 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
69 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
72 * extra flags for EDITSTATE.flags field
74 #define EF_MODIFIED 0x0001 /* text has been modified */
75 #define EF_FOCUSED 0x0002 /* we have input focus */
76 #define EF_UPDATE 0x0004 /* notify parent of changed state */
77 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
79 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
80 wrapped line, instead of in front of the next character */
81 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
82 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
83 the text buffer if this is clear. */
84 #define EF_DIALOGMODE 0x0400 /* Indicates that we are inside a dialog window */
86 typedef enum
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
93 } LINE_END;
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
98 LINE_END ending;
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
102 struct tagLINEDEF *next;
103 } LINEDEF;
105 typedef struct
107 BOOL is_unicode; /* how the control was created */
108 LPWSTR text; /* the actual contents of the control */
109 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
110 UINT buffer_size; /* the size of the buffer in characters */
111 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
112 HFONT font; /* NULL means standard system font */
113 INT x_offset; /* scroll offset for multi lines this is in pixels
114 for single lines it's in characters */
115 INT line_height; /* height of a screen line in pixels */
116 INT char_width; /* average character width in pixels */
117 DWORD style; /* sane version of wnd->dwStyle */
118 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
119 INT undo_insert_count; /* number of characters inserted in sequence */
120 UINT undo_position; /* character index of the insertion and deletion */
121 LPWSTR undo_text; /* deleted text */
122 UINT undo_buffer_size; /* size of the deleted text buffer */
123 INT selection_start; /* == selection_end if no selection */
124 INT selection_end; /* == current caret position */
125 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
126 INT left_margin; /* in pixels */
127 INT right_margin; /* in pixels */
128 RECT format_rect;
129 INT text_width; /* width of the widest line in pixels for multi line controls
130 and just line width for single line controls */
131 INT region_posx; /* Position of cursor relative to region: */
132 INT region_posy; /* -1: to left, 0: within, 1: to right */
133 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
134 INT line_count; /* number of lines */
135 INT y_offset; /* scroll offset in number of lines */
136 BOOL bCaptureState; /* flag indicating whether mouse was captured */
137 BOOL bEnableState; /* flag keeping the enable state */
138 HWND hwndSelf; /* the our window handle */
139 HWND hwndParent; /* Handle of parent for sending EN_* messages.
140 Even if parent will change, EN_* messages
141 should be sent to the first parent. */
142 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
143 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
145 * only for multi line controls
147 INT lock_count; /* amount of re-entries in the EditWndProc */
148 INT tabs_count;
149 LPINT tabs;
150 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
151 HLOCAL hloc32W; /* our unicode local memory block */
152 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
153 or EM_SETHANDLE */
155 * IME Data
157 UINT composition_len; /* length of composition, 0 == no composition */
158 int composition_start; /* the character position for the composition */
160 * Uniscribe Data
162 SCRIPT_LOGATTR *logAttr;
163 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
164 } EDITSTATE;
167 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
168 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
170 /* used for disabled or read-only edit control */
171 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
172 do \
173 { /* Notify parent which has created this edit control */ \
174 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
175 SendMessageW(es->hwndParent, WM_COMMAND, \
176 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
177 (LPARAM)(es->hwndSelf)); \
178 } while(0)
180 static const WCHAR empty_stringW[] = {0};
181 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
183 /*********************************************************************
185 * EM_CANUNDO
188 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
190 return (es->undo_insert_count || strlenW(es->undo_text));
194 /*********************************************************************
196 * EM_EMPTYUNDOBUFFER
199 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
201 es->undo_insert_count = 0;
202 *es->undo_text = '\0';
206 /**********************************************************************
207 * get_app_version
209 * Returns the window version in case Wine emulates a later version
210 * of windows than the application expects.
212 * In a number of cases when windows runs an application that was
213 * designed for an earlier windows version, windows reverts
214 * to "old" behaviour of that earlier version.
216 * An example is a disabled edit control that needs to be painted.
217 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
218 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
219 * applications with an expected version 0f 4.0 or higher.
222 static DWORD get_app_version(void)
224 static DWORD version;
225 if (!version)
227 DWORD dwEmulatedVersion;
228 OSVERSIONINFOW info;
229 DWORD dwProcVersion = GetProcessVersion(0);
231 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
232 GetVersionExW( &info );
233 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
234 /* FIXME: this may not be 100% correct; see discussion on the
235 * wine developer list in Nov 1999 */
236 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
238 return version;
241 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
243 HBRUSH hbrush;
244 UINT msg;
246 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
247 msg = WM_CTLCOLORSTATIC;
248 else
249 msg = WM_CTLCOLOREDIT;
251 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
252 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
253 if (!hbrush)
254 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
255 return hbrush;
259 static inline UINT get_text_length(EDITSTATE *es)
261 if(es->text_length == (UINT)-1)
262 es->text_length = strlenW(es->text);
263 return es->text_length;
267 /*********************************************************************
269 * EDIT_WordBreakProc
271 * Find the beginning of words.
272 * Note: unlike the specs for a WordBreakProc, this function can
273 * only be called without linebreaks between s[0] up to
274 * s[count - 1]. Remember it is only called
275 * internally, so we can decide this for ourselves.
276 * Additionally we will always be breaking the full string.
279 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
281 INT ret = 0;
283 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
285 if(!s) return 0;
287 if (!es->logAttr)
289 SCRIPT_ANALYSIS psa;
291 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
292 psa.eScript = SCRIPT_UNDEFINED;
294 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
295 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
298 switch (action) {
299 case WB_LEFT:
300 if (index)
301 index--;
302 while (index && !es->logAttr[index].fSoftBreak)
303 index--;
304 ret = index;
305 break;
306 case WB_RIGHT:
307 if (!count)
308 break;
309 while (s[index] && index < count && !es->logAttr[index].fSoftBreak)
310 index++;
311 ret = index;
312 break;
313 case WB_ISDELIMITER:
314 ret = es->logAttr[index].fWhiteSpace;
315 break;
316 default:
317 ERR("unknown action code, please report !\n");
318 break;
320 return ret;
324 /*********************************************************************
326 * EDIT_CallWordBreakProc
328 * Call appropriate WordBreakProc (internal or external).
330 * Note: The "start" argument should always be an index referring
331 * to es->text. The actual wordbreak proc might be
332 * 16 bit, so we can't always pass any 32 bit LPSTR.
333 * Hence we assume that es->text is the buffer that holds
334 * the string under examination (we can decide this for ourselves).
337 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
339 INT ret;
341 if (es->word_break_proc)
343 if(es->is_unicode)
345 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
347 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
348 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
349 ret = wbpW(es->text + start, index, count, action);
351 else
353 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
354 INT countA;
355 CHAR *textA;
357 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
358 textA = HeapAlloc(GetProcessHeap(), 0, countA);
359 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
360 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
361 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
362 ret = wbpA(textA, index, countA, action);
363 HeapFree(GetProcessHeap(), 0, textA);
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 = 0;
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 = 0, nstart_index = 0;
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.2) d -= 0.2;
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 = 0;
1084 LINEDEF *line_def;
1086 index = min(index, len);
1087 if (es->style & ES_MULTILINE) {
1088 l = EDIT_EM_LineFromChar(es, index);
1089 EDIT_UpdateUniscribeData(es, NULL, l);
1091 y = (l - es->y_offset) * es->line_height;
1092 li = EDIT_EM_LineIndex(es, l);
1093 if (after_wrap && (li == index) && l) {
1094 INT l2 = l - 1;
1095 line_def = es->first_line_def;
1096 while (l2) {
1097 line_def = line_def->next;
1098 l2--;
1100 if (line_def->ending == END_WRAP) {
1101 l--;
1102 y -= es->line_height;
1103 li = EDIT_EM_LineIndex(es, l);
1107 line_def = es->first_line_def;
1108 while (line_def->index != li)
1109 line_def = line_def->next;
1111 lw = line_def->width;
1112 w = es->format_rect.right - es->format_rect.left;
1113 if (line_def->ssa)
1115 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1116 x -= es->x_offset;
1118 else
1119 x = es->x_offset;
1121 if (es->style & ES_RIGHT)
1122 x = w - (lw - x);
1123 else if (es->style & ES_CENTER)
1124 x += (w - lw) / 2;
1125 } else {
1126 INT xoff = 0;
1127 INT xi = 0;
1128 EDIT_UpdateUniscribeData(es, NULL, 0);
1129 if (es->x_offset)
1131 if (es->ssa)
1133 if (es->x_offset >= get_text_length(es))
1135 int leftover = es->x_offset - get_text_length(es);
1136 if (es->ssa)
1138 const SIZE *size;
1139 size = ScriptString_pSize(es->ssa);
1140 xoff = size->cx;
1142 else
1143 xoff = 0;
1144 xoff += es->char_width * leftover;
1146 else
1147 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1149 else
1150 xoff = 0;
1152 if (index)
1154 if (index >= get_text_length(es))
1156 if (es->ssa)
1158 const SIZE *size;
1159 size = ScriptString_pSize(es->ssa);
1160 xi = size->cx;
1162 else
1163 xi = 0;
1165 else if (es->ssa)
1166 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1167 else
1168 xi = 0;
1170 x = xi - xoff;
1172 if (index >= es->x_offset) {
1173 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1175 w = es->format_rect.right - es->format_rect.left;
1176 if (w > es->text_width)
1178 if (es->style & ES_RIGHT)
1179 x += w - es->text_width;
1180 else if (es->style & ES_CENTER)
1181 x += (w - es->text_width) / 2;
1185 y = 0;
1187 x += es->format_rect.left;
1188 y += es->format_rect.top;
1189 return MAKELONG((INT16)x, (INT16)y);
1193 /*********************************************************************
1195 * EDIT_GetLineRect
1197 * Calculates the bounding rectangle for a line from a starting
1198 * column to an ending column.
1201 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1203 SCRIPT_STRING_ANALYSIS ssa;
1204 INT line_index = 0;
1205 INT pt1, pt2, pt3;
1207 if (es->style & ES_MULTILINE)
1209 const LINEDEF *line_def = NULL;
1210 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1211 if (line >= es->line_count)
1212 return;
1214 line_def = es->first_line_def;
1215 if (line == -1) {
1216 INT index = es->selection_end - line_def->length;
1217 while ((index >= 0) && line_def->next) {
1218 line_index += line_def->length;
1219 line_def = line_def->next;
1220 index -= line_def->length;
1222 } else {
1223 while (line > 0) {
1224 line_index += line_def->length;
1225 line_def = line_def->next;
1226 line--;
1229 ssa = line_def->ssa;
1231 else
1233 line_index = 0;
1234 rc->top = es->format_rect.top;
1235 ssa = es->ssa;
1238 rc->bottom = rc->top + es->line_height;
1239 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1240 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1241 if (ssa)
1243 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1244 pt3+=es->format_rect.left;
1246 else pt3 = pt1;
1247 rc->right = max(max(pt1 , pt2),pt3);
1248 rc->left = min(min(pt1, pt2),pt3);
1252 static inline void text_buffer_changed(EDITSTATE *es)
1254 es->text_length = (UINT)-1;
1256 HeapFree( GetProcessHeap(), 0, es->logAttr );
1257 es->logAttr = NULL;
1258 EDIT_InvalidateUniscribeData(es);
1261 /*********************************************************************
1262 * EDIT_LockBuffer
1265 static void EDIT_LockBuffer(EDITSTATE *es)
1267 if (!es->text) {
1269 if(!es->hloc32W) return;
1271 if(es->hloc32A)
1273 CHAR *textA = LocalLock(es->hloc32A);
1274 HLOCAL hloc32W_new;
1275 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1276 if(countW_new > es->buffer_size + 1)
1278 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1279 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1280 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1281 if(hloc32W_new)
1283 es->hloc32W = hloc32W_new;
1284 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1285 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1287 else
1288 WARN("FAILED! Will synchronize partially\n");
1290 es->text = LocalLock(es->hloc32W);
1291 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1292 LocalUnlock(es->hloc32A);
1294 else es->text = LocalLock(es->hloc32W);
1296 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1297 es->lock_count++;
1301 /*********************************************************************
1303 * EDIT_UnlockBuffer
1306 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1309 /* Edit window might be already destroyed */
1310 if(!IsWindow(es->hwndSelf))
1312 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1313 return;
1316 if (!es->lock_count) {
1317 ERR("lock_count == 0 ... please report\n");
1318 return;
1320 if (!es->text) {
1321 ERR("es->text == 0 ... please report\n");
1322 return;
1325 if (force || (es->lock_count == 1)) {
1326 if (es->hloc32W) {
1327 UINT countA = 0;
1328 UINT countW = get_text_length(es) + 1;
1330 if(es->hloc32A)
1332 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1333 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1334 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1335 countA = LocalSize(es->hloc32A);
1336 if(countA_new > countA)
1338 HLOCAL hloc32A_new;
1339 UINT alloc_size = ROUND_TO_GROW(countA_new);
1340 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1341 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1342 if(hloc32A_new)
1344 es->hloc32A = hloc32A_new;
1345 countA = LocalSize(hloc32A_new);
1346 TRACE("Real new size %d bytes\n", countA);
1348 else
1349 WARN("FAILED! Will synchronize partially\n");
1351 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1352 LocalLock(es->hloc32A), countA, NULL, NULL);
1353 LocalUnlock(es->hloc32A);
1356 LocalUnlock(es->hloc32W);
1357 es->text = NULL;
1359 else {
1360 ERR("no buffer ... please report\n");
1361 return;
1364 es->lock_count--;
1368 /*********************************************************************
1370 * EDIT_MakeFit
1372 * Try to fit size + 1 characters in the buffer.
1374 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1376 HLOCAL hNew32W;
1378 if (size <= es->buffer_size)
1379 return TRUE;
1381 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1383 /* Force edit to unlock it's buffer. es->text now NULL */
1384 EDIT_UnlockBuffer(es, TRUE);
1386 if (es->hloc32W) {
1387 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1388 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1389 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1390 es->hloc32W = hNew32W;
1391 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1395 EDIT_LockBuffer(es);
1397 if (es->buffer_size < size) {
1398 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1399 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1400 return FALSE;
1401 } else {
1402 TRACE("We now have %d+1\n", es->buffer_size);
1403 return TRUE;
1408 /*********************************************************************
1410 * EDIT_MakeUndoFit
1412 * Try to fit size + 1 bytes in the undo buffer.
1415 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1417 UINT alloc_size;
1419 if (size <= es->undo_buffer_size)
1420 return TRUE;
1422 TRACE("trying to ReAlloc to %d+1\n", size);
1424 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1425 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1426 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1427 return TRUE;
1429 else
1431 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1432 return FALSE;
1437 /*********************************************************************
1439 * EDIT_UpdateTextRegion
1442 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1444 if (es->flags & EF_UPDATE) {
1445 es->flags &= ~EF_UPDATE;
1446 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1448 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1452 /*********************************************************************
1454 * EDIT_UpdateText
1457 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1459 if (es->flags & EF_UPDATE) {
1460 es->flags &= ~EF_UPDATE;
1461 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1463 InvalidateRect(es->hwndSelf, rc, bErase);
1466 /*********************************************************************
1468 * EDIT_SL_InvalidateText
1470 * Called from EDIT_InvalidateText().
1471 * Does the job for single-line controls only.
1474 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1476 RECT line_rect;
1477 RECT rc;
1479 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1480 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1481 EDIT_UpdateText(es, &rc, TRUE);
1484 /*********************************************************************
1486 * EDIT_ML_InvalidateText
1488 * Called from EDIT_InvalidateText().
1489 * Does the job for multi-line controls only.
1492 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1494 INT vlc = get_vertical_line_count(es);
1495 INT sl = EDIT_EM_LineFromChar(es, start);
1496 INT el = EDIT_EM_LineFromChar(es, end);
1497 INT sc;
1498 INT ec;
1499 RECT rc1;
1500 RECT rcWnd;
1501 RECT rcLine;
1502 RECT rcUpdate;
1503 INT l;
1505 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1506 return;
1508 sc = start - EDIT_EM_LineIndex(es, sl);
1509 ec = end - EDIT_EM_LineIndex(es, el);
1510 if (sl < es->y_offset) {
1511 sl = es->y_offset;
1512 sc = 0;
1514 if (el > es->y_offset + vlc) {
1515 el = es->y_offset + vlc;
1516 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1518 GetClientRect(es->hwndSelf, &rc1);
1519 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1520 if (sl == el) {
1521 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1522 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1523 EDIT_UpdateText(es, &rcUpdate, TRUE);
1524 } else {
1525 EDIT_GetLineRect(es, sl, sc,
1526 EDIT_EM_LineLength(es,
1527 EDIT_EM_LineIndex(es, sl)),
1528 &rcLine);
1529 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1530 EDIT_UpdateText(es, &rcUpdate, TRUE);
1531 for (l = sl + 1 ; l < el ; l++) {
1532 EDIT_GetLineRect(es, l, 0,
1533 EDIT_EM_LineLength(es,
1534 EDIT_EM_LineIndex(es, l)),
1535 &rcLine);
1536 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1537 EDIT_UpdateText(es, &rcUpdate, TRUE);
1539 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1540 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1541 EDIT_UpdateText(es, &rcUpdate, TRUE);
1546 /*********************************************************************
1548 * EDIT_InvalidateText
1550 * Invalidate the text from offset start up to, but not including,
1551 * offset end. Useful for (re)painting the selection.
1552 * Regions outside the linewidth are not invalidated.
1553 * end == -1 means end == TextLength.
1554 * start and end need not be ordered.
1557 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1559 if (end == start)
1560 return;
1562 if (end == -1)
1563 end = get_text_length(es);
1565 if (end < start) {
1566 INT tmp = start;
1567 start = end;
1568 end = tmp;
1571 if (es->style & ES_MULTILINE)
1572 EDIT_ML_InvalidateText(es, start, end);
1573 else
1574 EDIT_SL_InvalidateText(es, start, end);
1578 /*********************************************************************
1580 * EDIT_EM_SetSel
1582 * note: unlike the specs say: the order of start and end
1583 * _is_ preserved in Windows. (i.e. start can be > end)
1584 * In other words: this handler is OK
1587 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1589 UINT old_start = es->selection_start;
1590 UINT old_end = es->selection_end;
1591 UINT len = get_text_length(es);
1593 if (start == (UINT)-1) {
1594 start = es->selection_end;
1595 end = es->selection_end;
1596 } else {
1597 start = min(start, len);
1598 end = min(end, len);
1600 es->selection_start = start;
1601 es->selection_end = end;
1602 if (after_wrap)
1603 es->flags |= EF_AFTER_WRAP;
1604 else
1605 es->flags &= ~EF_AFTER_WRAP;
1606 /* Compute the necessary invalidation region. */
1607 /* Note that we don't need to invalidate regions which have
1608 * "never" been selected, or those which are "still" selected.
1609 * In fact, every time we hit a selection boundary, we can
1610 * *toggle* whether we need to invalidate. Thus we can optimize by
1611 * *sorting* the interval endpoints. Let's assume that we sort them
1612 * in this order:
1613 * start <= end <= old_start <= old_end
1614 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1615 * in 5 comparisons; i.e. it is impossible to do better than the
1616 * following: */
1617 ORDER_UINT(end, old_end);
1618 ORDER_UINT(start, old_start);
1619 ORDER_UINT(old_start, old_end);
1620 ORDER_UINT(start, end);
1621 /* Note that at this point 'end' and 'old_start' are not in order, but
1622 * start is definitely the min. and old_end is definitely the max. */
1623 if (end != old_start)
1626 * One can also do
1627 * ORDER_UINT32(end, old_start);
1628 * EDIT_InvalidateText(es, start, end);
1629 * EDIT_InvalidateText(es, old_start, old_end);
1630 * in place of the following if statement.
1631 * (That would complete the optimal five-comparison four-element sort.)
1633 if (old_start > end )
1635 EDIT_InvalidateText(es, start, end);
1636 EDIT_InvalidateText(es, old_start, old_end);
1638 else
1640 EDIT_InvalidateText(es, start, old_start);
1641 EDIT_InvalidateText(es, end, old_end);
1644 else EDIT_InvalidateText(es, start, old_end);
1648 /*********************************************************************
1650 * EDIT_UpdateScrollInfo
1653 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1655 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1657 SCROLLINFO si;
1658 si.cbSize = sizeof(SCROLLINFO);
1659 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1660 si.nMin = 0;
1661 si.nMax = es->line_count - 1;
1662 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1663 si.nPos = es->y_offset;
1664 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1665 si.nMin, si.nMax, si.nPage, si.nPos);
1666 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1669 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1671 SCROLLINFO si;
1672 si.cbSize = sizeof(SCROLLINFO);
1673 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1674 si.nMin = 0;
1675 si.nMax = es->text_width - 1;
1676 si.nPage = es->format_rect.right - es->format_rect.left;
1677 si.nPos = es->x_offset;
1678 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1679 si.nMin, si.nMax, si.nPage, si.nPos);
1680 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1685 /*********************************************************************
1687 * EDIT_EM_LineScroll_internal
1689 * Version of EDIT_EM_LineScroll for internal use.
1690 * It doesn't refuse if ES_MULTILINE is set and assumes that
1691 * dx is in pixels, dy - in lines.
1694 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1696 INT nyoff;
1697 INT x_offset_in_pixels;
1698 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1699 es->line_height;
1701 if (es->style & ES_MULTILINE)
1703 x_offset_in_pixels = es->x_offset;
1705 else
1707 dy = 0;
1708 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1711 if (-dx > x_offset_in_pixels)
1712 dx = -x_offset_in_pixels;
1713 if (dx > es->text_width - x_offset_in_pixels)
1714 dx = es->text_width - x_offset_in_pixels;
1715 nyoff = max(0, es->y_offset + dy);
1716 if (nyoff >= es->line_count - lines_per_page)
1717 nyoff = max(0, es->line_count - lines_per_page);
1718 dy = (es->y_offset - nyoff) * es->line_height;
1719 if (dx || dy) {
1720 RECT rc1;
1721 RECT rc;
1723 es->y_offset = nyoff;
1724 if(es->style & ES_MULTILINE)
1725 es->x_offset += dx;
1726 else
1727 es->x_offset += dx / es->char_width;
1729 GetClientRect(es->hwndSelf, &rc1);
1730 IntersectRect(&rc, &rc1, &es->format_rect);
1731 ScrollWindowEx(es->hwndSelf, -dx, dy,
1732 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1733 /* force scroll info update */
1734 EDIT_UpdateScrollInfo(es);
1736 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1737 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1738 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1739 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1740 return TRUE;
1743 /*********************************************************************
1745 * EM_LINESCROLL
1747 * NOTE: dx is in average character widths, dy - in lines;
1750 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1752 if (!(es->style & ES_MULTILINE))
1753 return FALSE;
1755 dx *= es->char_width;
1756 return EDIT_EM_LineScroll_internal(es, dx, dy);
1760 /*********************************************************************
1762 * EM_SCROLL
1765 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1767 INT dy;
1769 if (!(es->style & ES_MULTILINE))
1770 return (LRESULT)FALSE;
1772 dy = 0;
1774 switch (action) {
1775 case SB_LINEUP:
1776 if (es->y_offset)
1777 dy = -1;
1778 break;
1779 case SB_LINEDOWN:
1780 if (es->y_offset < es->line_count - 1)
1781 dy = 1;
1782 break;
1783 case SB_PAGEUP:
1784 if (es->y_offset)
1785 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1786 break;
1787 case SB_PAGEDOWN:
1788 if (es->y_offset < es->line_count - 1)
1789 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1790 break;
1791 default:
1792 return (LRESULT)FALSE;
1794 if (dy) {
1795 INT vlc = get_vertical_line_count(es);
1796 /* check if we are going to move too far */
1797 if(es->y_offset + dy > es->line_count - vlc)
1798 dy = max(es->line_count - vlc, 0) - es->y_offset;
1800 /* Notification is done in EDIT_EM_LineScroll */
1801 if(dy) {
1802 EDIT_EM_LineScroll(es, 0, dy);
1803 return MAKELONG(dy, TRUE);
1807 return (LRESULT)FALSE;
1811 /*********************************************************************
1813 * EDIT_SetCaretPos
1816 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1817 BOOL after_wrap)
1819 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1820 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1821 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1825 /*********************************************************************
1827 * EM_SCROLLCARET
1830 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1832 if (es->style & ES_MULTILINE) {
1833 INT l;
1834 INT vlc;
1835 INT ww;
1836 INT cw = es->char_width;
1837 INT x;
1838 INT dy = 0;
1839 INT dx = 0;
1841 l = EDIT_EM_LineFromChar(es, es->selection_end);
1842 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1843 vlc = get_vertical_line_count(es);
1844 if (l >= es->y_offset + vlc)
1845 dy = l - vlc + 1 - es->y_offset;
1846 if (l < es->y_offset)
1847 dy = l - es->y_offset;
1848 ww = es->format_rect.right - es->format_rect.left;
1849 if (x < es->format_rect.left)
1850 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1851 if (x > es->format_rect.right)
1852 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1853 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1855 /* check if we are going to move too far */
1856 if(es->x_offset + dx + ww > es->text_width)
1857 dx = es->text_width - ww - es->x_offset;
1858 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1859 EDIT_EM_LineScroll_internal(es, dx, dy);
1861 } else {
1862 INT x;
1863 INT goal;
1864 INT format_width;
1866 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1867 format_width = es->format_rect.right - es->format_rect.left;
1868 if (x < es->format_rect.left) {
1869 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1870 do {
1871 es->x_offset--;
1872 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1873 } while ((x < goal) && es->x_offset);
1874 /* FIXME: use ScrollWindow() somehow to improve performance */
1875 EDIT_UpdateText(es, NULL, TRUE);
1876 } else if (x > es->format_rect.right) {
1877 INT x_last;
1878 INT len = get_text_length(es);
1879 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1880 do {
1881 es->x_offset++;
1882 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1883 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1884 } while ((x > goal) && (x_last > es->format_rect.right));
1885 /* FIXME: use ScrollWindow() somehow to improve performance */
1886 EDIT_UpdateText(es, NULL, TRUE);
1890 if(es->flags & EF_FOCUSED)
1891 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1895 /*********************************************************************
1897 * EDIT_MoveBackward
1900 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1902 INT e = es->selection_end;
1904 if (e) {
1905 e--;
1906 if ((es->style & ES_MULTILINE) && e &&
1907 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1908 e--;
1909 if (e && (es->text[e - 1] == '\r'))
1910 e--;
1913 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1914 EDIT_EM_ScrollCaret(es);
1918 /*********************************************************************
1920 * EDIT_MoveDown_ML
1922 * Only for multi line controls
1923 * Move the caret one line down, on a column with the nearest
1924 * x coordinate on the screen (might be a different column).
1927 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1929 INT s = es->selection_start;
1930 INT e = es->selection_end;
1931 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1932 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1933 INT x = (short)LOWORD(pos);
1934 INT y = (short)HIWORD(pos);
1936 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1937 if (!extend)
1938 s = e;
1939 EDIT_EM_SetSel(es, s, e, after_wrap);
1940 EDIT_EM_ScrollCaret(es);
1944 /*********************************************************************
1946 * EDIT_MoveEnd
1949 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1951 BOOL after_wrap = FALSE;
1952 INT e;
1954 /* Pass a high value in x to make sure of receiving the end of the line */
1955 if (!ctrl && (es->style & ES_MULTILINE))
1956 e = EDIT_CharFromPos(es, 0x3fffffff,
1957 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1958 else
1959 e = get_text_length(es);
1960 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1961 EDIT_EM_ScrollCaret(es);
1965 /*********************************************************************
1967 * EDIT_MoveForward
1970 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1972 INT e = es->selection_end;
1974 if (es->text[e]) {
1975 e++;
1976 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1977 if (es->text[e] == '\n')
1978 e++;
1979 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1980 e += 2;
1983 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1984 EDIT_EM_ScrollCaret(es);
1988 /*********************************************************************
1990 * EDIT_MoveHome
1992 * Home key: move to beginning of line.
1995 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1997 INT e;
1999 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2000 if (!ctrl && (es->style & ES_MULTILINE))
2001 e = EDIT_CharFromPos(es, -es->x_offset,
2002 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2003 else
2004 e = 0;
2005 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2006 EDIT_EM_ScrollCaret(es);
2010 /*********************************************************************
2012 * EDIT_MovePageDown_ML
2014 * Only for multi line controls
2015 * Move the caret one page down, on a column with the nearest
2016 * x coordinate on the screen (might be a different column).
2019 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2021 INT s = es->selection_start;
2022 INT e = es->selection_end;
2023 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2024 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2025 INT x = (short)LOWORD(pos);
2026 INT y = (short)HIWORD(pos);
2028 e = EDIT_CharFromPos(es, x,
2029 y + (es->format_rect.bottom - es->format_rect.top),
2030 &after_wrap);
2031 if (!extend)
2032 s = e;
2033 EDIT_EM_SetSel(es, s, e, after_wrap);
2034 EDIT_EM_ScrollCaret(es);
2038 /*********************************************************************
2040 * EDIT_MovePageUp_ML
2042 * Only for multi line controls
2043 * Move the caret one page up, on a column with the nearest
2044 * x coordinate on the screen (might be a different column).
2047 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2049 INT s = es->selection_start;
2050 INT e = es->selection_end;
2051 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2052 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2053 INT x = (short)LOWORD(pos);
2054 INT y = (short)HIWORD(pos);
2056 e = EDIT_CharFromPos(es, x,
2057 y - (es->format_rect.bottom - es->format_rect.top),
2058 &after_wrap);
2059 if (!extend)
2060 s = e;
2061 EDIT_EM_SetSel(es, s, e, after_wrap);
2062 EDIT_EM_ScrollCaret(es);
2066 /*********************************************************************
2068 * EDIT_MoveUp_ML
2070 * Only for multi line controls
2071 * Move the caret one line up, on a column with the nearest
2072 * x coordinate on the screen (might be a different column).
2075 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2077 INT s = es->selection_start;
2078 INT e = es->selection_end;
2079 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2080 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2081 INT x = (short)LOWORD(pos);
2082 INT y = (short)HIWORD(pos);
2084 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2085 if (!extend)
2086 s = e;
2087 EDIT_EM_SetSel(es, s, e, after_wrap);
2088 EDIT_EM_ScrollCaret(es);
2092 /*********************************************************************
2094 * EDIT_MoveWordBackward
2097 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2099 INT s = es->selection_start;
2100 INT e = es->selection_end;
2101 INT l;
2102 INT ll;
2103 INT li;
2105 l = EDIT_EM_LineFromChar(es, e);
2106 ll = EDIT_EM_LineLength(es, e);
2107 li = EDIT_EM_LineIndex(es, l);
2108 if (e - li == 0) {
2109 if (l) {
2110 li = EDIT_EM_LineIndex(es, l - 1);
2111 e = li + EDIT_EM_LineLength(es, li);
2113 } else {
2114 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2116 if (!extend)
2117 s = e;
2118 EDIT_EM_SetSel(es, s, e, FALSE);
2119 EDIT_EM_ScrollCaret(es);
2123 /*********************************************************************
2125 * EDIT_MoveWordForward
2128 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2130 INT s = es->selection_start;
2131 INT e = es->selection_end;
2132 INT l;
2133 INT ll;
2134 INT li;
2136 l = EDIT_EM_LineFromChar(es, e);
2137 ll = EDIT_EM_LineLength(es, e);
2138 li = EDIT_EM_LineIndex(es, l);
2139 if (e - li == ll) {
2140 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2141 e = EDIT_EM_LineIndex(es, l + 1);
2142 } else {
2143 e = li + EDIT_CallWordBreakProc(es,
2144 li, e - li + 1, ll, WB_RIGHT);
2146 if (!extend)
2147 s = e;
2148 EDIT_EM_SetSel(es, s, e, FALSE);
2149 EDIT_EM_ScrollCaret(es);
2153 /*********************************************************************
2155 * EDIT_PaintText
2158 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2160 COLORREF BkColor;
2161 COLORREF TextColor;
2162 LOGFONTW underline_font;
2163 HFONT hUnderline = 0;
2164 HFONT old_font = 0;
2165 INT ret;
2166 INT li;
2167 INT BkMode;
2168 SIZE size;
2170 if (!count)
2171 return 0;
2172 BkMode = GetBkMode(dc);
2173 BkColor = GetBkColor(dc);
2174 TextColor = GetTextColor(dc);
2175 if (rev) {
2176 if (es->composition_len == 0)
2178 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2179 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2180 SetBkMode( dc, OPAQUE);
2182 else
2184 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2185 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2186 underline_font.lfUnderline = TRUE;
2187 hUnderline = CreateFontIndirectW(&underline_font);
2188 old_font = SelectObject(dc,hUnderline);
2191 li = EDIT_EM_LineIndex(es, line);
2192 if (es->style & ES_MULTILINE) {
2193 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2194 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2195 } else {
2196 LPWSTR text = es->text;
2197 TextOutW(dc, x, y, text + li + col, count);
2198 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2199 ret = size.cx;
2200 if (es->style & ES_PASSWORD)
2201 HeapFree(GetProcessHeap(), 0, text);
2203 if (rev) {
2204 if (es->composition_len == 0)
2206 SetBkColor(dc, BkColor);
2207 SetTextColor(dc, TextColor);
2208 SetBkMode( dc, BkMode);
2210 else
2212 if (old_font)
2213 SelectObject(dc,old_font);
2214 if (hUnderline)
2215 DeleteObject(hUnderline);
2218 return ret;
2222 /*********************************************************************
2224 * EDIT_PaintLine
2227 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2229 INT s = 0;
2230 INT e = 0;
2231 INT li = 0;
2232 INT ll = 0;
2233 INT x;
2234 INT y;
2235 LRESULT pos;
2236 SCRIPT_STRING_ANALYSIS ssa;
2238 if (es->style & ES_MULTILINE) {
2239 INT vlc = get_vertical_line_count(es);
2241 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2242 return;
2243 } else if (line)
2244 return;
2246 TRACE("line=%d\n", line);
2248 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2249 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2250 x = (short)LOWORD(pos);
2251 y = (short)HIWORD(pos);
2253 if (es->style & ES_MULTILINE)
2255 int line_idx = line;
2256 x = -es->x_offset;
2257 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2259 LINEDEF *line_def = es->first_line_def;
2260 int w, lw;
2262 while (line_def && line_idx)
2264 line_def = line_def->next;
2265 line_idx--;
2267 w = es->format_rect.right - es->format_rect.left;
2268 lw = line_def->width;
2270 if (es->style & ES_RIGHT)
2271 x = w - (lw - x);
2272 else if (es->style & ES_CENTER)
2273 x += (w - lw) / 2;
2275 x += es->format_rect.left;
2278 if (rev)
2280 li = EDIT_EM_LineIndex(es, line);
2281 ll = EDIT_EM_LineLength(es, li);
2282 s = min(es->selection_start, es->selection_end);
2283 e = max(es->selection_start, es->selection_end);
2284 s = min(li + ll, max(li, s));
2285 e = min(li + ll, max(li, e));
2288 if (ssa)
2289 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2290 else if (rev && (s != e) &&
2291 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2292 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2293 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2294 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2295 } else
2296 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2300 /*********************************************************************
2302 * EDIT_AdjustFormatRect
2304 * Adjusts the format rectangle for the current font and the
2305 * current client rectangle.
2308 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2310 RECT ClientRect;
2312 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2313 if (es->style & ES_MULTILINE)
2315 INT fw, vlc, max_x_offset, max_y_offset;
2317 vlc = get_vertical_line_count(es);
2318 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2320 /* correct es->x_offset */
2321 fw = es->format_rect.right - es->format_rect.left;
2322 max_x_offset = es->text_width - fw;
2323 if(max_x_offset < 0) max_x_offset = 0;
2324 if(es->x_offset > max_x_offset)
2325 es->x_offset = max_x_offset;
2327 /* correct es->y_offset */
2328 max_y_offset = es->line_count - vlc;
2329 if(max_y_offset < 0) max_y_offset = 0;
2330 if(es->y_offset > max_y_offset)
2331 es->y_offset = max_y_offset;
2333 /* force scroll info update */
2334 EDIT_UpdateScrollInfo(es);
2336 else
2337 /* Windows doesn't care to fix text placement for SL controls */
2338 es->format_rect.bottom = es->format_rect.top + es->line_height;
2340 /* Always stay within the client area */
2341 GetClientRect(es->hwndSelf, &ClientRect);
2342 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2344 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2345 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2347 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2351 /*********************************************************************
2353 * EDIT_SetRectNP
2355 * note: this is not (exactly) the handler called on EM_SETRECTNP
2356 * it is also used to set the rect of a single line control
2359 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2361 LONG_PTR ExStyle;
2362 INT bw, bh;
2363 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2365 CopyRect(&es->format_rect, rc);
2367 if (ExStyle & WS_EX_CLIENTEDGE) {
2368 es->format_rect.left++;
2369 es->format_rect.right--;
2371 if (es->format_rect.bottom - es->format_rect.top
2372 >= es->line_height + 2)
2374 es->format_rect.top++;
2375 es->format_rect.bottom--;
2378 else if (es->style & WS_BORDER) {
2379 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2380 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2381 es->format_rect.left += bw;
2382 es->format_rect.right -= bw;
2383 if (es->format_rect.bottom - es->format_rect.top
2384 >= es->line_height + 2 * bh)
2386 es->format_rect.top += bh;
2387 es->format_rect.bottom -= bh;
2391 es->format_rect.left += es->left_margin;
2392 es->format_rect.right -= es->right_margin;
2393 EDIT_AdjustFormatRect(es);
2397 /*********************************************************************
2399 * EM_CHARFROMPOS
2401 * returns line number (not index) in high-order word of result.
2402 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2403 * to Richedit, not to the edit control. Original documentation is valid.
2404 * FIXME: do the specs mean to return -1 if outside client area or
2405 * if outside formatting rectangle ???
2408 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2410 POINT pt;
2411 RECT rc;
2412 INT index;
2414 pt.x = x;
2415 pt.y = y;
2416 GetClientRect(es->hwndSelf, &rc);
2417 if (!PtInRect(&rc, pt))
2418 return -1;
2420 index = EDIT_CharFromPos(es, x, y, NULL);
2421 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2425 /*********************************************************************
2427 * EM_FMTLINES
2429 * Enable or disable soft breaks.
2431 * This means: insert or remove the soft linebreak character (\r\r\n).
2432 * Take care to check if the text still fits the buffer after insertion.
2433 * If not, notify with EN_ERRSPACE.
2436 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2438 es->flags &= ~EF_USE_SOFTBRK;
2439 if (add_eol) {
2440 es->flags |= EF_USE_SOFTBRK;
2441 FIXME("soft break enabled, not implemented\n");
2443 return add_eol;
2447 /*********************************************************************
2449 * EM_GETHANDLE
2451 * Hopefully this won't fire back at us.
2452 * We always start with a fixed buffer in the local heap.
2453 * Despite of the documentation says that the local heap is used
2454 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2455 * buffer on the local heap.
2458 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2460 HLOCAL hLocal;
2462 if (!(es->style & ES_MULTILINE))
2463 return 0;
2465 if(es->is_unicode)
2466 hLocal = es->hloc32W;
2467 else
2469 if(!es->hloc32A)
2471 CHAR *textA;
2472 UINT countA, alloc_size;
2473 TRACE("Allocating 32-bit ANSI alias buffer\n");
2474 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2475 alloc_size = ROUND_TO_GROW(countA);
2476 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2478 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2479 return 0;
2481 textA = LocalLock(es->hloc32A);
2482 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2483 LocalUnlock(es->hloc32A);
2485 hLocal = es->hloc32A;
2488 es->flags |= EF_APP_HAS_HANDLE;
2489 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2490 return hLocal;
2494 /*********************************************************************
2496 * EM_GETLINE
2499 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2501 LPWSTR src;
2502 INT line_len, dst_len;
2503 INT i;
2505 if (es->style & ES_MULTILINE) {
2506 if (line >= es->line_count)
2507 return 0;
2508 } else
2509 line = 0;
2510 i = EDIT_EM_LineIndex(es, line);
2511 src = es->text + i;
2512 line_len = EDIT_EM_LineLength(es, i);
2513 dst_len = *(WORD *)dst;
2514 if(unicode)
2516 if(dst_len <= line_len)
2518 memcpy(dst, src, dst_len * sizeof(WCHAR));
2519 return dst_len;
2521 else /* Append 0 if enough space */
2523 memcpy(dst, src, line_len * sizeof(WCHAR));
2524 dst[line_len] = 0;
2525 return line_len;
2528 else
2530 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2531 if(!ret && line_len) /* Insufficient buffer size */
2532 return dst_len;
2533 if(ret < dst_len) /* Append 0 if enough space */
2534 ((LPSTR)dst)[ret] = 0;
2535 return ret;
2540 /*********************************************************************
2542 * EM_GETSEL
2545 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2547 UINT s = es->selection_start;
2548 UINT e = es->selection_end;
2550 ORDER_UINT(s, e);
2551 if (start)
2552 *start = s;
2553 if (end)
2554 *end = e;
2555 return MAKELONG(s, e);
2559 /*********************************************************************
2561 * EM_REPLACESEL
2563 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2566 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2568 UINT strl = strlenW(lpsz_replace);
2569 UINT tl = get_text_length(es);
2570 UINT utl;
2571 UINT s;
2572 UINT e;
2573 UINT i;
2574 UINT size;
2575 LPWSTR p;
2576 HRGN hrgn = 0;
2577 LPWSTR buf = NULL;
2578 UINT bufl = 0;
2580 TRACE("%s, can_undo %d, send_update %d\n",
2581 debugstr_w(lpsz_replace), can_undo, send_update);
2583 s = es->selection_start;
2584 e = es->selection_end;
2586 EDIT_InvalidateUniscribeData(es);
2587 if ((s == e) && !strl)
2588 return;
2590 ORDER_UINT(s, e);
2592 size = tl - (e - s) + strl;
2593 if (!size)
2594 es->text_width = 0;
2596 /* Issue the EN_MAXTEXT notification and continue with replacing text
2597 * such that buffer limit is honored. */
2598 if ((honor_limit) && (size > es->buffer_limit)) {
2599 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2600 /* Buffer limit can be smaller than the actual length of text in combobox */
2601 if (es->buffer_limit < (tl - (e-s)))
2602 strl = 0;
2603 else
2604 strl = es->buffer_limit - (tl - (e-s));
2607 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2608 return;
2610 if (e != s) {
2611 /* there is something to be deleted */
2612 TRACE("deleting stuff.\n");
2613 bufl = e - s;
2614 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2615 if (!buf) return;
2616 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2617 buf[bufl] = 0; /* ensure 0 termination */
2618 /* now delete */
2619 strcpyW(es->text + s, es->text + e);
2620 text_buffer_changed(es);
2622 if (strl) {
2623 /* there is an insertion */
2624 tl = get_text_length(es);
2625 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));
2626 for (p = es->text + tl ; p >= es->text + s ; p--)
2627 p[strl] = p[0];
2628 for (i = 0 , p = es->text + s ; i < strl ; i++)
2629 p[i] = lpsz_replace[i];
2630 if(es->style & ES_UPPERCASE)
2631 CharUpperBuffW(p, strl);
2632 else if(es->style & ES_LOWERCASE)
2633 CharLowerBuffW(p, strl);
2634 text_buffer_changed(es);
2636 if (es->style & ES_MULTILINE)
2638 INT st = min(es->selection_start, es->selection_end);
2639 INT vlc = get_vertical_line_count(es);
2641 hrgn = CreateRectRgn(0, 0, 0, 0);
2642 EDIT_BuildLineDefs_ML(es, st, st + strl,
2643 strl - abs(es->selection_end - es->selection_start), hrgn);
2644 /* if text is too long undo all changes */
2645 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2646 if (strl)
2647 strcpyW(es->text + e, es->text + e + strl);
2648 if (e != s)
2649 for (i = 0 , p = es->text ; i < e - s ; i++)
2650 p[i + s] = buf[i];
2651 text_buffer_changed(es);
2652 EDIT_BuildLineDefs_ML(es, s, e,
2653 abs(es->selection_end - es->selection_start) - strl, hrgn);
2654 strl = 0;
2655 e = s;
2656 hrgn = CreateRectRgn(0, 0, 0, 0);
2657 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2660 else {
2661 INT fw = es->format_rect.right - es->format_rect.left;
2662 EDIT_InvalidateUniscribeData(es);
2663 EDIT_CalcLineWidth_SL(es);
2664 /* remove chars that don't fit */
2665 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2666 while ((es->text_width > fw) && s + strl >= s) {
2667 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2668 strl--;
2669 es->text_length = -1;
2670 EDIT_InvalidateUniscribeData(es);
2671 EDIT_CalcLineWidth_SL(es);
2673 text_buffer_changed(es);
2674 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2678 if (e != s) {
2679 if (can_undo) {
2680 utl = strlenW(es->undo_text);
2681 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2682 /* undo-buffer is extended to the right */
2683 EDIT_MakeUndoFit(es, utl + e - s);
2684 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2685 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2686 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2687 /* undo-buffer is extended to the left */
2688 EDIT_MakeUndoFit(es, utl + e - s);
2689 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2690 p[e - s] = p[0];
2691 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2692 p[i] = buf[i];
2693 es->undo_position = s;
2694 } else {
2695 /* new undo-buffer */
2696 EDIT_MakeUndoFit(es, e - s);
2697 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2698 es->undo_text[e - s] = 0; /* ensure 0 termination */
2699 es->undo_position = s;
2701 /* any deletion makes the old insertion-undo invalid */
2702 es->undo_insert_count = 0;
2703 } else
2704 EDIT_EM_EmptyUndoBuffer(es);
2706 if (strl) {
2707 if (can_undo) {
2708 if ((s == es->undo_position) ||
2709 ((es->undo_insert_count) &&
2710 (s == es->undo_position + es->undo_insert_count)))
2712 * insertion is new and at delete position or
2713 * an extension to either left or right
2715 es->undo_insert_count += strl;
2716 else {
2717 /* new insertion undo */
2718 es->undo_position = s;
2719 es->undo_insert_count = strl;
2720 /* new insertion makes old delete-buffer invalid */
2721 *es->undo_text = '\0';
2723 } else
2724 EDIT_EM_EmptyUndoBuffer(es);
2727 if (bufl)
2728 HeapFree(GetProcessHeap(), 0, buf);
2730 s += strl;
2732 /* If text has been deleted and we're right or center aligned then scroll rightward */
2733 if (es->style & (ES_RIGHT | ES_CENTER))
2735 INT delta = strl - abs(es->selection_end - es->selection_start);
2737 if (delta < 0 && es->x_offset)
2739 if (abs(delta) > es->x_offset)
2740 es->x_offset = 0;
2741 else
2742 es->x_offset += delta;
2746 EDIT_EM_SetSel(es, s, s, FALSE);
2747 es->flags |= EF_MODIFIED;
2748 if (send_update) es->flags |= EF_UPDATE;
2749 if (hrgn)
2751 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2752 DeleteObject(hrgn);
2754 else
2755 EDIT_UpdateText(es, NULL, TRUE);
2757 EDIT_EM_ScrollCaret(es);
2759 /* force scroll info update */
2760 EDIT_UpdateScrollInfo(es);
2763 if(send_update || (es->flags & EF_UPDATE))
2765 es->flags &= ~EF_UPDATE;
2766 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2768 EDIT_InvalidateUniscribeData(es);
2772 /*********************************************************************
2774 * EM_SETHANDLE
2776 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2779 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2781 if (!(es->style & ES_MULTILINE))
2782 return;
2784 if (!hloc) {
2785 WARN("called with NULL handle\n");
2786 return;
2789 EDIT_UnlockBuffer(es, TRUE);
2791 if(es->is_unicode)
2793 if(es->hloc32A)
2795 LocalFree(es->hloc32A);
2796 es->hloc32A = NULL;
2798 es->hloc32W = hloc;
2800 else
2802 INT countW, countA;
2803 HLOCAL hloc32W_new;
2804 WCHAR *textW;
2805 CHAR *textA;
2807 countA = LocalSize(hloc);
2808 textA = LocalLock(hloc);
2809 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2810 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2812 ERR("Could not allocate new unicode buffer\n");
2813 return;
2815 textW = LocalLock(hloc32W_new);
2816 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2817 LocalUnlock(hloc32W_new);
2818 LocalUnlock(hloc);
2820 if(es->hloc32W)
2821 LocalFree(es->hloc32W);
2823 es->hloc32W = hloc32W_new;
2824 es->hloc32A = hloc;
2827 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2829 es->flags |= EF_APP_HAS_HANDLE;
2830 EDIT_LockBuffer(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 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2869 * of the char's width as the margin, but this is not how Windows handles this.
2870 * For all other fonts Windows sets the margins to zero.
2872 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2873 * edit control is equal to or larger than a certain size.
2874 * Interestingly if one subtracts both the left and right margins from
2875 * this size one always seems to get an even number. The extents of
2876 * the (four character) string "'**'" match this quite closely, so
2877 * we'll use this until we come up with a better idea.
2879 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2881 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2882 SIZE sz;
2884 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2885 return sz.cx + left + right;
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 GetTextMetricsW(dc, &tm);
2900 /* The default margins are only non zero for TrueType or Vector fonts */
2901 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2902 int min_size;
2903 RECT rc;
2904 /* This must be calculated more exactly! But how? */
2905 default_left_margin = tm.tmAveCharWidth / 2;
2906 default_right_margin = tm.tmAveCharWidth / 2;
2907 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2908 GetClientRect(es->hwndSelf, &rc);
2909 if(rc.right - rc.left < min_size) {
2910 default_left_margin = es->left_margin;
2911 default_right_margin = es->right_margin;
2914 SelectObject(dc, old_font);
2915 ReleaseDC(es->hwndSelf, dc);
2918 if (action & EC_LEFTMARGIN) {
2919 es->format_rect.left -= es->left_margin;
2920 if (left != EC_USEFONTINFO)
2921 es->left_margin = left;
2922 else
2923 es->left_margin = default_left_margin;
2924 es->format_rect.left += es->left_margin;
2927 if (action & EC_RIGHTMARGIN) {
2928 es->format_rect.right += es->right_margin;
2929 if (right != EC_USEFONTINFO)
2930 es->right_margin = right;
2931 else
2932 es->right_margin = default_right_margin;
2933 es->format_rect.right -= es->right_margin;
2936 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2937 EDIT_AdjustFormatRect(es);
2938 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2941 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2945 /*********************************************************************
2947 * EM_SETPASSWORDCHAR
2950 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2952 LONG style;
2954 if (es->style & ES_MULTILINE)
2955 return;
2957 if (es->password_char == c)
2958 return;
2960 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2961 es->password_char = c;
2962 if (c) {
2963 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2964 es->style |= ES_PASSWORD;
2965 } else {
2966 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2967 es->style &= ~ES_PASSWORD;
2969 EDIT_InvalidateUniscribeData(es);
2970 EDIT_UpdateText(es, NULL, TRUE);
2974 /*********************************************************************
2976 * EM_SETTABSTOPS
2979 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2981 if (!(es->style & ES_MULTILINE))
2982 return FALSE;
2983 HeapFree(GetProcessHeap(), 0, es->tabs);
2984 es->tabs_count = count;
2985 if (!count)
2986 es->tabs = NULL;
2987 else {
2988 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2989 memcpy(es->tabs, tabs, count * sizeof(INT));
2991 EDIT_InvalidateUniscribeData(es);
2992 return TRUE;
2996 /*********************************************************************
2998 * EM_SETWORDBREAKPROC
3001 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3003 if (es->word_break_proc == wbp)
3004 return;
3006 es->word_break_proc = wbp;
3008 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3009 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3010 EDIT_UpdateText(es, NULL, TRUE);
3015 /*********************************************************************
3017 * EM_UNDO / WM_UNDO
3020 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3022 INT ulength;
3023 LPWSTR utext;
3025 /* As per MSDN spec, for a single-line edit control,
3026 the return value is always TRUE */
3027 if( es->style & ES_READONLY )
3028 return !(es->style & ES_MULTILINE);
3030 ulength = strlenW(es->undo_text);
3032 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3034 strcpyW(utext, es->undo_text);
3036 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3037 es->undo_insert_count, debugstr_w(utext));
3039 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3040 EDIT_EM_EmptyUndoBuffer(es);
3041 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3042 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3043 /* send the notification after the selection start and end are set */
3044 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3045 EDIT_EM_ScrollCaret(es);
3046 HeapFree(GetProcessHeap(), 0, utext);
3048 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3049 es->undo_insert_count, debugstr_w(es->undo_text));
3050 return TRUE;
3054 /* Helper function for WM_CHAR
3056 * According to an MSDN blog article titled "Just because you're a control
3057 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3058 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3059 * a dialog box or not.
3061 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3063 return (es->flags & EF_DIALOGMODE);
3067 /*********************************************************************
3069 * WM_PASTE
3072 static void EDIT_WM_Paste(EDITSTATE *es)
3074 HGLOBAL hsrc;
3075 LPWSTR src;
3077 /* Protect read-only edit control from modification */
3078 if(es->style & ES_READONLY)
3079 return;
3081 OpenClipboard(es->hwndSelf);
3082 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3083 src = GlobalLock(hsrc);
3084 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
3085 GlobalUnlock(hsrc);
3087 else if (es->style & ES_PASSWORD) {
3088 /* clear selected text in password edit box even with empty clipboard */
3089 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
3091 CloseClipboard();
3095 /*********************************************************************
3097 * WM_COPY
3100 static void EDIT_WM_Copy(EDITSTATE *es)
3102 INT s = min(es->selection_start, es->selection_end);
3103 INT e = max(es->selection_start, es->selection_end);
3104 HGLOBAL hdst;
3105 LPWSTR dst;
3106 DWORD len;
3108 if (e == s) return;
3110 len = e - s;
3111 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3112 dst = GlobalLock(hdst);
3113 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3114 dst[len] = 0; /* ensure 0 termination */
3115 TRACE("%s\n", debugstr_w(dst));
3116 GlobalUnlock(hdst);
3117 OpenClipboard(es->hwndSelf);
3118 EmptyClipboard();
3119 SetClipboardData(CF_UNICODETEXT, hdst);
3120 CloseClipboard();
3124 /*********************************************************************
3126 * WM_CLEAR
3129 static inline void EDIT_WM_Clear(EDITSTATE *es)
3131 /* Protect read-only edit control from modification */
3132 if(es->style & ES_READONLY)
3133 return;
3135 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
3139 /*********************************************************************
3141 * WM_CUT
3144 static inline void EDIT_WM_Cut(EDITSTATE *es)
3146 EDIT_WM_Copy(es);
3147 EDIT_WM_Clear(es);
3151 /*********************************************************************
3153 * WM_CHAR
3156 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3158 BOOL control;
3160 control = GetKeyState(VK_CONTROL) & 0x8000;
3162 switch (c) {
3163 case '\r':
3164 /* If it's not a multiline edit box, it would be ignored below.
3165 * For multiline edit without ES_WANTRETURN, we have to make a
3166 * special case.
3168 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3169 if (EDIT_IsInsideDialog(es))
3170 break;
3171 case '\n':
3172 if (es->style & ES_MULTILINE) {
3173 if (es->style & ES_READONLY) {
3174 EDIT_MoveHome(es, FALSE, FALSE);
3175 EDIT_MoveDown_ML(es, FALSE);
3176 } else {
3177 static const WCHAR cr_lfW[] = {'\r','\n',0};
3178 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3181 break;
3182 case '\t':
3183 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3185 static const WCHAR tabW[] = {'\t',0};
3186 if (EDIT_IsInsideDialog(es))
3187 break;
3188 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3190 break;
3191 case VK_BACK:
3192 if (!(es->style & ES_READONLY) && !control) {
3193 if (es->selection_start != es->selection_end)
3194 EDIT_WM_Clear(es);
3195 else {
3196 /* delete character left of caret */
3197 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3198 EDIT_MoveBackward(es, TRUE);
3199 EDIT_WM_Clear(es);
3202 break;
3203 case 0x03: /* ^C */
3204 if (!(es->style & ES_PASSWORD))
3205 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3206 break;
3207 case 0x16: /* ^V */
3208 if (!(es->style & ES_READONLY))
3209 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3210 break;
3211 case 0x18: /* ^X */
3212 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3213 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3214 break;
3215 case 0x1A: /* ^Z */
3216 if (!(es->style & ES_READONLY))
3217 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3218 break;
3220 default:
3221 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3222 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3223 break;
3225 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3226 WCHAR str[2];
3227 str[0] = c;
3228 str[1] = '\0';
3229 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3231 break;
3233 return 1;
3237 /*********************************************************************
3239 * WM_COMMAND
3242 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3244 if (code || control)
3245 return;
3247 switch (id) {
3248 case EM_UNDO:
3249 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3250 break;
3251 case WM_CUT:
3252 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3253 break;
3254 case WM_COPY:
3255 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3256 break;
3257 case WM_PASTE:
3258 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3259 break;
3260 case WM_CLEAR:
3261 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3262 break;
3263 case EM_SETSEL:
3264 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3265 EDIT_EM_ScrollCaret(es);
3266 break;
3267 default:
3268 ERR("unknown menu item, please report\n");
3269 break;
3274 /*********************************************************************
3276 * WM_CONTEXTMENU
3278 * Note: the resource files resource/sysres_??.rc cannot define a
3279 * single popup menu. Hence we use a (dummy) menubar
3280 * containing the single popup menu as its first item.
3282 * FIXME: the message identifiers have been chosen arbitrarily,
3283 * hence we use MF_BYPOSITION.
3284 * We might as well use the "real" values (anybody knows ?)
3285 * The menu definition is in resources/sysres_??.rc.
3286 * Once these are OK, we better use MF_BYCOMMAND here
3287 * (as we do in EDIT_WM_Command()).
3290 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3292 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3293 HMENU popup = GetSubMenu(menu, 0);
3294 UINT start = es->selection_start;
3295 UINT end = es->selection_end;
3297 ORDER_UINT(start, end);
3299 /* undo */
3300 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3301 /* cut */
3302 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3303 /* copy */
3304 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3305 /* paste */
3306 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3307 /* delete */
3308 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3309 /* select all */
3310 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3312 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3314 RECT rc;
3315 /* Windows places the menu at the edit's center in this case */
3316 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3317 x = rc.left + (rc.right - rc.left) / 2;
3318 y = rc.top + (rc.bottom - rc.top) / 2;
3321 if (!(es->flags & EF_FOCUSED))
3322 SetFocus(es->hwndSelf);
3324 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3325 DestroyMenu(menu);
3329 /*********************************************************************
3331 * WM_GETTEXT
3334 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3336 if(!count) return 0;
3338 if(unicode)
3340 lstrcpynW(dst, es->text, count);
3341 return strlenW(dst);
3343 else
3345 LPSTR textA = (LPSTR)dst;
3346 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3347 textA[count - 1] = 0; /* ensure 0 termination */
3348 return strlen(textA);
3352 /*********************************************************************
3354 * EDIT_CheckCombo
3357 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3359 HWND hLBox = es->hwndListBox;
3360 HWND hCombo;
3361 BOOL bDropped;
3362 int nEUI;
3364 if (!hLBox)
3365 return FALSE;
3367 hCombo = GetParent(es->hwndSelf);
3368 bDropped = TRUE;
3369 nEUI = 0;
3371 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3373 if (key == VK_UP || key == VK_DOWN)
3375 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3376 nEUI = 1;
3378 if (msg == WM_KEYDOWN || nEUI)
3379 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3382 switch (msg)
3384 case WM_KEYDOWN:
3385 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3387 /* make sure ComboLBox pops up */
3388 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3389 key = VK_F4;
3390 nEUI = 2;
3393 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3394 break;
3396 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3397 if (nEUI)
3398 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3399 else
3400 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3401 break;
3404 if(nEUI == 2)
3405 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3407 return TRUE;
3411 /*********************************************************************
3413 * WM_KEYDOWN
3415 * Handling of special keys that don't produce a WM_CHAR
3416 * (i.e. non-printable keys) & Backspace & Delete
3419 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3421 BOOL shift;
3422 BOOL control;
3424 if (GetKeyState(VK_MENU) & 0x8000)
3425 return 0;
3427 shift = GetKeyState(VK_SHIFT) & 0x8000;
3428 control = GetKeyState(VK_CONTROL) & 0x8000;
3430 switch (key) {
3431 case VK_F4:
3432 case VK_UP:
3433 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3434 break;
3436 /* fall through */
3437 case VK_LEFT:
3438 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3439 EDIT_MoveUp_ML(es, shift);
3440 else
3441 if (control)
3442 EDIT_MoveWordBackward(es, shift);
3443 else
3444 EDIT_MoveBackward(es, shift);
3445 break;
3446 case VK_DOWN:
3447 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3448 break;
3449 /* fall through */
3450 case VK_RIGHT:
3451 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3452 EDIT_MoveDown_ML(es, shift);
3453 else if (control)
3454 EDIT_MoveWordForward(es, shift);
3455 else
3456 EDIT_MoveForward(es, shift);
3457 break;
3458 case VK_HOME:
3459 EDIT_MoveHome(es, shift, control);
3460 break;
3461 case VK_END:
3462 EDIT_MoveEnd(es, shift, control);
3463 break;
3464 case VK_PRIOR:
3465 if (es->style & ES_MULTILINE)
3466 EDIT_MovePageUp_ML(es, shift);
3467 else
3468 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3469 break;
3470 case VK_NEXT:
3471 if (es->style & ES_MULTILINE)
3472 EDIT_MovePageDown_ML(es, shift);
3473 else
3474 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3475 break;
3476 case VK_DELETE:
3477 if (!(es->style & ES_READONLY) && !(shift && control)) {
3478 if (es->selection_start != es->selection_end) {
3479 if (shift)
3480 EDIT_WM_Cut(es);
3481 else
3482 EDIT_WM_Clear(es);
3483 } else {
3484 if (shift) {
3485 /* delete character left of caret */
3486 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3487 EDIT_MoveBackward(es, TRUE);
3488 EDIT_WM_Clear(es);
3489 } else if (control) {
3490 /* delete to end of line */
3491 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3492 EDIT_MoveEnd(es, TRUE, FALSE);
3493 EDIT_WM_Clear(es);
3494 } else {
3495 /* delete character right of caret */
3496 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3497 EDIT_MoveForward(es, TRUE);
3498 EDIT_WM_Clear(es);
3502 break;
3503 case VK_INSERT:
3504 if (shift) {
3505 if (!(es->style & ES_READONLY))
3506 EDIT_WM_Paste(es);
3507 } else if (control)
3508 EDIT_WM_Copy(es);
3509 break;
3510 case VK_RETURN:
3511 /* If the edit doesn't want the return send a message to the default object */
3512 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3514 DWORD dw;
3516 if (!EDIT_IsInsideDialog(es)) break;
3517 if (control) break;
3518 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3519 if (HIWORD(dw) == DC_HASDEFID)
3521 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3522 if (hwDefCtrl)
3524 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3525 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3529 break;
3530 case VK_ESCAPE:
3531 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3532 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3533 break;
3534 case VK_TAB:
3535 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3536 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3537 break;
3539 return TRUE;
3543 /*********************************************************************
3545 * WM_KILLFOCUS
3548 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3550 es->flags &= ~EF_FOCUSED;
3551 DestroyCaret();
3552 if(!(es->style & ES_NOHIDESEL))
3553 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3554 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3555 /* throw away left over scroll when we lose focus */
3556 es->wheelDeltaRemainder = 0;
3557 return 0;
3561 /*********************************************************************
3563 * WM_LBUTTONDBLCLK
3565 * The caret position has been set on the WM_LBUTTONDOWN message
3568 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3570 INT s;
3571 INT e = es->selection_end;
3572 INT l;
3573 INT li;
3574 INT ll;
3576 es->bCaptureState = TRUE;
3577 SetCapture(es->hwndSelf);
3579 l = EDIT_EM_LineFromChar(es, e);
3580 li = EDIT_EM_LineIndex(es, l);
3581 ll = EDIT_EM_LineLength(es, e);
3582 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3583 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3584 EDIT_EM_SetSel(es, s, e, FALSE);
3585 EDIT_EM_ScrollCaret(es);
3586 es->region_posx = es->region_posy = 0;
3587 SetTimer(es->hwndSelf, 0, 100, NULL);
3588 return 0;
3592 /*********************************************************************
3594 * WM_LBUTTONDOWN
3597 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3599 INT e;
3600 BOOL after_wrap;
3602 es->bCaptureState = TRUE;
3603 SetCapture(es->hwndSelf);
3604 EDIT_ConfinePoint(es, &x, &y);
3605 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3606 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3607 EDIT_EM_ScrollCaret(es);
3608 es->region_posx = es->region_posy = 0;
3609 SetTimer(es->hwndSelf, 0, 100, NULL);
3611 if (!(es->flags & EF_FOCUSED))
3612 SetFocus(es->hwndSelf);
3614 return 0;
3618 /*********************************************************************
3620 * WM_LBUTTONUP
3623 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3625 if (es->bCaptureState) {
3626 KillTimer(es->hwndSelf, 0);
3627 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3629 es->bCaptureState = FALSE;
3630 return 0;
3634 /*********************************************************************
3636 * WM_MBUTTONDOWN
3639 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3641 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3642 return 0;
3646 /*********************************************************************
3648 * WM_MOUSEMOVE
3651 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3653 INT e;
3654 BOOL after_wrap;
3655 INT prex, prey;
3657 /* If the mouse has been captured by process other than the edit control itself,
3658 * the windows edit controls will not select the strings with mouse move.
3660 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3661 return 0;
3664 * FIXME: gotta do some scrolling if outside client
3665 * area. Maybe reset the timer ?
3667 prex = x; prey = y;
3668 EDIT_ConfinePoint(es, &x, &y);
3669 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3670 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3671 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3672 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3673 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3674 return 0;
3678 /*********************************************************************
3680 * WM_PAINT
3683 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3685 PAINTSTRUCT ps;
3686 INT i;
3687 HDC dc;
3688 HFONT old_font = 0;
3689 RECT rc;
3690 RECT rcClient;
3691 RECT rcLine;
3692 RECT rcRgn;
3693 HBRUSH brush;
3694 HBRUSH old_brush;
3695 INT bw, bh;
3696 BOOL rev = es->bEnableState &&
3697 ((es->flags & EF_FOCUSED) ||
3698 (es->style & ES_NOHIDESEL));
3699 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3701 /* The dc we use for calculating may not be the one we paint into.
3702 This is the safest action. */
3703 EDIT_InvalidateUniscribeData(es);
3704 GetClientRect(es->hwndSelf, &rcClient);
3706 /* get the background brush */
3707 brush = EDIT_NotifyCtlColor(es, dc);
3709 /* paint the border and the background */
3710 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3712 if(es->style & WS_BORDER) {
3713 bw = GetSystemMetrics(SM_CXBORDER);
3714 bh = GetSystemMetrics(SM_CYBORDER);
3715 rc = rcClient;
3716 if(es->style & ES_MULTILINE) {
3717 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3718 if(es->style & WS_VSCROLL) rc.right+=bw;
3721 /* Draw the frame. Same code as in nonclient.c */
3722 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3723 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3724 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3725 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3726 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3727 SelectObject(dc, old_brush);
3729 /* Keep the border clean */
3730 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3731 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3734 GetClipBox(dc, &rc);
3735 FillRect(dc, &rc, brush);
3737 IntersectClipRect(dc, es->format_rect.left,
3738 es->format_rect.top,
3739 es->format_rect.right,
3740 es->format_rect.bottom);
3741 if (es->style & ES_MULTILINE) {
3742 rc = rcClient;
3743 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3745 if (es->font)
3746 old_font = SelectObject(dc, es->font);
3748 if (!es->bEnableState)
3749 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3750 GetClipBox(dc, &rcRgn);
3751 if (es->style & ES_MULTILINE) {
3752 INT vlc = get_vertical_line_count(es);
3753 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3754 EDIT_UpdateUniscribeData(es, dc, i);
3755 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3756 if (IntersectRect(&rc, &rcRgn, &rcLine))
3757 EDIT_PaintLine(es, dc, i, rev);
3759 } else {
3760 EDIT_UpdateUniscribeData(es, dc, 0);
3761 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3762 if (IntersectRect(&rc, &rcRgn, &rcLine))
3763 EDIT_PaintLine(es, dc, 0, rev);
3765 if (es->font)
3766 SelectObject(dc, old_font);
3768 if (!hdc)
3769 EndPaint(es->hwndSelf, &ps);
3773 /*********************************************************************
3775 * WM_SETFOCUS
3778 static void EDIT_WM_SetFocus(EDITSTATE *es)
3780 es->flags |= EF_FOCUSED;
3782 if (!(es->style & ES_NOHIDESEL))
3783 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3785 /* single line edit updates itself */
3786 if (!(es->style & ES_MULTILINE))
3788 HDC hdc = GetDC(es->hwndSelf);
3789 EDIT_WM_Paint(es, hdc);
3790 ReleaseDC(es->hwndSelf, hdc);
3793 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3794 EDIT_SetCaretPos(es, es->selection_end,
3795 es->flags & EF_AFTER_WRAP);
3796 ShowCaret(es->hwndSelf);
3797 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3801 /*********************************************************************
3803 * WM_SETFONT
3805 * With Win95 look the margins are set to default font value unless
3806 * the system font (font == 0) is being set, in which case they are left
3807 * unchanged.
3810 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3812 TEXTMETRICW tm;
3813 HDC dc;
3814 HFONT old_font = 0;
3815 RECT clientRect;
3817 es->font = font;
3818 EDIT_InvalidateUniscribeData(es);
3819 dc = GetDC(es->hwndSelf);
3820 if (font)
3821 old_font = SelectObject(dc, font);
3822 GetTextMetricsW(dc, &tm);
3823 es->line_height = tm.tmHeight;
3824 es->char_width = tm.tmAveCharWidth;
3825 if (font)
3826 SelectObject(dc, old_font);
3827 ReleaseDC(es->hwndSelf, dc);
3829 /* Reset the format rect and the margins */
3830 GetClientRect(es->hwndSelf, &clientRect);
3831 EDIT_SetRectNP(es, &clientRect);
3832 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3833 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3835 if (es->style & ES_MULTILINE)
3836 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3837 else
3838 EDIT_CalcLineWidth_SL(es);
3840 if (redraw)
3841 EDIT_UpdateText(es, NULL, TRUE);
3842 if (es->flags & EF_FOCUSED) {
3843 DestroyCaret();
3844 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3845 EDIT_SetCaretPos(es, es->selection_end,
3846 es->flags & EF_AFTER_WRAP);
3847 ShowCaret(es->hwndSelf);
3852 /*********************************************************************
3854 * WM_SETTEXT
3856 * NOTES
3857 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3858 * The modified flag is reset. No notifications are sent.
3860 * For single-line controls, reception of WM_SETTEXT triggers:
3861 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3864 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3866 LPWSTR textW = NULL;
3867 if (!unicode && text)
3869 LPCSTR textA = (LPCSTR)text;
3870 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3871 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3872 if (textW)
3873 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3874 text = textW;
3877 if (es->flags & EF_UPDATE)
3878 /* fixed this bug once; complain if we see it about to happen again. */
3879 ERR("SetSel may generate UPDATE message whose handler may reset "
3880 "selection.\n");
3882 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3883 if (text)
3885 TRACE("%s\n", debugstr_w(text));
3886 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
3887 if(!unicode)
3888 HeapFree(GetProcessHeap(), 0, textW);
3890 else
3892 TRACE("<NULL>\n");
3893 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
3895 es->x_offset = 0;
3896 es->flags &= ~EF_MODIFIED;
3897 EDIT_EM_SetSel(es, 0, 0, FALSE);
3899 /* Send the notification after the selection start and end have been set
3900 * edit control doesn't send notification on WM_SETTEXT
3901 * if it is multiline, or it is part of combobox
3903 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3905 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3906 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3908 EDIT_EM_ScrollCaret(es);
3909 EDIT_UpdateScrollInfo(es);
3910 EDIT_InvalidateUniscribeData(es);
3914 /*********************************************************************
3916 * WM_SIZE
3919 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
3921 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3922 RECT rc;
3923 TRACE("width = %d, height = %d\n", width, height);
3924 SetRect(&rc, 0, 0, width, height);
3925 EDIT_SetRectNP(es, &rc);
3926 EDIT_UpdateText(es, NULL, TRUE);
3931 /*********************************************************************
3933 * WM_STYLECHANGED
3935 * This message is sent by SetWindowLong on having changed either the Style
3936 * or the extended style.
3938 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3940 * See also EDIT_WM_NCCreate
3942 * It appears that the Windows version of the edit control allows the style
3943 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3944 * style variable which will generally be different. In this function we
3945 * update the internal style based on what changed in the externally visible
3946 * style.
3948 * Much of this content as based upon the MSDN, especially:
3949 * Platform SDK Documentation -> User Interface Services ->
3950 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3951 * Edit Control Styles
3953 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3955 if (GWL_STYLE == which) {
3956 DWORD style_change_mask;
3957 DWORD new_style;
3958 /* Only a subset of changes can be applied after the control
3959 * has been created.
3961 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3962 ES_NUMBER;
3963 if (es->style & ES_MULTILINE)
3964 style_change_mask |= ES_WANTRETURN;
3966 new_style = style->styleNew & style_change_mask;
3968 /* Number overrides lowercase overrides uppercase (at least it
3969 * does in Win95). However I'll bet that ES_NUMBER would be
3970 * invalid under Win 3.1.
3972 if (new_style & ES_NUMBER) {
3973 ; /* do not override the ES_NUMBER */
3974 } else if (new_style & ES_LOWERCASE) {
3975 new_style &= ~ES_UPPERCASE;
3978 es->style = (es->style & ~style_change_mask) | new_style;
3979 } else if (GWL_EXSTYLE == which) {
3980 ; /* FIXME - what is needed here */
3981 } else {
3982 WARN ("Invalid style change %ld\n",which);
3985 return 0;
3988 /*********************************************************************
3990 * WM_SYSKEYDOWN
3993 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
3995 if ((key == VK_BACK) && (key_data & 0x2000)) {
3996 if (EDIT_EM_CanUndo(es))
3997 EDIT_EM_Undo(es);
3998 return 0;
3999 } else if (key == VK_UP || key == VK_DOWN) {
4000 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4001 return 0;
4003 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4007 /*********************************************************************
4009 * WM_TIMER
4012 static void EDIT_WM_Timer(EDITSTATE *es)
4014 if (es->region_posx < 0) {
4015 EDIT_MoveBackward(es, TRUE);
4016 } else if (es->region_posx > 0) {
4017 EDIT_MoveForward(es, TRUE);
4020 * FIXME: gotta do some vertical scrolling here, like
4021 * EDIT_EM_LineScroll(hwnd, 0, 1);
4025 /*********************************************************************
4027 * WM_HSCROLL
4030 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4032 INT dx;
4033 INT fw;
4035 if (!(es->style & ES_MULTILINE))
4036 return 0;
4038 if (!(es->style & ES_AUTOHSCROLL))
4039 return 0;
4041 dx = 0;
4042 fw = es->format_rect.right - es->format_rect.left;
4043 switch (action) {
4044 case SB_LINELEFT:
4045 TRACE("SB_LINELEFT\n");
4046 if (es->x_offset)
4047 dx = -es->char_width;
4048 break;
4049 case SB_LINERIGHT:
4050 TRACE("SB_LINERIGHT\n");
4051 if (es->x_offset < es->text_width)
4052 dx = es->char_width;
4053 break;
4054 case SB_PAGELEFT:
4055 TRACE("SB_PAGELEFT\n");
4056 if (es->x_offset)
4057 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4058 break;
4059 case SB_PAGERIGHT:
4060 TRACE("SB_PAGERIGHT\n");
4061 if (es->x_offset < es->text_width)
4062 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4063 break;
4064 case SB_LEFT:
4065 TRACE("SB_LEFT\n");
4066 if (es->x_offset)
4067 dx = -es->x_offset;
4068 break;
4069 case SB_RIGHT:
4070 TRACE("SB_RIGHT\n");
4071 if (es->x_offset < es->text_width)
4072 dx = es->text_width - es->x_offset;
4073 break;
4074 case SB_THUMBTRACK:
4075 TRACE("SB_THUMBTRACK %d\n", pos);
4076 es->flags |= EF_HSCROLL_TRACK;
4077 if(es->style & WS_HSCROLL)
4078 dx = pos - es->x_offset;
4079 else
4081 INT fw, new_x;
4082 /* Sanity check */
4083 if(pos < 0 || pos > 100) return 0;
4084 /* Assume default scroll range 0-100 */
4085 fw = es->format_rect.right - es->format_rect.left;
4086 new_x = pos * (es->text_width - fw) / 100;
4087 dx = es->text_width ? (new_x - es->x_offset) : 0;
4089 break;
4090 case SB_THUMBPOSITION:
4091 TRACE("SB_THUMBPOSITION %d\n", pos);
4092 es->flags &= ~EF_HSCROLL_TRACK;
4093 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4094 dx = pos - es->x_offset;
4095 else
4097 INT fw, new_x;
4098 /* Sanity check */
4099 if(pos < 0 || pos > 100) return 0;
4100 /* Assume default scroll range 0-100 */
4101 fw = es->format_rect.right - es->format_rect.left;
4102 new_x = pos * (es->text_width - fw) / 100;
4103 dx = es->text_width ? (new_x - es->x_offset) : 0;
4105 if (!dx) {
4106 /* force scroll info update */
4107 EDIT_UpdateScrollInfo(es);
4108 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4110 break;
4111 case SB_ENDSCROLL:
4112 TRACE("SB_ENDSCROLL\n");
4113 break;
4115 * FIXME : the next two are undocumented !
4116 * Are we doing the right thing ?
4117 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4118 * although it's also a regular control message.
4120 case EM_GETTHUMB: /* this one is used by NT notepad */
4122 LRESULT ret;
4123 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4124 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4125 else
4127 /* Assume default scroll range 0-100 */
4128 INT fw = es->format_rect.right - es->format_rect.left;
4129 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4131 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4132 return ret;
4134 case EM_LINESCROLL:
4135 TRACE("EM_LINESCROLL16\n");
4136 dx = pos;
4137 break;
4139 default:
4140 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4141 action, action);
4142 return 0;
4144 if (dx)
4146 INT fw = es->format_rect.right - es->format_rect.left;
4147 /* check if we are going to move too far */
4148 if(es->x_offset + dx + fw > es->text_width)
4149 dx = es->text_width - fw - es->x_offset;
4150 if(dx)
4151 EDIT_EM_LineScroll_internal(es, dx, 0);
4153 return 0;
4157 /*********************************************************************
4159 * WM_VSCROLL
4162 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4164 INT dy;
4166 if (!(es->style & ES_MULTILINE))
4167 return 0;
4169 if (!(es->style & ES_AUTOVSCROLL))
4170 return 0;
4172 dy = 0;
4173 switch (action) {
4174 case SB_LINEUP:
4175 case SB_LINEDOWN:
4176 case SB_PAGEUP:
4177 case SB_PAGEDOWN:
4178 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4179 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4180 (action == SB_PAGEUP ? "SB_PAGEUP" :
4181 "SB_PAGEDOWN"))));
4182 EDIT_EM_Scroll(es, action);
4183 return 0;
4184 case SB_TOP:
4185 TRACE("SB_TOP\n");
4186 dy = -es->y_offset;
4187 break;
4188 case SB_BOTTOM:
4189 TRACE("SB_BOTTOM\n");
4190 dy = es->line_count - 1 - es->y_offset;
4191 break;
4192 case SB_THUMBTRACK:
4193 TRACE("SB_THUMBTRACK %d\n", pos);
4194 es->flags |= EF_VSCROLL_TRACK;
4195 if(es->style & WS_VSCROLL)
4196 dy = pos - es->y_offset;
4197 else
4199 /* Assume default scroll range 0-100 */
4200 INT vlc, new_y;
4201 /* Sanity check */
4202 if(pos < 0 || pos > 100) return 0;
4203 vlc = get_vertical_line_count(es);
4204 new_y = pos * (es->line_count - vlc) / 100;
4205 dy = es->line_count ? (new_y - es->y_offset) : 0;
4206 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4207 es->line_count, es->y_offset, pos, dy);
4209 break;
4210 case SB_THUMBPOSITION:
4211 TRACE("SB_THUMBPOSITION %d\n", pos);
4212 es->flags &= ~EF_VSCROLL_TRACK;
4213 if(es->style & WS_VSCROLL)
4214 dy = pos - es->y_offset;
4215 else
4217 /* Assume default scroll range 0-100 */
4218 INT vlc, new_y;
4219 /* Sanity check */
4220 if(pos < 0 || pos > 100) return 0;
4221 vlc = get_vertical_line_count(es);
4222 new_y = pos * (es->line_count - vlc) / 100;
4223 dy = es->line_count ? (new_y - es->y_offset) : 0;
4224 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4225 es->line_count, es->y_offset, pos, dy);
4227 if (!dy)
4229 /* force scroll info update */
4230 EDIT_UpdateScrollInfo(es);
4231 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4233 break;
4234 case SB_ENDSCROLL:
4235 TRACE("SB_ENDSCROLL\n");
4236 break;
4238 * FIXME : the next two are undocumented !
4239 * Are we doing the right thing ?
4240 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4241 * although it's also a regular control message.
4243 case EM_GETTHUMB: /* this one is used by NT notepad */
4245 LRESULT ret;
4246 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4247 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4248 else
4250 /* Assume default scroll range 0-100 */
4251 INT vlc = get_vertical_line_count(es);
4252 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4254 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4255 return ret;
4257 case EM_LINESCROLL:
4258 TRACE("EM_LINESCROLL %d\n", pos);
4259 dy = pos;
4260 break;
4262 default:
4263 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4264 action, action);
4265 return 0;
4267 if (dy)
4268 EDIT_EM_LineScroll(es, 0, dy);
4269 return 0;
4272 /*********************************************************************
4274 * EM_GETTHUMB
4276 * FIXME: is this right ? (or should it be only VSCROLL)
4277 * (and maybe only for edit controls that really have their
4278 * own scrollbars) (and maybe only for multiline controls ?)
4279 * All in all: very poorly documented
4282 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4284 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4285 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4289 /********************************************************************
4291 * The Following code is to handle inline editing from IMEs
4294 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4296 LONG buflen;
4297 LPWSTR lpCompStr = NULL;
4298 LPSTR lpCompStrAttr = NULL;
4299 DWORD dwBufLenAttr;
4301 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4303 if (buflen < 0)
4305 return;
4308 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
4309 if (!lpCompStr)
4311 ERR("Unable to allocate IME CompositionString\n");
4312 return;
4315 if (buflen)
4316 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4317 lpCompStr[buflen/sizeof(WCHAR)] = 0;
4319 if (CompFlag & GCS_COMPATTR)
4322 * We do not use the attributes yet. it would tell us what characters
4323 * are in transition and which are converted or decided upon
4325 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4326 if (dwBufLenAttr)
4328 dwBufLenAttr ++;
4329 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4330 if (!lpCompStrAttr)
4332 ERR("Unable to allocate IME Attribute String\n");
4333 HeapFree(GetProcessHeap(),0,lpCompStr);
4334 return;
4336 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4337 dwBufLenAttr);
4338 lpCompStrAttr[dwBufLenAttr] = 0;
4340 else
4341 lpCompStrAttr = NULL;
4344 /* check for change in composition start */
4345 if (es->selection_end < es->composition_start)
4346 es->composition_start = es->selection_end;
4348 /* replace existing selection string */
4349 es->selection_start = es->composition_start;
4351 if (es->composition_len > 0)
4352 es->selection_end = es->composition_start + es->composition_len;
4353 else
4354 es->selection_end = es->selection_start;
4356 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
4357 es->composition_len = abs(es->composition_start - es->selection_end);
4359 es->selection_start = es->composition_start;
4360 es->selection_end = es->selection_start + es->composition_len;
4362 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4363 HeapFree(GetProcessHeap(),0,lpCompStr);
4366 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4368 LONG buflen;
4369 LPWSTR lpResultStr;
4371 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4372 if (buflen <= 0)
4374 return;
4377 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
4378 if (!lpResultStr)
4380 ERR("Unable to alloc buffer for IME string\n");
4381 return;
4384 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4385 lpResultStr[buflen/sizeof(WCHAR)] = 0;
4387 /* check for change in composition start */
4388 if (es->selection_end < es->composition_start)
4389 es->composition_start = es->selection_end;
4391 es->selection_start = es->composition_start;
4392 es->selection_end = es->composition_start + es->composition_len;
4393 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
4394 es->composition_start = es->selection_end;
4395 es->composition_len = 0;
4397 HeapFree(GetProcessHeap(),0,lpResultStr);
4400 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4402 HIMC hIMC;
4403 int cursor;
4405 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4407 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4408 es->composition_start = es->selection_end;
4411 hIMC = ImmGetContext(hwnd);
4412 if (!hIMC)
4413 return;
4415 if (CompFlag & GCS_RESULTSTR)
4416 EDIT_GetResultStr(hIMC, es);
4417 if (CompFlag & GCS_COMPSTR)
4418 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4419 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4420 ImmReleaseContext(hwnd, hIMC);
4421 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4425 /*********************************************************************
4427 * WM_NCCREATE
4429 * See also EDIT_WM_StyleChanged
4431 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4433 EDITSTATE *es;
4434 UINT alloc_size;
4436 TRACE("Creating %s edit control, style = %08x\n",
4437 unicode ? "Unicode" : "ANSI", lpcs->style);
4439 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4440 return FALSE;
4441 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4444 * Note: since the EDITSTATE has not been fully initialized yet,
4445 * we can't use any API calls that may send
4446 * WM_XXX messages before WM_NCCREATE is completed.
4449 es->is_unicode = unicode;
4450 es->style = lpcs->style;
4452 es->bEnableState = !(es->style & WS_DISABLED);
4454 es->hwndSelf = hwnd;
4455 /* Save parent, which will be notified by EN_* messages */
4456 es->hwndParent = lpcs->hwndParent;
4458 if (es->style & ES_COMBO)
4459 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4461 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4462 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4464 /* Number overrides lowercase overrides uppercase (at least it
4465 * does in Win95). However I'll bet that ES_NUMBER would be
4466 * invalid under Win 3.1.
4468 if (es->style & ES_NUMBER) {
4469 ; /* do not override the ES_NUMBER */
4470 } else if (es->style & ES_LOWERCASE) {
4471 es->style &= ~ES_UPPERCASE;
4473 if (es->style & ES_MULTILINE) {
4474 es->buffer_limit = BUFLIMIT_INITIAL;
4475 if (es->style & WS_VSCROLL)
4476 es->style |= ES_AUTOVSCROLL;
4477 if (es->style & WS_HSCROLL)
4478 es->style |= ES_AUTOHSCROLL;
4479 es->style &= ~ES_PASSWORD;
4480 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4481 /* Confirmed - RIGHT overrides CENTER */
4482 if (es->style & ES_RIGHT)
4483 es->style &= ~ES_CENTER;
4484 es->style &= ~WS_HSCROLL;
4485 es->style &= ~ES_AUTOHSCROLL;
4487 } else {
4488 es->buffer_limit = BUFLIMIT_INITIAL;
4489 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4490 es->style &= ~ES_CENTER;
4491 es->style &= ~WS_HSCROLL;
4492 es->style &= ~WS_VSCROLL;
4493 if (es->style & ES_PASSWORD)
4494 es->password_char = '*';
4497 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4498 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4499 goto cleanup;
4500 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4502 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4503 goto cleanup;
4504 es->undo_buffer_size = es->buffer_size;
4506 if (es->style & ES_MULTILINE)
4507 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4508 goto cleanup;
4509 es->line_count = 1;
4512 * In Win95 look and feel, the WS_BORDER style is replaced by the
4513 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4514 * control a nonclient area so we don't need to draw the border.
4515 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4516 * a nonclient area and we should handle painting the border ourselves.
4518 * When making modifications please ensure that the code still works
4519 * for edit controls created directly with style 0x50800000, exStyle 0
4520 * (which should have a single pixel border)
4522 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4523 es->style &= ~WS_BORDER;
4524 else if (es->style & WS_BORDER)
4525 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4527 return TRUE;
4529 cleanup:
4530 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4531 EDIT_InvalidateUniscribeData(es);
4532 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4533 HeapFree(GetProcessHeap(), 0, es->undo_text);
4534 if (es->hloc32W) LocalFree(es->hloc32W);
4535 HeapFree(GetProcessHeap(), 0, es->logAttr);
4536 HeapFree(GetProcessHeap(), 0, es);
4537 return FALSE;
4541 /*********************************************************************
4543 * WM_CREATE
4546 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4548 RECT clientRect;
4550 TRACE("%s\n", debugstr_w(name));
4552 * To initialize some final structure members, we call some helper
4553 * functions. However, since the EDITSTATE is not consistent (i.e.
4554 * not fully initialized), we should be very careful which
4555 * functions can be called, and in what order.
4557 EDIT_WM_SetFont(es, 0, FALSE);
4558 EDIT_EM_EmptyUndoBuffer(es);
4560 /* We need to calculate the format rect
4561 (applications may send EM_SETMARGINS before the control gets visible) */
4562 GetClientRect(es->hwndSelf, &clientRect);
4563 EDIT_SetRectNP(es, &clientRect);
4565 if (name && *name) {
4566 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4567 /* if we insert text to the editline, the text scrolls out
4568 * of the window, as the caret is placed after the insert
4569 * pos normally; thus we reset es->selection... to 0 and
4570 * update caret
4572 es->selection_start = es->selection_end = 0;
4573 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4574 * Messages are only to be sent when the USER does something to
4575 * change the contents. So I am removing this EN_CHANGE
4577 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4579 EDIT_EM_ScrollCaret(es);
4581 /* force scroll info update */
4582 EDIT_UpdateScrollInfo(es);
4583 /* The rule seems to return 1 here for success */
4584 /* Power Builder masked edit controls will crash */
4585 /* if not. */
4586 /* FIXME: is that in all cases so ? */
4587 return 1;
4591 /*********************************************************************
4593 * WM_NCDESTROY
4596 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4598 LINEDEF *pc, *pp;
4600 if (es->hloc32W) {
4601 LocalFree(es->hloc32W);
4603 if (es->hloc32A) {
4604 LocalFree(es->hloc32A);
4606 EDIT_InvalidateUniscribeData(es);
4607 pc = es->first_line_def;
4608 while (pc)
4610 pp = pc->next;
4611 HeapFree(GetProcessHeap(), 0, pc);
4612 pc = pp;
4615 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4616 HeapFree(GetProcessHeap(), 0, es->undo_text);
4617 HeapFree(GetProcessHeap(), 0, es);
4619 return 0;
4623 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4625 if(unicode)
4626 return DefWindowProcW(hwnd, msg, wParam, lParam);
4627 else
4628 return DefWindowProcA(hwnd, msg, wParam, lParam);
4631 /*********************************************************************
4633 * EditWndProc_common
4635 * The messages are in the order of the actual integer values
4636 * (which can be found in include/windows.h)
4638 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4640 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4641 LRESULT result = 0;
4643 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4645 if (!es && msg != WM_NCCREATE)
4646 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4648 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4650 switch (msg) {
4651 case EM_GETSEL:
4652 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4653 break;
4655 case EM_SETSEL:
4656 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4657 EDIT_EM_ScrollCaret(es);
4658 result = 1;
4659 break;
4661 case EM_GETRECT:
4662 if (lParam)
4663 CopyRect((LPRECT)lParam, &es->format_rect);
4664 break;
4666 case EM_SETRECT:
4667 if ((es->style & ES_MULTILINE) && lParam) {
4668 EDIT_SetRectNP(es, (LPRECT)lParam);
4669 EDIT_UpdateText(es, NULL, TRUE);
4671 break;
4673 case EM_SETRECTNP:
4674 if ((es->style & ES_MULTILINE) && lParam)
4675 EDIT_SetRectNP(es, (LPRECT)lParam);
4676 break;
4678 case EM_SCROLL:
4679 result = EDIT_EM_Scroll(es, (INT)wParam);
4680 break;
4682 case EM_LINESCROLL:
4683 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4684 break;
4686 case EM_SCROLLCARET:
4687 EDIT_EM_ScrollCaret(es);
4688 result = 1;
4689 break;
4691 case EM_GETMODIFY:
4692 result = ((es->flags & EF_MODIFIED) != 0);
4693 break;
4695 case EM_SETMODIFY:
4696 if (wParam)
4697 es->flags |= EF_MODIFIED;
4698 else
4699 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4700 break;
4702 case EM_GETLINECOUNT:
4703 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4704 break;
4706 case EM_LINEINDEX:
4707 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4708 break;
4710 case EM_SETHANDLE:
4711 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4712 break;
4714 case EM_GETHANDLE:
4715 result = (LRESULT)EDIT_EM_GetHandle(es);
4716 break;
4718 case EM_GETTHUMB:
4719 result = EDIT_EM_GetThumb(es);
4720 break;
4722 /* these messages missing from specs */
4723 case 0x00bf:
4724 case 0x00c0:
4725 case 0x00c3:
4726 case 0x00ca:
4727 FIXME("undocumented message 0x%x, please report\n", msg);
4728 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4729 break;
4731 case EM_LINELENGTH:
4732 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4733 break;
4735 case EM_REPLACESEL:
4737 LPWSTR textW;
4739 if(unicode)
4740 textW = (LPWSTR)lParam;
4741 else
4743 LPSTR textA = (LPSTR)lParam;
4744 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4745 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4746 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4749 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
4750 result = 1;
4752 if(!unicode)
4753 HeapFree(GetProcessHeap(), 0, textW);
4754 break;
4757 case EM_GETLINE:
4758 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4759 break;
4761 case EM_SETLIMITTEXT:
4762 EDIT_EM_SetLimitText(es, wParam);
4763 break;
4765 case EM_CANUNDO:
4766 result = (LRESULT)EDIT_EM_CanUndo(es);
4767 break;
4769 case EM_UNDO:
4770 case WM_UNDO:
4771 result = (LRESULT)EDIT_EM_Undo(es);
4772 break;
4774 case EM_FMTLINES:
4775 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4776 break;
4778 case EM_LINEFROMCHAR:
4779 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4780 break;
4782 case EM_SETTABSTOPS:
4783 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4784 break;
4786 case EM_SETPASSWORDCHAR:
4788 WCHAR charW = 0;
4790 if(unicode)
4791 charW = (WCHAR)wParam;
4792 else
4794 CHAR charA = wParam;
4795 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4798 EDIT_EM_SetPasswordChar(es, charW);
4799 break;
4802 case EM_EMPTYUNDOBUFFER:
4803 EDIT_EM_EmptyUndoBuffer(es);
4804 break;
4806 case EM_GETFIRSTVISIBLELINE:
4807 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4808 break;
4810 case EM_SETREADONLY:
4812 DWORD old_style = es->style;
4814 if (wParam) {
4815 SetWindowLongW( hwnd, GWL_STYLE,
4816 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4817 es->style |= ES_READONLY;
4818 } else {
4819 SetWindowLongW( hwnd, GWL_STYLE,
4820 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4821 es->style &= ~ES_READONLY;
4824 if (old_style ^ es->style)
4825 InvalidateRect(es->hwndSelf, NULL, TRUE);
4827 result = 1;
4828 break;
4831 case EM_SETWORDBREAKPROC:
4832 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4833 break;
4835 case EM_GETWORDBREAKPROC:
4836 result = (LRESULT)es->word_break_proc;
4837 break;
4839 case EM_GETPASSWORDCHAR:
4841 if(unicode)
4842 result = es->password_char;
4843 else
4845 WCHAR charW = es->password_char;
4846 CHAR charA = 0;
4847 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4848 result = charA;
4850 break;
4853 case EM_SETMARGINS:
4854 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4855 break;
4857 case EM_GETMARGINS:
4858 result = MAKELONG(es->left_margin, es->right_margin);
4859 break;
4861 case EM_GETLIMITTEXT:
4862 result = es->buffer_limit;
4863 break;
4865 case EM_POSFROMCHAR:
4866 if ((INT)wParam >= get_text_length(es)) result = -1;
4867 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4868 break;
4870 case EM_CHARFROMPOS:
4871 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4872 break;
4874 /* End of the EM_ messages which were in numerical order; what order
4875 * are these in? vaguely alphabetical?
4878 case WM_NCCREATE:
4879 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4880 break;
4882 case WM_NCDESTROY:
4883 result = EDIT_WM_NCDestroy(es);
4884 es = NULL;
4885 break;
4887 case WM_GETDLGCODE:
4888 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4890 if (es->style & ES_MULTILINE)
4891 result |= DLGC_WANTALLKEYS;
4893 if (lParam)
4895 es->flags|=EF_DIALOGMODE;
4897 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4899 int vk = (int)((LPMSG)lParam)->wParam;
4901 if (es->hwndListBox)
4903 if (vk == VK_RETURN || vk == VK_ESCAPE)
4904 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4905 result |= DLGC_WANTMESSAGE;
4909 break;
4911 case WM_IME_CHAR:
4912 if (!unicode)
4914 WCHAR charW;
4915 CHAR strng[2];
4917 strng[0] = wParam >> 8;
4918 strng[1] = wParam & 0xff;
4919 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4920 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4921 result = EDIT_WM_Char(es, charW);
4922 break;
4924 /* fall through */
4925 case WM_CHAR:
4927 WCHAR charW;
4929 if(unicode)
4930 charW = wParam;
4931 else
4933 CHAR charA = wParam;
4934 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4937 if (es->hwndListBox)
4939 if (charW == VK_RETURN || charW == VK_ESCAPE)
4941 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4942 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4943 break;
4946 result = EDIT_WM_Char(es, charW);
4947 break;
4950 case WM_UNICHAR:
4951 if (unicode)
4953 if (wParam == UNICODE_NOCHAR) return TRUE;
4954 if (wParam <= 0x000fffff)
4956 if(wParam > 0xffff) /* convert to surrogates */
4958 wParam -= 0x10000;
4959 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4960 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4962 else EDIT_WM_Char(es, wParam);
4964 return 0;
4966 break;
4968 case WM_CLEAR:
4969 EDIT_WM_Clear(es);
4970 break;
4972 case WM_COMMAND:
4973 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
4974 break;
4976 case WM_CONTEXTMENU:
4977 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4978 break;
4980 case WM_COPY:
4981 EDIT_WM_Copy(es);
4982 break;
4984 case WM_CREATE:
4985 if(unicode)
4986 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4987 else
4989 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4990 LPWSTR nameW = NULL;
4991 if(nameA)
4993 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
4994 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4995 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
4997 result = EDIT_WM_Create(es, nameW);
4998 HeapFree(GetProcessHeap(), 0, nameW);
5000 break;
5002 case WM_CUT:
5003 EDIT_WM_Cut(es);
5004 break;
5006 case WM_ENABLE:
5007 es->bEnableState = (BOOL) wParam;
5008 EDIT_UpdateText(es, NULL, TRUE);
5009 break;
5011 case WM_ERASEBKGND:
5012 /* we do the proper erase in EDIT_WM_Paint */
5013 result = 1;
5014 break;
5016 case WM_GETFONT:
5017 result = (LRESULT)es->font;
5018 break;
5020 case WM_GETTEXT:
5021 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5022 break;
5024 case WM_GETTEXTLENGTH:
5025 if (unicode) result = get_text_length(es);
5026 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5027 NULL, 0, NULL, NULL );
5028 break;
5030 case WM_HSCROLL:
5031 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5032 break;
5034 case WM_KEYDOWN:
5035 result = EDIT_WM_KeyDown(es, (INT)wParam);
5036 break;
5038 case WM_KILLFOCUS:
5039 result = EDIT_WM_KillFocus(es);
5040 break;
5042 case WM_LBUTTONDBLCLK:
5043 result = EDIT_WM_LButtonDblClk(es);
5044 break;
5046 case WM_LBUTTONDOWN:
5047 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5048 break;
5050 case WM_LBUTTONUP:
5051 result = EDIT_WM_LButtonUp(es);
5052 break;
5054 case WM_MBUTTONDOWN:
5055 result = EDIT_WM_MButtonDown(es);
5056 break;
5058 case WM_MOUSEMOVE:
5059 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5060 break;
5062 case WM_PRINTCLIENT:
5063 case WM_PAINT:
5064 EDIT_WM_Paint(es, (HDC)wParam);
5065 break;
5067 case WM_PASTE:
5068 EDIT_WM_Paste(es);
5069 break;
5071 case WM_SETFOCUS:
5072 EDIT_WM_SetFocus(es);
5073 break;
5075 case WM_SETFONT:
5076 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5077 break;
5079 case WM_SETREDRAW:
5080 /* FIXME: actually set an internal flag and behave accordingly */
5081 break;
5083 case WM_SETTEXT:
5084 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5085 result = TRUE;
5086 break;
5088 case WM_SIZE:
5089 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
5090 break;
5092 case WM_STYLECHANGED:
5093 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5094 break;
5096 case WM_STYLECHANGING:
5097 result = 0; /* See EDIT_WM_StyleChanged */
5098 break;
5100 case WM_SYSKEYDOWN:
5101 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5102 break;
5104 case WM_TIMER:
5105 EDIT_WM_Timer(es);
5106 break;
5108 case WM_VSCROLL:
5109 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5110 break;
5112 case WM_MOUSEWHEEL:
5114 int wheelDelta;
5115 UINT pulScrollLines = 3;
5116 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5118 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5119 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5120 break;
5122 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5123 /* if scrolling changes direction, ignore left overs */
5124 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5125 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5126 es->wheelDeltaRemainder += wheelDelta;
5127 else
5128 es->wheelDeltaRemainder = wheelDelta;
5129 if (es->wheelDeltaRemainder && pulScrollLines)
5131 int cLineScroll;
5132 pulScrollLines = (int) min((UINT) es->line_count, pulScrollLines);
5133 cLineScroll = pulScrollLines * (float)es->wheelDeltaRemainder / WHEEL_DELTA;
5134 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
5135 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5138 break;
5141 /* IME messages to make the edit control IME aware */
5142 case WM_IME_SETCONTEXT:
5143 break;
5145 case WM_IME_STARTCOMPOSITION:
5146 es->composition_start = es->selection_end;
5147 es->composition_len = 0;
5148 break;
5150 case WM_IME_COMPOSITION:
5151 EDIT_ImeComposition(hwnd, lParam, es);
5152 break;
5154 case WM_IME_ENDCOMPOSITION:
5155 if (es->composition_len > 0)
5157 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
5158 es->selection_end = es->selection_start;
5159 es->composition_len= 0;
5161 break;
5163 case WM_IME_COMPOSITIONFULL:
5164 break;
5166 case WM_IME_SELECT:
5167 break;
5169 case WM_IME_CONTROL:
5170 break;
5172 default:
5173 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5174 break;
5177 if (IsWindow(hwnd) && es) EDIT_UnlockBuffer(es, FALSE);
5179 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5181 return result;
5185 /*********************************************************************
5186 * edit class descriptor
5188 static const WCHAR editW[] = {'E','d','i','t',0};
5189 const struct builtin_class_descr EDIT_builtin_class =
5191 editW, /* name */
5192 CS_DBLCLKS | CS_PARENTDC, /* style */
5193 WINPROC_EDIT, /* proc */
5194 #ifdef __i386__
5195 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
5196 #else
5197 sizeof(EDITSTATE *), /* extra */
5198 #endif
5199 IDC_IBEAM, /* cursor */
5200 0 /* brush */