libport: Remove support for PPC32.
[wine.git] / dlls / user32 / edit.c
blob683c2faea3c28052c78a37820b935f315b8f301c
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 "config.h"
33 #include <stdarg.h>
34 #include <string.h>
35 #include <stdlib.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winnt.h"
40 #include "win.h"
41 #include "imm.h"
42 #include "usp10.h"
43 #include "wine/unicode.h"
44 #include "controls.h"
45 #include "user_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(edit);
49 WINE_DECLARE_DEBUG_CHANNEL(combo);
50 WINE_DECLARE_DEBUG_CHANNEL(relay);
52 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
53 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
54 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
55 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
58 * extra flags for EDITSTATE.flags field
60 #define EF_MODIFIED 0x0001 /* text has been modified */
61 #define EF_FOCUSED 0x0002 /* we have input focus */
62 #define EF_UPDATE 0x0004 /* notify parent of changed state */
63 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
64 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
65 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
66 wrapped line, instead of in front of the next character */
67 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
68 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
70 typedef enum
72 END_0 = 0, /* line ends with terminating '\0' character */
73 END_WRAP, /* line is wrapped */
74 END_HARD, /* line ends with a hard return '\r\n' */
75 END_SOFT, /* line ends with a soft return '\r\r\n' */
76 END_RICH /* line ends with a single '\n' */
77 } LINE_END;
79 typedef struct tagLINEDEF {
80 INT length; /* bruto length of a line in bytes */
81 INT net_length; /* netto length of a line in visible characters */
82 LINE_END ending;
83 INT width; /* width of the line in pixels */
84 INT index; /* line index into the buffer */
85 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
86 struct tagLINEDEF *next;
87 } LINEDEF;
89 typedef struct
91 BOOL is_unicode; /* how the control was created */
92 LPWSTR text; /* the actual contents of the control */
93 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
94 UINT buffer_size; /* the size of the buffer in characters */
95 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
96 HFONT font; /* NULL means standard system font */
97 INT x_offset; /* scroll offset for multi lines this is in pixels
98 for single lines it's in characters */
99 INT line_height; /* height of a screen line in pixels */
100 INT char_width; /* average character width in pixels */
101 DWORD style; /* sane version of wnd->dwStyle */
102 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
103 INT undo_insert_count; /* number of characters inserted in sequence */
104 UINT undo_position; /* character index of the insertion and deletion */
105 LPWSTR undo_text; /* deleted text */
106 UINT undo_buffer_size; /* size of the deleted text buffer */
107 INT selection_start; /* == selection_end if no selection */
108 INT selection_end; /* == current caret position */
109 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
110 INT left_margin; /* in pixels */
111 INT right_margin; /* in pixels */
112 RECT format_rect;
113 INT text_width; /* width of the widest line in pixels for multi line controls
114 and just line width for single line controls */
115 INT region_posx; /* Position of cursor relative to region: */
116 INT region_posy; /* -1: to left, 0: within, 1: to right */
117 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
118 INT line_count; /* number of lines */
119 INT y_offset; /* scroll offset in number of lines */
120 BOOL bCaptureState; /* flag indicating whether mouse was captured */
121 BOOL bEnableState; /* flag keeping the enable state */
122 HWND hwndSelf; /* the our window handle */
123 HWND hwndParent; /* Handle of parent for sending EN_* messages.
124 Even if parent will change, EN_* messages
125 should be sent to the first parent. */
126 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
127 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
129 * only for multi line controls
131 INT lock_count; /* amount of re-entries in the EditWndProc */
132 INT tabs_count;
133 LPINT tabs;
134 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
135 HLOCAL hloc32W; /* our unicode local memory block */
136 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
137 or EM_SETHANDLE */
138 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
140 * IME Data
142 UINT composition_len; /* length of composition, 0 == no composition */
143 int composition_start; /* the character position for the composition */
145 * Uniscribe Data
147 SCRIPT_LOGATTR *logAttr;
148 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
149 } EDITSTATE;
152 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
153 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
155 /* used for disabled or read-only edit control */
156 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
157 do \
158 { /* Notify parent which has created this edit control */ \
159 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
160 SendMessageW(es->hwndParent, WM_COMMAND, \
161 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
162 (LPARAM)(es->hwndSelf)); \
163 } while(0)
165 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
167 /*********************************************************************
169 * EM_CANUNDO
172 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
174 return (es->undo_insert_count || strlenW(es->undo_text));
178 /*********************************************************************
180 * EM_EMPTYUNDOBUFFER
183 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
185 es->undo_insert_count = 0;
186 *es->undo_text = '\0';
190 /**********************************************************************
191 * get_app_version
193 * Returns the window version in case Wine emulates a later version
194 * of windows than the application expects.
196 * In a number of cases when windows runs an application that was
197 * designed for an earlier windows version, windows reverts
198 * to "old" behaviour of that earlier version.
200 * An example is a disabled edit control that needs to be painted.
201 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
202 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
203 * applications with an expected version 0f 4.0 or higher.
206 static DWORD get_app_version(void)
208 static DWORD version;
209 if (!version)
211 DWORD dwEmulatedVersion;
212 OSVERSIONINFOW info;
213 DWORD dwProcVersion = GetProcessVersion(0);
215 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
216 GetVersionExW( &info );
217 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
218 /* FIXME: this may not be 100% correct; see discussion on the
219 * wine developer list in Nov 1999 */
220 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
222 return version;
225 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
227 HBRUSH hbrush;
228 UINT msg;
230 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
231 msg = WM_CTLCOLORSTATIC;
232 else
233 msg = WM_CTLCOLOREDIT;
235 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
236 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
237 if (!hbrush)
238 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
239 return hbrush;
243 static inline UINT get_text_length(EDITSTATE *es)
245 if(es->text_length == (UINT)-1)
246 es->text_length = strlenW(es->text);
247 return es->text_length;
251 /*********************************************************************
253 * EDIT_WordBreakProc
255 * Find the beginning of words.
256 * Note: unlike the specs for a WordBreakProc, this function can
257 * only be called without linebreaks between s[0] up to
258 * s[count - 1]. Remember it is only called
259 * internally, so we can decide this for ourselves.
260 * Additionally we will always be breaking the full string.
263 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
265 INT ret = 0;
267 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
269 if(!s) return 0;
271 if (!es->logAttr)
273 SCRIPT_ANALYSIS psa;
275 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
276 psa.eScript = SCRIPT_UNDEFINED;
278 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
279 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
282 switch (action) {
283 case WB_LEFT:
284 if (index)
285 index--;
286 while (index && !es->logAttr[index].fSoftBreak)
287 index--;
288 ret = index;
289 break;
290 case WB_RIGHT:
291 if (!count)
292 break;
293 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
294 index++;
295 ret = index;
296 break;
297 case WB_ISDELIMITER:
298 ret = es->logAttr[index].fWhiteSpace;
299 break;
300 default:
301 ERR("unknown action code, please report !\n");
302 break;
304 return ret;
308 /*********************************************************************
310 * EDIT_CallWordBreakProc
312 * Call appropriate WordBreakProc (internal or external).
314 * Note: The "start" argument should always be an index referring
315 * to es->text. The actual wordbreak proc might be
316 * 16 bit, so we can't always pass any 32 bit LPSTR.
317 * Hence we assume that es->text is the buffer that holds
318 * the string under examination (we can decide this for ourselves).
321 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
323 INT ret;
325 if (es->word_break_proc)
327 if(es->is_unicode)
329 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
331 TRACE_(relay)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
332 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
333 ret = wbpW(es->text + start, index, count, action);
334 TRACE_(relay)("\1Ret wordbreakW %p retval=%d\n", es->word_break_proc, ret );
336 else
338 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
339 INT countA;
340 CHAR *textA;
342 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
343 textA = HeapAlloc(GetProcessHeap(), 0, countA);
344 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
345 TRACE_(relay)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
346 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
347 ret = wbpA(textA, index, countA, action);
348 HeapFree(GetProcessHeap(), 0, textA);
349 TRACE_(relay)("\1Ret wordbreakA %p retval=%d\n", es->word_break_proc, ret );
352 else
353 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
355 return ret;
358 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
360 if (line_def->ssa)
362 ScriptStringFree(&line_def->ssa);
363 line_def->ssa = NULL;
367 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
369 LINEDEF *line_def = es->first_line_def;
370 while (line_def)
372 EDIT_InvalidateUniscribeData_linedef(line_def);
373 line_def = line_def->next;
375 if (es->ssa)
377 ScriptStringFree(&es->ssa);
378 es->ssa = NULL;
382 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
384 if (!line_def)
385 return NULL;
387 if (line_def->net_length && !line_def->ssa)
389 int index = line_def->index;
390 HFONT old_font = NULL;
391 HDC udc = dc;
392 SCRIPT_TABDEF tabdef;
393 HRESULT hr;
395 if (!udc)
396 udc = GetDC(es->hwndSelf);
397 if (es->font)
398 old_font = SelectObject(udc, es->font);
400 tabdef.cTabStops = es->tabs_count;
401 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
402 tabdef.pTabStops = es->tabs;
403 tabdef.iTabOrigin = 0;
405 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
406 (1.5*line_def->net_length+16), -1,
407 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
408 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
409 if (FAILED(hr))
411 WARN("ScriptStringAnalyse failed (%x)\n",hr);
412 line_def->ssa = NULL;
415 if (es->font)
416 SelectObject(udc, old_font);
417 if (udc != dc)
418 ReleaseDC(es->hwndSelf, udc);
421 return line_def->ssa;
424 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
426 LINEDEF *line_def;
428 if (!(es->style & ES_MULTILINE))
430 if (!es->ssa)
432 INT length = get_text_length(es);
433 HFONT old_font = NULL;
434 HDC udc = dc;
436 if (!udc)
437 udc = GetDC(es->hwndSelf);
438 if (es->font)
439 old_font = SelectObject(udc, es->font);
441 if (es->style & ES_PASSWORD)
442 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);
443 else
444 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
446 if (es->font)
447 SelectObject(udc, old_font);
448 if (udc != dc)
449 ReleaseDC(es->hwndSelf, udc);
451 return es->ssa;
453 else
455 line_def = es->first_line_def;
456 while (line_def && line)
458 line_def = line_def->next;
459 line--;
462 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
466 static inline INT get_vertical_line_count(EDITSTATE *es)
468 INT vlc = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
469 return max(1,vlc);
472 /*********************************************************************
474 * EDIT_BuildLineDefs_ML
476 * Build linked list of text lines.
477 * Lines can end with '\0' (last line), a character (if it is wrapped),
478 * a soft return '\r\r\n' or a hard return '\r\n'
481 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
483 LPWSTR current_position, cp;
484 INT fw;
485 LINEDEF *current_line;
486 LINEDEF *previous_line;
487 LINEDEF *start_line;
488 INT line_index = 0, nstart_line, nstart_index;
489 INT line_count = es->line_count;
490 INT orig_net_length;
491 RECT rc;
492 INT vlc;
494 if (istart == iend && delta == 0)
495 return;
497 previous_line = NULL;
498 current_line = es->first_line_def;
500 /* Find starting line. istart must lie inside an existing line or
501 * at the end of buffer */
502 do {
503 if (istart < current_line->index + current_line->length ||
504 current_line->ending == END_0)
505 break;
507 previous_line = current_line;
508 current_line = current_line->next;
509 line_index++;
510 } while (current_line);
512 if (!current_line) /* Error occurred start is not inside previous buffer */
514 FIXME(" modification occurred outside buffer\n");
515 return;
518 /* Remember start of modifications in order to calculate update region */
519 nstart_line = line_index;
520 nstart_index = current_line->index;
522 /* We must start to reformat from the previous line since the modifications
523 * may have caused the line to wrap upwards. */
524 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
526 line_index--;
527 current_line = previous_line;
529 start_line = current_line;
531 fw = es->format_rect.right - es->format_rect.left;
532 current_position = es->text + current_line->index;
533 vlc = get_vertical_line_count(es);
534 do {
535 if (current_line != start_line)
537 if (!current_line || current_line->index + delta > current_position - es->text)
539 /* The buffer has been expanded, create a new line and
540 insert it into the link list */
541 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
542 new_line->next = previous_line->next;
543 previous_line->next = new_line;
544 current_line = new_line;
545 es->line_count++;
547 else if (current_line->index + delta < current_position - es->text)
549 /* The previous line merged with this line so we delete this extra entry */
550 previous_line->next = current_line->next;
551 HeapFree(GetProcessHeap(), 0, current_line);
552 current_line = previous_line->next;
553 es->line_count--;
554 continue;
556 else /* current_line->index + delta == current_position */
558 if (current_position - es->text > iend)
559 break; /* We reached end of line modifications */
560 /* else recalculate this line */
564 current_line->index = current_position - es->text;
565 orig_net_length = current_line->net_length;
567 /* Find end of line */
568 cp = current_position;
569 while (*cp) {
570 if (*cp == '\n') break;
571 if ((*cp == '\r') && (*(cp + 1) == '\n'))
572 break;
573 cp++;
576 /* Mark type of line termination */
577 if (!(*cp)) {
578 current_line->ending = END_0;
579 current_line->net_length = strlenW(current_position);
580 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
581 current_line->ending = END_SOFT;
582 current_line->net_length = cp - current_position - 1;
583 } else if (*cp == '\n') {
584 current_line->ending = END_RICH;
585 current_line->net_length = cp - current_position;
586 } else {
587 current_line->ending = END_HARD;
588 current_line->net_length = cp - current_position;
591 if (current_line->net_length)
593 const SIZE *sz;
594 EDIT_InvalidateUniscribeData_linedef(current_line);
595 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
596 if (current_line->ssa)
598 sz = ScriptString_pSize(current_line->ssa);
599 /* Calculate line width */
600 current_line->width = sz->cx;
602 else current_line->width = es->char_width * current_line->net_length;
604 else current_line->width = 0;
606 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
608 /* Line breaks just look back from the end and find the next break and try that. */
610 if (!(es->style & ES_AUTOHSCROLL)) {
611 if (current_line->width > fw && fw > es->char_width) {
613 INT prev, next;
614 int w;
615 const SIZE *sz;
616 float d;
618 prev = current_line->net_length - 1;
619 w = current_line->net_length;
620 d = (float)current_line->width/(float)fw;
621 if (d > 1.2f) d -= 0.2f;
622 next = prev/d;
623 if (next >= prev) next = prev-1;
624 do {
625 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
626 next, current_line->net_length, WB_LEFT);
627 current_line->net_length = prev;
628 EDIT_InvalidateUniscribeData_linedef(current_line);
629 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
630 if (current_line->ssa)
631 sz = ScriptString_pSize(current_line->ssa);
632 else sz = 0;
633 if (sz)
634 current_line->width = sz->cx;
635 else
636 prev = 0;
637 next = prev - 1;
638 } while (prev && current_line->width > fw);
639 current_line->net_length = w;
641 if (prev == 0) { /* Didn't find a line break so force a break */
642 INT *piDx;
643 const INT *count;
645 EDIT_InvalidateUniscribeData_linedef(current_line);
646 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
648 if (current_line->ssa)
650 count = ScriptString_pcOutChars(current_line->ssa);
651 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
652 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
654 prev = current_line->net_length-1;
655 do {
656 current_line->width -= piDx[prev];
657 prev--;
658 } while ( prev > 0 && current_line->width > fw);
659 if (prev<=0)
660 prev = 1;
661 HeapFree(GetProcessHeap(),0,piDx);
663 else
664 prev = (fw / es->char_width);
667 /* If the first line we are calculating, wrapped before istart, we must
668 * adjust istart in order for this to be reflected in the update region. */
669 if (current_line->index == nstart_index && istart > current_line->index + prev)
670 istart = current_line->index + prev;
671 /* else if we are updating the previous line before the first line we
672 * are re-calculating and it expanded */
673 else if (current_line == start_line &&
674 current_line->index != nstart_index && orig_net_length < prev)
676 /* Line expanded due to an upwards line wrap so we must partially include
677 * previous line in update region */
678 nstart_line = line_index;
679 nstart_index = current_line->index;
680 istart = current_line->index + orig_net_length;
683 current_line->net_length = prev;
684 current_line->ending = END_WRAP;
686 if (current_line->net_length > 0)
688 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
689 if (current_line->ssa)
691 sz = ScriptString_pSize(current_line->ssa);
692 current_line->width = sz->cx;
694 else
695 current_line->width = 0;
697 else current_line->width = 0;
699 else if (current_line == start_line &&
700 current_line->index != nstart_index &&
701 orig_net_length < current_line->net_length) {
702 /* The previous line expanded but it's still not as wide as the client rect */
703 /* The expansion is due to an upwards line wrap so we must partially include
704 it in the update region */
705 nstart_line = line_index;
706 nstart_index = current_line->index;
707 istart = current_line->index + orig_net_length;
712 /* Adjust length to include line termination */
713 switch (current_line->ending) {
714 case END_SOFT:
715 current_line->length = current_line->net_length + 3;
716 break;
717 case END_RICH:
718 current_line->length = current_line->net_length + 1;
719 break;
720 case END_HARD:
721 current_line->length = current_line->net_length + 2;
722 break;
723 case END_WRAP:
724 case END_0:
725 current_line->length = current_line->net_length;
726 break;
728 es->text_width = max(es->text_width, current_line->width);
729 current_position += current_line->length;
730 previous_line = current_line;
732 /* Discard data for non-visible lines. It will be calculated as needed */
733 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
734 EDIT_InvalidateUniscribeData_linedef(current_line);
736 current_line = current_line->next;
737 line_index++;
738 } while (previous_line->ending != END_0);
740 /* Finish adjusting line indexes by delta or remove hanging lines */
741 if (previous_line->ending == END_0)
743 LINEDEF *pnext = NULL;
745 previous_line->next = NULL;
746 while (current_line)
748 pnext = current_line->next;
749 EDIT_InvalidateUniscribeData_linedef(current_line);
750 HeapFree(GetProcessHeap(), 0, current_line);
751 current_line = pnext;
752 es->line_count--;
755 else if (delta != 0)
757 while (current_line)
759 current_line->index += delta;
760 current_line = current_line->next;
764 /* Calculate rest of modification rectangle */
765 if (hrgn)
767 HRGN tmphrgn;
769 * We calculate two rectangles. One for the first line which may have
770 * an indent with respect to the format rect. The other is a format-width
771 * rectangle that spans the rest of the lines that changed or moved.
773 rc.top = es->format_rect.top + nstart_line * es->line_height -
774 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
775 rc.bottom = rc.top + es->line_height;
776 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
777 rc.left = es->format_rect.left;
778 else
779 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
780 rc.right = es->format_rect.right;
781 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
783 rc.top = rc.bottom;
784 rc.left = es->format_rect.left;
785 rc.right = es->format_rect.right;
787 * If lines were added or removed we must re-paint the remainder of the
788 * lines since the remaining lines were either shifted up or down.
790 if (line_count < es->line_count) /* We added lines */
791 rc.bottom = es->line_count * es->line_height;
792 else if (line_count > es->line_count) /* We removed lines */
793 rc.bottom = line_count * es->line_height;
794 else
795 rc.bottom = line_index * es->line_height;
796 rc.bottom += es->format_rect.top;
797 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
798 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
799 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
800 DeleteObject(tmphrgn);
804 /*********************************************************************
806 * EDIT_CalcLineWidth_SL
809 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
811 EDIT_UpdateUniscribeData(es, NULL, 0);
812 if (es->ssa)
814 const SIZE *size;
815 size = ScriptString_pSize(es->ssa);
816 es->text_width = size->cx;
818 else
819 es->text_width = 0;
822 /*********************************************************************
824 * EDIT_CharFromPos
826 * Beware: This is not the function called on EM_CHARFROMPOS
827 * The position _can_ be outside the formatting / client
828 * rectangle
829 * The return value is only the character index
832 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
834 INT index;
836 if (es->style & ES_MULTILINE) {
837 int trailing;
838 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
839 INT line_index = 0;
840 LINEDEF *line_def = es->first_line_def;
841 EDIT_UpdateUniscribeData(es, NULL, line);
842 while ((line > 0) && line_def->next) {
843 line_index += line_def->length;
844 line_def = line_def->next;
845 line--;
848 x += es->x_offset - es->format_rect.left;
849 if (es->style & ES_RIGHT)
850 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
851 else if (es->style & ES_CENTER)
852 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
853 if (x >= line_def->width) {
854 if (after_wrap)
855 *after_wrap = (line_def->ending == END_WRAP);
856 return line_index + line_def->net_length;
858 if (x <= 0 || !line_def->ssa) {
859 if (after_wrap)
860 *after_wrap = FALSE;
861 return line_index;
864 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
865 if (trailing) index++;
866 index += line_index;
867 if (after_wrap)
868 *after_wrap = ((index == line_index + line_def->net_length) &&
869 (line_def->ending == END_WRAP));
870 } else {
871 INT xoff = 0;
872 INT trailing;
873 if (after_wrap)
874 *after_wrap = FALSE;
875 x -= es->format_rect.left;
876 if (!x)
877 return es->x_offset;
879 if (!es->x_offset)
881 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
882 if (es->style & ES_RIGHT)
883 x -= indent;
884 else if (es->style & ES_CENTER)
885 x -= indent / 2;
888 EDIT_UpdateUniscribeData(es, NULL, 0);
889 if (es->x_offset)
891 if (es->ssa)
893 if (es->x_offset>= get_text_length(es))
895 const SIZE *size;
896 size = ScriptString_pSize(es->ssa);
897 xoff = size->cx;
899 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
901 else
902 xoff = 0;
904 if (x < 0)
906 if (x + xoff > 0 || !es->ssa)
908 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
909 if (trailing) index++;
911 else
912 index = 0;
914 else
916 if (x)
918 const SIZE *size = NULL;
919 if (es->ssa)
920 size = ScriptString_pSize(es->ssa);
921 if (!size)
922 index = 0;
923 else if (x > size->cx)
924 index = get_text_length(es);
925 else if (es->ssa)
927 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
928 if (trailing) index++;
930 else
931 index = 0;
933 else
934 index = es->x_offset;
937 return index;
941 /*********************************************************************
943 * EDIT_ConfinePoint
945 * adjusts the point to be within the formatting rectangle
946 * (so CharFromPos returns the nearest _visible_ character)
949 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
951 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
952 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
956 /*********************************************************************
958 * EM_LINEFROMCHAR
961 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
963 INT line;
964 LINEDEF *line_def;
966 if (!(es->style & ES_MULTILINE))
967 return 0;
968 if (index > (INT)get_text_length(es))
969 return es->line_count - 1;
970 if (index == -1)
971 index = min(es->selection_start, es->selection_end);
973 line = 0;
974 line_def = es->first_line_def;
975 index -= line_def->length;
976 while ((index >= 0) && line_def->next) {
977 line++;
978 line_def = line_def->next;
979 index -= line_def->length;
981 return line;
985 /*********************************************************************
987 * EM_LINEINDEX
990 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
992 INT line_index;
993 const LINEDEF *line_def;
995 if (!(es->style & ES_MULTILINE))
996 return 0;
997 if (line >= es->line_count)
998 return -1;
1000 line_index = 0;
1001 line_def = es->first_line_def;
1002 if (line == -1) {
1003 INT index = es->selection_end - line_def->length;
1004 while ((index >= 0) && line_def->next) {
1005 line_index += line_def->length;
1006 line_def = line_def->next;
1007 index -= line_def->length;
1009 } else {
1010 while (line > 0) {
1011 line_index += line_def->length;
1012 line_def = line_def->next;
1013 line--;
1016 return line_index;
1020 /*********************************************************************
1022 * EM_LINELENGTH
1025 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1027 LINEDEF *line_def;
1029 if (!(es->style & ES_MULTILINE))
1030 return get_text_length(es);
1032 if (index == -1) {
1033 /* get the number of remaining non-selected chars of selected lines */
1034 INT32 l; /* line number */
1035 INT32 li; /* index of first char in line */
1036 INT32 count;
1037 l = EDIT_EM_LineFromChar(es, es->selection_start);
1038 /* # chars before start of selection area */
1039 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1040 l = EDIT_EM_LineFromChar(es, es->selection_end);
1041 /* # chars after end of selection */
1042 li = EDIT_EM_LineIndex(es, l);
1043 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1044 return count;
1046 line_def = es->first_line_def;
1047 index -= line_def->length;
1048 while ((index >= 0) && line_def->next) {
1049 line_def = line_def->next;
1050 index -= line_def->length;
1052 return line_def->net_length;
1056 /*********************************************************************
1058 * EM_POSFROMCHAR
1061 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1063 INT len = get_text_length(es);
1064 INT l;
1065 INT li;
1066 INT x = 0;
1067 INT y = 0;
1068 INT w;
1069 INT lw;
1070 LINEDEF *line_def;
1072 index = min(index, len);
1073 if (es->style & ES_MULTILINE) {
1074 l = EDIT_EM_LineFromChar(es, index);
1075 EDIT_UpdateUniscribeData(es, NULL, l);
1077 y = (l - es->y_offset) * es->line_height;
1078 li = EDIT_EM_LineIndex(es, l);
1079 if (after_wrap && (li == index) && l) {
1080 INT l2 = l - 1;
1081 line_def = es->first_line_def;
1082 while (l2) {
1083 line_def = line_def->next;
1084 l2--;
1086 if (line_def->ending == END_WRAP) {
1087 l--;
1088 y -= es->line_height;
1089 li = EDIT_EM_LineIndex(es, l);
1093 line_def = es->first_line_def;
1094 while (line_def->index != li)
1095 line_def = line_def->next;
1097 lw = line_def->width;
1098 w = es->format_rect.right - es->format_rect.left;
1099 if (line_def->ssa)
1100 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1101 x -= es->x_offset;
1103 if (es->style & ES_RIGHT)
1104 x = w - (lw - x);
1105 else if (es->style & ES_CENTER)
1106 x += (w - lw) / 2;
1107 } else {
1108 INT xoff = 0;
1109 INT xi = 0;
1110 EDIT_UpdateUniscribeData(es, NULL, 0);
1111 if (es->x_offset)
1113 if (es->ssa)
1115 if (es->x_offset >= get_text_length(es))
1117 int leftover = es->x_offset - get_text_length(es);
1118 if (es->ssa)
1120 const SIZE *size;
1121 size = ScriptString_pSize(es->ssa);
1122 xoff = size->cx;
1124 else
1125 xoff = 0;
1126 xoff += es->char_width * leftover;
1128 else
1129 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1131 else
1132 xoff = 0;
1134 if (index)
1136 if (index >= get_text_length(es))
1138 if (es->ssa)
1140 const SIZE *size;
1141 size = ScriptString_pSize(es->ssa);
1142 xi = size->cx;
1144 else
1145 xi = 0;
1147 else if (es->ssa)
1148 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1149 else
1150 xi = 0;
1152 x = xi - xoff;
1154 if (index >= es->x_offset) {
1155 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1157 w = es->format_rect.right - es->format_rect.left;
1158 if (w > es->text_width)
1160 if (es->style & ES_RIGHT)
1161 x += w - es->text_width;
1162 else if (es->style & ES_CENTER)
1163 x += (w - es->text_width) / 2;
1167 y = 0;
1169 x += es->format_rect.left;
1170 y += es->format_rect.top;
1171 return MAKELONG((INT16)x, (INT16)y);
1175 /*********************************************************************
1177 * EDIT_GetLineRect
1179 * Calculates the bounding rectangle for a line from a starting
1180 * column to an ending column.
1183 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1185 SCRIPT_STRING_ANALYSIS ssa;
1186 INT line_index = 0;
1187 INT pt1, pt2, pt3;
1189 if (es->style & ES_MULTILINE)
1191 const LINEDEF *line_def = NULL;
1192 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1193 if (line >= es->line_count)
1194 return;
1196 line_def = es->first_line_def;
1197 if (line == -1) {
1198 INT index = es->selection_end - line_def->length;
1199 while ((index >= 0) && line_def->next) {
1200 line_index += line_def->length;
1201 line_def = line_def->next;
1202 index -= line_def->length;
1204 } else {
1205 while (line > 0) {
1206 line_index += line_def->length;
1207 line_def = line_def->next;
1208 line--;
1211 ssa = line_def->ssa;
1213 else
1215 line_index = 0;
1216 rc->top = es->format_rect.top;
1217 ssa = es->ssa;
1220 rc->bottom = rc->top + es->line_height;
1221 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1222 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1223 if (ssa)
1225 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1226 pt3+=es->format_rect.left;
1228 else pt3 = pt1;
1229 rc->right = max(max(pt1 , pt2),pt3);
1230 rc->left = min(min(pt1, pt2),pt3);
1234 static inline void text_buffer_changed(EDITSTATE *es)
1236 es->text_length = (UINT)-1;
1238 HeapFree( GetProcessHeap(), 0, es->logAttr );
1239 es->logAttr = NULL;
1240 EDIT_InvalidateUniscribeData(es);
1243 /*********************************************************************
1244 * EDIT_LockBuffer
1247 static void EDIT_LockBuffer(EDITSTATE *es)
1249 if (!es->text) {
1251 if(!es->hloc32W) return;
1253 if(es->hloc32A)
1255 CHAR *textA = LocalLock(es->hloc32A);
1256 HLOCAL hloc32W_new;
1257 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1258 if(countW_new > es->buffer_size + 1)
1260 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1261 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1262 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1263 if(hloc32W_new)
1265 es->hloc32W = hloc32W_new;
1266 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1267 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1269 else
1270 WARN("FAILED! Will synchronize partially\n");
1272 es->text = LocalLock(es->hloc32W);
1273 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1274 LocalUnlock(es->hloc32A);
1276 else es->text = LocalLock(es->hloc32W);
1278 es->lock_count++;
1282 /*********************************************************************
1284 * EDIT_UnlockBuffer
1287 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1289 /* Edit window might be already destroyed */
1290 if(!IsWindow(es->hwndSelf))
1292 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1293 return;
1296 if (!es->lock_count) {
1297 ERR("lock_count == 0 ... please report\n");
1298 return;
1300 if (!es->text) {
1301 ERR("es->text == 0 ... please report\n");
1302 return;
1304 if (force || (es->lock_count == 1)) {
1305 if (es->hloc32W) {
1306 UINT countA = 0;
1307 UINT countW = get_text_length(es) + 1;
1309 if(es->hloc32A)
1311 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1312 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1313 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1314 countA = LocalSize(es->hloc32A);
1315 if(countA_new > countA)
1317 HLOCAL hloc32A_new;
1318 UINT alloc_size = ROUND_TO_GROW(countA_new);
1319 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1320 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1321 if(hloc32A_new)
1323 es->hloc32A = hloc32A_new;
1324 countA = LocalSize(hloc32A_new);
1325 TRACE("Real new size %d bytes\n", countA);
1327 else
1328 WARN("FAILED! Will synchronize partially\n");
1330 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1331 LocalLock(es->hloc32A), countA, NULL, NULL);
1332 LocalUnlock(es->hloc32A);
1335 LocalUnlock(es->hloc32W);
1336 es->text = NULL;
1338 else {
1339 ERR("no buffer ... please report\n");
1340 return;
1343 es->lock_count--;
1347 /*********************************************************************
1349 * EDIT_MakeFit
1351 * Try to fit size + 1 characters in the buffer.
1353 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1355 HLOCAL hNew32W;
1357 if (size <= es->buffer_size)
1358 return TRUE;
1360 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1362 /* Force edit to unlock its buffer. es->text now NULL */
1363 EDIT_UnlockBuffer(es, TRUE);
1365 if (es->hloc32W) {
1366 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1367 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1368 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1369 es->hloc32W = hNew32W;
1370 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1374 EDIT_LockBuffer(es);
1376 if (es->buffer_size < size) {
1377 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1378 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1379 return FALSE;
1380 } else {
1381 TRACE("We now have %d+1\n", es->buffer_size);
1382 return TRUE;
1387 /*********************************************************************
1389 * EDIT_MakeUndoFit
1391 * Try to fit size + 1 bytes in the undo buffer.
1394 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1396 UINT alloc_size;
1398 if (size <= es->undo_buffer_size)
1399 return TRUE;
1401 TRACE("trying to ReAlloc to %d+1\n", size);
1403 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1404 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1405 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1406 return TRUE;
1408 else
1410 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1411 return FALSE;
1416 /*********************************************************************
1418 * EDIT_UpdateTextRegion
1421 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1423 if (es->flags & EF_UPDATE) {
1424 es->flags &= ~EF_UPDATE;
1425 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1427 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1431 /*********************************************************************
1433 * EDIT_UpdateText
1436 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1438 if (es->flags & EF_UPDATE) {
1439 es->flags &= ~EF_UPDATE;
1440 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1442 InvalidateRect(es->hwndSelf, rc, bErase);
1445 /*********************************************************************
1447 * EDIT_SL_InvalidateText
1449 * Called from EDIT_InvalidateText().
1450 * Does the job for single-line controls only.
1453 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1455 RECT line_rect;
1456 RECT rc;
1458 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1459 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1460 EDIT_UpdateText(es, &rc, TRUE);
1463 /*********************************************************************
1465 * EDIT_ML_InvalidateText
1467 * Called from EDIT_InvalidateText().
1468 * Does the job for multi-line controls only.
1471 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1473 INT vlc = get_vertical_line_count(es);
1474 INT sl = EDIT_EM_LineFromChar(es, start);
1475 INT el = EDIT_EM_LineFromChar(es, end);
1476 INT sc;
1477 INT ec;
1478 RECT rc1;
1479 RECT rcWnd;
1480 RECT rcLine;
1481 RECT rcUpdate;
1482 INT l;
1484 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1485 return;
1487 sc = start - EDIT_EM_LineIndex(es, sl);
1488 ec = end - EDIT_EM_LineIndex(es, el);
1489 if (sl < es->y_offset) {
1490 sl = es->y_offset;
1491 sc = 0;
1493 if (el > es->y_offset + vlc) {
1494 el = es->y_offset + vlc;
1495 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1497 GetClientRect(es->hwndSelf, &rc1);
1498 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1499 if (sl == el) {
1500 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1501 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1502 EDIT_UpdateText(es, &rcUpdate, TRUE);
1503 } else {
1504 EDIT_GetLineRect(es, sl, sc,
1505 EDIT_EM_LineLength(es,
1506 EDIT_EM_LineIndex(es, sl)),
1507 &rcLine);
1508 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1509 EDIT_UpdateText(es, &rcUpdate, TRUE);
1510 for (l = sl + 1 ; l < el ; l++) {
1511 EDIT_GetLineRect(es, l, 0,
1512 EDIT_EM_LineLength(es,
1513 EDIT_EM_LineIndex(es, l)),
1514 &rcLine);
1515 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1516 EDIT_UpdateText(es, &rcUpdate, TRUE);
1518 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1519 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1520 EDIT_UpdateText(es, &rcUpdate, TRUE);
1525 /*********************************************************************
1527 * EDIT_InvalidateText
1529 * Invalidate the text from offset start up to, but not including,
1530 * offset end. Useful for (re)painting the selection.
1531 * Regions outside the linewidth are not invalidated.
1532 * end == -1 means end == TextLength.
1533 * start and end need not be ordered.
1536 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1538 if (end == start)
1539 return;
1541 if (end == -1)
1542 end = get_text_length(es);
1544 if (end < start) {
1545 INT tmp = start;
1546 start = end;
1547 end = tmp;
1550 if (es->style & ES_MULTILINE)
1551 EDIT_ML_InvalidateText(es, start, end);
1552 else
1553 EDIT_SL_InvalidateText(es, start, end);
1557 /*********************************************************************
1559 * EDIT_EM_SetSel
1561 * note: unlike the specs say: the order of start and end
1562 * _is_ preserved in Windows. (i.e. start can be > end)
1563 * In other words: this handler is OK
1566 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1568 UINT old_start = es->selection_start;
1569 UINT old_end = es->selection_end;
1570 UINT len = get_text_length(es);
1572 if (start == (UINT)-1) {
1573 start = es->selection_end;
1574 end = es->selection_end;
1575 } else {
1576 start = min(start, len);
1577 end = min(end, len);
1579 es->selection_start = start;
1580 es->selection_end = end;
1581 if (after_wrap)
1582 es->flags |= EF_AFTER_WRAP;
1583 else
1584 es->flags &= ~EF_AFTER_WRAP;
1585 /* Compute the necessary invalidation region. */
1586 /* Note that we don't need to invalidate regions which have
1587 * "never" been selected, or those which are "still" selected.
1588 * In fact, every time we hit a selection boundary, we can
1589 * *toggle* whether we need to invalidate. Thus we can optimize by
1590 * *sorting* the interval endpoints. Let's assume that we sort them
1591 * in this order:
1592 * start <= end <= old_start <= old_end
1593 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1594 * in 5 comparisons; i.e. it is impossible to do better than the
1595 * following: */
1596 ORDER_UINT(end, old_end);
1597 ORDER_UINT(start, old_start);
1598 ORDER_UINT(old_start, old_end);
1599 ORDER_UINT(start, end);
1600 /* Note that at this point 'end' and 'old_start' are not in order, but
1601 * start is definitely the min. and old_end is definitely the max. */
1602 if (end != old_start)
1605 * One can also do
1606 * ORDER_UINT32(end, old_start);
1607 * EDIT_InvalidateText(es, start, end);
1608 * EDIT_InvalidateText(es, old_start, old_end);
1609 * in place of the following if statement.
1610 * (That would complete the optimal five-comparison four-element sort.)
1612 if (old_start > end )
1614 EDIT_InvalidateText(es, start, end);
1615 EDIT_InvalidateText(es, old_start, old_end);
1617 else
1619 EDIT_InvalidateText(es, start, old_start);
1620 EDIT_InvalidateText(es, end, old_end);
1623 else EDIT_InvalidateText(es, start, old_end);
1627 /*********************************************************************
1629 * EDIT_UpdateScrollInfo
1632 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1634 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1636 SCROLLINFO si;
1637 si.cbSize = sizeof(SCROLLINFO);
1638 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1639 si.nMin = 0;
1640 si.nMax = es->line_count - 1;
1641 si.nPage = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
1642 si.nPos = es->y_offset;
1643 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1644 si.nMin, si.nMax, si.nPage, si.nPos);
1645 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1648 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1650 SCROLLINFO si;
1651 si.cbSize = sizeof(SCROLLINFO);
1652 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1653 si.nMin = 0;
1654 si.nMax = es->text_width - 1;
1655 si.nPage = es->format_rect.right - es->format_rect.left;
1656 si.nPos = es->x_offset;
1657 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1658 si.nMin, si.nMax, si.nPage, si.nPos);
1659 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1664 /*********************************************************************
1666 * EDIT_EM_LineScroll_internal
1668 * Version of EDIT_EM_LineScroll for internal use.
1669 * It doesn't refuse if ES_MULTILINE is set and assumes that
1670 * dx is in pixels, dy - in lines.
1673 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1675 INT nyoff;
1676 INT x_offset_in_pixels;
1677 INT lines_per_page;
1679 if (!es->line_height || !es->char_width)
1680 return TRUE;
1682 lines_per_page = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1684 if (es->style & ES_MULTILINE)
1686 x_offset_in_pixels = es->x_offset;
1688 else
1690 dy = 0;
1691 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1694 if (-dx > x_offset_in_pixels)
1695 dx = -x_offset_in_pixels;
1696 if (dx > es->text_width - x_offset_in_pixels)
1697 dx = es->text_width - x_offset_in_pixels;
1698 nyoff = max(0, es->y_offset + dy);
1699 if (nyoff >= es->line_count - lines_per_page)
1700 nyoff = max(0, es->line_count - lines_per_page);
1701 dy = (es->y_offset - nyoff) * es->line_height;
1702 if (dx || dy) {
1703 RECT rc1;
1704 RECT rc;
1706 es->y_offset = nyoff;
1707 if(es->style & ES_MULTILINE)
1708 es->x_offset += dx;
1709 else
1710 es->x_offset += dx / es->char_width;
1712 GetClientRect(es->hwndSelf, &rc1);
1713 IntersectRect(&rc, &rc1, &es->format_rect);
1714 ScrollWindowEx(es->hwndSelf, -dx, dy,
1715 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1716 /* force scroll info update */
1717 EDIT_UpdateScrollInfo(es);
1719 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1720 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1721 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1722 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1723 return TRUE;
1726 /*********************************************************************
1728 * EM_LINESCROLL
1730 * NOTE: dx is in average character widths, dy - in lines;
1733 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1735 if (!(es->style & ES_MULTILINE))
1736 return FALSE;
1738 dx *= es->char_width;
1739 return EDIT_EM_LineScroll_internal(es, dx, dy);
1743 /*********************************************************************
1745 * EM_SCROLL
1748 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1750 INT dy;
1752 if (!(es->style & ES_MULTILINE))
1753 return (LRESULT)FALSE;
1755 dy = 0;
1757 switch (action) {
1758 case SB_LINEUP:
1759 if (es->y_offset)
1760 dy = -1;
1761 break;
1762 case SB_LINEDOWN:
1763 if (es->y_offset < es->line_count - 1)
1764 dy = 1;
1765 break;
1766 case SB_PAGEUP:
1767 if (es->y_offset)
1768 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1769 break;
1770 case SB_PAGEDOWN:
1771 if (es->y_offset < es->line_count - 1)
1772 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1773 break;
1774 default:
1775 return (LRESULT)FALSE;
1777 if (dy) {
1778 INT vlc = get_vertical_line_count(es);
1779 /* check if we are going to move too far */
1780 if(es->y_offset + dy > es->line_count - vlc)
1781 dy = max(es->line_count - vlc, 0) - es->y_offset;
1783 /* Notification is done in EDIT_EM_LineScroll */
1784 if(dy) {
1785 EDIT_EM_LineScroll(es, 0, dy);
1786 return MAKELONG(dy, TRUE);
1790 return (LRESULT)FALSE;
1794 /*********************************************************************
1796 * EDIT_SetCaretPos
1799 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1800 BOOL after_wrap)
1802 LRESULT res;
1804 if (es->flags & EF_FOCUSED)
1806 res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1807 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1808 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1813 /*********************************************************************
1815 * EM_SCROLLCARET
1818 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1820 if (es->style & ES_MULTILINE) {
1821 INT l;
1822 INT vlc;
1823 INT ww;
1824 INT cw = es->char_width;
1825 INT x;
1826 INT dy = 0;
1827 INT dx = 0;
1829 l = EDIT_EM_LineFromChar(es, es->selection_end);
1830 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1831 vlc = get_vertical_line_count(es);
1832 if (l >= es->y_offset + vlc)
1833 dy = l - vlc + 1 - es->y_offset;
1834 if (l < es->y_offset)
1835 dy = l - es->y_offset;
1836 ww = es->format_rect.right - es->format_rect.left;
1837 if (x < es->format_rect.left)
1838 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1839 if (x > es->format_rect.right)
1840 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1841 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1843 /* check if we are going to move too far */
1844 if(es->x_offset + dx + ww > es->text_width)
1845 dx = es->text_width - ww - es->x_offset;
1846 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1847 EDIT_EM_LineScroll_internal(es, dx, dy);
1849 } else {
1850 INT x;
1851 INT goal;
1852 INT format_width;
1854 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1855 format_width = es->format_rect.right - es->format_rect.left;
1856 if (x < es->format_rect.left) {
1857 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1858 do {
1859 es->x_offset--;
1860 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1861 } while ((x < goal) && es->x_offset);
1862 /* FIXME: use ScrollWindow() somehow to improve performance */
1863 EDIT_UpdateText(es, NULL, TRUE);
1864 } else if (x > es->format_rect.right) {
1865 INT x_last;
1866 INT len = get_text_length(es);
1867 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1868 do {
1869 es->x_offset++;
1870 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1871 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1872 } while ((x > goal) && (x_last > es->format_rect.right));
1873 /* FIXME: use ScrollWindow() somehow to improve performance */
1874 EDIT_UpdateText(es, NULL, TRUE);
1878 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1882 /*********************************************************************
1884 * EDIT_MoveBackward
1887 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1889 INT e = es->selection_end;
1891 if (e) {
1892 e--;
1893 if ((es->style & ES_MULTILINE) && e &&
1894 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1895 e--;
1896 if (e && (es->text[e - 1] == '\r'))
1897 e--;
1900 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1901 EDIT_EM_ScrollCaret(es);
1905 /*********************************************************************
1907 * EDIT_MoveDown_ML
1909 * Only for multi line controls
1910 * Move the caret one line down, on a column with the nearest
1911 * x coordinate on the screen (might be a different column).
1914 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1916 INT s = es->selection_start;
1917 INT e = es->selection_end;
1918 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1919 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1920 INT x = (short)LOWORD(pos);
1921 INT y = (short)HIWORD(pos);
1923 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1924 if (!extend)
1925 s = e;
1926 EDIT_EM_SetSel(es, s, e, after_wrap);
1927 EDIT_EM_ScrollCaret(es);
1931 /*********************************************************************
1933 * EDIT_MoveEnd
1936 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1938 BOOL after_wrap = FALSE;
1939 INT e;
1941 /* Pass a high value in x to make sure of receiving the end of the line */
1942 if (!ctrl && (es->style & ES_MULTILINE))
1943 e = EDIT_CharFromPos(es, 0x3fffffff,
1944 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1945 else
1946 e = get_text_length(es);
1947 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1948 EDIT_EM_ScrollCaret(es);
1952 /*********************************************************************
1954 * EDIT_MoveForward
1957 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1959 INT e = es->selection_end;
1961 if (es->text[e]) {
1962 e++;
1963 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1964 if (es->text[e] == '\n')
1965 e++;
1966 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1967 e += 2;
1970 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1971 EDIT_EM_ScrollCaret(es);
1975 /*********************************************************************
1977 * EDIT_MoveHome
1979 * Home key: move to beginning of line.
1982 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1984 INT e;
1986 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1987 if (!ctrl && (es->style & ES_MULTILINE))
1988 e = EDIT_CharFromPos(es, -es->x_offset,
1989 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1990 else
1991 e = 0;
1992 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1993 EDIT_EM_ScrollCaret(es);
1997 /*********************************************************************
1999 * EDIT_MovePageDown_ML
2001 * Only for multi line controls
2002 * Move the caret one page down, on a column with the nearest
2003 * x coordinate on the screen (might be a different column).
2006 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2008 INT s = es->selection_start;
2009 INT e = es->selection_end;
2010 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2011 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2012 INT x = (short)LOWORD(pos);
2013 INT y = (short)HIWORD(pos);
2015 e = EDIT_CharFromPos(es, x,
2016 y + (es->format_rect.bottom - es->format_rect.top),
2017 &after_wrap);
2018 if (!extend)
2019 s = e;
2020 EDIT_EM_SetSel(es, s, e, after_wrap);
2021 EDIT_EM_ScrollCaret(es);
2025 /*********************************************************************
2027 * EDIT_MovePageUp_ML
2029 * Only for multi line controls
2030 * Move the caret one page up, on a column with the nearest
2031 * x coordinate on the screen (might be a different column).
2034 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2036 INT s = es->selection_start;
2037 INT e = es->selection_end;
2038 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2039 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2040 INT x = (short)LOWORD(pos);
2041 INT y = (short)HIWORD(pos);
2043 e = EDIT_CharFromPos(es, x,
2044 y - (es->format_rect.bottom - es->format_rect.top),
2045 &after_wrap);
2046 if (!extend)
2047 s = e;
2048 EDIT_EM_SetSel(es, s, e, after_wrap);
2049 EDIT_EM_ScrollCaret(es);
2053 /*********************************************************************
2055 * EDIT_MoveUp_ML
2057 * Only for multi line controls
2058 * Move the caret one line up, on a column with the nearest
2059 * x coordinate on the screen (might be a different column).
2062 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2064 INT s = es->selection_start;
2065 INT e = es->selection_end;
2066 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2067 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2068 INT x = (short)LOWORD(pos);
2069 INT y = (short)HIWORD(pos);
2071 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2072 if (!extend)
2073 s = e;
2074 EDIT_EM_SetSel(es, s, e, after_wrap);
2075 EDIT_EM_ScrollCaret(es);
2079 /*********************************************************************
2081 * EDIT_MoveWordBackward
2084 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2086 INT s = es->selection_start;
2087 INT e = es->selection_end;
2088 INT l;
2089 INT ll;
2090 INT li;
2092 l = EDIT_EM_LineFromChar(es, e);
2093 ll = EDIT_EM_LineLength(es, e);
2094 li = EDIT_EM_LineIndex(es, l);
2095 if (e - li == 0) {
2096 if (l) {
2097 li = EDIT_EM_LineIndex(es, l - 1);
2098 e = li + EDIT_EM_LineLength(es, li);
2100 } else {
2101 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2103 if (!extend)
2104 s = e;
2105 EDIT_EM_SetSel(es, s, e, FALSE);
2106 EDIT_EM_ScrollCaret(es);
2110 /*********************************************************************
2112 * EDIT_MoveWordForward
2115 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2117 INT s = es->selection_start;
2118 INT e = es->selection_end;
2119 INT l;
2120 INT ll;
2121 INT li;
2123 l = EDIT_EM_LineFromChar(es, e);
2124 ll = EDIT_EM_LineLength(es, e);
2125 li = EDIT_EM_LineIndex(es, l);
2126 if (e - li == ll) {
2127 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2128 e = EDIT_EM_LineIndex(es, l + 1);
2129 } else {
2130 e = li + EDIT_CallWordBreakProc(es,
2131 li, e - li + 1, ll, WB_RIGHT);
2133 if (!extend)
2134 s = e;
2135 EDIT_EM_SetSel(es, s, e, FALSE);
2136 EDIT_EM_ScrollCaret(es);
2140 /*********************************************************************
2142 * EDIT_PaintText
2145 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2147 COLORREF BkColor;
2148 COLORREF TextColor;
2149 LOGFONTW underline_font;
2150 HFONT hUnderline = 0;
2151 HFONT old_font = 0;
2152 INT ret;
2153 INT li;
2154 INT BkMode;
2155 SIZE size;
2157 if (!count)
2158 return 0;
2159 BkMode = GetBkMode(dc);
2160 BkColor = GetBkColor(dc);
2161 TextColor = GetTextColor(dc);
2162 if (rev) {
2163 if (es->composition_len == 0)
2165 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2166 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2167 SetBkMode( dc, OPAQUE);
2169 else
2171 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2172 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2173 underline_font.lfUnderline = TRUE;
2174 hUnderline = CreateFontIndirectW(&underline_font);
2175 old_font = SelectObject(dc,hUnderline);
2178 li = EDIT_EM_LineIndex(es, line);
2179 if (es->style & ES_MULTILINE) {
2180 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2181 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2182 } else {
2183 TextOutW(dc, x, y, es->text + li + col, count);
2184 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2185 ret = size.cx;
2187 if (rev) {
2188 if (es->composition_len == 0)
2190 SetBkColor(dc, BkColor);
2191 SetTextColor(dc, TextColor);
2192 SetBkMode( dc, BkMode);
2194 else
2196 if (old_font)
2197 SelectObject(dc,old_font);
2198 if (hUnderline)
2199 DeleteObject(hUnderline);
2202 return ret;
2206 /*********************************************************************
2208 * EDIT_PaintLine
2211 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2213 INT s = 0;
2214 INT e = 0;
2215 INT li = 0;
2216 INT ll = 0;
2217 INT x;
2218 INT y;
2219 LRESULT pos;
2220 SCRIPT_STRING_ANALYSIS ssa;
2222 if (es->style & ES_MULTILINE) {
2223 INT vlc = get_vertical_line_count(es);
2225 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2226 return;
2227 } else if (line)
2228 return;
2230 TRACE("line=%d\n", line);
2232 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2233 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2234 x = (short)LOWORD(pos);
2235 y = (short)HIWORD(pos);
2237 if (es->style & ES_MULTILINE)
2239 int line_idx = line;
2240 x = -es->x_offset;
2241 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2243 LINEDEF *line_def = es->first_line_def;
2244 int w, lw;
2246 while (line_def && line_idx)
2248 line_def = line_def->next;
2249 line_idx--;
2251 w = es->format_rect.right - es->format_rect.left;
2252 lw = line_def->width;
2254 if (es->style & ES_RIGHT)
2255 x = w - (lw - x);
2256 else if (es->style & ES_CENTER)
2257 x += (w - lw) / 2;
2259 x += es->format_rect.left;
2262 if (rev)
2264 li = EDIT_EM_LineIndex(es, line);
2265 ll = EDIT_EM_LineLength(es, li);
2266 s = min(es->selection_start, es->selection_end);
2267 e = max(es->selection_start, es->selection_end);
2268 s = min(li + ll, max(li, s));
2269 e = min(li + ll, max(li, e));
2272 if (ssa)
2273 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2274 else if (rev && (s != e) &&
2275 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2276 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2277 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2278 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2279 } else
2280 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2284 /*********************************************************************
2286 * EDIT_AdjustFormatRect
2288 * Adjusts the format rectangle for the current font and the
2289 * current client rectangle.
2292 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2294 RECT ClientRect;
2296 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2297 if (es->style & ES_MULTILINE)
2299 INT fw, vlc, max_x_offset, max_y_offset;
2301 vlc = get_vertical_line_count(es);
2302 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2304 /* correct es->x_offset */
2305 fw = es->format_rect.right - es->format_rect.left;
2306 max_x_offset = es->text_width - fw;
2307 if(max_x_offset < 0) max_x_offset = 0;
2308 if(es->x_offset > max_x_offset)
2309 es->x_offset = max_x_offset;
2311 /* correct es->y_offset */
2312 max_y_offset = es->line_count - vlc;
2313 if(max_y_offset < 0) max_y_offset = 0;
2314 if(es->y_offset > max_y_offset)
2315 es->y_offset = max_y_offset;
2317 /* force scroll info update */
2318 EDIT_UpdateScrollInfo(es);
2320 else
2321 /* Windows doesn't care to fix text placement for SL controls */
2322 es->format_rect.bottom = es->format_rect.top + es->line_height;
2324 /* Always stay within the client area */
2325 GetClientRect(es->hwndSelf, &ClientRect);
2326 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2328 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2329 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2331 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2335 /*********************************************************************
2337 * EDIT_SetRectNP
2339 * note: this is not (exactly) the handler called on EM_SETRECTNP
2340 * it is also used to set the rect of a single line control
2343 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2345 LONG_PTR ExStyle;
2346 INT bw, bh;
2347 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2349 CopyRect(&es->format_rect, rc);
2351 if (ExStyle & WS_EX_CLIENTEDGE) {
2352 es->format_rect.left++;
2353 es->format_rect.right--;
2355 if (es->format_rect.bottom - es->format_rect.top
2356 >= es->line_height + 2)
2358 es->format_rect.top++;
2359 es->format_rect.bottom--;
2362 else if (es->style & WS_BORDER) {
2363 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2364 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2365 InflateRect(&es->format_rect, -bw, 0);
2366 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2367 InflateRect(&es->format_rect, 0, -bh);
2370 es->format_rect.left += es->left_margin;
2371 es->format_rect.right -= es->right_margin;
2372 EDIT_AdjustFormatRect(es);
2376 /*********************************************************************
2378 * EM_CHARFROMPOS
2380 * returns line number (not index) in high-order word of result.
2381 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2382 * to Richedit, not to the edit control. Original documentation is valid.
2383 * FIXME: do the specs mean to return -1 if outside client area or
2384 * if outside formatting rectangle ???
2387 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2389 POINT pt;
2390 RECT rc;
2391 INT index;
2393 pt.x = x;
2394 pt.y = y;
2395 GetClientRect(es->hwndSelf, &rc);
2396 if (!PtInRect(&rc, pt))
2397 return -1;
2399 index = EDIT_CharFromPos(es, x, y, NULL);
2400 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2404 /*********************************************************************
2406 * EM_FMTLINES
2408 * Enable or disable soft breaks.
2410 * This means: insert or remove the soft linebreak character (\r\r\n).
2411 * Take care to check if the text still fits the buffer after insertion.
2412 * If not, notify with EN_ERRSPACE.
2415 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2417 es->flags &= ~EF_USE_SOFTBRK;
2418 if (add_eol) {
2419 es->flags |= EF_USE_SOFTBRK;
2420 FIXME("soft break enabled, not implemented\n");
2422 return add_eol;
2426 /*********************************************************************
2428 * EM_GETHANDLE
2430 * Hopefully this won't fire back at us.
2431 * We always start with a fixed buffer in the local heap.
2432 * Despite of the documentation says that the local heap is used
2433 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2434 * buffer on the local heap.
2437 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2439 HLOCAL hLocal;
2441 if (!(es->style & ES_MULTILINE))
2442 return 0;
2444 if(es->is_unicode)
2445 hLocal = es->hloc32W;
2446 else
2448 if(!es->hloc32A)
2450 CHAR *textA;
2451 UINT countA, alloc_size;
2452 TRACE("Allocating 32-bit ANSI alias buffer\n");
2453 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2454 alloc_size = ROUND_TO_GROW(countA);
2455 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2457 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2458 return 0;
2460 textA = LocalLock(es->hloc32A);
2461 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2462 LocalUnlock(es->hloc32A);
2464 hLocal = es->hloc32A;
2467 EDIT_UnlockBuffer(es, TRUE);
2469 /* The text buffer handle belongs to the app */
2470 es->hlocapp = hLocal;
2472 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2473 return hLocal;
2477 /*********************************************************************
2479 * EM_GETLINE
2482 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2484 LPWSTR src;
2485 INT line_len, dst_len;
2486 INT i;
2488 if (es->style & ES_MULTILINE) {
2489 if (line >= es->line_count)
2490 return 0;
2491 } else
2492 line = 0;
2493 i = EDIT_EM_LineIndex(es, line);
2494 src = es->text + i;
2495 line_len = EDIT_EM_LineLength(es, i);
2496 dst_len = *(WORD *)dst;
2497 if(unicode)
2499 if(dst_len <= line_len)
2501 memcpy(dst, src, dst_len * sizeof(WCHAR));
2502 return dst_len;
2504 else /* Append 0 if enough space */
2506 memcpy(dst, src, line_len * sizeof(WCHAR));
2507 dst[line_len] = 0;
2508 return line_len;
2511 else
2513 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2514 if(!ret && line_len) /* Insufficient buffer size */
2515 return dst_len;
2516 if(ret < dst_len) /* Append 0 if enough space */
2517 ((LPSTR)dst)[ret] = 0;
2518 return ret;
2523 /*********************************************************************
2525 * EM_GETSEL
2528 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2530 UINT s = es->selection_start;
2531 UINT e = es->selection_end;
2533 ORDER_UINT(s, e);
2534 if (start)
2535 *start = s;
2536 if (end)
2537 *end = e;
2538 return MAKELONG(s, e);
2542 /*********************************************************************
2544 * EM_REPLACESEL
2546 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2549 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2550 BOOL send_update, BOOL honor_limit)
2552 UINT tl = get_text_length(es);
2553 UINT utl;
2554 UINT s;
2555 UINT e;
2556 UINT i;
2557 UINT size;
2558 LPWSTR p;
2559 HRGN hrgn = 0;
2560 LPWSTR buf = NULL;
2561 UINT bufl;
2563 TRACE("%s, can_undo %d, send_update %d\n",
2564 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2566 s = es->selection_start;
2567 e = es->selection_end;
2569 EDIT_InvalidateUniscribeData(es);
2570 if ((s == e) && !strl)
2571 return;
2573 ORDER_UINT(s, e);
2575 size = tl - (e - s) + strl;
2576 if (!size)
2577 es->text_width = 0;
2579 /* Issue the EN_MAXTEXT notification and continue with replacing text
2580 * so that buffer limit is honored. */
2581 if ((honor_limit) && (size > es->buffer_limit)) {
2582 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2583 /* Buffer limit can be smaller than the actual length of text in combobox */
2584 if (es->buffer_limit < (tl - (e-s)))
2585 strl = 0;
2586 else
2587 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2590 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2591 return;
2593 if (e != s) {
2594 /* there is something to be deleted */
2595 TRACE("deleting stuff.\n");
2596 bufl = e - s;
2597 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2598 if (!buf) return;
2599 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2600 buf[bufl] = 0; /* ensure 0 termination */
2601 /* now delete */
2602 strcpyW(es->text + s, es->text + e);
2603 text_buffer_changed(es);
2605 if (strl) {
2606 /* there is an insertion */
2607 tl = get_text_length(es);
2608 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));
2609 for (p = es->text + tl ; p >= es->text + s ; p--)
2610 p[strl] = p[0];
2611 for (i = 0 , p = es->text + s ; i < strl ; i++)
2612 p[i] = lpsz_replace[i];
2613 if(es->style & ES_UPPERCASE)
2614 CharUpperBuffW(p, strl);
2615 else if(es->style & ES_LOWERCASE)
2616 CharLowerBuffW(p, strl);
2617 text_buffer_changed(es);
2619 if (es->style & ES_MULTILINE)
2621 INT st = min(es->selection_start, es->selection_end);
2622 INT vlc = get_vertical_line_count(es);
2624 hrgn = CreateRectRgn(0, 0, 0, 0);
2625 EDIT_BuildLineDefs_ML(es, st, st + strl,
2626 strl - abs(es->selection_end - es->selection_start), hrgn);
2627 /* if text is too long undo all changes */
2628 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2629 if (strl)
2630 strcpyW(es->text + e, es->text + e + strl);
2631 if (e != s)
2632 for (i = 0 , p = es->text ; i < e - s ; i++)
2633 p[i + s] = buf[i];
2634 text_buffer_changed(es);
2635 EDIT_BuildLineDefs_ML(es, s, e,
2636 abs(es->selection_end - es->selection_start) - strl, hrgn);
2637 strl = 0;
2638 e = s;
2639 hrgn = CreateRectRgn(0, 0, 0, 0);
2640 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2643 else {
2644 INT fw = es->format_rect.right - es->format_rect.left;
2645 EDIT_InvalidateUniscribeData(es);
2646 EDIT_CalcLineWidth_SL(es);
2647 /* remove chars that don't fit */
2648 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2649 while ((es->text_width > fw) && s + strl >= s) {
2650 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2651 strl--;
2652 es->text_length = -1;
2653 EDIT_InvalidateUniscribeData(es);
2654 EDIT_CalcLineWidth_SL(es);
2656 text_buffer_changed(es);
2657 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2661 if (e != s) {
2662 if (can_undo) {
2663 utl = strlenW(es->undo_text);
2664 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2665 /* undo-buffer is extended to the right */
2666 EDIT_MakeUndoFit(es, utl + e - s);
2667 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2668 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2669 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2670 /* undo-buffer is extended to the left */
2671 EDIT_MakeUndoFit(es, utl + e - s);
2672 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2673 p[e - s] = p[0];
2674 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2675 p[i] = buf[i];
2676 es->undo_position = s;
2677 } else {
2678 /* new undo-buffer */
2679 EDIT_MakeUndoFit(es, e - s);
2680 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2681 es->undo_text[e - s] = 0; /* ensure 0 termination */
2682 es->undo_position = s;
2684 /* any deletion makes the old insertion-undo invalid */
2685 es->undo_insert_count = 0;
2686 } else
2687 EDIT_EM_EmptyUndoBuffer(es);
2689 if (strl) {
2690 if (can_undo) {
2691 if ((s == es->undo_position) ||
2692 ((es->undo_insert_count) &&
2693 (s == es->undo_position + es->undo_insert_count)))
2695 * insertion is new and at delete position or
2696 * an extension to either left or right
2698 es->undo_insert_count += strl;
2699 else {
2700 /* new insertion undo */
2701 es->undo_position = s;
2702 es->undo_insert_count = strl;
2703 /* new insertion makes old delete-buffer invalid */
2704 *es->undo_text = '\0';
2706 } else
2707 EDIT_EM_EmptyUndoBuffer(es);
2710 HeapFree(GetProcessHeap(), 0, buf);
2712 s += strl;
2714 /* If text has been deleted and we're right or center aligned then scroll rightward */
2715 if (es->style & (ES_RIGHT | ES_CENTER))
2717 INT delta = strl - abs(es->selection_end - es->selection_start);
2719 if (delta < 0 && es->x_offset)
2721 if (abs(delta) > es->x_offset)
2722 es->x_offset = 0;
2723 else
2724 es->x_offset += delta;
2728 EDIT_EM_SetSel(es, s, s, FALSE);
2729 es->flags |= EF_MODIFIED;
2730 if (send_update) es->flags |= EF_UPDATE;
2731 if (hrgn)
2733 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2734 DeleteObject(hrgn);
2736 else
2737 EDIT_UpdateText(es, NULL, TRUE);
2739 EDIT_EM_ScrollCaret(es);
2741 /* force scroll info update */
2742 EDIT_UpdateScrollInfo(es);
2745 if(send_update || (es->flags & EF_UPDATE))
2747 es->flags &= ~EF_UPDATE;
2748 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2750 EDIT_InvalidateUniscribeData(es);
2754 /*********************************************************************
2756 * EM_SETHANDLE
2758 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2761 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2763 if (!(es->style & ES_MULTILINE))
2764 return;
2766 if (!hloc) {
2767 WARN("called with NULL handle\n");
2768 return;
2771 EDIT_UnlockBuffer(es, TRUE);
2773 if(es->is_unicode)
2775 if(es->hloc32A)
2777 LocalFree(es->hloc32A);
2778 es->hloc32A = NULL;
2780 es->hloc32W = hloc;
2782 else
2784 INT countW, countA;
2785 HLOCAL hloc32W_new;
2786 WCHAR *textW;
2787 CHAR *textA;
2789 countA = LocalSize(hloc);
2790 textA = LocalLock(hloc);
2791 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2792 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2794 ERR("Could not allocate new unicode buffer\n");
2795 return;
2797 textW = LocalLock(hloc32W_new);
2798 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2799 LocalUnlock(hloc32W_new);
2800 LocalUnlock(hloc);
2802 if(es->hloc32W)
2803 LocalFree(es->hloc32W);
2805 es->hloc32W = hloc32W_new;
2806 es->hloc32A = hloc;
2809 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2811 /* The text buffer handle belongs to the control */
2812 es->hlocapp = NULL;
2814 EDIT_LockBuffer(es);
2815 text_buffer_changed(es);
2817 es->x_offset = es->y_offset = 0;
2818 es->selection_start = es->selection_end = 0;
2819 EDIT_EM_EmptyUndoBuffer(es);
2820 es->flags &= ~EF_MODIFIED;
2821 es->flags &= ~EF_UPDATE;
2822 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2823 EDIT_UpdateText(es, NULL, TRUE);
2824 EDIT_EM_ScrollCaret(es);
2825 /* force scroll info update */
2826 EDIT_UpdateScrollInfo(es);
2830 /*********************************************************************
2832 * EM_SETLIMITTEXT
2834 * NOTE: this version currently implements WinNT limits
2837 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2839 if (!limit) limit = ~0u;
2840 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2841 es->buffer_limit = limit;
2844 static BOOL is_cjk_charset(HDC dc)
2846 switch (GdiGetCodePage(dc)) {
2847 case 932: case 936: case 949: case 950: case 1361:
2848 return TRUE;
2849 default:
2850 return FALSE;
2854 static BOOL is_cjk_font(HDC dc)
2856 const DWORD FS_DBCS_MASK = FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB;
2857 FONTSIGNATURE fs;
2858 return (GetTextCharsetInfo(dc, &fs, 0) != DEFAULT_CHARSET &&
2859 (fs.fsCsb[0] & FS_DBCS_MASK));
2862 static int get_cjk_fontinfo_margin(int width, int side_bearing)
2864 int margin;
2865 if (side_bearing < 0)
2866 margin = min(-side_bearing, width/2);
2867 else
2868 margin = 0;
2869 return margin;
2872 struct char_width_info {
2873 INT min_lsb, min_rsb, unknown;
2876 /* Undocumented gdi32 export */
2877 extern BOOL WINAPI GetCharWidthInfo(HDC, struct char_width_info *);
2879 /*********************************************************************
2881 * EM_SETMARGINS
2883 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2884 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2885 * margin according to the textmetrics of the current font.
2887 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2888 * equal to or larger than a certain size. The empty client rect is treated as
2889 * 80 pixels width.
2891 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2892 WORD left, WORD right, BOOL repaint)
2894 TEXTMETRICW tm;
2895 INT default_left_margin = 0; /* in pixels */
2896 INT default_right_margin = 0; /* in pixels */
2898 /* Set the default margins depending on the font */
2899 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2900 HDC dc = GetDC(es->hwndSelf);
2901 HFONT old_font = SelectObject(dc, es->font);
2902 LONG width = GdiGetCharDimensions(dc, &tm, NULL), rc_width;
2903 RECT rc;
2905 /* The default margins are only non zero for TrueType or Vector fonts */
2906 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2907 struct char_width_info width_info;
2909 if ((is_cjk_charset(dc) || is_cjk_font(dc)) &&
2910 GetCharWidthInfo(dc, &width_info))
2912 default_left_margin = get_cjk_fontinfo_margin(width, width_info.min_lsb);
2913 default_right_margin = get_cjk_fontinfo_margin(width, width_info.min_rsb);
2915 else
2917 default_left_margin = width / 2;
2918 default_right_margin = width / 2;
2921 GetClientRect(es->hwndSelf, &rc);
2922 rc_width = !IsRectEmpty(&rc) ? rc.right - rc.left : 80;
2923 if (rc_width < default_left_margin + default_right_margin + width * 2) {
2924 default_left_margin = es->left_margin;
2925 default_right_margin = es->right_margin;
2928 SelectObject(dc, old_font);
2929 ReleaseDC(es->hwndSelf, dc);
2932 if (action & EC_LEFTMARGIN) {
2933 es->format_rect.left -= es->left_margin;
2934 if (left != EC_USEFONTINFO)
2935 es->left_margin = left;
2936 else
2937 es->left_margin = default_left_margin;
2938 es->format_rect.left += es->left_margin;
2941 if (action & EC_RIGHTMARGIN) {
2942 es->format_rect.right += es->right_margin;
2943 if (right != EC_USEFONTINFO)
2944 es->right_margin = right;
2945 else
2946 es->right_margin = default_right_margin;
2947 es->format_rect.right -= es->right_margin;
2950 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2951 EDIT_AdjustFormatRect(es);
2952 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2955 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2959 /*********************************************************************
2961 * EM_SETPASSWORDCHAR
2964 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2966 LONG style;
2968 if (es->style & ES_MULTILINE)
2969 return;
2971 if (es->password_char == c)
2972 return;
2974 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2975 es->password_char = c;
2976 if (c) {
2977 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2978 es->style |= ES_PASSWORD;
2979 } else {
2980 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2981 es->style &= ~ES_PASSWORD;
2983 EDIT_InvalidateUniscribeData(es);
2984 EDIT_UpdateText(es, NULL, TRUE);
2988 /*********************************************************************
2990 * EM_SETTABSTOPS
2993 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2995 if (!(es->style & ES_MULTILINE))
2996 return FALSE;
2997 HeapFree(GetProcessHeap(), 0, es->tabs);
2998 es->tabs_count = count;
2999 if (!count)
3000 es->tabs = NULL;
3001 else {
3002 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3003 memcpy(es->tabs, tabs, count * sizeof(INT));
3005 EDIT_InvalidateUniscribeData(es);
3006 return TRUE;
3010 /*********************************************************************
3012 * EM_SETWORDBREAKPROC
3015 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3017 if (es->word_break_proc == wbp)
3018 return;
3020 es->word_break_proc = wbp;
3022 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3023 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3024 EDIT_UpdateText(es, NULL, TRUE);
3029 /*********************************************************************
3031 * EM_UNDO / WM_UNDO
3034 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3036 INT ulength;
3037 LPWSTR utext;
3039 /* As per MSDN spec, for a single-line edit control,
3040 the return value is always TRUE */
3041 if( es->style & ES_READONLY )
3042 return !(es->style & ES_MULTILINE);
3044 ulength = strlenW(es->undo_text);
3046 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3048 strcpyW(utext, es->undo_text);
3050 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3051 es->undo_insert_count, debugstr_w(utext));
3053 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3054 EDIT_EM_EmptyUndoBuffer(es);
3055 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
3056 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3057 /* send the notification after the selection start and end are set */
3058 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3059 EDIT_EM_ScrollCaret(es);
3060 HeapFree(GetProcessHeap(), 0, utext);
3062 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3063 es->undo_insert_count, debugstr_w(es->undo_text));
3064 return TRUE;
3068 /* Helper function for WM_CHAR
3070 * According to an MSDN blog article titled "Just because you're a control
3071 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3072 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3073 * a dialog box or not.
3075 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3077 return (es->flags & EF_DIALOGMODE);
3081 /*********************************************************************
3083 * WM_PASTE
3086 static void EDIT_WM_Paste(EDITSTATE *es)
3088 HGLOBAL hsrc;
3089 LPWSTR src, ptr;
3090 int len;
3092 /* Protect read-only edit control from modification */
3093 if(es->style & ES_READONLY)
3094 return;
3096 OpenClipboard(es->hwndSelf);
3097 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3098 src = GlobalLock(hsrc);
3099 len = strlenW(src);
3100 /* Protect single-line edit against pasting new line character */
3101 if (!(es->style & ES_MULTILINE) && ((ptr = strchrW(src, '\n')))) {
3102 len = ptr - src;
3103 if (len && src[len - 1] == '\r')
3104 --len;
3106 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3107 GlobalUnlock(hsrc);
3109 else if (es->style & ES_PASSWORD) {
3110 /* clear selected text in password edit box even with empty clipboard */
3111 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3113 CloseClipboard();
3117 /*********************************************************************
3119 * WM_COPY
3122 static void EDIT_WM_Copy(EDITSTATE *es)
3124 INT s = min(es->selection_start, es->selection_end);
3125 INT e = max(es->selection_start, es->selection_end);
3126 HGLOBAL hdst;
3127 LPWSTR dst;
3128 DWORD len;
3130 if (e == s) return;
3132 len = e - s;
3133 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3134 dst = GlobalLock(hdst);
3135 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3136 dst[len] = 0; /* ensure 0 termination */
3137 TRACE("%s\n", debugstr_w(dst));
3138 GlobalUnlock(hdst);
3139 OpenClipboard(es->hwndSelf);
3140 EmptyClipboard();
3141 SetClipboardData(CF_UNICODETEXT, hdst);
3142 CloseClipboard();
3146 /*********************************************************************
3148 * WM_CLEAR
3151 static inline void EDIT_WM_Clear(EDITSTATE *es)
3153 /* Protect read-only edit control from modification */
3154 if(es->style & ES_READONLY)
3155 return;
3157 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3161 /*********************************************************************
3163 * WM_CUT
3166 static inline void EDIT_WM_Cut(EDITSTATE *es)
3168 EDIT_WM_Copy(es);
3169 EDIT_WM_Clear(es);
3173 /*********************************************************************
3175 * WM_CHAR
3178 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3180 BOOL control;
3182 control = GetKeyState(VK_CONTROL) & 0x8000;
3184 switch (c) {
3185 case '\r':
3186 /* If it's not a multiline edit box, it would be ignored below.
3187 * For multiline edit without ES_WANTRETURN, we have to make a
3188 * special case.
3190 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3191 if (EDIT_IsInsideDialog(es))
3192 break;
3193 case '\n':
3194 if (es->style & ES_MULTILINE) {
3195 if (es->style & ES_READONLY) {
3196 EDIT_MoveHome(es, FALSE, FALSE);
3197 EDIT_MoveDown_ML(es, FALSE);
3198 } else {
3199 static const WCHAR cr_lfW[] = {'\r','\n'};
3200 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, 2, TRUE, TRUE);
3203 break;
3204 case '\t':
3205 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3207 static const WCHAR tabW[] = {'\t'};
3208 if (EDIT_IsInsideDialog(es))
3209 break;
3210 EDIT_EM_ReplaceSel(es, TRUE, tabW, 1, TRUE, TRUE);
3212 break;
3213 case VK_BACK:
3214 if (!(es->style & ES_READONLY) && !control) {
3215 if (es->selection_start != es->selection_end)
3216 EDIT_WM_Clear(es);
3217 else {
3218 /* delete character left of caret */
3219 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3220 EDIT_MoveBackward(es, TRUE);
3221 EDIT_WM_Clear(es);
3224 break;
3225 case 0x03: /* ^C */
3226 if (!(es->style & ES_PASSWORD))
3227 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3228 break;
3229 case 0x16: /* ^V */
3230 if (!(es->style & ES_READONLY))
3231 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3232 break;
3233 case 0x18: /* ^X */
3234 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3235 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3236 break;
3237 case 0x1A: /* ^Z */
3238 if (!(es->style & ES_READONLY))
3239 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3240 break;
3242 default:
3243 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3244 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3245 break;
3247 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3248 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3249 break;
3251 return 1;
3255 /*********************************************************************
3257 * EDIT_ContextMenuCommand
3260 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3262 switch (id) {
3263 case EM_UNDO:
3264 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3265 break;
3266 case WM_CUT:
3267 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3268 break;
3269 case WM_COPY:
3270 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3271 break;
3272 case WM_PASTE:
3273 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3274 break;
3275 case WM_CLEAR:
3276 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3277 break;
3278 case EM_SETSEL:
3279 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3280 break;
3281 default:
3282 ERR("unknown menu item, please report\n");
3283 break;
3288 /*********************************************************************
3290 * WM_CONTEXTMENU
3292 * Note: the resource files resource/sysres_??.rc cannot define a
3293 * single popup menu. Hence we use a (dummy) menubar
3294 * containing the single popup menu as its first item.
3296 * FIXME: the message identifiers have been chosen arbitrarily,
3297 * hence we use MF_BYPOSITION.
3298 * We might as well use the "real" values (anybody knows ?)
3299 * The menu definition is in resources/sysres_??.rc.
3300 * Once these are OK, we better use MF_BYCOMMAND here
3301 * (as we do in EDIT_WM_Command()).
3304 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3306 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3307 HMENU popup = GetSubMenu(menu, 0);
3308 UINT start = es->selection_start;
3309 UINT end = es->selection_end;
3310 UINT cmd;
3312 ORDER_UINT(start, end);
3314 /* undo */
3315 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3316 /* cut */
3317 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3318 /* copy */
3319 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3320 /* paste */
3321 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3322 /* delete */
3323 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3324 /* select all */
3325 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3327 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3329 RECT rc;
3330 /* Windows places the menu at the edit's center in this case */
3331 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3332 x = rc.left + (rc.right - rc.left) / 2;
3333 y = rc.top + (rc.bottom - rc.top) / 2;
3336 if (!(es->flags & EF_FOCUSED))
3337 SetFocus(es->hwndSelf);
3339 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3340 x, y, 0, es->hwndSelf, NULL);
3342 if (cmd)
3343 EDIT_ContextMenuCommand(es, cmd);
3345 DestroyMenu(menu);
3349 /*********************************************************************
3351 * WM_GETTEXT
3354 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3356 if(!count) return 0;
3358 if(unicode)
3360 lstrcpynW(dst, es->text, count);
3361 return strlenW(dst);
3363 else
3365 LPSTR textA = (LPSTR)dst;
3366 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3367 textA[count - 1] = 0; /* ensure 0 termination */
3368 return strlen(textA);
3372 /*********************************************************************
3374 * EDIT_CheckCombo
3377 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3379 HWND hLBox = es->hwndListBox;
3380 HWND hCombo;
3381 BOOL bDropped;
3382 int nEUI;
3384 if (!hLBox)
3385 return FALSE;
3387 hCombo = GetParent(es->hwndSelf);
3388 bDropped = TRUE;
3389 nEUI = 0;
3391 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3393 if (key == VK_UP || key == VK_DOWN)
3395 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3396 nEUI = 1;
3398 if (msg == WM_KEYDOWN || nEUI)
3399 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3402 switch (msg)
3404 case WM_KEYDOWN:
3405 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3407 /* make sure ComboLBox pops up */
3408 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3409 key = VK_F4;
3410 nEUI = 2;
3413 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3414 break;
3416 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3417 if (nEUI)
3418 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3419 else
3420 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3421 break;
3424 if(nEUI == 2)
3425 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3427 return TRUE;
3431 /*********************************************************************
3433 * WM_KEYDOWN
3435 * Handling of special keys that don't produce a WM_CHAR
3436 * (i.e. non-printable keys) & Backspace & Delete
3439 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3441 BOOL shift;
3442 BOOL control;
3444 if (GetKeyState(VK_MENU) & 0x8000)
3445 return 0;
3447 shift = GetKeyState(VK_SHIFT) & 0x8000;
3448 control = GetKeyState(VK_CONTROL) & 0x8000;
3450 switch (key) {
3451 case VK_F4:
3452 case VK_UP:
3453 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3454 break;
3456 /* fall through */
3457 case VK_LEFT:
3458 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3459 EDIT_MoveUp_ML(es, shift);
3460 else
3461 if (control)
3462 EDIT_MoveWordBackward(es, shift);
3463 else
3464 EDIT_MoveBackward(es, shift);
3465 break;
3466 case VK_DOWN:
3467 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3468 break;
3469 /* fall through */
3470 case VK_RIGHT:
3471 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3472 EDIT_MoveDown_ML(es, shift);
3473 else if (control)
3474 EDIT_MoveWordForward(es, shift);
3475 else
3476 EDIT_MoveForward(es, shift);
3477 break;
3478 case VK_HOME:
3479 EDIT_MoveHome(es, shift, control);
3480 break;
3481 case VK_END:
3482 EDIT_MoveEnd(es, shift, control);
3483 break;
3484 case VK_PRIOR:
3485 if (es->style & ES_MULTILINE)
3486 EDIT_MovePageUp_ML(es, shift);
3487 else
3488 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3489 break;
3490 case VK_NEXT:
3491 if (es->style & ES_MULTILINE)
3492 EDIT_MovePageDown_ML(es, shift);
3493 else
3494 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3495 break;
3496 case VK_DELETE:
3497 if (!(es->style & ES_READONLY) && !(shift && control)) {
3498 if (es->selection_start != es->selection_end) {
3499 if (shift)
3500 EDIT_WM_Cut(es);
3501 else
3502 EDIT_WM_Clear(es);
3503 } else {
3504 EDIT_EM_SetSel(es, ~0u, 0, FALSE);
3505 if (shift)
3506 /* delete character left of caret */
3507 EDIT_MoveBackward(es, TRUE);
3508 else if (control)
3509 /* delete to end of line */
3510 EDIT_MoveEnd(es, TRUE, FALSE);
3511 else
3512 /* delete character right of caret */
3513 EDIT_MoveForward(es, TRUE);
3514 EDIT_WM_Clear(es);
3517 break;
3518 case VK_INSERT:
3519 if (shift) {
3520 if (!(es->style & ES_READONLY))
3521 EDIT_WM_Paste(es);
3522 } else if (control)
3523 EDIT_WM_Copy(es);
3524 break;
3525 case VK_RETURN:
3526 /* If the edit doesn't want the return send a message to the default object */
3527 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3529 DWORD dw;
3531 if (!EDIT_IsInsideDialog(es)) break;
3532 if (control) break;
3533 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3534 if (HIWORD(dw) == DC_HASDEFID)
3536 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3537 if (hwDefCtrl)
3539 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3540 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3544 break;
3545 case VK_ESCAPE:
3546 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3547 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3548 break;
3549 case VK_TAB:
3550 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3551 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3552 break;
3554 return TRUE;
3558 /*********************************************************************
3560 * WM_KILLFOCUS
3563 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3565 es->flags &= ~EF_FOCUSED;
3566 DestroyCaret();
3567 if(!(es->style & ES_NOHIDESEL))
3568 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3569 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3570 /* throw away left over scroll when we lose focus */
3571 es->wheelDeltaRemainder = 0;
3572 return 0;
3576 /*********************************************************************
3578 * WM_LBUTTONDBLCLK
3580 * The caret position has been set on the WM_LBUTTONDOWN message
3583 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3585 INT s;
3586 INT e = es->selection_end;
3587 INT l;
3588 INT li;
3589 INT ll;
3591 es->bCaptureState = TRUE;
3592 SetCapture(es->hwndSelf);
3594 l = EDIT_EM_LineFromChar(es, e);
3595 li = EDIT_EM_LineIndex(es, l);
3596 ll = EDIT_EM_LineLength(es, e);
3597 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3598 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3599 EDIT_EM_SetSel(es, s, e, FALSE);
3600 EDIT_EM_ScrollCaret(es);
3601 es->region_posx = es->region_posy = 0;
3602 SetTimer(es->hwndSelf, 0, 100, NULL);
3603 return 0;
3607 /*********************************************************************
3609 * WM_LBUTTONDOWN
3612 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3614 INT e;
3615 BOOL after_wrap;
3617 es->bCaptureState = TRUE;
3618 SetCapture(es->hwndSelf);
3619 EDIT_ConfinePoint(es, &x, &y);
3620 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3621 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3622 EDIT_EM_ScrollCaret(es);
3623 es->region_posx = es->region_posy = 0;
3624 SetTimer(es->hwndSelf, 0, 100, NULL);
3626 if (!(es->flags & EF_FOCUSED))
3627 SetFocus(es->hwndSelf);
3629 return 0;
3633 /*********************************************************************
3635 * WM_LBUTTONUP
3638 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3640 if (es->bCaptureState) {
3641 KillTimer(es->hwndSelf, 0);
3642 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3644 es->bCaptureState = FALSE;
3645 return 0;
3649 /*********************************************************************
3651 * WM_MBUTTONDOWN
3654 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3656 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3657 return 0;
3661 /*********************************************************************
3663 * WM_MOUSEMOVE
3666 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3668 INT e;
3669 BOOL after_wrap;
3670 INT prex, prey;
3672 /* If the mouse has been captured by process other than the edit control itself,
3673 * the windows edit controls will not select the strings with mouse move.
3675 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3676 return 0;
3679 * FIXME: gotta do some scrolling if outside client
3680 * area. Maybe reset the timer ?
3682 prex = x; prey = y;
3683 EDIT_ConfinePoint(es, &x, &y);
3684 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3685 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3686 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3687 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3688 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3689 return 0;
3693 /*********************************************************************
3695 * WM_PAINT
3698 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3700 PAINTSTRUCT ps;
3701 INT i;
3702 HDC dc;
3703 HFONT old_font = 0;
3704 RECT rc;
3705 RECT rcClient;
3706 RECT rcLine;
3707 RECT rcRgn;
3708 HBRUSH brush;
3709 HBRUSH old_brush;
3710 INT bw, bh;
3711 BOOL rev = es->bEnableState &&
3712 ((es->flags & EF_FOCUSED) ||
3713 (es->style & ES_NOHIDESEL));
3714 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3716 /* The dc we use for calculating may not be the one we paint into.
3717 This is the safest action. */
3718 EDIT_InvalidateUniscribeData(es);
3719 GetClientRect(es->hwndSelf, &rcClient);
3721 /* get the background brush */
3722 brush = EDIT_NotifyCtlColor(es, dc);
3724 /* paint the border and the background */
3725 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3727 if(es->style & WS_BORDER) {
3728 bw = GetSystemMetrics(SM_CXBORDER);
3729 bh = GetSystemMetrics(SM_CYBORDER);
3730 rc = rcClient;
3731 if(es->style & ES_MULTILINE) {
3732 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3733 if(es->style & WS_VSCROLL) rc.right+=bw;
3736 /* Draw the frame. Same code as in nonclient.c */
3737 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3738 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3739 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3740 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3741 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3742 SelectObject(dc, old_brush);
3744 /* Keep the border clean */
3745 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3746 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3749 GetClipBox(dc, &rc);
3750 FillRect(dc, &rc, brush);
3752 IntersectClipRect(dc, es->format_rect.left,
3753 es->format_rect.top,
3754 es->format_rect.right,
3755 es->format_rect.bottom);
3756 if (es->style & ES_MULTILINE) {
3757 rc = rcClient;
3758 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3760 if (es->font)
3761 old_font = SelectObject(dc, es->font);
3763 if (!es->bEnableState)
3764 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3765 GetClipBox(dc, &rcRgn);
3766 if (es->style & ES_MULTILINE) {
3767 INT vlc = get_vertical_line_count(es);
3768 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3769 EDIT_UpdateUniscribeData(es, dc, i);
3770 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3771 if (IntersectRect(&rc, &rcRgn, &rcLine))
3772 EDIT_PaintLine(es, dc, i, rev);
3774 } else {
3775 EDIT_UpdateUniscribeData(es, dc, 0);
3776 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3777 if (IntersectRect(&rc, &rcRgn, &rcLine))
3778 EDIT_PaintLine(es, dc, 0, rev);
3780 if (es->font)
3781 SelectObject(dc, old_font);
3783 if (!hdc)
3784 EndPaint(es->hwndSelf, &ps);
3788 /*********************************************************************
3790 * WM_SETFOCUS
3793 static void EDIT_WM_SetFocus(EDITSTATE *es)
3795 es->flags |= EF_FOCUSED;
3797 if (!(es->style & ES_NOHIDESEL))
3798 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3800 /* single line edit updates itself */
3801 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3803 HDC hdc = GetDC(es->hwndSelf);
3804 EDIT_WM_Paint(es, hdc);
3805 ReleaseDC(es->hwndSelf, hdc);
3808 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3809 EDIT_SetCaretPos(es, es->selection_end,
3810 es->flags & EF_AFTER_WRAP);
3811 ShowCaret(es->hwndSelf);
3812 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3815 static DWORD get_font_margins(HDC hdc, const TEXTMETRICW *tm, BOOL unicode)
3817 ABC abc[256];
3818 SHORT left, right;
3819 UINT i;
3821 if (!(tm->tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE)))
3822 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3824 if (unicode) {
3825 if (!is_cjk_charset(hdc) && !is_cjk_font(hdc))
3826 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3827 if (!GetCharABCWidthsW(hdc, 0, 255, abc))
3828 return 0;
3830 else if (is_cjk_charset(hdc)) {
3831 if (!GetCharABCWidthsA(hdc, 0, 255, abc))
3832 return 0;
3834 else
3835 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3837 left = right = 0;
3838 for (i = 0; i < ARRAY_SIZE(abc); i++) {
3839 if (-abc[i].abcA > right) right = -abc[i].abcA;
3840 if (-abc[i].abcC > left ) left = -abc[i].abcC;
3842 return MAKELONG(left, right);
3845 /*********************************************************************
3847 * WM_SETFONT
3849 * With Win95 look the margins are set to default font value unless
3850 * the system font (font == 0) is being set, in which case they are left
3851 * unchanged.
3854 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3856 TEXTMETRICW tm;
3857 HDC dc;
3858 HFONT old_font = 0;
3859 RECT clientRect;
3860 DWORD margins;
3862 es->font = font;
3863 EDIT_InvalidateUniscribeData(es);
3864 dc = GetDC(es->hwndSelf);
3865 if (font)
3866 old_font = SelectObject(dc, font);
3867 GetTextMetricsW(dc, &tm);
3868 es->line_height = tm.tmHeight;
3869 es->char_width = tm.tmAveCharWidth;
3870 margins = get_font_margins(dc, &tm, es->is_unicode);
3871 if (font)
3872 SelectObject(dc, old_font);
3873 ReleaseDC(es->hwndSelf, dc);
3875 /* Reset the format rect and the margins */
3876 GetClientRect(es->hwndSelf, &clientRect);
3877 EDIT_SetRectNP(es, &clientRect);
3878 if (margins)
3879 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3880 LOWORD(margins), HIWORD(margins), FALSE);
3882 if (es->style & ES_MULTILINE)
3883 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3884 else
3885 EDIT_CalcLineWidth_SL(es);
3887 if (redraw)
3888 EDIT_UpdateText(es, NULL, TRUE);
3889 if (es->flags & EF_FOCUSED) {
3890 DestroyCaret();
3891 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3892 EDIT_SetCaretPos(es, es->selection_end,
3893 es->flags & EF_AFTER_WRAP);
3894 ShowCaret(es->hwndSelf);
3899 /*********************************************************************
3901 * WM_SETTEXT
3903 * NOTES
3904 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3905 * The modified flag is reset. No notifications are sent.
3907 * For single-line controls, reception of WM_SETTEXT triggers:
3908 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3911 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3913 LPWSTR textW = NULL;
3914 if (!unicode && text)
3916 LPCSTR textA = (LPCSTR)text;
3917 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3918 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3919 if (textW)
3920 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3921 text = textW;
3924 if (es->flags & EF_UPDATE)
3925 /* fixed this bug once; complain if we see it about to happen again. */
3926 ERR("SetSel may generate UPDATE message whose handler may reset "
3927 "selection.\n");
3929 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3930 if (text)
3932 TRACE("%s\n", debugstr_w(text));
3933 EDIT_EM_ReplaceSel(es, FALSE, text, strlenW(text), FALSE, FALSE);
3934 if(!unicode)
3935 HeapFree(GetProcessHeap(), 0, textW);
3937 else
3939 TRACE("<NULL>\n");
3940 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3942 es->x_offset = 0;
3943 es->flags &= ~EF_MODIFIED;
3944 EDIT_EM_SetSel(es, 0, 0, FALSE);
3946 /* Send the notification after the selection start and end have been set
3947 * edit control doesn't send notification on WM_SETTEXT
3948 * if it is multiline, or it is part of combobox
3950 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3952 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3953 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3955 EDIT_EM_ScrollCaret(es);
3956 EDIT_UpdateScrollInfo(es);
3957 EDIT_InvalidateUniscribeData(es);
3961 /*********************************************************************
3963 * WM_SIZE
3966 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3968 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3969 RECT rc;
3970 GetClientRect(es->hwndSelf, &rc);
3971 EDIT_SetRectNP(es, &rc);
3972 EDIT_UpdateText(es, NULL, TRUE);
3977 /*********************************************************************
3979 * WM_STYLECHANGED
3981 * This message is sent by SetWindowLong on having changed either the Style
3982 * or the extended style.
3984 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3986 * See also EDIT_WM_NCCreate
3988 * It appears that the Windows version of the edit control allows the style
3989 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3990 * style variable which will generally be different. In this function we
3991 * update the internal style based on what changed in the externally visible
3992 * style.
3994 * Much of this content as based upon the MSDN, especially:
3995 * Platform SDK Documentation -> User Interface Services ->
3996 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3997 * Edit Control Styles
3999 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4001 if (GWL_STYLE == which) {
4002 DWORD style_change_mask;
4003 DWORD new_style;
4004 /* Only a subset of changes can be applied after the control
4005 * has been created.
4007 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4008 ES_NUMBER;
4009 if (es->style & ES_MULTILINE)
4010 style_change_mask |= ES_WANTRETURN;
4012 new_style = style->styleNew & style_change_mask;
4014 /* Number overrides lowercase overrides uppercase (at least it
4015 * does in Win95). However I'll bet that ES_NUMBER would be
4016 * invalid under Win 3.1.
4018 if (new_style & ES_NUMBER) {
4019 ; /* do not override the ES_NUMBER */
4020 } else if (new_style & ES_LOWERCASE) {
4021 new_style &= ~ES_UPPERCASE;
4024 es->style = (es->style & ~style_change_mask) | new_style;
4025 } else if (GWL_EXSTYLE == which) {
4026 ; /* FIXME - what is needed here */
4027 } else {
4028 WARN ("Invalid style change %ld\n",which);
4031 return 0;
4034 /*********************************************************************
4036 * WM_SYSKEYDOWN
4039 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4041 if ((key == VK_BACK) && (key_data & 0x2000)) {
4042 if (EDIT_EM_CanUndo(es))
4043 EDIT_EM_Undo(es);
4044 return 0;
4045 } else if (key == VK_UP || key == VK_DOWN) {
4046 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4047 return 0;
4049 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4053 /*********************************************************************
4055 * WM_TIMER
4058 static void EDIT_WM_Timer(EDITSTATE *es)
4060 if (es->region_posx < 0) {
4061 EDIT_MoveBackward(es, TRUE);
4062 } else if (es->region_posx > 0) {
4063 EDIT_MoveForward(es, TRUE);
4066 * FIXME: gotta do some vertical scrolling here, like
4067 * EDIT_EM_LineScroll(hwnd, 0, 1);
4071 /*********************************************************************
4073 * WM_HSCROLL
4076 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4078 INT dx;
4079 INT fw;
4081 if (!(es->style & ES_MULTILINE))
4082 return 0;
4084 if (!(es->style & ES_AUTOHSCROLL))
4085 return 0;
4087 dx = 0;
4088 fw = es->format_rect.right - es->format_rect.left;
4089 switch (action) {
4090 case SB_LINELEFT:
4091 TRACE("SB_LINELEFT\n");
4092 if (es->x_offset)
4093 dx = -es->char_width;
4094 break;
4095 case SB_LINERIGHT:
4096 TRACE("SB_LINERIGHT\n");
4097 if (es->x_offset < es->text_width)
4098 dx = es->char_width;
4099 break;
4100 case SB_PAGELEFT:
4101 TRACE("SB_PAGELEFT\n");
4102 if (es->x_offset)
4103 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4104 break;
4105 case SB_PAGERIGHT:
4106 TRACE("SB_PAGERIGHT\n");
4107 if (es->x_offset < es->text_width)
4108 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4109 break;
4110 case SB_LEFT:
4111 TRACE("SB_LEFT\n");
4112 if (es->x_offset)
4113 dx = -es->x_offset;
4114 break;
4115 case SB_RIGHT:
4116 TRACE("SB_RIGHT\n");
4117 if (es->x_offset < es->text_width)
4118 dx = es->text_width - es->x_offset;
4119 break;
4120 case SB_THUMBTRACK:
4121 TRACE("SB_THUMBTRACK %d\n", pos);
4122 es->flags |= EF_HSCROLL_TRACK;
4123 if(es->style & WS_HSCROLL)
4124 dx = pos - es->x_offset;
4125 else
4127 INT fw, new_x;
4128 /* Sanity check */
4129 if(pos < 0 || pos > 100) return 0;
4130 /* Assume default scroll range 0-100 */
4131 fw = es->format_rect.right - es->format_rect.left;
4132 new_x = pos * (es->text_width - fw) / 100;
4133 dx = es->text_width ? (new_x - es->x_offset) : 0;
4135 break;
4136 case SB_THUMBPOSITION:
4137 TRACE("SB_THUMBPOSITION %d\n", pos);
4138 es->flags &= ~EF_HSCROLL_TRACK;
4139 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4140 dx = pos - es->x_offset;
4141 else
4143 INT fw, new_x;
4144 /* Sanity check */
4145 if(pos < 0 || pos > 100) return 0;
4146 /* Assume default scroll range 0-100 */
4147 fw = es->format_rect.right - es->format_rect.left;
4148 new_x = pos * (es->text_width - fw) / 100;
4149 dx = es->text_width ? (new_x - es->x_offset) : 0;
4151 if (!dx) {
4152 /* force scroll info update */
4153 EDIT_UpdateScrollInfo(es);
4154 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4156 break;
4157 case SB_ENDSCROLL:
4158 TRACE("SB_ENDSCROLL\n");
4159 break;
4161 * FIXME : the next two are undocumented !
4162 * Are we doing the right thing ?
4163 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4164 * although it's also a regular control message.
4166 case EM_GETTHUMB: /* this one is used by NT notepad */
4168 LRESULT ret;
4169 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4170 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4171 else
4173 /* Assume default scroll range 0-100 */
4174 INT fw = es->format_rect.right - es->format_rect.left;
4175 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4177 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4178 return ret;
4180 case EM_LINESCROLL:
4181 TRACE("EM_LINESCROLL16\n");
4182 dx = pos;
4183 break;
4185 default:
4186 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4187 action, action);
4188 return 0;
4190 if (dx)
4192 INT fw = es->format_rect.right - es->format_rect.left;
4193 /* check if we are going to move too far */
4194 if(es->x_offset + dx + fw > es->text_width)
4195 dx = es->text_width - fw - es->x_offset;
4196 if(dx)
4197 EDIT_EM_LineScroll_internal(es, dx, 0);
4199 return 0;
4203 /*********************************************************************
4205 * WM_VSCROLL
4208 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4210 INT dy;
4212 if (!(es->style & ES_MULTILINE))
4213 return 0;
4215 if (!(es->style & ES_AUTOVSCROLL))
4216 return 0;
4218 dy = 0;
4219 switch (action) {
4220 case SB_LINEUP:
4221 case SB_LINEDOWN:
4222 case SB_PAGEUP:
4223 case SB_PAGEDOWN:
4224 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4225 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4226 (action == SB_PAGEUP ? "SB_PAGEUP" :
4227 "SB_PAGEDOWN"))));
4228 EDIT_EM_Scroll(es, action);
4229 return 0;
4230 case SB_TOP:
4231 TRACE("SB_TOP\n");
4232 dy = -es->y_offset;
4233 break;
4234 case SB_BOTTOM:
4235 TRACE("SB_BOTTOM\n");
4236 dy = es->line_count - 1 - es->y_offset;
4237 break;
4238 case SB_THUMBTRACK:
4239 TRACE("SB_THUMBTRACK %d\n", pos);
4240 es->flags |= EF_VSCROLL_TRACK;
4241 if(es->style & WS_VSCROLL)
4242 dy = pos - es->y_offset;
4243 else
4245 /* Assume default scroll range 0-100 */
4246 INT vlc, new_y;
4247 /* Sanity check */
4248 if(pos < 0 || pos > 100) return 0;
4249 vlc = get_vertical_line_count(es);
4250 new_y = pos * (es->line_count - vlc) / 100;
4251 dy = es->line_count ? (new_y - es->y_offset) : 0;
4252 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4253 es->line_count, es->y_offset, pos, dy);
4255 break;
4256 case SB_THUMBPOSITION:
4257 TRACE("SB_THUMBPOSITION %d\n", pos);
4258 es->flags &= ~EF_VSCROLL_TRACK;
4259 if(es->style & WS_VSCROLL)
4260 dy = pos - es->y_offset;
4261 else
4263 /* Assume default scroll range 0-100 */
4264 INT vlc, new_y;
4265 /* Sanity check */
4266 if(pos < 0 || pos > 100) return 0;
4267 vlc = get_vertical_line_count(es);
4268 new_y = pos * (es->line_count - vlc) / 100;
4269 dy = es->line_count ? (new_y - es->y_offset) : 0;
4270 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4271 es->line_count, es->y_offset, pos, dy);
4273 if (!dy)
4275 /* force scroll info update */
4276 EDIT_UpdateScrollInfo(es);
4277 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4279 break;
4280 case SB_ENDSCROLL:
4281 TRACE("SB_ENDSCROLL\n");
4282 break;
4284 * FIXME : the next two are undocumented !
4285 * Are we doing the right thing ?
4286 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4287 * although it's also a regular control message.
4289 case EM_GETTHUMB: /* this one is used by NT notepad */
4291 LRESULT ret;
4292 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4293 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4294 else
4296 /* Assume default scroll range 0-100 */
4297 INT vlc = get_vertical_line_count(es);
4298 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4300 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4301 return ret;
4303 case EM_LINESCROLL:
4304 TRACE("EM_LINESCROLL %d\n", pos);
4305 dy = pos;
4306 break;
4308 default:
4309 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4310 action, action);
4311 return 0;
4313 if (dy)
4314 EDIT_EM_LineScroll(es, 0, dy);
4315 return 0;
4318 /*********************************************************************
4320 * EM_GETTHUMB
4322 * FIXME: is this right ? (or should it be only VSCROLL)
4323 * (and maybe only for edit controls that really have their
4324 * own scrollbars) (and maybe only for multiline controls ?)
4325 * All in all: very poorly documented
4328 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4330 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4331 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4335 /********************************************************************
4337 * The Following code is to handle inline editing from IMEs
4340 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4342 LONG buflen;
4343 LPWSTR lpCompStr;
4344 LPSTR lpCompStrAttr = NULL;
4345 DWORD dwBufLenAttr;
4347 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4349 if (buflen < 0)
4351 return;
4354 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen);
4355 if (!lpCompStr)
4357 ERR("Unable to allocate IME CompositionString\n");
4358 return;
4361 if (buflen)
4362 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4364 if (CompFlag & GCS_COMPATTR)
4367 * We do not use the attributes yet. it would tell us what characters
4368 * are in transition and which are converted or decided upon
4370 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4371 if (dwBufLenAttr)
4373 dwBufLenAttr ++;
4374 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4375 if (!lpCompStrAttr)
4377 ERR("Unable to allocate IME Attribute String\n");
4378 HeapFree(GetProcessHeap(),0,lpCompStr);
4379 return;
4381 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4382 dwBufLenAttr);
4383 lpCompStrAttr[dwBufLenAttr] = 0;
4387 /* check for change in composition start */
4388 if (es->selection_end < es->composition_start)
4389 es->composition_start = es->selection_end;
4391 /* replace existing selection string */
4392 es->selection_start = es->composition_start;
4394 if (es->composition_len > 0)
4395 es->selection_end = es->composition_start + es->composition_len;
4396 else
4397 es->selection_end = es->selection_start;
4399 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4400 es->composition_len = abs(es->composition_start - es->selection_end);
4402 es->selection_start = es->composition_start;
4403 es->selection_end = es->selection_start + es->composition_len;
4405 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4406 HeapFree(GetProcessHeap(),0,lpCompStr);
4409 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4411 LONG buflen;
4412 LPWSTR lpResultStr;
4414 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4415 if (buflen <= 0)
4417 return;
4420 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4421 if (!lpResultStr)
4423 ERR("Unable to alloc buffer for IME string\n");
4424 return;
4427 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4429 /* check for change in composition start */
4430 if (es->selection_end < es->composition_start)
4431 es->composition_start = es->selection_end;
4433 es->selection_start = es->composition_start;
4434 es->selection_end = es->composition_start + es->composition_len;
4435 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4436 es->composition_start = es->selection_end;
4437 es->composition_len = 0;
4439 HeapFree(GetProcessHeap(),0,lpResultStr);
4442 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4444 HIMC hIMC;
4445 int cursor;
4447 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4449 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4450 es->composition_start = es->selection_end;
4453 hIMC = ImmGetContext(hwnd);
4454 if (!hIMC)
4455 return;
4457 if (CompFlag & GCS_RESULTSTR)
4459 EDIT_GetResultStr(hIMC, es);
4460 cursor = 0;
4462 else
4464 if (CompFlag & GCS_COMPSTR)
4465 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4466 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4468 ImmReleaseContext(hwnd, hIMC);
4469 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4473 /*********************************************************************
4475 * WM_NCCREATE
4477 * See also EDIT_WM_StyleChanged
4479 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4481 EDITSTATE *es;
4482 UINT alloc_size;
4484 TRACE("Creating %s edit control, style = %08x\n",
4485 unicode ? "Unicode" : "ANSI", lpcs->style);
4487 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4488 return FALSE;
4489 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4492 * Note: since the EDITSTATE has not been fully initialized yet,
4493 * we can't use any API calls that may send
4494 * WM_XXX messages before WM_NCCREATE is completed.
4497 es->is_unicode = unicode;
4498 es->style = lpcs->style;
4500 es->bEnableState = !(es->style & WS_DISABLED);
4502 es->hwndSelf = hwnd;
4503 /* Save parent, which will be notified by EN_* messages */
4504 es->hwndParent = lpcs->hwndParent;
4506 if (es->style & ES_COMBO)
4507 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4509 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4510 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4512 /* Number overrides lowercase overrides uppercase (at least it
4513 * does in Win95). However I'll bet that ES_NUMBER would be
4514 * invalid under Win 3.1.
4516 if (es->style & ES_NUMBER) {
4517 ; /* do not override the ES_NUMBER */
4518 } else if (es->style & ES_LOWERCASE) {
4519 es->style &= ~ES_UPPERCASE;
4521 if (es->style & ES_MULTILINE) {
4522 es->buffer_limit = BUFLIMIT_INITIAL;
4523 if (es->style & WS_VSCROLL)
4524 es->style |= ES_AUTOVSCROLL;
4525 if (es->style & WS_HSCROLL)
4526 es->style |= ES_AUTOHSCROLL;
4527 es->style &= ~ES_PASSWORD;
4528 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4529 /* Confirmed - RIGHT overrides CENTER */
4530 if (es->style & ES_RIGHT)
4531 es->style &= ~ES_CENTER;
4532 es->style &= ~WS_HSCROLL;
4533 es->style &= ~ES_AUTOHSCROLL;
4535 } else {
4536 es->buffer_limit = BUFLIMIT_INITIAL;
4537 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4538 es->style &= ~ES_CENTER;
4539 es->style &= ~WS_HSCROLL;
4540 es->style &= ~WS_VSCROLL;
4541 if (es->style & ES_PASSWORD)
4542 es->password_char = '*';
4545 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4546 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4547 goto cleanup;
4548 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4550 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4551 goto cleanup;
4552 es->undo_buffer_size = es->buffer_size;
4554 if (es->style & ES_MULTILINE)
4555 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4556 goto cleanup;
4557 es->line_count = 1;
4560 * In Win95 look and feel, the WS_BORDER style is replaced by the
4561 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4562 * control a nonclient area so we don't need to draw the border.
4563 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4564 * a nonclient area and we should handle painting the border ourselves.
4566 * When making modifications please ensure that the code still works
4567 * for edit controls created directly with style 0x50800000, exStyle 0
4568 * (which should have a single pixel border)
4570 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4571 es->style &= ~WS_BORDER;
4572 else if (es->style & WS_BORDER)
4573 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4575 return TRUE;
4577 cleanup:
4578 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4579 EDIT_InvalidateUniscribeData(es);
4580 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4581 HeapFree(GetProcessHeap(), 0, es->undo_text);
4582 if (es->hloc32W) LocalFree(es->hloc32W);
4583 HeapFree(GetProcessHeap(), 0, es->logAttr);
4584 HeapFree(GetProcessHeap(), 0, es);
4585 return FALSE;
4589 /*********************************************************************
4591 * WM_CREATE
4594 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4596 RECT clientRect;
4598 TRACE("%s\n", debugstr_w(name));
4600 * To initialize some final structure members, we call some helper
4601 * functions. However, since the EDITSTATE is not consistent (i.e.
4602 * not fully initialized), we should be very careful which
4603 * functions can be called, and in what order.
4605 EDIT_WM_SetFont(es, 0, FALSE);
4606 EDIT_EM_EmptyUndoBuffer(es);
4608 /* We need to calculate the format rect
4609 (applications may send EM_SETMARGINS before the control gets visible) */
4610 GetClientRect(es->hwndSelf, &clientRect);
4611 EDIT_SetRectNP(es, &clientRect);
4613 if (name && *name) {
4614 EDIT_EM_ReplaceSel(es, FALSE, name, strlenW(name), FALSE, FALSE);
4615 /* if we insert text to the editline, the text scrolls out
4616 * of the window, as the caret is placed after the insert
4617 * pos normally; thus we reset es->selection... to 0 and
4618 * update caret
4620 es->selection_start = es->selection_end = 0;
4621 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4622 * Messages are only to be sent when the USER does something to
4623 * change the contents. So I am removing this EN_CHANGE
4625 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4627 EDIT_EM_ScrollCaret(es);
4629 /* force scroll info update */
4630 EDIT_UpdateScrollInfo(es);
4631 /* The rule seems to return 1 here for success */
4632 /* Power Builder masked edit controls will crash */
4633 /* if not. */
4634 /* FIXME: is that in all cases so ? */
4635 return 1;
4639 /*********************************************************************
4641 * WM_NCDESTROY
4644 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4646 LINEDEF *pc, *pp;
4648 /* The app can own the text buffer handle */
4649 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4650 LocalFree(es->hloc32W);
4652 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4653 LocalFree(es->hloc32A);
4655 EDIT_InvalidateUniscribeData(es);
4656 pc = es->first_line_def;
4657 while (pc)
4659 pp = pc->next;
4660 HeapFree(GetProcessHeap(), 0, pc);
4661 pc = pp;
4664 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4665 HeapFree(GetProcessHeap(), 0, es->undo_text);
4666 HeapFree(GetProcessHeap(), 0, es);
4668 return 0;
4672 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4674 if(unicode)
4675 return DefWindowProcW(hwnd, msg, wParam, lParam);
4676 else
4677 return DefWindowProcA(hwnd, msg, wParam, lParam);
4680 /*********************************************************************
4682 * EditWndProc_common
4684 * The messages are in the order of the actual integer values
4685 * (which can be found in include/windows.h)
4687 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4689 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4690 LRESULT result = 0;
4692 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4694 if (!es && msg != WM_NCCREATE)
4695 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4697 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4699 switch (msg) {
4700 case EM_GETSEL:
4701 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4702 break;
4704 case EM_SETSEL:
4705 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4706 EDIT_EM_ScrollCaret(es);
4707 result = 1;
4708 break;
4710 case EM_GETRECT:
4711 if (lParam)
4712 *((LPRECT)lParam) = es->format_rect;
4713 break;
4715 case EM_SETRECT:
4716 if ((es->style & ES_MULTILINE) && lParam) {
4717 EDIT_SetRectNP(es, (LPRECT)lParam);
4718 EDIT_UpdateText(es, NULL, TRUE);
4720 break;
4722 case EM_SETRECTNP:
4723 if ((es->style & ES_MULTILINE) && lParam)
4724 EDIT_SetRectNP(es, (LPRECT)lParam);
4725 break;
4727 case EM_SCROLL:
4728 result = EDIT_EM_Scroll(es, (INT)wParam);
4729 break;
4731 case EM_LINESCROLL:
4732 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4733 break;
4735 case EM_SCROLLCARET:
4736 EDIT_EM_ScrollCaret(es);
4737 result = 1;
4738 break;
4740 case EM_GETMODIFY:
4741 result = ((es->flags & EF_MODIFIED) != 0);
4742 break;
4744 case EM_SETMODIFY:
4745 if (wParam)
4746 es->flags |= EF_MODIFIED;
4747 else
4748 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4749 break;
4751 case EM_GETLINECOUNT:
4752 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4753 break;
4755 case EM_LINEINDEX:
4756 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4757 break;
4759 case EM_SETHANDLE:
4760 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4761 break;
4763 case EM_GETHANDLE:
4764 result = (LRESULT)EDIT_EM_GetHandle(es);
4765 break;
4767 case EM_GETTHUMB:
4768 result = EDIT_EM_GetThumb(es);
4769 break;
4771 /* these messages missing from specs */
4772 case 0x00bf:
4773 case 0x00c0:
4774 case 0x00c3:
4775 case 0x00ca:
4776 FIXME("undocumented message 0x%x, please report\n", msg);
4777 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4778 break;
4780 case EM_LINELENGTH:
4781 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4782 break;
4784 case EM_REPLACESEL:
4786 LPWSTR textW;
4788 if(unicode)
4789 textW = (LPWSTR)lParam;
4790 else
4792 LPSTR textA = (LPSTR)lParam;
4793 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4794 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4795 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4798 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, strlenW(textW), TRUE, TRUE);
4799 result = 1;
4801 if(!unicode)
4802 HeapFree(GetProcessHeap(), 0, textW);
4803 break;
4806 case EM_GETLINE:
4807 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4808 break;
4810 case EM_SETLIMITTEXT:
4811 EDIT_EM_SetLimitText(es, wParam);
4812 break;
4814 case EM_CANUNDO:
4815 result = (LRESULT)EDIT_EM_CanUndo(es);
4816 break;
4818 case EM_UNDO:
4819 case WM_UNDO:
4820 result = (LRESULT)EDIT_EM_Undo(es);
4821 break;
4823 case EM_FMTLINES:
4824 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4825 break;
4827 case EM_LINEFROMCHAR:
4828 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4829 break;
4831 case EM_SETTABSTOPS:
4832 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4833 break;
4835 case EM_SETPASSWORDCHAR:
4837 WCHAR charW = 0;
4839 if(unicode)
4840 charW = (WCHAR)wParam;
4841 else
4843 CHAR charA = wParam;
4844 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4847 EDIT_EM_SetPasswordChar(es, charW);
4848 break;
4851 case EM_EMPTYUNDOBUFFER:
4852 EDIT_EM_EmptyUndoBuffer(es);
4853 break;
4855 case EM_GETFIRSTVISIBLELINE:
4856 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4857 break;
4859 case EM_SETREADONLY:
4861 DWORD old_style = es->style;
4863 if (wParam) {
4864 SetWindowLongW( hwnd, GWL_STYLE,
4865 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4866 es->style |= ES_READONLY;
4867 } else {
4868 SetWindowLongW( hwnd, GWL_STYLE,
4869 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4870 es->style &= ~ES_READONLY;
4873 if (old_style ^ es->style)
4874 InvalidateRect(es->hwndSelf, NULL, TRUE);
4876 result = 1;
4877 break;
4880 case EM_SETWORDBREAKPROC:
4881 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4882 result = 1;
4883 break;
4885 case EM_GETWORDBREAKPROC:
4886 result = (LRESULT)es->word_break_proc;
4887 break;
4889 case EM_GETPASSWORDCHAR:
4891 if(unicode)
4892 result = es->password_char;
4893 else
4895 WCHAR charW = es->password_char;
4896 CHAR charA = 0;
4897 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4898 result = charA;
4900 break;
4903 case EM_SETMARGINS:
4904 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4905 break;
4907 case EM_GETMARGINS:
4908 result = MAKELONG(es->left_margin, es->right_margin);
4909 break;
4911 case EM_GETLIMITTEXT:
4912 result = es->buffer_limit;
4913 break;
4915 case EM_POSFROMCHAR:
4916 if ((INT)wParam >= get_text_length(es)) result = -1;
4917 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4918 break;
4920 case EM_CHARFROMPOS:
4921 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4922 break;
4924 /* End of the EM_ messages which were in numerical order; what order
4925 * are these in? vaguely alphabetical?
4928 case WM_NCCREATE:
4929 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4930 break;
4932 case WM_NCDESTROY:
4933 result = EDIT_WM_NCDestroy(es);
4934 es = NULL;
4935 break;
4937 case WM_GETDLGCODE:
4938 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4940 if (es->style & ES_MULTILINE)
4941 result |= DLGC_WANTALLKEYS;
4943 if (lParam)
4945 es->flags|=EF_DIALOGMODE;
4947 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4949 int vk = (int)((LPMSG)lParam)->wParam;
4951 if (es->hwndListBox)
4953 if (vk == VK_RETURN || vk == VK_ESCAPE)
4954 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4955 result |= DLGC_WANTMESSAGE;
4959 break;
4961 case WM_IME_CHAR:
4962 if (!unicode)
4964 WCHAR charW;
4965 CHAR strng[2];
4967 strng[0] = wParam >> 8;
4968 strng[1] = wParam & 0xff;
4969 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4970 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4971 result = EDIT_WM_Char(es, charW);
4972 break;
4974 /* fall through */
4975 case WM_CHAR:
4977 WCHAR charW;
4979 if(unicode)
4980 charW = wParam;
4981 else
4983 CHAR charA = wParam;
4984 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4987 if (es->hwndListBox)
4989 if (charW == VK_RETURN || charW == VK_ESCAPE)
4991 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4992 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4993 break;
4996 result = EDIT_WM_Char(es, charW);
4997 break;
5000 case WM_UNICHAR:
5001 if (unicode)
5003 if (wParam == UNICODE_NOCHAR) return TRUE;
5004 if (wParam <= 0x000fffff)
5006 if(wParam > 0xffff) /* convert to surrogates */
5008 wParam -= 0x10000;
5009 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
5010 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
5012 else EDIT_WM_Char(es, wParam);
5014 return 0;
5016 break;
5018 case WM_CLEAR:
5019 EDIT_WM_Clear(es);
5020 break;
5022 case WM_CONTEXTMENU:
5023 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5024 break;
5026 case WM_COPY:
5027 EDIT_WM_Copy(es);
5028 break;
5030 case WM_CREATE:
5031 if(unicode)
5032 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
5033 else
5035 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
5036 LPWSTR nameW = NULL;
5037 if(nameA)
5039 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5040 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5041 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5043 result = EDIT_WM_Create(es, nameW);
5044 HeapFree(GetProcessHeap(), 0, nameW);
5046 break;
5048 case WM_CUT:
5049 EDIT_WM_Cut(es);
5050 break;
5052 case WM_ENABLE:
5053 es->bEnableState = (BOOL) wParam;
5054 EDIT_UpdateText(es, NULL, TRUE);
5055 break;
5057 case WM_ERASEBKGND:
5058 /* we do the proper erase in EDIT_WM_Paint */
5059 result = 1;
5060 break;
5062 case WM_GETFONT:
5063 result = (LRESULT)es->font;
5064 break;
5066 case WM_GETTEXT:
5067 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5068 break;
5070 case WM_GETTEXTLENGTH:
5071 if (unicode) result = get_text_length(es);
5072 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5073 NULL, 0, NULL, NULL );
5074 break;
5076 case WM_HSCROLL:
5077 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5078 break;
5080 case WM_KEYDOWN:
5081 result = EDIT_WM_KeyDown(es, (INT)wParam);
5082 break;
5084 case WM_KILLFOCUS:
5085 result = EDIT_WM_KillFocus(es);
5086 break;
5088 case WM_LBUTTONDBLCLK:
5089 result = EDIT_WM_LButtonDblClk(es);
5090 break;
5092 case WM_LBUTTONDOWN:
5093 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5094 break;
5096 case WM_LBUTTONUP:
5097 result = EDIT_WM_LButtonUp(es);
5098 break;
5100 case WM_MBUTTONDOWN:
5101 result = EDIT_WM_MButtonDown(es);
5102 break;
5104 case WM_MOUSEMOVE:
5105 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5106 break;
5108 case WM_PRINTCLIENT:
5109 case WM_PAINT:
5110 EDIT_WM_Paint(es, (HDC)wParam);
5111 break;
5113 case WM_PASTE:
5114 EDIT_WM_Paste(es);
5115 break;
5117 case WM_SETFOCUS:
5118 EDIT_WM_SetFocus(es);
5119 break;
5121 case WM_SETFONT:
5122 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5123 break;
5125 case WM_SETREDRAW:
5126 /* FIXME: actually set an internal flag and behave accordingly */
5127 break;
5129 case WM_SETTEXT:
5130 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5131 result = TRUE;
5132 break;
5134 case WM_SIZE:
5135 EDIT_WM_Size(es, (UINT)wParam);
5136 break;
5138 case WM_STYLECHANGED:
5139 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5140 break;
5142 case WM_STYLECHANGING:
5143 result = 0; /* See EDIT_WM_StyleChanged */
5144 break;
5146 case WM_SYSKEYDOWN:
5147 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5148 break;
5150 case WM_TIMER:
5151 EDIT_WM_Timer(es);
5152 break;
5154 case WM_VSCROLL:
5155 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5156 break;
5158 case WM_MOUSEWHEEL:
5160 int wheelDelta;
5161 INT pulScrollLines = 3;
5162 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5164 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5165 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5166 break;
5168 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5169 /* if scrolling changes direction, ignore left overs */
5170 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5171 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5172 es->wheelDeltaRemainder += wheelDelta;
5173 else
5174 es->wheelDeltaRemainder = wheelDelta;
5175 if (es->wheelDeltaRemainder && pulScrollLines)
5177 int cLineScroll;
5178 pulScrollLines = min(es->line_count, pulScrollLines);
5179 cLineScroll = pulScrollLines * es->wheelDeltaRemainder / WHEEL_DELTA;
5180 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
5181 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5184 break;
5187 /* IME messages to make the edit control IME aware */
5188 case WM_IME_SETCONTEXT:
5189 break;
5191 case WM_IME_STARTCOMPOSITION:
5192 es->composition_start = es->selection_end;
5193 es->composition_len = 0;
5194 break;
5196 case WM_IME_COMPOSITION:
5197 EDIT_ImeComposition(hwnd, lParam, es);
5198 break;
5200 case WM_IME_ENDCOMPOSITION:
5201 if (es->composition_len > 0)
5203 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5204 es->selection_end = es->selection_start;
5205 es->composition_len= 0;
5207 break;
5209 case WM_IME_COMPOSITIONFULL:
5210 break;
5212 case WM_IME_SELECT:
5213 break;
5215 case WM_IME_CONTROL:
5216 break;
5218 case WM_IME_REQUEST:
5219 switch (wParam)
5221 case IMR_QUERYCHARPOSITION:
5223 LRESULT pos;
5224 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5226 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5227 chpos->pt.x = LOWORD(pos);
5228 chpos->pt.y = HIWORD(pos);
5229 chpos->cLineHeight = es->line_height;
5230 chpos->rcDocument = es->format_rect;
5231 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5232 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5233 result = 1;
5234 break;
5237 break;
5239 default:
5240 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5241 break;
5244 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5245 EDIT_UnlockBuffer(es, FALSE);
5247 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5249 return result;
5253 /*********************************************************************
5254 * edit class descriptor
5256 static const WCHAR editW[] = {'E','d','i','t',0};
5257 const struct builtin_class_descr EDIT_builtin_class =
5259 editW, /* name */
5260 CS_DBLCLKS | CS_PARENTDC, /* style */
5261 WINPROC_EDIT, /* proc */
5262 #ifdef __i386__
5263 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
5264 #else
5265 sizeof(EDITSTATE *), /* extra */
5266 #endif
5267 IDC_IBEAM, /* cursor */
5268 0 /* brush */