gphoto2.ds: Set supported groups.
[wine.git] / dlls / user32 / edit.c
blobc93d3c3d20254e9e318be8591651829b797da07e
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTES
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_OEMCONVERT
45 #include "config.h"
47 #include <stdarg.h>
48 #include <string.h>
49 #include <stdlib.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnt.h"
54 #include "win.h"
55 #include "imm.h"
56 #include "usp10.h"
57 #include "wine/unicode.h"
58 #include "controls.h"
59 #include "user_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(edit);
63 WINE_DECLARE_DEBUG_CHANNEL(combo);
64 WINE_DECLARE_DEBUG_CHANNEL(relay);
66 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
67 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
68 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
69 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
72 * extra flags for EDITSTATE.flags field
74 #define EF_MODIFIED 0x0001 /* text has been modified */
75 #define EF_FOCUSED 0x0002 /* we have input focus */
76 #define EF_UPDATE 0x0004 /* notify parent of changed state */
77 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
78 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
79 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
80 wrapped line, instead of in front of the next character */
81 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
82 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
84 typedef enum
86 END_0 = 0, /* line ends with terminating '\0' character */
87 END_WRAP, /* line is wrapped */
88 END_HARD, /* line ends with a hard return '\r\n' */
89 END_SOFT, /* line ends with a soft return '\r\r\n' */
90 END_RICH /* line ends with a single '\n' */
91 } LINE_END;
93 typedef struct tagLINEDEF {
94 INT length; /* bruto length of a line in bytes */
95 INT net_length; /* netto length of a line in visible characters */
96 LINE_END ending;
97 INT width; /* width of the line in pixels */
98 INT index; /* line index into the buffer */
99 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
100 struct tagLINEDEF *next;
101 } LINEDEF;
103 typedef struct
105 BOOL is_unicode; /* how the control was created */
106 LPWSTR text; /* the actual contents of the control */
107 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
108 UINT buffer_size; /* the size of the buffer in characters */
109 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
110 HFONT font; /* NULL means standard system font */
111 INT x_offset; /* scroll offset for multi lines this is in pixels
112 for single lines it's in characters */
113 INT line_height; /* height of a screen line in pixels */
114 INT char_width; /* average character width in pixels */
115 DWORD style; /* sane version of wnd->dwStyle */
116 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
117 INT undo_insert_count; /* number of characters inserted in sequence */
118 UINT undo_position; /* character index of the insertion and deletion */
119 LPWSTR undo_text; /* deleted text */
120 UINT undo_buffer_size; /* size of the deleted text buffer */
121 INT selection_start; /* == selection_end if no selection */
122 INT selection_end; /* == current caret position */
123 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
124 INT left_margin; /* in pixels */
125 INT right_margin; /* in pixels */
126 RECT format_rect;
127 INT text_width; /* width of the widest line in pixels for multi line controls
128 and just line width for single line controls */
129 INT region_posx; /* Position of cursor relative to region: */
130 INT region_posy; /* -1: to left, 0: within, 1: to right */
131 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
132 INT line_count; /* number of lines */
133 INT y_offset; /* scroll offset in number of lines */
134 BOOL bCaptureState; /* flag indicating whether mouse was captured */
135 BOOL bEnableState; /* flag keeping the enable state */
136 HWND hwndSelf; /* the our window handle */
137 HWND hwndParent; /* Handle of parent for sending EN_* messages.
138 Even if parent will change, EN_* messages
139 should be sent to the first parent. */
140 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
141 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
143 * only for multi line controls
145 INT lock_count; /* amount of re-entries in the EditWndProc */
146 INT tabs_count;
147 LPINT tabs;
148 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
149 HLOCAL hloc32W; /* our unicode local memory block */
150 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
151 or EM_SETHANDLE */
152 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
154 * IME Data
156 UINT composition_len; /* length of composition, 0 == no composition */
157 int composition_start; /* the character position for the composition */
159 * Uniscribe Data
161 SCRIPT_LOGATTR *logAttr;
162 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
163 } EDITSTATE;
166 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
167 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
169 /* used for disabled or read-only edit control */
170 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
171 do \
172 { /* Notify parent which has created this edit control */ \
173 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
174 SendMessageW(es->hwndParent, WM_COMMAND, \
175 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
176 (LPARAM)(es->hwndSelf)); \
177 } while(0)
179 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
181 /*********************************************************************
183 * EM_CANUNDO
186 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
188 return (es->undo_insert_count || strlenW(es->undo_text));
192 /*********************************************************************
194 * EM_EMPTYUNDOBUFFER
197 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
199 es->undo_insert_count = 0;
200 *es->undo_text = '\0';
204 /**********************************************************************
205 * get_app_version
207 * Returns the window version in case Wine emulates a later version
208 * of windows than the application expects.
210 * In a number of cases when windows runs an application that was
211 * designed for an earlier windows version, windows reverts
212 * to "old" behaviour of that earlier version.
214 * An example is a disabled edit control that needs to be painted.
215 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
216 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
217 * applications with an expected version 0f 4.0 or higher.
220 static DWORD get_app_version(void)
222 static DWORD version;
223 if (!version)
225 DWORD dwEmulatedVersion;
226 OSVERSIONINFOW info;
227 DWORD dwProcVersion = GetProcessVersion(0);
229 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
230 GetVersionExW( &info );
231 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
232 /* FIXME: this may not be 100% correct; see discussion on the
233 * wine developer list in Nov 1999 */
234 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
236 return version;
239 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
241 HBRUSH hbrush;
242 UINT msg;
244 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
245 msg = WM_CTLCOLORSTATIC;
246 else
247 msg = WM_CTLCOLOREDIT;
249 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
250 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
251 if (!hbrush)
252 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
253 return hbrush;
257 static inline UINT get_text_length(EDITSTATE *es)
259 if(es->text_length == (UINT)-1)
260 es->text_length = strlenW(es->text);
261 return es->text_length;
265 /*********************************************************************
267 * EDIT_WordBreakProc
269 * Find the beginning of words.
270 * Note: unlike the specs for a WordBreakProc, this function can
271 * only be called without linebreaks between s[0] up to
272 * s[count - 1]. Remember it is only called
273 * internally, so we can decide this for ourselves.
274 * Additionally we will always be breaking the full string.
277 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
279 INT ret = 0;
281 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
283 if(!s) return 0;
285 if (!es->logAttr)
287 SCRIPT_ANALYSIS psa;
289 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
290 psa.eScript = SCRIPT_UNDEFINED;
292 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
293 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
296 switch (action) {
297 case WB_LEFT:
298 if (index)
299 index--;
300 while (index && !es->logAttr[index].fSoftBreak)
301 index--;
302 ret = index;
303 break;
304 case WB_RIGHT:
305 if (!count)
306 break;
307 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
308 index++;
309 ret = index;
310 break;
311 case WB_ISDELIMITER:
312 ret = es->logAttr[index].fWhiteSpace;
313 break;
314 default:
315 ERR("unknown action code, please report !\n");
316 break;
318 return ret;
322 /*********************************************************************
324 * EDIT_CallWordBreakProc
326 * Call appropriate WordBreakProc (internal or external).
328 * Note: The "start" argument should always be an index referring
329 * to es->text. The actual wordbreak proc might be
330 * 16 bit, so we can't always pass any 32 bit LPSTR.
331 * Hence we assume that es->text is the buffer that holds
332 * the string under examination (we can decide this for ourselves).
335 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
337 INT ret;
339 if (es->word_break_proc)
341 if(es->is_unicode)
343 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
345 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
346 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
347 ret = wbpW(es->text + start, index, count, action);
349 else
351 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
352 INT countA;
353 CHAR *textA;
355 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
356 textA = HeapAlloc(GetProcessHeap(), 0, countA);
357 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
358 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
359 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
360 ret = wbpA(textA, index, countA, action);
361 HeapFree(GetProcessHeap(), 0, textA);
364 else
365 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
367 return ret;
370 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
372 if (line_def->ssa)
374 ScriptStringFree(&line_def->ssa);
375 line_def->ssa = NULL;
379 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
381 LINEDEF *line_def = es->first_line_def;
382 while (line_def)
384 EDIT_InvalidateUniscribeData_linedef(line_def);
385 line_def = line_def->next;
387 if (es->ssa)
389 ScriptStringFree(&es->ssa);
390 es->ssa = NULL;
394 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
396 if (!line_def)
397 return NULL;
399 if (line_def->net_length && !line_def->ssa)
401 int index = line_def->index;
402 HFONT old_font = NULL;
403 HDC udc = dc;
404 SCRIPT_TABDEF tabdef;
405 HRESULT hr;
407 if (!udc)
408 udc = GetDC(es->hwndSelf);
409 if (es->font)
410 old_font = SelectObject(udc, es->font);
412 tabdef.cTabStops = es->tabs_count;
413 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
414 tabdef.pTabStops = es->tabs;
415 tabdef.iTabOrigin = 0;
417 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
418 (1.5*line_def->net_length+16), -1,
419 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
420 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
421 if (FAILED(hr))
423 WARN("ScriptStringAnalyse failed (%x)\n",hr);
424 line_def->ssa = NULL;
427 if (es->font)
428 SelectObject(udc, old_font);
429 if (udc != dc)
430 ReleaseDC(es->hwndSelf, udc);
433 return line_def->ssa;
436 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
438 LINEDEF *line_def;
440 if (!(es->style & ES_MULTILINE))
442 if (!es->ssa)
444 INT length = get_text_length(es);
445 HFONT old_font = NULL;
446 HDC udc = dc;
448 if (!udc)
449 udc = GetDC(es->hwndSelf);
450 if (es->font)
451 old_font = SelectObject(udc, es->font);
453 if (es->style & ES_PASSWORD)
454 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);
455 else
456 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
458 if (es->font)
459 SelectObject(udc, old_font);
460 if (udc != dc)
461 ReleaseDC(es->hwndSelf, udc);
463 return es->ssa;
465 else
467 line_def = es->first_line_def;
468 while (line_def && line)
470 line_def = line_def->next;
471 line--;
474 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
478 static inline INT get_vertical_line_count(EDITSTATE *es)
480 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
481 return max(1,vlc);
484 /*********************************************************************
486 * EDIT_BuildLineDefs_ML
488 * Build linked list of text lines.
489 * Lines can end with '\0' (last line), a character (if it is wrapped),
490 * a soft return '\r\r\n' or a hard return '\r\n'
493 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
495 LPWSTR current_position, cp;
496 INT fw;
497 LINEDEF *current_line;
498 LINEDEF *previous_line;
499 LINEDEF *start_line;
500 INT line_index = 0, nstart_line, nstart_index;
501 INT line_count = es->line_count;
502 INT orig_net_length;
503 RECT rc;
504 INT vlc;
506 if (istart == iend && delta == 0)
507 return;
509 previous_line = NULL;
510 current_line = es->first_line_def;
512 /* Find starting line. istart must lie inside an existing line or
513 * at the end of buffer */
514 do {
515 if (istart < current_line->index + current_line->length ||
516 current_line->ending == END_0)
517 break;
519 previous_line = current_line;
520 current_line = current_line->next;
521 line_index++;
522 } while (current_line);
524 if (!current_line) /* Error occurred start is not inside previous buffer */
526 FIXME(" modification occurred outside buffer\n");
527 return;
530 /* Remember start of modifications in order to calculate update region */
531 nstart_line = line_index;
532 nstart_index = current_line->index;
534 /* We must start to reformat from the previous line since the modifications
535 * may have caused the line to wrap upwards. */
536 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
538 line_index--;
539 current_line = previous_line;
541 start_line = current_line;
543 fw = es->format_rect.right - es->format_rect.left;
544 current_position = es->text + current_line->index;
545 vlc = get_vertical_line_count(es);
546 do {
547 if (current_line != start_line)
549 if (!current_line || current_line->index + delta > current_position - es->text)
551 /* The buffer has been expanded, create a new line and
552 insert it into the link list */
553 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
554 new_line->next = previous_line->next;
555 previous_line->next = new_line;
556 current_line = new_line;
557 es->line_count++;
559 else if (current_line->index + delta < current_position - es->text)
561 /* The previous line merged with this line so we delete this extra entry */
562 previous_line->next = current_line->next;
563 HeapFree(GetProcessHeap(), 0, current_line);
564 current_line = previous_line->next;
565 es->line_count--;
566 continue;
568 else /* current_line->index + delta == current_position */
570 if (current_position - es->text > iend)
571 break; /* We reached end of line modifications */
572 /* else recalculate this line */
576 current_line->index = current_position - es->text;
577 orig_net_length = current_line->net_length;
579 /* Find end of line */
580 cp = current_position;
581 while (*cp) {
582 if (*cp == '\n') break;
583 if ((*cp == '\r') && (*(cp + 1) == '\n'))
584 break;
585 cp++;
588 /* Mark type of line termination */
589 if (!(*cp)) {
590 current_line->ending = END_0;
591 current_line->net_length = strlenW(current_position);
592 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
593 current_line->ending = END_SOFT;
594 current_line->net_length = cp - current_position - 1;
595 } else if (*cp == '\n') {
596 current_line->ending = END_RICH;
597 current_line->net_length = cp - current_position;
598 } else {
599 current_line->ending = END_HARD;
600 current_line->net_length = cp - current_position;
603 if (current_line->net_length)
605 const SIZE *sz;
606 EDIT_InvalidateUniscribeData_linedef(current_line);
607 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
608 if (current_line->ssa)
610 sz = ScriptString_pSize(current_line->ssa);
611 /* Calculate line width */
612 current_line->width = sz->cx;
614 else current_line->width = es->char_width * current_line->net_length;
616 else current_line->width = 0;
618 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
620 /* Line breaks just look back from the end and find the next break and try that. */
622 if (!(es->style & ES_AUTOHSCROLL)) {
623 if (current_line->width > fw && fw > es->char_width) {
625 INT prev, next;
626 int w;
627 const SIZE *sz;
628 float d;
630 prev = current_line->net_length - 1;
631 w = current_line->net_length;
632 d = (float)current_line->width/(float)fw;
633 if (d > 1.2f) d -= 0.2f;
634 next = prev/d;
635 if (next >= prev) next = prev-1;
636 do {
637 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
638 next, current_line->net_length, WB_LEFT);
639 current_line->net_length = prev;
640 EDIT_InvalidateUniscribeData_linedef(current_line);
641 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
642 if (current_line->ssa)
643 sz = ScriptString_pSize(current_line->ssa);
644 else sz = 0;
645 if (sz)
646 current_line->width = sz->cx;
647 else
648 prev = 0;
649 next = prev - 1;
650 } while (prev && current_line->width > fw);
651 current_line->net_length = w;
653 if (prev == 0) { /* Didn't find a line break so force a break */
654 INT *piDx;
655 const INT *count;
657 EDIT_InvalidateUniscribeData_linedef(current_line);
658 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
660 if (current_line->ssa)
662 count = ScriptString_pcOutChars(current_line->ssa);
663 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
664 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
666 prev = current_line->net_length-1;
667 do {
668 current_line->width -= piDx[prev];
669 prev--;
670 } while ( prev > 0 && current_line->width > fw);
671 if (prev<=0)
672 prev = 1;
673 HeapFree(GetProcessHeap(),0,piDx);
675 else
676 prev = (fw / es->char_width);
679 /* If the first line we are calculating, wrapped before istart, we must
680 * adjust istart in order for this to be reflected in the update region. */
681 if (current_line->index == nstart_index && istart > current_line->index + prev)
682 istart = current_line->index + prev;
683 /* else if we are updating the previous line before the first line we
684 * are re-calculating and it expanded */
685 else if (current_line == start_line &&
686 current_line->index != nstart_index && orig_net_length < prev)
688 /* Line expanded due to an upwards line wrap so we must partially include
689 * previous line in update region */
690 nstart_line = line_index;
691 nstart_index = current_line->index;
692 istart = current_line->index + orig_net_length;
695 current_line->net_length = prev;
696 current_line->ending = END_WRAP;
698 if (current_line->net_length > 0)
700 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
701 if (current_line->ssa)
703 sz = ScriptString_pSize(current_line->ssa);
704 current_line->width = sz->cx;
706 else
707 current_line->width = 0;
709 else current_line->width = 0;
711 else if (current_line == start_line &&
712 current_line->index != nstart_index &&
713 orig_net_length < current_line->net_length) {
714 /* The previous line expanded but it's still not as wide as the client rect */
715 /* The expansion is due to an upwards line wrap so we must partially include
716 it in the update region */
717 nstart_line = line_index;
718 nstart_index = current_line->index;
719 istart = current_line->index + orig_net_length;
724 /* Adjust length to include line termination */
725 switch (current_line->ending) {
726 case END_SOFT:
727 current_line->length = current_line->net_length + 3;
728 break;
729 case END_RICH:
730 current_line->length = current_line->net_length + 1;
731 break;
732 case END_HARD:
733 current_line->length = current_line->net_length + 2;
734 break;
735 case END_WRAP:
736 case END_0:
737 current_line->length = current_line->net_length;
738 break;
740 es->text_width = max(es->text_width, current_line->width);
741 current_position += current_line->length;
742 previous_line = current_line;
744 /* Discard data for non-visible lines. It will be calculated as needed */
745 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
746 EDIT_InvalidateUniscribeData_linedef(current_line);
748 current_line = current_line->next;
749 line_index++;
750 } while (previous_line->ending != END_0);
752 /* Finish adjusting line indexes by delta or remove hanging lines */
753 if (previous_line->ending == END_0)
755 LINEDEF *pnext = NULL;
757 previous_line->next = NULL;
758 while (current_line)
760 pnext = current_line->next;
761 EDIT_InvalidateUniscribeData_linedef(current_line);
762 HeapFree(GetProcessHeap(), 0, current_line);
763 current_line = pnext;
764 es->line_count--;
767 else if (delta != 0)
769 while (current_line)
771 current_line->index += delta;
772 current_line = current_line->next;
776 /* Calculate rest of modification rectangle */
777 if (hrgn)
779 HRGN tmphrgn;
781 * We calculate two rectangles. One for the first line which may have
782 * an indent with respect to the format rect. The other is a format-width
783 * rectangle that spans the rest of the lines that changed or moved.
785 rc.top = es->format_rect.top + nstart_line * es->line_height -
786 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
787 rc.bottom = rc.top + es->line_height;
788 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
789 rc.left = es->format_rect.left;
790 else
791 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
792 rc.right = es->format_rect.right;
793 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
795 rc.top = rc.bottom;
796 rc.left = es->format_rect.left;
797 rc.right = es->format_rect.right;
799 * If lines were added or removed we must re-paint the remainder of the
800 * lines since the remaining lines were either shifted up or down.
802 if (line_count < es->line_count) /* We added lines */
803 rc.bottom = es->line_count * es->line_height;
804 else if (line_count > es->line_count) /* We removed lines */
805 rc.bottom = line_count * es->line_height;
806 else
807 rc.bottom = line_index * es->line_height;
808 rc.bottom += es->format_rect.top;
809 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
810 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
811 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
812 DeleteObject(tmphrgn);
816 /*********************************************************************
818 * EDIT_CalcLineWidth_SL
821 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
823 EDIT_UpdateUniscribeData(es, NULL, 0);
824 if (es->ssa)
826 const SIZE *size;
827 size = ScriptString_pSize(es->ssa);
828 es->text_width = size->cx;
830 else
831 es->text_width = 0;
834 /*********************************************************************
836 * EDIT_CharFromPos
838 * Beware: This is not the function called on EM_CHARFROMPOS
839 * The position _can_ be outside the formatting / client
840 * rectangle
841 * The return value is only the character index
844 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
846 INT index;
848 if (es->style & ES_MULTILINE) {
849 int trailing;
850 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
851 INT line_index = 0;
852 LINEDEF *line_def = es->first_line_def;
853 EDIT_UpdateUniscribeData(es, NULL, line);
854 while ((line > 0) && line_def->next) {
855 line_index += line_def->length;
856 line_def = line_def->next;
857 line--;
860 x += es->x_offset - es->format_rect.left;
861 if (es->style & ES_RIGHT)
862 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
863 else if (es->style & ES_CENTER)
864 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
865 if (x >= line_def->width) {
866 if (after_wrap)
867 *after_wrap = (line_def->ending == END_WRAP);
868 return line_index + line_def->net_length;
870 if (x <= 0 || !line_def->ssa) {
871 if (after_wrap)
872 *after_wrap = FALSE;
873 return line_index;
876 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
877 if (trailing) index++;
878 index += line_index;
879 if (after_wrap)
880 *after_wrap = ((index == line_index + line_def->net_length) &&
881 (line_def->ending == END_WRAP));
882 } else {
883 INT xoff = 0;
884 INT trailing;
885 if (after_wrap)
886 *after_wrap = FALSE;
887 x -= es->format_rect.left;
888 if (!x)
889 return es->x_offset;
891 if (!es->x_offset)
893 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
894 if (es->style & ES_RIGHT)
895 x -= indent;
896 else if (es->style & ES_CENTER)
897 x -= indent / 2;
900 EDIT_UpdateUniscribeData(es, NULL, 0);
901 if (es->x_offset)
903 if (es->ssa)
905 if (es->x_offset>= get_text_length(es))
907 const SIZE *size;
908 size = ScriptString_pSize(es->ssa);
909 xoff = size->cx;
911 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
913 else
914 xoff = 0;
916 if (x < 0)
918 if (x + xoff > 0 || !es->ssa)
920 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
921 if (trailing) index++;
923 else
924 index = 0;
926 else
928 if (x)
930 const SIZE *size = NULL;
931 if (es->ssa)
932 size = ScriptString_pSize(es->ssa);
933 if (!size)
934 index = 0;
935 else if (x > size->cx)
936 index = get_text_length(es);
937 else if (es->ssa)
939 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
940 if (trailing) index++;
942 else
943 index = 0;
945 else
946 index = es->x_offset;
949 return index;
953 /*********************************************************************
955 * EDIT_ConfinePoint
957 * adjusts the point to be within the formatting rectangle
958 * (so CharFromPos returns the nearest _visible_ character)
961 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
963 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
964 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
968 /*********************************************************************
970 * EM_LINEFROMCHAR
973 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
975 INT line;
976 LINEDEF *line_def;
978 if (!(es->style & ES_MULTILINE))
979 return 0;
980 if (index > (INT)get_text_length(es))
981 return es->line_count - 1;
982 if (index == -1)
983 index = min(es->selection_start, es->selection_end);
985 line = 0;
986 line_def = es->first_line_def;
987 index -= line_def->length;
988 while ((index >= 0) && line_def->next) {
989 line++;
990 line_def = line_def->next;
991 index -= line_def->length;
993 return line;
997 /*********************************************************************
999 * EM_LINEINDEX
1002 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
1004 INT line_index;
1005 const LINEDEF *line_def;
1007 if (!(es->style & ES_MULTILINE))
1008 return 0;
1009 if (line >= es->line_count)
1010 return -1;
1012 line_index = 0;
1013 line_def = es->first_line_def;
1014 if (line == -1) {
1015 INT index = es->selection_end - line_def->length;
1016 while ((index >= 0) && line_def->next) {
1017 line_index += line_def->length;
1018 line_def = line_def->next;
1019 index -= line_def->length;
1021 } else {
1022 while (line > 0) {
1023 line_index += line_def->length;
1024 line_def = line_def->next;
1025 line--;
1028 return line_index;
1032 /*********************************************************************
1034 * EM_LINELENGTH
1037 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1039 LINEDEF *line_def;
1041 if (!(es->style & ES_MULTILINE))
1042 return get_text_length(es);
1044 if (index == -1) {
1045 /* get the number of remaining non-selected chars of selected lines */
1046 INT32 l; /* line number */
1047 INT32 li; /* index of first char in line */
1048 INT32 count;
1049 l = EDIT_EM_LineFromChar(es, es->selection_start);
1050 /* # chars before start of selection area */
1051 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1052 l = EDIT_EM_LineFromChar(es, es->selection_end);
1053 /* # chars after end of selection */
1054 li = EDIT_EM_LineIndex(es, l);
1055 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1056 return count;
1058 line_def = es->first_line_def;
1059 index -= line_def->length;
1060 while ((index >= 0) && line_def->next) {
1061 line_def = line_def->next;
1062 index -= line_def->length;
1064 return line_def->net_length;
1068 /*********************************************************************
1070 * EM_POSFROMCHAR
1073 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1075 INT len = get_text_length(es);
1076 INT l;
1077 INT li;
1078 INT x = 0;
1079 INT y = 0;
1080 INT w;
1081 INT lw;
1082 LINEDEF *line_def;
1084 index = min(index, len);
1085 if (es->style & ES_MULTILINE) {
1086 l = EDIT_EM_LineFromChar(es, index);
1087 EDIT_UpdateUniscribeData(es, NULL, l);
1089 y = (l - es->y_offset) * es->line_height;
1090 li = EDIT_EM_LineIndex(es, l);
1091 if (after_wrap && (li == index) && l) {
1092 INT l2 = l - 1;
1093 line_def = es->first_line_def;
1094 while (l2) {
1095 line_def = line_def->next;
1096 l2--;
1098 if (line_def->ending == END_WRAP) {
1099 l--;
1100 y -= es->line_height;
1101 li = EDIT_EM_LineIndex(es, l);
1105 line_def = es->first_line_def;
1106 while (line_def->index != li)
1107 line_def = line_def->next;
1109 lw = line_def->width;
1110 w = es->format_rect.right - es->format_rect.left;
1111 if (line_def->ssa)
1113 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1114 x -= es->x_offset;
1116 else
1117 x = es->x_offset;
1119 if (es->style & ES_RIGHT)
1120 x = w - (lw - x);
1121 else if (es->style & ES_CENTER)
1122 x += (w - lw) / 2;
1123 } else {
1124 INT xoff = 0;
1125 INT xi = 0;
1126 EDIT_UpdateUniscribeData(es, NULL, 0);
1127 if (es->x_offset)
1129 if (es->ssa)
1131 if (es->x_offset >= get_text_length(es))
1133 int leftover = es->x_offset - get_text_length(es);
1134 if (es->ssa)
1136 const SIZE *size;
1137 size = ScriptString_pSize(es->ssa);
1138 xoff = size->cx;
1140 else
1141 xoff = 0;
1142 xoff += es->char_width * leftover;
1144 else
1145 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1147 else
1148 xoff = 0;
1150 if (index)
1152 if (index >= get_text_length(es))
1154 if (es->ssa)
1156 const SIZE *size;
1157 size = ScriptString_pSize(es->ssa);
1158 xi = size->cx;
1160 else
1161 xi = 0;
1163 else if (es->ssa)
1164 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1165 else
1166 xi = 0;
1168 x = xi - xoff;
1170 if (index >= es->x_offset) {
1171 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1173 w = es->format_rect.right - es->format_rect.left;
1174 if (w > es->text_width)
1176 if (es->style & ES_RIGHT)
1177 x += w - es->text_width;
1178 else if (es->style & ES_CENTER)
1179 x += (w - es->text_width) / 2;
1183 y = 0;
1185 x += es->format_rect.left;
1186 y += es->format_rect.top;
1187 return MAKELONG((INT16)x, (INT16)y);
1191 /*********************************************************************
1193 * EDIT_GetLineRect
1195 * Calculates the bounding rectangle for a line from a starting
1196 * column to an ending column.
1199 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1201 SCRIPT_STRING_ANALYSIS ssa;
1202 INT line_index = 0;
1203 INT pt1, pt2, pt3;
1205 if (es->style & ES_MULTILINE)
1207 const LINEDEF *line_def = NULL;
1208 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1209 if (line >= es->line_count)
1210 return;
1212 line_def = es->first_line_def;
1213 if (line == -1) {
1214 INT index = es->selection_end - line_def->length;
1215 while ((index >= 0) && line_def->next) {
1216 line_index += line_def->length;
1217 line_def = line_def->next;
1218 index -= line_def->length;
1220 } else {
1221 while (line > 0) {
1222 line_index += line_def->length;
1223 line_def = line_def->next;
1224 line--;
1227 ssa = line_def->ssa;
1229 else
1231 line_index = 0;
1232 rc->top = es->format_rect.top;
1233 ssa = es->ssa;
1236 rc->bottom = rc->top + es->line_height;
1237 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1238 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1239 if (ssa)
1241 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1242 pt3+=es->format_rect.left;
1244 else pt3 = pt1;
1245 rc->right = max(max(pt1 , pt2),pt3);
1246 rc->left = min(min(pt1, pt2),pt3);
1250 static inline void text_buffer_changed(EDITSTATE *es)
1252 es->text_length = (UINT)-1;
1254 HeapFree( GetProcessHeap(), 0, es->logAttr );
1255 es->logAttr = NULL;
1256 EDIT_InvalidateUniscribeData(es);
1259 /*********************************************************************
1260 * EDIT_LockBuffer
1263 static void EDIT_LockBuffer(EDITSTATE *es)
1265 if (es->hlocapp) return;
1267 if (!es->text) {
1269 if(!es->hloc32W) return;
1271 if(es->hloc32A)
1273 CHAR *textA = LocalLock(es->hloc32A);
1274 HLOCAL hloc32W_new;
1275 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1276 if(countW_new > es->buffer_size + 1)
1278 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1279 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1280 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1281 if(hloc32W_new)
1283 es->hloc32W = hloc32W_new;
1284 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1285 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1287 else
1288 WARN("FAILED! Will synchronize partially\n");
1290 es->text = LocalLock(es->hloc32W);
1291 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1292 LocalUnlock(es->hloc32A);
1294 else es->text = LocalLock(es->hloc32W);
1296 es->lock_count++;
1300 /*********************************************************************
1302 * EDIT_UnlockBuffer
1305 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1307 if (es->hlocapp) return;
1309 /* Edit window might be already destroyed */
1310 if(!IsWindow(es->hwndSelf))
1312 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1313 return;
1316 if (!es->lock_count) {
1317 ERR("lock_count == 0 ... please report\n");
1318 return;
1320 if (!es->text) {
1321 ERR("es->text == 0 ... please report\n");
1322 return;
1325 if (force || (es->lock_count == 1)) {
1326 if (es->hloc32W) {
1327 UINT countA = 0;
1328 UINT countW = get_text_length(es) + 1;
1330 if(es->hloc32A)
1332 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1333 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1334 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1335 countA = LocalSize(es->hloc32A);
1336 if(countA_new > countA)
1338 HLOCAL hloc32A_new;
1339 UINT alloc_size = ROUND_TO_GROW(countA_new);
1340 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1341 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1342 if(hloc32A_new)
1344 es->hloc32A = hloc32A_new;
1345 countA = LocalSize(hloc32A_new);
1346 TRACE("Real new size %d bytes\n", countA);
1348 else
1349 WARN("FAILED! Will synchronize partially\n");
1351 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1352 LocalLock(es->hloc32A), countA, NULL, NULL);
1353 LocalUnlock(es->hloc32A);
1356 LocalUnlock(es->hloc32W);
1357 es->text = NULL;
1359 else {
1360 ERR("no buffer ... please report\n");
1361 return;
1364 es->lock_count--;
1368 /*********************************************************************
1370 * EDIT_MakeFit
1372 * Try to fit size + 1 characters in the buffer.
1374 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1376 HLOCAL hNew32W;
1378 if (size <= es->buffer_size)
1379 return TRUE;
1381 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1383 /* Force edit to unlock its buffer. es->text now NULL */
1384 EDIT_UnlockBuffer(es, TRUE);
1386 if (es->hloc32W) {
1387 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1388 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1389 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1390 es->hloc32W = hNew32W;
1391 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1395 EDIT_LockBuffer(es);
1397 if (es->buffer_size < size) {
1398 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1399 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1400 return FALSE;
1401 } else {
1402 TRACE("We now have %d+1\n", es->buffer_size);
1403 return TRUE;
1408 /*********************************************************************
1410 * EDIT_MakeUndoFit
1412 * Try to fit size + 1 bytes in the undo buffer.
1415 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1417 UINT alloc_size;
1419 if (size <= es->undo_buffer_size)
1420 return TRUE;
1422 TRACE("trying to ReAlloc to %d+1\n", size);
1424 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1425 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1426 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1427 return TRUE;
1429 else
1431 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1432 return FALSE;
1437 /*********************************************************************
1439 * EDIT_UpdateTextRegion
1442 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1444 if (es->flags & EF_UPDATE) {
1445 es->flags &= ~EF_UPDATE;
1446 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1448 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1452 /*********************************************************************
1454 * EDIT_UpdateText
1457 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1459 if (es->flags & EF_UPDATE) {
1460 es->flags &= ~EF_UPDATE;
1461 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1463 InvalidateRect(es->hwndSelf, rc, bErase);
1466 /*********************************************************************
1468 * EDIT_SL_InvalidateText
1470 * Called from EDIT_InvalidateText().
1471 * Does the job for single-line controls only.
1474 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1476 RECT line_rect;
1477 RECT rc;
1479 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1480 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1481 EDIT_UpdateText(es, &rc, TRUE);
1484 /*********************************************************************
1486 * EDIT_ML_InvalidateText
1488 * Called from EDIT_InvalidateText().
1489 * Does the job for multi-line controls only.
1492 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1494 INT vlc = get_vertical_line_count(es);
1495 INT sl = EDIT_EM_LineFromChar(es, start);
1496 INT el = EDIT_EM_LineFromChar(es, end);
1497 INT sc;
1498 INT ec;
1499 RECT rc1;
1500 RECT rcWnd;
1501 RECT rcLine;
1502 RECT rcUpdate;
1503 INT l;
1505 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1506 return;
1508 sc = start - EDIT_EM_LineIndex(es, sl);
1509 ec = end - EDIT_EM_LineIndex(es, el);
1510 if (sl < es->y_offset) {
1511 sl = es->y_offset;
1512 sc = 0;
1514 if (el > es->y_offset + vlc) {
1515 el = es->y_offset + vlc;
1516 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1518 GetClientRect(es->hwndSelf, &rc1);
1519 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1520 if (sl == el) {
1521 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1522 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1523 EDIT_UpdateText(es, &rcUpdate, TRUE);
1524 } else {
1525 EDIT_GetLineRect(es, sl, sc,
1526 EDIT_EM_LineLength(es,
1527 EDIT_EM_LineIndex(es, sl)),
1528 &rcLine);
1529 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1530 EDIT_UpdateText(es, &rcUpdate, TRUE);
1531 for (l = sl + 1 ; l < el ; l++) {
1532 EDIT_GetLineRect(es, l, 0,
1533 EDIT_EM_LineLength(es,
1534 EDIT_EM_LineIndex(es, l)),
1535 &rcLine);
1536 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1537 EDIT_UpdateText(es, &rcUpdate, TRUE);
1539 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1540 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1541 EDIT_UpdateText(es, &rcUpdate, TRUE);
1546 /*********************************************************************
1548 * EDIT_InvalidateText
1550 * Invalidate the text from offset start up to, but not including,
1551 * offset end. Useful for (re)painting the selection.
1552 * Regions outside the linewidth are not invalidated.
1553 * end == -1 means end == TextLength.
1554 * start and end need not be ordered.
1557 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1559 if (end == start)
1560 return;
1562 if (end == -1)
1563 end = get_text_length(es);
1565 if (end < start) {
1566 INT tmp = start;
1567 start = end;
1568 end = tmp;
1571 if (es->style & ES_MULTILINE)
1572 EDIT_ML_InvalidateText(es, start, end);
1573 else
1574 EDIT_SL_InvalidateText(es, start, end);
1578 /*********************************************************************
1580 * EDIT_EM_SetSel
1582 * note: unlike the specs say: the order of start and end
1583 * _is_ preserved in Windows. (i.e. start can be > end)
1584 * In other words: this handler is OK
1587 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1589 UINT old_start = es->selection_start;
1590 UINT old_end = es->selection_end;
1591 UINT len = get_text_length(es);
1593 if (start == (UINT)-1) {
1594 start = es->selection_end;
1595 end = es->selection_end;
1596 } else {
1597 start = min(start, len);
1598 end = min(end, len);
1600 es->selection_start = start;
1601 es->selection_end = end;
1602 if (after_wrap)
1603 es->flags |= EF_AFTER_WRAP;
1604 else
1605 es->flags &= ~EF_AFTER_WRAP;
1606 /* Compute the necessary invalidation region. */
1607 /* Note that we don't need to invalidate regions which have
1608 * "never" been selected, or those which are "still" selected.
1609 * In fact, every time we hit a selection boundary, we can
1610 * *toggle* whether we need to invalidate. Thus we can optimize by
1611 * *sorting* the interval endpoints. Let's assume that we sort them
1612 * in this order:
1613 * start <= end <= old_start <= old_end
1614 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1615 * in 5 comparisons; i.e. it is impossible to do better than the
1616 * following: */
1617 ORDER_UINT(end, old_end);
1618 ORDER_UINT(start, old_start);
1619 ORDER_UINT(old_start, old_end);
1620 ORDER_UINT(start, end);
1621 /* Note that at this point 'end' and 'old_start' are not in order, but
1622 * start is definitely the min. and old_end is definitely the max. */
1623 if (end != old_start)
1626 * One can also do
1627 * ORDER_UINT32(end, old_start);
1628 * EDIT_InvalidateText(es, start, end);
1629 * EDIT_InvalidateText(es, old_start, old_end);
1630 * in place of the following if statement.
1631 * (That would complete the optimal five-comparison four-element sort.)
1633 if (old_start > end )
1635 EDIT_InvalidateText(es, start, end);
1636 EDIT_InvalidateText(es, old_start, old_end);
1638 else
1640 EDIT_InvalidateText(es, start, old_start);
1641 EDIT_InvalidateText(es, end, old_end);
1644 else EDIT_InvalidateText(es, start, old_end);
1648 /*********************************************************************
1650 * EDIT_UpdateScrollInfo
1653 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1655 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1657 SCROLLINFO si;
1658 si.cbSize = sizeof(SCROLLINFO);
1659 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1660 si.nMin = 0;
1661 si.nMax = es->line_count - 1;
1662 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1663 si.nPos = es->y_offset;
1664 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1665 si.nMin, si.nMax, si.nPage, si.nPos);
1666 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1669 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1671 SCROLLINFO si;
1672 si.cbSize = sizeof(SCROLLINFO);
1673 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1674 si.nMin = 0;
1675 si.nMax = es->text_width - 1;
1676 si.nPage = es->format_rect.right - es->format_rect.left;
1677 si.nPos = es->x_offset;
1678 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1679 si.nMin, si.nMax, si.nPage, si.nPos);
1680 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1685 /*********************************************************************
1687 * EDIT_EM_LineScroll_internal
1689 * Version of EDIT_EM_LineScroll for internal use.
1690 * It doesn't refuse if ES_MULTILINE is set and assumes that
1691 * dx is in pixels, dy - in lines.
1694 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1696 INT nyoff;
1697 INT x_offset_in_pixels;
1698 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1699 es->line_height;
1701 if (es->style & ES_MULTILINE)
1703 x_offset_in_pixels = es->x_offset;
1705 else
1707 dy = 0;
1708 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1711 if (-dx > x_offset_in_pixels)
1712 dx = -x_offset_in_pixels;
1713 if (dx > es->text_width - x_offset_in_pixels)
1714 dx = es->text_width - x_offset_in_pixels;
1715 nyoff = max(0, es->y_offset + dy);
1716 if (nyoff >= es->line_count - lines_per_page)
1717 nyoff = max(0, es->line_count - lines_per_page);
1718 dy = (es->y_offset - nyoff) * es->line_height;
1719 if (dx || dy) {
1720 RECT rc1;
1721 RECT rc;
1723 es->y_offset = nyoff;
1724 if(es->style & ES_MULTILINE)
1725 es->x_offset += dx;
1726 else
1727 es->x_offset += dx / es->char_width;
1729 GetClientRect(es->hwndSelf, &rc1);
1730 IntersectRect(&rc, &rc1, &es->format_rect);
1731 ScrollWindowEx(es->hwndSelf, -dx, dy,
1732 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1733 /* force scroll info update */
1734 EDIT_UpdateScrollInfo(es);
1736 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1737 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1738 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1739 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1740 return TRUE;
1743 /*********************************************************************
1745 * EM_LINESCROLL
1747 * NOTE: dx is in average character widths, dy - in lines;
1750 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1752 if (!(es->style & ES_MULTILINE))
1753 return FALSE;
1755 dx *= es->char_width;
1756 return EDIT_EM_LineScroll_internal(es, dx, dy);
1760 /*********************************************************************
1762 * EM_SCROLL
1765 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1767 INT dy;
1769 if (!(es->style & ES_MULTILINE))
1770 return (LRESULT)FALSE;
1772 dy = 0;
1774 switch (action) {
1775 case SB_LINEUP:
1776 if (es->y_offset)
1777 dy = -1;
1778 break;
1779 case SB_LINEDOWN:
1780 if (es->y_offset < es->line_count - 1)
1781 dy = 1;
1782 break;
1783 case SB_PAGEUP:
1784 if (es->y_offset)
1785 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1786 break;
1787 case SB_PAGEDOWN:
1788 if (es->y_offset < es->line_count - 1)
1789 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1790 break;
1791 default:
1792 return (LRESULT)FALSE;
1794 if (dy) {
1795 INT vlc = get_vertical_line_count(es);
1796 /* check if we are going to move too far */
1797 if(es->y_offset + dy > es->line_count - vlc)
1798 dy = max(es->line_count - vlc, 0) - es->y_offset;
1800 /* Notification is done in EDIT_EM_LineScroll */
1801 if(dy) {
1802 EDIT_EM_LineScroll(es, 0, dy);
1803 return MAKELONG(dy, TRUE);
1807 return (LRESULT)FALSE;
1811 /*********************************************************************
1813 * EDIT_SetCaretPos
1816 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1817 BOOL after_wrap)
1819 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1820 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1821 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1825 /*********************************************************************
1827 * EM_SCROLLCARET
1830 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1832 if (es->style & ES_MULTILINE) {
1833 INT l;
1834 INT vlc;
1835 INT ww;
1836 INT cw = es->char_width;
1837 INT x;
1838 INT dy = 0;
1839 INT dx = 0;
1841 l = EDIT_EM_LineFromChar(es, es->selection_end);
1842 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1843 vlc = get_vertical_line_count(es);
1844 if (l >= es->y_offset + vlc)
1845 dy = l - vlc + 1 - es->y_offset;
1846 if (l < es->y_offset)
1847 dy = l - es->y_offset;
1848 ww = es->format_rect.right - es->format_rect.left;
1849 if (x < es->format_rect.left)
1850 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1851 if (x > es->format_rect.right)
1852 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1853 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1855 /* check if we are going to move too far */
1856 if(es->x_offset + dx + ww > es->text_width)
1857 dx = es->text_width - ww - es->x_offset;
1858 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1859 EDIT_EM_LineScroll_internal(es, dx, dy);
1861 } else {
1862 INT x;
1863 INT goal;
1864 INT format_width;
1866 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1867 format_width = es->format_rect.right - es->format_rect.left;
1868 if (x < es->format_rect.left) {
1869 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1870 do {
1871 es->x_offset--;
1872 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1873 } while ((x < goal) && es->x_offset);
1874 /* FIXME: use ScrollWindow() somehow to improve performance */
1875 EDIT_UpdateText(es, NULL, TRUE);
1876 } else if (x > es->format_rect.right) {
1877 INT x_last;
1878 INT len = get_text_length(es);
1879 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1880 do {
1881 es->x_offset++;
1882 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1883 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1884 } while ((x > goal) && (x_last > es->format_rect.right));
1885 /* FIXME: use ScrollWindow() somehow to improve performance */
1886 EDIT_UpdateText(es, NULL, TRUE);
1890 if(es->flags & EF_FOCUSED)
1891 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1895 /*********************************************************************
1897 * EDIT_MoveBackward
1900 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1902 INT e = es->selection_end;
1904 if (e) {
1905 e--;
1906 if ((es->style & ES_MULTILINE) && e &&
1907 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1908 e--;
1909 if (e && (es->text[e - 1] == '\r'))
1910 e--;
1913 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1914 EDIT_EM_ScrollCaret(es);
1918 /*********************************************************************
1920 * EDIT_MoveDown_ML
1922 * Only for multi line controls
1923 * Move the caret one line down, on a column with the nearest
1924 * x coordinate on the screen (might be a different column).
1927 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1929 INT s = es->selection_start;
1930 INT e = es->selection_end;
1931 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1932 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1933 INT x = (short)LOWORD(pos);
1934 INT y = (short)HIWORD(pos);
1936 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1937 if (!extend)
1938 s = e;
1939 EDIT_EM_SetSel(es, s, e, after_wrap);
1940 EDIT_EM_ScrollCaret(es);
1944 /*********************************************************************
1946 * EDIT_MoveEnd
1949 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1951 BOOL after_wrap = FALSE;
1952 INT e;
1954 /* Pass a high value in x to make sure of receiving the end of the line */
1955 if (!ctrl && (es->style & ES_MULTILINE))
1956 e = EDIT_CharFromPos(es, 0x3fffffff,
1957 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1958 else
1959 e = get_text_length(es);
1960 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1961 EDIT_EM_ScrollCaret(es);
1965 /*********************************************************************
1967 * EDIT_MoveForward
1970 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1972 INT e = es->selection_end;
1974 if (es->text[e]) {
1975 e++;
1976 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1977 if (es->text[e] == '\n')
1978 e++;
1979 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1980 e += 2;
1983 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1984 EDIT_EM_ScrollCaret(es);
1988 /*********************************************************************
1990 * EDIT_MoveHome
1992 * Home key: move to beginning of line.
1995 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1997 INT e;
1999 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2000 if (!ctrl && (es->style & ES_MULTILINE))
2001 e = EDIT_CharFromPos(es, -es->x_offset,
2002 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2003 else
2004 e = 0;
2005 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2006 EDIT_EM_ScrollCaret(es);
2010 /*********************************************************************
2012 * EDIT_MovePageDown_ML
2014 * Only for multi line controls
2015 * Move the caret one page down, on a column with the nearest
2016 * x coordinate on the screen (might be a different column).
2019 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2021 INT s = es->selection_start;
2022 INT e = es->selection_end;
2023 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2024 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2025 INT x = (short)LOWORD(pos);
2026 INT y = (short)HIWORD(pos);
2028 e = EDIT_CharFromPos(es, x,
2029 y + (es->format_rect.bottom - es->format_rect.top),
2030 &after_wrap);
2031 if (!extend)
2032 s = e;
2033 EDIT_EM_SetSel(es, s, e, after_wrap);
2034 EDIT_EM_ScrollCaret(es);
2038 /*********************************************************************
2040 * EDIT_MovePageUp_ML
2042 * Only for multi line controls
2043 * Move the caret one page up, on a column with the nearest
2044 * x coordinate on the screen (might be a different column).
2047 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2049 INT s = es->selection_start;
2050 INT e = es->selection_end;
2051 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2052 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2053 INT x = (short)LOWORD(pos);
2054 INT y = (short)HIWORD(pos);
2056 e = EDIT_CharFromPos(es, x,
2057 y - (es->format_rect.bottom - es->format_rect.top),
2058 &after_wrap);
2059 if (!extend)
2060 s = e;
2061 EDIT_EM_SetSel(es, s, e, after_wrap);
2062 EDIT_EM_ScrollCaret(es);
2066 /*********************************************************************
2068 * EDIT_MoveUp_ML
2070 * Only for multi line controls
2071 * Move the caret one line up, on a column with the nearest
2072 * x coordinate on the screen (might be a different column).
2075 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2077 INT s = es->selection_start;
2078 INT e = es->selection_end;
2079 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2080 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2081 INT x = (short)LOWORD(pos);
2082 INT y = (short)HIWORD(pos);
2084 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2085 if (!extend)
2086 s = e;
2087 EDIT_EM_SetSel(es, s, e, after_wrap);
2088 EDIT_EM_ScrollCaret(es);
2092 /*********************************************************************
2094 * EDIT_MoveWordBackward
2097 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2099 INT s = es->selection_start;
2100 INT e = es->selection_end;
2101 INT l;
2102 INT ll;
2103 INT li;
2105 l = EDIT_EM_LineFromChar(es, e);
2106 ll = EDIT_EM_LineLength(es, e);
2107 li = EDIT_EM_LineIndex(es, l);
2108 if (e - li == 0) {
2109 if (l) {
2110 li = EDIT_EM_LineIndex(es, l - 1);
2111 e = li + EDIT_EM_LineLength(es, li);
2113 } else {
2114 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2116 if (!extend)
2117 s = e;
2118 EDIT_EM_SetSel(es, s, e, FALSE);
2119 EDIT_EM_ScrollCaret(es);
2123 /*********************************************************************
2125 * EDIT_MoveWordForward
2128 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2130 INT s = es->selection_start;
2131 INT e = es->selection_end;
2132 INT l;
2133 INT ll;
2134 INT li;
2136 l = EDIT_EM_LineFromChar(es, e);
2137 ll = EDIT_EM_LineLength(es, e);
2138 li = EDIT_EM_LineIndex(es, l);
2139 if (e - li == ll) {
2140 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2141 e = EDIT_EM_LineIndex(es, l + 1);
2142 } else {
2143 e = li + EDIT_CallWordBreakProc(es,
2144 li, e - li + 1, ll, WB_RIGHT);
2146 if (!extend)
2147 s = e;
2148 EDIT_EM_SetSel(es, s, e, FALSE);
2149 EDIT_EM_ScrollCaret(es);
2153 /*********************************************************************
2155 * EDIT_PaintText
2158 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2160 COLORREF BkColor;
2161 COLORREF TextColor;
2162 LOGFONTW underline_font;
2163 HFONT hUnderline = 0;
2164 HFONT old_font = 0;
2165 INT ret;
2166 INT li;
2167 INT BkMode;
2168 SIZE size;
2170 if (!count)
2171 return 0;
2172 BkMode = GetBkMode(dc);
2173 BkColor = GetBkColor(dc);
2174 TextColor = GetTextColor(dc);
2175 if (rev) {
2176 if (es->composition_len == 0)
2178 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2179 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2180 SetBkMode( dc, OPAQUE);
2182 else
2184 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2185 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2186 underline_font.lfUnderline = TRUE;
2187 hUnderline = CreateFontIndirectW(&underline_font);
2188 old_font = SelectObject(dc,hUnderline);
2191 li = EDIT_EM_LineIndex(es, line);
2192 if (es->style & ES_MULTILINE) {
2193 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2194 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2195 } else {
2196 TextOutW(dc, x, y, es->text + li + col, count);
2197 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2198 ret = size.cx;
2200 if (rev) {
2201 if (es->composition_len == 0)
2203 SetBkColor(dc, BkColor);
2204 SetTextColor(dc, TextColor);
2205 SetBkMode( dc, BkMode);
2207 else
2209 if (old_font)
2210 SelectObject(dc,old_font);
2211 if (hUnderline)
2212 DeleteObject(hUnderline);
2215 return ret;
2219 /*********************************************************************
2221 * EDIT_PaintLine
2224 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2226 INT s = 0;
2227 INT e = 0;
2228 INT li = 0;
2229 INT ll = 0;
2230 INT x;
2231 INT y;
2232 LRESULT pos;
2233 SCRIPT_STRING_ANALYSIS ssa;
2235 if (es->style & ES_MULTILINE) {
2236 INT vlc = get_vertical_line_count(es);
2238 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2239 return;
2240 } else if (line)
2241 return;
2243 TRACE("line=%d\n", line);
2245 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2246 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2247 x = (short)LOWORD(pos);
2248 y = (short)HIWORD(pos);
2250 if (es->style & ES_MULTILINE)
2252 int line_idx = line;
2253 x = -es->x_offset;
2254 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2256 LINEDEF *line_def = es->first_line_def;
2257 int w, lw;
2259 while (line_def && line_idx)
2261 line_def = line_def->next;
2262 line_idx--;
2264 w = es->format_rect.right - es->format_rect.left;
2265 lw = line_def->width;
2267 if (es->style & ES_RIGHT)
2268 x = w - (lw - x);
2269 else if (es->style & ES_CENTER)
2270 x += (w - lw) / 2;
2272 x += es->format_rect.left;
2275 if (rev)
2277 li = EDIT_EM_LineIndex(es, line);
2278 ll = EDIT_EM_LineLength(es, li);
2279 s = min(es->selection_start, es->selection_end);
2280 e = max(es->selection_start, es->selection_end);
2281 s = min(li + ll, max(li, s));
2282 e = min(li + ll, max(li, e));
2285 if (ssa)
2286 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2287 else if (rev && (s != e) &&
2288 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2289 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2290 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2291 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2292 } else
2293 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2297 /*********************************************************************
2299 * EDIT_AdjustFormatRect
2301 * Adjusts the format rectangle for the current font and the
2302 * current client rectangle.
2305 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2307 RECT ClientRect;
2309 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2310 if (es->style & ES_MULTILINE)
2312 INT fw, vlc, max_x_offset, max_y_offset;
2314 vlc = get_vertical_line_count(es);
2315 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2317 /* correct es->x_offset */
2318 fw = es->format_rect.right - es->format_rect.left;
2319 max_x_offset = es->text_width - fw;
2320 if(max_x_offset < 0) max_x_offset = 0;
2321 if(es->x_offset > max_x_offset)
2322 es->x_offset = max_x_offset;
2324 /* correct es->y_offset */
2325 max_y_offset = es->line_count - vlc;
2326 if(max_y_offset < 0) max_y_offset = 0;
2327 if(es->y_offset > max_y_offset)
2328 es->y_offset = max_y_offset;
2330 /* force scroll info update */
2331 EDIT_UpdateScrollInfo(es);
2333 else
2334 /* Windows doesn't care to fix text placement for SL controls */
2335 es->format_rect.bottom = es->format_rect.top + es->line_height;
2337 /* Always stay within the client area */
2338 GetClientRect(es->hwndSelf, &ClientRect);
2339 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2341 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2342 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2344 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2348 /*********************************************************************
2350 * EDIT_SetRectNP
2352 * note: this is not (exactly) the handler called on EM_SETRECTNP
2353 * it is also used to set the rect of a single line control
2356 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2358 LONG_PTR ExStyle;
2359 INT bw, bh;
2360 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2362 CopyRect(&es->format_rect, rc);
2364 if (ExStyle & WS_EX_CLIENTEDGE) {
2365 es->format_rect.left++;
2366 es->format_rect.right--;
2368 if (es->format_rect.bottom - es->format_rect.top
2369 >= es->line_height + 2)
2371 es->format_rect.top++;
2372 es->format_rect.bottom--;
2375 else if (es->style & WS_BORDER) {
2376 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2377 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2378 InflateRect(&es->format_rect, -bw, 0);
2379 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2380 InflateRect(&es->format_rect, 0, -bh);
2383 es->format_rect.left += es->left_margin;
2384 es->format_rect.right -= es->right_margin;
2385 EDIT_AdjustFormatRect(es);
2389 /*********************************************************************
2391 * EM_CHARFROMPOS
2393 * returns line number (not index) in high-order word of result.
2394 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2395 * to Richedit, not to the edit control. Original documentation is valid.
2396 * FIXME: do the specs mean to return -1 if outside client area or
2397 * if outside formatting rectangle ???
2400 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2402 POINT pt;
2403 RECT rc;
2404 INT index;
2406 pt.x = x;
2407 pt.y = y;
2408 GetClientRect(es->hwndSelf, &rc);
2409 if (!PtInRect(&rc, pt))
2410 return -1;
2412 index = EDIT_CharFromPos(es, x, y, NULL);
2413 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2417 /*********************************************************************
2419 * EM_FMTLINES
2421 * Enable or disable soft breaks.
2423 * This means: insert or remove the soft linebreak character (\r\r\n).
2424 * Take care to check if the text still fits the buffer after insertion.
2425 * If not, notify with EN_ERRSPACE.
2428 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2430 es->flags &= ~EF_USE_SOFTBRK;
2431 if (add_eol) {
2432 es->flags |= EF_USE_SOFTBRK;
2433 FIXME("soft break enabled, not implemented\n");
2435 return add_eol;
2439 /*********************************************************************
2441 * EM_GETHANDLE
2443 * Hopefully this won't fire back at us.
2444 * We always start with a fixed buffer in the local heap.
2445 * Despite of the documentation says that the local heap is used
2446 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2447 * buffer on the local heap.
2450 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2452 HLOCAL hLocal;
2454 if (!(es->style & ES_MULTILINE))
2455 return 0;
2457 if(es->is_unicode)
2458 hLocal = es->hloc32W;
2459 else
2461 if(!es->hloc32A)
2463 CHAR *textA;
2464 UINT countA, alloc_size;
2465 TRACE("Allocating 32-bit ANSI alias buffer\n");
2466 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2467 alloc_size = ROUND_TO_GROW(countA);
2468 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2470 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2471 return 0;
2473 textA = LocalLock(es->hloc32A);
2474 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2475 LocalUnlock(es->hloc32A);
2477 hLocal = es->hloc32A;
2480 EDIT_UnlockBuffer(es, TRUE);
2482 /* The text buffer handle belongs to the app */
2483 es->hlocapp = hLocal;
2485 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2486 return hLocal;
2490 /*********************************************************************
2492 * EM_GETLINE
2495 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2497 LPWSTR src;
2498 INT line_len, dst_len;
2499 INT i;
2501 if (es->style & ES_MULTILINE) {
2502 if (line >= es->line_count)
2503 return 0;
2504 } else
2505 line = 0;
2506 i = EDIT_EM_LineIndex(es, line);
2507 src = es->text + i;
2508 line_len = EDIT_EM_LineLength(es, i);
2509 dst_len = *(WORD *)dst;
2510 if(unicode)
2512 if(dst_len <= line_len)
2514 memcpy(dst, src, dst_len * sizeof(WCHAR));
2515 return dst_len;
2517 else /* Append 0 if enough space */
2519 memcpy(dst, src, line_len * sizeof(WCHAR));
2520 dst[line_len] = 0;
2521 return line_len;
2524 else
2526 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2527 if(!ret && line_len) /* Insufficient buffer size */
2528 return dst_len;
2529 if(ret < dst_len) /* Append 0 if enough space */
2530 ((LPSTR)dst)[ret] = 0;
2531 return ret;
2536 /*********************************************************************
2538 * EM_GETSEL
2541 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2543 UINT s = es->selection_start;
2544 UINT e = es->selection_end;
2546 ORDER_UINT(s, e);
2547 if (start)
2548 *start = s;
2549 if (end)
2550 *end = e;
2551 return MAKELONG(s, e);
2555 /*********************************************************************
2557 * EM_REPLACESEL
2559 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2562 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2563 BOOL send_update, BOOL honor_limit)
2565 UINT tl = get_text_length(es);
2566 UINT utl;
2567 UINT s;
2568 UINT e;
2569 UINT i;
2570 UINT size;
2571 LPWSTR p;
2572 HRGN hrgn = 0;
2573 LPWSTR buf = NULL;
2574 UINT bufl;
2576 TRACE("%s, can_undo %d, send_update %d\n",
2577 debugstr_w(lpsz_replace), can_undo, send_update);
2579 s = es->selection_start;
2580 e = es->selection_end;
2582 EDIT_InvalidateUniscribeData(es);
2583 if ((s == e) && !strl)
2584 return;
2586 ORDER_UINT(s, e);
2588 size = tl - (e - s) + strl;
2589 if (!size)
2590 es->text_width = 0;
2592 /* Issue the EN_MAXTEXT notification and continue with replacing text
2593 * so that buffer limit is honored. */
2594 if ((honor_limit) && (size > es->buffer_limit)) {
2595 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2596 /* Buffer limit can be smaller than the actual length of text in combobox */
2597 if (es->buffer_limit < (tl - (e-s)))
2598 strl = 0;
2599 else
2600 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2603 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2604 return;
2606 if (e != s) {
2607 /* there is something to be deleted */
2608 TRACE("deleting stuff.\n");
2609 bufl = e - s;
2610 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2611 if (!buf) return;
2612 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2613 buf[bufl] = 0; /* ensure 0 termination */
2614 /* now delete */
2615 strcpyW(es->text + s, es->text + e);
2616 text_buffer_changed(es);
2618 if (strl) {
2619 /* there is an insertion */
2620 tl = get_text_length(es);
2621 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));
2622 for (p = es->text + tl ; p >= es->text + s ; p--)
2623 p[strl] = p[0];
2624 for (i = 0 , p = es->text + s ; i < strl ; i++)
2625 p[i] = lpsz_replace[i];
2626 if(es->style & ES_UPPERCASE)
2627 CharUpperBuffW(p, strl);
2628 else if(es->style & ES_LOWERCASE)
2629 CharLowerBuffW(p, strl);
2630 text_buffer_changed(es);
2632 if (es->style & ES_MULTILINE)
2634 INT st = min(es->selection_start, es->selection_end);
2635 INT vlc = get_vertical_line_count(es);
2637 hrgn = CreateRectRgn(0, 0, 0, 0);
2638 EDIT_BuildLineDefs_ML(es, st, st + strl,
2639 strl - abs(es->selection_end - es->selection_start), hrgn);
2640 /* if text is too long undo all changes */
2641 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2642 if (strl)
2643 strcpyW(es->text + e, es->text + e + strl);
2644 if (e != s)
2645 for (i = 0 , p = es->text ; i < e - s ; i++)
2646 p[i + s] = buf[i];
2647 text_buffer_changed(es);
2648 EDIT_BuildLineDefs_ML(es, s, e,
2649 abs(es->selection_end - es->selection_start) - strl, hrgn);
2650 strl = 0;
2651 e = s;
2652 hrgn = CreateRectRgn(0, 0, 0, 0);
2653 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2656 else {
2657 INT fw = es->format_rect.right - es->format_rect.left;
2658 EDIT_InvalidateUniscribeData(es);
2659 EDIT_CalcLineWidth_SL(es);
2660 /* remove chars that don't fit */
2661 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2662 while ((es->text_width > fw) && s + strl >= s) {
2663 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2664 strl--;
2665 es->text_length = -1;
2666 EDIT_InvalidateUniscribeData(es);
2667 EDIT_CalcLineWidth_SL(es);
2669 text_buffer_changed(es);
2670 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2674 if (e != s) {
2675 if (can_undo) {
2676 utl = strlenW(es->undo_text);
2677 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2678 /* undo-buffer is extended to the right */
2679 EDIT_MakeUndoFit(es, utl + e - s);
2680 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2681 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2682 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2683 /* undo-buffer is extended to the left */
2684 EDIT_MakeUndoFit(es, utl + e - s);
2685 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2686 p[e - s] = p[0];
2687 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2688 p[i] = buf[i];
2689 es->undo_position = s;
2690 } else {
2691 /* new undo-buffer */
2692 EDIT_MakeUndoFit(es, e - s);
2693 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2694 es->undo_text[e - s] = 0; /* ensure 0 termination */
2695 es->undo_position = s;
2697 /* any deletion makes the old insertion-undo invalid */
2698 es->undo_insert_count = 0;
2699 } else
2700 EDIT_EM_EmptyUndoBuffer(es);
2702 if (strl) {
2703 if (can_undo) {
2704 if ((s == es->undo_position) ||
2705 ((es->undo_insert_count) &&
2706 (s == es->undo_position + es->undo_insert_count)))
2708 * insertion is new and at delete position or
2709 * an extension to either left or right
2711 es->undo_insert_count += strl;
2712 else {
2713 /* new insertion undo */
2714 es->undo_position = s;
2715 es->undo_insert_count = strl;
2716 /* new insertion makes old delete-buffer invalid */
2717 *es->undo_text = '\0';
2719 } else
2720 EDIT_EM_EmptyUndoBuffer(es);
2723 HeapFree(GetProcessHeap(), 0, buf);
2725 s += strl;
2727 /* If text has been deleted and we're right or center aligned then scroll rightward */
2728 if (es->style & (ES_RIGHT | ES_CENTER))
2730 INT delta = strl - abs(es->selection_end - es->selection_start);
2732 if (delta < 0 && es->x_offset)
2734 if (abs(delta) > es->x_offset)
2735 es->x_offset = 0;
2736 else
2737 es->x_offset += delta;
2741 EDIT_EM_SetSel(es, s, s, FALSE);
2742 es->flags |= EF_MODIFIED;
2743 if (send_update) es->flags |= EF_UPDATE;
2744 if (hrgn)
2746 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2747 DeleteObject(hrgn);
2749 else
2750 EDIT_UpdateText(es, NULL, TRUE);
2752 EDIT_EM_ScrollCaret(es);
2754 /* force scroll info update */
2755 EDIT_UpdateScrollInfo(es);
2758 if(send_update || (es->flags & EF_UPDATE))
2760 es->flags &= ~EF_UPDATE;
2761 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2763 EDIT_InvalidateUniscribeData(es);
2767 /*********************************************************************
2769 * EM_SETHANDLE
2771 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2774 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2776 if (!(es->style & ES_MULTILINE))
2777 return;
2779 if (!hloc) {
2780 WARN("called with NULL handle\n");
2781 return;
2784 EDIT_UnlockBuffer(es, TRUE);
2786 if(es->is_unicode)
2788 if(es->hloc32A)
2790 LocalFree(es->hloc32A);
2791 es->hloc32A = NULL;
2793 es->hloc32W = hloc;
2795 else
2797 INT countW, countA;
2798 HLOCAL hloc32W_new;
2799 WCHAR *textW;
2800 CHAR *textA;
2802 countA = LocalSize(hloc);
2803 textA = LocalLock(hloc);
2804 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2805 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2807 ERR("Could not allocate new unicode buffer\n");
2808 return;
2810 textW = LocalLock(hloc32W_new);
2811 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2812 LocalUnlock(hloc32W_new);
2813 LocalUnlock(hloc);
2815 if(es->hloc32W)
2816 LocalFree(es->hloc32W);
2818 es->hloc32W = hloc32W_new;
2819 es->hloc32A = hloc;
2822 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2824 /* The text buffer handle belongs to the control */
2825 es->hlocapp = NULL;
2827 EDIT_LockBuffer(es);
2828 text_buffer_changed(es);
2830 es->x_offset = es->y_offset = 0;
2831 es->selection_start = es->selection_end = 0;
2832 EDIT_EM_EmptyUndoBuffer(es);
2833 es->flags &= ~EF_MODIFIED;
2834 es->flags &= ~EF_UPDATE;
2835 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2836 EDIT_UpdateText(es, NULL, TRUE);
2837 EDIT_EM_ScrollCaret(es);
2838 /* force scroll info update */
2839 EDIT_UpdateScrollInfo(es);
2843 /*********************************************************************
2845 * EM_SETLIMITTEXT
2847 * NOTE: this version currently implements WinNT limits
2850 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2852 if (!limit) limit = ~0u;
2853 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2854 es->buffer_limit = limit;
2858 /*********************************************************************
2860 * EM_SETMARGINS
2862 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2863 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2864 * margin according to the textmetrics of the current font.
2866 * When EC_USEFONTINFO is used in the non_cjk case the margins only
2867 * change if the edit control is equal to or larger than a certain
2868 * size. Though there is an exception for the empty client rect case
2869 * with small font sizes.
2871 static BOOL is_cjk(UINT charset)
2873 switch(charset)
2875 case SHIFTJIS_CHARSET:
2876 case HANGUL_CHARSET:
2877 case GB2312_CHARSET:
2878 case CHINESEBIG5_CHARSET:
2879 return TRUE;
2881 /* HANGUL_CHARSET is strange, though treated as CJK by Win 8, it is
2882 * not by other versions including Win 10. */
2883 return FALSE;
2886 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2887 WORD left, WORD right, BOOL repaint)
2889 TEXTMETRICW tm;
2890 INT default_left_margin = 0; /* in pixels */
2891 INT default_right_margin = 0; /* in pixels */
2893 /* Set the default margins depending on the font */
2894 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2895 HDC dc = GetDC(es->hwndSelf);
2896 HFONT old_font = SelectObject(dc, es->font);
2897 LONG width = GdiGetCharDimensions(dc, &tm, NULL);
2898 RECT rc;
2900 /* The default margins are only non zero for TrueType or Vector fonts */
2901 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2902 if (!is_cjk(tm.tmCharSet)) {
2903 default_left_margin = width / 2;
2904 default_right_margin = width / 2;
2906 GetClientRect(es->hwndSelf, &rc);
2907 if (rc.right - rc.left < (width / 2 + width) * 2 &&
2908 (width >= 28 || !IsRectEmpty(&rc)) ) {
2909 default_left_margin = es->left_margin;
2910 default_right_margin = es->right_margin;
2912 } else {
2913 /* FIXME: figure out the CJK values. They are not affected by the client rect. */
2914 default_left_margin = width / 2;
2915 default_right_margin = width / 2;
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, ulength, 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, ptr;
3080 int len;
3082 /* Protect read-only edit control from modification */
3083 if(es->style & ES_READONLY)
3084 return;
3086 OpenClipboard(es->hwndSelf);
3087 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3088 src = GlobalLock(hsrc);
3089 len = strlenW(src);
3090 /* Protect single-line edit against pasting new line character */
3091 if (!(es->style & ES_MULTILINE) && ((ptr = strchrW(src, '\n')))) {
3092 len = ptr - src;
3093 if (len && src[len - 1] == '\r')
3094 --len;
3096 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3097 GlobalUnlock(hsrc);
3099 else if (es->style & ES_PASSWORD) {
3100 /* clear selected text in password edit box even with empty clipboard */
3101 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3103 CloseClipboard();
3107 /*********************************************************************
3109 * WM_COPY
3112 static void EDIT_WM_Copy(EDITSTATE *es)
3114 INT s = min(es->selection_start, es->selection_end);
3115 INT e = max(es->selection_start, es->selection_end);
3116 HGLOBAL hdst;
3117 LPWSTR dst;
3118 DWORD len;
3120 if (e == s) return;
3122 len = e - s;
3123 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3124 dst = GlobalLock(hdst);
3125 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3126 dst[len] = 0; /* ensure 0 termination */
3127 TRACE("%s\n", debugstr_w(dst));
3128 GlobalUnlock(hdst);
3129 OpenClipboard(es->hwndSelf);
3130 EmptyClipboard();
3131 SetClipboardData(CF_UNICODETEXT, hdst);
3132 CloseClipboard();
3136 /*********************************************************************
3138 * WM_CLEAR
3141 static inline void EDIT_WM_Clear(EDITSTATE *es)
3143 /* Protect read-only edit control from modification */
3144 if(es->style & ES_READONLY)
3145 return;
3147 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3151 /*********************************************************************
3153 * WM_CUT
3156 static inline void EDIT_WM_Cut(EDITSTATE *es)
3158 EDIT_WM_Copy(es);
3159 EDIT_WM_Clear(es);
3163 /*********************************************************************
3165 * WM_CHAR
3168 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3170 BOOL control;
3172 control = GetKeyState(VK_CONTROL) & 0x8000;
3174 switch (c) {
3175 case '\r':
3176 /* If it's not a multiline edit box, it would be ignored below.
3177 * For multiline edit without ES_WANTRETURN, we have to make a
3178 * special case.
3180 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3181 if (EDIT_IsInsideDialog(es))
3182 break;
3183 case '\n':
3184 if (es->style & ES_MULTILINE) {
3185 if (es->style & ES_READONLY) {
3186 EDIT_MoveHome(es, FALSE, FALSE);
3187 EDIT_MoveDown_ML(es, FALSE);
3188 } else {
3189 static const WCHAR cr_lfW[] = {'\r','\n'};
3190 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, 2, TRUE, TRUE);
3193 break;
3194 case '\t':
3195 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3197 static const WCHAR tabW[] = {'\t'};
3198 if (EDIT_IsInsideDialog(es))
3199 break;
3200 EDIT_EM_ReplaceSel(es, TRUE, tabW, 1, TRUE, TRUE);
3202 break;
3203 case VK_BACK:
3204 if (!(es->style & ES_READONLY) && !control) {
3205 if (es->selection_start != es->selection_end)
3206 EDIT_WM_Clear(es);
3207 else {
3208 /* delete character left of caret */
3209 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3210 EDIT_MoveBackward(es, TRUE);
3211 EDIT_WM_Clear(es);
3214 break;
3215 case 0x03: /* ^C */
3216 if (!(es->style & ES_PASSWORD))
3217 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3218 break;
3219 case 0x16: /* ^V */
3220 if (!(es->style & ES_READONLY))
3221 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3222 break;
3223 case 0x18: /* ^X */
3224 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3225 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3226 break;
3227 case 0x1A: /* ^Z */
3228 if (!(es->style & ES_READONLY))
3229 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3230 break;
3232 default:
3233 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3234 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3235 break;
3237 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3238 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3239 break;
3241 return 1;
3245 /*********************************************************************
3247 * EDIT_ContextMenuCommand
3250 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3252 switch (id) {
3253 case EM_UNDO:
3254 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3255 break;
3256 case WM_CUT:
3257 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3258 break;
3259 case WM_COPY:
3260 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3261 break;
3262 case WM_PASTE:
3263 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3264 break;
3265 case WM_CLEAR:
3266 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3267 break;
3268 case EM_SETSEL:
3269 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3270 break;
3271 default:
3272 ERR("unknown menu item, please report\n");
3273 break;
3278 /*********************************************************************
3280 * WM_CONTEXTMENU
3282 * Note: the resource files resource/sysres_??.rc cannot define a
3283 * single popup menu. Hence we use a (dummy) menubar
3284 * containing the single popup menu as its first item.
3286 * FIXME: the message identifiers have been chosen arbitrarily,
3287 * hence we use MF_BYPOSITION.
3288 * We might as well use the "real" values (anybody knows ?)
3289 * The menu definition is in resources/sysres_??.rc.
3290 * Once these are OK, we better use MF_BYCOMMAND here
3291 * (as we do in EDIT_WM_Command()).
3294 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3296 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3297 HMENU popup = GetSubMenu(menu, 0);
3298 UINT start = es->selection_start;
3299 UINT end = es->selection_end;
3300 UINT cmd;
3302 ORDER_UINT(start, end);
3304 /* undo */
3305 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3306 /* cut */
3307 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3308 /* copy */
3309 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3310 /* paste */
3311 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3312 /* delete */
3313 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3314 /* select all */
3315 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3317 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3319 RECT rc;
3320 /* Windows places the menu at the edit's center in this case */
3321 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3322 x = rc.left + (rc.right - rc.left) / 2;
3323 y = rc.top + (rc.bottom - rc.top) / 2;
3326 if (!(es->flags & EF_FOCUSED))
3327 SetFocus(es->hwndSelf);
3329 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3330 x, y, 0, es->hwndSelf, NULL);
3332 if (cmd)
3333 EDIT_ContextMenuCommand(es, cmd);
3335 DestroyMenu(menu);
3339 /*********************************************************************
3341 * WM_GETTEXT
3344 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3346 if(!count) return 0;
3348 if(unicode)
3350 lstrcpynW(dst, es->text, count);
3351 return strlenW(dst);
3353 else
3355 LPSTR textA = (LPSTR)dst;
3356 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3357 textA[count - 1] = 0; /* ensure 0 termination */
3358 return strlen(textA);
3362 /*********************************************************************
3364 * EDIT_CheckCombo
3367 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3369 HWND hLBox = es->hwndListBox;
3370 HWND hCombo;
3371 BOOL bDropped;
3372 int nEUI;
3374 if (!hLBox)
3375 return FALSE;
3377 hCombo = GetParent(es->hwndSelf);
3378 bDropped = TRUE;
3379 nEUI = 0;
3381 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3383 if (key == VK_UP || key == VK_DOWN)
3385 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3386 nEUI = 1;
3388 if (msg == WM_KEYDOWN || nEUI)
3389 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3392 switch (msg)
3394 case WM_KEYDOWN:
3395 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3397 /* make sure ComboLBox pops up */
3398 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3399 key = VK_F4;
3400 nEUI = 2;
3403 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3404 break;
3406 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3407 if (nEUI)
3408 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3409 else
3410 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3411 break;
3414 if(nEUI == 2)
3415 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3417 return TRUE;
3421 /*********************************************************************
3423 * WM_KEYDOWN
3425 * Handling of special keys that don't produce a WM_CHAR
3426 * (i.e. non-printable keys) & Backspace & Delete
3429 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3431 BOOL shift;
3432 BOOL control;
3434 if (GetKeyState(VK_MENU) & 0x8000)
3435 return 0;
3437 shift = GetKeyState(VK_SHIFT) & 0x8000;
3438 control = GetKeyState(VK_CONTROL) & 0x8000;
3440 switch (key) {
3441 case VK_F4:
3442 case VK_UP:
3443 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3444 break;
3446 /* fall through */
3447 case VK_LEFT:
3448 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3449 EDIT_MoveUp_ML(es, shift);
3450 else
3451 if (control)
3452 EDIT_MoveWordBackward(es, shift);
3453 else
3454 EDIT_MoveBackward(es, shift);
3455 break;
3456 case VK_DOWN:
3457 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3458 break;
3459 /* fall through */
3460 case VK_RIGHT:
3461 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3462 EDIT_MoveDown_ML(es, shift);
3463 else if (control)
3464 EDIT_MoveWordForward(es, shift);
3465 else
3466 EDIT_MoveForward(es, shift);
3467 break;
3468 case VK_HOME:
3469 EDIT_MoveHome(es, shift, control);
3470 break;
3471 case VK_END:
3472 EDIT_MoveEnd(es, shift, control);
3473 break;
3474 case VK_PRIOR:
3475 if (es->style & ES_MULTILINE)
3476 EDIT_MovePageUp_ML(es, shift);
3477 else
3478 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3479 break;
3480 case VK_NEXT:
3481 if (es->style & ES_MULTILINE)
3482 EDIT_MovePageDown_ML(es, shift);
3483 else
3484 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3485 break;
3486 case VK_DELETE:
3487 if (!(es->style & ES_READONLY) && !(shift && control)) {
3488 if (es->selection_start != es->selection_end) {
3489 if (shift)
3490 EDIT_WM_Cut(es);
3491 else
3492 EDIT_WM_Clear(es);
3493 } else {
3494 if (shift) {
3495 /* delete character left of caret */
3496 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3497 EDIT_MoveBackward(es, TRUE);
3498 EDIT_WM_Clear(es);
3499 } else if (control) {
3500 /* delete to end of line */
3501 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3502 EDIT_MoveEnd(es, TRUE, FALSE);
3503 EDIT_WM_Clear(es);
3504 } else {
3505 /* delete character right of caret */
3506 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3507 EDIT_MoveForward(es, TRUE);
3508 EDIT_WM_Clear(es);
3512 break;
3513 case VK_INSERT:
3514 if (shift) {
3515 if (!(es->style & ES_READONLY))
3516 EDIT_WM_Paste(es);
3517 } else if (control)
3518 EDIT_WM_Copy(es);
3519 break;
3520 case VK_RETURN:
3521 /* If the edit doesn't want the return send a message to the default object */
3522 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3524 DWORD dw;
3526 if (!EDIT_IsInsideDialog(es)) break;
3527 if (control) break;
3528 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3529 if (HIWORD(dw) == DC_HASDEFID)
3531 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3532 if (hwDefCtrl)
3534 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3535 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3539 break;
3540 case VK_ESCAPE:
3541 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3542 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3543 break;
3544 case VK_TAB:
3545 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3546 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3547 break;
3549 return TRUE;
3553 /*********************************************************************
3555 * WM_KILLFOCUS
3558 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3560 es->flags &= ~EF_FOCUSED;
3561 DestroyCaret();
3562 if(!(es->style & ES_NOHIDESEL))
3563 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3564 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3565 /* throw away left over scroll when we lose focus */
3566 es->wheelDeltaRemainder = 0;
3567 return 0;
3571 /*********************************************************************
3573 * WM_LBUTTONDBLCLK
3575 * The caret position has been set on the WM_LBUTTONDOWN message
3578 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3580 INT s;
3581 INT e = es->selection_end;
3582 INT l;
3583 INT li;
3584 INT ll;
3586 es->bCaptureState = TRUE;
3587 SetCapture(es->hwndSelf);
3589 l = EDIT_EM_LineFromChar(es, e);
3590 li = EDIT_EM_LineIndex(es, l);
3591 ll = EDIT_EM_LineLength(es, e);
3592 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3593 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3594 EDIT_EM_SetSel(es, s, e, FALSE);
3595 EDIT_EM_ScrollCaret(es);
3596 es->region_posx = es->region_posy = 0;
3597 SetTimer(es->hwndSelf, 0, 100, NULL);
3598 return 0;
3602 /*********************************************************************
3604 * WM_LBUTTONDOWN
3607 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3609 INT e;
3610 BOOL after_wrap;
3612 es->bCaptureState = TRUE;
3613 SetCapture(es->hwndSelf);
3614 EDIT_ConfinePoint(es, &x, &y);
3615 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3616 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3617 EDIT_EM_ScrollCaret(es);
3618 es->region_posx = es->region_posy = 0;
3619 SetTimer(es->hwndSelf, 0, 100, NULL);
3621 if (!(es->flags & EF_FOCUSED))
3622 SetFocus(es->hwndSelf);
3624 return 0;
3628 /*********************************************************************
3630 * WM_LBUTTONUP
3633 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3635 if (es->bCaptureState) {
3636 KillTimer(es->hwndSelf, 0);
3637 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3639 es->bCaptureState = FALSE;
3640 return 0;
3644 /*********************************************************************
3646 * WM_MBUTTONDOWN
3649 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3651 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3652 return 0;
3656 /*********************************************************************
3658 * WM_MOUSEMOVE
3661 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3663 INT e;
3664 BOOL after_wrap;
3665 INT prex, prey;
3667 /* If the mouse has been captured by process other than the edit control itself,
3668 * the windows edit controls will not select the strings with mouse move.
3670 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3671 return 0;
3674 * FIXME: gotta do some scrolling if outside client
3675 * area. Maybe reset the timer ?
3677 prex = x; prey = y;
3678 EDIT_ConfinePoint(es, &x, &y);
3679 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3680 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3681 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3682 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3683 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3684 return 0;
3688 /*********************************************************************
3690 * WM_PAINT
3693 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3695 PAINTSTRUCT ps;
3696 INT i;
3697 HDC dc;
3698 HFONT old_font = 0;
3699 RECT rc;
3700 RECT rcClient;
3701 RECT rcLine;
3702 RECT rcRgn;
3703 HBRUSH brush;
3704 HBRUSH old_brush;
3705 INT bw, bh;
3706 BOOL rev = es->bEnableState &&
3707 ((es->flags & EF_FOCUSED) ||
3708 (es->style & ES_NOHIDESEL));
3709 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3711 /* The dc we use for calculating may not be the one we paint into.
3712 This is the safest action. */
3713 EDIT_InvalidateUniscribeData(es);
3714 GetClientRect(es->hwndSelf, &rcClient);
3716 /* get the background brush */
3717 brush = EDIT_NotifyCtlColor(es, dc);
3719 /* paint the border and the background */
3720 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3722 if(es->style & WS_BORDER) {
3723 bw = GetSystemMetrics(SM_CXBORDER);
3724 bh = GetSystemMetrics(SM_CYBORDER);
3725 rc = rcClient;
3726 if(es->style & ES_MULTILINE) {
3727 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3728 if(es->style & WS_VSCROLL) rc.right+=bw;
3731 /* Draw the frame. Same code as in nonclient.c */
3732 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3733 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3734 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3735 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3736 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3737 SelectObject(dc, old_brush);
3739 /* Keep the border clean */
3740 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3741 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3744 GetClipBox(dc, &rc);
3745 FillRect(dc, &rc, brush);
3747 IntersectClipRect(dc, es->format_rect.left,
3748 es->format_rect.top,
3749 es->format_rect.right,
3750 es->format_rect.bottom);
3751 if (es->style & ES_MULTILINE) {
3752 rc = rcClient;
3753 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3755 if (es->font)
3756 old_font = SelectObject(dc, es->font);
3758 if (!es->bEnableState)
3759 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3760 GetClipBox(dc, &rcRgn);
3761 if (es->style & ES_MULTILINE) {
3762 INT vlc = get_vertical_line_count(es);
3763 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3764 EDIT_UpdateUniscribeData(es, dc, i);
3765 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3766 if (IntersectRect(&rc, &rcRgn, &rcLine))
3767 EDIT_PaintLine(es, dc, i, rev);
3769 } else {
3770 EDIT_UpdateUniscribeData(es, dc, 0);
3771 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3772 if (IntersectRect(&rc, &rcRgn, &rcLine))
3773 EDIT_PaintLine(es, dc, 0, rev);
3775 if (es->font)
3776 SelectObject(dc, old_font);
3778 if (!hdc)
3779 EndPaint(es->hwndSelf, &ps);
3783 /*********************************************************************
3785 * WM_SETFOCUS
3788 static void EDIT_WM_SetFocus(EDITSTATE *es)
3790 es->flags |= EF_FOCUSED;
3792 if (!(es->style & ES_NOHIDESEL))
3793 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3795 /* single line edit updates itself */
3796 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3798 HDC hdc = GetDC(es->hwndSelf);
3799 EDIT_WM_Paint(es, hdc);
3800 ReleaseDC(es->hwndSelf, hdc);
3803 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3804 EDIT_SetCaretPos(es, es->selection_end,
3805 es->flags & EF_AFTER_WRAP);
3806 ShowCaret(es->hwndSelf);
3807 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3811 /*********************************************************************
3813 * WM_SETFONT
3815 * With Win95 look the margins are set to default font value unless
3816 * the system font (font == 0) is being set, in which case they are left
3817 * unchanged.
3820 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3822 TEXTMETRICW tm;
3823 HDC dc;
3824 HFONT old_font = 0;
3825 RECT clientRect;
3827 es->font = font;
3828 EDIT_InvalidateUniscribeData(es);
3829 dc = GetDC(es->hwndSelf);
3830 if (font)
3831 old_font = SelectObject(dc, font);
3832 GetTextMetricsW(dc, &tm);
3833 es->line_height = tm.tmHeight;
3834 es->char_width = tm.tmAveCharWidth;
3835 if (font)
3836 SelectObject(dc, old_font);
3837 ReleaseDC(es->hwndSelf, dc);
3839 /* Reset the format rect and the margins */
3840 GetClientRect(es->hwndSelf, &clientRect);
3841 EDIT_SetRectNP(es, &clientRect);
3842 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3843 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3845 if (es->style & ES_MULTILINE)
3846 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3847 else
3848 EDIT_CalcLineWidth_SL(es);
3850 if (redraw)
3851 EDIT_UpdateText(es, NULL, TRUE);
3852 if (es->flags & EF_FOCUSED) {
3853 DestroyCaret();
3854 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3855 EDIT_SetCaretPos(es, es->selection_end,
3856 es->flags & EF_AFTER_WRAP);
3857 ShowCaret(es->hwndSelf);
3862 /*********************************************************************
3864 * WM_SETTEXT
3866 * NOTES
3867 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3868 * The modified flag is reset. No notifications are sent.
3870 * For single-line controls, reception of WM_SETTEXT triggers:
3871 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3874 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3876 LPWSTR textW = NULL;
3877 if (!unicode && text)
3879 LPCSTR textA = (LPCSTR)text;
3880 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3881 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3882 if (textW)
3883 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3884 text = textW;
3887 if (es->flags & EF_UPDATE)
3888 /* fixed this bug once; complain if we see it about to happen again. */
3889 ERR("SetSel may generate UPDATE message whose handler may reset "
3890 "selection.\n");
3892 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3893 if (text)
3895 TRACE("%s\n", debugstr_w(text));
3896 EDIT_EM_ReplaceSel(es, FALSE, text, strlenW(text), FALSE, FALSE);
3897 if(!unicode)
3898 HeapFree(GetProcessHeap(), 0, textW);
3900 else
3902 TRACE("<NULL>\n");
3903 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3905 es->x_offset = 0;
3906 es->flags &= ~EF_MODIFIED;
3907 EDIT_EM_SetSel(es, 0, 0, FALSE);
3909 /* Send the notification after the selection start and end have been set
3910 * edit control doesn't send notification on WM_SETTEXT
3911 * if it is multiline, or it is part of combobox
3913 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3915 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3916 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3918 EDIT_EM_ScrollCaret(es);
3919 EDIT_UpdateScrollInfo(es);
3920 EDIT_InvalidateUniscribeData(es);
3924 /*********************************************************************
3926 * WM_SIZE
3929 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3931 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3932 RECT rc;
3933 GetClientRect(es->hwndSelf, &rc);
3934 EDIT_SetRectNP(es, &rc);
3935 EDIT_UpdateText(es, NULL, TRUE);
3940 /*********************************************************************
3942 * WM_STYLECHANGED
3944 * This message is sent by SetWindowLong on having changed either the Style
3945 * or the extended style.
3947 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3949 * See also EDIT_WM_NCCreate
3951 * It appears that the Windows version of the edit control allows the style
3952 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3953 * style variable which will generally be different. In this function we
3954 * update the internal style based on what changed in the externally visible
3955 * style.
3957 * Much of this content as based upon the MSDN, especially:
3958 * Platform SDK Documentation -> User Interface Services ->
3959 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3960 * Edit Control Styles
3962 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3964 if (GWL_STYLE == which) {
3965 DWORD style_change_mask;
3966 DWORD new_style;
3967 /* Only a subset of changes can be applied after the control
3968 * has been created.
3970 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3971 ES_NUMBER;
3972 if (es->style & ES_MULTILINE)
3973 style_change_mask |= ES_WANTRETURN;
3975 new_style = style->styleNew & style_change_mask;
3977 /* Number overrides lowercase overrides uppercase (at least it
3978 * does in Win95). However I'll bet that ES_NUMBER would be
3979 * invalid under Win 3.1.
3981 if (new_style & ES_NUMBER) {
3982 ; /* do not override the ES_NUMBER */
3983 } else if (new_style & ES_LOWERCASE) {
3984 new_style &= ~ES_UPPERCASE;
3987 es->style = (es->style & ~style_change_mask) | new_style;
3988 } else if (GWL_EXSTYLE == which) {
3989 ; /* FIXME - what is needed here */
3990 } else {
3991 WARN ("Invalid style change %ld\n",which);
3994 return 0;
3997 /*********************************************************************
3999 * WM_SYSKEYDOWN
4002 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4004 if ((key == VK_BACK) && (key_data & 0x2000)) {
4005 if (EDIT_EM_CanUndo(es))
4006 EDIT_EM_Undo(es);
4007 return 0;
4008 } else if (key == VK_UP || key == VK_DOWN) {
4009 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4010 return 0;
4012 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4016 /*********************************************************************
4018 * WM_TIMER
4021 static void EDIT_WM_Timer(EDITSTATE *es)
4023 if (es->region_posx < 0) {
4024 EDIT_MoveBackward(es, TRUE);
4025 } else if (es->region_posx > 0) {
4026 EDIT_MoveForward(es, TRUE);
4029 * FIXME: gotta do some vertical scrolling here, like
4030 * EDIT_EM_LineScroll(hwnd, 0, 1);
4034 /*********************************************************************
4036 * WM_HSCROLL
4039 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4041 INT dx;
4042 INT fw;
4044 if (!(es->style & ES_MULTILINE))
4045 return 0;
4047 if (!(es->style & ES_AUTOHSCROLL))
4048 return 0;
4050 dx = 0;
4051 fw = es->format_rect.right - es->format_rect.left;
4052 switch (action) {
4053 case SB_LINELEFT:
4054 TRACE("SB_LINELEFT\n");
4055 if (es->x_offset)
4056 dx = -es->char_width;
4057 break;
4058 case SB_LINERIGHT:
4059 TRACE("SB_LINERIGHT\n");
4060 if (es->x_offset < es->text_width)
4061 dx = es->char_width;
4062 break;
4063 case SB_PAGELEFT:
4064 TRACE("SB_PAGELEFT\n");
4065 if (es->x_offset)
4066 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4067 break;
4068 case SB_PAGERIGHT:
4069 TRACE("SB_PAGERIGHT\n");
4070 if (es->x_offset < es->text_width)
4071 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4072 break;
4073 case SB_LEFT:
4074 TRACE("SB_LEFT\n");
4075 if (es->x_offset)
4076 dx = -es->x_offset;
4077 break;
4078 case SB_RIGHT:
4079 TRACE("SB_RIGHT\n");
4080 if (es->x_offset < es->text_width)
4081 dx = es->text_width - es->x_offset;
4082 break;
4083 case SB_THUMBTRACK:
4084 TRACE("SB_THUMBTRACK %d\n", pos);
4085 es->flags |= EF_HSCROLL_TRACK;
4086 if(es->style & WS_HSCROLL)
4087 dx = pos - es->x_offset;
4088 else
4090 INT fw, new_x;
4091 /* Sanity check */
4092 if(pos < 0 || pos > 100) return 0;
4093 /* Assume default scroll range 0-100 */
4094 fw = es->format_rect.right - es->format_rect.left;
4095 new_x = pos * (es->text_width - fw) / 100;
4096 dx = es->text_width ? (new_x - es->x_offset) : 0;
4098 break;
4099 case SB_THUMBPOSITION:
4100 TRACE("SB_THUMBPOSITION %d\n", pos);
4101 es->flags &= ~EF_HSCROLL_TRACK;
4102 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4103 dx = pos - es->x_offset;
4104 else
4106 INT fw, new_x;
4107 /* Sanity check */
4108 if(pos < 0 || pos > 100) return 0;
4109 /* Assume default scroll range 0-100 */
4110 fw = es->format_rect.right - es->format_rect.left;
4111 new_x = pos * (es->text_width - fw) / 100;
4112 dx = es->text_width ? (new_x - es->x_offset) : 0;
4114 if (!dx) {
4115 /* force scroll info update */
4116 EDIT_UpdateScrollInfo(es);
4117 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4119 break;
4120 case SB_ENDSCROLL:
4121 TRACE("SB_ENDSCROLL\n");
4122 break;
4124 * FIXME : the next two are undocumented !
4125 * Are we doing the right thing ?
4126 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4127 * although it's also a regular control message.
4129 case EM_GETTHUMB: /* this one is used by NT notepad */
4131 LRESULT ret;
4132 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4133 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4134 else
4136 /* Assume default scroll range 0-100 */
4137 INT fw = es->format_rect.right - es->format_rect.left;
4138 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4140 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4141 return ret;
4143 case EM_LINESCROLL:
4144 TRACE("EM_LINESCROLL16\n");
4145 dx = pos;
4146 break;
4148 default:
4149 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4150 action, action);
4151 return 0;
4153 if (dx)
4155 INT fw = es->format_rect.right - es->format_rect.left;
4156 /* check if we are going to move too far */
4157 if(es->x_offset + dx + fw > es->text_width)
4158 dx = es->text_width - fw - es->x_offset;
4159 if(dx)
4160 EDIT_EM_LineScroll_internal(es, dx, 0);
4162 return 0;
4166 /*********************************************************************
4168 * WM_VSCROLL
4171 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4173 INT dy;
4175 if (!(es->style & ES_MULTILINE))
4176 return 0;
4178 if (!(es->style & ES_AUTOVSCROLL))
4179 return 0;
4181 dy = 0;
4182 switch (action) {
4183 case SB_LINEUP:
4184 case SB_LINEDOWN:
4185 case SB_PAGEUP:
4186 case SB_PAGEDOWN:
4187 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4188 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4189 (action == SB_PAGEUP ? "SB_PAGEUP" :
4190 "SB_PAGEDOWN"))));
4191 EDIT_EM_Scroll(es, action);
4192 return 0;
4193 case SB_TOP:
4194 TRACE("SB_TOP\n");
4195 dy = -es->y_offset;
4196 break;
4197 case SB_BOTTOM:
4198 TRACE("SB_BOTTOM\n");
4199 dy = es->line_count - 1 - es->y_offset;
4200 break;
4201 case SB_THUMBTRACK:
4202 TRACE("SB_THUMBTRACK %d\n", pos);
4203 es->flags |= EF_VSCROLL_TRACK;
4204 if(es->style & WS_VSCROLL)
4205 dy = pos - es->y_offset;
4206 else
4208 /* Assume default scroll range 0-100 */
4209 INT vlc, new_y;
4210 /* Sanity check */
4211 if(pos < 0 || pos > 100) return 0;
4212 vlc = get_vertical_line_count(es);
4213 new_y = pos * (es->line_count - vlc) / 100;
4214 dy = es->line_count ? (new_y - es->y_offset) : 0;
4215 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4216 es->line_count, es->y_offset, pos, dy);
4218 break;
4219 case SB_THUMBPOSITION:
4220 TRACE("SB_THUMBPOSITION %d\n", pos);
4221 es->flags &= ~EF_VSCROLL_TRACK;
4222 if(es->style & WS_VSCROLL)
4223 dy = pos - es->y_offset;
4224 else
4226 /* Assume default scroll range 0-100 */
4227 INT vlc, new_y;
4228 /* Sanity check */
4229 if(pos < 0 || pos > 100) return 0;
4230 vlc = get_vertical_line_count(es);
4231 new_y = pos * (es->line_count - vlc) / 100;
4232 dy = es->line_count ? (new_y - es->y_offset) : 0;
4233 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4234 es->line_count, es->y_offset, pos, dy);
4236 if (!dy)
4238 /* force scroll info update */
4239 EDIT_UpdateScrollInfo(es);
4240 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4242 break;
4243 case SB_ENDSCROLL:
4244 TRACE("SB_ENDSCROLL\n");
4245 break;
4247 * FIXME : the next two are undocumented !
4248 * Are we doing the right thing ?
4249 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4250 * although it's also a regular control message.
4252 case EM_GETTHUMB: /* this one is used by NT notepad */
4254 LRESULT ret;
4255 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4256 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4257 else
4259 /* Assume default scroll range 0-100 */
4260 INT vlc = get_vertical_line_count(es);
4261 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4263 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4264 return ret;
4266 case EM_LINESCROLL:
4267 TRACE("EM_LINESCROLL %d\n", pos);
4268 dy = pos;
4269 break;
4271 default:
4272 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4273 action, action);
4274 return 0;
4276 if (dy)
4277 EDIT_EM_LineScroll(es, 0, dy);
4278 return 0;
4281 /*********************************************************************
4283 * EM_GETTHUMB
4285 * FIXME: is this right ? (or should it be only VSCROLL)
4286 * (and maybe only for edit controls that really have their
4287 * own scrollbars) (and maybe only for multiline controls ?)
4288 * All in all: very poorly documented
4291 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4293 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4294 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4298 /********************************************************************
4300 * The Following code is to handle inline editing from IMEs
4303 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4305 LONG buflen;
4306 LPWSTR lpCompStr;
4307 LPSTR lpCompStrAttr = NULL;
4308 DWORD dwBufLenAttr;
4310 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4312 if (buflen < 0)
4314 return;
4317 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen);
4318 if (!lpCompStr)
4320 ERR("Unable to allocate IME CompositionString\n");
4321 return;
4324 if (buflen)
4325 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4327 if (CompFlag & GCS_COMPATTR)
4330 * We do not use the attributes yet. it would tell us what characters
4331 * are in transition and which are converted or decided upon
4333 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4334 if (dwBufLenAttr)
4336 dwBufLenAttr ++;
4337 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4338 if (!lpCompStrAttr)
4340 ERR("Unable to allocate IME Attribute String\n");
4341 HeapFree(GetProcessHeap(),0,lpCompStr);
4342 return;
4344 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4345 dwBufLenAttr);
4346 lpCompStrAttr[dwBufLenAttr] = 0;
4350 /* check for change in composition start */
4351 if (es->selection_end < es->composition_start)
4352 es->composition_start = es->selection_end;
4354 /* replace existing selection string */
4355 es->selection_start = es->composition_start;
4357 if (es->composition_len > 0)
4358 es->selection_end = es->composition_start + es->composition_len;
4359 else
4360 es->selection_end = es->selection_start;
4362 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4363 es->composition_len = abs(es->composition_start - es->selection_end);
4365 es->selection_start = es->composition_start;
4366 es->selection_end = es->selection_start + es->composition_len;
4368 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4369 HeapFree(GetProcessHeap(),0,lpCompStr);
4372 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4374 LONG buflen;
4375 LPWSTR lpResultStr;
4377 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4378 if (buflen <= 0)
4380 return;
4383 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4384 if (!lpResultStr)
4386 ERR("Unable to alloc buffer for IME string\n");
4387 return;
4390 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4392 /* check for change in composition start */
4393 if (es->selection_end < es->composition_start)
4394 es->composition_start = es->selection_end;
4396 es->selection_start = es->composition_start;
4397 es->selection_end = es->composition_start + es->composition_len;
4398 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4399 es->composition_start = es->selection_end;
4400 es->composition_len = 0;
4402 HeapFree(GetProcessHeap(),0,lpResultStr);
4405 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4407 HIMC hIMC;
4408 int cursor;
4410 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4412 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4413 es->composition_start = es->selection_end;
4416 hIMC = ImmGetContext(hwnd);
4417 if (!hIMC)
4418 return;
4420 if (CompFlag & GCS_RESULTSTR)
4422 EDIT_GetResultStr(hIMC, es);
4423 cursor = 0;
4425 else
4427 if (CompFlag & GCS_COMPSTR)
4428 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4429 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4431 ImmReleaseContext(hwnd, hIMC);
4432 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4436 /*********************************************************************
4438 * WM_NCCREATE
4440 * See also EDIT_WM_StyleChanged
4442 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4444 EDITSTATE *es;
4445 UINT alloc_size;
4447 TRACE("Creating %s edit control, style = %08x\n",
4448 unicode ? "Unicode" : "ANSI", lpcs->style);
4450 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4451 return FALSE;
4452 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4455 * Note: since the EDITSTATE has not been fully initialized yet,
4456 * we can't use any API calls that may send
4457 * WM_XXX messages before WM_NCCREATE is completed.
4460 es->is_unicode = unicode;
4461 es->style = lpcs->style;
4463 es->bEnableState = !(es->style & WS_DISABLED);
4465 es->hwndSelf = hwnd;
4466 /* Save parent, which will be notified by EN_* messages */
4467 es->hwndParent = lpcs->hwndParent;
4469 if (es->style & ES_COMBO)
4470 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4472 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4473 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4475 /* Number overrides lowercase overrides uppercase (at least it
4476 * does in Win95). However I'll bet that ES_NUMBER would be
4477 * invalid under Win 3.1.
4479 if (es->style & ES_NUMBER) {
4480 ; /* do not override the ES_NUMBER */
4481 } else if (es->style & ES_LOWERCASE) {
4482 es->style &= ~ES_UPPERCASE;
4484 if (es->style & ES_MULTILINE) {
4485 es->buffer_limit = BUFLIMIT_INITIAL;
4486 if (es->style & WS_VSCROLL)
4487 es->style |= ES_AUTOVSCROLL;
4488 if (es->style & WS_HSCROLL)
4489 es->style |= ES_AUTOHSCROLL;
4490 es->style &= ~ES_PASSWORD;
4491 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4492 /* Confirmed - RIGHT overrides CENTER */
4493 if (es->style & ES_RIGHT)
4494 es->style &= ~ES_CENTER;
4495 es->style &= ~WS_HSCROLL;
4496 es->style &= ~ES_AUTOHSCROLL;
4498 } else {
4499 es->buffer_limit = BUFLIMIT_INITIAL;
4500 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4501 es->style &= ~ES_CENTER;
4502 es->style &= ~WS_HSCROLL;
4503 es->style &= ~WS_VSCROLL;
4504 if (es->style & ES_PASSWORD)
4505 es->password_char = '*';
4508 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4509 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4510 goto cleanup;
4511 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4513 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4514 goto cleanup;
4515 es->undo_buffer_size = es->buffer_size;
4517 if (es->style & ES_MULTILINE)
4518 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4519 goto cleanup;
4520 es->line_count = 1;
4523 * In Win95 look and feel, the WS_BORDER style is replaced by the
4524 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4525 * control a nonclient area so we don't need to draw the border.
4526 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4527 * a nonclient area and we should handle painting the border ourselves.
4529 * When making modifications please ensure that the code still works
4530 * for edit controls created directly with style 0x50800000, exStyle 0
4531 * (which should have a single pixel border)
4533 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4534 es->style &= ~WS_BORDER;
4535 else if (es->style & WS_BORDER)
4536 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4538 return TRUE;
4540 cleanup:
4541 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4542 EDIT_InvalidateUniscribeData(es);
4543 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4544 HeapFree(GetProcessHeap(), 0, es->undo_text);
4545 if (es->hloc32W) LocalFree(es->hloc32W);
4546 HeapFree(GetProcessHeap(), 0, es->logAttr);
4547 HeapFree(GetProcessHeap(), 0, es);
4548 return FALSE;
4552 /*********************************************************************
4554 * WM_CREATE
4557 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4559 RECT clientRect;
4561 TRACE("%s\n", debugstr_w(name));
4563 * To initialize some final structure members, we call some helper
4564 * functions. However, since the EDITSTATE is not consistent (i.e.
4565 * not fully initialized), we should be very careful which
4566 * functions can be called, and in what order.
4568 EDIT_WM_SetFont(es, 0, FALSE);
4569 EDIT_EM_EmptyUndoBuffer(es);
4571 /* We need to calculate the format rect
4572 (applications may send EM_SETMARGINS before the control gets visible) */
4573 GetClientRect(es->hwndSelf, &clientRect);
4574 EDIT_SetRectNP(es, &clientRect);
4576 if (name && *name) {
4577 EDIT_EM_ReplaceSel(es, FALSE, name, strlenW(name), FALSE, FALSE);
4578 /* if we insert text to the editline, the text scrolls out
4579 * of the window, as the caret is placed after the insert
4580 * pos normally; thus we reset es->selection... to 0 and
4581 * update caret
4583 es->selection_start = es->selection_end = 0;
4584 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4585 * Messages are only to be sent when the USER does something to
4586 * change the contents. So I am removing this EN_CHANGE
4588 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4590 EDIT_EM_ScrollCaret(es);
4592 /* force scroll info update */
4593 EDIT_UpdateScrollInfo(es);
4594 /* The rule seems to return 1 here for success */
4595 /* Power Builder masked edit controls will crash */
4596 /* if not. */
4597 /* FIXME: is that in all cases so ? */
4598 return 1;
4602 /*********************************************************************
4604 * WM_NCDESTROY
4607 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4609 LINEDEF *pc, *pp;
4611 /* The app can own the text buffer handle */
4612 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4613 LocalFree(es->hloc32W);
4615 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4616 LocalFree(es->hloc32A);
4618 EDIT_InvalidateUniscribeData(es);
4619 pc = es->first_line_def;
4620 while (pc)
4622 pp = pc->next;
4623 HeapFree(GetProcessHeap(), 0, pc);
4624 pc = pp;
4627 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4628 HeapFree(GetProcessHeap(), 0, es->undo_text);
4629 HeapFree(GetProcessHeap(), 0, es);
4631 return 0;
4635 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4637 if(unicode)
4638 return DefWindowProcW(hwnd, msg, wParam, lParam);
4639 else
4640 return DefWindowProcA(hwnd, msg, wParam, lParam);
4643 /*********************************************************************
4645 * EditWndProc_common
4647 * The messages are in the order of the actual integer values
4648 * (which can be found in include/windows.h)
4650 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4652 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4653 LRESULT result = 0;
4655 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4657 if (!es && msg != WM_NCCREATE)
4658 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4660 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4662 switch (msg) {
4663 case EM_GETSEL:
4664 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4665 break;
4667 case EM_SETSEL:
4668 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4669 EDIT_EM_ScrollCaret(es);
4670 result = 1;
4671 break;
4673 case EM_GETRECT:
4674 if (lParam)
4675 *((LPRECT)lParam) = es->format_rect;
4676 break;
4678 case EM_SETRECT:
4679 if ((es->style & ES_MULTILINE) && lParam) {
4680 EDIT_SetRectNP(es, (LPRECT)lParam);
4681 EDIT_UpdateText(es, NULL, TRUE);
4683 break;
4685 case EM_SETRECTNP:
4686 if ((es->style & ES_MULTILINE) && lParam)
4687 EDIT_SetRectNP(es, (LPRECT)lParam);
4688 break;
4690 case EM_SCROLL:
4691 result = EDIT_EM_Scroll(es, (INT)wParam);
4692 break;
4694 case EM_LINESCROLL:
4695 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4696 break;
4698 case EM_SCROLLCARET:
4699 EDIT_EM_ScrollCaret(es);
4700 result = 1;
4701 break;
4703 case EM_GETMODIFY:
4704 result = ((es->flags & EF_MODIFIED) != 0);
4705 break;
4707 case EM_SETMODIFY:
4708 if (wParam)
4709 es->flags |= EF_MODIFIED;
4710 else
4711 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4712 break;
4714 case EM_GETLINECOUNT:
4715 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4716 break;
4718 case EM_LINEINDEX:
4719 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4720 break;
4722 case EM_SETHANDLE:
4723 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4724 break;
4726 case EM_GETHANDLE:
4727 result = (LRESULT)EDIT_EM_GetHandle(es);
4728 break;
4730 case EM_GETTHUMB:
4731 result = EDIT_EM_GetThumb(es);
4732 break;
4734 /* these messages missing from specs */
4735 case 0x00bf:
4736 case 0x00c0:
4737 case 0x00c3:
4738 case 0x00ca:
4739 FIXME("undocumented message 0x%x, please report\n", msg);
4740 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4741 break;
4743 case EM_LINELENGTH:
4744 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4745 break;
4747 case EM_REPLACESEL:
4749 LPWSTR textW;
4751 if(unicode)
4752 textW = (LPWSTR)lParam;
4753 else
4755 LPSTR textA = (LPSTR)lParam;
4756 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4757 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4758 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4761 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, strlenW(textW), TRUE, TRUE);
4762 result = 1;
4764 if(!unicode)
4765 HeapFree(GetProcessHeap(), 0, textW);
4766 break;
4769 case EM_GETLINE:
4770 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4771 break;
4773 case EM_SETLIMITTEXT:
4774 EDIT_EM_SetLimitText(es, wParam);
4775 break;
4777 case EM_CANUNDO:
4778 result = (LRESULT)EDIT_EM_CanUndo(es);
4779 break;
4781 case EM_UNDO:
4782 case WM_UNDO:
4783 result = (LRESULT)EDIT_EM_Undo(es);
4784 break;
4786 case EM_FMTLINES:
4787 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4788 break;
4790 case EM_LINEFROMCHAR:
4791 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4792 break;
4794 case EM_SETTABSTOPS:
4795 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4796 break;
4798 case EM_SETPASSWORDCHAR:
4800 WCHAR charW = 0;
4802 if(unicode)
4803 charW = (WCHAR)wParam;
4804 else
4806 CHAR charA = wParam;
4807 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4810 EDIT_EM_SetPasswordChar(es, charW);
4811 break;
4814 case EM_EMPTYUNDOBUFFER:
4815 EDIT_EM_EmptyUndoBuffer(es);
4816 break;
4818 case EM_GETFIRSTVISIBLELINE:
4819 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4820 break;
4822 case EM_SETREADONLY:
4824 DWORD old_style = es->style;
4826 if (wParam) {
4827 SetWindowLongW( hwnd, GWL_STYLE,
4828 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4829 es->style |= ES_READONLY;
4830 } else {
4831 SetWindowLongW( hwnd, GWL_STYLE,
4832 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4833 es->style &= ~ES_READONLY;
4836 if (old_style ^ es->style)
4837 InvalidateRect(es->hwndSelf, NULL, TRUE);
4839 result = 1;
4840 break;
4843 case EM_SETWORDBREAKPROC:
4844 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4845 break;
4847 case EM_GETWORDBREAKPROC:
4848 result = (LRESULT)es->word_break_proc;
4849 break;
4851 case EM_GETPASSWORDCHAR:
4853 if(unicode)
4854 result = es->password_char;
4855 else
4857 WCHAR charW = es->password_char;
4858 CHAR charA = 0;
4859 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4860 result = charA;
4862 break;
4865 case EM_SETMARGINS:
4866 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4867 break;
4869 case EM_GETMARGINS:
4870 result = MAKELONG(es->left_margin, es->right_margin);
4871 break;
4873 case EM_GETLIMITTEXT:
4874 result = es->buffer_limit;
4875 break;
4877 case EM_POSFROMCHAR:
4878 if ((INT)wParam >= get_text_length(es)) result = -1;
4879 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4880 break;
4882 case EM_CHARFROMPOS:
4883 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4884 break;
4886 /* End of the EM_ messages which were in numerical order; what order
4887 * are these in? vaguely alphabetical?
4890 case WM_NCCREATE:
4891 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4892 break;
4894 case WM_NCDESTROY:
4895 result = EDIT_WM_NCDestroy(es);
4896 es = NULL;
4897 break;
4899 case WM_GETDLGCODE:
4900 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4902 if (es->style & ES_MULTILINE)
4903 result |= DLGC_WANTALLKEYS;
4905 if (lParam)
4907 es->flags|=EF_DIALOGMODE;
4909 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4911 int vk = (int)((LPMSG)lParam)->wParam;
4913 if (es->hwndListBox)
4915 if (vk == VK_RETURN || vk == VK_ESCAPE)
4916 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4917 result |= DLGC_WANTMESSAGE;
4921 break;
4923 case WM_IME_CHAR:
4924 if (!unicode)
4926 WCHAR charW;
4927 CHAR strng[2];
4929 strng[0] = wParam >> 8;
4930 strng[1] = wParam & 0xff;
4931 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4932 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4933 result = EDIT_WM_Char(es, charW);
4934 break;
4936 /* fall through */
4937 case WM_CHAR:
4939 WCHAR charW;
4941 if(unicode)
4942 charW = wParam;
4943 else
4945 CHAR charA = wParam;
4946 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4949 if (es->hwndListBox)
4951 if (charW == VK_RETURN || charW == VK_ESCAPE)
4953 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4954 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4955 break;
4958 result = EDIT_WM_Char(es, charW);
4959 break;
4962 case WM_UNICHAR:
4963 if (unicode)
4965 if (wParam == UNICODE_NOCHAR) return TRUE;
4966 if (wParam <= 0x000fffff)
4968 if(wParam > 0xffff) /* convert to surrogates */
4970 wParam -= 0x10000;
4971 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4972 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4974 else EDIT_WM_Char(es, wParam);
4976 return 0;
4978 break;
4980 case WM_CLEAR:
4981 EDIT_WM_Clear(es);
4982 break;
4984 case WM_CONTEXTMENU:
4985 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4986 break;
4988 case WM_COPY:
4989 EDIT_WM_Copy(es);
4990 break;
4992 case WM_CREATE:
4993 if(unicode)
4994 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4995 else
4997 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4998 LPWSTR nameW = NULL;
4999 if(nameA)
5001 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5002 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5003 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5005 result = EDIT_WM_Create(es, nameW);
5006 HeapFree(GetProcessHeap(), 0, nameW);
5008 break;
5010 case WM_CUT:
5011 EDIT_WM_Cut(es);
5012 break;
5014 case WM_ENABLE:
5015 es->bEnableState = (BOOL) wParam;
5016 EDIT_UpdateText(es, NULL, TRUE);
5017 break;
5019 case WM_ERASEBKGND:
5020 /* we do the proper erase in EDIT_WM_Paint */
5021 result = 1;
5022 break;
5024 case WM_GETFONT:
5025 result = (LRESULT)es->font;
5026 break;
5028 case WM_GETTEXT:
5029 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5030 break;
5032 case WM_GETTEXTLENGTH:
5033 if (unicode) result = get_text_length(es);
5034 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5035 NULL, 0, NULL, NULL );
5036 break;
5038 case WM_HSCROLL:
5039 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5040 break;
5042 case WM_KEYDOWN:
5043 result = EDIT_WM_KeyDown(es, (INT)wParam);
5044 break;
5046 case WM_KILLFOCUS:
5047 result = EDIT_WM_KillFocus(es);
5048 break;
5050 case WM_LBUTTONDBLCLK:
5051 result = EDIT_WM_LButtonDblClk(es);
5052 break;
5054 case WM_LBUTTONDOWN:
5055 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5056 break;
5058 case WM_LBUTTONUP:
5059 result = EDIT_WM_LButtonUp(es);
5060 break;
5062 case WM_MBUTTONDOWN:
5063 result = EDIT_WM_MButtonDown(es);
5064 break;
5066 case WM_MOUSEMOVE:
5067 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5068 break;
5070 case WM_PRINTCLIENT:
5071 case WM_PAINT:
5072 EDIT_WM_Paint(es, (HDC)wParam);
5073 break;
5075 case WM_PASTE:
5076 EDIT_WM_Paste(es);
5077 break;
5079 case WM_SETFOCUS:
5080 EDIT_WM_SetFocus(es);
5081 break;
5083 case WM_SETFONT:
5084 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5085 break;
5087 case WM_SETREDRAW:
5088 /* FIXME: actually set an internal flag and behave accordingly */
5089 break;
5091 case WM_SETTEXT:
5092 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5093 result = TRUE;
5094 break;
5096 case WM_SIZE:
5097 EDIT_WM_Size(es, (UINT)wParam);
5098 break;
5100 case WM_STYLECHANGED:
5101 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5102 break;
5104 case WM_STYLECHANGING:
5105 result = 0; /* See EDIT_WM_StyleChanged */
5106 break;
5108 case WM_SYSKEYDOWN:
5109 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5110 break;
5112 case WM_TIMER:
5113 EDIT_WM_Timer(es);
5114 break;
5116 case WM_VSCROLL:
5117 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5118 break;
5120 case WM_MOUSEWHEEL:
5122 int wheelDelta;
5123 UINT pulScrollLines = 3;
5124 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5126 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5127 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5128 break;
5130 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5131 /* if scrolling changes direction, ignore left overs */
5132 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5133 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5134 es->wheelDeltaRemainder += wheelDelta;
5135 else
5136 es->wheelDeltaRemainder = wheelDelta;
5137 if (es->wheelDeltaRemainder && pulScrollLines)
5139 int cLineScroll;
5140 pulScrollLines = (int) min((UINT) es->line_count, pulScrollLines);
5141 cLineScroll = pulScrollLines * (float)es->wheelDeltaRemainder / WHEEL_DELTA;
5142 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
5143 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5146 break;
5149 /* IME messages to make the edit control IME aware */
5150 case WM_IME_SETCONTEXT:
5151 break;
5153 case WM_IME_STARTCOMPOSITION:
5154 es->composition_start = es->selection_end;
5155 es->composition_len = 0;
5156 break;
5158 case WM_IME_COMPOSITION:
5159 EDIT_ImeComposition(hwnd, lParam, es);
5160 break;
5162 case WM_IME_ENDCOMPOSITION:
5163 if (es->composition_len > 0)
5165 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5166 es->selection_end = es->selection_start;
5167 es->composition_len= 0;
5169 break;
5171 case WM_IME_COMPOSITIONFULL:
5172 break;
5174 case WM_IME_SELECT:
5175 break;
5177 case WM_IME_CONTROL:
5178 break;
5180 case WM_IME_REQUEST:
5181 switch (wParam)
5183 case IMR_QUERYCHARPOSITION:
5185 LRESULT pos;
5186 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5188 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5189 chpos->pt.x = LOWORD(pos);
5190 chpos->pt.y = HIWORD(pos);
5191 chpos->cLineHeight = es->line_height;
5192 chpos->rcDocument = es->format_rect;
5193 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5194 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5195 result = 1;
5196 break;
5199 break;
5201 default:
5202 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5203 break;
5206 if (IsWindow(hwnd) && es) EDIT_UnlockBuffer(es, FALSE);
5208 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5210 return result;
5214 /*********************************************************************
5215 * edit class descriptor
5217 static const WCHAR editW[] = {'E','d','i','t',0};
5218 const struct builtin_class_descr EDIT_builtin_class =
5220 editW, /* name */
5221 CS_DBLCLKS | CS_PARENTDC, /* style */
5222 WINPROC_EDIT, /* proc */
5223 #ifdef __i386__
5224 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
5225 #else
5226 sizeof(EDITSTATE *), /* extra */
5227 #endif
5228 IDC_IBEAM, /* cursor */
5229 0 /* brush */