wined3d: Merge wined3d_surface_upload_data() into texture2d_upload_data().
[wine.git] / dlls / user32 / edit.c
blob693c58f6f8bbfc3de017e8dfe060af4937e8b2ed
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 * - EDITBALLOONTIP structure
25 * - EM_GETCUEBANNER/Edit_GetCueBannerText
26 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
27 * - EM_SETCUEBANNER/Edit_SetCueBannerText
28 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
29 * - EM_GETIMESTATUS, EM_SETIMESTATUS
30 * - EN_ALIGN_LTR_EC
31 * - EN_ALIGN_RTL_EC
32 * - ES_OEMCONVERT
36 #include "config.h"
38 #include <stdarg.h>
39 #include <string.h>
40 #include <stdlib.h>
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winnt.h"
45 #include "win.h"
46 #include "imm.h"
47 #include "usp10.h"
48 #include "wine/unicode.h"
49 #include "controls.h"
50 #include "user_private.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(edit);
54 WINE_DECLARE_DEBUG_CHANNEL(combo);
55 WINE_DECLARE_DEBUG_CHANNEL(relay);
57 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
58 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
59 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
60 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
63 * extra flags for EDITSTATE.flags field
65 #define EF_MODIFIED 0x0001 /* text has been modified */
66 #define EF_FOCUSED 0x0002 /* we have input focus */
67 #define EF_UPDATE 0x0004 /* notify parent of changed state */
68 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
69 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
70 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
71 wrapped line, instead of in front of the next character */
72 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
73 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
75 typedef enum
77 END_0 = 0, /* line ends with terminating '\0' character */
78 END_WRAP, /* line is wrapped */
79 END_HARD, /* line ends with a hard return '\r\n' */
80 END_SOFT, /* line ends with a soft return '\r\r\n' */
81 END_RICH /* line ends with a single '\n' */
82 } LINE_END;
84 typedef struct tagLINEDEF {
85 INT length; /* bruto length of a line in bytes */
86 INT net_length; /* netto length of a line in visible characters */
87 LINE_END ending;
88 INT width; /* width of the line in pixels */
89 INT index; /* line index into the buffer */
90 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
91 struct tagLINEDEF *next;
92 } LINEDEF;
94 typedef struct
96 BOOL is_unicode; /* how the control was created */
97 LPWSTR text; /* the actual contents of the control */
98 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
99 UINT buffer_size; /* the size of the buffer in characters */
100 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
101 HFONT font; /* NULL means standard system font */
102 INT x_offset; /* scroll offset for multi lines this is in pixels
103 for single lines it's in characters */
104 INT line_height; /* height of a screen line in pixels */
105 INT char_width; /* average character width in pixels */
106 DWORD style; /* sane version of wnd->dwStyle */
107 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
108 INT undo_insert_count; /* number of characters inserted in sequence */
109 UINT undo_position; /* character index of the insertion and deletion */
110 LPWSTR undo_text; /* deleted text */
111 UINT undo_buffer_size; /* size of the deleted text buffer */
112 INT selection_start; /* == selection_end if no selection */
113 INT selection_end; /* == current caret position */
114 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
115 INT left_margin; /* in pixels */
116 INT right_margin; /* in pixels */
117 RECT format_rect;
118 INT text_width; /* width of the widest line in pixels for multi line controls
119 and just line width for single line controls */
120 INT region_posx; /* Position of cursor relative to region: */
121 INT region_posy; /* -1: to left, 0: within, 1: to right */
122 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
123 INT line_count; /* number of lines */
124 INT y_offset; /* scroll offset in number of lines */
125 BOOL bCaptureState; /* flag indicating whether mouse was captured */
126 BOOL bEnableState; /* flag keeping the enable state */
127 HWND hwndSelf; /* the our window handle */
128 HWND hwndParent; /* Handle of parent for sending EN_* messages.
129 Even if parent will change, EN_* messages
130 should be sent to the first parent. */
131 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
132 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
134 * only for multi line controls
136 INT lock_count; /* amount of re-entries in the EditWndProc */
137 INT tabs_count;
138 LPINT tabs;
139 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
140 HLOCAL hloc32W; /* our unicode local memory block */
141 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
142 or EM_SETHANDLE */
143 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
145 * IME Data
147 UINT composition_len; /* length of composition, 0 == no composition */
148 int composition_start; /* the character position for the composition */
150 * Uniscribe Data
152 SCRIPT_LOGATTR *logAttr;
153 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
154 } EDITSTATE;
157 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
158 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
160 /* used for disabled or read-only edit control */
161 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
162 do \
163 { /* Notify parent which has created this edit control */ \
164 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
165 SendMessageW(es->hwndParent, WM_COMMAND, \
166 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
167 (LPARAM)(es->hwndSelf)); \
168 } while(0)
170 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
172 /*********************************************************************
174 * EM_CANUNDO
177 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
179 return (es->undo_insert_count || strlenW(es->undo_text));
183 /*********************************************************************
185 * EM_EMPTYUNDOBUFFER
188 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
190 es->undo_insert_count = 0;
191 *es->undo_text = '\0';
195 /**********************************************************************
196 * get_app_version
198 * Returns the window version in case Wine emulates a later version
199 * of windows than the application expects.
201 * In a number of cases when windows runs an application that was
202 * designed for an earlier windows version, windows reverts
203 * to "old" behaviour of that earlier version.
205 * An example is a disabled edit control that needs to be painted.
206 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
207 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
208 * applications with an expected version 0f 4.0 or higher.
211 static DWORD get_app_version(void)
213 static DWORD version;
214 if (!version)
216 DWORD dwEmulatedVersion;
217 OSVERSIONINFOW info;
218 DWORD dwProcVersion = GetProcessVersion(0);
220 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
221 GetVersionExW( &info );
222 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
223 /* FIXME: this may not be 100% correct; see discussion on the
224 * wine developer list in Nov 1999 */
225 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
227 return version;
230 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
232 HBRUSH hbrush;
233 UINT msg;
235 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
236 msg = WM_CTLCOLORSTATIC;
237 else
238 msg = WM_CTLCOLOREDIT;
240 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
241 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
242 if (!hbrush)
243 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
244 return hbrush;
248 static inline UINT get_text_length(EDITSTATE *es)
250 if(es->text_length == (UINT)-1)
251 es->text_length = strlenW(es->text);
252 return es->text_length;
256 /*********************************************************************
258 * EDIT_WordBreakProc
260 * Find the beginning of words.
261 * Note: unlike the specs for a WordBreakProc, this function can
262 * only be called without linebreaks between s[0] up to
263 * s[count - 1]. Remember it is only called
264 * internally, so we can decide this for ourselves.
265 * Additionally we will always be breaking the full string.
268 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
270 INT ret = 0;
272 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
274 if(!s) return 0;
276 if (!es->logAttr)
278 SCRIPT_ANALYSIS psa;
280 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
281 psa.eScript = SCRIPT_UNDEFINED;
283 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
284 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
287 switch (action) {
288 case WB_LEFT:
289 if (index)
290 index--;
291 while (index && !es->logAttr[index].fSoftBreak)
292 index--;
293 ret = index;
294 break;
295 case WB_RIGHT:
296 if (!count)
297 break;
298 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
299 index++;
300 ret = index;
301 break;
302 case WB_ISDELIMITER:
303 ret = es->logAttr[index].fWhiteSpace;
304 break;
305 default:
306 ERR("unknown action code, please report !\n");
307 break;
309 return ret;
313 /*********************************************************************
315 * EDIT_CallWordBreakProc
317 * Call appropriate WordBreakProc (internal or external).
319 * Note: The "start" argument should always be an index referring
320 * to es->text. The actual wordbreak proc might be
321 * 16 bit, so we can't always pass any 32 bit LPSTR.
322 * Hence we assume that es->text is the buffer that holds
323 * the string under examination (we can decide this for ourselves).
326 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
328 INT ret;
330 if (es->word_break_proc)
332 if(es->is_unicode)
334 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
336 TRACE_(relay)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
337 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
338 ret = wbpW(es->text + start, index, count, action);
339 TRACE_(relay)("\1Ret wordbreakW %p retval=%d\n", es->word_break_proc, ret );
341 else
343 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
344 INT countA;
345 CHAR *textA;
347 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
348 textA = HeapAlloc(GetProcessHeap(), 0, countA);
349 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
350 TRACE_(relay)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
351 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
352 ret = wbpA(textA, index, countA, action);
353 HeapFree(GetProcessHeap(), 0, textA);
354 TRACE_(relay)("\1Ret wordbreakA %p retval=%d\n", es->word_break_proc, ret );
357 else
358 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
360 return ret;
363 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
365 if (line_def->ssa)
367 ScriptStringFree(&line_def->ssa);
368 line_def->ssa = NULL;
372 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
374 LINEDEF *line_def = es->first_line_def;
375 while (line_def)
377 EDIT_InvalidateUniscribeData_linedef(line_def);
378 line_def = line_def->next;
380 if (es->ssa)
382 ScriptStringFree(&es->ssa);
383 es->ssa = NULL;
387 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
389 if (!line_def)
390 return NULL;
392 if (line_def->net_length && !line_def->ssa)
394 int index = line_def->index;
395 HFONT old_font = NULL;
396 HDC udc = dc;
397 SCRIPT_TABDEF tabdef;
398 HRESULT hr;
400 if (!udc)
401 udc = GetDC(es->hwndSelf);
402 if (es->font)
403 old_font = SelectObject(udc, es->font);
405 tabdef.cTabStops = es->tabs_count;
406 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
407 tabdef.pTabStops = es->tabs;
408 tabdef.iTabOrigin = 0;
410 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
411 (1.5*line_def->net_length+16), -1,
412 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
413 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
414 if (FAILED(hr))
416 WARN("ScriptStringAnalyse failed (%x)\n",hr);
417 line_def->ssa = NULL;
420 if (es->font)
421 SelectObject(udc, old_font);
422 if (udc != dc)
423 ReleaseDC(es->hwndSelf, udc);
426 return line_def->ssa;
429 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
431 LINEDEF *line_def;
433 if (!(es->style & ES_MULTILINE))
435 if (!es->ssa)
437 INT length = get_text_length(es);
438 HFONT old_font = NULL;
439 HDC udc = dc;
441 if (!udc)
442 udc = GetDC(es->hwndSelf);
443 if (es->font)
444 old_font = SelectObject(udc, es->font);
446 if (es->style & ES_PASSWORD)
447 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);
448 else
449 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
451 if (es->font)
452 SelectObject(udc, old_font);
453 if (udc != dc)
454 ReleaseDC(es->hwndSelf, udc);
456 return es->ssa;
458 else
460 line_def = es->first_line_def;
461 while (line_def && line)
463 line_def = line_def->next;
464 line--;
467 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
471 static inline INT get_vertical_line_count(EDITSTATE *es)
473 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
474 return max(1,vlc);
477 /*********************************************************************
479 * EDIT_BuildLineDefs_ML
481 * Build linked list of text lines.
482 * Lines can end with '\0' (last line), a character (if it is wrapped),
483 * a soft return '\r\r\n' or a hard return '\r\n'
486 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
488 LPWSTR current_position, cp;
489 INT fw;
490 LINEDEF *current_line;
491 LINEDEF *previous_line;
492 LINEDEF *start_line;
493 INT line_index = 0, nstart_line, nstart_index;
494 INT line_count = es->line_count;
495 INT orig_net_length;
496 RECT rc;
497 INT vlc;
499 if (istart == iend && delta == 0)
500 return;
502 previous_line = NULL;
503 current_line = es->first_line_def;
505 /* Find starting line. istart must lie inside an existing line or
506 * at the end of buffer */
507 do {
508 if (istart < current_line->index + current_line->length ||
509 current_line->ending == END_0)
510 break;
512 previous_line = current_line;
513 current_line = current_line->next;
514 line_index++;
515 } while (current_line);
517 if (!current_line) /* Error occurred start is not inside previous buffer */
519 FIXME(" modification occurred outside buffer\n");
520 return;
523 /* Remember start of modifications in order to calculate update region */
524 nstart_line = line_index;
525 nstart_index = current_line->index;
527 /* We must start to reformat from the previous line since the modifications
528 * may have caused the line to wrap upwards. */
529 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
531 line_index--;
532 current_line = previous_line;
534 start_line = current_line;
536 fw = es->format_rect.right - es->format_rect.left;
537 current_position = es->text + current_line->index;
538 vlc = get_vertical_line_count(es);
539 do {
540 if (current_line != start_line)
542 if (!current_line || current_line->index + delta > current_position - es->text)
544 /* The buffer has been expanded, create a new line and
545 insert it into the link list */
546 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
547 new_line->next = previous_line->next;
548 previous_line->next = new_line;
549 current_line = new_line;
550 es->line_count++;
552 else if (current_line->index + delta < current_position - es->text)
554 /* The previous line merged with this line so we delete this extra entry */
555 previous_line->next = current_line->next;
556 HeapFree(GetProcessHeap(), 0, current_line);
557 current_line = previous_line->next;
558 es->line_count--;
559 continue;
561 else /* current_line->index + delta == current_position */
563 if (current_position - es->text > iend)
564 break; /* We reached end of line modifications */
565 /* else recalculate this line */
569 current_line->index = current_position - es->text;
570 orig_net_length = current_line->net_length;
572 /* Find end of line */
573 cp = current_position;
574 while (*cp) {
575 if (*cp == '\n') break;
576 if ((*cp == '\r') && (*(cp + 1) == '\n'))
577 break;
578 cp++;
581 /* Mark type of line termination */
582 if (!(*cp)) {
583 current_line->ending = END_0;
584 current_line->net_length = strlenW(current_position);
585 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
586 current_line->ending = END_SOFT;
587 current_line->net_length = cp - current_position - 1;
588 } else if (*cp == '\n') {
589 current_line->ending = END_RICH;
590 current_line->net_length = cp - current_position;
591 } else {
592 current_line->ending = END_HARD;
593 current_line->net_length = cp - current_position;
596 if (current_line->net_length)
598 const SIZE *sz;
599 EDIT_InvalidateUniscribeData_linedef(current_line);
600 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
601 if (current_line->ssa)
603 sz = ScriptString_pSize(current_line->ssa);
604 /* Calculate line width */
605 current_line->width = sz->cx;
607 else current_line->width = es->char_width * current_line->net_length;
609 else current_line->width = 0;
611 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
613 /* Line breaks just look back from the end and find the next break and try that. */
615 if (!(es->style & ES_AUTOHSCROLL)) {
616 if (current_line->width > fw && fw > es->char_width) {
618 INT prev, next;
619 int w;
620 const SIZE *sz;
621 float d;
623 prev = current_line->net_length - 1;
624 w = current_line->net_length;
625 d = (float)current_line->width/(float)fw;
626 if (d > 1.2f) d -= 0.2f;
627 next = prev/d;
628 if (next >= prev) next = prev-1;
629 do {
630 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
631 next, current_line->net_length, WB_LEFT);
632 current_line->net_length = prev;
633 EDIT_InvalidateUniscribeData_linedef(current_line);
634 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
635 if (current_line->ssa)
636 sz = ScriptString_pSize(current_line->ssa);
637 else sz = 0;
638 if (sz)
639 current_line->width = sz->cx;
640 else
641 prev = 0;
642 next = prev - 1;
643 } while (prev && current_line->width > fw);
644 current_line->net_length = w;
646 if (prev == 0) { /* Didn't find a line break so force a break */
647 INT *piDx;
648 const INT *count;
650 EDIT_InvalidateUniscribeData_linedef(current_line);
651 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
653 if (current_line->ssa)
655 count = ScriptString_pcOutChars(current_line->ssa);
656 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
657 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
659 prev = current_line->net_length-1;
660 do {
661 current_line->width -= piDx[prev];
662 prev--;
663 } while ( prev > 0 && current_line->width > fw);
664 if (prev<=0)
665 prev = 1;
666 HeapFree(GetProcessHeap(),0,piDx);
668 else
669 prev = (fw / es->char_width);
672 /* If the first line we are calculating, wrapped before istart, we must
673 * adjust istart in order for this to be reflected in the update region. */
674 if (current_line->index == nstart_index && istart > current_line->index + prev)
675 istart = current_line->index + prev;
676 /* else if we are updating the previous line before the first line we
677 * are re-calculating and it expanded */
678 else if (current_line == start_line &&
679 current_line->index != nstart_index && orig_net_length < prev)
681 /* Line expanded due to an upwards line wrap so we must partially include
682 * previous line in update region */
683 nstart_line = line_index;
684 nstart_index = current_line->index;
685 istart = current_line->index + orig_net_length;
688 current_line->net_length = prev;
689 current_line->ending = END_WRAP;
691 if (current_line->net_length > 0)
693 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
694 if (current_line->ssa)
696 sz = ScriptString_pSize(current_line->ssa);
697 current_line->width = sz->cx;
699 else
700 current_line->width = 0;
702 else current_line->width = 0;
704 else if (current_line == start_line &&
705 current_line->index != nstart_index &&
706 orig_net_length < current_line->net_length) {
707 /* The previous line expanded but it's still not as wide as the client rect */
708 /* The expansion is due to an upwards line wrap so we must partially include
709 it in the update region */
710 nstart_line = line_index;
711 nstart_index = current_line->index;
712 istart = current_line->index + orig_net_length;
717 /* Adjust length to include line termination */
718 switch (current_line->ending) {
719 case END_SOFT:
720 current_line->length = current_line->net_length + 3;
721 break;
722 case END_RICH:
723 current_line->length = current_line->net_length + 1;
724 break;
725 case END_HARD:
726 current_line->length = current_line->net_length + 2;
727 break;
728 case END_WRAP:
729 case END_0:
730 current_line->length = current_line->net_length;
731 break;
733 es->text_width = max(es->text_width, current_line->width);
734 current_position += current_line->length;
735 previous_line = current_line;
737 /* Discard data for non-visible lines. It will be calculated as needed */
738 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
739 EDIT_InvalidateUniscribeData_linedef(current_line);
741 current_line = current_line->next;
742 line_index++;
743 } while (previous_line->ending != END_0);
745 /* Finish adjusting line indexes by delta or remove hanging lines */
746 if (previous_line->ending == END_0)
748 LINEDEF *pnext = NULL;
750 previous_line->next = NULL;
751 while (current_line)
753 pnext = current_line->next;
754 EDIT_InvalidateUniscribeData_linedef(current_line);
755 HeapFree(GetProcessHeap(), 0, current_line);
756 current_line = pnext;
757 es->line_count--;
760 else if (delta != 0)
762 while (current_line)
764 current_line->index += delta;
765 current_line = current_line->next;
769 /* Calculate rest of modification rectangle */
770 if (hrgn)
772 HRGN tmphrgn;
774 * We calculate two rectangles. One for the first line which may have
775 * an indent with respect to the format rect. The other is a format-width
776 * rectangle that spans the rest of the lines that changed or moved.
778 rc.top = es->format_rect.top + nstart_line * es->line_height -
779 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
780 rc.bottom = rc.top + es->line_height;
781 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
782 rc.left = es->format_rect.left;
783 else
784 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
785 rc.right = es->format_rect.right;
786 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
788 rc.top = rc.bottom;
789 rc.left = es->format_rect.left;
790 rc.right = es->format_rect.right;
792 * If lines were added or removed we must re-paint the remainder of the
793 * lines since the remaining lines were either shifted up or down.
795 if (line_count < es->line_count) /* We added lines */
796 rc.bottom = es->line_count * es->line_height;
797 else if (line_count > es->line_count) /* We removed lines */
798 rc.bottom = line_count * es->line_height;
799 else
800 rc.bottom = line_index * es->line_height;
801 rc.bottom += es->format_rect.top;
802 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
803 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
804 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
805 DeleteObject(tmphrgn);
809 /*********************************************************************
811 * EDIT_CalcLineWidth_SL
814 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
816 EDIT_UpdateUniscribeData(es, NULL, 0);
817 if (es->ssa)
819 const SIZE *size;
820 size = ScriptString_pSize(es->ssa);
821 es->text_width = size->cx;
823 else
824 es->text_width = 0;
827 /*********************************************************************
829 * EDIT_CharFromPos
831 * Beware: This is not the function called on EM_CHARFROMPOS
832 * The position _can_ be outside the formatting / client
833 * rectangle
834 * The return value is only the character index
837 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
839 INT index;
841 if (es->style & ES_MULTILINE) {
842 int trailing;
843 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
844 INT line_index = 0;
845 LINEDEF *line_def = es->first_line_def;
846 EDIT_UpdateUniscribeData(es, NULL, line);
847 while ((line > 0) && line_def->next) {
848 line_index += line_def->length;
849 line_def = line_def->next;
850 line--;
853 x += es->x_offset - es->format_rect.left;
854 if (es->style & ES_RIGHT)
855 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
856 else if (es->style & ES_CENTER)
857 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
858 if (x >= line_def->width) {
859 if (after_wrap)
860 *after_wrap = (line_def->ending == END_WRAP);
861 return line_index + line_def->net_length;
863 if (x <= 0 || !line_def->ssa) {
864 if (after_wrap)
865 *after_wrap = FALSE;
866 return line_index;
869 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
870 if (trailing) index++;
871 index += line_index;
872 if (after_wrap)
873 *after_wrap = ((index == line_index + line_def->net_length) &&
874 (line_def->ending == END_WRAP));
875 } else {
876 INT xoff = 0;
877 INT trailing;
878 if (after_wrap)
879 *after_wrap = FALSE;
880 x -= es->format_rect.left;
881 if (!x)
882 return es->x_offset;
884 if (!es->x_offset)
886 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
887 if (es->style & ES_RIGHT)
888 x -= indent;
889 else if (es->style & ES_CENTER)
890 x -= indent / 2;
893 EDIT_UpdateUniscribeData(es, NULL, 0);
894 if (es->x_offset)
896 if (es->ssa)
898 if (es->x_offset>= get_text_length(es))
900 const SIZE *size;
901 size = ScriptString_pSize(es->ssa);
902 xoff = size->cx;
904 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
906 else
907 xoff = 0;
909 if (x < 0)
911 if (x + xoff > 0 || !es->ssa)
913 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
914 if (trailing) index++;
916 else
917 index = 0;
919 else
921 if (x)
923 const SIZE *size = NULL;
924 if (es->ssa)
925 size = ScriptString_pSize(es->ssa);
926 if (!size)
927 index = 0;
928 else if (x > size->cx)
929 index = get_text_length(es);
930 else if (es->ssa)
932 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
933 if (trailing) index++;
935 else
936 index = 0;
938 else
939 index = es->x_offset;
942 return index;
946 /*********************************************************************
948 * EDIT_ConfinePoint
950 * adjusts the point to be within the formatting rectangle
951 * (so CharFromPos returns the nearest _visible_ character)
954 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
956 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
957 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
961 /*********************************************************************
963 * EM_LINEFROMCHAR
966 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
968 INT line;
969 LINEDEF *line_def;
971 if (!(es->style & ES_MULTILINE))
972 return 0;
973 if (index > (INT)get_text_length(es))
974 return es->line_count - 1;
975 if (index == -1)
976 index = min(es->selection_start, es->selection_end);
978 line = 0;
979 line_def = es->first_line_def;
980 index -= line_def->length;
981 while ((index >= 0) && line_def->next) {
982 line++;
983 line_def = line_def->next;
984 index -= line_def->length;
986 return line;
990 /*********************************************************************
992 * EM_LINEINDEX
995 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
997 INT line_index;
998 const LINEDEF *line_def;
1000 if (!(es->style & ES_MULTILINE))
1001 return 0;
1002 if (line >= es->line_count)
1003 return -1;
1005 line_index = 0;
1006 line_def = es->first_line_def;
1007 if (line == -1) {
1008 INT index = es->selection_end - line_def->length;
1009 while ((index >= 0) && line_def->next) {
1010 line_index += line_def->length;
1011 line_def = line_def->next;
1012 index -= line_def->length;
1014 } else {
1015 while (line > 0) {
1016 line_index += line_def->length;
1017 line_def = line_def->next;
1018 line--;
1021 return line_index;
1025 /*********************************************************************
1027 * EM_LINELENGTH
1030 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1032 LINEDEF *line_def;
1034 if (!(es->style & ES_MULTILINE))
1035 return get_text_length(es);
1037 if (index == -1) {
1038 /* get the number of remaining non-selected chars of selected lines */
1039 INT32 l; /* line number */
1040 INT32 li; /* index of first char in line */
1041 INT32 count;
1042 l = EDIT_EM_LineFromChar(es, es->selection_start);
1043 /* # chars before start of selection area */
1044 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1045 l = EDIT_EM_LineFromChar(es, es->selection_end);
1046 /* # chars after end of selection */
1047 li = EDIT_EM_LineIndex(es, l);
1048 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1049 return count;
1051 line_def = es->first_line_def;
1052 index -= line_def->length;
1053 while ((index >= 0) && line_def->next) {
1054 line_def = line_def->next;
1055 index -= line_def->length;
1057 return line_def->net_length;
1061 /*********************************************************************
1063 * EM_POSFROMCHAR
1066 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1068 INT len = get_text_length(es);
1069 INT l;
1070 INT li;
1071 INT x = 0;
1072 INT y = 0;
1073 INT w;
1074 INT lw;
1075 LINEDEF *line_def;
1077 index = min(index, len);
1078 if (es->style & ES_MULTILINE) {
1079 l = EDIT_EM_LineFromChar(es, index);
1080 EDIT_UpdateUniscribeData(es, NULL, l);
1082 y = (l - es->y_offset) * es->line_height;
1083 li = EDIT_EM_LineIndex(es, l);
1084 if (after_wrap && (li == index) && l) {
1085 INT l2 = l - 1;
1086 line_def = es->first_line_def;
1087 while (l2) {
1088 line_def = line_def->next;
1089 l2--;
1091 if (line_def->ending == END_WRAP) {
1092 l--;
1093 y -= es->line_height;
1094 li = EDIT_EM_LineIndex(es, l);
1098 line_def = es->first_line_def;
1099 while (line_def->index != li)
1100 line_def = line_def->next;
1102 lw = line_def->width;
1103 w = es->format_rect.right - es->format_rect.left;
1104 if (line_def->ssa)
1106 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1107 x -= es->x_offset;
1109 else
1110 x = es->x_offset;
1112 if (es->style & ES_RIGHT)
1113 x = w - (lw - x);
1114 else if (es->style & ES_CENTER)
1115 x += (w - lw) / 2;
1116 } else {
1117 INT xoff = 0;
1118 INT xi = 0;
1119 EDIT_UpdateUniscribeData(es, NULL, 0);
1120 if (es->x_offset)
1122 if (es->ssa)
1124 if (es->x_offset >= get_text_length(es))
1126 int leftover = es->x_offset - get_text_length(es);
1127 if (es->ssa)
1129 const SIZE *size;
1130 size = ScriptString_pSize(es->ssa);
1131 xoff = size->cx;
1133 else
1134 xoff = 0;
1135 xoff += es->char_width * leftover;
1137 else
1138 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1140 else
1141 xoff = 0;
1143 if (index)
1145 if (index >= get_text_length(es))
1147 if (es->ssa)
1149 const SIZE *size;
1150 size = ScriptString_pSize(es->ssa);
1151 xi = size->cx;
1153 else
1154 xi = 0;
1156 else if (es->ssa)
1157 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1158 else
1159 xi = 0;
1161 x = xi - xoff;
1163 if (index >= es->x_offset) {
1164 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1166 w = es->format_rect.right - es->format_rect.left;
1167 if (w > es->text_width)
1169 if (es->style & ES_RIGHT)
1170 x += w - es->text_width;
1171 else if (es->style & ES_CENTER)
1172 x += (w - es->text_width) / 2;
1176 y = 0;
1178 x += es->format_rect.left;
1179 y += es->format_rect.top;
1180 return MAKELONG((INT16)x, (INT16)y);
1184 /*********************************************************************
1186 * EDIT_GetLineRect
1188 * Calculates the bounding rectangle for a line from a starting
1189 * column to an ending column.
1192 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1194 SCRIPT_STRING_ANALYSIS ssa;
1195 INT line_index = 0;
1196 INT pt1, pt2, pt3;
1198 if (es->style & ES_MULTILINE)
1200 const LINEDEF *line_def = NULL;
1201 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1202 if (line >= es->line_count)
1203 return;
1205 line_def = es->first_line_def;
1206 if (line == -1) {
1207 INT index = es->selection_end - line_def->length;
1208 while ((index >= 0) && line_def->next) {
1209 line_index += line_def->length;
1210 line_def = line_def->next;
1211 index -= line_def->length;
1213 } else {
1214 while (line > 0) {
1215 line_index += line_def->length;
1216 line_def = line_def->next;
1217 line--;
1220 ssa = line_def->ssa;
1222 else
1224 line_index = 0;
1225 rc->top = es->format_rect.top;
1226 ssa = es->ssa;
1229 rc->bottom = rc->top + es->line_height;
1230 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1231 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1232 if (ssa)
1234 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1235 pt3+=es->format_rect.left;
1237 else pt3 = pt1;
1238 rc->right = max(max(pt1 , pt2),pt3);
1239 rc->left = min(min(pt1, pt2),pt3);
1243 static inline void text_buffer_changed(EDITSTATE *es)
1245 es->text_length = (UINT)-1;
1247 HeapFree( GetProcessHeap(), 0, es->logAttr );
1248 es->logAttr = NULL;
1249 EDIT_InvalidateUniscribeData(es);
1252 /*********************************************************************
1253 * EDIT_LockBuffer
1256 static void EDIT_LockBuffer(EDITSTATE *es)
1258 if (!es->text) {
1260 if(!es->hloc32W) return;
1262 if(es->hloc32A)
1264 CHAR *textA = LocalLock(es->hloc32A);
1265 HLOCAL hloc32W_new;
1266 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1267 if(countW_new > es->buffer_size + 1)
1269 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1270 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1271 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1272 if(hloc32W_new)
1274 es->hloc32W = hloc32W_new;
1275 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1276 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1278 else
1279 WARN("FAILED! Will synchronize partially\n");
1281 es->text = LocalLock(es->hloc32W);
1282 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1283 LocalUnlock(es->hloc32A);
1285 else es->text = LocalLock(es->hloc32W);
1287 es->lock_count++;
1291 /*********************************************************************
1293 * EDIT_UnlockBuffer
1296 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1298 /* Edit window might be already destroyed */
1299 if(!IsWindow(es->hwndSelf))
1301 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1302 return;
1305 if (!es->lock_count) {
1306 ERR("lock_count == 0 ... please report\n");
1307 return;
1309 if (!es->text) {
1310 ERR("es->text == 0 ... please report\n");
1311 return;
1313 if (force || (es->lock_count == 1)) {
1314 if (es->hloc32W) {
1315 UINT countA = 0;
1316 UINT countW = get_text_length(es) + 1;
1318 if(es->hloc32A)
1320 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1321 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1322 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1323 countA = LocalSize(es->hloc32A);
1324 if(countA_new > countA)
1326 HLOCAL hloc32A_new;
1327 UINT alloc_size = ROUND_TO_GROW(countA_new);
1328 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1329 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1330 if(hloc32A_new)
1332 es->hloc32A = hloc32A_new;
1333 countA = LocalSize(hloc32A_new);
1334 TRACE("Real new size %d bytes\n", countA);
1336 else
1337 WARN("FAILED! Will synchronize partially\n");
1339 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1340 LocalLock(es->hloc32A), countA, NULL, NULL);
1341 LocalUnlock(es->hloc32A);
1344 LocalUnlock(es->hloc32W);
1345 es->text = NULL;
1347 else {
1348 ERR("no buffer ... please report\n");
1349 return;
1352 es->lock_count--;
1356 /*********************************************************************
1358 * EDIT_MakeFit
1360 * Try to fit size + 1 characters in the buffer.
1362 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1364 HLOCAL hNew32W;
1366 if (size <= es->buffer_size)
1367 return TRUE;
1369 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1371 /* Force edit to unlock its buffer. es->text now NULL */
1372 EDIT_UnlockBuffer(es, TRUE);
1374 if (es->hloc32W) {
1375 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1376 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1377 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1378 es->hloc32W = hNew32W;
1379 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1383 EDIT_LockBuffer(es);
1385 if (es->buffer_size < size) {
1386 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1387 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1388 return FALSE;
1389 } else {
1390 TRACE("We now have %d+1\n", es->buffer_size);
1391 return TRUE;
1396 /*********************************************************************
1398 * EDIT_MakeUndoFit
1400 * Try to fit size + 1 bytes in the undo buffer.
1403 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1405 UINT alloc_size;
1407 if (size <= es->undo_buffer_size)
1408 return TRUE;
1410 TRACE("trying to ReAlloc to %d+1\n", size);
1412 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1413 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1414 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1415 return TRUE;
1417 else
1419 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1420 return FALSE;
1425 /*********************************************************************
1427 * EDIT_UpdateTextRegion
1430 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1432 if (es->flags & EF_UPDATE) {
1433 es->flags &= ~EF_UPDATE;
1434 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1436 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1440 /*********************************************************************
1442 * EDIT_UpdateText
1445 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1447 if (es->flags & EF_UPDATE) {
1448 es->flags &= ~EF_UPDATE;
1449 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1451 InvalidateRect(es->hwndSelf, rc, bErase);
1454 /*********************************************************************
1456 * EDIT_SL_InvalidateText
1458 * Called from EDIT_InvalidateText().
1459 * Does the job for single-line controls only.
1462 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1464 RECT line_rect;
1465 RECT rc;
1467 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1468 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1469 EDIT_UpdateText(es, &rc, TRUE);
1472 /*********************************************************************
1474 * EDIT_ML_InvalidateText
1476 * Called from EDIT_InvalidateText().
1477 * Does the job for multi-line controls only.
1480 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1482 INT vlc = get_vertical_line_count(es);
1483 INT sl = EDIT_EM_LineFromChar(es, start);
1484 INT el = EDIT_EM_LineFromChar(es, end);
1485 INT sc;
1486 INT ec;
1487 RECT rc1;
1488 RECT rcWnd;
1489 RECT rcLine;
1490 RECT rcUpdate;
1491 INT l;
1493 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1494 return;
1496 sc = start - EDIT_EM_LineIndex(es, sl);
1497 ec = end - EDIT_EM_LineIndex(es, el);
1498 if (sl < es->y_offset) {
1499 sl = es->y_offset;
1500 sc = 0;
1502 if (el > es->y_offset + vlc) {
1503 el = es->y_offset + vlc;
1504 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1506 GetClientRect(es->hwndSelf, &rc1);
1507 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1508 if (sl == el) {
1509 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1510 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1511 EDIT_UpdateText(es, &rcUpdate, TRUE);
1512 } else {
1513 EDIT_GetLineRect(es, sl, sc,
1514 EDIT_EM_LineLength(es,
1515 EDIT_EM_LineIndex(es, sl)),
1516 &rcLine);
1517 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1518 EDIT_UpdateText(es, &rcUpdate, TRUE);
1519 for (l = sl + 1 ; l < el ; l++) {
1520 EDIT_GetLineRect(es, l, 0,
1521 EDIT_EM_LineLength(es,
1522 EDIT_EM_LineIndex(es, l)),
1523 &rcLine);
1524 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1525 EDIT_UpdateText(es, &rcUpdate, TRUE);
1527 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1528 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1529 EDIT_UpdateText(es, &rcUpdate, TRUE);
1534 /*********************************************************************
1536 * EDIT_InvalidateText
1538 * Invalidate the text from offset start up to, but not including,
1539 * offset end. Useful for (re)painting the selection.
1540 * Regions outside the linewidth are not invalidated.
1541 * end == -1 means end == TextLength.
1542 * start and end need not be ordered.
1545 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1547 if (end == start)
1548 return;
1550 if (end == -1)
1551 end = get_text_length(es);
1553 if (end < start) {
1554 INT tmp = start;
1555 start = end;
1556 end = tmp;
1559 if (es->style & ES_MULTILINE)
1560 EDIT_ML_InvalidateText(es, start, end);
1561 else
1562 EDIT_SL_InvalidateText(es, start, end);
1566 /*********************************************************************
1568 * EDIT_EM_SetSel
1570 * note: unlike the specs say: the order of start and end
1571 * _is_ preserved in Windows. (i.e. start can be > end)
1572 * In other words: this handler is OK
1575 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1577 UINT old_start = es->selection_start;
1578 UINT old_end = es->selection_end;
1579 UINT len = get_text_length(es);
1581 if (start == (UINT)-1) {
1582 start = es->selection_end;
1583 end = es->selection_end;
1584 } else {
1585 start = min(start, len);
1586 end = min(end, len);
1588 es->selection_start = start;
1589 es->selection_end = end;
1590 if (after_wrap)
1591 es->flags |= EF_AFTER_WRAP;
1592 else
1593 es->flags &= ~EF_AFTER_WRAP;
1594 /* Compute the necessary invalidation region. */
1595 /* Note that we don't need to invalidate regions which have
1596 * "never" been selected, or those which are "still" selected.
1597 * In fact, every time we hit a selection boundary, we can
1598 * *toggle* whether we need to invalidate. Thus we can optimize by
1599 * *sorting* the interval endpoints. Let's assume that we sort them
1600 * in this order:
1601 * start <= end <= old_start <= old_end
1602 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1603 * in 5 comparisons; i.e. it is impossible to do better than the
1604 * following: */
1605 ORDER_UINT(end, old_end);
1606 ORDER_UINT(start, old_start);
1607 ORDER_UINT(old_start, old_end);
1608 ORDER_UINT(start, end);
1609 /* Note that at this point 'end' and 'old_start' are not in order, but
1610 * start is definitely the min. and old_end is definitely the max. */
1611 if (end != old_start)
1614 * One can also do
1615 * ORDER_UINT32(end, old_start);
1616 * EDIT_InvalidateText(es, start, end);
1617 * EDIT_InvalidateText(es, old_start, old_end);
1618 * in place of the following if statement.
1619 * (That would complete the optimal five-comparison four-element sort.)
1621 if (old_start > end )
1623 EDIT_InvalidateText(es, start, end);
1624 EDIT_InvalidateText(es, old_start, old_end);
1626 else
1628 EDIT_InvalidateText(es, start, old_start);
1629 EDIT_InvalidateText(es, end, old_end);
1632 else EDIT_InvalidateText(es, start, old_end);
1636 /*********************************************************************
1638 * EDIT_UpdateScrollInfo
1641 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1643 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1645 SCROLLINFO si;
1646 si.cbSize = sizeof(SCROLLINFO);
1647 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1648 si.nMin = 0;
1649 si.nMax = es->line_count - 1;
1650 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1651 si.nPos = es->y_offset;
1652 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1653 si.nMin, si.nMax, si.nPage, si.nPos);
1654 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1657 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1659 SCROLLINFO si;
1660 si.cbSize = sizeof(SCROLLINFO);
1661 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1662 si.nMin = 0;
1663 si.nMax = es->text_width - 1;
1664 si.nPage = es->format_rect.right - es->format_rect.left;
1665 si.nPos = es->x_offset;
1666 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1667 si.nMin, si.nMax, si.nPage, si.nPos);
1668 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1673 /*********************************************************************
1675 * EDIT_EM_LineScroll_internal
1677 * Version of EDIT_EM_LineScroll for internal use.
1678 * It doesn't refuse if ES_MULTILINE is set and assumes that
1679 * dx is in pixels, dy - in lines.
1682 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1684 INT nyoff;
1685 INT x_offset_in_pixels;
1686 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1687 es->line_height;
1689 if (es->style & ES_MULTILINE)
1691 x_offset_in_pixels = es->x_offset;
1693 else
1695 dy = 0;
1696 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1699 if (-dx > x_offset_in_pixels)
1700 dx = -x_offset_in_pixels;
1701 if (dx > es->text_width - x_offset_in_pixels)
1702 dx = es->text_width - x_offset_in_pixels;
1703 nyoff = max(0, es->y_offset + dy);
1704 if (nyoff >= es->line_count - lines_per_page)
1705 nyoff = max(0, es->line_count - lines_per_page);
1706 dy = (es->y_offset - nyoff) * es->line_height;
1707 if (dx || dy) {
1708 RECT rc1;
1709 RECT rc;
1711 es->y_offset = nyoff;
1712 if(es->style & ES_MULTILINE)
1713 es->x_offset += dx;
1714 else
1715 es->x_offset += dx / es->char_width;
1717 GetClientRect(es->hwndSelf, &rc1);
1718 IntersectRect(&rc, &rc1, &es->format_rect);
1719 ScrollWindowEx(es->hwndSelf, -dx, dy,
1720 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1721 /* force scroll info update */
1722 EDIT_UpdateScrollInfo(es);
1724 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1725 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1726 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1727 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1728 return TRUE;
1731 /*********************************************************************
1733 * EM_LINESCROLL
1735 * NOTE: dx is in average character widths, dy - in lines;
1738 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1740 if (!(es->style & ES_MULTILINE))
1741 return FALSE;
1743 dx *= es->char_width;
1744 return EDIT_EM_LineScroll_internal(es, dx, dy);
1748 /*********************************************************************
1750 * EM_SCROLL
1753 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1755 INT dy;
1757 if (!(es->style & ES_MULTILINE))
1758 return (LRESULT)FALSE;
1760 dy = 0;
1762 switch (action) {
1763 case SB_LINEUP:
1764 if (es->y_offset)
1765 dy = -1;
1766 break;
1767 case SB_LINEDOWN:
1768 if (es->y_offset < es->line_count - 1)
1769 dy = 1;
1770 break;
1771 case SB_PAGEUP:
1772 if (es->y_offset)
1773 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1774 break;
1775 case SB_PAGEDOWN:
1776 if (es->y_offset < es->line_count - 1)
1777 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1778 break;
1779 default:
1780 return (LRESULT)FALSE;
1782 if (dy) {
1783 INT vlc = get_vertical_line_count(es);
1784 /* check if we are going to move too far */
1785 if(es->y_offset + dy > es->line_count - vlc)
1786 dy = max(es->line_count - vlc, 0) - es->y_offset;
1788 /* Notification is done in EDIT_EM_LineScroll */
1789 if(dy) {
1790 EDIT_EM_LineScroll(es, 0, dy);
1791 return MAKELONG(dy, TRUE);
1795 return (LRESULT)FALSE;
1799 /*********************************************************************
1801 * EDIT_SetCaretPos
1804 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1805 BOOL after_wrap)
1807 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1808 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1809 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 if(es->flags & EF_FOCUSED)
1879 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1883 /*********************************************************************
1885 * EDIT_MoveBackward
1888 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1890 INT e = es->selection_end;
1892 if (e) {
1893 e--;
1894 if ((es->style & ES_MULTILINE) && e &&
1895 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1896 e--;
1897 if (e && (es->text[e - 1] == '\r'))
1898 e--;
1901 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1902 EDIT_EM_ScrollCaret(es);
1906 /*********************************************************************
1908 * EDIT_MoveDown_ML
1910 * Only for multi line controls
1911 * Move the caret one line down, on a column with the nearest
1912 * x coordinate on the screen (might be a different column).
1915 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1917 INT s = es->selection_start;
1918 INT e = es->selection_end;
1919 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1920 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1921 INT x = (short)LOWORD(pos);
1922 INT y = (short)HIWORD(pos);
1924 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1925 if (!extend)
1926 s = e;
1927 EDIT_EM_SetSel(es, s, e, after_wrap);
1928 EDIT_EM_ScrollCaret(es);
1932 /*********************************************************************
1934 * EDIT_MoveEnd
1937 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1939 BOOL after_wrap = FALSE;
1940 INT e;
1942 /* Pass a high value in x to make sure of receiving the end of the line */
1943 if (!ctrl && (es->style & ES_MULTILINE))
1944 e = EDIT_CharFromPos(es, 0x3fffffff,
1945 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1946 else
1947 e = get_text_length(es);
1948 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1949 EDIT_EM_ScrollCaret(es);
1953 /*********************************************************************
1955 * EDIT_MoveForward
1958 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1960 INT e = es->selection_end;
1962 if (es->text[e]) {
1963 e++;
1964 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1965 if (es->text[e] == '\n')
1966 e++;
1967 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1968 e += 2;
1971 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1972 EDIT_EM_ScrollCaret(es);
1976 /*********************************************************************
1978 * EDIT_MoveHome
1980 * Home key: move to beginning of line.
1983 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1985 INT e;
1987 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1988 if (!ctrl && (es->style & ES_MULTILINE))
1989 e = EDIT_CharFromPos(es, -es->x_offset,
1990 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1991 else
1992 e = 0;
1993 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1994 EDIT_EM_ScrollCaret(es);
1998 /*********************************************************************
2000 * EDIT_MovePageDown_ML
2002 * Only for multi line controls
2003 * Move the caret one page down, on a column with the nearest
2004 * x coordinate on the screen (might be a different column).
2007 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2009 INT s = es->selection_start;
2010 INT e = es->selection_end;
2011 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2012 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2013 INT x = (short)LOWORD(pos);
2014 INT y = (short)HIWORD(pos);
2016 e = EDIT_CharFromPos(es, x,
2017 y + (es->format_rect.bottom - es->format_rect.top),
2018 &after_wrap);
2019 if (!extend)
2020 s = e;
2021 EDIT_EM_SetSel(es, s, e, after_wrap);
2022 EDIT_EM_ScrollCaret(es);
2026 /*********************************************************************
2028 * EDIT_MovePageUp_ML
2030 * Only for multi line controls
2031 * Move the caret one page up, on a column with the nearest
2032 * x coordinate on the screen (might be a different column).
2035 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2037 INT s = es->selection_start;
2038 INT e = es->selection_end;
2039 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2040 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2041 INT x = (short)LOWORD(pos);
2042 INT y = (short)HIWORD(pos);
2044 e = EDIT_CharFromPos(es, x,
2045 y - (es->format_rect.bottom - es->format_rect.top),
2046 &after_wrap);
2047 if (!extend)
2048 s = e;
2049 EDIT_EM_SetSel(es, s, e, after_wrap);
2050 EDIT_EM_ScrollCaret(es);
2054 /*********************************************************************
2056 * EDIT_MoveUp_ML
2058 * Only for multi line controls
2059 * Move the caret one line up, on a column with the nearest
2060 * x coordinate on the screen (might be a different column).
2063 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2065 INT s = es->selection_start;
2066 INT e = es->selection_end;
2067 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2068 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2069 INT x = (short)LOWORD(pos);
2070 INT y = (short)HIWORD(pos);
2072 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2073 if (!extend)
2074 s = e;
2075 EDIT_EM_SetSel(es, s, e, after_wrap);
2076 EDIT_EM_ScrollCaret(es);
2080 /*********************************************************************
2082 * EDIT_MoveWordBackward
2085 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2087 INT s = es->selection_start;
2088 INT e = es->selection_end;
2089 INT l;
2090 INT ll;
2091 INT li;
2093 l = EDIT_EM_LineFromChar(es, e);
2094 ll = EDIT_EM_LineLength(es, e);
2095 li = EDIT_EM_LineIndex(es, l);
2096 if (e - li == 0) {
2097 if (l) {
2098 li = EDIT_EM_LineIndex(es, l - 1);
2099 e = li + EDIT_EM_LineLength(es, li);
2101 } else {
2102 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2104 if (!extend)
2105 s = e;
2106 EDIT_EM_SetSel(es, s, e, FALSE);
2107 EDIT_EM_ScrollCaret(es);
2111 /*********************************************************************
2113 * EDIT_MoveWordForward
2116 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2118 INT s = es->selection_start;
2119 INT e = es->selection_end;
2120 INT l;
2121 INT ll;
2122 INT li;
2124 l = EDIT_EM_LineFromChar(es, e);
2125 ll = EDIT_EM_LineLength(es, e);
2126 li = EDIT_EM_LineIndex(es, l);
2127 if (e - li == ll) {
2128 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2129 e = EDIT_EM_LineIndex(es, l + 1);
2130 } else {
2131 e = li + EDIT_CallWordBreakProc(es,
2132 li, e - li + 1, ll, WB_RIGHT);
2134 if (!extend)
2135 s = e;
2136 EDIT_EM_SetSel(es, s, e, FALSE);
2137 EDIT_EM_ScrollCaret(es);
2141 /*********************************************************************
2143 * EDIT_PaintText
2146 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2148 COLORREF BkColor;
2149 COLORREF TextColor;
2150 LOGFONTW underline_font;
2151 HFONT hUnderline = 0;
2152 HFONT old_font = 0;
2153 INT ret;
2154 INT li;
2155 INT BkMode;
2156 SIZE size;
2158 if (!count)
2159 return 0;
2160 BkMode = GetBkMode(dc);
2161 BkColor = GetBkColor(dc);
2162 TextColor = GetTextColor(dc);
2163 if (rev) {
2164 if (es->composition_len == 0)
2166 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2167 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2168 SetBkMode( dc, OPAQUE);
2170 else
2172 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2173 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2174 underline_font.lfUnderline = TRUE;
2175 hUnderline = CreateFontIndirectW(&underline_font);
2176 old_font = SelectObject(dc,hUnderline);
2179 li = EDIT_EM_LineIndex(es, line);
2180 if (es->style & ES_MULTILINE) {
2181 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2182 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2183 } else {
2184 TextOutW(dc, x, y, es->text + li + col, count);
2185 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2186 ret = size.cx;
2188 if (rev) {
2189 if (es->composition_len == 0)
2191 SetBkColor(dc, BkColor);
2192 SetTextColor(dc, TextColor);
2193 SetBkMode( dc, BkMode);
2195 else
2197 if (old_font)
2198 SelectObject(dc,old_font);
2199 if (hUnderline)
2200 DeleteObject(hUnderline);
2203 return ret;
2207 /*********************************************************************
2209 * EDIT_PaintLine
2212 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2214 INT s = 0;
2215 INT e = 0;
2216 INT li = 0;
2217 INT ll = 0;
2218 INT x;
2219 INT y;
2220 LRESULT pos;
2221 SCRIPT_STRING_ANALYSIS ssa;
2223 if (es->style & ES_MULTILINE) {
2224 INT vlc = get_vertical_line_count(es);
2226 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2227 return;
2228 } else if (line)
2229 return;
2231 TRACE("line=%d\n", line);
2233 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2234 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2235 x = (short)LOWORD(pos);
2236 y = (short)HIWORD(pos);
2238 if (es->style & ES_MULTILINE)
2240 int line_idx = line;
2241 x = -es->x_offset;
2242 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2244 LINEDEF *line_def = es->first_line_def;
2245 int w, lw;
2247 while (line_def && line_idx)
2249 line_def = line_def->next;
2250 line_idx--;
2252 w = es->format_rect.right - es->format_rect.left;
2253 lw = line_def->width;
2255 if (es->style & ES_RIGHT)
2256 x = w - (lw - x);
2257 else if (es->style & ES_CENTER)
2258 x += (w - lw) / 2;
2260 x += es->format_rect.left;
2263 if (rev)
2265 li = EDIT_EM_LineIndex(es, line);
2266 ll = EDIT_EM_LineLength(es, li);
2267 s = min(es->selection_start, es->selection_end);
2268 e = max(es->selection_start, es->selection_end);
2269 s = min(li + ll, max(li, s));
2270 e = min(li + ll, max(li, e));
2273 if (ssa)
2274 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2275 else if (rev && (s != e) &&
2276 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2277 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2278 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2279 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2280 } else
2281 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2285 /*********************************************************************
2287 * EDIT_AdjustFormatRect
2289 * Adjusts the format rectangle for the current font and the
2290 * current client rectangle.
2293 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2295 RECT ClientRect;
2297 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2298 if (es->style & ES_MULTILINE)
2300 INT fw, vlc, max_x_offset, max_y_offset;
2302 vlc = get_vertical_line_count(es);
2303 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2305 /* correct es->x_offset */
2306 fw = es->format_rect.right - es->format_rect.left;
2307 max_x_offset = es->text_width - fw;
2308 if(max_x_offset < 0) max_x_offset = 0;
2309 if(es->x_offset > max_x_offset)
2310 es->x_offset = max_x_offset;
2312 /* correct es->y_offset */
2313 max_y_offset = es->line_count - vlc;
2314 if(max_y_offset < 0) max_y_offset = 0;
2315 if(es->y_offset > max_y_offset)
2316 es->y_offset = max_y_offset;
2318 /* force scroll info update */
2319 EDIT_UpdateScrollInfo(es);
2321 else
2322 /* Windows doesn't care to fix text placement for SL controls */
2323 es->format_rect.bottom = es->format_rect.top + es->line_height;
2325 /* Always stay within the client area */
2326 GetClientRect(es->hwndSelf, &ClientRect);
2327 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2329 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2330 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2332 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2336 /*********************************************************************
2338 * EDIT_SetRectNP
2340 * note: this is not (exactly) the handler called on EM_SETRECTNP
2341 * it is also used to set the rect of a single line control
2344 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2346 LONG_PTR ExStyle;
2347 INT bw, bh;
2348 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2350 CopyRect(&es->format_rect, rc);
2352 if (ExStyle & WS_EX_CLIENTEDGE) {
2353 es->format_rect.left++;
2354 es->format_rect.right--;
2356 if (es->format_rect.bottom - es->format_rect.top
2357 >= es->line_height + 2)
2359 es->format_rect.top++;
2360 es->format_rect.bottom--;
2363 else if (es->style & WS_BORDER) {
2364 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2365 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2366 InflateRect(&es->format_rect, -bw, 0);
2367 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2368 InflateRect(&es->format_rect, 0, -bh);
2371 es->format_rect.left += es->left_margin;
2372 es->format_rect.right -= es->right_margin;
2373 EDIT_AdjustFormatRect(es);
2377 /*********************************************************************
2379 * EM_CHARFROMPOS
2381 * returns line number (not index) in high-order word of result.
2382 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2383 * to Richedit, not to the edit control. Original documentation is valid.
2384 * FIXME: do the specs mean to return -1 if outside client area or
2385 * if outside formatting rectangle ???
2388 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2390 POINT pt;
2391 RECT rc;
2392 INT index;
2394 pt.x = x;
2395 pt.y = y;
2396 GetClientRect(es->hwndSelf, &rc);
2397 if (!PtInRect(&rc, pt))
2398 return -1;
2400 index = EDIT_CharFromPos(es, x, y, NULL);
2401 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2405 /*********************************************************************
2407 * EM_FMTLINES
2409 * Enable or disable soft breaks.
2411 * This means: insert or remove the soft linebreak character (\r\r\n).
2412 * Take care to check if the text still fits the buffer after insertion.
2413 * If not, notify with EN_ERRSPACE.
2416 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2418 es->flags &= ~EF_USE_SOFTBRK;
2419 if (add_eol) {
2420 es->flags |= EF_USE_SOFTBRK;
2421 FIXME("soft break enabled, not implemented\n");
2423 return add_eol;
2427 /*********************************************************************
2429 * EM_GETHANDLE
2431 * Hopefully this won't fire back at us.
2432 * We always start with a fixed buffer in the local heap.
2433 * Despite of the documentation says that the local heap is used
2434 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2435 * buffer on the local heap.
2438 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2440 HLOCAL hLocal;
2442 if (!(es->style & ES_MULTILINE))
2443 return 0;
2445 if(es->is_unicode)
2446 hLocal = es->hloc32W;
2447 else
2449 if(!es->hloc32A)
2451 CHAR *textA;
2452 UINT countA, alloc_size;
2453 TRACE("Allocating 32-bit ANSI alias buffer\n");
2454 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2455 alloc_size = ROUND_TO_GROW(countA);
2456 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2458 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2459 return 0;
2461 textA = LocalLock(es->hloc32A);
2462 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2463 LocalUnlock(es->hloc32A);
2465 hLocal = es->hloc32A;
2468 EDIT_UnlockBuffer(es, TRUE);
2470 /* The text buffer handle belongs to the app */
2471 es->hlocapp = hLocal;
2473 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2474 return hLocal;
2478 /*********************************************************************
2480 * EM_GETLINE
2483 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2485 LPWSTR src;
2486 INT line_len, dst_len;
2487 INT i;
2489 if (es->style & ES_MULTILINE) {
2490 if (line >= es->line_count)
2491 return 0;
2492 } else
2493 line = 0;
2494 i = EDIT_EM_LineIndex(es, line);
2495 src = es->text + i;
2496 line_len = EDIT_EM_LineLength(es, i);
2497 dst_len = *(WORD *)dst;
2498 if(unicode)
2500 if(dst_len <= line_len)
2502 memcpy(dst, src, dst_len * sizeof(WCHAR));
2503 return dst_len;
2505 else /* Append 0 if enough space */
2507 memcpy(dst, src, line_len * sizeof(WCHAR));
2508 dst[line_len] = 0;
2509 return line_len;
2512 else
2514 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2515 if(!ret && line_len) /* Insufficient buffer size */
2516 return dst_len;
2517 if(ret < dst_len) /* Append 0 if enough space */
2518 ((LPSTR)dst)[ret] = 0;
2519 return ret;
2524 /*********************************************************************
2526 * EM_GETSEL
2529 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2531 UINT s = es->selection_start;
2532 UINT e = es->selection_end;
2534 ORDER_UINT(s, e);
2535 if (start)
2536 *start = s;
2537 if (end)
2538 *end = e;
2539 return MAKELONG(s, e);
2543 /*********************************************************************
2545 * EM_REPLACESEL
2547 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2550 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2551 BOOL send_update, BOOL honor_limit)
2553 UINT tl = get_text_length(es);
2554 UINT utl;
2555 UINT s;
2556 UINT e;
2557 UINT i;
2558 UINT size;
2559 LPWSTR p;
2560 HRGN hrgn = 0;
2561 LPWSTR buf = NULL;
2562 UINT bufl;
2564 TRACE("%s, can_undo %d, send_update %d\n",
2565 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2567 s = es->selection_start;
2568 e = es->selection_end;
2570 EDIT_InvalidateUniscribeData(es);
2571 if ((s == e) && !strl)
2572 return;
2574 ORDER_UINT(s, e);
2576 size = tl - (e - s) + strl;
2577 if (!size)
2578 es->text_width = 0;
2580 /* Issue the EN_MAXTEXT notification and continue with replacing text
2581 * so that buffer limit is honored. */
2582 if ((honor_limit) && (size > es->buffer_limit)) {
2583 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2584 /* Buffer limit can be smaller than the actual length of text in combobox */
2585 if (es->buffer_limit < (tl - (e-s)))
2586 strl = 0;
2587 else
2588 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2591 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2592 return;
2594 if (e != s) {
2595 /* there is something to be deleted */
2596 TRACE("deleting stuff.\n");
2597 bufl = e - s;
2598 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2599 if (!buf) return;
2600 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2601 buf[bufl] = 0; /* ensure 0 termination */
2602 /* now delete */
2603 strcpyW(es->text + s, es->text + e);
2604 text_buffer_changed(es);
2606 if (strl) {
2607 /* there is an insertion */
2608 tl = get_text_length(es);
2609 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));
2610 for (p = es->text + tl ; p >= es->text + s ; p--)
2611 p[strl] = p[0];
2612 for (i = 0 , p = es->text + s ; i < strl ; i++)
2613 p[i] = lpsz_replace[i];
2614 if(es->style & ES_UPPERCASE)
2615 CharUpperBuffW(p, strl);
2616 else if(es->style & ES_LOWERCASE)
2617 CharLowerBuffW(p, strl);
2618 text_buffer_changed(es);
2620 if (es->style & ES_MULTILINE)
2622 INT st = min(es->selection_start, es->selection_end);
2623 INT vlc = get_vertical_line_count(es);
2625 hrgn = CreateRectRgn(0, 0, 0, 0);
2626 EDIT_BuildLineDefs_ML(es, st, st + strl,
2627 strl - abs(es->selection_end - es->selection_start), hrgn);
2628 /* if text is too long undo all changes */
2629 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2630 if (strl)
2631 strcpyW(es->text + e, es->text + e + strl);
2632 if (e != s)
2633 for (i = 0 , p = es->text ; i < e - s ; i++)
2634 p[i + s] = buf[i];
2635 text_buffer_changed(es);
2636 EDIT_BuildLineDefs_ML(es, s, e,
2637 abs(es->selection_end - es->selection_start) - strl, hrgn);
2638 strl = 0;
2639 e = s;
2640 hrgn = CreateRectRgn(0, 0, 0, 0);
2641 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2644 else {
2645 INT fw = es->format_rect.right - es->format_rect.left;
2646 EDIT_InvalidateUniscribeData(es);
2647 EDIT_CalcLineWidth_SL(es);
2648 /* remove chars that don't fit */
2649 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2650 while ((es->text_width > fw) && s + strl >= s) {
2651 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2652 strl--;
2653 es->text_length = -1;
2654 EDIT_InvalidateUniscribeData(es);
2655 EDIT_CalcLineWidth_SL(es);
2657 text_buffer_changed(es);
2658 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2662 if (e != s) {
2663 if (can_undo) {
2664 utl = strlenW(es->undo_text);
2665 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2666 /* undo-buffer is extended to the right */
2667 EDIT_MakeUndoFit(es, utl + e - s);
2668 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2669 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2670 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2671 /* undo-buffer is extended to the left */
2672 EDIT_MakeUndoFit(es, utl + e - s);
2673 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2674 p[e - s] = p[0];
2675 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2676 p[i] = buf[i];
2677 es->undo_position = s;
2678 } else {
2679 /* new undo-buffer */
2680 EDIT_MakeUndoFit(es, e - s);
2681 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2682 es->undo_text[e - s] = 0; /* ensure 0 termination */
2683 es->undo_position = s;
2685 /* any deletion makes the old insertion-undo invalid */
2686 es->undo_insert_count = 0;
2687 } else
2688 EDIT_EM_EmptyUndoBuffer(es);
2690 if (strl) {
2691 if (can_undo) {
2692 if ((s == es->undo_position) ||
2693 ((es->undo_insert_count) &&
2694 (s == es->undo_position + es->undo_insert_count)))
2696 * insertion is new and at delete position or
2697 * an extension to either left or right
2699 es->undo_insert_count += strl;
2700 else {
2701 /* new insertion undo */
2702 es->undo_position = s;
2703 es->undo_insert_count = strl;
2704 /* new insertion makes old delete-buffer invalid */
2705 *es->undo_text = '\0';
2707 } else
2708 EDIT_EM_EmptyUndoBuffer(es);
2711 HeapFree(GetProcessHeap(), 0, buf);
2713 s += strl;
2715 /* If text has been deleted and we're right or center aligned then scroll rightward */
2716 if (es->style & (ES_RIGHT | ES_CENTER))
2718 INT delta = strl - abs(es->selection_end - es->selection_start);
2720 if (delta < 0 && es->x_offset)
2722 if (abs(delta) > es->x_offset)
2723 es->x_offset = 0;
2724 else
2725 es->x_offset += delta;
2729 EDIT_EM_SetSel(es, s, s, FALSE);
2730 es->flags |= EF_MODIFIED;
2731 if (send_update) es->flags |= EF_UPDATE;
2732 if (hrgn)
2734 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2735 DeleteObject(hrgn);
2737 else
2738 EDIT_UpdateText(es, NULL, TRUE);
2740 EDIT_EM_ScrollCaret(es);
2742 /* force scroll info update */
2743 EDIT_UpdateScrollInfo(es);
2746 if(send_update || (es->flags & EF_UPDATE))
2748 es->flags &= ~EF_UPDATE;
2749 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2751 EDIT_InvalidateUniscribeData(es);
2755 /*********************************************************************
2757 * EM_SETHANDLE
2759 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2762 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2764 if (!(es->style & ES_MULTILINE))
2765 return;
2767 if (!hloc) {
2768 WARN("called with NULL handle\n");
2769 return;
2772 EDIT_UnlockBuffer(es, TRUE);
2774 if(es->is_unicode)
2776 if(es->hloc32A)
2778 LocalFree(es->hloc32A);
2779 es->hloc32A = NULL;
2781 es->hloc32W = hloc;
2783 else
2785 INT countW, countA;
2786 HLOCAL hloc32W_new;
2787 WCHAR *textW;
2788 CHAR *textA;
2790 countA = LocalSize(hloc);
2791 textA = LocalLock(hloc);
2792 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2793 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2795 ERR("Could not allocate new unicode buffer\n");
2796 return;
2798 textW = LocalLock(hloc32W_new);
2799 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2800 LocalUnlock(hloc32W_new);
2801 LocalUnlock(hloc);
2803 if(es->hloc32W)
2804 LocalFree(es->hloc32W);
2806 es->hloc32W = hloc32W_new;
2807 es->hloc32A = hloc;
2810 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2812 /* The text buffer handle belongs to the control */
2813 es->hlocapp = NULL;
2815 EDIT_LockBuffer(es);
2816 text_buffer_changed(es);
2818 es->x_offset = es->y_offset = 0;
2819 es->selection_start = es->selection_end = 0;
2820 EDIT_EM_EmptyUndoBuffer(es);
2821 es->flags &= ~EF_MODIFIED;
2822 es->flags &= ~EF_UPDATE;
2823 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2824 EDIT_UpdateText(es, NULL, TRUE);
2825 EDIT_EM_ScrollCaret(es);
2826 /* force scroll info update */
2827 EDIT_UpdateScrollInfo(es);
2831 /*********************************************************************
2833 * EM_SETLIMITTEXT
2835 * NOTE: this version currently implements WinNT limits
2838 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2840 if (!limit) limit = ~0u;
2841 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2842 es->buffer_limit = limit;
2846 /*********************************************************************
2848 * EM_SETMARGINS
2850 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2851 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2852 * margin according to the textmetrics of the current font.
2854 * When EC_USEFONTINFO is used in the non_cjk case the margins only
2855 * change if the edit control is equal to or larger than a certain
2856 * size. Though there is an exception for the empty client rect case
2857 * with small font sizes.
2859 static BOOL is_cjk(UINT charset)
2861 switch(charset)
2863 case SHIFTJIS_CHARSET:
2864 case HANGUL_CHARSET:
2865 case GB2312_CHARSET:
2866 case CHINESEBIG5_CHARSET:
2867 return TRUE;
2869 /* HANGUL_CHARSET is strange, though treated as CJK by Win 8, it is
2870 * not by other versions including Win 10. */
2871 return FALSE;
2874 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2875 WORD left, WORD right, BOOL repaint)
2877 TEXTMETRICW tm;
2878 INT default_left_margin = 0; /* in pixels */
2879 INT default_right_margin = 0; /* in pixels */
2881 /* Set the default margins depending on the font */
2882 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2883 HDC dc = GetDC(es->hwndSelf);
2884 HFONT old_font = SelectObject(dc, es->font);
2885 LONG width = GdiGetCharDimensions(dc, &tm, NULL);
2886 RECT rc;
2888 /* The default margins are only non zero for TrueType or Vector fonts */
2889 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2890 if (!is_cjk(tm.tmCharSet)) {
2891 default_left_margin = width / 2;
2892 default_right_margin = width / 2;
2894 GetClientRect(es->hwndSelf, &rc);
2895 if (rc.right - rc.left < (width / 2 + width) * 2 &&
2896 (width >= 28 || !IsRectEmpty(&rc)) ) {
2897 default_left_margin = es->left_margin;
2898 default_right_margin = es->right_margin;
2900 } else {
2901 /* FIXME: figure out the CJK values. They are not affected by the client rect. */
2902 default_left_margin = width / 2;
2903 default_right_margin = width / 2;
2906 SelectObject(dc, old_font);
2907 ReleaseDC(es->hwndSelf, dc);
2910 if (action & EC_LEFTMARGIN) {
2911 es->format_rect.left -= es->left_margin;
2912 if (left != EC_USEFONTINFO)
2913 es->left_margin = left;
2914 else
2915 es->left_margin = default_left_margin;
2916 es->format_rect.left += es->left_margin;
2919 if (action & EC_RIGHTMARGIN) {
2920 es->format_rect.right += es->right_margin;
2921 if (right != EC_USEFONTINFO)
2922 es->right_margin = right;
2923 else
2924 es->right_margin = default_right_margin;
2925 es->format_rect.right -= es->right_margin;
2928 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2929 EDIT_AdjustFormatRect(es);
2930 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2933 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2937 /*********************************************************************
2939 * EM_SETPASSWORDCHAR
2942 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2944 LONG style;
2946 if (es->style & ES_MULTILINE)
2947 return;
2949 if (es->password_char == c)
2950 return;
2952 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2953 es->password_char = c;
2954 if (c) {
2955 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2956 es->style |= ES_PASSWORD;
2957 } else {
2958 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2959 es->style &= ~ES_PASSWORD;
2961 EDIT_InvalidateUniscribeData(es);
2962 EDIT_UpdateText(es, NULL, TRUE);
2966 /*********************************************************************
2968 * EM_SETTABSTOPS
2971 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2973 if (!(es->style & ES_MULTILINE))
2974 return FALSE;
2975 HeapFree(GetProcessHeap(), 0, es->tabs);
2976 es->tabs_count = count;
2977 if (!count)
2978 es->tabs = NULL;
2979 else {
2980 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2981 memcpy(es->tabs, tabs, count * sizeof(INT));
2983 EDIT_InvalidateUniscribeData(es);
2984 return TRUE;
2988 /*********************************************************************
2990 * EM_SETWORDBREAKPROC
2993 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
2995 if (es->word_break_proc == wbp)
2996 return;
2998 es->word_break_proc = wbp;
3000 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3001 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3002 EDIT_UpdateText(es, NULL, TRUE);
3007 /*********************************************************************
3009 * EM_UNDO / WM_UNDO
3012 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3014 INT ulength;
3015 LPWSTR utext;
3017 /* As per MSDN spec, for a single-line edit control,
3018 the return value is always TRUE */
3019 if( es->style & ES_READONLY )
3020 return !(es->style & ES_MULTILINE);
3022 ulength = strlenW(es->undo_text);
3024 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3026 strcpyW(utext, es->undo_text);
3028 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3029 es->undo_insert_count, debugstr_w(utext));
3031 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3032 EDIT_EM_EmptyUndoBuffer(es);
3033 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
3034 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3035 /* send the notification after the selection start and end are set */
3036 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3037 EDIT_EM_ScrollCaret(es);
3038 HeapFree(GetProcessHeap(), 0, utext);
3040 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3041 es->undo_insert_count, debugstr_w(es->undo_text));
3042 return TRUE;
3046 /* Helper function for WM_CHAR
3048 * According to an MSDN blog article titled "Just because you're a control
3049 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3050 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3051 * a dialog box or not.
3053 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3055 return (es->flags & EF_DIALOGMODE);
3059 /*********************************************************************
3061 * WM_PASTE
3064 static void EDIT_WM_Paste(EDITSTATE *es)
3066 HGLOBAL hsrc;
3067 LPWSTR src, ptr;
3068 int len;
3070 /* Protect read-only edit control from modification */
3071 if(es->style & ES_READONLY)
3072 return;
3074 OpenClipboard(es->hwndSelf);
3075 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3076 src = GlobalLock(hsrc);
3077 len = strlenW(src);
3078 /* Protect single-line edit against pasting new line character */
3079 if (!(es->style & ES_MULTILINE) && ((ptr = strchrW(src, '\n')))) {
3080 len = ptr - src;
3081 if (len && src[len - 1] == '\r')
3082 --len;
3084 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3085 GlobalUnlock(hsrc);
3087 else if (es->style & ES_PASSWORD) {
3088 /* clear selected text in password edit box even with empty clipboard */
3089 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3091 CloseClipboard();
3095 /*********************************************************************
3097 * WM_COPY
3100 static void EDIT_WM_Copy(EDITSTATE *es)
3102 INT s = min(es->selection_start, es->selection_end);
3103 INT e = max(es->selection_start, es->selection_end);
3104 HGLOBAL hdst;
3105 LPWSTR dst;
3106 DWORD len;
3108 if (e == s) return;
3110 len = e - s;
3111 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3112 dst = GlobalLock(hdst);
3113 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3114 dst[len] = 0; /* ensure 0 termination */
3115 TRACE("%s\n", debugstr_w(dst));
3116 GlobalUnlock(hdst);
3117 OpenClipboard(es->hwndSelf);
3118 EmptyClipboard();
3119 SetClipboardData(CF_UNICODETEXT, hdst);
3120 CloseClipboard();
3124 /*********************************************************************
3126 * WM_CLEAR
3129 static inline void EDIT_WM_Clear(EDITSTATE *es)
3131 /* Protect read-only edit control from modification */
3132 if(es->style & ES_READONLY)
3133 return;
3135 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3139 /*********************************************************************
3141 * WM_CUT
3144 static inline void EDIT_WM_Cut(EDITSTATE *es)
3146 EDIT_WM_Copy(es);
3147 EDIT_WM_Clear(es);
3151 /*********************************************************************
3153 * WM_CHAR
3156 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3158 BOOL control;
3160 control = GetKeyState(VK_CONTROL) & 0x8000;
3162 switch (c) {
3163 case '\r':
3164 /* If it's not a multiline edit box, it would be ignored below.
3165 * For multiline edit without ES_WANTRETURN, we have to make a
3166 * special case.
3168 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3169 if (EDIT_IsInsideDialog(es))
3170 break;
3171 case '\n':
3172 if (es->style & ES_MULTILINE) {
3173 if (es->style & ES_READONLY) {
3174 EDIT_MoveHome(es, FALSE, FALSE);
3175 EDIT_MoveDown_ML(es, FALSE);
3176 } else {
3177 static const WCHAR cr_lfW[] = {'\r','\n'};
3178 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, 2, TRUE, TRUE);
3181 break;
3182 case '\t':
3183 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3185 static const WCHAR tabW[] = {'\t'};
3186 if (EDIT_IsInsideDialog(es))
3187 break;
3188 EDIT_EM_ReplaceSel(es, TRUE, tabW, 1, TRUE, TRUE);
3190 break;
3191 case VK_BACK:
3192 if (!(es->style & ES_READONLY) && !control) {
3193 if (es->selection_start != es->selection_end)
3194 EDIT_WM_Clear(es);
3195 else {
3196 /* delete character left of caret */
3197 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3198 EDIT_MoveBackward(es, TRUE);
3199 EDIT_WM_Clear(es);
3202 break;
3203 case 0x03: /* ^C */
3204 if (!(es->style & ES_PASSWORD))
3205 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3206 break;
3207 case 0x16: /* ^V */
3208 if (!(es->style & ES_READONLY))
3209 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3210 break;
3211 case 0x18: /* ^X */
3212 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3213 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3214 break;
3215 case 0x1A: /* ^Z */
3216 if (!(es->style & ES_READONLY))
3217 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3218 break;
3220 default:
3221 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3222 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3223 break;
3225 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3226 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3227 break;
3229 return 1;
3233 /*********************************************************************
3235 * EDIT_ContextMenuCommand
3238 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3240 switch (id) {
3241 case EM_UNDO:
3242 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3243 break;
3244 case WM_CUT:
3245 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3246 break;
3247 case WM_COPY:
3248 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3249 break;
3250 case WM_PASTE:
3251 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3252 break;
3253 case WM_CLEAR:
3254 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3255 break;
3256 case EM_SETSEL:
3257 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3258 break;
3259 default:
3260 ERR("unknown menu item, please report\n");
3261 break;
3266 /*********************************************************************
3268 * WM_CONTEXTMENU
3270 * Note: the resource files resource/sysres_??.rc cannot define a
3271 * single popup menu. Hence we use a (dummy) menubar
3272 * containing the single popup menu as its first item.
3274 * FIXME: the message identifiers have been chosen arbitrarily,
3275 * hence we use MF_BYPOSITION.
3276 * We might as well use the "real" values (anybody knows ?)
3277 * The menu definition is in resources/sysres_??.rc.
3278 * Once these are OK, we better use MF_BYCOMMAND here
3279 * (as we do in EDIT_WM_Command()).
3282 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3284 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3285 HMENU popup = GetSubMenu(menu, 0);
3286 UINT start = es->selection_start;
3287 UINT end = es->selection_end;
3288 UINT cmd;
3290 ORDER_UINT(start, end);
3292 /* undo */
3293 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3294 /* cut */
3295 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3296 /* copy */
3297 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3298 /* paste */
3299 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3300 /* delete */
3301 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3302 /* select all */
3303 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3305 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3307 RECT rc;
3308 /* Windows places the menu at the edit's center in this case */
3309 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3310 x = rc.left + (rc.right - rc.left) / 2;
3311 y = rc.top + (rc.bottom - rc.top) / 2;
3314 if (!(es->flags & EF_FOCUSED))
3315 SetFocus(es->hwndSelf);
3317 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3318 x, y, 0, es->hwndSelf, NULL);
3320 if (cmd)
3321 EDIT_ContextMenuCommand(es, cmd);
3323 DestroyMenu(menu);
3327 /*********************************************************************
3329 * WM_GETTEXT
3332 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3334 if(!count) return 0;
3336 if(unicode)
3338 lstrcpynW(dst, es->text, count);
3339 return strlenW(dst);
3341 else
3343 LPSTR textA = (LPSTR)dst;
3344 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3345 textA[count - 1] = 0; /* ensure 0 termination */
3346 return strlen(textA);
3350 /*********************************************************************
3352 * EDIT_CheckCombo
3355 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3357 HWND hLBox = es->hwndListBox;
3358 HWND hCombo;
3359 BOOL bDropped;
3360 int nEUI;
3362 if (!hLBox)
3363 return FALSE;
3365 hCombo = GetParent(es->hwndSelf);
3366 bDropped = TRUE;
3367 nEUI = 0;
3369 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3371 if (key == VK_UP || key == VK_DOWN)
3373 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3374 nEUI = 1;
3376 if (msg == WM_KEYDOWN || nEUI)
3377 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3380 switch (msg)
3382 case WM_KEYDOWN:
3383 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3385 /* make sure ComboLBox pops up */
3386 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3387 key = VK_F4;
3388 nEUI = 2;
3391 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3392 break;
3394 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3395 if (nEUI)
3396 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3397 else
3398 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3399 break;
3402 if(nEUI == 2)
3403 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3405 return TRUE;
3409 /*********************************************************************
3411 * WM_KEYDOWN
3413 * Handling of special keys that don't produce a WM_CHAR
3414 * (i.e. non-printable keys) & Backspace & Delete
3417 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3419 BOOL shift;
3420 BOOL control;
3422 if (GetKeyState(VK_MENU) & 0x8000)
3423 return 0;
3425 shift = GetKeyState(VK_SHIFT) & 0x8000;
3426 control = GetKeyState(VK_CONTROL) & 0x8000;
3428 switch (key) {
3429 case VK_F4:
3430 case VK_UP:
3431 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3432 break;
3434 /* fall through */
3435 case VK_LEFT:
3436 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3437 EDIT_MoveUp_ML(es, shift);
3438 else
3439 if (control)
3440 EDIT_MoveWordBackward(es, shift);
3441 else
3442 EDIT_MoveBackward(es, shift);
3443 break;
3444 case VK_DOWN:
3445 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3446 break;
3447 /* fall through */
3448 case VK_RIGHT:
3449 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3450 EDIT_MoveDown_ML(es, shift);
3451 else if (control)
3452 EDIT_MoveWordForward(es, shift);
3453 else
3454 EDIT_MoveForward(es, shift);
3455 break;
3456 case VK_HOME:
3457 EDIT_MoveHome(es, shift, control);
3458 break;
3459 case VK_END:
3460 EDIT_MoveEnd(es, shift, control);
3461 break;
3462 case VK_PRIOR:
3463 if (es->style & ES_MULTILINE)
3464 EDIT_MovePageUp_ML(es, shift);
3465 else
3466 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3467 break;
3468 case VK_NEXT:
3469 if (es->style & ES_MULTILINE)
3470 EDIT_MovePageDown_ML(es, shift);
3471 else
3472 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3473 break;
3474 case VK_DELETE:
3475 if (!(es->style & ES_READONLY) && !(shift && control)) {
3476 if (es->selection_start != es->selection_end) {
3477 if (shift)
3478 EDIT_WM_Cut(es);
3479 else
3480 EDIT_WM_Clear(es);
3481 } else {
3482 if (shift) {
3483 /* delete character left of caret */
3484 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3485 EDIT_MoveBackward(es, TRUE);
3486 EDIT_WM_Clear(es);
3487 } else if (control) {
3488 /* delete to end of line */
3489 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3490 EDIT_MoveEnd(es, TRUE, FALSE);
3491 EDIT_WM_Clear(es);
3492 } else {
3493 /* delete character right of caret */
3494 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3495 EDIT_MoveForward(es, TRUE);
3496 EDIT_WM_Clear(es);
3500 break;
3501 case VK_INSERT:
3502 if (shift) {
3503 if (!(es->style & ES_READONLY))
3504 EDIT_WM_Paste(es);
3505 } else if (control)
3506 EDIT_WM_Copy(es);
3507 break;
3508 case VK_RETURN:
3509 /* If the edit doesn't want the return send a message to the default object */
3510 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3512 DWORD dw;
3514 if (!EDIT_IsInsideDialog(es)) break;
3515 if (control) break;
3516 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3517 if (HIWORD(dw) == DC_HASDEFID)
3519 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3520 if (hwDefCtrl)
3522 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3523 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3527 break;
3528 case VK_ESCAPE:
3529 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3530 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3531 break;
3532 case VK_TAB:
3533 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3534 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3535 break;
3537 return TRUE;
3541 /*********************************************************************
3543 * WM_KILLFOCUS
3546 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3548 es->flags &= ~EF_FOCUSED;
3549 DestroyCaret();
3550 if(!(es->style & ES_NOHIDESEL))
3551 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3552 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3553 /* throw away left over scroll when we lose focus */
3554 es->wheelDeltaRemainder = 0;
3555 return 0;
3559 /*********************************************************************
3561 * WM_LBUTTONDBLCLK
3563 * The caret position has been set on the WM_LBUTTONDOWN message
3566 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3568 INT s;
3569 INT e = es->selection_end;
3570 INT l;
3571 INT li;
3572 INT ll;
3574 es->bCaptureState = TRUE;
3575 SetCapture(es->hwndSelf);
3577 l = EDIT_EM_LineFromChar(es, e);
3578 li = EDIT_EM_LineIndex(es, l);
3579 ll = EDIT_EM_LineLength(es, e);
3580 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3581 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3582 EDIT_EM_SetSel(es, s, e, FALSE);
3583 EDIT_EM_ScrollCaret(es);
3584 es->region_posx = es->region_posy = 0;
3585 SetTimer(es->hwndSelf, 0, 100, NULL);
3586 return 0;
3590 /*********************************************************************
3592 * WM_LBUTTONDOWN
3595 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3597 INT e;
3598 BOOL after_wrap;
3600 es->bCaptureState = TRUE;
3601 SetCapture(es->hwndSelf);
3602 EDIT_ConfinePoint(es, &x, &y);
3603 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3604 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3605 EDIT_EM_ScrollCaret(es);
3606 es->region_posx = es->region_posy = 0;
3607 SetTimer(es->hwndSelf, 0, 100, NULL);
3609 if (!(es->flags & EF_FOCUSED))
3610 SetFocus(es->hwndSelf);
3612 return 0;
3616 /*********************************************************************
3618 * WM_LBUTTONUP
3621 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3623 if (es->bCaptureState) {
3624 KillTimer(es->hwndSelf, 0);
3625 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3627 es->bCaptureState = FALSE;
3628 return 0;
3632 /*********************************************************************
3634 * WM_MBUTTONDOWN
3637 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3639 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3640 return 0;
3644 /*********************************************************************
3646 * WM_MOUSEMOVE
3649 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3651 INT e;
3652 BOOL after_wrap;
3653 INT prex, prey;
3655 /* If the mouse has been captured by process other than the edit control itself,
3656 * the windows edit controls will not select the strings with mouse move.
3658 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3659 return 0;
3662 * FIXME: gotta do some scrolling if outside client
3663 * area. Maybe reset the timer ?
3665 prex = x; prey = y;
3666 EDIT_ConfinePoint(es, &x, &y);
3667 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3668 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3669 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3670 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3671 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3672 return 0;
3676 /*********************************************************************
3678 * WM_PAINT
3681 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3683 PAINTSTRUCT ps;
3684 INT i;
3685 HDC dc;
3686 HFONT old_font = 0;
3687 RECT rc;
3688 RECT rcClient;
3689 RECT rcLine;
3690 RECT rcRgn;
3691 HBRUSH brush;
3692 HBRUSH old_brush;
3693 INT bw, bh;
3694 BOOL rev = es->bEnableState &&
3695 ((es->flags & EF_FOCUSED) ||
3696 (es->style & ES_NOHIDESEL));
3697 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3699 /* The dc we use for calculating may not be the one we paint into.
3700 This is the safest action. */
3701 EDIT_InvalidateUniscribeData(es);
3702 GetClientRect(es->hwndSelf, &rcClient);
3704 /* get the background brush */
3705 brush = EDIT_NotifyCtlColor(es, dc);
3707 /* paint the border and the background */
3708 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3710 if(es->style & WS_BORDER) {
3711 bw = GetSystemMetrics(SM_CXBORDER);
3712 bh = GetSystemMetrics(SM_CYBORDER);
3713 rc = rcClient;
3714 if(es->style & ES_MULTILINE) {
3715 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3716 if(es->style & WS_VSCROLL) rc.right+=bw;
3719 /* Draw the frame. Same code as in nonclient.c */
3720 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3721 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3722 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3723 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3724 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3725 SelectObject(dc, old_brush);
3727 /* Keep the border clean */
3728 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3729 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3732 GetClipBox(dc, &rc);
3733 FillRect(dc, &rc, brush);
3735 IntersectClipRect(dc, es->format_rect.left,
3736 es->format_rect.top,
3737 es->format_rect.right,
3738 es->format_rect.bottom);
3739 if (es->style & ES_MULTILINE) {
3740 rc = rcClient;
3741 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3743 if (es->font)
3744 old_font = SelectObject(dc, es->font);
3746 if (!es->bEnableState)
3747 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3748 GetClipBox(dc, &rcRgn);
3749 if (es->style & ES_MULTILINE) {
3750 INT vlc = get_vertical_line_count(es);
3751 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3752 EDIT_UpdateUniscribeData(es, dc, i);
3753 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3754 if (IntersectRect(&rc, &rcRgn, &rcLine))
3755 EDIT_PaintLine(es, dc, i, rev);
3757 } else {
3758 EDIT_UpdateUniscribeData(es, dc, 0);
3759 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3760 if (IntersectRect(&rc, &rcRgn, &rcLine))
3761 EDIT_PaintLine(es, dc, 0, rev);
3763 if (es->font)
3764 SelectObject(dc, old_font);
3766 if (!hdc)
3767 EndPaint(es->hwndSelf, &ps);
3771 /*********************************************************************
3773 * WM_SETFOCUS
3776 static void EDIT_WM_SetFocus(EDITSTATE *es)
3778 es->flags |= EF_FOCUSED;
3780 if (!(es->style & ES_NOHIDESEL))
3781 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3783 /* single line edit updates itself */
3784 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3786 HDC hdc = GetDC(es->hwndSelf);
3787 EDIT_WM_Paint(es, hdc);
3788 ReleaseDC(es->hwndSelf, hdc);
3791 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3792 EDIT_SetCaretPos(es, es->selection_end,
3793 es->flags & EF_AFTER_WRAP);
3794 ShowCaret(es->hwndSelf);
3795 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3799 /*********************************************************************
3801 * WM_SETFONT
3803 * With Win95 look the margins are set to default font value unless
3804 * the system font (font == 0) is being set, in which case they are left
3805 * unchanged.
3808 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3810 TEXTMETRICW tm;
3811 HDC dc;
3812 HFONT old_font = 0;
3813 RECT clientRect;
3815 es->font = font;
3816 EDIT_InvalidateUniscribeData(es);
3817 dc = GetDC(es->hwndSelf);
3818 if (font)
3819 old_font = SelectObject(dc, font);
3820 GetTextMetricsW(dc, &tm);
3821 es->line_height = tm.tmHeight;
3822 es->char_width = tm.tmAveCharWidth;
3823 if (font)
3824 SelectObject(dc, old_font);
3825 ReleaseDC(es->hwndSelf, dc);
3827 /* Reset the format rect and the margins */
3828 GetClientRect(es->hwndSelf, &clientRect);
3829 EDIT_SetRectNP(es, &clientRect);
3830 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3831 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3833 if (es->style & ES_MULTILINE)
3834 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3835 else
3836 EDIT_CalcLineWidth_SL(es);
3838 if (redraw)
3839 EDIT_UpdateText(es, NULL, TRUE);
3840 if (es->flags & EF_FOCUSED) {
3841 DestroyCaret();
3842 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3843 EDIT_SetCaretPos(es, es->selection_end,
3844 es->flags & EF_AFTER_WRAP);
3845 ShowCaret(es->hwndSelf);
3850 /*********************************************************************
3852 * WM_SETTEXT
3854 * NOTES
3855 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3856 * The modified flag is reset. No notifications are sent.
3858 * For single-line controls, reception of WM_SETTEXT triggers:
3859 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3862 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3864 LPWSTR textW = NULL;
3865 if (!unicode && text)
3867 LPCSTR textA = (LPCSTR)text;
3868 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3869 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3870 if (textW)
3871 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3872 text = textW;
3875 if (es->flags & EF_UPDATE)
3876 /* fixed this bug once; complain if we see it about to happen again. */
3877 ERR("SetSel may generate UPDATE message whose handler may reset "
3878 "selection.\n");
3880 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3881 if (text)
3883 TRACE("%s\n", debugstr_w(text));
3884 EDIT_EM_ReplaceSel(es, FALSE, text, strlenW(text), FALSE, FALSE);
3885 if(!unicode)
3886 HeapFree(GetProcessHeap(), 0, textW);
3888 else
3890 TRACE("<NULL>\n");
3891 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3893 es->x_offset = 0;
3894 es->flags &= ~EF_MODIFIED;
3895 EDIT_EM_SetSel(es, 0, 0, FALSE);
3897 /* Send the notification after the selection start and end have been set
3898 * edit control doesn't send notification on WM_SETTEXT
3899 * if it is multiline, or it is part of combobox
3901 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3903 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3904 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3906 EDIT_EM_ScrollCaret(es);
3907 EDIT_UpdateScrollInfo(es);
3908 EDIT_InvalidateUniscribeData(es);
3912 /*********************************************************************
3914 * WM_SIZE
3917 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3919 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3920 RECT rc;
3921 GetClientRect(es->hwndSelf, &rc);
3922 EDIT_SetRectNP(es, &rc);
3923 EDIT_UpdateText(es, NULL, TRUE);
3928 /*********************************************************************
3930 * WM_STYLECHANGED
3932 * This message is sent by SetWindowLong on having changed either the Style
3933 * or the extended style.
3935 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3937 * See also EDIT_WM_NCCreate
3939 * It appears that the Windows version of the edit control allows the style
3940 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3941 * style variable which will generally be different. In this function we
3942 * update the internal style based on what changed in the externally visible
3943 * style.
3945 * Much of this content as based upon the MSDN, especially:
3946 * Platform SDK Documentation -> User Interface Services ->
3947 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3948 * Edit Control Styles
3950 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3952 if (GWL_STYLE == which) {
3953 DWORD style_change_mask;
3954 DWORD new_style;
3955 /* Only a subset of changes can be applied after the control
3956 * has been created.
3958 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3959 ES_NUMBER;
3960 if (es->style & ES_MULTILINE)
3961 style_change_mask |= ES_WANTRETURN;
3963 new_style = style->styleNew & style_change_mask;
3965 /* Number overrides lowercase overrides uppercase (at least it
3966 * does in Win95). However I'll bet that ES_NUMBER would be
3967 * invalid under Win 3.1.
3969 if (new_style & ES_NUMBER) {
3970 ; /* do not override the ES_NUMBER */
3971 } else if (new_style & ES_LOWERCASE) {
3972 new_style &= ~ES_UPPERCASE;
3975 es->style = (es->style & ~style_change_mask) | new_style;
3976 } else if (GWL_EXSTYLE == which) {
3977 ; /* FIXME - what is needed here */
3978 } else {
3979 WARN ("Invalid style change %ld\n",which);
3982 return 0;
3985 /*********************************************************************
3987 * WM_SYSKEYDOWN
3990 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
3992 if ((key == VK_BACK) && (key_data & 0x2000)) {
3993 if (EDIT_EM_CanUndo(es))
3994 EDIT_EM_Undo(es);
3995 return 0;
3996 } else if (key == VK_UP || key == VK_DOWN) {
3997 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
3998 return 0;
4000 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4004 /*********************************************************************
4006 * WM_TIMER
4009 static void EDIT_WM_Timer(EDITSTATE *es)
4011 if (es->region_posx < 0) {
4012 EDIT_MoveBackward(es, TRUE);
4013 } else if (es->region_posx > 0) {
4014 EDIT_MoveForward(es, TRUE);
4017 * FIXME: gotta do some vertical scrolling here, like
4018 * EDIT_EM_LineScroll(hwnd, 0, 1);
4022 /*********************************************************************
4024 * WM_HSCROLL
4027 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4029 INT dx;
4030 INT fw;
4032 if (!(es->style & ES_MULTILINE))
4033 return 0;
4035 if (!(es->style & ES_AUTOHSCROLL))
4036 return 0;
4038 dx = 0;
4039 fw = es->format_rect.right - es->format_rect.left;
4040 switch (action) {
4041 case SB_LINELEFT:
4042 TRACE("SB_LINELEFT\n");
4043 if (es->x_offset)
4044 dx = -es->char_width;
4045 break;
4046 case SB_LINERIGHT:
4047 TRACE("SB_LINERIGHT\n");
4048 if (es->x_offset < es->text_width)
4049 dx = es->char_width;
4050 break;
4051 case SB_PAGELEFT:
4052 TRACE("SB_PAGELEFT\n");
4053 if (es->x_offset)
4054 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4055 break;
4056 case SB_PAGERIGHT:
4057 TRACE("SB_PAGERIGHT\n");
4058 if (es->x_offset < es->text_width)
4059 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4060 break;
4061 case SB_LEFT:
4062 TRACE("SB_LEFT\n");
4063 if (es->x_offset)
4064 dx = -es->x_offset;
4065 break;
4066 case SB_RIGHT:
4067 TRACE("SB_RIGHT\n");
4068 if (es->x_offset < es->text_width)
4069 dx = es->text_width - es->x_offset;
4070 break;
4071 case SB_THUMBTRACK:
4072 TRACE("SB_THUMBTRACK %d\n", pos);
4073 es->flags |= EF_HSCROLL_TRACK;
4074 if(es->style & WS_HSCROLL)
4075 dx = pos - es->x_offset;
4076 else
4078 INT fw, new_x;
4079 /* Sanity check */
4080 if(pos < 0 || pos > 100) return 0;
4081 /* Assume default scroll range 0-100 */
4082 fw = es->format_rect.right - es->format_rect.left;
4083 new_x = pos * (es->text_width - fw) / 100;
4084 dx = es->text_width ? (new_x - es->x_offset) : 0;
4086 break;
4087 case SB_THUMBPOSITION:
4088 TRACE("SB_THUMBPOSITION %d\n", pos);
4089 es->flags &= ~EF_HSCROLL_TRACK;
4090 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4091 dx = pos - es->x_offset;
4092 else
4094 INT fw, new_x;
4095 /* Sanity check */
4096 if(pos < 0 || pos > 100) return 0;
4097 /* Assume default scroll range 0-100 */
4098 fw = es->format_rect.right - es->format_rect.left;
4099 new_x = pos * (es->text_width - fw) / 100;
4100 dx = es->text_width ? (new_x - es->x_offset) : 0;
4102 if (!dx) {
4103 /* force scroll info update */
4104 EDIT_UpdateScrollInfo(es);
4105 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4107 break;
4108 case SB_ENDSCROLL:
4109 TRACE("SB_ENDSCROLL\n");
4110 break;
4112 * FIXME : the next two are undocumented !
4113 * Are we doing the right thing ?
4114 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4115 * although it's also a regular control message.
4117 case EM_GETTHUMB: /* this one is used by NT notepad */
4119 LRESULT ret;
4120 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4121 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4122 else
4124 /* Assume default scroll range 0-100 */
4125 INT fw = es->format_rect.right - es->format_rect.left;
4126 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4128 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4129 return ret;
4131 case EM_LINESCROLL:
4132 TRACE("EM_LINESCROLL16\n");
4133 dx = pos;
4134 break;
4136 default:
4137 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4138 action, action);
4139 return 0;
4141 if (dx)
4143 INT fw = es->format_rect.right - es->format_rect.left;
4144 /* check if we are going to move too far */
4145 if(es->x_offset + dx + fw > es->text_width)
4146 dx = es->text_width - fw - es->x_offset;
4147 if(dx)
4148 EDIT_EM_LineScroll_internal(es, dx, 0);
4150 return 0;
4154 /*********************************************************************
4156 * WM_VSCROLL
4159 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4161 INT dy;
4163 if (!(es->style & ES_MULTILINE))
4164 return 0;
4166 if (!(es->style & ES_AUTOVSCROLL))
4167 return 0;
4169 dy = 0;
4170 switch (action) {
4171 case SB_LINEUP:
4172 case SB_LINEDOWN:
4173 case SB_PAGEUP:
4174 case SB_PAGEDOWN:
4175 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4176 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4177 (action == SB_PAGEUP ? "SB_PAGEUP" :
4178 "SB_PAGEDOWN"))));
4179 EDIT_EM_Scroll(es, action);
4180 return 0;
4181 case SB_TOP:
4182 TRACE("SB_TOP\n");
4183 dy = -es->y_offset;
4184 break;
4185 case SB_BOTTOM:
4186 TRACE("SB_BOTTOM\n");
4187 dy = es->line_count - 1 - es->y_offset;
4188 break;
4189 case SB_THUMBTRACK:
4190 TRACE("SB_THUMBTRACK %d\n", pos);
4191 es->flags |= EF_VSCROLL_TRACK;
4192 if(es->style & WS_VSCROLL)
4193 dy = pos - es->y_offset;
4194 else
4196 /* Assume default scroll range 0-100 */
4197 INT vlc, new_y;
4198 /* Sanity check */
4199 if(pos < 0 || pos > 100) return 0;
4200 vlc = get_vertical_line_count(es);
4201 new_y = pos * (es->line_count - vlc) / 100;
4202 dy = es->line_count ? (new_y - es->y_offset) : 0;
4203 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4204 es->line_count, es->y_offset, pos, dy);
4206 break;
4207 case SB_THUMBPOSITION:
4208 TRACE("SB_THUMBPOSITION %d\n", pos);
4209 es->flags &= ~EF_VSCROLL_TRACK;
4210 if(es->style & WS_VSCROLL)
4211 dy = pos - es->y_offset;
4212 else
4214 /* Assume default scroll range 0-100 */
4215 INT vlc, new_y;
4216 /* Sanity check */
4217 if(pos < 0 || pos > 100) return 0;
4218 vlc = get_vertical_line_count(es);
4219 new_y = pos * (es->line_count - vlc) / 100;
4220 dy = es->line_count ? (new_y - es->y_offset) : 0;
4221 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4222 es->line_count, es->y_offset, pos, dy);
4224 if (!dy)
4226 /* force scroll info update */
4227 EDIT_UpdateScrollInfo(es);
4228 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4230 break;
4231 case SB_ENDSCROLL:
4232 TRACE("SB_ENDSCROLL\n");
4233 break;
4235 * FIXME : the next two are undocumented !
4236 * Are we doing the right thing ?
4237 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4238 * although it's also a regular control message.
4240 case EM_GETTHUMB: /* this one is used by NT notepad */
4242 LRESULT ret;
4243 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4244 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4245 else
4247 /* Assume default scroll range 0-100 */
4248 INT vlc = get_vertical_line_count(es);
4249 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4251 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4252 return ret;
4254 case EM_LINESCROLL:
4255 TRACE("EM_LINESCROLL %d\n", pos);
4256 dy = pos;
4257 break;
4259 default:
4260 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4261 action, action);
4262 return 0;
4264 if (dy)
4265 EDIT_EM_LineScroll(es, 0, dy);
4266 return 0;
4269 /*********************************************************************
4271 * EM_GETTHUMB
4273 * FIXME: is this right ? (or should it be only VSCROLL)
4274 * (and maybe only for edit controls that really have their
4275 * own scrollbars) (and maybe only for multiline controls ?)
4276 * All in all: very poorly documented
4279 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4281 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4282 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4286 /********************************************************************
4288 * The Following code is to handle inline editing from IMEs
4291 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4293 LONG buflen;
4294 LPWSTR lpCompStr;
4295 LPSTR lpCompStrAttr = NULL;
4296 DWORD dwBufLenAttr;
4298 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4300 if (buflen < 0)
4302 return;
4305 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen);
4306 if (!lpCompStr)
4308 ERR("Unable to allocate IME CompositionString\n");
4309 return;
4312 if (buflen)
4313 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4315 if (CompFlag & GCS_COMPATTR)
4318 * We do not use the attributes yet. it would tell us what characters
4319 * are in transition and which are converted or decided upon
4321 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4322 if (dwBufLenAttr)
4324 dwBufLenAttr ++;
4325 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4326 if (!lpCompStrAttr)
4328 ERR("Unable to allocate IME Attribute String\n");
4329 HeapFree(GetProcessHeap(),0,lpCompStr);
4330 return;
4332 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4333 dwBufLenAttr);
4334 lpCompStrAttr[dwBufLenAttr] = 0;
4338 /* check for change in composition start */
4339 if (es->selection_end < es->composition_start)
4340 es->composition_start = es->selection_end;
4342 /* replace existing selection string */
4343 es->selection_start = es->composition_start;
4345 if (es->composition_len > 0)
4346 es->selection_end = es->composition_start + es->composition_len;
4347 else
4348 es->selection_end = es->selection_start;
4350 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4351 es->composition_len = abs(es->composition_start - es->selection_end);
4353 es->selection_start = es->composition_start;
4354 es->selection_end = es->selection_start + es->composition_len;
4356 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4357 HeapFree(GetProcessHeap(),0,lpCompStr);
4360 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4362 LONG buflen;
4363 LPWSTR lpResultStr;
4365 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4366 if (buflen <= 0)
4368 return;
4371 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4372 if (!lpResultStr)
4374 ERR("Unable to alloc buffer for IME string\n");
4375 return;
4378 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4380 /* check for change in composition start */
4381 if (es->selection_end < es->composition_start)
4382 es->composition_start = es->selection_end;
4384 es->selection_start = es->composition_start;
4385 es->selection_end = es->composition_start + es->composition_len;
4386 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4387 es->composition_start = es->selection_end;
4388 es->composition_len = 0;
4390 HeapFree(GetProcessHeap(),0,lpResultStr);
4393 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4395 HIMC hIMC;
4396 int cursor;
4398 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4400 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4401 es->composition_start = es->selection_end;
4404 hIMC = ImmGetContext(hwnd);
4405 if (!hIMC)
4406 return;
4408 if (CompFlag & GCS_RESULTSTR)
4410 EDIT_GetResultStr(hIMC, es);
4411 cursor = 0;
4413 else
4415 if (CompFlag & GCS_COMPSTR)
4416 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4417 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4419 ImmReleaseContext(hwnd, hIMC);
4420 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4424 /*********************************************************************
4426 * WM_NCCREATE
4428 * See also EDIT_WM_StyleChanged
4430 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4432 EDITSTATE *es;
4433 UINT alloc_size;
4435 TRACE("Creating %s edit control, style = %08x\n",
4436 unicode ? "Unicode" : "ANSI", lpcs->style);
4438 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4439 return FALSE;
4440 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4443 * Note: since the EDITSTATE has not been fully initialized yet,
4444 * we can't use any API calls that may send
4445 * WM_XXX messages before WM_NCCREATE is completed.
4448 es->is_unicode = unicode;
4449 es->style = lpcs->style;
4451 es->bEnableState = !(es->style & WS_DISABLED);
4453 es->hwndSelf = hwnd;
4454 /* Save parent, which will be notified by EN_* messages */
4455 es->hwndParent = lpcs->hwndParent;
4457 if (es->style & ES_COMBO)
4458 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4460 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4461 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4463 /* Number overrides lowercase overrides uppercase (at least it
4464 * does in Win95). However I'll bet that ES_NUMBER would be
4465 * invalid under Win 3.1.
4467 if (es->style & ES_NUMBER) {
4468 ; /* do not override the ES_NUMBER */
4469 } else if (es->style & ES_LOWERCASE) {
4470 es->style &= ~ES_UPPERCASE;
4472 if (es->style & ES_MULTILINE) {
4473 es->buffer_limit = BUFLIMIT_INITIAL;
4474 if (es->style & WS_VSCROLL)
4475 es->style |= ES_AUTOVSCROLL;
4476 if (es->style & WS_HSCROLL)
4477 es->style |= ES_AUTOHSCROLL;
4478 es->style &= ~ES_PASSWORD;
4479 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4480 /* Confirmed - RIGHT overrides CENTER */
4481 if (es->style & ES_RIGHT)
4482 es->style &= ~ES_CENTER;
4483 es->style &= ~WS_HSCROLL;
4484 es->style &= ~ES_AUTOHSCROLL;
4486 } else {
4487 es->buffer_limit = BUFLIMIT_INITIAL;
4488 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4489 es->style &= ~ES_CENTER;
4490 es->style &= ~WS_HSCROLL;
4491 es->style &= ~WS_VSCROLL;
4492 if (es->style & ES_PASSWORD)
4493 es->password_char = '*';
4496 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4497 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4498 goto cleanup;
4499 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4501 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4502 goto cleanup;
4503 es->undo_buffer_size = es->buffer_size;
4505 if (es->style & ES_MULTILINE)
4506 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4507 goto cleanup;
4508 es->line_count = 1;
4511 * In Win95 look and feel, the WS_BORDER style is replaced by the
4512 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4513 * control a nonclient area so we don't need to draw the border.
4514 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4515 * a nonclient area and we should handle painting the border ourselves.
4517 * When making modifications please ensure that the code still works
4518 * for edit controls created directly with style 0x50800000, exStyle 0
4519 * (which should have a single pixel border)
4521 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4522 es->style &= ~WS_BORDER;
4523 else if (es->style & WS_BORDER)
4524 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4526 return TRUE;
4528 cleanup:
4529 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4530 EDIT_InvalidateUniscribeData(es);
4531 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4532 HeapFree(GetProcessHeap(), 0, es->undo_text);
4533 if (es->hloc32W) LocalFree(es->hloc32W);
4534 HeapFree(GetProcessHeap(), 0, es->logAttr);
4535 HeapFree(GetProcessHeap(), 0, es);
4536 return FALSE;
4540 /*********************************************************************
4542 * WM_CREATE
4545 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4547 RECT clientRect;
4549 TRACE("%s\n", debugstr_w(name));
4551 * To initialize some final structure members, we call some helper
4552 * functions. However, since the EDITSTATE is not consistent (i.e.
4553 * not fully initialized), we should be very careful which
4554 * functions can be called, and in what order.
4556 EDIT_WM_SetFont(es, 0, FALSE);
4557 EDIT_EM_EmptyUndoBuffer(es);
4559 /* We need to calculate the format rect
4560 (applications may send EM_SETMARGINS before the control gets visible) */
4561 GetClientRect(es->hwndSelf, &clientRect);
4562 EDIT_SetRectNP(es, &clientRect);
4564 if (name && *name) {
4565 EDIT_EM_ReplaceSel(es, FALSE, name, strlenW(name), FALSE, FALSE);
4566 /* if we insert text to the editline, the text scrolls out
4567 * of the window, as the caret is placed after the insert
4568 * pos normally; thus we reset es->selection... to 0 and
4569 * update caret
4571 es->selection_start = es->selection_end = 0;
4572 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4573 * Messages are only to be sent when the USER does something to
4574 * change the contents. So I am removing this EN_CHANGE
4576 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4578 EDIT_EM_ScrollCaret(es);
4580 /* force scroll info update */
4581 EDIT_UpdateScrollInfo(es);
4582 /* The rule seems to return 1 here for success */
4583 /* Power Builder masked edit controls will crash */
4584 /* if not. */
4585 /* FIXME: is that in all cases so ? */
4586 return 1;
4590 /*********************************************************************
4592 * WM_NCDESTROY
4595 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4597 LINEDEF *pc, *pp;
4599 /* The app can own the text buffer handle */
4600 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4601 LocalFree(es->hloc32W);
4603 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4604 LocalFree(es->hloc32A);
4606 EDIT_InvalidateUniscribeData(es);
4607 pc = es->first_line_def;
4608 while (pc)
4610 pp = pc->next;
4611 HeapFree(GetProcessHeap(), 0, pc);
4612 pc = pp;
4615 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4616 HeapFree(GetProcessHeap(), 0, es->undo_text);
4617 HeapFree(GetProcessHeap(), 0, es);
4619 return 0;
4623 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4625 if(unicode)
4626 return DefWindowProcW(hwnd, msg, wParam, lParam);
4627 else
4628 return DefWindowProcA(hwnd, msg, wParam, lParam);
4631 /*********************************************************************
4633 * EditWndProc_common
4635 * The messages are in the order of the actual integer values
4636 * (which can be found in include/windows.h)
4638 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4640 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4641 LRESULT result = 0;
4643 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4645 if (!es && msg != WM_NCCREATE)
4646 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4648 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4650 switch (msg) {
4651 case EM_GETSEL:
4652 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4653 break;
4655 case EM_SETSEL:
4656 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4657 EDIT_EM_ScrollCaret(es);
4658 result = 1;
4659 break;
4661 case EM_GETRECT:
4662 if (lParam)
4663 *((LPRECT)lParam) = es->format_rect;
4664 break;
4666 case EM_SETRECT:
4667 if ((es->style & ES_MULTILINE) && lParam) {
4668 EDIT_SetRectNP(es, (LPRECT)lParam);
4669 EDIT_UpdateText(es, NULL, TRUE);
4671 break;
4673 case EM_SETRECTNP:
4674 if ((es->style & ES_MULTILINE) && lParam)
4675 EDIT_SetRectNP(es, (LPRECT)lParam);
4676 break;
4678 case EM_SCROLL:
4679 result = EDIT_EM_Scroll(es, (INT)wParam);
4680 break;
4682 case EM_LINESCROLL:
4683 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4684 break;
4686 case EM_SCROLLCARET:
4687 EDIT_EM_ScrollCaret(es);
4688 result = 1;
4689 break;
4691 case EM_GETMODIFY:
4692 result = ((es->flags & EF_MODIFIED) != 0);
4693 break;
4695 case EM_SETMODIFY:
4696 if (wParam)
4697 es->flags |= EF_MODIFIED;
4698 else
4699 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4700 break;
4702 case EM_GETLINECOUNT:
4703 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4704 break;
4706 case EM_LINEINDEX:
4707 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4708 break;
4710 case EM_SETHANDLE:
4711 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4712 break;
4714 case EM_GETHANDLE:
4715 result = (LRESULT)EDIT_EM_GetHandle(es);
4716 break;
4718 case EM_GETTHUMB:
4719 result = EDIT_EM_GetThumb(es);
4720 break;
4722 /* these messages missing from specs */
4723 case 0x00bf:
4724 case 0x00c0:
4725 case 0x00c3:
4726 case 0x00ca:
4727 FIXME("undocumented message 0x%x, please report\n", msg);
4728 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4729 break;
4731 case EM_LINELENGTH:
4732 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4733 break;
4735 case EM_REPLACESEL:
4737 LPWSTR textW;
4739 if(unicode)
4740 textW = (LPWSTR)lParam;
4741 else
4743 LPSTR textA = (LPSTR)lParam;
4744 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4745 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4746 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4749 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, strlenW(textW), TRUE, TRUE);
4750 result = 1;
4752 if(!unicode)
4753 HeapFree(GetProcessHeap(), 0, textW);
4754 break;
4757 case EM_GETLINE:
4758 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4759 break;
4761 case EM_SETLIMITTEXT:
4762 EDIT_EM_SetLimitText(es, wParam);
4763 break;
4765 case EM_CANUNDO:
4766 result = (LRESULT)EDIT_EM_CanUndo(es);
4767 break;
4769 case EM_UNDO:
4770 case WM_UNDO:
4771 result = (LRESULT)EDIT_EM_Undo(es);
4772 break;
4774 case EM_FMTLINES:
4775 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4776 break;
4778 case EM_LINEFROMCHAR:
4779 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4780 break;
4782 case EM_SETTABSTOPS:
4783 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4784 break;
4786 case EM_SETPASSWORDCHAR:
4788 WCHAR charW = 0;
4790 if(unicode)
4791 charW = (WCHAR)wParam;
4792 else
4794 CHAR charA = wParam;
4795 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4798 EDIT_EM_SetPasswordChar(es, charW);
4799 break;
4802 case EM_EMPTYUNDOBUFFER:
4803 EDIT_EM_EmptyUndoBuffer(es);
4804 break;
4806 case EM_GETFIRSTVISIBLELINE:
4807 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4808 break;
4810 case EM_SETREADONLY:
4812 DWORD old_style = es->style;
4814 if (wParam) {
4815 SetWindowLongW( hwnd, GWL_STYLE,
4816 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4817 es->style |= ES_READONLY;
4818 } else {
4819 SetWindowLongW( hwnd, GWL_STYLE,
4820 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4821 es->style &= ~ES_READONLY;
4824 if (old_style ^ es->style)
4825 InvalidateRect(es->hwndSelf, NULL, TRUE);
4827 result = 1;
4828 break;
4831 case EM_SETWORDBREAKPROC:
4832 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4833 result = 1;
4834 break;
4836 case EM_GETWORDBREAKPROC:
4837 result = (LRESULT)es->word_break_proc;
4838 break;
4840 case EM_GETPASSWORDCHAR:
4842 if(unicode)
4843 result = es->password_char;
4844 else
4846 WCHAR charW = es->password_char;
4847 CHAR charA = 0;
4848 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4849 result = charA;
4851 break;
4854 case EM_SETMARGINS:
4855 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4856 break;
4858 case EM_GETMARGINS:
4859 result = MAKELONG(es->left_margin, es->right_margin);
4860 break;
4862 case EM_GETLIMITTEXT:
4863 result = es->buffer_limit;
4864 break;
4866 case EM_POSFROMCHAR:
4867 if ((INT)wParam >= get_text_length(es)) result = -1;
4868 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4869 break;
4871 case EM_CHARFROMPOS:
4872 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4873 break;
4875 /* End of the EM_ messages which were in numerical order; what order
4876 * are these in? vaguely alphabetical?
4879 case WM_NCCREATE:
4880 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4881 break;
4883 case WM_NCDESTROY:
4884 result = EDIT_WM_NCDestroy(es);
4885 es = NULL;
4886 break;
4888 case WM_GETDLGCODE:
4889 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4891 if (es->style & ES_MULTILINE)
4892 result |= DLGC_WANTALLKEYS;
4894 if (lParam)
4896 es->flags|=EF_DIALOGMODE;
4898 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4900 int vk = (int)((LPMSG)lParam)->wParam;
4902 if (es->hwndListBox)
4904 if (vk == VK_RETURN || vk == VK_ESCAPE)
4905 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4906 result |= DLGC_WANTMESSAGE;
4910 break;
4912 case WM_IME_CHAR:
4913 if (!unicode)
4915 WCHAR charW;
4916 CHAR strng[2];
4918 strng[0] = wParam >> 8;
4919 strng[1] = wParam & 0xff;
4920 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4921 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4922 result = EDIT_WM_Char(es, charW);
4923 break;
4925 /* fall through */
4926 case WM_CHAR:
4928 WCHAR charW;
4930 if(unicode)
4931 charW = wParam;
4932 else
4934 CHAR charA = wParam;
4935 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4938 if (es->hwndListBox)
4940 if (charW == VK_RETURN || charW == VK_ESCAPE)
4942 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4943 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4944 break;
4947 result = EDIT_WM_Char(es, charW);
4948 break;
4951 case WM_UNICHAR:
4952 if (unicode)
4954 if (wParam == UNICODE_NOCHAR) return TRUE;
4955 if (wParam <= 0x000fffff)
4957 if(wParam > 0xffff) /* convert to surrogates */
4959 wParam -= 0x10000;
4960 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4961 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4963 else EDIT_WM_Char(es, wParam);
4965 return 0;
4967 break;
4969 case WM_CLEAR:
4970 EDIT_WM_Clear(es);
4971 break;
4973 case WM_CONTEXTMENU:
4974 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4975 break;
4977 case WM_COPY:
4978 EDIT_WM_Copy(es);
4979 break;
4981 case WM_CREATE:
4982 if(unicode)
4983 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4984 else
4986 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4987 LPWSTR nameW = NULL;
4988 if(nameA)
4990 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
4991 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4992 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
4994 result = EDIT_WM_Create(es, nameW);
4995 HeapFree(GetProcessHeap(), 0, nameW);
4997 break;
4999 case WM_CUT:
5000 EDIT_WM_Cut(es);
5001 break;
5003 case WM_ENABLE:
5004 es->bEnableState = (BOOL) wParam;
5005 EDIT_UpdateText(es, NULL, TRUE);
5006 break;
5008 case WM_ERASEBKGND:
5009 /* we do the proper erase in EDIT_WM_Paint */
5010 result = 1;
5011 break;
5013 case WM_GETFONT:
5014 result = (LRESULT)es->font;
5015 break;
5017 case WM_GETTEXT:
5018 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5019 break;
5021 case WM_GETTEXTLENGTH:
5022 if (unicode) result = get_text_length(es);
5023 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5024 NULL, 0, NULL, NULL );
5025 break;
5027 case WM_HSCROLL:
5028 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5029 break;
5031 case WM_KEYDOWN:
5032 result = EDIT_WM_KeyDown(es, (INT)wParam);
5033 break;
5035 case WM_KILLFOCUS:
5036 result = EDIT_WM_KillFocus(es);
5037 break;
5039 case WM_LBUTTONDBLCLK:
5040 result = EDIT_WM_LButtonDblClk(es);
5041 break;
5043 case WM_LBUTTONDOWN:
5044 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5045 break;
5047 case WM_LBUTTONUP:
5048 result = EDIT_WM_LButtonUp(es);
5049 break;
5051 case WM_MBUTTONDOWN:
5052 result = EDIT_WM_MButtonDown(es);
5053 break;
5055 case WM_MOUSEMOVE:
5056 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5057 break;
5059 case WM_PRINTCLIENT:
5060 case WM_PAINT:
5061 EDIT_WM_Paint(es, (HDC)wParam);
5062 break;
5064 case WM_PASTE:
5065 EDIT_WM_Paste(es);
5066 break;
5068 case WM_SETFOCUS:
5069 EDIT_WM_SetFocus(es);
5070 break;
5072 case WM_SETFONT:
5073 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5074 break;
5076 case WM_SETREDRAW:
5077 /* FIXME: actually set an internal flag and behave accordingly */
5078 break;
5080 case WM_SETTEXT:
5081 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5082 result = TRUE;
5083 break;
5085 case WM_SIZE:
5086 EDIT_WM_Size(es, (UINT)wParam);
5087 break;
5089 case WM_STYLECHANGED:
5090 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5091 break;
5093 case WM_STYLECHANGING:
5094 result = 0; /* See EDIT_WM_StyleChanged */
5095 break;
5097 case WM_SYSKEYDOWN:
5098 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5099 break;
5101 case WM_TIMER:
5102 EDIT_WM_Timer(es);
5103 break;
5105 case WM_VSCROLL:
5106 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5107 break;
5109 case WM_MOUSEWHEEL:
5111 int wheelDelta;
5112 UINT pulScrollLines = 3;
5113 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5115 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5116 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5117 break;
5119 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5120 /* if scrolling changes direction, ignore left overs */
5121 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5122 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5123 es->wheelDeltaRemainder += wheelDelta;
5124 else
5125 es->wheelDeltaRemainder = wheelDelta;
5126 if (es->wheelDeltaRemainder && pulScrollLines)
5128 int cLineScroll;
5129 pulScrollLines = (int) min((UINT) es->line_count, pulScrollLines);
5130 cLineScroll = pulScrollLines * (float)es->wheelDeltaRemainder / WHEEL_DELTA;
5131 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
5132 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5135 break;
5138 /* IME messages to make the edit control IME aware */
5139 case WM_IME_SETCONTEXT:
5140 break;
5142 case WM_IME_STARTCOMPOSITION:
5143 es->composition_start = es->selection_end;
5144 es->composition_len = 0;
5145 break;
5147 case WM_IME_COMPOSITION:
5148 EDIT_ImeComposition(hwnd, lParam, es);
5149 break;
5151 case WM_IME_ENDCOMPOSITION:
5152 if (es->composition_len > 0)
5154 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5155 es->selection_end = es->selection_start;
5156 es->composition_len= 0;
5158 break;
5160 case WM_IME_COMPOSITIONFULL:
5161 break;
5163 case WM_IME_SELECT:
5164 break;
5166 case WM_IME_CONTROL:
5167 break;
5169 case WM_IME_REQUEST:
5170 switch (wParam)
5172 case IMR_QUERYCHARPOSITION:
5174 LRESULT pos;
5175 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5177 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5178 chpos->pt.x = LOWORD(pos);
5179 chpos->pt.y = HIWORD(pos);
5180 chpos->cLineHeight = es->line_height;
5181 chpos->rcDocument = es->format_rect;
5182 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5183 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5184 result = 1;
5185 break;
5188 break;
5190 default:
5191 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5192 break;
5195 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5196 EDIT_UnlockBuffer(es, FALSE);
5198 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5200 return result;
5204 /*********************************************************************
5205 * edit class descriptor
5207 static const WCHAR editW[] = {'E','d','i','t',0};
5208 const struct builtin_class_descr EDIT_builtin_class =
5210 editW, /* name */
5211 CS_DBLCLKS | CS_PARENTDC, /* style */
5212 WINPROC_EDIT, /* proc */
5213 #ifdef __i386__
5214 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
5215 #else
5216 sizeof(EDITSTATE *), /* extra */
5217 #endif
5218 IDC_IBEAM, /* cursor */
5219 0 /* brush */