include: Remove the wine_ prefix on rbtree functions.
[wine.git] / dlls / user32 / edit.c
blob73885566d3b3b9669593db5f65055fc7e5b042be
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 * TODO:
24 * - EM_GETIMESTATUS, EM_SETIMESTATUS
25 * - EN_ALIGN_LTR_EC
26 * - EN_ALIGN_RTL_EC
27 * - ES_OEMCONVERT
31 #include <stdarg.h>
32 #include <string.h>
33 #include <stdlib.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winnt.h"
38 #include "win.h"
39 #include "imm.h"
40 #include "usp10.h"
41 #include "controls.h"
42 #include "user_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(edit);
46 WINE_DECLARE_DEBUG_CHANNEL(combo);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
49 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
50 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
51 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
52 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
55 * extra flags for EDITSTATE.flags field
57 #define EF_MODIFIED 0x0001 /* text has been modified */
58 #define EF_FOCUSED 0x0002 /* we have input focus */
59 #define EF_UPDATE 0x0004 /* notify parent of changed state */
60 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
61 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
62 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
63 wrapped line, instead of in front of the next character */
64 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
65 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
67 typedef enum
69 END_0 = 0, /* line ends with terminating '\0' character */
70 END_WRAP, /* line is wrapped */
71 END_HARD, /* line ends with a hard return '\r\n' */
72 END_SOFT, /* line ends with a soft return '\r\r\n' */
73 END_RICH /* line ends with a single '\n' */
74 } LINE_END;
76 typedef struct tagLINEDEF {
77 INT length; /* bruto length of a line in bytes */
78 INT net_length; /* netto length of a line in visible characters */
79 LINE_END ending;
80 INT width; /* width of the line in pixels */
81 INT index; /* line index into the buffer */
82 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
83 struct tagLINEDEF *next;
84 } LINEDEF;
86 typedef struct
88 BOOL is_unicode; /* how the control was created */
89 LPWSTR text; /* the actual contents of the control */
90 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
91 UINT buffer_size; /* the size of the buffer in characters */
92 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
93 HFONT font; /* NULL means standard system font */
94 INT x_offset; /* scroll offset for multi lines this is in pixels
95 for single lines it's in characters */
96 INT line_height; /* height of a screen line in pixels */
97 INT char_width; /* average character width in pixels */
98 DWORD style; /* sane version of wnd->dwStyle */
99 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
100 INT undo_insert_count; /* number of characters inserted in sequence */
101 UINT undo_position; /* character index of the insertion and deletion */
102 LPWSTR undo_text; /* deleted text */
103 UINT undo_buffer_size; /* size of the deleted text buffer */
104 INT selection_start; /* == selection_end if no selection */
105 INT selection_end; /* == current caret position */
106 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
107 INT left_margin; /* in pixels */
108 INT right_margin; /* in pixels */
109 RECT format_rect;
110 INT text_width; /* width of the widest line in pixels for multi line controls
111 and just line width for single line controls */
112 INT region_posx; /* Position of cursor relative to region: */
113 INT region_posy; /* -1: to left, 0: within, 1: to right */
114 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
115 INT line_count; /* number of lines */
116 INT y_offset; /* scroll offset in number of lines */
117 BOOL bCaptureState; /* flag indicating whether mouse was captured */
118 BOOL bEnableState; /* flag keeping the enable state */
119 HWND hwndSelf; /* the our window handle */
120 HWND hwndParent; /* Handle of parent for sending EN_* messages.
121 Even if parent will change, EN_* messages
122 should be sent to the first parent. */
123 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
124 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
126 * only for multi line controls
128 INT lock_count; /* amount of re-entries in the EditWndProc */
129 INT tabs_count;
130 LPINT tabs;
131 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
132 HLOCAL hloc32W; /* our unicode local memory block */
133 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
134 or EM_SETHANDLE */
135 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
137 * IME Data
139 UINT composition_len; /* length of composition, 0 == no composition */
140 int composition_start; /* the character position for the composition */
142 * Uniscribe Data
144 SCRIPT_LOGATTR *logAttr;
145 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
146 } EDITSTATE;
149 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
150 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
152 /* used for disabled or read-only edit control */
153 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
154 do \
155 { /* Notify parent which has created this edit control */ \
156 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
157 SendMessageW(es->hwndParent, WM_COMMAND, \
158 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
159 (LPARAM)(es->hwndSelf)); \
160 } while(0)
162 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
164 /*********************************************************************
166 * EM_CANUNDO
169 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
171 return (es->undo_insert_count || lstrlenW(es->undo_text));
175 /*********************************************************************
177 * EM_EMPTYUNDOBUFFER
180 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
182 es->undo_insert_count = 0;
183 *es->undo_text = '\0';
187 /**********************************************************************
188 * get_app_version
190 * Returns the window version in case Wine emulates a later version
191 * of windows than the application expects.
193 * In a number of cases when windows runs an application that was
194 * designed for an earlier windows version, windows reverts
195 * to "old" behaviour of that earlier version.
197 * An example is a disabled edit control that needs to be painted.
198 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
199 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
200 * applications with an expected version 0f 4.0 or higher.
203 static DWORD get_app_version(void)
205 static DWORD version;
206 if (!version)
208 DWORD dwEmulatedVersion;
209 OSVERSIONINFOW info;
210 DWORD dwProcVersion = GetProcessVersion(0);
212 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
213 GetVersionExW( &info );
214 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
215 /* FIXME: this may not be 100% correct; see discussion on the
216 * wine developer list in Nov 1999 */
217 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
219 return version;
222 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
224 HBRUSH hbrush;
225 UINT msg;
227 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
228 msg = WM_CTLCOLORSTATIC;
229 else
230 msg = WM_CTLCOLOREDIT;
232 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
233 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
234 if (!hbrush)
235 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
236 return hbrush;
240 static inline UINT get_text_length(EDITSTATE *es)
242 if(es->text_length == (UINT)-1)
243 es->text_length = lstrlenW(es->text);
244 return es->text_length;
248 /*********************************************************************
250 * EDIT_WordBreakProc
252 * Find the beginning of words.
253 * Note: unlike the specs for a WordBreakProc, this function can
254 * only be called without linebreaks between s[0] up to
255 * s[count - 1]. Remember it is only called
256 * internally, so we can decide this for ourselves.
257 * Additionally we will always be breaking the full string.
260 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
262 INT ret = 0;
264 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
266 if(!s) return 0;
268 if (!es->logAttr)
270 SCRIPT_ANALYSIS psa;
272 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
273 psa.eScript = SCRIPT_UNDEFINED;
275 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
276 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
279 switch (action) {
280 case WB_LEFT:
281 if (index)
282 index--;
283 while (index && !es->logAttr[index].fSoftBreak)
284 index--;
285 ret = index;
286 break;
287 case WB_RIGHT:
288 if (!count)
289 break;
290 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
291 index++;
292 ret = index;
293 break;
294 case WB_ISDELIMITER:
295 ret = es->logAttr[index].fWhiteSpace;
296 break;
297 default:
298 ERR("unknown action code, please report !\n");
299 break;
301 return ret;
305 /*********************************************************************
307 * EDIT_CallWordBreakProc
309 * Call appropriate WordBreakProc (internal or external).
311 * Note: The "start" argument should always be an index referring
312 * to es->text. The actual wordbreak proc might be
313 * 16 bit, so we can't always pass any 32 bit LPSTR.
314 * Hence we assume that es->text is the buffer that holds
315 * the string under examination (we can decide this for ourselves).
318 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
320 INT ret;
322 if (es->word_break_proc)
324 if(es->is_unicode)
326 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
328 TRACE_(relay)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
329 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
330 ret = wbpW(es->text + start, index, count, action);
331 TRACE_(relay)("\1Ret wordbreakW %p retval=%d\n", es->word_break_proc, ret );
333 else
335 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
336 INT countA;
337 CHAR *textA;
339 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
340 textA = HeapAlloc(GetProcessHeap(), 0, countA);
341 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
342 TRACE_(relay)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
343 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
344 ret = wbpA(textA, index, countA, action);
345 HeapFree(GetProcessHeap(), 0, textA);
346 TRACE_(relay)("\1Ret wordbreakA %p retval=%d\n", es->word_break_proc, ret );
349 else
350 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
352 return ret;
355 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
357 if (line_def->ssa)
359 ScriptStringFree(&line_def->ssa);
360 line_def->ssa = NULL;
364 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
366 LINEDEF *line_def = es->first_line_def;
367 while (line_def)
369 EDIT_InvalidateUniscribeData_linedef(line_def);
370 line_def = line_def->next;
372 if (es->ssa)
374 ScriptStringFree(&es->ssa);
375 es->ssa = NULL;
379 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
381 if (!line_def)
382 return NULL;
384 if (line_def->net_length && !line_def->ssa)
386 int index = line_def->index;
387 HFONT old_font = NULL;
388 HDC udc = dc;
389 SCRIPT_TABDEF tabdef;
390 HRESULT hr;
392 if (!udc)
393 udc = GetDC(es->hwndSelf);
394 if (es->font)
395 old_font = SelectObject(udc, es->font);
397 tabdef.cTabStops = es->tabs_count;
398 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
399 tabdef.pTabStops = es->tabs;
400 tabdef.iTabOrigin = 0;
402 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
403 (1.5*line_def->net_length+16), -1,
404 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
405 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
406 if (FAILED(hr))
408 WARN("ScriptStringAnalyse failed (%x)\n",hr);
409 line_def->ssa = NULL;
412 if (es->font)
413 SelectObject(udc, old_font);
414 if (udc != dc)
415 ReleaseDC(es->hwndSelf, udc);
418 return line_def->ssa;
421 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
423 LINEDEF *line_def;
425 if (!(es->style & ES_MULTILINE))
427 if (!es->ssa)
429 INT length = get_text_length(es);
430 HFONT old_font = NULL;
431 HDC udc = dc;
433 if (!udc)
434 udc = GetDC(es->hwndSelf);
435 if (es->font)
436 old_font = SelectObject(udc, es->font);
438 if (es->style & ES_PASSWORD)
439 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);
440 else
441 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
443 if (es->font)
444 SelectObject(udc, old_font);
445 if (udc != dc)
446 ReleaseDC(es->hwndSelf, udc);
448 return es->ssa;
450 else
452 line_def = es->first_line_def;
453 while (line_def && line)
455 line_def = line_def->next;
456 line--;
459 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
463 static inline INT get_vertical_line_count(EDITSTATE *es)
465 INT vlc = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
466 return max(1,vlc);
469 /*********************************************************************
471 * EDIT_BuildLineDefs_ML
473 * Build linked list of text lines.
474 * Lines can end with '\0' (last line), a character (if it is wrapped),
475 * a soft return '\r\r\n' or a hard return '\r\n'
478 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
480 LPWSTR current_position, cp;
481 INT fw;
482 LINEDEF *current_line;
483 LINEDEF *previous_line;
484 LINEDEF *start_line;
485 INT line_index = 0, nstart_line, nstart_index;
486 INT line_count = es->line_count;
487 INT orig_net_length;
488 RECT rc;
489 INT vlc;
491 if (istart == iend && delta == 0)
492 return;
494 previous_line = NULL;
495 current_line = es->first_line_def;
497 /* Find starting line. istart must lie inside an existing line or
498 * at the end of buffer */
499 do {
500 if (istart < current_line->index + current_line->length ||
501 current_line->ending == END_0)
502 break;
504 previous_line = current_line;
505 current_line = current_line->next;
506 line_index++;
507 } while (current_line);
509 if (!current_line) /* Error occurred start is not inside previous buffer */
511 FIXME(" modification occurred outside buffer\n");
512 return;
515 /* Remember start of modifications in order to calculate update region */
516 nstart_line = line_index;
517 nstart_index = current_line->index;
519 /* We must start to reformat from the previous line since the modifications
520 * may have caused the line to wrap upwards. */
521 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
523 line_index--;
524 current_line = previous_line;
526 start_line = current_line;
528 fw = es->format_rect.right - es->format_rect.left;
529 current_position = es->text + current_line->index;
530 vlc = get_vertical_line_count(es);
531 do {
532 if (current_line != start_line)
534 if (!current_line || current_line->index + delta > current_position - es->text)
536 /* The buffer has been expanded, create a new line and
537 insert it into the link list */
538 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
539 new_line->next = previous_line->next;
540 previous_line->next = new_line;
541 current_line = new_line;
542 es->line_count++;
544 else if (current_line->index + delta < current_position - es->text)
546 /* The previous line merged with this line so we delete this extra entry */
547 previous_line->next = current_line->next;
548 HeapFree(GetProcessHeap(), 0, current_line);
549 current_line = previous_line->next;
550 es->line_count--;
551 continue;
553 else /* current_line->index + delta == current_position */
555 if (current_position - es->text > iend)
556 break; /* We reached end of line modifications */
557 /* else recalculate this line */
561 current_line->index = current_position - es->text;
562 orig_net_length = current_line->net_length;
564 /* Find end of line */
565 cp = current_position;
566 while (*cp) {
567 if (*cp == '\n') break;
568 if ((*cp == '\r') && (*(cp + 1) == '\n'))
569 break;
570 cp++;
573 /* Mark type of line termination */
574 if (!(*cp)) {
575 current_line->ending = END_0;
576 current_line->net_length = lstrlenW(current_position);
577 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
578 current_line->ending = END_SOFT;
579 current_line->net_length = cp - current_position - 1;
580 } else if (*cp == '\n') {
581 current_line->ending = END_RICH;
582 current_line->net_length = cp - current_position;
583 } else {
584 current_line->ending = END_HARD;
585 current_line->net_length = cp - current_position;
588 if (current_line->net_length)
590 const SIZE *sz;
591 EDIT_InvalidateUniscribeData_linedef(current_line);
592 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
593 if (current_line->ssa)
595 sz = ScriptString_pSize(current_line->ssa);
596 /* Calculate line width */
597 current_line->width = sz->cx;
599 else current_line->width = es->char_width * current_line->net_length;
601 else current_line->width = 0;
603 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
605 /* Line breaks just look back from the end and find the next break and try that. */
607 if (!(es->style & ES_AUTOHSCROLL)) {
608 if (current_line->width > fw && fw > es->char_width) {
610 INT prev, next;
611 int w;
612 const SIZE *sz;
613 float d;
615 prev = current_line->net_length - 1;
616 w = current_line->net_length;
617 d = (float)current_line->width/(float)fw;
618 if (d > 1.2f) d -= 0.2f;
619 next = prev/d;
620 if (next >= prev) next = prev-1;
621 do {
622 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
623 next, current_line->net_length, WB_LEFT);
624 current_line->net_length = prev;
625 EDIT_InvalidateUniscribeData_linedef(current_line);
626 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
627 if (current_line->ssa)
628 sz = ScriptString_pSize(current_line->ssa);
629 else sz = 0;
630 if (sz)
631 current_line->width = sz->cx;
632 else
633 prev = 0;
634 next = prev - 1;
635 } while (prev && current_line->width > fw);
636 current_line->net_length = w;
638 if (prev == 0) { /* Didn't find a line break so force a break */
639 INT *piDx;
640 const INT *count;
642 EDIT_InvalidateUniscribeData_linedef(current_line);
643 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
645 if (current_line->ssa)
647 count = ScriptString_pcOutChars(current_line->ssa);
648 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
649 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
651 prev = current_line->net_length-1;
652 do {
653 current_line->width -= piDx[prev];
654 prev--;
655 } while ( prev > 0 && current_line->width > fw);
656 if (prev<=0)
657 prev = 1;
658 HeapFree(GetProcessHeap(),0,piDx);
660 else
661 prev = (fw / es->char_width);
664 /* If the first line we are calculating, wrapped before istart, we must
665 * adjust istart in order for this to be reflected in the update region. */
666 if (current_line->index == nstart_index && istart > current_line->index + prev)
667 istart = current_line->index + prev;
668 /* else if we are updating the previous line before the first line we
669 * are re-calculating and it expanded */
670 else if (current_line == start_line &&
671 current_line->index != nstart_index && orig_net_length < prev)
673 /* Line expanded due to an upwards line wrap so we must partially include
674 * previous line in update region */
675 nstart_line = line_index;
676 nstart_index = current_line->index;
677 istart = current_line->index + orig_net_length;
680 current_line->net_length = prev;
681 current_line->ending = END_WRAP;
683 if (current_line->net_length > 0)
685 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
686 if (current_line->ssa)
688 sz = ScriptString_pSize(current_line->ssa);
689 current_line->width = sz->cx;
691 else
692 current_line->width = 0;
694 else current_line->width = 0;
696 else if (current_line == start_line &&
697 current_line->index != nstart_index &&
698 orig_net_length < current_line->net_length) {
699 /* The previous line expanded but it's still not as wide as the client rect */
700 /* The expansion is due to an upwards line wrap so we must partially include
701 it in the update region */
702 nstart_line = line_index;
703 nstart_index = current_line->index;
704 istart = current_line->index + orig_net_length;
709 /* Adjust length to include line termination */
710 switch (current_line->ending) {
711 case END_SOFT:
712 current_line->length = current_line->net_length + 3;
713 break;
714 case END_RICH:
715 current_line->length = current_line->net_length + 1;
716 break;
717 case END_HARD:
718 current_line->length = current_line->net_length + 2;
719 break;
720 case END_WRAP:
721 case END_0:
722 current_line->length = current_line->net_length;
723 break;
725 es->text_width = max(es->text_width, current_line->width);
726 current_position += current_line->length;
727 previous_line = current_line;
729 /* Discard data for non-visible lines. It will be calculated as needed */
730 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
731 EDIT_InvalidateUniscribeData_linedef(current_line);
733 current_line = current_line->next;
734 line_index++;
735 } while (previous_line->ending != END_0);
737 /* Finish adjusting line indexes by delta or remove hanging lines */
738 if (previous_line->ending == END_0)
740 LINEDEF *pnext = NULL;
742 previous_line->next = NULL;
743 while (current_line)
745 pnext = current_line->next;
746 EDIT_InvalidateUniscribeData_linedef(current_line);
747 HeapFree(GetProcessHeap(), 0, current_line);
748 current_line = pnext;
749 es->line_count--;
752 else if (delta != 0)
754 while (current_line)
756 current_line->index += delta;
757 current_line = current_line->next;
761 /* Calculate rest of modification rectangle */
762 if (hrgn)
764 HRGN tmphrgn;
766 * We calculate two rectangles. One for the first line which may have
767 * an indent with respect to the format rect. The other is a format-width
768 * rectangle that spans the rest of the lines that changed or moved.
770 rc.top = es->format_rect.top + nstart_line * es->line_height -
771 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
772 rc.bottom = rc.top + es->line_height;
773 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
774 rc.left = es->format_rect.left;
775 else
776 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
777 rc.right = es->format_rect.right;
778 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
780 rc.top = rc.bottom;
781 rc.left = es->format_rect.left;
782 rc.right = es->format_rect.right;
784 * If lines were added or removed we must re-paint the remainder of the
785 * lines since the remaining lines were either shifted up or down.
787 if (line_count < es->line_count) /* We added lines */
788 rc.bottom = es->line_count * es->line_height;
789 else if (line_count > es->line_count) /* We removed lines */
790 rc.bottom = line_count * es->line_height;
791 else
792 rc.bottom = line_index * es->line_height;
793 rc.bottom += es->format_rect.top;
794 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
795 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
796 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
797 DeleteObject(tmphrgn);
801 /*********************************************************************
803 * EDIT_CalcLineWidth_SL
806 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
808 EDIT_UpdateUniscribeData(es, NULL, 0);
809 if (es->ssa)
811 const SIZE *size;
812 size = ScriptString_pSize(es->ssa);
813 es->text_width = size->cx;
815 else
816 es->text_width = 0;
819 /*********************************************************************
821 * EDIT_CharFromPos
823 * Beware: This is not the function called on EM_CHARFROMPOS
824 * The position _can_ be outside the formatting / client
825 * rectangle
826 * The return value is only the character index
829 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
831 INT index;
833 if (es->style & ES_MULTILINE) {
834 int trailing;
835 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
836 INT line_index = 0;
837 LINEDEF *line_def = es->first_line_def;
838 EDIT_UpdateUniscribeData(es, NULL, line);
839 while ((line > 0) && line_def->next) {
840 line_index += line_def->length;
841 line_def = line_def->next;
842 line--;
845 x += es->x_offset - es->format_rect.left;
846 if (es->style & ES_RIGHT)
847 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
848 else if (es->style & ES_CENTER)
849 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
850 if (x >= line_def->width) {
851 if (after_wrap)
852 *after_wrap = (line_def->ending == END_WRAP);
853 return line_index + line_def->net_length;
855 if (x <= 0 || !line_def->ssa) {
856 if (after_wrap)
857 *after_wrap = FALSE;
858 return line_index;
861 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
862 if (trailing) index++;
863 index += line_index;
864 if (after_wrap)
865 *after_wrap = ((index == line_index + line_def->net_length) &&
866 (line_def->ending == END_WRAP));
867 } else {
868 INT xoff = 0;
869 INT trailing;
870 if (after_wrap)
871 *after_wrap = FALSE;
872 x -= es->format_rect.left;
873 if (!x)
874 return es->x_offset;
876 if (!es->x_offset)
878 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
879 if (es->style & ES_RIGHT)
880 x -= indent;
881 else if (es->style & ES_CENTER)
882 x -= indent / 2;
885 EDIT_UpdateUniscribeData(es, NULL, 0);
886 if (es->x_offset)
888 if (es->ssa)
890 if (es->x_offset>= get_text_length(es))
892 const SIZE *size;
893 size = ScriptString_pSize(es->ssa);
894 xoff = size->cx;
896 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
898 else
899 xoff = 0;
901 if (x < 0)
903 if (x + xoff > 0 || !es->ssa)
905 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
906 if (trailing) index++;
908 else
909 index = 0;
911 else
913 if (x)
915 const SIZE *size = NULL;
916 if (es->ssa)
917 size = ScriptString_pSize(es->ssa);
918 if (!size)
919 index = 0;
920 else if (x > size->cx)
921 index = get_text_length(es);
922 else if (es->ssa)
924 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
925 if (trailing) index++;
927 else
928 index = 0;
930 else
931 index = es->x_offset;
934 return index;
938 /*********************************************************************
940 * EDIT_ConfinePoint
942 * adjusts the point to be within the formatting rectangle
943 * (so CharFromPos returns the nearest _visible_ character)
946 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
948 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
949 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
953 /*********************************************************************
955 * EM_LINEFROMCHAR
958 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
960 INT line;
961 LINEDEF *line_def;
963 if (!(es->style & ES_MULTILINE))
964 return 0;
965 if (index > (INT)get_text_length(es))
966 return es->line_count - 1;
967 if (index == -1)
968 index = min(es->selection_start, es->selection_end);
970 line = 0;
971 line_def = es->first_line_def;
972 index -= line_def->length;
973 while ((index >= 0) && line_def->next) {
974 line++;
975 line_def = line_def->next;
976 index -= line_def->length;
978 return line;
982 /*********************************************************************
984 * EM_LINEINDEX
987 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
989 INT line_index;
990 const LINEDEF *line_def;
992 if (!(es->style & ES_MULTILINE))
993 return 0;
994 if (line >= es->line_count)
995 return -1;
997 line_index = 0;
998 line_def = es->first_line_def;
999 if (line == -1) {
1000 INT index = es->selection_end - line_def->length;
1001 while ((index >= 0) && line_def->next) {
1002 line_index += line_def->length;
1003 line_def = line_def->next;
1004 index -= line_def->length;
1006 } else {
1007 while (line > 0) {
1008 line_index += line_def->length;
1009 line_def = line_def->next;
1010 line--;
1013 return line_index;
1017 /*********************************************************************
1019 * EM_LINELENGTH
1022 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1024 LINEDEF *line_def;
1026 if (!(es->style & ES_MULTILINE))
1027 return get_text_length(es);
1029 if (index == -1) {
1030 /* get the number of remaining non-selected chars of selected lines */
1031 INT32 l; /* line number */
1032 INT32 li; /* index of first char in line */
1033 INT32 count;
1034 l = EDIT_EM_LineFromChar(es, es->selection_start);
1035 /* # chars before start of selection area */
1036 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1037 l = EDIT_EM_LineFromChar(es, es->selection_end);
1038 /* # chars after end of selection */
1039 li = EDIT_EM_LineIndex(es, l);
1040 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1041 return count;
1043 line_def = es->first_line_def;
1044 index -= line_def->length;
1045 while ((index >= 0) && line_def->next) {
1046 line_def = line_def->next;
1047 index -= line_def->length;
1049 return line_def->net_length;
1053 /*********************************************************************
1055 * EM_POSFROMCHAR
1058 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1060 INT len = get_text_length(es);
1061 INT l;
1062 INT li;
1063 INT x = 0;
1064 INT y = 0;
1065 INT w;
1066 INT lw;
1067 LINEDEF *line_def;
1069 index = min(index, len);
1070 if (es->style & ES_MULTILINE) {
1071 l = EDIT_EM_LineFromChar(es, index);
1072 EDIT_UpdateUniscribeData(es, NULL, l);
1074 y = (l - es->y_offset) * es->line_height;
1075 li = EDIT_EM_LineIndex(es, l);
1076 if (after_wrap && (li == index) && l) {
1077 INT l2 = l - 1;
1078 line_def = es->first_line_def;
1079 while (l2) {
1080 line_def = line_def->next;
1081 l2--;
1083 if (line_def->ending == END_WRAP) {
1084 l--;
1085 y -= es->line_height;
1086 li = EDIT_EM_LineIndex(es, l);
1090 line_def = es->first_line_def;
1091 while (line_def->index != li)
1092 line_def = line_def->next;
1094 lw = line_def->width;
1095 w = es->format_rect.right - es->format_rect.left;
1096 if (line_def->ssa)
1097 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1098 x -= es->x_offset;
1100 if (es->style & ES_RIGHT)
1101 x = w - (lw - x);
1102 else if (es->style & ES_CENTER)
1103 x += (w - lw) / 2;
1104 } else {
1105 INT xoff = 0;
1106 INT xi = 0;
1107 EDIT_UpdateUniscribeData(es, NULL, 0);
1108 if (es->x_offset)
1110 if (es->ssa)
1112 if (es->x_offset >= get_text_length(es))
1114 int leftover = es->x_offset - get_text_length(es);
1115 if (es->ssa)
1117 const SIZE *size;
1118 size = ScriptString_pSize(es->ssa);
1119 xoff = size->cx;
1121 else
1122 xoff = 0;
1123 xoff += es->char_width * leftover;
1125 else
1126 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1128 else
1129 xoff = 0;
1131 if (index)
1133 if (index >= get_text_length(es))
1135 if (es->ssa)
1137 const SIZE *size;
1138 size = ScriptString_pSize(es->ssa);
1139 xi = size->cx;
1141 else
1142 xi = 0;
1144 else if (es->ssa)
1145 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1146 else
1147 xi = 0;
1149 x = xi - xoff;
1151 if (index >= es->x_offset) {
1152 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1154 w = es->format_rect.right - es->format_rect.left;
1155 if (w > es->text_width)
1157 if (es->style & ES_RIGHT)
1158 x += w - es->text_width;
1159 else if (es->style & ES_CENTER)
1160 x += (w - es->text_width) / 2;
1164 y = 0;
1166 x += es->format_rect.left;
1167 y += es->format_rect.top;
1168 return MAKELONG((INT16)x, (INT16)y);
1172 /*********************************************************************
1174 * EDIT_GetLineRect
1176 * Calculates the bounding rectangle for a line from a starting
1177 * column to an ending column.
1180 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1182 SCRIPT_STRING_ANALYSIS ssa;
1183 INT line_index = 0;
1184 INT pt1, pt2, pt3;
1186 if (es->style & ES_MULTILINE)
1188 const LINEDEF *line_def = NULL;
1189 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1190 if (line >= es->line_count)
1191 return;
1193 line_def = es->first_line_def;
1194 if (line == -1) {
1195 INT index = es->selection_end - line_def->length;
1196 while ((index >= 0) && line_def->next) {
1197 line_index += line_def->length;
1198 line_def = line_def->next;
1199 index -= line_def->length;
1201 } else {
1202 while (line > 0) {
1203 line_index += line_def->length;
1204 line_def = line_def->next;
1205 line--;
1208 ssa = line_def->ssa;
1210 else
1212 line_index = 0;
1213 rc->top = es->format_rect.top;
1214 ssa = es->ssa;
1217 rc->bottom = rc->top + es->line_height;
1218 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1219 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1220 if (ssa)
1222 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1223 pt3+=es->format_rect.left;
1225 else pt3 = pt1;
1226 rc->right = max(max(pt1 , pt2),pt3);
1227 rc->left = min(min(pt1, pt2),pt3);
1231 static inline void text_buffer_changed(EDITSTATE *es)
1233 es->text_length = (UINT)-1;
1235 HeapFree( GetProcessHeap(), 0, es->logAttr );
1236 es->logAttr = NULL;
1237 EDIT_InvalidateUniscribeData(es);
1240 /*********************************************************************
1241 * EDIT_LockBuffer
1244 static void EDIT_LockBuffer(EDITSTATE *es)
1246 if (!es->text) {
1248 if(!es->hloc32W) return;
1250 if(es->hloc32A)
1252 CHAR *textA = LocalLock(es->hloc32A);
1253 HLOCAL hloc32W_new;
1254 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1255 if(countW_new > es->buffer_size + 1)
1257 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1258 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1259 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1260 if(hloc32W_new)
1262 es->hloc32W = hloc32W_new;
1263 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1264 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1266 else
1267 WARN("FAILED! Will synchronize partially\n");
1269 es->text = LocalLock(es->hloc32W);
1270 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1271 LocalUnlock(es->hloc32A);
1273 else es->text = LocalLock(es->hloc32W);
1275 es->lock_count++;
1279 /*********************************************************************
1281 * EDIT_UnlockBuffer
1284 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1286 /* Edit window might be already destroyed */
1287 if(!IsWindow(es->hwndSelf))
1289 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1290 return;
1293 if (!es->lock_count) {
1294 ERR("lock_count == 0 ... please report\n");
1295 return;
1297 if (!es->text) {
1298 ERR("es->text == 0 ... please report\n");
1299 return;
1301 if (force || (es->lock_count == 1)) {
1302 if (es->hloc32W) {
1303 UINT countA = 0;
1304 UINT countW = get_text_length(es) + 1;
1306 if(es->hloc32A)
1308 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1309 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1310 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1311 countA = LocalSize(es->hloc32A);
1312 if(countA_new > countA)
1314 HLOCAL hloc32A_new;
1315 UINT alloc_size = ROUND_TO_GROW(countA_new);
1316 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1317 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1318 if(hloc32A_new)
1320 es->hloc32A = hloc32A_new;
1321 countA = LocalSize(hloc32A_new);
1322 TRACE("Real new size %d bytes\n", countA);
1324 else
1325 WARN("FAILED! Will synchronize partially\n");
1327 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1328 LocalLock(es->hloc32A), countA, NULL, NULL);
1329 LocalUnlock(es->hloc32A);
1332 LocalUnlock(es->hloc32W);
1333 es->text = NULL;
1335 else {
1336 ERR("no buffer ... please report\n");
1337 return;
1340 es->lock_count--;
1344 /*********************************************************************
1346 * EDIT_MakeFit
1348 * Try to fit size + 1 characters in the buffer.
1350 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1352 HLOCAL hNew32W;
1354 if (size <= es->buffer_size)
1355 return TRUE;
1357 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1359 /* Force edit to unlock its buffer. es->text now NULL */
1360 EDIT_UnlockBuffer(es, TRUE);
1362 if (es->hloc32W) {
1363 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1364 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1365 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1366 es->hloc32W = hNew32W;
1367 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1371 EDIT_LockBuffer(es);
1373 if (es->buffer_size < size) {
1374 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1375 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1376 return FALSE;
1377 } else {
1378 TRACE("We now have %d+1\n", es->buffer_size);
1379 return TRUE;
1384 /*********************************************************************
1386 * EDIT_MakeUndoFit
1388 * Try to fit size + 1 bytes in the undo buffer.
1391 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1393 UINT alloc_size;
1395 if (size <= es->undo_buffer_size)
1396 return TRUE;
1398 TRACE("trying to ReAlloc to %d+1\n", size);
1400 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1401 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1402 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1403 return TRUE;
1405 else
1407 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1408 return FALSE;
1413 /*********************************************************************
1415 * EDIT_UpdateTextRegion
1418 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1420 if (es->flags & EF_UPDATE) {
1421 es->flags &= ~EF_UPDATE;
1422 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1424 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1428 /*********************************************************************
1430 * EDIT_UpdateText
1433 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1435 if (es->flags & EF_UPDATE) {
1436 es->flags &= ~EF_UPDATE;
1437 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1439 InvalidateRect(es->hwndSelf, rc, bErase);
1442 /*********************************************************************
1444 * EDIT_SL_InvalidateText
1446 * Called from EDIT_InvalidateText().
1447 * Does the job for single-line controls only.
1450 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1452 RECT line_rect;
1453 RECT rc;
1455 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1456 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1457 EDIT_UpdateText(es, &rc, TRUE);
1460 /*********************************************************************
1462 * EDIT_ML_InvalidateText
1464 * Called from EDIT_InvalidateText().
1465 * Does the job for multi-line controls only.
1468 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1470 INT vlc = get_vertical_line_count(es);
1471 INT sl = EDIT_EM_LineFromChar(es, start);
1472 INT el = EDIT_EM_LineFromChar(es, end);
1473 INT sc;
1474 INT ec;
1475 RECT rc1;
1476 RECT rcWnd;
1477 RECT rcLine;
1478 RECT rcUpdate;
1479 INT l;
1481 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1482 return;
1484 sc = start - EDIT_EM_LineIndex(es, sl);
1485 ec = end - EDIT_EM_LineIndex(es, el);
1486 if (sl < es->y_offset) {
1487 sl = es->y_offset;
1488 sc = 0;
1490 if (el > es->y_offset + vlc) {
1491 el = es->y_offset + vlc;
1492 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1494 GetClientRect(es->hwndSelf, &rc1);
1495 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1496 if (sl == el) {
1497 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1498 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1499 EDIT_UpdateText(es, &rcUpdate, TRUE);
1500 } else {
1501 EDIT_GetLineRect(es, sl, sc,
1502 EDIT_EM_LineLength(es,
1503 EDIT_EM_LineIndex(es, sl)),
1504 &rcLine);
1505 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1506 EDIT_UpdateText(es, &rcUpdate, TRUE);
1507 for (l = sl + 1 ; l < el ; l++) {
1508 EDIT_GetLineRect(es, l, 0,
1509 EDIT_EM_LineLength(es,
1510 EDIT_EM_LineIndex(es, l)),
1511 &rcLine);
1512 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1513 EDIT_UpdateText(es, &rcUpdate, TRUE);
1515 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1516 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1517 EDIT_UpdateText(es, &rcUpdate, TRUE);
1522 /*********************************************************************
1524 * EDIT_InvalidateText
1526 * Invalidate the text from offset start up to, but not including,
1527 * offset end. Useful for (re)painting the selection.
1528 * Regions outside the linewidth are not invalidated.
1529 * end == -1 means end == TextLength.
1530 * start and end need not be ordered.
1533 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1535 if (end == start)
1536 return;
1538 if (end == -1)
1539 end = get_text_length(es);
1541 if (end < start) {
1542 INT tmp = start;
1543 start = end;
1544 end = tmp;
1547 if (es->style & ES_MULTILINE)
1548 EDIT_ML_InvalidateText(es, start, end);
1549 else
1550 EDIT_SL_InvalidateText(es, start, end);
1554 /*********************************************************************
1556 * EDIT_EM_SetSel
1558 * note: unlike the specs say: the order of start and end
1559 * _is_ preserved in Windows. (i.e. start can be > end)
1560 * In other words: this handler is OK
1563 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1565 UINT old_start = es->selection_start;
1566 UINT old_end = es->selection_end;
1567 UINT len = get_text_length(es);
1569 if (start == (UINT)-1) {
1570 start = es->selection_end;
1571 end = es->selection_end;
1572 } else {
1573 start = min(start, len);
1574 end = min(end, len);
1576 es->selection_start = start;
1577 es->selection_end = end;
1578 if (after_wrap)
1579 es->flags |= EF_AFTER_WRAP;
1580 else
1581 es->flags &= ~EF_AFTER_WRAP;
1582 /* Compute the necessary invalidation region. */
1583 /* Note that we don't need to invalidate regions which have
1584 * "never" been selected, or those which are "still" selected.
1585 * In fact, every time we hit a selection boundary, we can
1586 * *toggle* whether we need to invalidate. Thus we can optimize by
1587 * *sorting* the interval endpoints. Let's assume that we sort them
1588 * in this order:
1589 * start <= end <= old_start <= old_end
1590 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1591 * in 5 comparisons; i.e. it is impossible to do better than the
1592 * following: */
1593 ORDER_UINT(end, old_end);
1594 ORDER_UINT(start, old_start);
1595 ORDER_UINT(old_start, old_end);
1596 ORDER_UINT(start, end);
1597 /* Note that at this point 'end' and 'old_start' are not in order, but
1598 * start is definitely the min. and old_end is definitely the max. */
1599 if (end != old_start)
1602 * One can also do
1603 * ORDER_UINT32(end, old_start);
1604 * EDIT_InvalidateText(es, start, end);
1605 * EDIT_InvalidateText(es, old_start, old_end);
1606 * in place of the following if statement.
1607 * (That would complete the optimal five-comparison four-element sort.)
1609 if (old_start > end )
1611 EDIT_InvalidateText(es, start, end);
1612 EDIT_InvalidateText(es, old_start, old_end);
1614 else
1616 EDIT_InvalidateText(es, start, old_start);
1617 EDIT_InvalidateText(es, end, old_end);
1620 else EDIT_InvalidateText(es, start, old_end);
1624 /*********************************************************************
1626 * EDIT_UpdateScrollInfo
1629 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1631 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1633 SCROLLINFO si;
1634 si.cbSize = sizeof(SCROLLINFO);
1635 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1636 si.nMin = 0;
1637 si.nMax = es->line_count - 1;
1638 si.nPage = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
1639 si.nPos = es->y_offset;
1640 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1641 si.nMin, si.nMax, si.nPage, si.nPos);
1642 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1645 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1647 SCROLLINFO si;
1648 si.cbSize = sizeof(SCROLLINFO);
1649 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1650 si.nMin = 0;
1651 si.nMax = es->text_width - 1;
1652 si.nPage = es->format_rect.right - es->format_rect.left;
1653 si.nPos = es->x_offset;
1654 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1655 si.nMin, si.nMax, si.nPage, si.nPos);
1656 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1661 /*********************************************************************
1663 * EDIT_EM_LineScroll_internal
1665 * Version of EDIT_EM_LineScroll for internal use.
1666 * It doesn't refuse if ES_MULTILINE is set and assumes that
1667 * dx is in pixels, dy - in lines.
1670 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1672 INT nyoff;
1673 INT x_offset_in_pixels;
1674 INT lines_per_page;
1676 if (!es->line_height || !es->char_width)
1677 return TRUE;
1679 lines_per_page = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1681 if (es->style & ES_MULTILINE)
1683 x_offset_in_pixels = es->x_offset;
1685 else
1687 dy = 0;
1688 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1691 if (-dx > x_offset_in_pixels)
1692 dx = -x_offset_in_pixels;
1693 if (dx > es->text_width - x_offset_in_pixels)
1694 dx = es->text_width - x_offset_in_pixels;
1695 nyoff = max(0, es->y_offset + dy);
1696 if (nyoff >= es->line_count - lines_per_page)
1697 nyoff = max(0, es->line_count - lines_per_page);
1698 dy = (es->y_offset - nyoff) * es->line_height;
1699 if (dx || dy) {
1700 RECT rc1;
1701 RECT rc;
1703 es->y_offset = nyoff;
1704 if(es->style & ES_MULTILINE)
1705 es->x_offset += dx;
1706 else
1707 es->x_offset += dx / es->char_width;
1709 GetClientRect(es->hwndSelf, &rc1);
1710 IntersectRect(&rc, &rc1, &es->format_rect);
1711 ScrollWindowEx(es->hwndSelf, -dx, dy,
1712 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1713 /* force scroll info update */
1714 EDIT_UpdateScrollInfo(es);
1716 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1717 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1718 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1719 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1720 return TRUE;
1723 /*********************************************************************
1725 * EM_LINESCROLL
1727 * NOTE: dx is in average character widths, dy - in lines;
1730 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1732 if (!(es->style & ES_MULTILINE))
1733 return FALSE;
1735 dx *= es->char_width;
1736 return EDIT_EM_LineScroll_internal(es, dx, dy);
1740 /*********************************************************************
1742 * EM_SCROLL
1745 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1747 INT dy;
1749 if (!(es->style & ES_MULTILINE))
1750 return (LRESULT)FALSE;
1752 dy = 0;
1754 switch (action) {
1755 case SB_LINEUP:
1756 if (es->y_offset)
1757 dy = -1;
1758 break;
1759 case SB_LINEDOWN:
1760 if (es->y_offset < es->line_count - 1)
1761 dy = 1;
1762 break;
1763 case SB_PAGEUP:
1764 if (es->y_offset)
1765 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1766 break;
1767 case SB_PAGEDOWN:
1768 if (es->y_offset < es->line_count - 1)
1769 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1770 break;
1771 default:
1772 return (LRESULT)FALSE;
1774 if (dy) {
1775 INT vlc = get_vertical_line_count(es);
1776 /* check if we are going to move too far */
1777 if(es->y_offset + dy > es->line_count - vlc)
1778 dy = max(es->line_count - vlc, 0) - es->y_offset;
1780 /* Notification is done in EDIT_EM_LineScroll */
1781 if(dy) {
1782 EDIT_EM_LineScroll(es, 0, dy);
1783 return MAKELONG(dy, TRUE);
1787 return (LRESULT)FALSE;
1791 /*********************************************************************
1793 * EDIT_SetCaretPos
1796 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1797 BOOL after_wrap)
1799 LRESULT res;
1801 if (es->flags & EF_FOCUSED)
1803 res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1804 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1805 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1810 /*********************************************************************
1812 * EM_SCROLLCARET
1815 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1817 if (es->style & ES_MULTILINE) {
1818 INT l;
1819 INT vlc;
1820 INT ww;
1821 INT cw = es->char_width;
1822 INT x;
1823 INT dy = 0;
1824 INT dx = 0;
1826 l = EDIT_EM_LineFromChar(es, es->selection_end);
1827 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1828 vlc = get_vertical_line_count(es);
1829 if (l >= es->y_offset + vlc)
1830 dy = l - vlc + 1 - es->y_offset;
1831 if (l < es->y_offset)
1832 dy = l - es->y_offset;
1833 ww = es->format_rect.right - es->format_rect.left;
1834 if (x < es->format_rect.left)
1835 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1836 if (x > es->format_rect.right)
1837 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1838 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1840 /* check if we are going to move too far */
1841 if(es->x_offset + dx + ww > es->text_width)
1842 dx = es->text_width - ww - es->x_offset;
1843 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1844 EDIT_EM_LineScroll_internal(es, dx, dy);
1846 } else {
1847 INT x;
1848 INT goal;
1849 INT format_width;
1851 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1852 format_width = es->format_rect.right - es->format_rect.left;
1853 if (x < es->format_rect.left) {
1854 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1855 do {
1856 es->x_offset--;
1857 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1858 } while ((x < goal) && es->x_offset);
1859 /* FIXME: use ScrollWindow() somehow to improve performance */
1860 EDIT_UpdateText(es, NULL, TRUE);
1861 } else if (x > es->format_rect.right) {
1862 INT x_last;
1863 INT len = get_text_length(es);
1864 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1865 do {
1866 es->x_offset++;
1867 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1868 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1869 } while ((x > goal) && (x_last > es->format_rect.right));
1870 /* FIXME: use ScrollWindow() somehow to improve performance */
1871 EDIT_UpdateText(es, NULL, TRUE);
1875 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1879 /*********************************************************************
1881 * EDIT_MoveBackward
1884 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1886 INT e = es->selection_end;
1888 if (e) {
1889 e--;
1890 if ((es->style & ES_MULTILINE) && e &&
1891 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1892 e--;
1893 if (e && (es->text[e - 1] == '\r'))
1894 e--;
1897 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1898 EDIT_EM_ScrollCaret(es);
1902 /*********************************************************************
1904 * EDIT_MoveDown_ML
1906 * Only for multi line controls
1907 * Move the caret one line down, on a column with the nearest
1908 * x coordinate on the screen (might be a different column).
1911 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1913 INT s = es->selection_start;
1914 INT e = es->selection_end;
1915 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1916 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1917 INT x = (short)LOWORD(pos);
1918 INT y = (short)HIWORD(pos);
1920 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1921 if (!extend)
1922 s = e;
1923 EDIT_EM_SetSel(es, s, e, after_wrap);
1924 EDIT_EM_ScrollCaret(es);
1928 /*********************************************************************
1930 * EDIT_MoveEnd
1933 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1935 BOOL after_wrap = FALSE;
1936 INT e;
1938 /* Pass a high value in x to make sure of receiving the end of the line */
1939 if (!ctrl && (es->style & ES_MULTILINE))
1940 e = EDIT_CharFromPos(es, 0x3fffffff,
1941 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1942 else
1943 e = get_text_length(es);
1944 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1945 EDIT_EM_ScrollCaret(es);
1949 /*********************************************************************
1951 * EDIT_MoveForward
1954 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1956 INT e = es->selection_end;
1958 if (es->text[e]) {
1959 e++;
1960 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1961 if (es->text[e] == '\n')
1962 e++;
1963 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1964 e += 2;
1967 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1968 EDIT_EM_ScrollCaret(es);
1972 /*********************************************************************
1974 * EDIT_MoveHome
1976 * Home key: move to beginning of line.
1979 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1981 INT e;
1983 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1984 if (!ctrl && (es->style & ES_MULTILINE))
1985 e = EDIT_CharFromPos(es, -es->x_offset,
1986 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1987 else
1988 e = 0;
1989 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1990 EDIT_EM_ScrollCaret(es);
1994 /*********************************************************************
1996 * EDIT_MovePageDown_ML
1998 * Only for multi line controls
1999 * Move the caret one page down, on a column with the nearest
2000 * x coordinate on the screen (might be a different column).
2003 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2005 INT s = es->selection_start;
2006 INT e = es->selection_end;
2007 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2008 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2009 INT x = (short)LOWORD(pos);
2010 INT y = (short)HIWORD(pos);
2012 e = EDIT_CharFromPos(es, x,
2013 y + (es->format_rect.bottom - es->format_rect.top),
2014 &after_wrap);
2015 if (!extend)
2016 s = e;
2017 EDIT_EM_SetSel(es, s, e, after_wrap);
2018 EDIT_EM_ScrollCaret(es);
2022 /*********************************************************************
2024 * EDIT_MovePageUp_ML
2026 * Only for multi line controls
2027 * Move the caret one page up, on a column with the nearest
2028 * x coordinate on the screen (might be a different column).
2031 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2033 INT s = es->selection_start;
2034 INT e = es->selection_end;
2035 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2036 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2037 INT x = (short)LOWORD(pos);
2038 INT y = (short)HIWORD(pos);
2040 e = EDIT_CharFromPos(es, x,
2041 y - (es->format_rect.bottom - es->format_rect.top),
2042 &after_wrap);
2043 if (!extend)
2044 s = e;
2045 EDIT_EM_SetSel(es, s, e, after_wrap);
2046 EDIT_EM_ScrollCaret(es);
2050 /*********************************************************************
2052 * EDIT_MoveUp_ML
2054 * Only for multi line controls
2055 * Move the caret one line up, on a column with the nearest
2056 * x coordinate on the screen (might be a different column).
2059 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2061 INT s = es->selection_start;
2062 INT e = es->selection_end;
2063 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2064 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2065 INT x = (short)LOWORD(pos);
2066 INT y = (short)HIWORD(pos);
2068 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2069 if (!extend)
2070 s = e;
2071 EDIT_EM_SetSel(es, s, e, after_wrap);
2072 EDIT_EM_ScrollCaret(es);
2076 /*********************************************************************
2078 * EDIT_MoveWordBackward
2081 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2083 INT s = es->selection_start;
2084 INT e = es->selection_end;
2085 INT l;
2086 INT ll;
2087 INT li;
2089 l = EDIT_EM_LineFromChar(es, e);
2090 ll = EDIT_EM_LineLength(es, e);
2091 li = EDIT_EM_LineIndex(es, l);
2092 if (e - li == 0) {
2093 if (l) {
2094 li = EDIT_EM_LineIndex(es, l - 1);
2095 e = li + EDIT_EM_LineLength(es, li);
2097 } else {
2098 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2100 if (!extend)
2101 s = e;
2102 EDIT_EM_SetSel(es, s, e, FALSE);
2103 EDIT_EM_ScrollCaret(es);
2107 /*********************************************************************
2109 * EDIT_MoveWordForward
2112 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2114 INT s = es->selection_start;
2115 INT e = es->selection_end;
2116 INT l;
2117 INT ll;
2118 INT li;
2120 l = EDIT_EM_LineFromChar(es, e);
2121 ll = EDIT_EM_LineLength(es, e);
2122 li = EDIT_EM_LineIndex(es, l);
2123 if (e - li == ll) {
2124 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2125 e = EDIT_EM_LineIndex(es, l + 1);
2126 } else {
2127 e = li + EDIT_CallWordBreakProc(es,
2128 li, e - li + 1, ll, WB_RIGHT);
2130 if (!extend)
2131 s = e;
2132 EDIT_EM_SetSel(es, s, e, FALSE);
2133 EDIT_EM_ScrollCaret(es);
2137 /*********************************************************************
2139 * EDIT_PaintText
2142 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2144 COLORREF BkColor;
2145 COLORREF TextColor;
2146 LOGFONTW underline_font;
2147 HFONT hUnderline = 0;
2148 HFONT old_font = 0;
2149 INT ret;
2150 INT li;
2151 INT BkMode;
2152 SIZE size;
2154 if (!count)
2155 return 0;
2156 BkMode = GetBkMode(dc);
2157 BkColor = GetBkColor(dc);
2158 TextColor = GetTextColor(dc);
2159 if (rev) {
2160 if (es->composition_len == 0)
2162 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2163 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2164 SetBkMode( dc, OPAQUE);
2166 else
2168 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2169 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2170 underline_font.lfUnderline = TRUE;
2171 hUnderline = CreateFontIndirectW(&underline_font);
2172 old_font = SelectObject(dc,hUnderline);
2175 li = EDIT_EM_LineIndex(es, line);
2176 if (es->style & ES_MULTILINE) {
2177 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2178 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2179 } else {
2180 TextOutW(dc, x, y, es->text + li + col, count);
2181 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2182 ret = size.cx;
2184 if (rev) {
2185 if (es->composition_len == 0)
2187 SetBkColor(dc, BkColor);
2188 SetTextColor(dc, TextColor);
2189 SetBkMode( dc, BkMode);
2191 else
2193 if (old_font)
2194 SelectObject(dc,old_font);
2195 if (hUnderline)
2196 DeleteObject(hUnderline);
2199 return ret;
2203 /*********************************************************************
2205 * EDIT_PaintLine
2208 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2210 INT s = 0;
2211 INT e = 0;
2212 INT li = 0;
2213 INT ll = 0;
2214 INT x;
2215 INT y;
2216 LRESULT pos;
2217 SCRIPT_STRING_ANALYSIS ssa;
2219 if (es->style & ES_MULTILINE) {
2220 INT vlc = get_vertical_line_count(es);
2222 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2223 return;
2224 } else if (line)
2225 return;
2227 TRACE("line=%d\n", line);
2229 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2230 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2231 x = (short)LOWORD(pos);
2232 y = (short)HIWORD(pos);
2234 if (es->style & ES_MULTILINE)
2236 int line_idx = line;
2237 x = -es->x_offset;
2238 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2240 LINEDEF *line_def = es->first_line_def;
2241 int w, lw;
2243 while (line_def && line_idx)
2245 line_def = line_def->next;
2246 line_idx--;
2248 w = es->format_rect.right - es->format_rect.left;
2249 lw = line_def->width;
2251 if (es->style & ES_RIGHT)
2252 x = w - (lw - x);
2253 else if (es->style & ES_CENTER)
2254 x += (w - lw) / 2;
2256 x += es->format_rect.left;
2259 if (rev)
2261 li = EDIT_EM_LineIndex(es, line);
2262 ll = EDIT_EM_LineLength(es, li);
2263 s = min(es->selection_start, es->selection_end);
2264 e = max(es->selection_start, es->selection_end);
2265 s = min(li + ll, max(li, s));
2266 e = min(li + ll, max(li, e));
2269 if (ssa)
2270 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2271 else if (rev && (s != e) &&
2272 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2273 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2274 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2275 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2276 } else
2277 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2281 /*********************************************************************
2283 * EDIT_AdjustFormatRect
2285 * Adjusts the format rectangle for the current font and the
2286 * current client rectangle.
2289 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2291 RECT ClientRect;
2293 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2294 if (es->style & ES_MULTILINE)
2296 INT fw, vlc, max_x_offset, max_y_offset;
2298 vlc = get_vertical_line_count(es);
2299 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2301 /* correct es->x_offset */
2302 fw = es->format_rect.right - es->format_rect.left;
2303 max_x_offset = es->text_width - fw;
2304 if(max_x_offset < 0) max_x_offset = 0;
2305 if(es->x_offset > max_x_offset)
2306 es->x_offset = max_x_offset;
2308 /* correct es->y_offset */
2309 max_y_offset = es->line_count - vlc;
2310 if(max_y_offset < 0) max_y_offset = 0;
2311 if(es->y_offset > max_y_offset)
2312 es->y_offset = max_y_offset;
2314 /* force scroll info update */
2315 EDIT_UpdateScrollInfo(es);
2317 else
2318 /* Windows doesn't care to fix text placement for SL controls */
2319 es->format_rect.bottom = es->format_rect.top + es->line_height;
2321 /* Always stay within the client area */
2322 GetClientRect(es->hwndSelf, &ClientRect);
2323 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2325 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2326 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2328 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2332 /*********************************************************************
2334 * EDIT_SetRectNP
2336 * note: this is not (exactly) the handler called on EM_SETRECTNP
2337 * it is also used to set the rect of a single line control
2340 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2342 LONG_PTR ExStyle;
2343 INT bw, bh;
2344 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2346 CopyRect(&es->format_rect, rc);
2348 if (ExStyle & WS_EX_CLIENTEDGE) {
2349 es->format_rect.left++;
2350 es->format_rect.right--;
2352 if (es->format_rect.bottom - es->format_rect.top
2353 >= es->line_height + 2)
2355 es->format_rect.top++;
2356 es->format_rect.bottom--;
2359 else if (es->style & WS_BORDER) {
2360 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2361 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2362 InflateRect(&es->format_rect, -bw, 0);
2363 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2364 InflateRect(&es->format_rect, 0, -bh);
2367 es->format_rect.left += es->left_margin;
2368 es->format_rect.right -= es->right_margin;
2369 EDIT_AdjustFormatRect(es);
2373 /*********************************************************************
2375 * EM_CHARFROMPOS
2377 * returns line number (not index) in high-order word of result.
2378 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2379 * to Richedit, not to the edit control. Original documentation is valid.
2380 * FIXME: do the specs mean to return -1 if outside client area or
2381 * if outside formatting rectangle ???
2384 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2386 POINT pt;
2387 RECT rc;
2388 INT index;
2390 pt.x = x;
2391 pt.y = y;
2392 GetClientRect(es->hwndSelf, &rc);
2393 if (!PtInRect(&rc, pt))
2394 return -1;
2396 index = EDIT_CharFromPos(es, x, y, NULL);
2397 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2401 /*********************************************************************
2403 * EM_FMTLINES
2405 * Enable or disable soft breaks.
2407 * This means: insert or remove the soft linebreak character (\r\r\n).
2408 * Take care to check if the text still fits the buffer after insertion.
2409 * If not, notify with EN_ERRSPACE.
2412 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2414 es->flags &= ~EF_USE_SOFTBRK;
2415 if (add_eol) {
2416 es->flags |= EF_USE_SOFTBRK;
2417 FIXME("soft break enabled, not implemented\n");
2419 return add_eol;
2423 /*********************************************************************
2425 * EM_GETHANDLE
2427 * Hopefully this won't fire back at us.
2428 * We always start with a fixed buffer in the local heap.
2429 * Despite of the documentation says that the local heap is used
2430 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2431 * buffer on the local heap.
2434 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2436 HLOCAL hLocal;
2438 if (!(es->style & ES_MULTILINE))
2439 return 0;
2441 if(es->is_unicode)
2442 hLocal = es->hloc32W;
2443 else
2445 if(!es->hloc32A)
2447 CHAR *textA;
2448 UINT countA, alloc_size;
2449 TRACE("Allocating 32-bit ANSI alias buffer\n");
2450 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2451 alloc_size = ROUND_TO_GROW(countA);
2452 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2454 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2455 return 0;
2457 textA = LocalLock(es->hloc32A);
2458 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2459 LocalUnlock(es->hloc32A);
2461 hLocal = es->hloc32A;
2464 EDIT_UnlockBuffer(es, TRUE);
2466 /* The text buffer handle belongs to the app */
2467 es->hlocapp = hLocal;
2469 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2470 return hLocal;
2474 /*********************************************************************
2476 * EM_GETLINE
2479 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2481 LPWSTR src;
2482 INT line_len, dst_len;
2483 INT i;
2485 if (es->style & ES_MULTILINE) {
2486 if (line >= es->line_count)
2487 return 0;
2488 } else
2489 line = 0;
2490 i = EDIT_EM_LineIndex(es, line);
2491 src = es->text + i;
2492 line_len = EDIT_EM_LineLength(es, i);
2493 dst_len = *(WORD *)dst;
2494 if(unicode)
2496 if(dst_len <= line_len)
2498 memcpy(dst, src, dst_len * sizeof(WCHAR));
2499 return dst_len;
2501 else /* Append 0 if enough space */
2503 memcpy(dst, src, line_len * sizeof(WCHAR));
2504 dst[line_len] = 0;
2505 return line_len;
2508 else
2510 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2511 if(!ret && line_len) /* Insufficient buffer size */
2512 return dst_len;
2513 if(ret < dst_len) /* Append 0 if enough space */
2514 ((LPSTR)dst)[ret] = 0;
2515 return ret;
2520 /*********************************************************************
2522 * EM_GETSEL
2525 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2527 UINT s = es->selection_start;
2528 UINT e = es->selection_end;
2530 ORDER_UINT(s, e);
2531 if (start)
2532 *start = s;
2533 if (end)
2534 *end = e;
2535 return MAKELONG(s, e);
2539 /*********************************************************************
2541 * EM_REPLACESEL
2543 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2546 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2547 BOOL send_update, BOOL honor_limit)
2549 UINT tl = get_text_length(es);
2550 UINT utl;
2551 UINT s;
2552 UINT e;
2553 UINT i;
2554 UINT size;
2555 LPWSTR p;
2556 HRGN hrgn = 0;
2557 LPWSTR buf = NULL;
2558 UINT bufl;
2560 TRACE("%s, can_undo %d, send_update %d\n",
2561 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2563 s = es->selection_start;
2564 e = es->selection_end;
2566 EDIT_InvalidateUniscribeData(es);
2567 if ((s == e) && !strl)
2568 return;
2570 ORDER_UINT(s, e);
2572 size = tl - (e - s) + strl;
2573 if (!size)
2574 es->text_width = 0;
2576 /* Issue the EN_MAXTEXT notification and continue with replacing text
2577 * so that buffer limit is honored. */
2578 if ((honor_limit) && (size > es->buffer_limit)) {
2579 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2580 /* Buffer limit can be smaller than the actual length of text in combobox */
2581 if (es->buffer_limit < (tl - (e-s)))
2582 strl = 0;
2583 else
2584 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2587 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2588 return;
2590 if (e != s) {
2591 /* there is something to be deleted */
2592 TRACE("deleting stuff.\n");
2593 bufl = e - s;
2594 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2595 if (!buf) return;
2596 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2597 buf[bufl] = 0; /* ensure 0 termination */
2598 /* now delete */
2599 lstrcpyW(es->text + s, es->text + e);
2600 text_buffer_changed(es);
2602 if (strl) {
2603 /* there is an insertion */
2604 tl = get_text_length(es);
2605 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));
2606 for (p = es->text + tl ; p >= es->text + s ; p--)
2607 p[strl] = p[0];
2608 for (i = 0 , p = es->text + s ; i < strl ; i++)
2609 p[i] = lpsz_replace[i];
2610 if(es->style & ES_UPPERCASE)
2611 CharUpperBuffW(p, strl);
2612 else if(es->style & ES_LOWERCASE)
2613 CharLowerBuffW(p, strl);
2614 text_buffer_changed(es);
2616 if (es->style & ES_MULTILINE)
2618 INT st = min(es->selection_start, es->selection_end);
2619 INT vlc = get_vertical_line_count(es);
2621 hrgn = CreateRectRgn(0, 0, 0, 0);
2622 EDIT_BuildLineDefs_ML(es, st, st + strl,
2623 strl - abs(es->selection_end - es->selection_start), hrgn);
2624 /* if text is too long undo all changes */
2625 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2626 if (strl)
2627 lstrcpyW(es->text + e, es->text + e + strl);
2628 if (e != s)
2629 for (i = 0 , p = es->text ; i < e - s ; i++)
2630 p[i + s] = buf[i];
2631 text_buffer_changed(es);
2632 EDIT_BuildLineDefs_ML(es, s, e,
2633 abs(es->selection_end - es->selection_start) - strl, hrgn);
2634 strl = 0;
2635 e = s;
2636 SetRectRgn(hrgn, 0, 0, 0, 0);
2637 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2640 else {
2641 INT fw = es->format_rect.right - es->format_rect.left;
2642 EDIT_InvalidateUniscribeData(es);
2643 EDIT_CalcLineWidth_SL(es);
2644 /* remove chars that don't fit */
2645 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2646 while ((es->text_width > fw) && s + strl >= s) {
2647 lstrcpyW(es->text + s + strl - 1, es->text + s + strl);
2648 strl--;
2649 es->text_length = -1;
2650 EDIT_InvalidateUniscribeData(es);
2651 EDIT_CalcLineWidth_SL(es);
2653 text_buffer_changed(es);
2654 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2658 if (e != s) {
2659 if (can_undo) {
2660 utl = lstrlenW(es->undo_text);
2661 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2662 /* undo-buffer is extended to the right */
2663 EDIT_MakeUndoFit(es, utl + e - s);
2664 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2665 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2666 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2667 /* undo-buffer is extended to the left */
2668 EDIT_MakeUndoFit(es, utl + e - s);
2669 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2670 p[e - s] = p[0];
2671 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2672 p[i] = buf[i];
2673 es->undo_position = s;
2674 } else {
2675 /* new undo-buffer */
2676 EDIT_MakeUndoFit(es, e - s);
2677 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2678 es->undo_text[e - s] = 0; /* ensure 0 termination */
2679 es->undo_position = s;
2681 /* any deletion makes the old insertion-undo invalid */
2682 es->undo_insert_count = 0;
2683 } else
2684 EDIT_EM_EmptyUndoBuffer(es);
2686 if (strl) {
2687 if (can_undo) {
2688 if ((s == es->undo_position) ||
2689 ((es->undo_insert_count) &&
2690 (s == es->undo_position + es->undo_insert_count)))
2692 * insertion is new and at delete position or
2693 * an extension to either left or right
2695 es->undo_insert_count += strl;
2696 else {
2697 /* new insertion undo */
2698 es->undo_position = s;
2699 es->undo_insert_count = strl;
2700 /* new insertion makes old delete-buffer invalid */
2701 *es->undo_text = '\0';
2703 } else
2704 EDIT_EM_EmptyUndoBuffer(es);
2707 HeapFree(GetProcessHeap(), 0, buf);
2709 s += strl;
2711 /* If text has been deleted and we're right or center aligned then scroll rightward */
2712 if (es->style & (ES_RIGHT | ES_CENTER))
2714 INT delta = strl - abs(es->selection_end - es->selection_start);
2716 if (delta < 0 && es->x_offset)
2718 if (abs(delta) > es->x_offset)
2719 es->x_offset = 0;
2720 else
2721 es->x_offset += delta;
2725 EDIT_EM_SetSel(es, s, s, FALSE);
2726 es->flags |= EF_MODIFIED;
2727 if (send_update) es->flags |= EF_UPDATE;
2728 if (hrgn)
2730 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2731 DeleteObject(hrgn);
2733 else
2734 EDIT_UpdateText(es, NULL, TRUE);
2736 EDIT_EM_ScrollCaret(es);
2738 /* force scroll info update */
2739 EDIT_UpdateScrollInfo(es);
2742 if(send_update || (es->flags & EF_UPDATE))
2744 es->flags &= ~EF_UPDATE;
2745 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2747 EDIT_InvalidateUniscribeData(es);
2751 /*********************************************************************
2753 * EM_SETHANDLE
2755 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2758 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2760 if (!(es->style & ES_MULTILINE))
2761 return;
2763 if (!hloc) {
2764 WARN("called with NULL handle\n");
2765 return;
2768 EDIT_UnlockBuffer(es, TRUE);
2770 if(es->is_unicode)
2772 if(es->hloc32A)
2774 LocalFree(es->hloc32A);
2775 es->hloc32A = NULL;
2777 es->hloc32W = hloc;
2779 else
2781 INT countW, countA;
2782 HLOCAL hloc32W_new;
2783 WCHAR *textW;
2784 CHAR *textA;
2786 countA = LocalSize(hloc);
2787 textA = LocalLock(hloc);
2788 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2789 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2791 ERR("Could not allocate new unicode buffer\n");
2792 return;
2794 textW = LocalLock(hloc32W_new);
2795 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2796 LocalUnlock(hloc32W_new);
2797 LocalUnlock(hloc);
2799 if(es->hloc32W)
2800 LocalFree(es->hloc32W);
2802 es->hloc32W = hloc32W_new;
2803 es->hloc32A = hloc;
2806 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2808 /* The text buffer handle belongs to the control */
2809 es->hlocapp = NULL;
2811 EDIT_LockBuffer(es);
2812 text_buffer_changed(es);
2814 es->x_offset = es->y_offset = 0;
2815 es->selection_start = es->selection_end = 0;
2816 EDIT_EM_EmptyUndoBuffer(es);
2817 es->flags &= ~EF_MODIFIED;
2818 es->flags &= ~EF_UPDATE;
2819 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2820 EDIT_UpdateText(es, NULL, TRUE);
2821 EDIT_EM_ScrollCaret(es);
2822 /* force scroll info update */
2823 EDIT_UpdateScrollInfo(es);
2827 /*********************************************************************
2829 * EM_SETLIMITTEXT
2831 * NOTE: this version currently implements WinNT limits
2834 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2836 if (!limit) limit = ~0u;
2837 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2838 es->buffer_limit = limit;
2841 static BOOL is_cjk_charset(HDC dc)
2843 switch (GdiGetCodePage(dc)) {
2844 case 932: case 936: case 949: case 950: case 1361:
2845 return TRUE;
2846 default:
2847 return FALSE;
2851 static BOOL is_cjk_font(HDC dc)
2853 const DWORD FS_DBCS_MASK = FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB;
2854 FONTSIGNATURE fs;
2855 return (GetTextCharsetInfo(dc, &fs, 0) != DEFAULT_CHARSET &&
2856 (fs.fsCsb[0] & FS_DBCS_MASK));
2859 static int get_cjk_fontinfo_margin(int width, int side_bearing)
2861 int margin;
2862 if (side_bearing < 0)
2863 margin = min(-side_bearing, width/2);
2864 else
2865 margin = 0;
2866 return margin;
2869 struct char_width_info {
2870 INT min_lsb, min_rsb, unknown;
2873 /* Undocumented gdi32 export */
2874 extern BOOL WINAPI GetCharWidthInfo(HDC, struct char_width_info *);
2876 /*********************************************************************
2878 * EM_SETMARGINS
2880 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2881 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2882 * margin according to the textmetrics of the current font.
2884 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2885 * equal to or larger than a certain size. The empty client rect is treated as
2886 * 80 pixels width.
2888 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2889 WORD left, WORD right, BOOL repaint)
2891 TEXTMETRICW tm;
2892 INT default_left_margin = 0; /* in pixels */
2893 INT default_right_margin = 0; /* in pixels */
2895 /* Set the default margins depending on the font */
2896 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2897 HDC dc = GetDC(es->hwndSelf);
2898 HFONT old_font = SelectObject(dc, es->font);
2899 LONG width = GdiGetCharDimensions(dc, &tm, NULL), rc_width;
2900 RECT rc;
2902 /* The default margins are only non zero for TrueType or Vector fonts */
2903 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2904 struct char_width_info width_info;
2906 if ((is_cjk_charset(dc) || is_cjk_font(dc)) &&
2907 GetCharWidthInfo(dc, &width_info))
2909 default_left_margin = get_cjk_fontinfo_margin(width, width_info.min_lsb);
2910 default_right_margin = get_cjk_fontinfo_margin(width, width_info.min_rsb);
2912 else
2914 default_left_margin = width / 2;
2915 default_right_margin = width / 2;
2918 GetClientRect(es->hwndSelf, &rc);
2919 rc_width = !IsRectEmpty(&rc) ? rc.right - rc.left : 80;
2920 if (rc_width < default_left_margin + default_right_margin + width * 2) {
2921 default_left_margin = es->left_margin;
2922 default_right_margin = es->right_margin;
2925 SelectObject(dc, old_font);
2926 ReleaseDC(es->hwndSelf, dc);
2929 if (action & EC_LEFTMARGIN) {
2930 es->format_rect.left -= es->left_margin;
2931 if (left != EC_USEFONTINFO)
2932 es->left_margin = left;
2933 else
2934 es->left_margin = default_left_margin;
2935 es->format_rect.left += es->left_margin;
2938 if (action & EC_RIGHTMARGIN) {
2939 es->format_rect.right += es->right_margin;
2940 if (right != EC_USEFONTINFO)
2941 es->right_margin = right;
2942 else
2943 es->right_margin = default_right_margin;
2944 es->format_rect.right -= es->right_margin;
2947 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2948 EDIT_AdjustFormatRect(es);
2949 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2952 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2956 /*********************************************************************
2958 * EM_SETPASSWORDCHAR
2961 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2963 LONG style;
2965 if (es->style & ES_MULTILINE)
2966 return;
2968 if (es->password_char == c)
2969 return;
2971 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2972 es->password_char = c;
2973 if (c) {
2974 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2975 es->style |= ES_PASSWORD;
2976 } else {
2977 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2978 es->style &= ~ES_PASSWORD;
2980 EDIT_InvalidateUniscribeData(es);
2981 EDIT_UpdateText(es, NULL, TRUE);
2985 /*********************************************************************
2987 * EM_SETTABSTOPS
2990 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2992 if (!(es->style & ES_MULTILINE))
2993 return FALSE;
2994 HeapFree(GetProcessHeap(), 0, es->tabs);
2995 es->tabs_count = count;
2996 if (!count)
2997 es->tabs = NULL;
2998 else {
2999 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3000 memcpy(es->tabs, tabs, count * sizeof(INT));
3002 EDIT_InvalidateUniscribeData(es);
3003 return TRUE;
3007 /*********************************************************************
3009 * EM_SETWORDBREAKPROC
3012 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3014 if (es->word_break_proc == wbp)
3015 return;
3017 es->word_break_proc = wbp;
3019 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3020 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3021 EDIT_UpdateText(es, NULL, TRUE);
3026 /*********************************************************************
3028 * EM_UNDO / WM_UNDO
3031 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3033 INT ulength;
3034 LPWSTR utext;
3036 /* As per MSDN spec, for a single-line edit control,
3037 the return value is always TRUE */
3038 if( es->style & ES_READONLY )
3039 return !(es->style & ES_MULTILINE);
3041 ulength = lstrlenW(es->undo_text);
3043 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3045 lstrcpyW(utext, es->undo_text);
3047 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3048 es->undo_insert_count, debugstr_w(utext));
3050 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3051 EDIT_EM_EmptyUndoBuffer(es);
3052 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
3053 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3054 /* send the notification after the selection start and end are set */
3055 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3056 EDIT_EM_ScrollCaret(es);
3057 HeapFree(GetProcessHeap(), 0, utext);
3059 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3060 es->undo_insert_count, debugstr_w(es->undo_text));
3061 return TRUE;
3065 /* Helper function for WM_CHAR
3067 * According to an MSDN blog article titled "Just because you're a control
3068 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3069 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3070 * a dialog box or not.
3072 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3074 return (es->flags & EF_DIALOGMODE);
3078 /*********************************************************************
3080 * WM_PASTE
3083 static void EDIT_WM_Paste(EDITSTATE *es)
3085 HGLOBAL hsrc;
3086 LPWSTR src, ptr;
3087 int len;
3089 /* Protect read-only edit control from modification */
3090 if(es->style & ES_READONLY)
3091 return;
3093 OpenClipboard(es->hwndSelf);
3094 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3095 src = GlobalLock(hsrc);
3096 len = lstrlenW(src);
3097 /* Protect single-line edit against pasting new line character */
3098 if (!(es->style & ES_MULTILINE) && ((ptr = wcschr(src, '\n')))) {
3099 len = ptr - src;
3100 if (len && src[len - 1] == '\r')
3101 --len;
3103 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3104 GlobalUnlock(hsrc);
3106 else if (es->style & ES_PASSWORD) {
3107 /* clear selected text in password edit box even with empty clipboard */
3108 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3110 CloseClipboard();
3114 /*********************************************************************
3116 * WM_COPY
3119 static void EDIT_WM_Copy(EDITSTATE *es)
3121 INT s = min(es->selection_start, es->selection_end);
3122 INT e = max(es->selection_start, es->selection_end);
3123 HGLOBAL hdst;
3124 LPWSTR dst;
3125 DWORD len;
3127 if (e == s) return;
3129 len = e - s;
3130 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3131 dst = GlobalLock(hdst);
3132 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3133 dst[len] = 0; /* ensure 0 termination */
3134 TRACE("%s\n", debugstr_w(dst));
3135 GlobalUnlock(hdst);
3136 OpenClipboard(es->hwndSelf);
3137 EmptyClipboard();
3138 SetClipboardData(CF_UNICODETEXT, hdst);
3139 CloseClipboard();
3143 /*********************************************************************
3145 * WM_CLEAR
3148 static inline void EDIT_WM_Clear(EDITSTATE *es)
3150 /* Protect read-only edit control from modification */
3151 if(es->style & ES_READONLY)
3152 return;
3154 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3158 /*********************************************************************
3160 * WM_CUT
3163 static inline void EDIT_WM_Cut(EDITSTATE *es)
3165 EDIT_WM_Copy(es);
3166 EDIT_WM_Clear(es);
3170 /*********************************************************************
3172 * WM_CHAR
3175 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3177 BOOL control;
3179 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3181 switch (c) {
3182 case '\r':
3183 /* If it's not a multiline edit box, it would be ignored below.
3184 * For multiline edit without ES_WANTRETURN, we have to make a
3185 * special case.
3187 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3188 if (EDIT_IsInsideDialog(es))
3189 break;
3190 case '\n':
3191 if (es->style & ES_MULTILINE) {
3192 if (es->style & ES_READONLY) {
3193 EDIT_MoveHome(es, FALSE, FALSE);
3194 EDIT_MoveDown_ML(es, FALSE);
3195 } else {
3196 EDIT_EM_ReplaceSel(es, TRUE, L"\r\n", 2, TRUE, TRUE);
3199 break;
3200 case '\t':
3201 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3203 if (EDIT_IsInsideDialog(es))
3204 break;
3205 EDIT_EM_ReplaceSel(es, TRUE, L"\t", 1, TRUE, TRUE);
3207 break;
3208 case VK_BACK:
3209 if (!(es->style & ES_READONLY) && !control) {
3210 if (es->selection_start != es->selection_end)
3211 EDIT_WM_Clear(es);
3212 else {
3213 /* delete character left of caret */
3214 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3215 EDIT_MoveBackward(es, TRUE);
3216 EDIT_WM_Clear(es);
3219 break;
3220 case 0x03: /* ^C */
3221 if (!(es->style & ES_PASSWORD))
3222 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3223 break;
3224 case 0x16: /* ^V */
3225 if (!(es->style & ES_READONLY))
3226 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3227 break;
3228 case 0x18: /* ^X */
3229 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3230 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3231 break;
3232 case 0x1A: /* ^Z */
3233 if (!(es->style & ES_READONLY))
3234 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3235 break;
3237 default:
3238 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3239 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3240 break;
3242 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3243 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3244 break;
3246 return 1;
3250 /*********************************************************************
3252 * EDIT_ContextMenuCommand
3255 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3257 switch (id) {
3258 case EM_UNDO:
3259 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3260 break;
3261 case WM_CUT:
3262 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3263 break;
3264 case WM_COPY:
3265 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3266 break;
3267 case WM_PASTE:
3268 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3269 break;
3270 case WM_CLEAR:
3271 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3272 break;
3273 case EM_SETSEL:
3274 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3275 break;
3276 default:
3277 ERR("unknown menu item, please report\n");
3278 break;
3283 /*********************************************************************
3285 * WM_CONTEXTMENU
3287 * Note: the resource files resource/sysres_??.rc cannot define a
3288 * single popup menu. Hence we use a (dummy) menubar
3289 * containing the single popup menu as its first item.
3291 * FIXME: the message identifiers have been chosen arbitrarily,
3292 * hence we use MF_BYPOSITION.
3293 * We might as well use the "real" values (anybody knows ?)
3294 * The menu definition is in resources/sysres_??.rc.
3295 * Once these are OK, we better use MF_BYCOMMAND here
3296 * (as we do in EDIT_WM_Command()).
3299 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3301 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3302 HMENU popup = GetSubMenu(menu, 0);
3303 UINT start = es->selection_start;
3304 UINT end = es->selection_end;
3305 UINT cmd;
3307 ORDER_UINT(start, end);
3309 /* undo */
3310 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3311 /* cut */
3312 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3313 /* copy */
3314 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3315 /* paste */
3316 EnableMenuItem(popup, 4, MF_BYPOSITION | (NtUserIsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3317 /* delete */
3318 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3319 /* select all */
3320 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3322 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3324 RECT rc;
3325 /* Windows places the menu at the edit's center in this case */
3326 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3327 x = rc.left + (rc.right - rc.left) / 2;
3328 y = rc.top + (rc.bottom - rc.top) / 2;
3331 if (!(es->flags & EF_FOCUSED))
3332 SetFocus(es->hwndSelf);
3334 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3335 x, y, 0, es->hwndSelf, NULL);
3337 if (cmd)
3338 EDIT_ContextMenuCommand(es, cmd);
3340 DestroyMenu(menu);
3344 /*********************************************************************
3346 * WM_GETTEXT
3349 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3351 if(!count) return 0;
3353 if(unicode)
3355 lstrcpynW(dst, es->text, count);
3356 return lstrlenW(dst);
3358 else
3360 LPSTR textA = (LPSTR)dst;
3361 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3362 textA[count - 1] = 0; /* ensure 0 termination */
3363 return strlen(textA);
3367 /*********************************************************************
3369 * EDIT_CheckCombo
3372 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3374 HWND hLBox = es->hwndListBox;
3375 HWND hCombo;
3376 BOOL bDropped;
3377 int nEUI;
3379 if (!hLBox)
3380 return FALSE;
3382 hCombo = GetParent(es->hwndSelf);
3383 bDropped = TRUE;
3384 nEUI = 0;
3386 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3388 if (key == VK_UP || key == VK_DOWN)
3390 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3391 nEUI = 1;
3393 if (msg == WM_KEYDOWN || nEUI)
3394 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3397 switch (msg)
3399 case WM_KEYDOWN:
3400 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3402 /* make sure ComboLBox pops up */
3403 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3404 key = VK_F4;
3405 nEUI = 2;
3408 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3409 break;
3411 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3412 if (nEUI)
3413 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3414 else
3415 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3416 break;
3419 if(nEUI == 2)
3420 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3422 return TRUE;
3426 /*********************************************************************
3428 * WM_KEYDOWN
3430 * Handling of special keys that don't produce a WM_CHAR
3431 * (i.e. non-printable keys) & Backspace & Delete
3434 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3436 BOOL shift;
3437 BOOL control;
3439 if (NtUserGetKeyState(VK_MENU) & 0x8000)
3440 return 0;
3442 shift = NtUserGetKeyState(VK_SHIFT) & 0x8000;
3443 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3445 switch (key) {
3446 case VK_F4:
3447 case VK_UP:
3448 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3449 break;
3451 /* fall through */
3452 case VK_LEFT:
3453 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3454 EDIT_MoveUp_ML(es, shift);
3455 else
3456 if (control)
3457 EDIT_MoveWordBackward(es, shift);
3458 else
3459 EDIT_MoveBackward(es, shift);
3460 break;
3461 case VK_DOWN:
3462 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3463 break;
3464 /* fall through */
3465 case VK_RIGHT:
3466 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3467 EDIT_MoveDown_ML(es, shift);
3468 else if (control)
3469 EDIT_MoveWordForward(es, shift);
3470 else
3471 EDIT_MoveForward(es, shift);
3472 break;
3473 case VK_HOME:
3474 EDIT_MoveHome(es, shift, control);
3475 break;
3476 case VK_END:
3477 EDIT_MoveEnd(es, shift, control);
3478 break;
3479 case VK_PRIOR:
3480 if (es->style & ES_MULTILINE)
3481 EDIT_MovePageUp_ML(es, shift);
3482 else
3483 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3484 break;
3485 case VK_NEXT:
3486 if (es->style & ES_MULTILINE)
3487 EDIT_MovePageDown_ML(es, shift);
3488 else
3489 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3490 break;
3491 case VK_DELETE:
3492 if (!(es->style & ES_READONLY) && !(shift && control)) {
3493 if (es->selection_start != es->selection_end) {
3494 if (shift)
3495 EDIT_WM_Cut(es);
3496 else
3497 EDIT_WM_Clear(es);
3498 } else {
3499 EDIT_EM_SetSel(es, ~0u, 0, FALSE);
3500 if (shift)
3501 /* delete character left of caret */
3502 EDIT_MoveBackward(es, TRUE);
3503 else if (control)
3504 /* delete to end of line */
3505 EDIT_MoveEnd(es, TRUE, FALSE);
3506 else
3507 /* delete character right of caret */
3508 EDIT_MoveForward(es, TRUE);
3509 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);
3810 static DWORD get_font_margins(HDC hdc, const TEXTMETRICW *tm, BOOL unicode)
3812 ABC abc[256];
3813 SHORT left, right;
3814 UINT i;
3816 if (!(tm->tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE)))
3817 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3819 if (unicode) {
3820 if (!is_cjk_charset(hdc) && !is_cjk_font(hdc))
3821 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3822 if (!GetCharABCWidthsW(hdc, 0, 255, abc))
3823 return 0;
3825 else if (is_cjk_charset(hdc)) {
3826 if (!GetCharABCWidthsA(hdc, 0, 255, abc))
3827 return 0;
3829 else
3830 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3832 left = right = 0;
3833 for (i = 0; i < ARRAY_SIZE(abc); i++) {
3834 if (-abc[i].abcA > right) right = -abc[i].abcA;
3835 if (-abc[i].abcC > left ) left = -abc[i].abcC;
3837 return MAKELONG(left, right);
3840 /*********************************************************************
3842 * WM_SETFONT
3844 * With Win95 look the margins are set to default font value unless
3845 * the system font (font == 0) is being set, in which case they are left
3846 * unchanged.
3849 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3851 TEXTMETRICW tm;
3852 HDC dc;
3853 HFONT old_font = 0;
3854 RECT clientRect;
3855 DWORD margins;
3857 es->font = font;
3858 EDIT_InvalidateUniscribeData(es);
3859 dc = GetDC(es->hwndSelf);
3860 if (font)
3861 old_font = SelectObject(dc, font);
3862 GetTextMetricsW(dc, &tm);
3863 es->line_height = tm.tmHeight;
3864 es->char_width = tm.tmAveCharWidth;
3865 margins = get_font_margins(dc, &tm, es->is_unicode);
3866 if (font)
3867 SelectObject(dc, old_font);
3868 ReleaseDC(es->hwndSelf, dc);
3870 /* Reset the format rect and the margins */
3871 GetClientRect(es->hwndSelf, &clientRect);
3872 EDIT_SetRectNP(es, &clientRect);
3873 if (margins)
3874 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3875 LOWORD(margins), HIWORD(margins), FALSE);
3877 if (es->style & ES_MULTILINE)
3878 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3879 else
3880 EDIT_CalcLineWidth_SL(es);
3882 if (redraw)
3883 EDIT_UpdateText(es, NULL, TRUE);
3884 if (es->flags & EF_FOCUSED) {
3885 DestroyCaret();
3886 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3887 EDIT_SetCaretPos(es, es->selection_end,
3888 es->flags & EF_AFTER_WRAP);
3889 ShowCaret(es->hwndSelf);
3894 /*********************************************************************
3896 * WM_SETTEXT
3898 * NOTES
3899 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3900 * The modified flag is reset. No notifications are sent.
3902 * For single-line controls, reception of WM_SETTEXT triggers:
3903 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3906 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3908 LPWSTR textW = NULL;
3909 if (!unicode && text)
3911 LPCSTR textA = (LPCSTR)text;
3912 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3913 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3914 if (textW)
3915 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3916 text = textW;
3919 if (es->flags & EF_UPDATE)
3920 /* fixed this bug once; complain if we see it about to happen again. */
3921 ERR("SetSel may generate UPDATE message whose handler may reset "
3922 "selection.\n");
3924 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3925 if (text)
3927 TRACE("%s\n", debugstr_w(text));
3928 EDIT_EM_ReplaceSel(es, FALSE, text, lstrlenW(text), FALSE, FALSE);
3929 if(!unicode)
3930 HeapFree(GetProcessHeap(), 0, textW);
3932 else
3934 TRACE("<NULL>\n");
3935 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3937 es->x_offset = 0;
3938 es->flags &= ~EF_MODIFIED;
3939 EDIT_EM_SetSel(es, 0, 0, FALSE);
3941 /* Send the notification after the selection start and end have been set
3942 * edit control doesn't send notification on WM_SETTEXT
3943 * if it is multiline, or it is part of combobox
3945 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3947 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3948 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3950 EDIT_EM_ScrollCaret(es);
3951 EDIT_UpdateScrollInfo(es);
3952 EDIT_InvalidateUniscribeData(es);
3956 /*********************************************************************
3958 * WM_SIZE
3961 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3963 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3964 RECT rc;
3965 GetClientRect(es->hwndSelf, &rc);
3966 EDIT_SetRectNP(es, &rc);
3967 EDIT_UpdateText(es, NULL, TRUE);
3972 /*********************************************************************
3974 * WM_STYLECHANGED
3976 * This message is sent by SetWindowLong on having changed either the Style
3977 * or the extended style.
3979 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3981 * See also EDIT_WM_NCCreate
3983 * It appears that the Windows version of the edit control allows the style
3984 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3985 * style variable which will generally be different. In this function we
3986 * update the internal style based on what changed in the externally visible
3987 * style.
3989 * Much of this content as based upon the MSDN, especially:
3990 * Platform SDK Documentation -> User Interface Services ->
3991 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3992 * Edit Control Styles
3994 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3996 if (GWL_STYLE == which) {
3997 DWORD style_change_mask;
3998 DWORD new_style;
3999 /* Only a subset of changes can be applied after the control
4000 * has been created.
4002 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4003 ES_NUMBER;
4004 if (es->style & ES_MULTILINE)
4005 style_change_mask |= ES_WANTRETURN;
4007 new_style = style->styleNew & style_change_mask;
4009 /* Number overrides lowercase overrides uppercase (at least it
4010 * does in Win95). However I'll bet that ES_NUMBER would be
4011 * invalid under Win 3.1.
4013 if (new_style & ES_NUMBER) {
4014 ; /* do not override the ES_NUMBER */
4015 } else if (new_style & ES_LOWERCASE) {
4016 new_style &= ~ES_UPPERCASE;
4019 es->style = (es->style & ~style_change_mask) | new_style;
4020 } else if (GWL_EXSTYLE == which) {
4021 ; /* FIXME - what is needed here */
4022 } else {
4023 WARN ("Invalid style change %ld\n",which);
4026 return 0;
4029 /*********************************************************************
4031 * WM_SYSKEYDOWN
4034 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4036 if ((key == VK_BACK) && (key_data & 0x2000)) {
4037 if (EDIT_EM_CanUndo(es))
4038 EDIT_EM_Undo(es);
4039 return 0;
4040 } else if (key == VK_UP || key == VK_DOWN) {
4041 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4042 return 0;
4044 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4048 /*********************************************************************
4050 * WM_TIMER
4053 static void EDIT_WM_Timer(EDITSTATE *es)
4055 if (es->region_posx < 0) {
4056 EDIT_MoveBackward(es, TRUE);
4057 } else if (es->region_posx > 0) {
4058 EDIT_MoveForward(es, TRUE);
4061 * FIXME: gotta do some vertical scrolling here, like
4062 * EDIT_EM_LineScroll(hwnd, 0, 1);
4066 /*********************************************************************
4068 * WM_HSCROLL
4071 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4073 INT dx;
4074 INT fw;
4076 if (!(es->style & ES_MULTILINE))
4077 return 0;
4079 if (!(es->style & ES_AUTOHSCROLL))
4080 return 0;
4082 dx = 0;
4083 fw = es->format_rect.right - es->format_rect.left;
4084 switch (action) {
4085 case SB_LINELEFT:
4086 TRACE("SB_LINELEFT\n");
4087 if (es->x_offset)
4088 dx = -es->char_width;
4089 break;
4090 case SB_LINERIGHT:
4091 TRACE("SB_LINERIGHT\n");
4092 if (es->x_offset < es->text_width)
4093 dx = es->char_width;
4094 break;
4095 case SB_PAGELEFT:
4096 TRACE("SB_PAGELEFT\n");
4097 if (es->x_offset)
4098 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4099 break;
4100 case SB_PAGERIGHT:
4101 TRACE("SB_PAGERIGHT\n");
4102 if (es->x_offset < es->text_width)
4103 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4104 break;
4105 case SB_LEFT:
4106 TRACE("SB_LEFT\n");
4107 if (es->x_offset)
4108 dx = -es->x_offset;
4109 break;
4110 case SB_RIGHT:
4111 TRACE("SB_RIGHT\n");
4112 if (es->x_offset < es->text_width)
4113 dx = es->text_width - es->x_offset;
4114 break;
4115 case SB_THUMBTRACK:
4116 TRACE("SB_THUMBTRACK %d\n", pos);
4117 es->flags |= EF_HSCROLL_TRACK;
4118 if(es->style & WS_HSCROLL)
4119 dx = pos - es->x_offset;
4120 else
4122 INT fw, new_x;
4123 /* Sanity check */
4124 if(pos < 0 || pos > 100) return 0;
4125 /* Assume default scroll range 0-100 */
4126 fw = es->format_rect.right - es->format_rect.left;
4127 new_x = pos * (es->text_width - fw) / 100;
4128 dx = es->text_width ? (new_x - es->x_offset) : 0;
4130 break;
4131 case SB_THUMBPOSITION:
4132 TRACE("SB_THUMBPOSITION %d\n", pos);
4133 es->flags &= ~EF_HSCROLL_TRACK;
4134 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4135 dx = pos - es->x_offset;
4136 else
4138 INT fw, new_x;
4139 /* Sanity check */
4140 if(pos < 0 || pos > 100) return 0;
4141 /* Assume default scroll range 0-100 */
4142 fw = es->format_rect.right - es->format_rect.left;
4143 new_x = pos * (es->text_width - fw) / 100;
4144 dx = es->text_width ? (new_x - es->x_offset) : 0;
4146 if (!dx) {
4147 /* force scroll info update */
4148 EDIT_UpdateScrollInfo(es);
4149 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4151 break;
4152 case SB_ENDSCROLL:
4153 TRACE("SB_ENDSCROLL\n");
4154 break;
4156 * FIXME : the next two are undocumented !
4157 * Are we doing the right thing ?
4158 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4159 * although it's also a regular control message.
4161 case EM_GETTHUMB: /* this one is used by NT notepad */
4163 LRESULT ret;
4164 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4165 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4166 else
4168 /* Assume default scroll range 0-100 */
4169 INT fw = es->format_rect.right - es->format_rect.left;
4170 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4172 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4173 return ret;
4175 case EM_LINESCROLL:
4176 TRACE("EM_LINESCROLL16\n");
4177 dx = pos;
4178 break;
4180 default:
4181 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4182 action, action);
4183 return 0;
4185 if (dx)
4187 INT fw = es->format_rect.right - es->format_rect.left;
4188 /* check if we are going to move too far */
4189 if(es->x_offset + dx + fw > es->text_width)
4190 dx = es->text_width - fw - es->x_offset;
4191 if(dx)
4192 EDIT_EM_LineScroll_internal(es, dx, 0);
4194 return 0;
4198 /*********************************************************************
4200 * WM_VSCROLL
4203 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4205 INT dy;
4207 if (!(es->style & ES_MULTILINE))
4208 return 0;
4210 if (!(es->style & ES_AUTOVSCROLL))
4211 return 0;
4213 dy = 0;
4214 switch (action) {
4215 case SB_LINEUP:
4216 case SB_LINEDOWN:
4217 case SB_PAGEUP:
4218 case SB_PAGEDOWN:
4219 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4220 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4221 (action == SB_PAGEUP ? "SB_PAGEUP" :
4222 "SB_PAGEDOWN"))));
4223 EDIT_EM_Scroll(es, action);
4224 return 0;
4225 case SB_TOP:
4226 TRACE("SB_TOP\n");
4227 dy = -es->y_offset;
4228 break;
4229 case SB_BOTTOM:
4230 TRACE("SB_BOTTOM\n");
4231 dy = es->line_count - 1 - es->y_offset;
4232 break;
4233 case SB_THUMBTRACK:
4234 TRACE("SB_THUMBTRACK %d\n", pos);
4235 es->flags |= EF_VSCROLL_TRACK;
4236 if(es->style & WS_VSCROLL)
4237 dy = pos - es->y_offset;
4238 else
4240 /* Assume default scroll range 0-100 */
4241 INT vlc, new_y;
4242 /* Sanity check */
4243 if(pos < 0 || pos > 100) return 0;
4244 vlc = get_vertical_line_count(es);
4245 new_y = pos * (es->line_count - vlc) / 100;
4246 dy = es->line_count ? (new_y - es->y_offset) : 0;
4247 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4248 es->line_count, es->y_offset, pos, dy);
4250 break;
4251 case SB_THUMBPOSITION:
4252 TRACE("SB_THUMBPOSITION %d\n", pos);
4253 es->flags &= ~EF_VSCROLL_TRACK;
4254 if(es->style & WS_VSCROLL)
4255 dy = pos - es->y_offset;
4256 else
4258 /* Assume default scroll range 0-100 */
4259 INT vlc, new_y;
4260 /* Sanity check */
4261 if(pos < 0 || pos > 100) return 0;
4262 vlc = get_vertical_line_count(es);
4263 new_y = pos * (es->line_count - vlc) / 100;
4264 dy = es->line_count ? (new_y - es->y_offset) : 0;
4265 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4266 es->line_count, es->y_offset, pos, dy);
4268 if (!dy)
4270 /* force scroll info update */
4271 EDIT_UpdateScrollInfo(es);
4272 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4274 break;
4275 case SB_ENDSCROLL:
4276 TRACE("SB_ENDSCROLL\n");
4277 break;
4279 * FIXME : the next two are undocumented !
4280 * Are we doing the right thing ?
4281 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4282 * although it's also a regular control message.
4284 case EM_GETTHUMB: /* this one is used by NT notepad */
4286 LRESULT ret;
4287 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4288 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4289 else
4291 /* Assume default scroll range 0-100 */
4292 INT vlc = get_vertical_line_count(es);
4293 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4295 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4296 return ret;
4298 case EM_LINESCROLL:
4299 TRACE("EM_LINESCROLL %d\n", pos);
4300 dy = pos;
4301 break;
4303 default:
4304 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4305 action, action);
4306 return 0;
4308 if (dy)
4309 EDIT_EM_LineScroll(es, 0, dy);
4310 return 0;
4313 /*********************************************************************
4315 * EM_GETTHUMB
4317 * FIXME: is this right ? (or should it be only VSCROLL)
4318 * (and maybe only for edit controls that really have their
4319 * own scrollbars) (and maybe only for multiline controls ?)
4320 * All in all: very poorly documented
4323 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4325 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4326 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4330 /********************************************************************
4332 * The Following code is to handle inline editing from IMEs
4335 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4337 LONG buflen;
4338 LPWSTR lpCompStr;
4339 LPSTR lpCompStrAttr = NULL;
4340 DWORD dwBufLenAttr;
4342 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4344 if (buflen < 0)
4346 return;
4349 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen);
4350 if (!lpCompStr)
4352 ERR("Unable to allocate IME CompositionString\n");
4353 return;
4356 if (buflen)
4357 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4359 if (CompFlag & GCS_COMPATTR)
4362 * We do not use the attributes yet. it would tell us what characters
4363 * are in transition and which are converted or decided upon
4365 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4366 if (dwBufLenAttr)
4368 dwBufLenAttr ++;
4369 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4370 if (!lpCompStrAttr)
4372 ERR("Unable to allocate IME Attribute String\n");
4373 HeapFree(GetProcessHeap(),0,lpCompStr);
4374 return;
4376 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4377 dwBufLenAttr);
4378 lpCompStrAttr[dwBufLenAttr] = 0;
4382 /* check for change in composition start */
4383 if (es->selection_end < es->composition_start)
4384 es->composition_start = es->selection_end;
4386 /* replace existing selection string */
4387 es->selection_start = es->composition_start;
4389 if (es->composition_len > 0)
4390 es->selection_end = es->composition_start + es->composition_len;
4391 else
4392 es->selection_end = es->selection_start;
4394 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4395 es->composition_len = abs(es->composition_start - es->selection_end);
4397 es->selection_start = es->composition_start;
4398 es->selection_end = es->selection_start + es->composition_len;
4400 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4401 HeapFree(GetProcessHeap(),0,lpCompStr);
4404 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4406 LONG buflen;
4407 LPWSTR lpResultStr;
4409 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4410 if (buflen <= 0)
4412 return;
4415 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4416 if (!lpResultStr)
4418 ERR("Unable to alloc buffer for IME string\n");
4419 return;
4422 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4424 /* check for change in composition start */
4425 if (es->selection_end < es->composition_start)
4426 es->composition_start = es->selection_end;
4428 es->selection_start = es->composition_start;
4429 es->selection_end = es->composition_start + es->composition_len;
4430 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4431 es->composition_start = es->selection_end;
4432 es->composition_len = 0;
4434 HeapFree(GetProcessHeap(),0,lpResultStr);
4437 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4439 HIMC hIMC;
4440 int cursor;
4442 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4444 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4445 es->composition_start = es->selection_end;
4448 hIMC = ImmGetContext(hwnd);
4449 if (!hIMC)
4450 return;
4452 if (CompFlag & GCS_RESULTSTR)
4454 EDIT_GetResultStr(hIMC, es);
4455 cursor = 0;
4457 else
4459 if (CompFlag & GCS_COMPSTR)
4460 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4461 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4463 ImmReleaseContext(hwnd, hIMC);
4464 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4468 /*********************************************************************
4470 * WM_NCCREATE
4472 * See also EDIT_WM_StyleChanged
4474 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4476 EDITSTATE *es;
4477 UINT alloc_size;
4479 TRACE("Creating %s edit control, style = %08x\n",
4480 unicode ? "Unicode" : "ANSI", lpcs->style);
4482 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4483 return FALSE;
4484 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4487 * Note: since the EDITSTATE has not been fully initialized yet,
4488 * we can't use any API calls that may send
4489 * WM_XXX messages before WM_NCCREATE is completed.
4492 es->is_unicode = unicode;
4493 es->style = lpcs->style;
4495 es->bEnableState = !(es->style & WS_DISABLED);
4497 es->hwndSelf = hwnd;
4498 /* Save parent, which will be notified by EN_* messages */
4499 es->hwndParent = lpcs->hwndParent;
4501 if (es->style & ES_COMBO)
4502 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4504 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4505 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4507 /* Number overrides lowercase overrides uppercase (at least it
4508 * does in Win95). However I'll bet that ES_NUMBER would be
4509 * invalid under Win 3.1.
4511 if (es->style & ES_NUMBER) {
4512 ; /* do not override the ES_NUMBER */
4513 } else if (es->style & ES_LOWERCASE) {
4514 es->style &= ~ES_UPPERCASE;
4516 if (es->style & ES_MULTILINE) {
4517 es->buffer_limit = BUFLIMIT_INITIAL;
4518 if (es->style & WS_VSCROLL)
4519 es->style |= ES_AUTOVSCROLL;
4520 if (es->style & WS_HSCROLL)
4521 es->style |= ES_AUTOHSCROLL;
4522 es->style &= ~ES_PASSWORD;
4523 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4524 /* Confirmed - RIGHT overrides CENTER */
4525 if (es->style & ES_RIGHT)
4526 es->style &= ~ES_CENTER;
4527 es->style &= ~WS_HSCROLL;
4528 es->style &= ~ES_AUTOHSCROLL;
4530 } else {
4531 es->buffer_limit = BUFLIMIT_INITIAL;
4532 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4533 es->style &= ~ES_CENTER;
4534 es->style &= ~WS_HSCROLL;
4535 es->style &= ~WS_VSCROLL;
4536 if (es->style & ES_PASSWORD)
4537 es->password_char = '*';
4540 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4541 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4542 goto cleanup;
4543 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4545 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4546 goto cleanup;
4547 es->undo_buffer_size = es->buffer_size;
4549 if (es->style & ES_MULTILINE)
4550 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4551 goto cleanup;
4552 es->line_count = 1;
4555 * In Win95 look and feel, the WS_BORDER style is replaced by the
4556 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4557 * control a nonclient area so we don't need to draw the border.
4558 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4559 * a nonclient area and we should handle painting the border ourselves.
4561 * When making modifications please ensure that the code still works
4562 * for edit controls created directly with style 0x50800000, exStyle 0
4563 * (which should have a single pixel border)
4565 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4566 es->style &= ~WS_BORDER;
4567 else if (es->style & WS_BORDER)
4568 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4570 return TRUE;
4572 cleanup:
4573 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4574 EDIT_InvalidateUniscribeData(es);
4575 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4576 HeapFree(GetProcessHeap(), 0, es->undo_text);
4577 if (es->hloc32W) LocalFree(es->hloc32W);
4578 HeapFree(GetProcessHeap(), 0, es->logAttr);
4579 HeapFree(GetProcessHeap(), 0, es);
4580 return FALSE;
4584 /*********************************************************************
4586 * WM_CREATE
4589 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4591 RECT clientRect;
4593 TRACE("%s\n", debugstr_w(name));
4595 * To initialize some final structure members, we call some helper
4596 * functions. However, since the EDITSTATE is not consistent (i.e.
4597 * not fully initialized), we should be very careful which
4598 * functions can be called, and in what order.
4600 EDIT_WM_SetFont(es, 0, FALSE);
4601 EDIT_EM_EmptyUndoBuffer(es);
4603 /* We need to calculate the format rect
4604 (applications may send EM_SETMARGINS before the control gets visible) */
4605 GetClientRect(es->hwndSelf, &clientRect);
4606 EDIT_SetRectNP(es, &clientRect);
4608 if (name && *name) {
4609 EDIT_EM_ReplaceSel(es, FALSE, name, lstrlenW(name), FALSE, FALSE);
4610 /* if we insert text to the editline, the text scrolls out
4611 * of the window, as the caret is placed after the insert
4612 * pos normally; thus we reset es->selection... to 0 and
4613 * update caret
4615 es->selection_start = es->selection_end = 0;
4616 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4617 * Messages are only to be sent when the USER does something to
4618 * change the contents. So I am removing this EN_CHANGE
4620 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4622 EDIT_EM_ScrollCaret(es);
4624 /* force scroll info update */
4625 EDIT_UpdateScrollInfo(es);
4626 /* The rule seems to return 1 here for success */
4627 /* Power Builder masked edit controls will crash */
4628 /* if not. */
4629 /* FIXME: is that in all cases so ? */
4630 return 1;
4634 /*********************************************************************
4636 * WM_NCDESTROY
4639 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4641 LINEDEF *pc, *pp;
4643 /* The app can own the text buffer handle */
4644 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4645 LocalFree(es->hloc32W);
4647 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4648 LocalFree(es->hloc32A);
4650 EDIT_InvalidateUniscribeData(es);
4651 pc = es->first_line_def;
4652 while (pc)
4654 pp = pc->next;
4655 HeapFree(GetProcessHeap(), 0, pc);
4656 pc = pp;
4659 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4660 HeapFree(GetProcessHeap(), 0, es->undo_text);
4661 HeapFree(GetProcessHeap(), 0, es);
4663 return 0;
4667 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4669 if(unicode)
4670 return DefWindowProcW(hwnd, msg, wParam, lParam);
4671 else
4672 return DefWindowProcA(hwnd, msg, wParam, lParam);
4675 /*********************************************************************
4677 * EditWndProc_common
4679 * The messages are in the order of the actual integer values
4680 * (which can be found in include/windows.h)
4682 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4684 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4685 LRESULT result = 0;
4687 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4689 if (!es && msg != WM_NCCREATE)
4690 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4692 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4694 switch (msg) {
4695 case EM_GETSEL:
4696 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4697 break;
4699 case EM_SETSEL:
4700 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4701 EDIT_EM_ScrollCaret(es);
4702 result = 1;
4703 break;
4705 case EM_GETRECT:
4706 if (lParam)
4707 *((LPRECT)lParam) = es->format_rect;
4708 break;
4710 case EM_SETRECT:
4711 if ((es->style & ES_MULTILINE) && lParam) {
4712 EDIT_SetRectNP(es, (LPRECT)lParam);
4713 EDIT_UpdateText(es, NULL, TRUE);
4715 break;
4717 case EM_SETRECTNP:
4718 if ((es->style & ES_MULTILINE) && lParam)
4719 EDIT_SetRectNP(es, (LPRECT)lParam);
4720 break;
4722 case EM_SCROLL:
4723 result = EDIT_EM_Scroll(es, (INT)wParam);
4724 break;
4726 case EM_LINESCROLL:
4727 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4728 break;
4730 case EM_SCROLLCARET:
4731 EDIT_EM_ScrollCaret(es);
4732 result = 1;
4733 break;
4735 case EM_GETMODIFY:
4736 result = ((es->flags & EF_MODIFIED) != 0);
4737 break;
4739 case EM_SETMODIFY:
4740 if (wParam)
4741 es->flags |= EF_MODIFIED;
4742 else
4743 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4744 break;
4746 case EM_GETLINECOUNT:
4747 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4748 break;
4750 case EM_LINEINDEX:
4751 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4752 break;
4754 case EM_SETHANDLE:
4755 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4756 break;
4758 case EM_GETHANDLE:
4759 result = (LRESULT)EDIT_EM_GetHandle(es);
4760 break;
4762 case EM_GETTHUMB:
4763 result = EDIT_EM_GetThumb(es);
4764 break;
4766 /* these messages missing from specs */
4767 case 0x00bf:
4768 case 0x00c0:
4769 case 0x00c3:
4770 case 0x00ca:
4771 FIXME("undocumented message 0x%x, please report\n", msg);
4772 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4773 break;
4775 case EM_LINELENGTH:
4776 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4777 break;
4779 case EM_REPLACESEL:
4781 LPWSTR textW;
4783 if(unicode)
4784 textW = (LPWSTR)lParam;
4785 else
4787 LPSTR textA = (LPSTR)lParam;
4788 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4789 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4790 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4793 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, lstrlenW(textW), TRUE, TRUE);
4794 result = 1;
4796 if(!unicode)
4797 HeapFree(GetProcessHeap(), 0, textW);
4798 break;
4801 case EM_GETLINE:
4802 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4803 break;
4805 case EM_SETLIMITTEXT:
4806 EDIT_EM_SetLimitText(es, wParam);
4807 break;
4809 case EM_CANUNDO:
4810 result = (LRESULT)EDIT_EM_CanUndo(es);
4811 break;
4813 case EM_UNDO:
4814 case WM_UNDO:
4815 result = (LRESULT)EDIT_EM_Undo(es);
4816 break;
4818 case EM_FMTLINES:
4819 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4820 break;
4822 case EM_LINEFROMCHAR:
4823 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4824 break;
4826 case EM_SETTABSTOPS:
4827 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4828 break;
4830 case EM_SETPASSWORDCHAR:
4832 WCHAR charW = 0;
4834 if(unicode)
4835 charW = (WCHAR)wParam;
4836 else
4838 CHAR charA = wParam;
4839 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4842 EDIT_EM_SetPasswordChar(es, charW);
4843 break;
4846 case EM_EMPTYUNDOBUFFER:
4847 EDIT_EM_EmptyUndoBuffer(es);
4848 break;
4850 case EM_GETFIRSTVISIBLELINE:
4851 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4852 break;
4854 case EM_SETREADONLY:
4856 DWORD old_style = es->style;
4858 if (wParam) {
4859 SetWindowLongW( hwnd, GWL_STYLE,
4860 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4861 es->style |= ES_READONLY;
4862 } else {
4863 SetWindowLongW( hwnd, GWL_STYLE,
4864 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4865 es->style &= ~ES_READONLY;
4868 if (old_style ^ es->style)
4869 InvalidateRect(es->hwndSelf, NULL, TRUE);
4871 result = 1;
4872 break;
4875 case EM_SETWORDBREAKPROC:
4876 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4877 result = 1;
4878 break;
4880 case EM_GETWORDBREAKPROC:
4881 result = (LRESULT)es->word_break_proc;
4882 break;
4884 case EM_GETPASSWORDCHAR:
4886 if(unicode)
4887 result = es->password_char;
4888 else
4890 WCHAR charW = es->password_char;
4891 CHAR charA = 0;
4892 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4893 result = charA;
4895 break;
4898 case EM_SETMARGINS:
4899 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4900 break;
4902 case EM_GETMARGINS:
4903 result = MAKELONG(es->left_margin, es->right_margin);
4904 break;
4906 case EM_GETLIMITTEXT:
4907 result = es->buffer_limit;
4908 break;
4910 case EM_POSFROMCHAR:
4911 if ((INT)wParam >= get_text_length(es)) result = -1;
4912 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4913 break;
4915 case EM_CHARFROMPOS:
4916 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4917 break;
4919 /* End of the EM_ messages which were in numerical order; what order
4920 * are these in? vaguely alphabetical?
4923 case WM_NCCREATE:
4924 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4925 break;
4927 case WM_NCDESTROY:
4928 result = EDIT_WM_NCDestroy(es);
4929 es = NULL;
4930 break;
4932 case WM_GETDLGCODE:
4933 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4935 if (es->style & ES_MULTILINE)
4936 result |= DLGC_WANTALLKEYS;
4938 if (lParam)
4940 es->flags|=EF_DIALOGMODE;
4942 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4944 int vk = (int)((LPMSG)lParam)->wParam;
4946 if (es->hwndListBox)
4948 if (vk == VK_RETURN || vk == VK_ESCAPE)
4949 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4950 result |= DLGC_WANTMESSAGE;
4954 break;
4956 case WM_IME_CHAR:
4957 if (!unicode)
4959 WCHAR charW;
4960 CHAR strng[2];
4962 strng[0] = wParam >> 8;
4963 strng[1] = wParam & 0xff;
4964 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4965 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4966 result = EDIT_WM_Char(es, charW);
4967 break;
4969 /* fall through */
4970 case WM_CHAR:
4972 WCHAR charW;
4974 if(unicode)
4975 charW = wParam;
4976 else
4978 CHAR charA = wParam;
4979 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4982 if (es->hwndListBox)
4984 if (charW == VK_RETURN || charW == VK_ESCAPE)
4986 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4987 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4988 break;
4991 result = EDIT_WM_Char(es, charW);
4992 break;
4995 case WM_UNICHAR:
4996 if (unicode)
4998 if (wParam == UNICODE_NOCHAR) return TRUE;
4999 if (wParam <= 0x000fffff)
5001 if(wParam > 0xffff) /* convert to surrogates */
5003 wParam -= 0x10000;
5004 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
5005 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
5007 else EDIT_WM_Char(es, wParam);
5009 return 0;
5011 break;
5013 case WM_CLEAR:
5014 EDIT_WM_Clear(es);
5015 break;
5017 case WM_CONTEXTMENU:
5018 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5019 break;
5021 case WM_COPY:
5022 EDIT_WM_Copy(es);
5023 break;
5025 case WM_CREATE:
5026 if(unicode)
5027 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
5028 else
5030 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
5031 LPWSTR nameW = NULL;
5032 if(nameA)
5034 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5035 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5036 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5038 result = EDIT_WM_Create(es, nameW);
5039 HeapFree(GetProcessHeap(), 0, nameW);
5041 break;
5043 case WM_CUT:
5044 EDIT_WM_Cut(es);
5045 break;
5047 case WM_ENABLE:
5048 es->bEnableState = (BOOL) wParam;
5049 EDIT_UpdateText(es, NULL, TRUE);
5050 break;
5052 case WM_ERASEBKGND:
5053 /* we do the proper erase in EDIT_WM_Paint */
5054 result = 1;
5055 break;
5057 case WM_GETFONT:
5058 result = (LRESULT)es->font;
5059 break;
5061 case WM_GETTEXT:
5062 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5063 break;
5065 case WM_GETTEXTLENGTH:
5066 if (unicode) result = get_text_length(es);
5067 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5068 NULL, 0, NULL, NULL );
5069 break;
5071 case WM_HSCROLL:
5072 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5073 break;
5075 case WM_KEYDOWN:
5076 result = EDIT_WM_KeyDown(es, (INT)wParam);
5077 break;
5079 case WM_KILLFOCUS:
5080 result = EDIT_WM_KillFocus(es);
5081 break;
5083 case WM_LBUTTONDBLCLK:
5084 result = EDIT_WM_LButtonDblClk(es);
5085 break;
5087 case WM_LBUTTONDOWN:
5088 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5089 break;
5091 case WM_LBUTTONUP:
5092 result = EDIT_WM_LButtonUp(es);
5093 break;
5095 case WM_MBUTTONDOWN:
5096 result = EDIT_WM_MButtonDown(es);
5097 break;
5099 case WM_MOUSEMOVE:
5100 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5101 break;
5103 case WM_PRINTCLIENT:
5104 case WM_PAINT:
5105 EDIT_WM_Paint(es, (HDC)wParam);
5106 break;
5108 case WM_PASTE:
5109 EDIT_WM_Paste(es);
5110 break;
5112 case WM_SETFOCUS:
5113 EDIT_WM_SetFocus(es);
5114 break;
5116 case WM_SETFONT:
5117 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5118 break;
5120 case WM_SETREDRAW:
5121 /* FIXME: actually set an internal flag and behave accordingly */
5122 break;
5124 case WM_SETTEXT:
5125 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5126 result = TRUE;
5127 break;
5129 case WM_SIZE:
5130 EDIT_WM_Size(es, (UINT)wParam);
5131 break;
5133 case WM_STYLECHANGED:
5134 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5135 break;
5137 case WM_STYLECHANGING:
5138 result = 0; /* See EDIT_WM_StyleChanged */
5139 break;
5141 case WM_SYSKEYDOWN:
5142 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5143 break;
5145 case WM_TIMER:
5146 EDIT_WM_Timer(es);
5147 break;
5149 case WM_VSCROLL:
5150 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5151 break;
5153 case WM_MOUSEWHEEL:
5155 int wheelDelta;
5156 INT pulScrollLines = 3;
5157 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5159 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5160 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5161 break;
5163 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5164 /* if scrolling changes direction, ignore left overs */
5165 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5166 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5167 es->wheelDeltaRemainder += wheelDelta;
5168 else
5169 es->wheelDeltaRemainder = wheelDelta;
5170 if (es->wheelDeltaRemainder && pulScrollLines)
5172 int cLineScroll;
5173 pulScrollLines = min(es->line_count, pulScrollLines);
5174 cLineScroll = pulScrollLines * es->wheelDeltaRemainder / WHEEL_DELTA;
5175 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
5176 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5179 break;
5182 /* IME messages to make the edit control IME aware */
5183 case WM_IME_SETCONTEXT:
5184 break;
5186 case WM_IME_STARTCOMPOSITION:
5187 es->composition_start = es->selection_end;
5188 es->composition_len = 0;
5189 break;
5191 case WM_IME_COMPOSITION:
5192 EDIT_ImeComposition(hwnd, lParam, es);
5193 break;
5195 case WM_IME_ENDCOMPOSITION:
5196 if (es->composition_len > 0)
5198 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5199 es->selection_end = es->selection_start;
5200 es->composition_len= 0;
5202 break;
5204 case WM_IME_COMPOSITIONFULL:
5205 break;
5207 case WM_IME_SELECT:
5208 break;
5210 case WM_IME_CONTROL:
5211 break;
5213 case WM_IME_REQUEST:
5214 switch (wParam)
5216 case IMR_QUERYCHARPOSITION:
5218 LRESULT pos;
5219 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5221 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5222 chpos->pt.x = LOWORD(pos);
5223 chpos->pt.y = HIWORD(pos);
5224 chpos->cLineHeight = es->line_height;
5225 chpos->rcDocument = es->format_rect;
5226 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5227 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5228 result = 1;
5229 break;
5232 break;
5234 default:
5235 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5236 break;
5239 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5240 EDIT_UnlockBuffer(es, FALSE);
5242 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5244 return result;
5248 /*********************************************************************
5249 * edit class descriptor
5251 const struct builtin_class_descr EDIT_builtin_class =
5253 L"Edit", /* name */
5254 CS_DBLCLKS | CS_PARENTDC, /* style */
5255 WINPROC_EDIT, /* proc */
5256 #ifdef __i386__
5257 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
5258 #else
5259 sizeof(EDITSTATE *), /* extra */
5260 #endif
5261 IDC_IBEAM, /* cursor */
5262 0 /* brush */