ntdll/tests: Add some tests for opening objects through symlinks.
[wine.git] / dlls / user32 / edit.c
blobca40ed07164d487847b889623dd4fa64a33412d7
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTES
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_OEMCONVERT
45 #include "config.h"
47 #include <stdarg.h>
48 #include <string.h>
49 #include <stdlib.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnt.h"
54 #include "win.h"
55 #include "imm.h"
56 #include "usp10.h"
57 #include "wine/unicode.h"
58 #include "controls.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(edit);
63 WINE_DECLARE_DEBUG_CHANNEL(combo);
64 WINE_DECLARE_DEBUG_CHANNEL(relay);
66 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
67 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
68 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
69 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
72 * extra flags for EDITSTATE.flags field
74 #define EF_MODIFIED 0x0001 /* text has been modified */
75 #define EF_FOCUSED 0x0002 /* we have input focus */
76 #define EF_UPDATE 0x0004 /* notify parent of changed state */
77 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
79 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
80 wrapped line, instead of in front of the next character */
81 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
82 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
84 typedef enum
86 END_0 = 0, /* line ends with terminating '\0' character */
87 END_WRAP, /* line is wrapped */
88 END_HARD, /* line ends with a hard return '\r\n' */
89 END_SOFT, /* line ends with a soft return '\r\r\n' */
90 END_RICH /* line ends with a single '\n' */
91 } LINE_END;
93 typedef struct tagLINEDEF {
94 INT length; /* bruto length of a line in bytes */
95 INT net_length; /* netto length of a line in visible characters */
96 LINE_END ending;
97 INT width; /* width of the line in pixels */
98 INT index; /* line index into the buffer */
99 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
100 struct tagLINEDEF *next;
101 } LINEDEF;
103 typedef struct
105 BOOL is_unicode; /* how the control was created */
106 LPWSTR text; /* the actual contents of the control */
107 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
108 UINT buffer_size; /* the size of the buffer in characters */
109 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
110 HFONT font; /* NULL means standard system font */
111 INT x_offset; /* scroll offset for multi lines this is in pixels
112 for single lines it's in characters */
113 INT line_height; /* height of a screen line in pixels */
114 INT char_width; /* average character width in pixels */
115 DWORD style; /* sane version of wnd->dwStyle */
116 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
117 INT undo_insert_count; /* number of characters inserted in sequence */
118 UINT undo_position; /* character index of the insertion and deletion */
119 LPWSTR undo_text; /* deleted text */
120 UINT undo_buffer_size; /* size of the deleted text buffer */
121 INT selection_start; /* == selection_end if no selection */
122 INT selection_end; /* == current caret position */
123 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
124 INT left_margin; /* in pixels */
125 INT right_margin; /* in pixels */
126 RECT format_rect;
127 INT text_width; /* width of the widest line in pixels for multi line controls
128 and just line width for single line controls */
129 INT region_posx; /* Position of cursor relative to region: */
130 INT region_posy; /* -1: to left, 0: within, 1: to right */
131 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
132 INT line_count; /* number of lines */
133 INT y_offset; /* scroll offset in number of lines */
134 BOOL bCaptureState; /* flag indicating whether mouse was captured */
135 BOOL bEnableState; /* flag keeping the enable state */
136 HWND hwndSelf; /* the our window handle */
137 HWND hwndParent; /* Handle of parent for sending EN_* messages.
138 Even if parent will change, EN_* messages
139 should be sent to the first parent. */
140 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
141 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
143 * only for multi line controls
145 INT lock_count; /* amount of re-entries in the EditWndProc */
146 INT tabs_count;
147 LPINT tabs;
148 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
149 HLOCAL hloc32W; /* our unicode local memory block */
150 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
151 or EM_SETHANDLE */
152 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
154 * IME Data
156 UINT composition_len; /* length of composition, 0 == no composition */
157 int composition_start; /* the character position for the composition */
159 * Uniscribe Data
161 SCRIPT_LOGATTR *logAttr;
162 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
163 } EDITSTATE;
166 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
167 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
169 /* used for disabled or read-only edit control */
170 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
171 do \
172 { /* Notify parent which has created this edit control */ \
173 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
174 SendMessageW(es->hwndParent, WM_COMMAND, \
175 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
176 (LPARAM)(es->hwndSelf)); \
177 } while(0)
179 static const WCHAR empty_stringW[] = {0};
180 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
182 /*********************************************************************
184 * EM_CANUNDO
187 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
189 return (es->undo_insert_count || strlenW(es->undo_text));
193 /*********************************************************************
195 * EM_EMPTYUNDOBUFFER
198 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
200 es->undo_insert_count = 0;
201 *es->undo_text = '\0';
205 /**********************************************************************
206 * get_app_version
208 * Returns the window version in case Wine emulates a later version
209 * of windows than the application expects.
211 * In a number of cases when windows runs an application that was
212 * designed for an earlier windows version, windows reverts
213 * to "old" behaviour of that earlier version.
215 * An example is a disabled edit control that needs to be painted.
216 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
217 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
218 * applications with an expected version 0f 4.0 or higher.
221 static DWORD get_app_version(void)
223 static DWORD version;
224 if (!version)
226 DWORD dwEmulatedVersion;
227 OSVERSIONINFOW info;
228 DWORD dwProcVersion = GetProcessVersion(0);
230 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
231 GetVersionExW( &info );
232 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
233 /* FIXME: this may not be 100% correct; see discussion on the
234 * wine developer list in Nov 1999 */
235 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
237 return version;
240 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
242 HBRUSH hbrush;
243 UINT msg;
245 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
246 msg = WM_CTLCOLORSTATIC;
247 else
248 msg = WM_CTLCOLOREDIT;
250 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
251 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
252 if (!hbrush)
253 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
254 return hbrush;
258 static inline UINT get_text_length(EDITSTATE *es)
260 if(es->text_length == (UINT)-1)
261 es->text_length = strlenW(es->text);
262 return es->text_length;
266 /*********************************************************************
268 * EDIT_WordBreakProc
270 * Find the beginning of words.
271 * Note: unlike the specs for a WordBreakProc, this function can
272 * only be called without linebreaks between s[0] up to
273 * s[count - 1]. Remember it is only called
274 * internally, so we can decide this for ourselves.
275 * Additionally we will always be breaking the full string.
278 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
280 INT ret = 0;
282 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
284 if(!s) return 0;
286 if (!es->logAttr)
288 SCRIPT_ANALYSIS psa;
290 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
291 psa.eScript = SCRIPT_UNDEFINED;
293 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
294 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
297 switch (action) {
298 case WB_LEFT:
299 if (index)
300 index--;
301 while (index && !es->logAttr[index].fSoftBreak)
302 index--;
303 ret = index;
304 break;
305 case WB_RIGHT:
306 if (!count)
307 break;
308 while (s[index] && index < count && !es->logAttr[index].fSoftBreak)
309 index++;
310 ret = index;
311 break;
312 case WB_ISDELIMITER:
313 ret = es->logAttr[index].fWhiteSpace;
314 break;
315 default:
316 ERR("unknown action code, please report !\n");
317 break;
319 return ret;
323 /*********************************************************************
325 * EDIT_CallWordBreakProc
327 * Call appropriate WordBreakProc (internal or external).
329 * Note: The "start" argument should always be an index referring
330 * to es->text. The actual wordbreak proc might be
331 * 16 bit, so we can't always pass any 32 bit LPSTR.
332 * Hence we assume that es->text is the buffer that holds
333 * the string under examination (we can decide this for ourselves).
336 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
338 INT ret;
340 if (es->word_break_proc)
342 if(es->is_unicode)
344 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
346 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
347 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
348 ret = wbpW(es->text + start, index, count, action);
350 else
352 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
353 INT countA;
354 CHAR *textA;
356 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
357 textA = HeapAlloc(GetProcessHeap(), 0, countA);
358 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
359 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
360 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
361 ret = wbpA(textA, index, countA, action);
362 HeapFree(GetProcessHeap(), 0, textA);
365 else
366 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
368 return ret;
371 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
373 if (line_def->ssa)
375 ScriptStringFree(&line_def->ssa);
376 line_def->ssa = NULL;
380 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
382 LINEDEF *line_def = es->first_line_def;
383 while (line_def)
385 EDIT_InvalidateUniscribeData_linedef(line_def);
386 line_def = line_def->next;
388 if (es->ssa)
390 ScriptStringFree(&es->ssa);
391 es->ssa = NULL;
395 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
397 if (!line_def)
398 return NULL;
400 if (line_def->net_length && !line_def->ssa)
402 int index = line_def->index;
403 HFONT old_font = NULL;
404 HDC udc = dc;
405 SCRIPT_TABDEF tabdef;
406 HRESULT hr;
408 if (!udc)
409 udc = GetDC(es->hwndSelf);
410 if (es->font)
411 old_font = SelectObject(udc, es->font);
413 tabdef.cTabStops = es->tabs_count;
414 tabdef.iScale = 0;
415 tabdef.pTabStops = es->tabs;
416 tabdef.iTabOrigin = 0;
418 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
419 (1.5*line_def->net_length+16), -1,
420 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
421 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
422 if (FAILED(hr))
424 WARN("ScriptStringAnalyse failed (%x)\n",hr);
425 line_def->ssa = NULL;
428 if (es->font)
429 SelectObject(udc, old_font);
430 if (udc != dc)
431 ReleaseDC(es->hwndSelf, udc);
434 return line_def->ssa;
437 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
439 LINEDEF *line_def;
441 if (!(es->style & ES_MULTILINE))
443 if (!es->ssa)
445 INT length = get_text_length(es);
446 HFONT old_font = NULL;
447 HDC udc = dc;
449 if (!udc)
450 udc = GetDC(es->hwndSelf);
451 if (es->font)
452 old_font = SelectObject(udc, es->font);
454 if (es->style & ES_PASSWORD)
455 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);
456 else
457 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
459 if (es->font)
460 SelectObject(udc, old_font);
461 if (udc != dc)
462 ReleaseDC(es->hwndSelf, udc);
464 return es->ssa;
466 else
468 line_def = es->first_line_def;
469 while (line_def && line)
471 line_def = line_def->next;
472 line--;
475 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
479 static inline INT get_vertical_line_count(EDITSTATE *es)
481 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
482 return max(1,vlc);
485 /*********************************************************************
487 * EDIT_BuildLineDefs_ML
489 * Build linked list of text lines.
490 * Lines can end with '\0' (last line), a character (if it is wrapped),
491 * a soft return '\r\r\n' or a hard return '\r\n'
494 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
496 LPWSTR current_position, cp;
497 INT fw;
498 LINEDEF *current_line;
499 LINEDEF *previous_line;
500 LINEDEF *start_line;
501 INT line_index = 0, nstart_line, nstart_index;
502 INT line_count = es->line_count;
503 INT orig_net_length;
504 RECT rc;
505 INT vlc;
507 if (istart == iend && delta == 0)
508 return;
510 previous_line = NULL;
511 current_line = es->first_line_def;
513 /* Find starting line. istart must lie inside an existing line or
514 * at the end of buffer */
515 do {
516 if (istart < current_line->index + current_line->length ||
517 current_line->ending == END_0)
518 break;
520 previous_line = current_line;
521 current_line = current_line->next;
522 line_index++;
523 } while (current_line);
525 if (!current_line) /* Error occurred start is not inside previous buffer */
527 FIXME(" modification occurred outside buffer\n");
528 return;
531 /* Remember start of modifications in order to calculate update region */
532 nstart_line = line_index;
533 nstart_index = current_line->index;
535 /* We must start to reformat from the previous line since the modifications
536 * may have caused the line to wrap upwards. */
537 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
539 line_index--;
540 current_line = previous_line;
542 start_line = current_line;
544 fw = es->format_rect.right - es->format_rect.left;
545 current_position = es->text + current_line->index;
546 vlc = get_vertical_line_count(es);
547 do {
548 if (current_line != start_line)
550 if (!current_line || current_line->index + delta > current_position - es->text)
552 /* The buffer has been expanded, create a new line and
553 insert it into the link list */
554 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
555 new_line->next = previous_line->next;
556 previous_line->next = new_line;
557 current_line = new_line;
558 es->line_count++;
560 else if (current_line->index + delta < current_position - es->text)
562 /* The previous line merged with this line so we delete this extra entry */
563 previous_line->next = current_line->next;
564 HeapFree(GetProcessHeap(), 0, current_line);
565 current_line = previous_line->next;
566 es->line_count--;
567 continue;
569 else /* current_line->index + delta == current_position */
571 if (current_position - es->text > iend)
572 break; /* We reached end of line modifications */
573 /* else recalculate this line */
577 current_line->index = current_position - es->text;
578 orig_net_length = current_line->net_length;
580 /* Find end of line */
581 cp = current_position;
582 while (*cp) {
583 if (*cp == '\n') break;
584 if ((*cp == '\r') && (*(cp + 1) == '\n'))
585 break;
586 cp++;
589 /* Mark type of line termination */
590 if (!(*cp)) {
591 current_line->ending = END_0;
592 current_line->net_length = strlenW(current_position);
593 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
594 current_line->ending = END_SOFT;
595 current_line->net_length = cp - current_position - 1;
596 } else if (*cp == '\n') {
597 current_line->ending = END_RICH;
598 current_line->net_length = cp - current_position;
599 } else {
600 current_line->ending = END_HARD;
601 current_line->net_length = cp - current_position;
604 if (current_line->net_length)
606 const SIZE *sz;
607 EDIT_InvalidateUniscribeData_linedef(current_line);
608 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
609 if (current_line->ssa)
611 sz = ScriptString_pSize(current_line->ssa);
612 /* Calculate line width */
613 current_line->width = sz->cx;
615 else current_line->width = es->char_width * current_line->net_length;
617 else current_line->width = 0;
619 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
621 /* Line breaks just look back from the end and find the next break and try that. */
623 if (!(es->style & ES_AUTOHSCROLL)) {
624 if (current_line->width > fw && fw > es->char_width) {
626 INT prev, next;
627 int w;
628 const SIZE *sz;
629 float d;
631 prev = current_line->net_length - 1;
632 w = current_line->net_length;
633 d = (float)current_line->width/(float)fw;
634 if (d > 1.2f) d -= 0.2f;
635 next = prev/d;
636 if (next >= prev) next = prev-1;
637 do {
638 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
639 next, current_line->net_length, WB_LEFT);
640 current_line->net_length = prev;
641 EDIT_InvalidateUniscribeData_linedef(current_line);
642 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
643 if (current_line->ssa)
644 sz = ScriptString_pSize(current_line->ssa);
645 else sz = 0;
646 if (sz)
647 current_line->width = sz->cx;
648 else
649 prev = 0;
650 next = prev - 1;
651 } while (prev && current_line->width > fw);
652 current_line->net_length = w;
654 if (prev == 0) { /* Didn't find a line break so force a break */
655 INT *piDx;
656 const INT *count;
658 EDIT_InvalidateUniscribeData_linedef(current_line);
659 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
661 if (current_line->ssa)
663 count = ScriptString_pcOutChars(current_line->ssa);
664 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
665 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
667 prev = current_line->net_length-1;
668 do {
669 current_line->width -= piDx[prev];
670 prev--;
671 } while ( prev > 0 && current_line->width > fw);
672 if (prev<=0)
673 prev = 1;
674 HeapFree(GetProcessHeap(),0,piDx);
676 else
677 prev = (fw / es->char_width);
680 /* If the first line we are calculating, wrapped before istart, we must
681 * adjust istart in order for this to be reflected in the update region. */
682 if (current_line->index == nstart_index && istart > current_line->index + prev)
683 istart = current_line->index + prev;
684 /* else if we are updating the previous line before the first line we
685 * are re-calculating and it expanded */
686 else if (current_line == start_line &&
687 current_line->index != nstart_index && orig_net_length < prev)
689 /* Line expanded due to an upwards line wrap so we must partially include
690 * previous line in update region */
691 nstart_line = line_index;
692 nstart_index = current_line->index;
693 istart = current_line->index + orig_net_length;
696 current_line->net_length = prev;
697 current_line->ending = END_WRAP;
699 if (current_line->net_length > 0)
701 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
702 if (current_line->ssa)
704 sz = ScriptString_pSize(current_line->ssa);
705 current_line->width = sz->cx;
707 else
708 current_line->width = 0;
710 else current_line->width = 0;
712 else if (current_line == start_line &&
713 current_line->index != nstart_index &&
714 orig_net_length < current_line->net_length) {
715 /* The previous line expanded but it's still not as wide as the client rect */
716 /* The expansion is due to an upwards line wrap so we must partially include
717 it in the update region */
718 nstart_line = line_index;
719 nstart_index = current_line->index;
720 istart = current_line->index + orig_net_length;
725 /* Adjust length to include line termination */
726 switch (current_line->ending) {
727 case END_SOFT:
728 current_line->length = current_line->net_length + 3;
729 break;
730 case END_RICH:
731 current_line->length = current_line->net_length + 1;
732 break;
733 case END_HARD:
734 current_line->length = current_line->net_length + 2;
735 break;
736 case END_WRAP:
737 case END_0:
738 current_line->length = current_line->net_length;
739 break;
741 es->text_width = max(es->text_width, current_line->width);
742 current_position += current_line->length;
743 previous_line = current_line;
745 /* Discard data for non-visible lines. It will be calculated as needed */
746 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
747 EDIT_InvalidateUniscribeData_linedef(current_line);
749 current_line = current_line->next;
750 line_index++;
751 } while (previous_line->ending != END_0);
753 /* Finish adjusting line indexes by delta or remove hanging lines */
754 if (previous_line->ending == END_0)
756 LINEDEF *pnext = NULL;
758 previous_line->next = NULL;
759 while (current_line)
761 pnext = current_line->next;
762 EDIT_InvalidateUniscribeData_linedef(current_line);
763 HeapFree(GetProcessHeap(), 0, current_line);
764 current_line = pnext;
765 es->line_count--;
768 else if (delta != 0)
770 while (current_line)
772 current_line->index += delta;
773 current_line = current_line->next;
777 /* Calculate rest of modification rectangle */
778 if (hrgn)
780 HRGN tmphrgn;
782 * We calculate two rectangles. One for the first line which may have
783 * an indent with respect to the format rect. The other is a format-width
784 * rectangle that spans the rest of the lines that changed or moved.
786 rc.top = es->format_rect.top + nstart_line * es->line_height -
787 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
788 rc.bottom = rc.top + es->line_height;
789 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
790 rc.left = es->format_rect.left;
791 else
792 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
793 rc.right = es->format_rect.right;
794 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
796 rc.top = rc.bottom;
797 rc.left = es->format_rect.left;
798 rc.right = es->format_rect.right;
800 * If lines were added or removed we must re-paint the remainder of the
801 * lines since the remaining lines were either shifted up or down.
803 if (line_count < es->line_count) /* We added lines */
804 rc.bottom = es->line_count * es->line_height;
805 else if (line_count > es->line_count) /* We removed lines */
806 rc.bottom = line_count * es->line_height;
807 else
808 rc.bottom = line_index * es->line_height;
809 rc.bottom += es->format_rect.top;
810 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
811 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
812 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
813 DeleteObject(tmphrgn);
817 /*********************************************************************
819 * EDIT_CalcLineWidth_SL
822 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
824 EDIT_UpdateUniscribeData(es, NULL, 0);
825 if (es->ssa)
827 const SIZE *size;
828 size = ScriptString_pSize(es->ssa);
829 es->text_width = size->cx;
831 else
832 es->text_width = 0;
835 /*********************************************************************
837 * EDIT_CharFromPos
839 * Beware: This is not the function called on EM_CHARFROMPOS
840 * The position _can_ be outside the formatting / client
841 * rectangle
842 * The return value is only the character index
845 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
847 INT index;
849 if (es->style & ES_MULTILINE) {
850 int trailing;
851 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
852 INT line_index = 0;
853 LINEDEF *line_def = es->first_line_def;
854 EDIT_UpdateUniscribeData(es, NULL, line);
855 while ((line > 0) && line_def->next) {
856 line_index += line_def->length;
857 line_def = line_def->next;
858 line--;
861 x += es->x_offset - es->format_rect.left;
862 if (es->style & ES_RIGHT)
863 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
864 else if (es->style & ES_CENTER)
865 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
866 if (x >= line_def->width) {
867 if (after_wrap)
868 *after_wrap = (line_def->ending == END_WRAP);
869 return line_index + line_def->net_length;
871 if (x <= 0 || !line_def->ssa) {
872 if (after_wrap)
873 *after_wrap = FALSE;
874 return line_index;
877 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
878 if (trailing) index++;
879 index += line_index;
880 if (after_wrap)
881 *after_wrap = ((index == line_index + line_def->net_length) &&
882 (line_def->ending == END_WRAP));
883 } else {
884 INT xoff = 0;
885 INT trailing;
886 if (after_wrap)
887 *after_wrap = FALSE;
888 x -= es->format_rect.left;
889 if (!x)
890 return es->x_offset;
892 if (!es->x_offset)
894 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
895 if (es->style & ES_RIGHT)
896 x -= indent;
897 else if (es->style & ES_CENTER)
898 x -= indent / 2;
901 EDIT_UpdateUniscribeData(es, NULL, 0);
902 if (es->x_offset)
904 if (es->ssa)
906 if (es->x_offset>= get_text_length(es))
908 const SIZE *size;
909 size = ScriptString_pSize(es->ssa);
910 xoff = size->cx;
912 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
914 else
915 xoff = 0;
917 if (x < 0)
919 if (x + xoff > 0 || !es->ssa)
921 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
922 if (trailing) index++;
924 else
925 index = 0;
927 else
929 if (x)
931 const SIZE *size = NULL;
932 if (es->ssa)
933 size = ScriptString_pSize(es->ssa);
934 if (!size)
935 index = 0;
936 else if (x > size->cx)
937 index = get_text_length(es);
938 else if (es->ssa)
940 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
941 if (trailing) index++;
943 else
944 index = 0;
946 else
947 index = es->x_offset;
950 return index;
954 /*********************************************************************
956 * EDIT_ConfinePoint
958 * adjusts the point to be within the formatting rectangle
959 * (so CharFromPos returns the nearest _visible_ character)
962 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
964 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
965 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
969 /*********************************************************************
971 * EM_LINEFROMCHAR
974 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
976 INT line;
977 LINEDEF *line_def;
979 if (!(es->style & ES_MULTILINE))
980 return 0;
981 if (index > (INT)get_text_length(es))
982 return es->line_count - 1;
983 if (index == -1)
984 index = min(es->selection_start, es->selection_end);
986 line = 0;
987 line_def = es->first_line_def;
988 index -= line_def->length;
989 while ((index >= 0) && line_def->next) {
990 line++;
991 line_def = line_def->next;
992 index -= line_def->length;
994 return line;
998 /*********************************************************************
1000 * EM_LINEINDEX
1003 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
1005 INT line_index;
1006 const LINEDEF *line_def;
1008 if (!(es->style & ES_MULTILINE))
1009 return 0;
1010 if (line >= es->line_count)
1011 return -1;
1013 line_index = 0;
1014 line_def = es->first_line_def;
1015 if (line == -1) {
1016 INT index = es->selection_end - line_def->length;
1017 while ((index >= 0) && line_def->next) {
1018 line_index += line_def->length;
1019 line_def = line_def->next;
1020 index -= line_def->length;
1022 } else {
1023 while (line > 0) {
1024 line_index += line_def->length;
1025 line_def = line_def->next;
1026 line--;
1029 return line_index;
1033 /*********************************************************************
1035 * EM_LINELENGTH
1038 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1040 LINEDEF *line_def;
1042 if (!(es->style & ES_MULTILINE))
1043 return get_text_length(es);
1045 if (index == -1) {
1046 /* get the number of remaining non-selected chars of selected lines */
1047 INT32 l; /* line number */
1048 INT32 li; /* index of first char in line */
1049 INT32 count;
1050 l = EDIT_EM_LineFromChar(es, es->selection_start);
1051 /* # chars before start of selection area */
1052 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1053 l = EDIT_EM_LineFromChar(es, es->selection_end);
1054 /* # chars after end of selection */
1055 li = EDIT_EM_LineIndex(es, l);
1056 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1057 return count;
1059 line_def = es->first_line_def;
1060 index -= line_def->length;
1061 while ((index >= 0) && line_def->next) {
1062 line_def = line_def->next;
1063 index -= line_def->length;
1065 return line_def->net_length;
1069 /*********************************************************************
1071 * EM_POSFROMCHAR
1074 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1076 INT len = get_text_length(es);
1077 INT l;
1078 INT li;
1079 INT x = 0;
1080 INT y = 0;
1081 INT w;
1082 INT lw;
1083 LINEDEF *line_def;
1085 index = min(index, len);
1086 if (es->style & ES_MULTILINE) {
1087 l = EDIT_EM_LineFromChar(es, index);
1088 EDIT_UpdateUniscribeData(es, NULL, l);
1090 y = (l - es->y_offset) * es->line_height;
1091 li = EDIT_EM_LineIndex(es, l);
1092 if (after_wrap && (li == index) && l) {
1093 INT l2 = l - 1;
1094 line_def = es->first_line_def;
1095 while (l2) {
1096 line_def = line_def->next;
1097 l2--;
1099 if (line_def->ending == END_WRAP) {
1100 l--;
1101 y -= es->line_height;
1102 li = EDIT_EM_LineIndex(es, l);
1106 line_def = es->first_line_def;
1107 while (line_def->index != li)
1108 line_def = line_def->next;
1110 lw = line_def->width;
1111 w = es->format_rect.right - es->format_rect.left;
1112 if (line_def->ssa)
1114 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1115 x -= es->x_offset;
1117 else
1118 x = es->x_offset;
1120 if (es->style & ES_RIGHT)
1121 x = w - (lw - x);
1122 else if (es->style & ES_CENTER)
1123 x += (w - lw) / 2;
1124 } else {
1125 INT xoff = 0;
1126 INT xi = 0;
1127 EDIT_UpdateUniscribeData(es, NULL, 0);
1128 if (es->x_offset)
1130 if (es->ssa)
1132 if (es->x_offset >= get_text_length(es))
1134 int leftover = es->x_offset - get_text_length(es);
1135 if (es->ssa)
1137 const SIZE *size;
1138 size = ScriptString_pSize(es->ssa);
1139 xoff = size->cx;
1141 else
1142 xoff = 0;
1143 xoff += es->char_width * leftover;
1145 else
1146 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1148 else
1149 xoff = 0;
1151 if (index)
1153 if (index >= get_text_length(es))
1155 if (es->ssa)
1157 const SIZE *size;
1158 size = ScriptString_pSize(es->ssa);
1159 xi = size->cx;
1161 else
1162 xi = 0;
1164 else if (es->ssa)
1165 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1166 else
1167 xi = 0;
1169 x = xi - xoff;
1171 if (index >= es->x_offset) {
1172 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1174 w = es->format_rect.right - es->format_rect.left;
1175 if (w > es->text_width)
1177 if (es->style & ES_RIGHT)
1178 x += w - es->text_width;
1179 else if (es->style & ES_CENTER)
1180 x += (w - es->text_width) / 2;
1184 y = 0;
1186 x += es->format_rect.left;
1187 y += es->format_rect.top;
1188 return MAKELONG((INT16)x, (INT16)y);
1192 /*********************************************************************
1194 * EDIT_GetLineRect
1196 * Calculates the bounding rectangle for a line from a starting
1197 * column to an ending column.
1200 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1202 SCRIPT_STRING_ANALYSIS ssa;
1203 INT line_index = 0;
1204 INT pt1, pt2, pt3;
1206 if (es->style & ES_MULTILINE)
1208 const LINEDEF *line_def = NULL;
1209 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1210 if (line >= es->line_count)
1211 return;
1213 line_def = es->first_line_def;
1214 if (line == -1) {
1215 INT index = es->selection_end - line_def->length;
1216 while ((index >= 0) && line_def->next) {
1217 line_index += line_def->length;
1218 line_def = line_def->next;
1219 index -= line_def->length;
1221 } else {
1222 while (line > 0) {
1223 line_index += line_def->length;
1224 line_def = line_def->next;
1225 line--;
1228 ssa = line_def->ssa;
1230 else
1232 line_index = 0;
1233 rc->top = es->format_rect.top;
1234 ssa = es->ssa;
1237 rc->bottom = rc->top + es->line_height;
1238 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1239 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1240 if (ssa)
1242 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1243 pt3+=es->format_rect.left;
1245 else pt3 = pt1;
1246 rc->right = max(max(pt1 , pt2),pt3);
1247 rc->left = min(min(pt1, pt2),pt3);
1251 static inline void text_buffer_changed(EDITSTATE *es)
1253 es->text_length = (UINT)-1;
1255 HeapFree( GetProcessHeap(), 0, es->logAttr );
1256 es->logAttr = NULL;
1257 EDIT_InvalidateUniscribeData(es);
1260 /*********************************************************************
1261 * EDIT_LockBuffer
1264 static void EDIT_LockBuffer(EDITSTATE *es)
1266 if (es->hlocapp) return;
1268 if (!es->text) {
1270 if(!es->hloc32W) return;
1272 if(es->hloc32A)
1274 CHAR *textA = LocalLock(es->hloc32A);
1275 HLOCAL hloc32W_new;
1276 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1277 if(countW_new > es->buffer_size + 1)
1279 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1280 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1281 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1282 if(hloc32W_new)
1284 es->hloc32W = hloc32W_new;
1285 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1286 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1288 else
1289 WARN("FAILED! Will synchronize partially\n");
1291 es->text = LocalLock(es->hloc32W);
1292 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1293 LocalUnlock(es->hloc32A);
1295 else es->text = LocalLock(es->hloc32W);
1297 es->lock_count++;
1301 /*********************************************************************
1303 * EDIT_UnlockBuffer
1306 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1308 if (es->hlocapp) return;
1310 /* Edit window might be already destroyed */
1311 if(!IsWindow(es->hwndSelf))
1313 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1314 return;
1317 if (!es->lock_count) {
1318 ERR("lock_count == 0 ... please report\n");
1319 return;
1321 if (!es->text) {
1322 ERR("es->text == 0 ... please report\n");
1323 return;
1326 if (force || (es->lock_count == 1)) {
1327 if (es->hloc32W) {
1328 UINT countA = 0;
1329 UINT countW = get_text_length(es) + 1;
1331 if(es->hloc32A)
1333 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1334 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1335 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1336 countA = LocalSize(es->hloc32A);
1337 if(countA_new > countA)
1339 HLOCAL hloc32A_new;
1340 UINT alloc_size = ROUND_TO_GROW(countA_new);
1341 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1342 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1343 if(hloc32A_new)
1345 es->hloc32A = hloc32A_new;
1346 countA = LocalSize(hloc32A_new);
1347 TRACE("Real new size %d bytes\n", countA);
1349 else
1350 WARN("FAILED! Will synchronize partially\n");
1352 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1353 LocalLock(es->hloc32A), countA, NULL, NULL);
1354 LocalUnlock(es->hloc32A);
1357 LocalUnlock(es->hloc32W);
1358 es->text = NULL;
1360 else {
1361 ERR("no buffer ... please report\n");
1362 return;
1365 es->lock_count--;
1369 /*********************************************************************
1371 * EDIT_MakeFit
1373 * Try to fit size + 1 characters in the buffer.
1375 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1377 HLOCAL hNew32W;
1379 if (size <= es->buffer_size)
1380 return TRUE;
1382 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1384 /* Force edit to unlock its buffer. es->text now NULL */
1385 EDIT_UnlockBuffer(es, TRUE);
1387 if (es->hloc32W) {
1388 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1389 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1390 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1391 es->hloc32W = hNew32W;
1392 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1396 EDIT_LockBuffer(es);
1398 if (es->buffer_size < size) {
1399 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1400 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1401 return FALSE;
1402 } else {
1403 TRACE("We now have %d+1\n", es->buffer_size);
1404 return TRUE;
1409 /*********************************************************************
1411 * EDIT_MakeUndoFit
1413 * Try to fit size + 1 bytes in the undo buffer.
1416 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1418 UINT alloc_size;
1420 if (size <= es->undo_buffer_size)
1421 return TRUE;
1423 TRACE("trying to ReAlloc to %d+1\n", size);
1425 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1426 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1427 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1428 return TRUE;
1430 else
1432 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1433 return FALSE;
1438 /*********************************************************************
1440 * EDIT_UpdateTextRegion
1443 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1445 if (es->flags & EF_UPDATE) {
1446 es->flags &= ~EF_UPDATE;
1447 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1449 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1453 /*********************************************************************
1455 * EDIT_UpdateText
1458 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1460 if (es->flags & EF_UPDATE) {
1461 es->flags &= ~EF_UPDATE;
1462 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1464 InvalidateRect(es->hwndSelf, rc, bErase);
1467 /*********************************************************************
1469 * EDIT_SL_InvalidateText
1471 * Called from EDIT_InvalidateText().
1472 * Does the job for single-line controls only.
1475 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1477 RECT line_rect;
1478 RECT rc;
1480 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1481 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1482 EDIT_UpdateText(es, &rc, TRUE);
1485 /*********************************************************************
1487 * EDIT_ML_InvalidateText
1489 * Called from EDIT_InvalidateText().
1490 * Does the job for multi-line controls only.
1493 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1495 INT vlc = get_vertical_line_count(es);
1496 INT sl = EDIT_EM_LineFromChar(es, start);
1497 INT el = EDIT_EM_LineFromChar(es, end);
1498 INT sc;
1499 INT ec;
1500 RECT rc1;
1501 RECT rcWnd;
1502 RECT rcLine;
1503 RECT rcUpdate;
1504 INT l;
1506 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1507 return;
1509 sc = start - EDIT_EM_LineIndex(es, sl);
1510 ec = end - EDIT_EM_LineIndex(es, el);
1511 if (sl < es->y_offset) {
1512 sl = es->y_offset;
1513 sc = 0;
1515 if (el > es->y_offset + vlc) {
1516 el = es->y_offset + vlc;
1517 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1519 GetClientRect(es->hwndSelf, &rc1);
1520 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1521 if (sl == el) {
1522 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1523 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1524 EDIT_UpdateText(es, &rcUpdate, TRUE);
1525 } else {
1526 EDIT_GetLineRect(es, sl, sc,
1527 EDIT_EM_LineLength(es,
1528 EDIT_EM_LineIndex(es, sl)),
1529 &rcLine);
1530 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1531 EDIT_UpdateText(es, &rcUpdate, TRUE);
1532 for (l = sl + 1 ; l < el ; l++) {
1533 EDIT_GetLineRect(es, l, 0,
1534 EDIT_EM_LineLength(es,
1535 EDIT_EM_LineIndex(es, l)),
1536 &rcLine);
1537 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1538 EDIT_UpdateText(es, &rcUpdate, TRUE);
1540 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1541 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1542 EDIT_UpdateText(es, &rcUpdate, TRUE);
1547 /*********************************************************************
1549 * EDIT_InvalidateText
1551 * Invalidate the text from offset start up to, but not including,
1552 * offset end. Useful for (re)painting the selection.
1553 * Regions outside the linewidth are not invalidated.
1554 * end == -1 means end == TextLength.
1555 * start and end need not be ordered.
1558 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1560 if (end == start)
1561 return;
1563 if (end == -1)
1564 end = get_text_length(es);
1566 if (end < start) {
1567 INT tmp = start;
1568 start = end;
1569 end = tmp;
1572 if (es->style & ES_MULTILINE)
1573 EDIT_ML_InvalidateText(es, start, end);
1574 else
1575 EDIT_SL_InvalidateText(es, start, end);
1579 /*********************************************************************
1581 * EDIT_EM_SetSel
1583 * note: unlike the specs say: the order of start and end
1584 * _is_ preserved in Windows. (i.e. start can be > end)
1585 * In other words: this handler is OK
1588 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1590 UINT old_start = es->selection_start;
1591 UINT old_end = es->selection_end;
1592 UINT len = get_text_length(es);
1594 if (start == (UINT)-1) {
1595 start = es->selection_end;
1596 end = es->selection_end;
1597 } else {
1598 start = min(start, len);
1599 end = min(end, len);
1601 es->selection_start = start;
1602 es->selection_end = end;
1603 if (after_wrap)
1604 es->flags |= EF_AFTER_WRAP;
1605 else
1606 es->flags &= ~EF_AFTER_WRAP;
1607 /* Compute the necessary invalidation region. */
1608 /* Note that we don't need to invalidate regions which have
1609 * "never" been selected, or those which are "still" selected.
1610 * In fact, every time we hit a selection boundary, we can
1611 * *toggle* whether we need to invalidate. Thus we can optimize by
1612 * *sorting* the interval endpoints. Let's assume that we sort them
1613 * in this order:
1614 * start <= end <= old_start <= old_end
1615 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1616 * in 5 comparisons; i.e. it is impossible to do better than the
1617 * following: */
1618 ORDER_UINT(end, old_end);
1619 ORDER_UINT(start, old_start);
1620 ORDER_UINT(old_start, old_end);
1621 ORDER_UINT(start, end);
1622 /* Note that at this point 'end' and 'old_start' are not in order, but
1623 * start is definitely the min. and old_end is definitely the max. */
1624 if (end != old_start)
1627 * One can also do
1628 * ORDER_UINT32(end, old_start);
1629 * EDIT_InvalidateText(es, start, end);
1630 * EDIT_InvalidateText(es, old_start, old_end);
1631 * in place of the following if statement.
1632 * (That would complete the optimal five-comparison four-element sort.)
1634 if (old_start > end )
1636 EDIT_InvalidateText(es, start, end);
1637 EDIT_InvalidateText(es, old_start, old_end);
1639 else
1641 EDIT_InvalidateText(es, start, old_start);
1642 EDIT_InvalidateText(es, end, old_end);
1645 else EDIT_InvalidateText(es, start, old_end);
1649 /*********************************************************************
1651 * EDIT_UpdateScrollInfo
1654 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1656 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1658 SCROLLINFO si;
1659 si.cbSize = sizeof(SCROLLINFO);
1660 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1661 si.nMin = 0;
1662 si.nMax = es->line_count - 1;
1663 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1664 si.nPos = es->y_offset;
1665 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1666 si.nMin, si.nMax, si.nPage, si.nPos);
1667 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1670 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1672 SCROLLINFO si;
1673 si.cbSize = sizeof(SCROLLINFO);
1674 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1675 si.nMin = 0;
1676 si.nMax = es->text_width - 1;
1677 si.nPage = es->format_rect.right - es->format_rect.left;
1678 si.nPos = es->x_offset;
1679 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1680 si.nMin, si.nMax, si.nPage, si.nPos);
1681 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1686 /*********************************************************************
1688 * EDIT_EM_LineScroll_internal
1690 * Version of EDIT_EM_LineScroll for internal use.
1691 * It doesn't refuse if ES_MULTILINE is set and assumes that
1692 * dx is in pixels, dy - in lines.
1695 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1697 INT nyoff;
1698 INT x_offset_in_pixels;
1699 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1700 es->line_height;
1702 if (es->style & ES_MULTILINE)
1704 x_offset_in_pixels = es->x_offset;
1706 else
1708 dy = 0;
1709 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1712 if (-dx > x_offset_in_pixels)
1713 dx = -x_offset_in_pixels;
1714 if (dx > es->text_width - x_offset_in_pixels)
1715 dx = es->text_width - x_offset_in_pixels;
1716 nyoff = max(0, es->y_offset + dy);
1717 if (nyoff >= es->line_count - lines_per_page)
1718 nyoff = max(0, es->line_count - lines_per_page);
1719 dy = (es->y_offset - nyoff) * es->line_height;
1720 if (dx || dy) {
1721 RECT rc1;
1722 RECT rc;
1724 es->y_offset = nyoff;
1725 if(es->style & ES_MULTILINE)
1726 es->x_offset += dx;
1727 else
1728 es->x_offset += dx / es->char_width;
1730 GetClientRect(es->hwndSelf, &rc1);
1731 IntersectRect(&rc, &rc1, &es->format_rect);
1732 ScrollWindowEx(es->hwndSelf, -dx, dy,
1733 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1734 /* force scroll info update */
1735 EDIT_UpdateScrollInfo(es);
1737 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1738 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1739 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1740 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1741 return TRUE;
1744 /*********************************************************************
1746 * EM_LINESCROLL
1748 * NOTE: dx is in average character widths, dy - in lines;
1751 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1753 if (!(es->style & ES_MULTILINE))
1754 return FALSE;
1756 dx *= es->char_width;
1757 return EDIT_EM_LineScroll_internal(es, dx, dy);
1761 /*********************************************************************
1763 * EM_SCROLL
1766 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1768 INT dy;
1770 if (!(es->style & ES_MULTILINE))
1771 return (LRESULT)FALSE;
1773 dy = 0;
1775 switch (action) {
1776 case SB_LINEUP:
1777 if (es->y_offset)
1778 dy = -1;
1779 break;
1780 case SB_LINEDOWN:
1781 if (es->y_offset < es->line_count - 1)
1782 dy = 1;
1783 break;
1784 case SB_PAGEUP:
1785 if (es->y_offset)
1786 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1787 break;
1788 case SB_PAGEDOWN:
1789 if (es->y_offset < es->line_count - 1)
1790 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1791 break;
1792 default:
1793 return (LRESULT)FALSE;
1795 if (dy) {
1796 INT vlc = get_vertical_line_count(es);
1797 /* check if we are going to move too far */
1798 if(es->y_offset + dy > es->line_count - vlc)
1799 dy = max(es->line_count - vlc, 0) - es->y_offset;
1801 /* Notification is done in EDIT_EM_LineScroll */
1802 if(dy) {
1803 EDIT_EM_LineScroll(es, 0, dy);
1804 return MAKELONG(dy, TRUE);
1808 return (LRESULT)FALSE;
1812 /*********************************************************************
1814 * EDIT_SetCaretPos
1817 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1818 BOOL after_wrap)
1820 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1821 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1822 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1826 /*********************************************************************
1828 * EM_SCROLLCARET
1831 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1833 if (es->style & ES_MULTILINE) {
1834 INT l;
1835 INT vlc;
1836 INT ww;
1837 INT cw = es->char_width;
1838 INT x;
1839 INT dy = 0;
1840 INT dx = 0;
1842 l = EDIT_EM_LineFromChar(es, es->selection_end);
1843 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1844 vlc = get_vertical_line_count(es);
1845 if (l >= es->y_offset + vlc)
1846 dy = l - vlc + 1 - es->y_offset;
1847 if (l < es->y_offset)
1848 dy = l - es->y_offset;
1849 ww = es->format_rect.right - es->format_rect.left;
1850 if (x < es->format_rect.left)
1851 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1852 if (x > es->format_rect.right)
1853 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1854 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1856 /* check if we are going to move too far */
1857 if(es->x_offset + dx + ww > es->text_width)
1858 dx = es->text_width - ww - es->x_offset;
1859 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1860 EDIT_EM_LineScroll_internal(es, dx, dy);
1862 } else {
1863 INT x;
1864 INT goal;
1865 INT format_width;
1867 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1868 format_width = es->format_rect.right - es->format_rect.left;
1869 if (x < es->format_rect.left) {
1870 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1871 do {
1872 es->x_offset--;
1873 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1874 } while ((x < goal) && es->x_offset);
1875 /* FIXME: use ScrollWindow() somehow to improve performance */
1876 EDIT_UpdateText(es, NULL, TRUE);
1877 } else if (x > es->format_rect.right) {
1878 INT x_last;
1879 INT len = get_text_length(es);
1880 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1881 do {
1882 es->x_offset++;
1883 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1884 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1885 } while ((x > goal) && (x_last > es->format_rect.right));
1886 /* FIXME: use ScrollWindow() somehow to improve performance */
1887 EDIT_UpdateText(es, NULL, TRUE);
1891 if(es->flags & EF_FOCUSED)
1892 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1896 /*********************************************************************
1898 * EDIT_MoveBackward
1901 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1903 INT e = es->selection_end;
1905 if (e) {
1906 e--;
1907 if ((es->style & ES_MULTILINE) && e &&
1908 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1909 e--;
1910 if (e && (es->text[e - 1] == '\r'))
1911 e--;
1914 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1915 EDIT_EM_ScrollCaret(es);
1919 /*********************************************************************
1921 * EDIT_MoveDown_ML
1923 * Only for multi line controls
1924 * Move the caret one line down, on a column with the nearest
1925 * x coordinate on the screen (might be a different column).
1928 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1930 INT s = es->selection_start;
1931 INT e = es->selection_end;
1932 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1933 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1934 INT x = (short)LOWORD(pos);
1935 INT y = (short)HIWORD(pos);
1937 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1938 if (!extend)
1939 s = e;
1940 EDIT_EM_SetSel(es, s, e, after_wrap);
1941 EDIT_EM_ScrollCaret(es);
1945 /*********************************************************************
1947 * EDIT_MoveEnd
1950 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1952 BOOL after_wrap = FALSE;
1953 INT e;
1955 /* Pass a high value in x to make sure of receiving the end of the line */
1956 if (!ctrl && (es->style & ES_MULTILINE))
1957 e = EDIT_CharFromPos(es, 0x3fffffff,
1958 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1959 else
1960 e = get_text_length(es);
1961 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1962 EDIT_EM_ScrollCaret(es);
1966 /*********************************************************************
1968 * EDIT_MoveForward
1971 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1973 INT e = es->selection_end;
1975 if (es->text[e]) {
1976 e++;
1977 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1978 if (es->text[e] == '\n')
1979 e++;
1980 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1981 e += 2;
1984 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1985 EDIT_EM_ScrollCaret(es);
1989 /*********************************************************************
1991 * EDIT_MoveHome
1993 * Home key: move to beginning of line.
1996 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1998 INT e;
2000 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2001 if (!ctrl && (es->style & ES_MULTILINE))
2002 e = EDIT_CharFromPos(es, -es->x_offset,
2003 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2004 else
2005 e = 0;
2006 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2007 EDIT_EM_ScrollCaret(es);
2011 /*********************************************************************
2013 * EDIT_MovePageDown_ML
2015 * Only for multi line controls
2016 * Move the caret one page down, on a column with the nearest
2017 * x coordinate on the screen (might be a different column).
2020 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2022 INT s = es->selection_start;
2023 INT e = es->selection_end;
2024 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2025 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2026 INT x = (short)LOWORD(pos);
2027 INT y = (short)HIWORD(pos);
2029 e = EDIT_CharFromPos(es, x,
2030 y + (es->format_rect.bottom - es->format_rect.top),
2031 &after_wrap);
2032 if (!extend)
2033 s = e;
2034 EDIT_EM_SetSel(es, s, e, after_wrap);
2035 EDIT_EM_ScrollCaret(es);
2039 /*********************************************************************
2041 * EDIT_MovePageUp_ML
2043 * Only for multi line controls
2044 * Move the caret one page up, on a column with the nearest
2045 * x coordinate on the screen (might be a different column).
2048 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2050 INT s = es->selection_start;
2051 INT e = es->selection_end;
2052 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2053 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2054 INT x = (short)LOWORD(pos);
2055 INT y = (short)HIWORD(pos);
2057 e = EDIT_CharFromPos(es, x,
2058 y - (es->format_rect.bottom - es->format_rect.top),
2059 &after_wrap);
2060 if (!extend)
2061 s = e;
2062 EDIT_EM_SetSel(es, s, e, after_wrap);
2063 EDIT_EM_ScrollCaret(es);
2067 /*********************************************************************
2069 * EDIT_MoveUp_ML
2071 * Only for multi line controls
2072 * Move the caret one line up, on a column with the nearest
2073 * x coordinate on the screen (might be a different column).
2076 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2078 INT s = es->selection_start;
2079 INT e = es->selection_end;
2080 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2081 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2082 INT x = (short)LOWORD(pos);
2083 INT y = (short)HIWORD(pos);
2085 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2086 if (!extend)
2087 s = e;
2088 EDIT_EM_SetSel(es, s, e, after_wrap);
2089 EDIT_EM_ScrollCaret(es);
2093 /*********************************************************************
2095 * EDIT_MoveWordBackward
2098 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2100 INT s = es->selection_start;
2101 INT e = es->selection_end;
2102 INT l;
2103 INT ll;
2104 INT li;
2106 l = EDIT_EM_LineFromChar(es, e);
2107 ll = EDIT_EM_LineLength(es, e);
2108 li = EDIT_EM_LineIndex(es, l);
2109 if (e - li == 0) {
2110 if (l) {
2111 li = EDIT_EM_LineIndex(es, l - 1);
2112 e = li + EDIT_EM_LineLength(es, li);
2114 } else {
2115 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2117 if (!extend)
2118 s = e;
2119 EDIT_EM_SetSel(es, s, e, FALSE);
2120 EDIT_EM_ScrollCaret(es);
2124 /*********************************************************************
2126 * EDIT_MoveWordForward
2129 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2131 INT s = es->selection_start;
2132 INT e = es->selection_end;
2133 INT l;
2134 INT ll;
2135 INT li;
2137 l = EDIT_EM_LineFromChar(es, e);
2138 ll = EDIT_EM_LineLength(es, e);
2139 li = EDIT_EM_LineIndex(es, l);
2140 if (e - li == ll) {
2141 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2142 e = EDIT_EM_LineIndex(es, l + 1);
2143 } else {
2144 e = li + EDIT_CallWordBreakProc(es,
2145 li, e - li + 1, ll, WB_RIGHT);
2147 if (!extend)
2148 s = e;
2149 EDIT_EM_SetSel(es, s, e, FALSE);
2150 EDIT_EM_ScrollCaret(es);
2154 /*********************************************************************
2156 * EDIT_PaintText
2159 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2161 COLORREF BkColor;
2162 COLORREF TextColor;
2163 LOGFONTW underline_font;
2164 HFONT hUnderline = 0;
2165 HFONT old_font = 0;
2166 INT ret;
2167 INT li;
2168 INT BkMode;
2169 SIZE size;
2171 if (!count)
2172 return 0;
2173 BkMode = GetBkMode(dc);
2174 BkColor = GetBkColor(dc);
2175 TextColor = GetTextColor(dc);
2176 if (rev) {
2177 if (es->composition_len == 0)
2179 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2180 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2181 SetBkMode( dc, OPAQUE);
2183 else
2185 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2186 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2187 underline_font.lfUnderline = TRUE;
2188 hUnderline = CreateFontIndirectW(&underline_font);
2189 old_font = SelectObject(dc,hUnderline);
2192 li = EDIT_EM_LineIndex(es, line);
2193 if (es->style & ES_MULTILINE) {
2194 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2195 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2196 } else {
2197 TextOutW(dc, x, y, es->text + li + col, count);
2198 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2199 ret = size.cx;
2201 if (rev) {
2202 if (es->composition_len == 0)
2204 SetBkColor(dc, BkColor);
2205 SetTextColor(dc, TextColor);
2206 SetBkMode( dc, BkMode);
2208 else
2210 if (old_font)
2211 SelectObject(dc,old_font);
2212 if (hUnderline)
2213 DeleteObject(hUnderline);
2216 return ret;
2220 /*********************************************************************
2222 * EDIT_PaintLine
2225 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2227 INT s = 0;
2228 INT e = 0;
2229 INT li = 0;
2230 INT ll = 0;
2231 INT x;
2232 INT y;
2233 LRESULT pos;
2234 SCRIPT_STRING_ANALYSIS ssa;
2236 if (es->style & ES_MULTILINE) {
2237 INT vlc = get_vertical_line_count(es);
2239 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2240 return;
2241 } else if (line)
2242 return;
2244 TRACE("line=%d\n", line);
2246 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2247 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2248 x = (short)LOWORD(pos);
2249 y = (short)HIWORD(pos);
2251 if (es->style & ES_MULTILINE)
2253 int line_idx = line;
2254 x = -es->x_offset;
2255 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2257 LINEDEF *line_def = es->first_line_def;
2258 int w, lw;
2260 while (line_def && line_idx)
2262 line_def = line_def->next;
2263 line_idx--;
2265 w = es->format_rect.right - es->format_rect.left;
2266 lw = line_def->width;
2268 if (es->style & ES_RIGHT)
2269 x = w - (lw - x);
2270 else if (es->style & ES_CENTER)
2271 x += (w - lw) / 2;
2273 x += es->format_rect.left;
2276 if (rev)
2278 li = EDIT_EM_LineIndex(es, line);
2279 ll = EDIT_EM_LineLength(es, li);
2280 s = min(es->selection_start, es->selection_end);
2281 e = max(es->selection_start, es->selection_end);
2282 s = min(li + ll, max(li, s));
2283 e = min(li + ll, max(li, e));
2286 if (ssa)
2287 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2288 else if (rev && (s != e) &&
2289 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2290 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2291 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2292 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2293 } else
2294 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2298 /*********************************************************************
2300 * EDIT_AdjustFormatRect
2302 * Adjusts the format rectangle for the current font and the
2303 * current client rectangle.
2306 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2308 RECT ClientRect;
2310 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2311 if (es->style & ES_MULTILINE)
2313 INT fw, vlc, max_x_offset, max_y_offset;
2315 vlc = get_vertical_line_count(es);
2316 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2318 /* correct es->x_offset */
2319 fw = es->format_rect.right - es->format_rect.left;
2320 max_x_offset = es->text_width - fw;
2321 if(max_x_offset < 0) max_x_offset = 0;
2322 if(es->x_offset > max_x_offset)
2323 es->x_offset = max_x_offset;
2325 /* correct es->y_offset */
2326 max_y_offset = es->line_count - vlc;
2327 if(max_y_offset < 0) max_y_offset = 0;
2328 if(es->y_offset > max_y_offset)
2329 es->y_offset = max_y_offset;
2331 /* force scroll info update */
2332 EDIT_UpdateScrollInfo(es);
2334 else
2335 /* Windows doesn't care to fix text placement for SL controls */
2336 es->format_rect.bottom = es->format_rect.top + es->line_height;
2338 /* Always stay within the client area */
2339 GetClientRect(es->hwndSelf, &ClientRect);
2340 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2342 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2343 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2345 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2349 /*********************************************************************
2351 * EDIT_SetRectNP
2353 * note: this is not (exactly) the handler called on EM_SETRECTNP
2354 * it is also used to set the rect of a single line control
2357 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2359 LONG_PTR ExStyle;
2360 INT bw, bh;
2361 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2363 CopyRect(&es->format_rect, rc);
2365 if (ExStyle & WS_EX_CLIENTEDGE) {
2366 es->format_rect.left++;
2367 es->format_rect.right--;
2369 if (es->format_rect.bottom - es->format_rect.top
2370 >= es->line_height + 2)
2372 es->format_rect.top++;
2373 es->format_rect.bottom--;
2376 else if (es->style & WS_BORDER) {
2377 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2378 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2379 es->format_rect.left += bw;
2380 es->format_rect.right -= bw;
2381 if (es->format_rect.bottom - es->format_rect.top
2382 >= es->line_height + 2 * bh)
2384 es->format_rect.top += bh;
2385 es->format_rect.bottom -= bh;
2389 es->format_rect.left += es->left_margin;
2390 es->format_rect.right -= es->right_margin;
2391 EDIT_AdjustFormatRect(es);
2395 /*********************************************************************
2397 * EM_CHARFROMPOS
2399 * returns line number (not index) in high-order word of result.
2400 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2401 * to Richedit, not to the edit control. Original documentation is valid.
2402 * FIXME: do the specs mean to return -1 if outside client area or
2403 * if outside formatting rectangle ???
2406 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2408 POINT pt;
2409 RECT rc;
2410 INT index;
2412 pt.x = x;
2413 pt.y = y;
2414 GetClientRect(es->hwndSelf, &rc);
2415 if (!PtInRect(&rc, pt))
2416 return -1;
2418 index = EDIT_CharFromPos(es, x, y, NULL);
2419 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2423 /*********************************************************************
2425 * EM_FMTLINES
2427 * Enable or disable soft breaks.
2429 * This means: insert or remove the soft linebreak character (\r\r\n).
2430 * Take care to check if the text still fits the buffer after insertion.
2431 * If not, notify with EN_ERRSPACE.
2434 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2436 es->flags &= ~EF_USE_SOFTBRK;
2437 if (add_eol) {
2438 es->flags |= EF_USE_SOFTBRK;
2439 FIXME("soft break enabled, not implemented\n");
2441 return add_eol;
2445 /*********************************************************************
2447 * EM_GETHANDLE
2449 * Hopefully this won't fire back at us.
2450 * We always start with a fixed buffer in the local heap.
2451 * Despite of the documentation says that the local heap is used
2452 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2453 * buffer on the local heap.
2456 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2458 HLOCAL hLocal;
2460 if (!(es->style & ES_MULTILINE))
2461 return 0;
2463 if(es->is_unicode)
2464 hLocal = es->hloc32W;
2465 else
2467 if(!es->hloc32A)
2469 CHAR *textA;
2470 UINT countA, alloc_size;
2471 TRACE("Allocating 32-bit ANSI alias buffer\n");
2472 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2473 alloc_size = ROUND_TO_GROW(countA);
2474 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2476 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2477 return 0;
2479 textA = LocalLock(es->hloc32A);
2480 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2481 LocalUnlock(es->hloc32A);
2483 hLocal = es->hloc32A;
2486 EDIT_UnlockBuffer(es, TRUE);
2488 /* The text buffer handle belongs to the app */
2489 es->hlocapp = hLocal;
2491 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2492 return hLocal;
2496 /*********************************************************************
2498 * EM_GETLINE
2501 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2503 LPWSTR src;
2504 INT line_len, dst_len;
2505 INT i;
2507 if (es->style & ES_MULTILINE) {
2508 if (line >= es->line_count)
2509 return 0;
2510 } else
2511 line = 0;
2512 i = EDIT_EM_LineIndex(es, line);
2513 src = es->text + i;
2514 line_len = EDIT_EM_LineLength(es, i);
2515 dst_len = *(WORD *)dst;
2516 if(unicode)
2518 if(dst_len <= line_len)
2520 memcpy(dst, src, dst_len * sizeof(WCHAR));
2521 return dst_len;
2523 else /* Append 0 if enough space */
2525 memcpy(dst, src, line_len * sizeof(WCHAR));
2526 dst[line_len] = 0;
2527 return line_len;
2530 else
2532 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2533 if(!ret && line_len) /* Insufficient buffer size */
2534 return dst_len;
2535 if(ret < dst_len) /* Append 0 if enough space */
2536 ((LPSTR)dst)[ret] = 0;
2537 return ret;
2542 /*********************************************************************
2544 * EM_GETSEL
2547 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2549 UINT s = es->selection_start;
2550 UINT e = es->selection_end;
2552 ORDER_UINT(s, e);
2553 if (start)
2554 *start = s;
2555 if (end)
2556 *end = e;
2557 return MAKELONG(s, e);
2561 /*********************************************************************
2563 * EM_REPLACESEL
2565 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2568 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2570 UINT strl = strlenW(lpsz_replace);
2571 UINT tl = get_text_length(es);
2572 UINT utl;
2573 UINT s;
2574 UINT e;
2575 UINT i;
2576 UINT size;
2577 LPWSTR p;
2578 HRGN hrgn = 0;
2579 LPWSTR buf = NULL;
2580 UINT bufl;
2582 TRACE("%s, can_undo %d, send_update %d\n",
2583 debugstr_w(lpsz_replace), can_undo, send_update);
2585 s = es->selection_start;
2586 e = es->selection_end;
2588 EDIT_InvalidateUniscribeData(es);
2589 if ((s == e) && !strl)
2590 return;
2592 ORDER_UINT(s, e);
2594 size = tl - (e - s) + strl;
2595 if (!size)
2596 es->text_width = 0;
2598 /* Issue the EN_MAXTEXT notification and continue with replacing text
2599 * so that buffer limit is honored. */
2600 if ((honor_limit) && (size > es->buffer_limit)) {
2601 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2602 /* Buffer limit can be smaller than the actual length of text in combobox */
2603 if (es->buffer_limit < (tl - (e-s)))
2604 strl = 0;
2605 else
2606 strl = es->buffer_limit - (tl - (e-s));
2609 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2610 return;
2612 if (e != s) {
2613 /* there is something to be deleted */
2614 TRACE("deleting stuff.\n");
2615 bufl = e - s;
2616 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2617 if (!buf) return;
2618 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2619 buf[bufl] = 0; /* ensure 0 termination */
2620 /* now delete */
2621 strcpyW(es->text + s, es->text + e);
2622 text_buffer_changed(es);
2624 if (strl) {
2625 /* there is an insertion */
2626 tl = get_text_length(es);
2627 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));
2628 for (p = es->text + tl ; p >= es->text + s ; p--)
2629 p[strl] = p[0];
2630 for (i = 0 , p = es->text + s ; i < strl ; i++)
2631 p[i] = lpsz_replace[i];
2632 if(es->style & ES_UPPERCASE)
2633 CharUpperBuffW(p, strl);
2634 else if(es->style & ES_LOWERCASE)
2635 CharLowerBuffW(p, strl);
2636 text_buffer_changed(es);
2638 if (es->style & ES_MULTILINE)
2640 INT st = min(es->selection_start, es->selection_end);
2641 INT vlc = get_vertical_line_count(es);
2643 hrgn = CreateRectRgn(0, 0, 0, 0);
2644 EDIT_BuildLineDefs_ML(es, st, st + strl,
2645 strl - abs(es->selection_end - es->selection_start), hrgn);
2646 /* if text is too long undo all changes */
2647 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2648 if (strl)
2649 strcpyW(es->text + e, es->text + e + strl);
2650 if (e != s)
2651 for (i = 0 , p = es->text ; i < e - s ; i++)
2652 p[i + s] = buf[i];
2653 text_buffer_changed(es);
2654 EDIT_BuildLineDefs_ML(es, s, e,
2655 abs(es->selection_end - es->selection_start) - strl, hrgn);
2656 strl = 0;
2657 e = s;
2658 hrgn = CreateRectRgn(0, 0, 0, 0);
2659 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2662 else {
2663 INT fw = es->format_rect.right - es->format_rect.left;
2664 EDIT_InvalidateUniscribeData(es);
2665 EDIT_CalcLineWidth_SL(es);
2666 /* remove chars that don't fit */
2667 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2668 while ((es->text_width > fw) && s + strl >= s) {
2669 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2670 strl--;
2671 es->text_length = -1;
2672 EDIT_InvalidateUniscribeData(es);
2673 EDIT_CalcLineWidth_SL(es);
2675 text_buffer_changed(es);
2676 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2680 if (e != s) {
2681 if (can_undo) {
2682 utl = strlenW(es->undo_text);
2683 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2684 /* undo-buffer is extended to the right */
2685 EDIT_MakeUndoFit(es, utl + e - s);
2686 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2687 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2688 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2689 /* undo-buffer is extended to the left */
2690 EDIT_MakeUndoFit(es, utl + e - s);
2691 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2692 p[e - s] = p[0];
2693 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2694 p[i] = buf[i];
2695 es->undo_position = s;
2696 } else {
2697 /* new undo-buffer */
2698 EDIT_MakeUndoFit(es, e - s);
2699 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2700 es->undo_text[e - s] = 0; /* ensure 0 termination */
2701 es->undo_position = s;
2703 /* any deletion makes the old insertion-undo invalid */
2704 es->undo_insert_count = 0;
2705 } else
2706 EDIT_EM_EmptyUndoBuffer(es);
2708 if (strl) {
2709 if (can_undo) {
2710 if ((s == es->undo_position) ||
2711 ((es->undo_insert_count) &&
2712 (s == es->undo_position + es->undo_insert_count)))
2714 * insertion is new and at delete position or
2715 * an extension to either left or right
2717 es->undo_insert_count += strl;
2718 else {
2719 /* new insertion undo */
2720 es->undo_position = s;
2721 es->undo_insert_count = strl;
2722 /* new insertion makes old delete-buffer invalid */
2723 *es->undo_text = '\0';
2725 } else
2726 EDIT_EM_EmptyUndoBuffer(es);
2729 HeapFree(GetProcessHeap(), 0, buf);
2731 s += strl;
2733 /* If text has been deleted and we're right or center aligned then scroll rightward */
2734 if (es->style & (ES_RIGHT | ES_CENTER))
2736 INT delta = strl - abs(es->selection_end - es->selection_start);
2738 if (delta < 0 && es->x_offset)
2740 if (abs(delta) > es->x_offset)
2741 es->x_offset = 0;
2742 else
2743 es->x_offset += delta;
2747 EDIT_EM_SetSel(es, s, s, FALSE);
2748 es->flags |= EF_MODIFIED;
2749 if (send_update) es->flags |= EF_UPDATE;
2750 if (hrgn)
2752 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2753 DeleteObject(hrgn);
2755 else
2756 EDIT_UpdateText(es, NULL, TRUE);
2758 EDIT_EM_ScrollCaret(es);
2760 /* force scroll info update */
2761 EDIT_UpdateScrollInfo(es);
2764 if(send_update || (es->flags & EF_UPDATE))
2766 es->flags &= ~EF_UPDATE;
2767 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2769 EDIT_InvalidateUniscribeData(es);
2773 /*********************************************************************
2775 * EM_SETHANDLE
2777 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2780 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2782 if (!(es->style & ES_MULTILINE))
2783 return;
2785 if (!hloc) {
2786 WARN("called with NULL handle\n");
2787 return;
2790 EDIT_UnlockBuffer(es, TRUE);
2792 if(es->is_unicode)
2794 if(es->hloc32A)
2796 LocalFree(es->hloc32A);
2797 es->hloc32A = NULL;
2799 es->hloc32W = hloc;
2801 else
2803 INT countW, countA;
2804 HLOCAL hloc32W_new;
2805 WCHAR *textW;
2806 CHAR *textA;
2808 countA = LocalSize(hloc);
2809 textA = LocalLock(hloc);
2810 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2811 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2813 ERR("Could not allocate new unicode buffer\n");
2814 return;
2816 textW = LocalLock(hloc32W_new);
2817 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2818 LocalUnlock(hloc32W_new);
2819 LocalUnlock(hloc);
2821 if(es->hloc32W)
2822 LocalFree(es->hloc32W);
2824 es->hloc32W = hloc32W_new;
2825 es->hloc32A = hloc;
2828 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2830 /* The text buffer handle belongs to the control */
2831 es->hlocapp = NULL;
2833 EDIT_LockBuffer(es);
2834 text_buffer_changed(es);
2836 es->x_offset = es->y_offset = 0;
2837 es->selection_start = es->selection_end = 0;
2838 EDIT_EM_EmptyUndoBuffer(es);
2839 es->flags &= ~EF_MODIFIED;
2840 es->flags &= ~EF_UPDATE;
2841 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2842 EDIT_UpdateText(es, NULL, TRUE);
2843 EDIT_EM_ScrollCaret(es);
2844 /* force scroll info update */
2845 EDIT_UpdateScrollInfo(es);
2849 /*********************************************************************
2851 * EM_SETLIMITTEXT
2853 * NOTE: this version currently implements WinNT limits
2856 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2858 if (!limit) limit = ~0u;
2859 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2860 es->buffer_limit = limit;
2864 /*********************************************************************
2866 * EM_SETMARGINS
2868 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2869 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2870 * margin according to the textmetrics of the current font.
2872 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2873 * of the char's width as the margin, but this is not how Windows handles this.
2874 * For all other fonts Windows sets the margins to zero.
2876 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2877 * edit control is equal to or larger than a certain size.
2878 * Interestingly if one subtracts both the left and right margins from
2879 * this size one always seems to get an even number. The extents of
2880 * the (four character) string "'**'" match this quite closely, so
2881 * we'll use this until we come up with a better idea.
2883 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2885 static const WCHAR magic_string[] = {'\'','*','*','\'', 0};
2886 SIZE sz;
2888 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2889 return sz.cx + left + right;
2892 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2893 WORD left, WORD right, BOOL repaint)
2895 TEXTMETRICW tm;
2896 INT default_left_margin = 0; /* in pixels */
2897 INT default_right_margin = 0; /* in pixels */
2899 /* Set the default margins depending on the font */
2900 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2901 HDC dc = GetDC(es->hwndSelf);
2902 HFONT old_font = SelectObject(dc, es->font);
2903 GetTextMetricsW(dc, &tm);
2904 /* The default margins are only non zero for TrueType or Vector fonts */
2905 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2906 int min_size;
2907 RECT rc;
2908 /* This must be calculated more exactly! But how? */
2909 default_left_margin = tm.tmAveCharWidth / 2;
2910 default_right_margin = tm.tmAveCharWidth / 2;
2911 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2912 GetClientRect(es->hwndSelf, &rc);
2913 if (!IsRectEmpty(&rc) && (rc.right - rc.left < min_size)) {
2914 default_left_margin = es->left_margin;
2915 default_right_margin = es->right_margin;
2918 SelectObject(dc, old_font);
2919 ReleaseDC(es->hwndSelf, dc);
2922 if (action & EC_LEFTMARGIN) {
2923 es->format_rect.left -= es->left_margin;
2924 if (left != EC_USEFONTINFO)
2925 es->left_margin = left;
2926 else
2927 es->left_margin = default_left_margin;
2928 es->format_rect.left += es->left_margin;
2931 if (action & EC_RIGHTMARGIN) {
2932 es->format_rect.right += es->right_margin;
2933 if (right != EC_USEFONTINFO)
2934 es->right_margin = right;
2935 else
2936 es->right_margin = default_right_margin;
2937 es->format_rect.right -= es->right_margin;
2940 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2941 EDIT_AdjustFormatRect(es);
2942 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2945 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2949 /*********************************************************************
2951 * EM_SETPASSWORDCHAR
2954 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2956 LONG style;
2958 if (es->style & ES_MULTILINE)
2959 return;
2961 if (es->password_char == c)
2962 return;
2964 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2965 es->password_char = c;
2966 if (c) {
2967 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2968 es->style |= ES_PASSWORD;
2969 } else {
2970 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2971 es->style &= ~ES_PASSWORD;
2973 EDIT_InvalidateUniscribeData(es);
2974 EDIT_UpdateText(es, NULL, TRUE);
2978 /*********************************************************************
2980 * EM_SETTABSTOPS
2983 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2985 if (!(es->style & ES_MULTILINE))
2986 return FALSE;
2987 HeapFree(GetProcessHeap(), 0, es->tabs);
2988 es->tabs_count = count;
2989 if (!count)
2990 es->tabs = NULL;
2991 else {
2992 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2993 memcpy(es->tabs, tabs, count * sizeof(INT));
2995 EDIT_InvalidateUniscribeData(es);
2996 return TRUE;
3000 /*********************************************************************
3002 * EM_SETWORDBREAKPROC
3005 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3007 if (es->word_break_proc == wbp)
3008 return;
3010 es->word_break_proc = wbp;
3012 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3013 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3014 EDIT_UpdateText(es, NULL, TRUE);
3019 /*********************************************************************
3021 * EM_UNDO / WM_UNDO
3024 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3026 INT ulength;
3027 LPWSTR utext;
3029 /* As per MSDN spec, for a single-line edit control,
3030 the return value is always TRUE */
3031 if( es->style & ES_READONLY )
3032 return !(es->style & ES_MULTILINE);
3034 ulength = strlenW(es->undo_text);
3036 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3038 strcpyW(utext, es->undo_text);
3040 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3041 es->undo_insert_count, debugstr_w(utext));
3043 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3044 EDIT_EM_EmptyUndoBuffer(es);
3045 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3046 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3047 /* send the notification after the selection start and end are set */
3048 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3049 EDIT_EM_ScrollCaret(es);
3050 HeapFree(GetProcessHeap(), 0, utext);
3052 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3053 es->undo_insert_count, debugstr_w(es->undo_text));
3054 return TRUE;
3058 /* Helper function for WM_CHAR
3060 * According to an MSDN blog article titled "Just because you're a control
3061 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3062 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3063 * a dialog box or not.
3065 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3067 return (es->flags & EF_DIALOGMODE);
3071 /*********************************************************************
3073 * WM_PASTE
3076 static void EDIT_WM_Paste(EDITSTATE *es)
3078 HGLOBAL hsrc;
3079 LPWSTR src;
3081 /* Protect read-only edit control from modification */
3082 if(es->style & ES_READONLY)
3083 return;
3085 OpenClipboard(es->hwndSelf);
3086 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3087 src = GlobalLock(hsrc);
3088 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
3089 GlobalUnlock(hsrc);
3091 else if (es->style & ES_PASSWORD) {
3092 /* clear selected text in password edit box even with empty clipboard */
3093 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
3095 CloseClipboard();
3099 /*********************************************************************
3101 * WM_COPY
3104 static void EDIT_WM_Copy(EDITSTATE *es)
3106 INT s = min(es->selection_start, es->selection_end);
3107 INT e = max(es->selection_start, es->selection_end);
3108 HGLOBAL hdst;
3109 LPWSTR dst;
3110 DWORD len;
3112 if (e == s) return;
3114 len = e - s;
3115 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3116 dst = GlobalLock(hdst);
3117 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3118 dst[len] = 0; /* ensure 0 termination */
3119 TRACE("%s\n", debugstr_w(dst));
3120 GlobalUnlock(hdst);
3121 OpenClipboard(es->hwndSelf);
3122 EmptyClipboard();
3123 SetClipboardData(CF_UNICODETEXT, hdst);
3124 CloseClipboard();
3128 /*********************************************************************
3130 * WM_CLEAR
3133 static inline void EDIT_WM_Clear(EDITSTATE *es)
3135 /* Protect read-only edit control from modification */
3136 if(es->style & ES_READONLY)
3137 return;
3139 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
3143 /*********************************************************************
3145 * WM_CUT
3148 static inline void EDIT_WM_Cut(EDITSTATE *es)
3150 EDIT_WM_Copy(es);
3151 EDIT_WM_Clear(es);
3155 /*********************************************************************
3157 * WM_CHAR
3160 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3162 BOOL control;
3164 control = GetKeyState(VK_CONTROL) & 0x8000;
3166 switch (c) {
3167 case '\r':
3168 /* If it's not a multiline edit box, it would be ignored below.
3169 * For multiline edit without ES_WANTRETURN, we have to make a
3170 * special case.
3172 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3173 if (EDIT_IsInsideDialog(es))
3174 break;
3175 case '\n':
3176 if (es->style & ES_MULTILINE) {
3177 if (es->style & ES_READONLY) {
3178 EDIT_MoveHome(es, FALSE, FALSE);
3179 EDIT_MoveDown_ML(es, FALSE);
3180 } else {
3181 static const WCHAR cr_lfW[] = {'\r','\n',0};
3182 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3185 break;
3186 case '\t':
3187 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3189 static const WCHAR tabW[] = {'\t',0};
3190 if (EDIT_IsInsideDialog(es))
3191 break;
3192 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3194 break;
3195 case VK_BACK:
3196 if (!(es->style & ES_READONLY) && !control) {
3197 if (es->selection_start != es->selection_end)
3198 EDIT_WM_Clear(es);
3199 else {
3200 /* delete character left of caret */
3201 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3202 EDIT_MoveBackward(es, TRUE);
3203 EDIT_WM_Clear(es);
3206 break;
3207 case 0x03: /* ^C */
3208 if (!(es->style & ES_PASSWORD))
3209 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3210 break;
3211 case 0x16: /* ^V */
3212 if (!(es->style & ES_READONLY))
3213 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3214 break;
3215 case 0x18: /* ^X */
3216 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3217 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3218 break;
3219 case 0x1A: /* ^Z */
3220 if (!(es->style & ES_READONLY))
3221 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3222 break;
3224 default:
3225 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3226 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3227 break;
3229 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3230 WCHAR str[2];
3231 str[0] = c;
3232 str[1] = '\0';
3233 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3235 break;
3237 return 1;
3241 /*********************************************************************
3243 * EDIT_ContextMenuCommand
3246 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3248 switch (id) {
3249 case EM_UNDO:
3250 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3251 break;
3252 case WM_CUT:
3253 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3254 break;
3255 case WM_COPY:
3256 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3257 break;
3258 case WM_PASTE:
3259 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3260 break;
3261 case WM_CLEAR:
3262 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3263 break;
3264 case EM_SETSEL:
3265 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3266 break;
3267 default:
3268 ERR("unknown menu item, please report\n");
3269 break;
3274 /*********************************************************************
3276 * WM_CONTEXTMENU
3278 * Note: the resource files resource/sysres_??.rc cannot define a
3279 * single popup menu. Hence we use a (dummy) menubar
3280 * containing the single popup menu as its first item.
3282 * FIXME: the message identifiers have been chosen arbitrarily,
3283 * hence we use MF_BYPOSITION.
3284 * We might as well use the "real" values (anybody knows ?)
3285 * The menu definition is in resources/sysres_??.rc.
3286 * Once these are OK, we better use MF_BYCOMMAND here
3287 * (as we do in EDIT_WM_Command()).
3290 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3292 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3293 HMENU popup = GetSubMenu(menu, 0);
3294 UINT start = es->selection_start;
3295 UINT end = es->selection_end;
3296 UINT cmd;
3298 ORDER_UINT(start, end);
3300 /* undo */
3301 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3302 /* cut */
3303 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3304 /* copy */
3305 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3306 /* paste */
3307 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3308 /* delete */
3309 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3310 /* select all */
3311 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3313 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3315 RECT rc;
3316 /* Windows places the menu at the edit's center in this case */
3317 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3318 x = rc.left + (rc.right - rc.left) / 2;
3319 y = rc.top + (rc.bottom - rc.top) / 2;
3322 if (!(es->flags & EF_FOCUSED))
3323 SetFocus(es->hwndSelf);
3325 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3326 x, y, 0, es->hwndSelf, NULL);
3328 if (cmd)
3329 EDIT_ContextMenuCommand(es, cmd);
3331 DestroyMenu(menu);
3335 /*********************************************************************
3337 * WM_GETTEXT
3340 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3342 if(!count) return 0;
3344 if(unicode)
3346 lstrcpynW(dst, es->text, count);
3347 return strlenW(dst);
3349 else
3351 LPSTR textA = (LPSTR)dst;
3352 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3353 textA[count - 1] = 0; /* ensure 0 termination */
3354 return strlen(textA);
3358 /*********************************************************************
3360 * EDIT_CheckCombo
3363 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3365 HWND hLBox = es->hwndListBox;
3366 HWND hCombo;
3367 BOOL bDropped;
3368 int nEUI;
3370 if (!hLBox)
3371 return FALSE;
3373 hCombo = GetParent(es->hwndSelf);
3374 bDropped = TRUE;
3375 nEUI = 0;
3377 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3379 if (key == VK_UP || key == VK_DOWN)
3381 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3382 nEUI = 1;
3384 if (msg == WM_KEYDOWN || nEUI)
3385 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3388 switch (msg)
3390 case WM_KEYDOWN:
3391 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3393 /* make sure ComboLBox pops up */
3394 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3395 key = VK_F4;
3396 nEUI = 2;
3399 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3400 break;
3402 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3403 if (nEUI)
3404 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3405 else
3406 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3407 break;
3410 if(nEUI == 2)
3411 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3413 return TRUE;
3417 /*********************************************************************
3419 * WM_KEYDOWN
3421 * Handling of special keys that don't produce a WM_CHAR
3422 * (i.e. non-printable keys) & Backspace & Delete
3425 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3427 BOOL shift;
3428 BOOL control;
3430 if (GetKeyState(VK_MENU) & 0x8000)
3431 return 0;
3433 shift = GetKeyState(VK_SHIFT) & 0x8000;
3434 control = GetKeyState(VK_CONTROL) & 0x8000;
3436 switch (key) {
3437 case VK_F4:
3438 case VK_UP:
3439 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3440 break;
3442 /* fall through */
3443 case VK_LEFT:
3444 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3445 EDIT_MoveUp_ML(es, shift);
3446 else
3447 if (control)
3448 EDIT_MoveWordBackward(es, shift);
3449 else
3450 EDIT_MoveBackward(es, shift);
3451 break;
3452 case VK_DOWN:
3453 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3454 break;
3455 /* fall through */
3456 case VK_RIGHT:
3457 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3458 EDIT_MoveDown_ML(es, shift);
3459 else if (control)
3460 EDIT_MoveWordForward(es, shift);
3461 else
3462 EDIT_MoveForward(es, shift);
3463 break;
3464 case VK_HOME:
3465 EDIT_MoveHome(es, shift, control);
3466 break;
3467 case VK_END:
3468 EDIT_MoveEnd(es, shift, control);
3469 break;
3470 case VK_PRIOR:
3471 if (es->style & ES_MULTILINE)
3472 EDIT_MovePageUp_ML(es, shift);
3473 else
3474 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3475 break;
3476 case VK_NEXT:
3477 if (es->style & ES_MULTILINE)
3478 EDIT_MovePageDown_ML(es, shift);
3479 else
3480 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3481 break;
3482 case VK_DELETE:
3483 if (!(es->style & ES_READONLY) && !(shift && control)) {
3484 if (es->selection_start != es->selection_end) {
3485 if (shift)
3486 EDIT_WM_Cut(es);
3487 else
3488 EDIT_WM_Clear(es);
3489 } else {
3490 if (shift) {
3491 /* delete character left of caret */
3492 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3493 EDIT_MoveBackward(es, TRUE);
3494 EDIT_WM_Clear(es);
3495 } else if (control) {
3496 /* delete to end of line */
3497 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3498 EDIT_MoveEnd(es, TRUE, FALSE);
3499 EDIT_WM_Clear(es);
3500 } else {
3501 /* delete character right of caret */
3502 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3503 EDIT_MoveForward(es, TRUE);
3504 EDIT_WM_Clear(es);
3508 break;
3509 case VK_INSERT:
3510 if (shift) {
3511 if (!(es->style & ES_READONLY))
3512 EDIT_WM_Paste(es);
3513 } else if (control)
3514 EDIT_WM_Copy(es);
3515 break;
3516 case VK_RETURN:
3517 /* If the edit doesn't want the return send a message to the default object */
3518 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3520 DWORD dw;
3522 if (!EDIT_IsInsideDialog(es)) break;
3523 if (control) break;
3524 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3525 if (HIWORD(dw) == DC_HASDEFID)
3527 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3528 if (hwDefCtrl)
3530 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3531 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3535 break;
3536 case VK_ESCAPE:
3537 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3538 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3539 break;
3540 case VK_TAB:
3541 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3542 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3543 break;
3545 return TRUE;
3549 /*********************************************************************
3551 * WM_KILLFOCUS
3554 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3556 es->flags &= ~EF_FOCUSED;
3557 DestroyCaret();
3558 if(!(es->style & ES_NOHIDESEL))
3559 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3560 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3561 /* throw away left over scroll when we lose focus */
3562 es->wheelDeltaRemainder = 0;
3563 return 0;
3567 /*********************************************************************
3569 * WM_LBUTTONDBLCLK
3571 * The caret position has been set on the WM_LBUTTONDOWN message
3574 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3576 INT s;
3577 INT e = es->selection_end;
3578 INT l;
3579 INT li;
3580 INT ll;
3582 es->bCaptureState = TRUE;
3583 SetCapture(es->hwndSelf);
3585 l = EDIT_EM_LineFromChar(es, e);
3586 li = EDIT_EM_LineIndex(es, l);
3587 ll = EDIT_EM_LineLength(es, e);
3588 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3589 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3590 EDIT_EM_SetSel(es, s, e, FALSE);
3591 EDIT_EM_ScrollCaret(es);
3592 es->region_posx = es->region_posy = 0;
3593 SetTimer(es->hwndSelf, 0, 100, NULL);
3594 return 0;
3598 /*********************************************************************
3600 * WM_LBUTTONDOWN
3603 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3605 INT e;
3606 BOOL after_wrap;
3608 es->bCaptureState = TRUE;
3609 SetCapture(es->hwndSelf);
3610 EDIT_ConfinePoint(es, &x, &y);
3611 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3612 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3613 EDIT_EM_ScrollCaret(es);
3614 es->region_posx = es->region_posy = 0;
3615 SetTimer(es->hwndSelf, 0, 100, NULL);
3617 if (!(es->flags & EF_FOCUSED))
3618 SetFocus(es->hwndSelf);
3620 return 0;
3624 /*********************************************************************
3626 * WM_LBUTTONUP
3629 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3631 if (es->bCaptureState) {
3632 KillTimer(es->hwndSelf, 0);
3633 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3635 es->bCaptureState = FALSE;
3636 return 0;
3640 /*********************************************************************
3642 * WM_MBUTTONDOWN
3645 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3647 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3648 return 0;
3652 /*********************************************************************
3654 * WM_MOUSEMOVE
3657 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3659 INT e;
3660 BOOL after_wrap;
3661 INT prex, prey;
3663 /* If the mouse has been captured by process other than the edit control itself,
3664 * the windows edit controls will not select the strings with mouse move.
3666 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3667 return 0;
3670 * FIXME: gotta do some scrolling if outside client
3671 * area. Maybe reset the timer ?
3673 prex = x; prey = y;
3674 EDIT_ConfinePoint(es, &x, &y);
3675 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3676 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3677 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3678 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3679 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3680 return 0;
3684 /*********************************************************************
3686 * WM_PAINT
3689 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3691 PAINTSTRUCT ps;
3692 INT i;
3693 HDC dc;
3694 HFONT old_font = 0;
3695 RECT rc;
3696 RECT rcClient;
3697 RECT rcLine;
3698 RECT rcRgn;
3699 HBRUSH brush;
3700 HBRUSH old_brush;
3701 INT bw, bh;
3702 BOOL rev = es->bEnableState &&
3703 ((es->flags & EF_FOCUSED) ||
3704 (es->style & ES_NOHIDESEL));
3705 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3707 /* The dc we use for calculating may not be the one we paint into.
3708 This is the safest action. */
3709 EDIT_InvalidateUniscribeData(es);
3710 GetClientRect(es->hwndSelf, &rcClient);
3712 /* get the background brush */
3713 brush = EDIT_NotifyCtlColor(es, dc);
3715 /* paint the border and the background */
3716 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3718 if(es->style & WS_BORDER) {
3719 bw = GetSystemMetrics(SM_CXBORDER);
3720 bh = GetSystemMetrics(SM_CYBORDER);
3721 rc = rcClient;
3722 if(es->style & ES_MULTILINE) {
3723 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3724 if(es->style & WS_VSCROLL) rc.right+=bw;
3727 /* Draw the frame. Same code as in nonclient.c */
3728 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3729 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3730 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3731 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3732 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3733 SelectObject(dc, old_brush);
3735 /* Keep the border clean */
3736 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3737 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3740 GetClipBox(dc, &rc);
3741 FillRect(dc, &rc, brush);
3743 IntersectClipRect(dc, es->format_rect.left,
3744 es->format_rect.top,
3745 es->format_rect.right,
3746 es->format_rect.bottom);
3747 if (es->style & ES_MULTILINE) {
3748 rc = rcClient;
3749 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3751 if (es->font)
3752 old_font = SelectObject(dc, es->font);
3754 if (!es->bEnableState)
3755 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3756 GetClipBox(dc, &rcRgn);
3757 if (es->style & ES_MULTILINE) {
3758 INT vlc = get_vertical_line_count(es);
3759 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3760 EDIT_UpdateUniscribeData(es, dc, i);
3761 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3762 if (IntersectRect(&rc, &rcRgn, &rcLine))
3763 EDIT_PaintLine(es, dc, i, rev);
3765 } else {
3766 EDIT_UpdateUniscribeData(es, dc, 0);
3767 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3768 if (IntersectRect(&rc, &rcRgn, &rcLine))
3769 EDIT_PaintLine(es, dc, 0, rev);
3771 if (es->font)
3772 SelectObject(dc, old_font);
3774 if (!hdc)
3775 EndPaint(es->hwndSelf, &ps);
3779 /*********************************************************************
3781 * WM_SETFOCUS
3784 static void EDIT_WM_SetFocus(EDITSTATE *es)
3786 es->flags |= EF_FOCUSED;
3788 if (!(es->style & ES_NOHIDESEL))
3789 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3791 /* single line edit updates itself */
3792 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3794 HDC hdc = GetDC(es->hwndSelf);
3795 EDIT_WM_Paint(es, hdc);
3796 ReleaseDC(es->hwndSelf, hdc);
3799 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3800 EDIT_SetCaretPos(es, es->selection_end,
3801 es->flags & EF_AFTER_WRAP);
3802 ShowCaret(es->hwndSelf);
3803 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3807 /*********************************************************************
3809 * WM_SETFONT
3811 * With Win95 look the margins are set to default font value unless
3812 * the system font (font == 0) is being set, in which case they are left
3813 * unchanged.
3816 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3818 TEXTMETRICW tm;
3819 HDC dc;
3820 HFONT old_font = 0;
3821 RECT clientRect;
3823 es->font = font;
3824 EDIT_InvalidateUniscribeData(es);
3825 dc = GetDC(es->hwndSelf);
3826 if (font)
3827 old_font = SelectObject(dc, font);
3828 GetTextMetricsW(dc, &tm);
3829 es->line_height = tm.tmHeight;
3830 es->char_width = tm.tmAveCharWidth;
3831 if (font)
3832 SelectObject(dc, old_font);
3833 ReleaseDC(es->hwndSelf, dc);
3835 /* Reset the format rect and the margins */
3836 GetClientRect(es->hwndSelf, &clientRect);
3837 EDIT_SetRectNP(es, &clientRect);
3838 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3839 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3841 if (es->style & ES_MULTILINE)
3842 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3843 else
3844 EDIT_CalcLineWidth_SL(es);
3846 if (redraw)
3847 EDIT_UpdateText(es, NULL, TRUE);
3848 if (es->flags & EF_FOCUSED) {
3849 DestroyCaret();
3850 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3851 EDIT_SetCaretPos(es, es->selection_end,
3852 es->flags & EF_AFTER_WRAP);
3853 ShowCaret(es->hwndSelf);
3858 /*********************************************************************
3860 * WM_SETTEXT
3862 * NOTES
3863 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3864 * The modified flag is reset. No notifications are sent.
3866 * For single-line controls, reception of WM_SETTEXT triggers:
3867 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3870 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3872 LPWSTR textW = NULL;
3873 if (!unicode && text)
3875 LPCSTR textA = (LPCSTR)text;
3876 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3877 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3878 if (textW)
3879 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3880 text = textW;
3883 if (es->flags & EF_UPDATE)
3884 /* fixed this bug once; complain if we see it about to happen again. */
3885 ERR("SetSel may generate UPDATE message whose handler may reset "
3886 "selection.\n");
3888 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3889 if (text)
3891 TRACE("%s\n", debugstr_w(text));
3892 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
3893 if(!unicode)
3894 HeapFree(GetProcessHeap(), 0, textW);
3896 else
3898 TRACE("<NULL>\n");
3899 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
3901 es->x_offset = 0;
3902 es->flags &= ~EF_MODIFIED;
3903 EDIT_EM_SetSel(es, 0, 0, FALSE);
3905 /* Send the notification after the selection start and end have been set
3906 * edit control doesn't send notification on WM_SETTEXT
3907 * if it is multiline, or it is part of combobox
3909 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3911 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3912 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3914 EDIT_EM_ScrollCaret(es);
3915 EDIT_UpdateScrollInfo(es);
3916 EDIT_InvalidateUniscribeData(es);
3920 /*********************************************************************
3922 * WM_SIZE
3925 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3927 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3928 RECT rc;
3929 GetClientRect(es->hwndSelf, &rc);
3930 EDIT_SetRectNP(es, &rc);
3931 EDIT_UpdateText(es, NULL, TRUE);
3936 /*********************************************************************
3938 * WM_STYLECHANGED
3940 * This message is sent by SetWindowLong on having changed either the Style
3941 * or the extended style.
3943 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3945 * See also EDIT_WM_NCCreate
3947 * It appears that the Windows version of the edit control allows the style
3948 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3949 * style variable which will generally be different. In this function we
3950 * update the internal style based on what changed in the externally visible
3951 * style.
3953 * Much of this content as based upon the MSDN, especially:
3954 * Platform SDK Documentation -> User Interface Services ->
3955 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3956 * Edit Control Styles
3958 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3960 if (GWL_STYLE == which) {
3961 DWORD style_change_mask;
3962 DWORD new_style;
3963 /* Only a subset of changes can be applied after the control
3964 * has been created.
3966 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3967 ES_NUMBER;
3968 if (es->style & ES_MULTILINE)
3969 style_change_mask |= ES_WANTRETURN;
3971 new_style = style->styleNew & style_change_mask;
3973 /* Number overrides lowercase overrides uppercase (at least it
3974 * does in Win95). However I'll bet that ES_NUMBER would be
3975 * invalid under Win 3.1.
3977 if (new_style & ES_NUMBER) {
3978 ; /* do not override the ES_NUMBER */
3979 } else if (new_style & ES_LOWERCASE) {
3980 new_style &= ~ES_UPPERCASE;
3983 es->style = (es->style & ~style_change_mask) | new_style;
3984 } else if (GWL_EXSTYLE == which) {
3985 ; /* FIXME - what is needed here */
3986 } else {
3987 WARN ("Invalid style change %ld\n",which);
3990 return 0;
3993 /*********************************************************************
3995 * WM_SYSKEYDOWN
3998 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4000 if ((key == VK_BACK) && (key_data & 0x2000)) {
4001 if (EDIT_EM_CanUndo(es))
4002 EDIT_EM_Undo(es);
4003 return 0;
4004 } else if (key == VK_UP || key == VK_DOWN) {
4005 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4006 return 0;
4008 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4012 /*********************************************************************
4014 * WM_TIMER
4017 static void EDIT_WM_Timer(EDITSTATE *es)
4019 if (es->region_posx < 0) {
4020 EDIT_MoveBackward(es, TRUE);
4021 } else if (es->region_posx > 0) {
4022 EDIT_MoveForward(es, TRUE);
4025 * FIXME: gotta do some vertical scrolling here, like
4026 * EDIT_EM_LineScroll(hwnd, 0, 1);
4030 /*********************************************************************
4032 * WM_HSCROLL
4035 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4037 INT dx;
4038 INT fw;
4040 if (!(es->style & ES_MULTILINE))
4041 return 0;
4043 if (!(es->style & ES_AUTOHSCROLL))
4044 return 0;
4046 dx = 0;
4047 fw = es->format_rect.right - es->format_rect.left;
4048 switch (action) {
4049 case SB_LINELEFT:
4050 TRACE("SB_LINELEFT\n");
4051 if (es->x_offset)
4052 dx = -es->char_width;
4053 break;
4054 case SB_LINERIGHT:
4055 TRACE("SB_LINERIGHT\n");
4056 if (es->x_offset < es->text_width)
4057 dx = es->char_width;
4058 break;
4059 case SB_PAGELEFT:
4060 TRACE("SB_PAGELEFT\n");
4061 if (es->x_offset)
4062 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4063 break;
4064 case SB_PAGERIGHT:
4065 TRACE("SB_PAGERIGHT\n");
4066 if (es->x_offset < es->text_width)
4067 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4068 break;
4069 case SB_LEFT:
4070 TRACE("SB_LEFT\n");
4071 if (es->x_offset)
4072 dx = -es->x_offset;
4073 break;
4074 case SB_RIGHT:
4075 TRACE("SB_RIGHT\n");
4076 if (es->x_offset < es->text_width)
4077 dx = es->text_width - es->x_offset;
4078 break;
4079 case SB_THUMBTRACK:
4080 TRACE("SB_THUMBTRACK %d\n", pos);
4081 es->flags |= EF_HSCROLL_TRACK;
4082 if(es->style & WS_HSCROLL)
4083 dx = pos - es->x_offset;
4084 else
4086 INT fw, new_x;
4087 /* Sanity check */
4088 if(pos < 0 || pos > 100) return 0;
4089 /* Assume default scroll range 0-100 */
4090 fw = es->format_rect.right - es->format_rect.left;
4091 new_x = pos * (es->text_width - fw) / 100;
4092 dx = es->text_width ? (new_x - es->x_offset) : 0;
4094 break;
4095 case SB_THUMBPOSITION:
4096 TRACE("SB_THUMBPOSITION %d\n", pos);
4097 es->flags &= ~EF_HSCROLL_TRACK;
4098 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4099 dx = pos - es->x_offset;
4100 else
4102 INT fw, new_x;
4103 /* Sanity check */
4104 if(pos < 0 || pos > 100) return 0;
4105 /* Assume default scroll range 0-100 */
4106 fw = es->format_rect.right - es->format_rect.left;
4107 new_x = pos * (es->text_width - fw) / 100;
4108 dx = es->text_width ? (new_x - es->x_offset) : 0;
4110 if (!dx) {
4111 /* force scroll info update */
4112 EDIT_UpdateScrollInfo(es);
4113 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4115 break;
4116 case SB_ENDSCROLL:
4117 TRACE("SB_ENDSCROLL\n");
4118 break;
4120 * FIXME : the next two are undocumented !
4121 * Are we doing the right thing ?
4122 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4123 * although it's also a regular control message.
4125 case EM_GETTHUMB: /* this one is used by NT notepad */
4127 LRESULT ret;
4128 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4129 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4130 else
4132 /* Assume default scroll range 0-100 */
4133 INT fw = es->format_rect.right - es->format_rect.left;
4134 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4136 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4137 return ret;
4139 case EM_LINESCROLL:
4140 TRACE("EM_LINESCROLL16\n");
4141 dx = pos;
4142 break;
4144 default:
4145 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4146 action, action);
4147 return 0;
4149 if (dx)
4151 INT fw = es->format_rect.right - es->format_rect.left;
4152 /* check if we are going to move too far */
4153 if(es->x_offset + dx + fw > es->text_width)
4154 dx = es->text_width - fw - es->x_offset;
4155 if(dx)
4156 EDIT_EM_LineScroll_internal(es, dx, 0);
4158 return 0;
4162 /*********************************************************************
4164 * WM_VSCROLL
4167 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4169 INT dy;
4171 if (!(es->style & ES_MULTILINE))
4172 return 0;
4174 if (!(es->style & ES_AUTOVSCROLL))
4175 return 0;
4177 dy = 0;
4178 switch (action) {
4179 case SB_LINEUP:
4180 case SB_LINEDOWN:
4181 case SB_PAGEUP:
4182 case SB_PAGEDOWN:
4183 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4184 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4185 (action == SB_PAGEUP ? "SB_PAGEUP" :
4186 "SB_PAGEDOWN"))));
4187 EDIT_EM_Scroll(es, action);
4188 return 0;
4189 case SB_TOP:
4190 TRACE("SB_TOP\n");
4191 dy = -es->y_offset;
4192 break;
4193 case SB_BOTTOM:
4194 TRACE("SB_BOTTOM\n");
4195 dy = es->line_count - 1 - es->y_offset;
4196 break;
4197 case SB_THUMBTRACK:
4198 TRACE("SB_THUMBTRACK %d\n", pos);
4199 es->flags |= EF_VSCROLL_TRACK;
4200 if(es->style & WS_VSCROLL)
4201 dy = pos - es->y_offset;
4202 else
4204 /* Assume default scroll range 0-100 */
4205 INT vlc, new_y;
4206 /* Sanity check */
4207 if(pos < 0 || pos > 100) return 0;
4208 vlc = get_vertical_line_count(es);
4209 new_y = pos * (es->line_count - vlc) / 100;
4210 dy = es->line_count ? (new_y - es->y_offset) : 0;
4211 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4212 es->line_count, es->y_offset, pos, dy);
4214 break;
4215 case SB_THUMBPOSITION:
4216 TRACE("SB_THUMBPOSITION %d\n", pos);
4217 es->flags &= ~EF_VSCROLL_TRACK;
4218 if(es->style & WS_VSCROLL)
4219 dy = pos - es->y_offset;
4220 else
4222 /* Assume default scroll range 0-100 */
4223 INT vlc, new_y;
4224 /* Sanity check */
4225 if(pos < 0 || pos > 100) return 0;
4226 vlc = get_vertical_line_count(es);
4227 new_y = pos * (es->line_count - vlc) / 100;
4228 dy = es->line_count ? (new_y - es->y_offset) : 0;
4229 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4230 es->line_count, es->y_offset, pos, dy);
4232 if (!dy)
4234 /* force scroll info update */
4235 EDIT_UpdateScrollInfo(es);
4236 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4238 break;
4239 case SB_ENDSCROLL:
4240 TRACE("SB_ENDSCROLL\n");
4241 break;
4243 * FIXME : the next two are undocumented !
4244 * Are we doing the right thing ?
4245 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4246 * although it's also a regular control message.
4248 case EM_GETTHUMB: /* this one is used by NT notepad */
4250 LRESULT ret;
4251 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4252 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4253 else
4255 /* Assume default scroll range 0-100 */
4256 INT vlc = get_vertical_line_count(es);
4257 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4259 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4260 return ret;
4262 case EM_LINESCROLL:
4263 TRACE("EM_LINESCROLL %d\n", pos);
4264 dy = pos;
4265 break;
4267 default:
4268 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4269 action, action);
4270 return 0;
4272 if (dy)
4273 EDIT_EM_LineScroll(es, 0, dy);
4274 return 0;
4277 /*********************************************************************
4279 * EM_GETTHUMB
4281 * FIXME: is this right ? (or should it be only VSCROLL)
4282 * (and maybe only for edit controls that really have their
4283 * own scrollbars) (and maybe only for multiline controls ?)
4284 * All in all: very poorly documented
4287 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4289 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4290 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4294 /********************************************************************
4296 * The Following code is to handle inline editing from IMEs
4299 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4301 LONG buflen;
4302 LPWSTR lpCompStr;
4303 LPSTR lpCompStrAttr = NULL;
4304 DWORD dwBufLenAttr;
4306 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4308 if (buflen < 0)
4310 return;
4313 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
4314 if (!lpCompStr)
4316 ERR("Unable to allocate IME CompositionString\n");
4317 return;
4320 if (buflen)
4321 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4322 lpCompStr[buflen/sizeof(WCHAR)] = 0;
4324 if (CompFlag & GCS_COMPATTR)
4327 * We do not use the attributes yet. it would tell us what characters
4328 * are in transition and which are converted or decided upon
4330 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4331 if (dwBufLenAttr)
4333 dwBufLenAttr ++;
4334 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4335 if (!lpCompStrAttr)
4337 ERR("Unable to allocate IME Attribute String\n");
4338 HeapFree(GetProcessHeap(),0,lpCompStr);
4339 return;
4341 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4342 dwBufLenAttr);
4343 lpCompStrAttr[dwBufLenAttr] = 0;
4347 /* check for change in composition start */
4348 if (es->selection_end < es->composition_start)
4349 es->composition_start = es->selection_end;
4351 /* replace existing selection string */
4352 es->selection_start = es->composition_start;
4354 if (es->composition_len > 0)
4355 es->selection_end = es->composition_start + es->composition_len;
4356 else
4357 es->selection_end = es->selection_start;
4359 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
4360 es->composition_len = abs(es->composition_start - es->selection_end);
4362 es->selection_start = es->composition_start;
4363 es->selection_end = es->selection_start + es->composition_len;
4365 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4366 HeapFree(GetProcessHeap(),0,lpCompStr);
4369 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4371 LONG buflen;
4372 LPWSTR lpResultStr;
4374 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4375 if (buflen <= 0)
4377 return;
4380 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
4381 if (!lpResultStr)
4383 ERR("Unable to alloc buffer for IME string\n");
4384 return;
4387 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4388 lpResultStr[buflen/sizeof(WCHAR)] = 0;
4390 /* check for change in composition start */
4391 if (es->selection_end < es->composition_start)
4392 es->composition_start = es->selection_end;
4394 es->selection_start = es->composition_start;
4395 es->selection_end = es->composition_start + es->composition_len;
4396 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
4397 es->composition_start = es->selection_end;
4398 es->composition_len = 0;
4400 HeapFree(GetProcessHeap(),0,lpResultStr);
4403 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4405 HIMC hIMC;
4406 int cursor;
4408 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4410 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4411 es->composition_start = es->selection_end;
4414 hIMC = ImmGetContext(hwnd);
4415 if (!hIMC)
4416 return;
4418 if (CompFlag & GCS_RESULTSTR)
4420 EDIT_GetResultStr(hIMC, es);
4421 cursor = 0;
4423 else
4425 if (CompFlag & GCS_COMPSTR)
4426 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4427 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4429 ImmReleaseContext(hwnd, hIMC);
4430 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4434 /*********************************************************************
4436 * WM_NCCREATE
4438 * See also EDIT_WM_StyleChanged
4440 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4442 EDITSTATE *es;
4443 UINT alloc_size;
4445 TRACE("Creating %s edit control, style = %08x\n",
4446 unicode ? "Unicode" : "ANSI", lpcs->style);
4448 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4449 return FALSE;
4450 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4453 * Note: since the EDITSTATE has not been fully initialized yet,
4454 * we can't use any API calls that may send
4455 * WM_XXX messages before WM_NCCREATE is completed.
4458 es->is_unicode = unicode;
4459 es->style = lpcs->style;
4461 es->bEnableState = !(es->style & WS_DISABLED);
4463 es->hwndSelf = hwnd;
4464 /* Save parent, which will be notified by EN_* messages */
4465 es->hwndParent = lpcs->hwndParent;
4467 if (es->style & ES_COMBO)
4468 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4470 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4471 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4473 /* Number overrides lowercase overrides uppercase (at least it
4474 * does in Win95). However I'll bet that ES_NUMBER would be
4475 * invalid under Win 3.1.
4477 if (es->style & ES_NUMBER) {
4478 ; /* do not override the ES_NUMBER */
4479 } else if (es->style & ES_LOWERCASE) {
4480 es->style &= ~ES_UPPERCASE;
4482 if (es->style & ES_MULTILINE) {
4483 es->buffer_limit = BUFLIMIT_INITIAL;
4484 if (es->style & WS_VSCROLL)
4485 es->style |= ES_AUTOVSCROLL;
4486 if (es->style & WS_HSCROLL)
4487 es->style |= ES_AUTOHSCROLL;
4488 es->style &= ~ES_PASSWORD;
4489 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4490 /* Confirmed - RIGHT overrides CENTER */
4491 if (es->style & ES_RIGHT)
4492 es->style &= ~ES_CENTER;
4493 es->style &= ~WS_HSCROLL;
4494 es->style &= ~ES_AUTOHSCROLL;
4496 } else {
4497 es->buffer_limit = BUFLIMIT_INITIAL;
4498 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4499 es->style &= ~ES_CENTER;
4500 es->style &= ~WS_HSCROLL;
4501 es->style &= ~WS_VSCROLL;
4502 if (es->style & ES_PASSWORD)
4503 es->password_char = '*';
4506 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4507 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4508 goto cleanup;
4509 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4511 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4512 goto cleanup;
4513 es->undo_buffer_size = es->buffer_size;
4515 if (es->style & ES_MULTILINE)
4516 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4517 goto cleanup;
4518 es->line_count = 1;
4521 * In Win95 look and feel, the WS_BORDER style is replaced by the
4522 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4523 * control a nonclient area so we don't need to draw the border.
4524 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4525 * a nonclient area and we should handle painting the border ourselves.
4527 * When making modifications please ensure that the code still works
4528 * for edit controls created directly with style 0x50800000, exStyle 0
4529 * (which should have a single pixel border)
4531 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4532 es->style &= ~WS_BORDER;
4533 else if (es->style & WS_BORDER)
4534 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4536 return TRUE;
4538 cleanup:
4539 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4540 EDIT_InvalidateUniscribeData(es);
4541 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4542 HeapFree(GetProcessHeap(), 0, es->undo_text);
4543 if (es->hloc32W) LocalFree(es->hloc32W);
4544 HeapFree(GetProcessHeap(), 0, es->logAttr);
4545 HeapFree(GetProcessHeap(), 0, es);
4546 return FALSE;
4550 /*********************************************************************
4552 * WM_CREATE
4555 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4557 RECT clientRect;
4559 TRACE("%s\n", debugstr_w(name));
4561 * To initialize some final structure members, we call some helper
4562 * functions. However, since the EDITSTATE is not consistent (i.e.
4563 * not fully initialized), we should be very careful which
4564 * functions can be called, and in what order.
4566 EDIT_WM_SetFont(es, 0, FALSE);
4567 EDIT_EM_EmptyUndoBuffer(es);
4569 /* We need to calculate the format rect
4570 (applications may send EM_SETMARGINS before the control gets visible) */
4571 GetClientRect(es->hwndSelf, &clientRect);
4572 EDIT_SetRectNP(es, &clientRect);
4574 if (name && *name) {
4575 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4576 /* if we insert text to the editline, the text scrolls out
4577 * of the window, as the caret is placed after the insert
4578 * pos normally; thus we reset es->selection... to 0 and
4579 * update caret
4581 es->selection_start = es->selection_end = 0;
4582 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4583 * Messages are only to be sent when the USER does something to
4584 * change the contents. So I am removing this EN_CHANGE
4586 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4588 EDIT_EM_ScrollCaret(es);
4590 /* force scroll info update */
4591 EDIT_UpdateScrollInfo(es);
4592 /* The rule seems to return 1 here for success */
4593 /* Power Builder masked edit controls will crash */
4594 /* if not. */
4595 /* FIXME: is that in all cases so ? */
4596 return 1;
4600 /*********************************************************************
4602 * WM_NCDESTROY
4605 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4607 LINEDEF *pc, *pp;
4609 /* The app can own the text buffer handle */
4610 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4611 LocalFree(es->hloc32W);
4613 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4614 LocalFree(es->hloc32A);
4616 EDIT_InvalidateUniscribeData(es);
4617 pc = es->first_line_def;
4618 while (pc)
4620 pp = pc->next;
4621 HeapFree(GetProcessHeap(), 0, pc);
4622 pc = pp;
4625 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4626 HeapFree(GetProcessHeap(), 0, es->undo_text);
4627 HeapFree(GetProcessHeap(), 0, es);
4629 return 0;
4633 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4635 if(unicode)
4636 return DefWindowProcW(hwnd, msg, wParam, lParam);
4637 else
4638 return DefWindowProcA(hwnd, msg, wParam, lParam);
4641 /*********************************************************************
4643 * EditWndProc_common
4645 * The messages are in the order of the actual integer values
4646 * (which can be found in include/windows.h)
4648 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4650 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4651 LRESULT result = 0;
4653 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4655 if (!es && msg != WM_NCCREATE)
4656 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4658 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4660 switch (msg) {
4661 case EM_GETSEL:
4662 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4663 break;
4665 case EM_SETSEL:
4666 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4667 EDIT_EM_ScrollCaret(es);
4668 result = 1;
4669 break;
4671 case EM_GETRECT:
4672 if (lParam)
4673 CopyRect((LPRECT)lParam, &es->format_rect);
4674 break;
4676 case EM_SETRECT:
4677 if ((es->style & ES_MULTILINE) && lParam) {
4678 EDIT_SetRectNP(es, (LPRECT)lParam);
4679 EDIT_UpdateText(es, NULL, TRUE);
4681 break;
4683 case EM_SETRECTNP:
4684 if ((es->style & ES_MULTILINE) && lParam)
4685 EDIT_SetRectNP(es, (LPRECT)lParam);
4686 break;
4688 case EM_SCROLL:
4689 result = EDIT_EM_Scroll(es, (INT)wParam);
4690 break;
4692 case EM_LINESCROLL:
4693 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4694 break;
4696 case EM_SCROLLCARET:
4697 EDIT_EM_ScrollCaret(es);
4698 result = 1;
4699 break;
4701 case EM_GETMODIFY:
4702 result = ((es->flags & EF_MODIFIED) != 0);
4703 break;
4705 case EM_SETMODIFY:
4706 if (wParam)
4707 es->flags |= EF_MODIFIED;
4708 else
4709 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4710 break;
4712 case EM_GETLINECOUNT:
4713 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4714 break;
4716 case EM_LINEINDEX:
4717 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4718 break;
4720 case EM_SETHANDLE:
4721 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4722 break;
4724 case EM_GETHANDLE:
4725 result = (LRESULT)EDIT_EM_GetHandle(es);
4726 break;
4728 case EM_GETTHUMB:
4729 result = EDIT_EM_GetThumb(es);
4730 break;
4732 /* these messages missing from specs */
4733 case 0x00bf:
4734 case 0x00c0:
4735 case 0x00c3:
4736 case 0x00ca:
4737 FIXME("undocumented message 0x%x, please report\n", msg);
4738 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4739 break;
4741 case EM_LINELENGTH:
4742 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4743 break;
4745 case EM_REPLACESEL:
4747 LPWSTR textW;
4749 if(unicode)
4750 textW = (LPWSTR)lParam;
4751 else
4753 LPSTR textA = (LPSTR)lParam;
4754 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4755 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4756 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4759 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
4760 result = 1;
4762 if(!unicode)
4763 HeapFree(GetProcessHeap(), 0, textW);
4764 break;
4767 case EM_GETLINE:
4768 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4769 break;
4771 case EM_SETLIMITTEXT:
4772 EDIT_EM_SetLimitText(es, wParam);
4773 break;
4775 case EM_CANUNDO:
4776 result = (LRESULT)EDIT_EM_CanUndo(es);
4777 break;
4779 case EM_UNDO:
4780 case WM_UNDO:
4781 result = (LRESULT)EDIT_EM_Undo(es);
4782 break;
4784 case EM_FMTLINES:
4785 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4786 break;
4788 case EM_LINEFROMCHAR:
4789 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4790 break;
4792 case EM_SETTABSTOPS:
4793 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4794 break;
4796 case EM_SETPASSWORDCHAR:
4798 WCHAR charW = 0;
4800 if(unicode)
4801 charW = (WCHAR)wParam;
4802 else
4804 CHAR charA = wParam;
4805 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4808 EDIT_EM_SetPasswordChar(es, charW);
4809 break;
4812 case EM_EMPTYUNDOBUFFER:
4813 EDIT_EM_EmptyUndoBuffer(es);
4814 break;
4816 case EM_GETFIRSTVISIBLELINE:
4817 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4818 break;
4820 case EM_SETREADONLY:
4822 DWORD old_style = es->style;
4824 if (wParam) {
4825 SetWindowLongW( hwnd, GWL_STYLE,
4826 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4827 es->style |= ES_READONLY;
4828 } else {
4829 SetWindowLongW( hwnd, GWL_STYLE,
4830 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4831 es->style &= ~ES_READONLY;
4834 if (old_style ^ es->style)
4835 InvalidateRect(es->hwndSelf, NULL, TRUE);
4837 result = 1;
4838 break;
4841 case EM_SETWORDBREAKPROC:
4842 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4843 break;
4845 case EM_GETWORDBREAKPROC:
4846 result = (LRESULT)es->word_break_proc;
4847 break;
4849 case EM_GETPASSWORDCHAR:
4851 if(unicode)
4852 result = es->password_char;
4853 else
4855 WCHAR charW = es->password_char;
4856 CHAR charA = 0;
4857 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4858 result = charA;
4860 break;
4863 case EM_SETMARGINS:
4864 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4865 break;
4867 case EM_GETMARGINS:
4868 result = MAKELONG(es->left_margin, es->right_margin);
4869 break;
4871 case EM_GETLIMITTEXT:
4872 result = es->buffer_limit;
4873 break;
4875 case EM_POSFROMCHAR:
4876 if ((INT)wParam >= get_text_length(es)) result = -1;
4877 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4878 break;
4880 case EM_CHARFROMPOS:
4881 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4882 break;
4884 /* End of the EM_ messages which were in numerical order; what order
4885 * are these in? vaguely alphabetical?
4888 case WM_NCCREATE:
4889 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4890 break;
4892 case WM_NCDESTROY:
4893 result = EDIT_WM_NCDestroy(es);
4894 es = NULL;
4895 break;
4897 case WM_GETDLGCODE:
4898 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4900 if (es->style & ES_MULTILINE)
4901 result |= DLGC_WANTALLKEYS;
4903 if (lParam)
4905 es->flags|=EF_DIALOGMODE;
4907 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4909 int vk = (int)((LPMSG)lParam)->wParam;
4911 if (es->hwndListBox)
4913 if (vk == VK_RETURN || vk == VK_ESCAPE)
4914 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4915 result |= DLGC_WANTMESSAGE;
4919 break;
4921 case WM_IME_CHAR:
4922 if (!unicode)
4924 WCHAR charW;
4925 CHAR strng[2];
4927 strng[0] = wParam >> 8;
4928 strng[1] = wParam & 0xff;
4929 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4930 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4931 result = EDIT_WM_Char(es, charW);
4932 break;
4934 /* fall through */
4935 case WM_CHAR:
4937 WCHAR charW;
4939 if(unicode)
4940 charW = wParam;
4941 else
4943 CHAR charA = wParam;
4944 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4947 if (es->hwndListBox)
4949 if (charW == VK_RETURN || charW == VK_ESCAPE)
4951 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4952 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4953 break;
4956 result = EDIT_WM_Char(es, charW);
4957 break;
4960 case WM_UNICHAR:
4961 if (unicode)
4963 if (wParam == UNICODE_NOCHAR) return TRUE;
4964 if (wParam <= 0x000fffff)
4966 if(wParam > 0xffff) /* convert to surrogates */
4968 wParam -= 0x10000;
4969 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4970 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4972 else EDIT_WM_Char(es, wParam);
4974 return 0;
4976 break;
4978 case WM_CLEAR:
4979 EDIT_WM_Clear(es);
4980 break;
4982 case WM_CONTEXTMENU:
4983 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4984 break;
4986 case WM_COPY:
4987 EDIT_WM_Copy(es);
4988 break;
4990 case WM_CREATE:
4991 if(unicode)
4992 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4993 else
4995 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4996 LPWSTR nameW = NULL;
4997 if(nameA)
4999 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5000 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5001 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5003 result = EDIT_WM_Create(es, nameW);
5004 HeapFree(GetProcessHeap(), 0, nameW);
5006 break;
5008 case WM_CUT:
5009 EDIT_WM_Cut(es);
5010 break;
5012 case WM_ENABLE:
5013 es->bEnableState = (BOOL) wParam;
5014 EDIT_UpdateText(es, NULL, TRUE);
5015 break;
5017 case WM_ERASEBKGND:
5018 /* we do the proper erase in EDIT_WM_Paint */
5019 result = 1;
5020 break;
5022 case WM_GETFONT:
5023 result = (LRESULT)es->font;
5024 break;
5026 case WM_GETTEXT:
5027 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5028 break;
5030 case WM_GETTEXTLENGTH:
5031 if (unicode) result = get_text_length(es);
5032 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5033 NULL, 0, NULL, NULL );
5034 break;
5036 case WM_HSCROLL:
5037 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5038 break;
5040 case WM_KEYDOWN:
5041 result = EDIT_WM_KeyDown(es, (INT)wParam);
5042 break;
5044 case WM_KILLFOCUS:
5045 result = EDIT_WM_KillFocus(es);
5046 break;
5048 case WM_LBUTTONDBLCLK:
5049 result = EDIT_WM_LButtonDblClk(es);
5050 break;
5052 case WM_LBUTTONDOWN:
5053 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5054 break;
5056 case WM_LBUTTONUP:
5057 result = EDIT_WM_LButtonUp(es);
5058 break;
5060 case WM_MBUTTONDOWN:
5061 result = EDIT_WM_MButtonDown(es);
5062 break;
5064 case WM_MOUSEMOVE:
5065 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5066 break;
5068 case WM_PRINTCLIENT:
5069 case WM_PAINT:
5070 EDIT_WM_Paint(es, (HDC)wParam);
5071 break;
5073 case WM_PASTE:
5074 EDIT_WM_Paste(es);
5075 break;
5077 case WM_SETFOCUS:
5078 EDIT_WM_SetFocus(es);
5079 break;
5081 case WM_SETFONT:
5082 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5083 break;
5085 case WM_SETREDRAW:
5086 /* FIXME: actually set an internal flag and behave accordingly */
5087 break;
5089 case WM_SETTEXT:
5090 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5091 result = TRUE;
5092 break;
5094 case WM_SIZE:
5095 EDIT_WM_Size(es, (UINT)wParam);
5096 break;
5098 case WM_STYLECHANGED:
5099 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5100 break;
5102 case WM_STYLECHANGING:
5103 result = 0; /* See EDIT_WM_StyleChanged */
5104 break;
5106 case WM_SYSKEYDOWN:
5107 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5108 break;
5110 case WM_TIMER:
5111 EDIT_WM_Timer(es);
5112 break;
5114 case WM_VSCROLL:
5115 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5116 break;
5118 case WM_MOUSEWHEEL:
5120 int wheelDelta;
5121 UINT pulScrollLines = 3;
5122 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5124 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5125 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5126 break;
5128 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5129 /* if scrolling changes direction, ignore left overs */
5130 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5131 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5132 es->wheelDeltaRemainder += wheelDelta;
5133 else
5134 es->wheelDeltaRemainder = wheelDelta;
5135 if (es->wheelDeltaRemainder && pulScrollLines)
5137 int cLineScroll;
5138 pulScrollLines = (int) min((UINT) es->line_count, pulScrollLines);
5139 cLineScroll = pulScrollLines * (float)es->wheelDeltaRemainder / WHEEL_DELTA;
5140 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
5141 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5144 break;
5147 /* IME messages to make the edit control IME aware */
5148 case WM_IME_SETCONTEXT:
5149 break;
5151 case WM_IME_STARTCOMPOSITION:
5152 es->composition_start = es->selection_end;
5153 es->composition_len = 0;
5154 break;
5156 case WM_IME_COMPOSITION:
5157 EDIT_ImeComposition(hwnd, lParam, es);
5158 break;
5160 case WM_IME_ENDCOMPOSITION:
5161 if (es->composition_len > 0)
5163 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
5164 es->selection_end = es->selection_start;
5165 es->composition_len= 0;
5167 break;
5169 case WM_IME_COMPOSITIONFULL:
5170 break;
5172 case WM_IME_SELECT:
5173 break;
5175 case WM_IME_CONTROL:
5176 break;
5178 case WM_IME_REQUEST:
5179 switch (wParam)
5181 case IMR_QUERYCHARPOSITION:
5183 LRESULT pos;
5184 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5186 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5187 chpos->pt.x = LOWORD(pos);
5188 chpos->pt.y = HIWORD(pos);
5189 chpos->cLineHeight = es->line_height;
5190 chpos->rcDocument = es->format_rect;
5191 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5192 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5193 result = 1;
5194 break;
5197 break;
5199 default:
5200 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5201 break;
5204 if (IsWindow(hwnd) && es) EDIT_UnlockBuffer(es, FALSE);
5206 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5208 return result;
5212 /*********************************************************************
5213 * edit class descriptor
5215 static const WCHAR editW[] = {'E','d','i','t',0};
5216 const struct builtin_class_descr EDIT_builtin_class =
5218 editW, /* name */
5219 CS_DBLCLKS | CS_PARENTDC, /* style */
5220 WINPROC_EDIT, /* proc */
5221 #ifdef __i386__
5222 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
5223 #else
5224 sizeof(EDITSTATE *), /* extra */
5225 #endif
5226 IDC_IBEAM, /* cursor */
5227 0 /* brush */