windows.networking.hostname/tests: Add IHostNameFactory::CreateHostName() tests.
[wine.git] / dlls / user32 / edit.c
blob3a8241fcf15a427f65c9b68511318f9dc60f6dea
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 * - EN_ALIGN_LTR_EC
25 * - EN_ALIGN_RTL_EC
26 * - ES_OEMCONVERT
30 #include <stdlib.h>
31 #include "user_private.h"
32 #include "controls.h"
33 #include "usp10.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(edit);
37 WINE_DECLARE_DEBUG_CHANNEL(combo);
38 WINE_DECLARE_DEBUG_CHANNEL(relay);
40 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
41 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
42 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
43 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
46 * extra flags for EDITSTATE.flags field
48 #define EF_MODIFIED 0x0001 /* text has been modified */
49 #define EF_FOCUSED 0x0002 /* we have input focus */
50 #define EF_UPDATE 0x0004 /* notify parent of changed state */
51 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
52 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
53 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
54 wrapped line, instead of in front of the next character */
55 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
56 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
58 typedef enum
60 END_0 = 0, /* line ends with terminating '\0' character */
61 END_WRAP, /* line is wrapped */
62 END_HARD, /* line ends with a hard return '\r\n' */
63 END_SOFT, /* line ends with a soft return '\r\r\n' */
64 END_RICH /* line ends with a single '\n' */
65 } LINE_END;
67 typedef struct tagLINEDEF {
68 INT length; /* bruto length of a line in bytes */
69 INT net_length; /* netto length of a line in visible characters */
70 LINE_END ending;
71 INT width; /* width of the line in pixels */
72 INT index; /* line index into the buffer */
73 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
74 struct tagLINEDEF *next;
75 } LINEDEF;
77 typedef struct
79 BOOL is_unicode; /* how the control was created */
80 LPWSTR text; /* the actual contents of the control */
81 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
82 UINT buffer_size; /* the size of the buffer in characters */
83 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
84 HFONT font; /* NULL means standard system font */
85 INT x_offset; /* scroll offset for multi lines this is in pixels
86 for single lines it's in characters */
87 INT line_height; /* height of a screen line in pixels */
88 INT char_width; /* average character width in pixels */
89 DWORD style; /* sane version of wnd->dwStyle */
90 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
91 INT undo_insert_count; /* number of characters inserted in sequence */
92 UINT undo_position; /* character index of the insertion and deletion */
93 LPWSTR undo_text; /* deleted text */
94 UINT undo_buffer_size; /* size of the deleted text buffer */
95 INT selection_start; /* == selection_end if no selection */
96 INT selection_end; /* == current caret position */
97 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
98 INT left_margin; /* in pixels */
99 INT right_margin; /* in pixels */
100 RECT format_rect;
101 INT text_width; /* width of the widest line in pixels for multi line controls
102 and just line width for single line controls */
103 INT region_posx; /* Position of cursor relative to region: */
104 INT region_posy; /* -1: to left, 0: within, 1: to right */
105 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
106 INT line_count; /* number of lines */
107 INT y_offset; /* scroll offset in number of lines */
108 BOOL bCaptureState; /* flag indicating whether mouse was captured */
109 BOOL bEnableState; /* flag keeping the enable state */
110 HWND hwndSelf; /* the our window handle */
111 HWND hwndParent; /* Handle of parent for sending EN_* messages.
112 Even if parent will change, EN_* messages
113 should be sent to the first parent. */
114 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
115 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
116 BYTE lead_byte; /* DBCS lead byte store for WM_CHAR */
118 * only for multi line controls
120 INT lock_count; /* amount of re-entries in the EditWndProc */
121 INT tabs_count;
122 LPINT tabs;
123 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
124 HLOCAL hloc32W; /* our unicode local memory block */
125 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
126 or EM_SETHANDLE */
127 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
129 * IME Data
131 UINT ime_status; /* IME status flag */
134 * Uniscribe Data
136 SCRIPT_LOGATTR *logAttr;
137 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
138 } EDITSTATE;
141 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
142 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
144 static inline BOOL notify_parent(const EDITSTATE *es, INT code)
146 HWND hwnd = es->hwndSelf;
147 TRACE("notification %d sent to %p.\n", code, es->hwndParent);
148 SendMessageW(es->hwndParent, WM_COMMAND, MAKEWPARAM(GetWindowLongPtrW(es->hwndSelf, GWLP_ID), code), (LPARAM)es->hwndSelf);
149 return IsWindow(hwnd);
152 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
154 /*********************************************************************
156 * EM_CANUNDO
159 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
161 return (es->undo_insert_count || lstrlenW(es->undo_text));
165 /*********************************************************************
167 * EM_EMPTYUNDOBUFFER
170 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
172 es->undo_insert_count = 0;
173 *es->undo_text = '\0';
177 /**********************************************************************
178 * get_app_version
180 * Returns the window version in case Wine emulates a later version
181 * of windows than the application expects.
183 * In a number of cases when windows runs an application that was
184 * designed for an earlier windows version, windows reverts
185 * to "old" behaviour of that earlier version.
187 * An example is a disabled edit control that needs to be painted.
188 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
189 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
190 * applications with an expected version 0f 4.0 or higher.
193 static DWORD get_app_version(void)
195 static DWORD version;
196 if (!version)
198 DWORD dwEmulatedVersion;
199 OSVERSIONINFOW info;
200 DWORD dwProcVersion = GetProcessVersion(0);
202 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
203 GetVersionExW( &info );
204 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
205 /* FIXME: this may not be 100% correct; see discussion on the
206 * wine developer list in Nov 1999 */
207 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
209 return version;
212 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
214 HBRUSH hbrush;
215 UINT msg;
217 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
218 msg = WM_CTLCOLORSTATIC;
219 else
220 msg = WM_CTLCOLOREDIT;
222 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
223 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
224 if (!hbrush)
225 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
226 return hbrush;
230 static inline UINT get_text_length(EDITSTATE *es)
232 if(es->text_length == (UINT)-1)
233 es->text_length = lstrlenW(es->text);
234 return es->text_length;
238 /*********************************************************************
240 * EDIT_WordBreakProc
242 * Find the beginning of words.
243 * Note: unlike the specs for a WordBreakProc, this function can
244 * only be called without linebreaks between s[0] up to
245 * s[count - 1]. Remember it is only called
246 * internally, so we can decide this for ourselves.
247 * Additionally we will always be breaking the full string.
250 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
252 INT ret = 0;
254 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
256 if(!s) return 0;
258 if (!es->logAttr)
260 SCRIPT_ANALYSIS psa;
262 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
263 psa.eScript = SCRIPT_UNDEFINED;
265 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
266 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
269 switch (action) {
270 case WB_LEFT:
271 if (index)
272 index--;
273 while (index && !es->logAttr[index].fSoftBreak)
274 index--;
275 ret = index;
276 break;
277 case WB_RIGHT:
278 if (!count)
279 break;
280 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
281 index++;
282 ret = index;
283 break;
284 case WB_ISDELIMITER:
285 ret = es->logAttr[index].fWhiteSpace;
286 break;
287 default:
288 ERR("unknown action code, please report !\n");
289 break;
291 return ret;
295 /*********************************************************************
297 * EDIT_CallWordBreakProc
299 * Call appropriate WordBreakProc (internal or external).
301 * Note: The "start" argument should always be an index referring
302 * to es->text. The actual wordbreak proc might be
303 * 16 bit, so we can't always pass any 32 bit LPSTR.
304 * Hence we assume that es->text is the buffer that holds
305 * the string under examination (we can decide this for ourselves).
308 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
310 INT ret;
312 if (es->word_break_proc)
314 if(es->is_unicode)
316 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
318 TRACE_(relay)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
319 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
320 ret = wbpW(es->text + start, index, count, action);
321 TRACE_(relay)("\1Ret wordbreakW %p retval=%d\n", es->word_break_proc, ret );
323 else
325 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
326 INT countA;
327 CHAR *textA;
329 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
330 textA = HeapAlloc(GetProcessHeap(), 0, countA);
331 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
332 TRACE_(relay)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
333 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
334 ret = wbpA(textA, index, countA, action);
335 HeapFree(GetProcessHeap(), 0, textA);
336 TRACE_(relay)("\1Ret wordbreakA %p retval=%d\n", es->word_break_proc, ret );
339 else
340 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
342 return ret;
345 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
347 if (line_def->ssa)
349 ScriptStringFree(&line_def->ssa);
350 line_def->ssa = NULL;
354 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
356 LINEDEF *line_def = es->first_line_def;
357 while (line_def)
359 EDIT_InvalidateUniscribeData_linedef(line_def);
360 line_def = line_def->next;
362 if (es->ssa)
364 ScriptStringFree(&es->ssa);
365 es->ssa = NULL;
369 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
371 if (!line_def)
372 return NULL;
374 if (line_def->net_length && !line_def->ssa)
376 int index = line_def->index;
377 HFONT old_font = NULL;
378 HDC udc = dc;
379 SCRIPT_TABDEF tabdef;
380 HRESULT hr;
382 if (!udc)
383 udc = NtUserGetDC(es->hwndSelf);
384 if (es->font)
385 old_font = SelectObject(udc, es->font);
387 tabdef.cTabStops = es->tabs_count;
388 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
389 tabdef.pTabStops = es->tabs;
390 tabdef.iTabOrigin = 0;
392 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
393 (1.5*line_def->net_length+16), -1,
394 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
395 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
396 if (FAILED(hr))
398 WARN("ScriptStringAnalyse failed (%lx)\n",hr);
399 line_def->ssa = NULL;
402 if (es->font)
403 SelectObject(udc, old_font);
404 if (udc != dc)
405 NtUserReleaseDC( es->hwndSelf, udc );
408 return line_def->ssa;
411 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
413 LINEDEF *line_def;
415 if (!(es->style & ES_MULTILINE))
417 if (!es->ssa)
419 INT length = get_text_length(es);
420 HFONT old_font = NULL;
421 HDC udc = dc;
423 if (!udc)
424 udc = NtUserGetDC(es->hwndSelf);
425 if (es->font)
426 old_font = SelectObject(udc, es->font);
428 if (es->style & ES_PASSWORD)
429 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);
430 else
431 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
433 if (es->font)
434 SelectObject(udc, old_font);
435 if (udc != dc)
436 NtUserReleaseDC( es->hwndSelf, udc );
438 return es->ssa;
440 else
442 line_def = es->first_line_def;
443 while (line_def && line)
445 line_def = line_def->next;
446 line--;
449 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
453 static inline INT get_vertical_line_count(EDITSTATE *es)
455 INT vlc = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
456 return max(1,vlc);
459 /*********************************************************************
461 * EDIT_BuildLineDefs_ML
463 * Build linked list of text lines.
464 * Lines can end with '\0' (last line), a character (if it is wrapped),
465 * a soft return '\r\r\n' or a hard return '\r\n'
468 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
470 LPWSTR current_position, cp;
471 INT fw;
472 LINEDEF *current_line;
473 LINEDEF *previous_line;
474 LINEDEF *start_line;
475 INT line_index = 0, nstart_line, nstart_index;
476 INT line_count = es->line_count;
477 INT orig_net_length;
478 RECT rc;
479 INT vlc;
481 if (istart == iend && delta == 0)
482 return;
484 previous_line = NULL;
485 current_line = es->first_line_def;
487 /* Find starting line. istart must lie inside an existing line or
488 * at the end of buffer */
489 do {
490 if (istart < current_line->index + current_line->length ||
491 current_line->ending == END_0)
492 break;
494 previous_line = current_line;
495 current_line = current_line->next;
496 line_index++;
497 } while (current_line);
499 if (!current_line) /* Error occurred start is not inside previous buffer */
501 FIXME(" modification occurred outside buffer\n");
502 return;
505 /* Remember start of modifications in order to calculate update region */
506 nstart_line = line_index;
507 nstart_index = current_line->index;
509 /* We must start to reformat from the previous line since the modifications
510 * may have caused the line to wrap upwards. */
511 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
513 line_index--;
514 current_line = previous_line;
516 start_line = current_line;
518 fw = es->format_rect.right - es->format_rect.left;
519 current_position = es->text + current_line->index;
520 vlc = get_vertical_line_count(es);
521 do {
522 if (current_line != start_line)
524 if (!current_line || current_line->index + delta > current_position - es->text)
526 /* The buffer has been expanded, create a new line and
527 insert it into the link list */
528 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
529 new_line->next = previous_line->next;
530 previous_line->next = new_line;
531 current_line = new_line;
532 es->line_count++;
534 else if (current_line->index + delta < current_position - es->text)
536 /* The previous line merged with this line so we delete this extra entry */
537 previous_line->next = current_line->next;
538 HeapFree(GetProcessHeap(), 0, current_line);
539 current_line = previous_line->next;
540 es->line_count--;
541 continue;
543 else /* current_line->index + delta == current_position */
545 if (current_position - es->text > iend)
546 break; /* We reached end of line modifications */
547 /* else recalculate this line */
551 current_line->index = current_position - es->text;
552 orig_net_length = current_line->net_length;
554 /* Find end of line */
555 cp = current_position;
556 while (*cp) {
557 if (*cp == '\n') break;
558 if ((*cp == '\r') && (*(cp + 1) == '\n'))
559 break;
560 cp++;
563 /* Mark type of line termination */
564 if (!(*cp)) {
565 current_line->ending = END_0;
566 current_line->net_length = lstrlenW(current_position);
567 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
568 current_line->ending = END_SOFT;
569 current_line->net_length = cp - current_position - 1;
570 } else if (*cp == '\n') {
571 current_line->ending = END_RICH;
572 current_line->net_length = cp - current_position;
573 } else {
574 current_line->ending = END_HARD;
575 current_line->net_length = cp - current_position;
578 if (current_line->net_length)
580 const SIZE *sz;
581 EDIT_InvalidateUniscribeData_linedef(current_line);
582 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
583 if (current_line->ssa)
585 sz = ScriptString_pSize(current_line->ssa);
586 /* Calculate line width */
587 current_line->width = sz->cx;
589 else current_line->width = es->char_width * current_line->net_length;
591 else current_line->width = 0;
593 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
595 /* Line breaks just look back from the end and find the next break and try that. */
597 if (!(es->style & ES_AUTOHSCROLL)) {
598 if (current_line->width > fw && fw > es->char_width) {
600 INT prev, next;
601 int w;
602 const SIZE *sz;
603 float d;
605 prev = current_line->net_length - 1;
606 w = current_line->net_length;
607 d = (float)current_line->width/(float)fw;
608 if (d > 1.2f) d -= 0.2f;
609 next = prev/d;
610 if (next >= prev) next = prev-1;
611 do {
612 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
613 next, current_line->net_length, WB_LEFT);
614 current_line->net_length = prev;
615 EDIT_InvalidateUniscribeData_linedef(current_line);
616 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
617 if (current_line->ssa)
618 sz = ScriptString_pSize(current_line->ssa);
619 else sz = 0;
620 if (sz)
621 current_line->width = sz->cx;
622 else
623 prev = 0;
624 next = prev - 1;
625 } while (prev && current_line->width > fw);
626 current_line->net_length = w;
628 if (prev == 0) { /* Didn't find a line break so force a break */
629 INT *piDx;
630 const INT *count;
632 EDIT_InvalidateUniscribeData_linedef(current_line);
633 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
635 if (current_line->ssa)
637 count = ScriptString_pcOutChars(current_line->ssa);
638 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
639 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
641 prev = current_line->net_length-1;
642 do {
643 current_line->width -= piDx[prev];
644 prev--;
645 } while ( prev > 0 && current_line->width > fw);
646 if (prev<=0)
647 prev = 1;
648 HeapFree(GetProcessHeap(),0,piDx);
650 else
651 prev = (fw / es->char_width);
654 /* If the first line we are calculating, wrapped before istart, we must
655 * adjust istart in order for this to be reflected in the update region. */
656 if (current_line->index == nstart_index && istart > current_line->index + prev)
657 istart = current_line->index + prev;
658 /* else if we are updating the previous line before the first line we
659 * are re-calculating and it expanded */
660 else if (current_line == start_line &&
661 current_line->index != nstart_index && orig_net_length < prev)
663 /* Line expanded due to an upwards line wrap so we must partially include
664 * previous line in update region */
665 nstart_line = line_index;
666 nstart_index = current_line->index;
667 istart = current_line->index + orig_net_length;
670 current_line->net_length = prev;
671 current_line->ending = END_WRAP;
673 if (current_line->net_length > 0)
675 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
676 if (current_line->ssa)
678 sz = ScriptString_pSize(current_line->ssa);
679 current_line->width = sz->cx;
681 else
682 current_line->width = 0;
684 else current_line->width = 0;
686 else if (current_line == start_line &&
687 current_line->index != nstart_index &&
688 orig_net_length < current_line->net_length) {
689 /* The previous line expanded but it's still not as wide as the client rect */
690 /* The expansion is due to an upwards line wrap so we must partially include
691 it in the update region */
692 nstart_line = line_index;
693 nstart_index = current_line->index;
694 istart = current_line->index + orig_net_length;
699 /* Adjust length to include line termination */
700 switch (current_line->ending) {
701 case END_SOFT:
702 current_line->length = current_line->net_length + 3;
703 break;
704 case END_RICH:
705 current_line->length = current_line->net_length + 1;
706 break;
707 case END_HARD:
708 current_line->length = current_line->net_length + 2;
709 break;
710 case END_WRAP:
711 case END_0:
712 current_line->length = current_line->net_length;
713 break;
715 es->text_width = max(es->text_width, current_line->width);
716 current_position += current_line->length;
717 previous_line = current_line;
719 /* Discard data for non-visible lines. It will be calculated as needed */
720 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
721 EDIT_InvalidateUniscribeData_linedef(current_line);
723 current_line = current_line->next;
724 line_index++;
725 } while (previous_line->ending != END_0);
727 /* Finish adjusting line indexes by delta or remove hanging lines */
728 if (previous_line->ending == END_0)
730 LINEDEF *pnext = NULL;
732 previous_line->next = NULL;
733 while (current_line)
735 pnext = current_line->next;
736 EDIT_InvalidateUniscribeData_linedef(current_line);
737 HeapFree(GetProcessHeap(), 0, current_line);
738 current_line = pnext;
739 es->line_count--;
742 else if (delta != 0)
744 while (current_line)
746 current_line->index += delta;
747 current_line = current_line->next;
751 /* Calculate rest of modification rectangle */
752 if (hrgn)
754 HRGN tmphrgn;
756 * We calculate two rectangles. One for the first line which may have
757 * an indent with respect to the format rect. The other is a format-width
758 * rectangle that spans the rest of the lines that changed or moved.
760 rc.top = es->format_rect.top + nstart_line * es->line_height -
761 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
762 rc.bottom = rc.top + es->line_height;
763 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
764 rc.left = es->format_rect.left;
765 else
766 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
767 rc.right = es->format_rect.right;
768 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
770 rc.top = rc.bottom;
771 rc.left = es->format_rect.left;
772 rc.right = es->format_rect.right;
774 * If lines were added or removed we must re-paint the remainder of the
775 * lines since the remaining lines were either shifted up or down.
777 if (line_count < es->line_count) /* We added lines */
778 rc.bottom = es->line_count * es->line_height;
779 else if (line_count > es->line_count) /* We removed lines */
780 rc.bottom = line_count * es->line_height;
781 else
782 rc.bottom = line_index * es->line_height;
783 rc.bottom += es->format_rect.top;
784 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
785 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
786 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
787 DeleteObject(tmphrgn);
791 /*********************************************************************
793 * EDIT_CalcLineWidth_SL
796 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
798 EDIT_UpdateUniscribeData(es, NULL, 0);
799 if (es->ssa)
801 const SIZE *size;
802 size = ScriptString_pSize(es->ssa);
803 es->text_width = size->cx;
805 else
806 es->text_width = 0;
809 /*********************************************************************
811 * EDIT_CharFromPos
813 * Beware: This is not the function called on EM_CHARFROMPOS
814 * The position _can_ be outside the formatting / client
815 * rectangle
816 * The return value is only the character index
819 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
821 INT index;
823 if (es->style & ES_MULTILINE) {
824 int trailing;
825 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
826 INT line_index = 0;
827 LINEDEF *line_def = es->first_line_def;
828 EDIT_UpdateUniscribeData(es, NULL, line);
829 while ((line > 0) && line_def->next) {
830 line_index += line_def->length;
831 line_def = line_def->next;
832 line--;
835 x += es->x_offset - es->format_rect.left;
836 if (es->style & ES_RIGHT)
837 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
838 else if (es->style & ES_CENTER)
839 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
840 if (x >= line_def->width) {
841 if (after_wrap)
842 *after_wrap = (line_def->ending == END_WRAP);
843 return line_index + line_def->net_length;
845 if (x <= 0 || !line_def->ssa) {
846 if (after_wrap)
847 *after_wrap = FALSE;
848 return line_index;
851 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
852 if (trailing) index++;
853 index += line_index;
854 if (after_wrap)
855 *after_wrap = ((index == line_index + line_def->net_length) &&
856 (line_def->ending == END_WRAP));
857 } else {
858 INT xoff = 0;
859 INT trailing;
860 if (after_wrap)
861 *after_wrap = FALSE;
862 x -= es->format_rect.left;
863 if (!x)
864 return es->x_offset;
866 if (!es->x_offset)
868 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
869 if (es->style & ES_RIGHT)
870 x -= indent;
871 else if (es->style & ES_CENTER)
872 x -= indent / 2;
875 EDIT_UpdateUniscribeData(es, NULL, 0);
876 if (es->x_offset)
878 if (es->ssa)
880 if (es->x_offset>= get_text_length(es))
882 const SIZE *size;
883 size = ScriptString_pSize(es->ssa);
884 xoff = size->cx;
886 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
888 else
889 xoff = 0;
891 if (x < 0)
893 if (x + xoff > 0 || !es->ssa)
895 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
896 if (trailing) index++;
898 else
899 index = 0;
901 else
903 if (x)
905 const SIZE *size = NULL;
906 if (es->ssa)
907 size = ScriptString_pSize(es->ssa);
908 if (!size)
909 index = 0;
910 else if (x > size->cx)
911 index = get_text_length(es);
912 else if (es->ssa)
914 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
915 if (trailing) index++;
917 else
918 index = 0;
920 else
921 index = es->x_offset;
924 return index;
928 /*********************************************************************
930 * EDIT_ConfinePoint
932 * adjusts the point to be within the formatting rectangle
933 * (so CharFromPos returns the nearest _visible_ character)
936 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
938 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
939 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
943 /*********************************************************************
945 * EM_LINEFROMCHAR
948 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
950 INT line;
951 LINEDEF *line_def;
953 if (!(es->style & ES_MULTILINE))
954 return 0;
955 if (index > (INT)get_text_length(es))
956 return es->line_count - 1;
957 if (index == -1)
958 index = min(es->selection_start, es->selection_end);
960 line = 0;
961 line_def = es->first_line_def;
962 index -= line_def->length;
963 while ((index >= 0) && line_def->next) {
964 line++;
965 line_def = line_def->next;
966 index -= line_def->length;
968 return line;
972 /*********************************************************************
974 * EM_LINEINDEX
977 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
979 INT line_index;
980 const LINEDEF *line_def;
982 if (!(es->style & ES_MULTILINE))
983 return 0;
984 if (line >= es->line_count)
985 return -1;
987 line_index = 0;
988 line_def = es->first_line_def;
989 if (line == -1) {
990 INT index = es->selection_end - line_def->length;
991 while ((index >= 0) && line_def->next) {
992 line_index += line_def->length;
993 line_def = line_def->next;
994 index -= line_def->length;
996 } else {
997 while (line > 0) {
998 line_index += line_def->length;
999 line_def = line_def->next;
1000 line--;
1003 return line_index;
1007 /*********************************************************************
1009 * EM_LINELENGTH
1012 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1014 LINEDEF *line_def;
1016 if (!(es->style & ES_MULTILINE))
1017 return get_text_length(es);
1019 if (index == -1) {
1020 /* get the number of remaining non-selected chars of selected lines */
1021 INT32 l; /* line number */
1022 INT32 li; /* index of first char in line */
1023 INT32 count;
1024 l = EDIT_EM_LineFromChar(es, es->selection_start);
1025 /* # chars before start of selection area */
1026 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1027 l = EDIT_EM_LineFromChar(es, es->selection_end);
1028 /* # chars after end of selection */
1029 li = EDIT_EM_LineIndex(es, l);
1030 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1031 return count;
1033 line_def = es->first_line_def;
1034 index -= line_def->length;
1035 while ((index >= 0) && line_def->next) {
1036 line_def = line_def->next;
1037 index -= line_def->length;
1039 return line_def->net_length;
1043 /*********************************************************************
1045 * EM_POSFROMCHAR
1048 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1050 INT len = get_text_length(es);
1051 INT l;
1052 INT li;
1053 INT x = 0;
1054 INT y = 0;
1055 INT w;
1056 INT lw;
1057 LINEDEF *line_def;
1059 index = min(index, len);
1060 if (es->style & ES_MULTILINE) {
1061 l = EDIT_EM_LineFromChar(es, index);
1062 EDIT_UpdateUniscribeData(es, NULL, l);
1064 y = (l - es->y_offset) * es->line_height;
1065 li = EDIT_EM_LineIndex(es, l);
1066 if (after_wrap && (li == index) && l) {
1067 INT l2 = l - 1;
1068 line_def = es->first_line_def;
1069 while (l2) {
1070 line_def = line_def->next;
1071 l2--;
1073 if (line_def->ending == END_WRAP) {
1074 l--;
1075 y -= es->line_height;
1076 li = EDIT_EM_LineIndex(es, l);
1080 line_def = es->first_line_def;
1081 while (line_def->index != li)
1082 line_def = line_def->next;
1084 lw = line_def->width;
1085 w = es->format_rect.right - es->format_rect.left;
1086 if (line_def->ssa)
1087 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1088 x -= es->x_offset;
1090 if (es->style & ES_RIGHT)
1091 x = w - (lw - x);
1092 else if (es->style & ES_CENTER)
1093 x += (w - lw) / 2;
1094 } else {
1095 INT xoff = 0;
1096 INT xi = 0;
1097 EDIT_UpdateUniscribeData(es, NULL, 0);
1098 if (es->x_offset)
1100 if (es->ssa)
1102 if (es->x_offset >= get_text_length(es))
1104 int leftover = es->x_offset - get_text_length(es);
1105 if (es->ssa)
1107 const SIZE *size;
1108 size = ScriptString_pSize(es->ssa);
1109 xoff = size->cx;
1111 else
1112 xoff = 0;
1113 xoff += es->char_width * leftover;
1115 else
1116 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1118 else
1119 xoff = 0;
1121 if (index)
1123 if (index >= get_text_length(es))
1125 if (es->ssa)
1127 const SIZE *size;
1128 size = ScriptString_pSize(es->ssa);
1129 xi = size->cx;
1131 else
1132 xi = 0;
1134 else if (es->ssa)
1135 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1136 else
1137 xi = 0;
1139 x = xi - xoff;
1141 if (index >= es->x_offset) {
1142 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1144 w = es->format_rect.right - es->format_rect.left;
1145 if (w > es->text_width)
1147 if (es->style & ES_RIGHT)
1148 x += w - es->text_width;
1149 else if (es->style & ES_CENTER)
1150 x += (w - es->text_width) / 2;
1154 y = 0;
1156 x += es->format_rect.left;
1157 y += es->format_rect.top;
1158 return MAKELONG((INT16)x, (INT16)y);
1162 /*********************************************************************
1164 * EDIT_GetLineRect
1166 * Calculates the bounding rectangle for a line from a starting
1167 * column to an ending column.
1170 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1172 SCRIPT_STRING_ANALYSIS ssa;
1173 INT line_index = 0;
1174 INT pt1, pt2, pt3;
1176 if (es->style & ES_MULTILINE)
1178 const LINEDEF *line_def = NULL;
1179 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1180 if (line >= es->line_count)
1181 return;
1183 line_def = es->first_line_def;
1184 if (line == -1) {
1185 INT index = es->selection_end - line_def->length;
1186 while ((index >= 0) && line_def->next) {
1187 line_index += line_def->length;
1188 line_def = line_def->next;
1189 index -= line_def->length;
1191 } else {
1192 while (line > 0) {
1193 line_index += line_def->length;
1194 line_def = line_def->next;
1195 line--;
1198 ssa = line_def->ssa;
1200 else
1202 line_index = 0;
1203 rc->top = es->format_rect.top;
1204 ssa = es->ssa;
1207 rc->bottom = rc->top + es->line_height;
1208 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1209 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1210 if (ssa)
1212 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1213 pt3+=es->format_rect.left;
1215 else pt3 = pt1;
1216 rc->right = max(max(pt1 , pt2),pt3);
1217 rc->left = min(min(pt1, pt2),pt3);
1221 static inline void text_buffer_changed(EDITSTATE *es)
1223 es->text_length = (UINT)-1;
1225 HeapFree( GetProcessHeap(), 0, es->logAttr );
1226 es->logAttr = NULL;
1227 EDIT_InvalidateUniscribeData(es);
1230 /*********************************************************************
1231 * EDIT_LockBuffer
1234 static void EDIT_LockBuffer(EDITSTATE *es)
1236 if (!es->text) {
1238 if(!es->hloc32W) return;
1240 if(es->hloc32A)
1242 CHAR *textA = LocalLock(es->hloc32A);
1243 HLOCAL hloc32W_new;
1244 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1245 if(countW_new > es->buffer_size + 1)
1247 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1248 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1249 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1250 if(hloc32W_new)
1252 es->hloc32W = hloc32W_new;
1253 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1254 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1256 else
1257 WARN("FAILED! Will synchronize partially\n");
1259 es->text = LocalLock(es->hloc32W);
1260 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1261 LocalUnlock(es->hloc32A);
1263 else es->text = LocalLock(es->hloc32W);
1265 es->lock_count++;
1269 /*********************************************************************
1271 * EDIT_UnlockBuffer
1274 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1276 /* Edit window might be already destroyed */
1277 if(!IsWindow(es->hwndSelf))
1279 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1280 return;
1283 if (!es->lock_count) {
1284 ERR("lock_count == 0 ... please report\n");
1285 return;
1287 if (!es->text) {
1288 ERR("es->text == 0 ... please report\n");
1289 return;
1291 if (force || (es->lock_count == 1)) {
1292 if (es->hloc32W) {
1293 UINT countA = 0;
1294 UINT countW = get_text_length(es) + 1;
1296 if(es->hloc32A)
1298 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1299 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1300 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1301 countA = LocalSize(es->hloc32A);
1302 if(countA_new > countA)
1304 HLOCAL hloc32A_new;
1305 UINT alloc_size = ROUND_TO_GROW(countA_new);
1306 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1307 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1308 if(hloc32A_new)
1310 es->hloc32A = hloc32A_new;
1311 countA = LocalSize(hloc32A_new);
1312 TRACE("Real new size %d bytes\n", countA);
1314 else
1315 WARN("FAILED! Will synchronize partially\n");
1317 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1318 LocalLock(es->hloc32A), countA, NULL, NULL);
1319 LocalUnlock(es->hloc32A);
1322 LocalUnlock(es->hloc32W);
1323 es->text = NULL;
1325 else {
1326 ERR("no buffer ... please report\n");
1327 return;
1330 es->lock_count--;
1334 /*********************************************************************
1336 * EDIT_MakeFit
1338 * Try to fit size + 1 characters in the buffer.
1340 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1342 HLOCAL hNew32W;
1344 if (size <= es->buffer_size)
1345 return TRUE;
1347 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1349 /* Force edit to unlock its buffer. es->text now NULL */
1350 EDIT_UnlockBuffer(es, TRUE);
1352 if (es->hloc32W) {
1353 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1354 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1355 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1356 es->hloc32W = hNew32W;
1357 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1361 EDIT_LockBuffer(es);
1363 if (es->buffer_size < size) {
1364 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1365 notify_parent(es, EN_ERRSPACE);
1366 return FALSE;
1367 } else {
1368 TRACE("We now have %d+1\n", es->buffer_size);
1369 return TRUE;
1374 /*********************************************************************
1376 * EDIT_MakeUndoFit
1378 * Try to fit size + 1 bytes in the undo buffer.
1381 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1383 UINT alloc_size;
1385 if (size <= es->undo_buffer_size)
1386 return TRUE;
1388 TRACE("trying to ReAlloc to %d+1\n", size);
1390 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1391 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1392 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1393 return TRUE;
1395 else
1397 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1398 return FALSE;
1403 /*********************************************************************
1405 * EDIT_UpdateTextRegion
1408 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1410 if (es->flags & EF_UPDATE) {
1411 es->flags &= ~EF_UPDATE;
1412 if (!notify_parent(es, EN_UPDATE)) return;
1414 NtUserInvalidateRgn(es->hwndSelf, hrgn, bErase);
1418 /*********************************************************************
1420 * EDIT_UpdateText
1423 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1425 if (es->flags & EF_UPDATE) {
1426 es->flags &= ~EF_UPDATE;
1427 if (!notify_parent(es, EN_UPDATE)) return;
1429 NtUserInvalidateRect(es->hwndSelf, rc, bErase);
1432 /*********************************************************************
1434 * EDIT_SL_InvalidateText
1436 * Called from EDIT_InvalidateText().
1437 * Does the job for single-line controls only.
1440 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1442 RECT line_rect;
1443 RECT rc;
1445 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1446 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1447 EDIT_UpdateText(es, &rc, TRUE);
1450 /*********************************************************************
1452 * EDIT_ML_InvalidateText
1454 * Called from EDIT_InvalidateText().
1455 * Does the job for multi-line controls only.
1458 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1460 INT vlc = get_vertical_line_count(es);
1461 INT sl = EDIT_EM_LineFromChar(es, start);
1462 INT el = EDIT_EM_LineFromChar(es, end);
1463 INT sc;
1464 INT ec;
1465 RECT rc1;
1466 RECT rcWnd;
1467 RECT rcLine;
1468 RECT rcUpdate;
1469 INT l;
1471 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1472 return;
1474 sc = start - EDIT_EM_LineIndex(es, sl);
1475 ec = end - EDIT_EM_LineIndex(es, el);
1476 if (sl < es->y_offset) {
1477 sl = es->y_offset;
1478 sc = 0;
1480 if (el > es->y_offset + vlc) {
1481 el = es->y_offset + vlc;
1482 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1484 GetClientRect(es->hwndSelf, &rc1);
1485 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1486 if (sl == el) {
1487 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1488 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1489 EDIT_UpdateText(es, &rcUpdate, TRUE);
1490 } else {
1491 EDIT_GetLineRect(es, sl, sc,
1492 EDIT_EM_LineLength(es,
1493 EDIT_EM_LineIndex(es, sl)),
1494 &rcLine);
1495 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1496 EDIT_UpdateText(es, &rcUpdate, TRUE);
1497 for (l = sl + 1 ; l < el ; l++) {
1498 EDIT_GetLineRect(es, l, 0,
1499 EDIT_EM_LineLength(es,
1500 EDIT_EM_LineIndex(es, l)),
1501 &rcLine);
1502 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1503 EDIT_UpdateText(es, &rcUpdate, TRUE);
1505 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1506 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1507 EDIT_UpdateText(es, &rcUpdate, TRUE);
1512 /*********************************************************************
1514 * EDIT_InvalidateText
1516 * Invalidate the text from offset start up to, but not including,
1517 * offset end. Useful for (re)painting the selection.
1518 * Regions outside the linewidth are not invalidated.
1519 * end == -1 means end == TextLength.
1520 * start and end need not be ordered.
1523 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1525 if (end == start)
1526 return;
1528 if (end == -1)
1529 end = get_text_length(es);
1531 if (end < start) {
1532 INT tmp = start;
1533 start = end;
1534 end = tmp;
1537 if (es->style & ES_MULTILINE)
1538 EDIT_ML_InvalidateText(es, start, end);
1539 else
1540 EDIT_SL_InvalidateText(es, start, end);
1544 /*********************************************************************
1546 * EDIT_EM_SetSel
1548 * note: unlike the specs say: the order of start and end
1549 * _is_ preserved in Windows. (i.e. start can be > end)
1550 * In other words: this handler is OK
1553 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1555 UINT old_start = es->selection_start;
1556 UINT old_end = es->selection_end;
1557 UINT len = get_text_length(es);
1559 if (start == (UINT)-1) {
1560 start = es->selection_end;
1561 end = es->selection_end;
1562 } else {
1563 start = min(start, len);
1564 end = min(end, len);
1566 es->selection_start = start;
1567 es->selection_end = end;
1568 if (after_wrap)
1569 es->flags |= EF_AFTER_WRAP;
1570 else
1571 es->flags &= ~EF_AFTER_WRAP;
1572 /* Compute the necessary invalidation region. */
1573 /* Note that we don't need to invalidate regions which have
1574 * "never" been selected, or those which are "still" selected.
1575 * In fact, every time we hit a selection boundary, we can
1576 * *toggle* whether we need to invalidate. Thus we can optimize by
1577 * *sorting* the interval endpoints. Let's assume that we sort them
1578 * in this order:
1579 * start <= end <= old_start <= old_end
1580 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1581 * in 5 comparisons; i.e. it is impossible to do better than the
1582 * following: */
1583 ORDER_UINT(end, old_end);
1584 ORDER_UINT(start, old_start);
1585 ORDER_UINT(old_start, old_end);
1586 ORDER_UINT(start, end);
1587 /* Note that at this point 'end' and 'old_start' are not in order, but
1588 * start is definitely the min. and old_end is definitely the max. */
1589 if (end != old_start)
1592 * One can also do
1593 * ORDER_UINT32(end, old_start);
1594 * EDIT_InvalidateText(es, start, end);
1595 * EDIT_InvalidateText(es, old_start, old_end);
1596 * in place of the following if statement.
1597 * (That would complete the optimal five-comparison four-element sort.)
1599 if (old_start > end )
1601 EDIT_InvalidateText(es, start, end);
1602 EDIT_InvalidateText(es, old_start, old_end);
1604 else
1606 EDIT_InvalidateText(es, start, old_start);
1607 EDIT_InvalidateText(es, end, old_end);
1610 else EDIT_InvalidateText(es, start, old_end);
1614 /*********************************************************************
1616 * EDIT_UpdateScrollInfo
1619 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1621 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1623 SCROLLINFO si;
1624 si.cbSize = sizeof(SCROLLINFO);
1625 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1626 si.nMin = 0;
1627 si.nMax = es->line_count - 1;
1628 si.nPage = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
1629 si.nPos = es->y_offset;
1630 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1631 si.nMin, si.nMax, si.nPage, si.nPos);
1632 NtUserSetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1635 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1637 SCROLLINFO si;
1638 si.cbSize = sizeof(SCROLLINFO);
1639 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1640 si.nMin = 0;
1641 si.nMax = es->text_width - 1;
1642 si.nPage = es->format_rect.right - es->format_rect.left;
1643 si.nPos = es->x_offset;
1644 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1645 si.nMin, si.nMax, si.nPage, si.nPos);
1646 NtUserSetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1651 /*********************************************************************
1653 * EDIT_EM_LineScroll_internal
1655 * Version of EDIT_EM_LineScroll for internal use.
1656 * It doesn't refuse if ES_MULTILINE is set and assumes that
1657 * dx is in pixels, dy - in lines.
1660 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1662 INT nyoff;
1663 INT x_offset_in_pixels;
1664 INT lines_per_page;
1666 if (!es->line_height || !es->char_width)
1667 return TRUE;
1669 lines_per_page = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1671 if (es->style & ES_MULTILINE)
1673 x_offset_in_pixels = es->x_offset;
1675 else
1677 dy = 0;
1678 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1681 if (-dx > x_offset_in_pixels)
1682 dx = -x_offset_in_pixels;
1683 if (dx > es->text_width - x_offset_in_pixels)
1684 dx = es->text_width - x_offset_in_pixels;
1685 nyoff = max(0, es->y_offset + dy);
1686 if (nyoff >= es->line_count - lines_per_page)
1687 nyoff = max(0, es->line_count - lines_per_page);
1688 dy = (es->y_offset - nyoff) * es->line_height;
1689 if (dx || dy) {
1690 RECT rc1;
1691 RECT rc;
1693 es->y_offset = nyoff;
1694 if(es->style & ES_MULTILINE)
1695 es->x_offset += dx;
1696 else
1697 es->x_offset += dx / es->char_width;
1699 GetClientRect(es->hwndSelf, &rc1);
1700 IntersectRect(&rc, &rc1, &es->format_rect);
1701 NtUserScrollWindowEx(es->hwndSelf, -dx, dy,
1702 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1703 /* force scroll info update */
1704 EDIT_UpdateScrollInfo(es);
1706 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1707 notify_parent(es, EN_HSCROLL);
1708 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1709 notify_parent(es, EN_VSCROLL);
1710 return TRUE;
1713 /*********************************************************************
1715 * EM_LINESCROLL
1717 * NOTE: dx is in average character widths, dy - in lines;
1720 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1722 if (!(es->style & ES_MULTILINE))
1723 return FALSE;
1725 dx *= es->char_width;
1726 return EDIT_EM_LineScroll_internal(es, dx, dy);
1730 /*********************************************************************
1732 * EM_SCROLL
1735 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1737 INT dy;
1739 if (!(es->style & ES_MULTILINE))
1740 return (LRESULT)FALSE;
1742 dy = 0;
1744 switch (action) {
1745 case SB_LINEUP:
1746 if (es->y_offset)
1747 dy = -1;
1748 break;
1749 case SB_LINEDOWN:
1750 if (es->y_offset < es->line_count - 1)
1751 dy = 1;
1752 break;
1753 case SB_PAGEUP:
1754 if (es->y_offset)
1755 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1756 break;
1757 case SB_PAGEDOWN:
1758 if (es->y_offset < es->line_count - 1)
1759 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1760 break;
1761 default:
1762 return (LRESULT)FALSE;
1764 if (dy) {
1765 INT vlc = get_vertical_line_count(es);
1766 /* check if we are going to move too far */
1767 if(es->y_offset + dy > es->line_count - vlc)
1768 dy = max(es->line_count - vlc, 0) - es->y_offset;
1770 /* Notification is done in EDIT_EM_LineScroll */
1771 if(dy) {
1772 EDIT_EM_LineScroll(es, 0, dy);
1773 return MAKELONG(dy, TRUE);
1777 return (LRESULT)FALSE;
1781 static void EDIT_UpdateImmCompositionWindow(EDITSTATE *es, UINT x, UINT y)
1783 COMPOSITIONFORM form =
1785 .dwStyle = CFS_RECT,
1786 .ptCurrentPos = {.x = x, .y = y},
1787 .rcArea = es->format_rect,
1789 HIMC himc = ImmGetContext(es->hwndSelf);
1790 ImmSetCompositionWindow(himc, &form);
1791 ImmReleaseContext(es->hwndSelf, himc);
1795 /*********************************************************************
1797 * EDIT_SetCaretPos
1800 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1801 BOOL after_wrap)
1803 LRESULT res;
1805 if (es->flags & EF_FOCUSED)
1807 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));
1810 EDIT_UpdateImmCompositionWindow(es, (short)LOWORD(res), (short)HIWORD(res));
1815 /*********************************************************************
1817 * EM_SCROLLCARET
1820 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1822 if (es->style & ES_MULTILINE) {
1823 INT l;
1824 INT vlc;
1825 INT ww;
1826 INT cw = es->char_width;
1827 INT x;
1828 INT dy = 0;
1829 INT dx = 0;
1831 l = EDIT_EM_LineFromChar(es, es->selection_end);
1832 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1833 vlc = get_vertical_line_count(es);
1834 if (l >= es->y_offset + vlc)
1835 dy = l - vlc + 1 - es->y_offset;
1836 if (l < es->y_offset)
1837 dy = l - es->y_offset;
1838 ww = es->format_rect.right - es->format_rect.left;
1839 if (x < es->format_rect.left)
1840 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1841 if (x > es->format_rect.right)
1842 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1843 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1845 /* check if we are going to move too far */
1846 if(es->x_offset + dx + ww > es->text_width)
1847 dx = es->text_width - ww - es->x_offset;
1848 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1849 EDIT_EM_LineScroll_internal(es, dx, dy);
1851 } else {
1852 INT x;
1853 INT goal;
1854 INT format_width;
1856 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1857 format_width = es->format_rect.right - es->format_rect.left;
1858 if (x < es->format_rect.left) {
1859 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1860 do {
1861 es->x_offset--;
1862 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1863 } while ((x < goal) && es->x_offset);
1864 /* FIXME: use ScrollWindow() somehow to improve performance */
1865 EDIT_UpdateText(es, NULL, TRUE);
1866 } else if (x > es->format_rect.right) {
1867 INT x_last;
1868 INT len = get_text_length(es);
1869 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1870 do {
1871 es->x_offset++;
1872 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1873 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1874 } while ((x > goal) && (x_last > es->format_rect.right));
1875 /* FIXME: use ScrollWindow() somehow to improve performance */
1876 EDIT_UpdateText(es, NULL, TRUE);
1880 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1884 /*********************************************************************
1886 * EDIT_MoveBackward
1889 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1891 INT e = es->selection_end;
1893 if (e) {
1894 e--;
1895 if ((es->style & ES_MULTILINE) && e &&
1896 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1897 e--;
1898 if (e && (es->text[e - 1] == '\r'))
1899 e--;
1902 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1903 EDIT_EM_ScrollCaret(es);
1907 /*********************************************************************
1909 * EDIT_MoveDown_ML
1911 * Only for multi line controls
1912 * Move the caret one line down, on a column with the nearest
1913 * x coordinate on the screen (might be a different column).
1916 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1918 INT s = es->selection_start;
1919 INT e = es->selection_end;
1920 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1921 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1922 INT x = (short)LOWORD(pos);
1923 INT y = (short)HIWORD(pos);
1925 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1926 if (!extend)
1927 s = e;
1928 EDIT_EM_SetSel(es, s, e, after_wrap);
1929 EDIT_EM_ScrollCaret(es);
1933 /*********************************************************************
1935 * EDIT_MoveEnd
1938 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1940 BOOL after_wrap = FALSE;
1941 INT e;
1943 /* Pass a high value in x to make sure of receiving the end of the line */
1944 if (!ctrl && (es->style & ES_MULTILINE))
1945 e = EDIT_CharFromPos(es, 0x3fffffff,
1946 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1947 else
1948 e = get_text_length(es);
1949 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1950 EDIT_EM_ScrollCaret(es);
1954 /*********************************************************************
1956 * EDIT_MoveForward
1959 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1961 INT e = es->selection_end;
1963 if (es->text[e]) {
1964 e++;
1965 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1966 if (es->text[e] == '\n')
1967 e++;
1968 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1969 e += 2;
1972 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1973 EDIT_EM_ScrollCaret(es);
1977 /*********************************************************************
1979 * EDIT_MoveHome
1981 * Home key: move to beginning of line.
1984 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1986 INT e;
1988 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1989 if (!ctrl && (es->style & ES_MULTILINE))
1990 e = EDIT_CharFromPos(es, -es->x_offset,
1991 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1992 else
1993 e = 0;
1994 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1995 EDIT_EM_ScrollCaret(es);
1999 /*********************************************************************
2001 * EDIT_MovePageDown_ML
2003 * Only for multi line controls
2004 * Move the caret one page down, on a column with the nearest
2005 * x coordinate on the screen (might be a different column).
2008 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2010 INT s = es->selection_start;
2011 INT e = es->selection_end;
2012 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2013 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2014 INT x = (short)LOWORD(pos);
2015 INT y = (short)HIWORD(pos);
2017 e = EDIT_CharFromPos(es, x,
2018 y + (es->format_rect.bottom - es->format_rect.top),
2019 &after_wrap);
2020 if (!extend)
2021 s = e;
2022 EDIT_EM_SetSel(es, s, e, after_wrap);
2023 EDIT_EM_ScrollCaret(es);
2027 /*********************************************************************
2029 * EDIT_MovePageUp_ML
2031 * Only for multi line controls
2032 * Move the caret one page up, on a column with the nearest
2033 * x coordinate on the screen (might be a different column).
2036 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2038 INT s = es->selection_start;
2039 INT e = es->selection_end;
2040 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2041 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2042 INT x = (short)LOWORD(pos);
2043 INT y = (short)HIWORD(pos);
2045 e = EDIT_CharFromPos(es, x,
2046 y - (es->format_rect.bottom - es->format_rect.top),
2047 &after_wrap);
2048 if (!extend)
2049 s = e;
2050 EDIT_EM_SetSel(es, s, e, after_wrap);
2051 EDIT_EM_ScrollCaret(es);
2055 /*********************************************************************
2057 * EDIT_MoveUp_ML
2059 * Only for multi line controls
2060 * Move the caret one line up, on a column with the nearest
2061 * x coordinate on the screen (might be a different column).
2064 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2066 INT s = es->selection_start;
2067 INT e = es->selection_end;
2068 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2069 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2070 INT x = (short)LOWORD(pos);
2071 INT y = (short)HIWORD(pos);
2073 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2074 if (!extend)
2075 s = e;
2076 EDIT_EM_SetSel(es, s, e, after_wrap);
2077 EDIT_EM_ScrollCaret(es);
2081 /*********************************************************************
2083 * EDIT_MoveWordBackward
2086 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2088 INT s = es->selection_start;
2089 INT e = es->selection_end;
2090 INT l;
2091 INT ll;
2092 INT li;
2094 l = EDIT_EM_LineFromChar(es, e);
2095 ll = EDIT_EM_LineLength(es, e);
2096 li = EDIT_EM_LineIndex(es, l);
2097 if (e - li == 0) {
2098 if (l) {
2099 li = EDIT_EM_LineIndex(es, l - 1);
2100 e = li + EDIT_EM_LineLength(es, li);
2102 } else {
2103 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2105 if (!extend)
2106 s = e;
2107 EDIT_EM_SetSel(es, s, e, FALSE);
2108 EDIT_EM_ScrollCaret(es);
2112 /*********************************************************************
2114 * EDIT_MoveWordForward
2117 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2119 INT s = es->selection_start;
2120 INT e = es->selection_end;
2121 INT l;
2122 INT ll;
2123 INT li;
2125 l = EDIT_EM_LineFromChar(es, e);
2126 ll = EDIT_EM_LineLength(es, e);
2127 li = EDIT_EM_LineIndex(es, l);
2128 if (e - li == ll) {
2129 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2130 e = EDIT_EM_LineIndex(es, l + 1);
2131 } else {
2132 e = li + EDIT_CallWordBreakProc(es,
2133 li, e - li + 1, ll, WB_RIGHT);
2135 if (!extend)
2136 s = e;
2137 EDIT_EM_SetSel(es, s, e, FALSE);
2138 EDIT_EM_ScrollCaret(es);
2142 /*********************************************************************
2144 * EDIT_PaintText
2147 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2149 COLORREF BkColor;
2150 COLORREF TextColor;
2151 INT ret;
2152 INT li;
2153 INT BkMode;
2154 SIZE size;
2156 if (!count)
2157 return 0;
2158 BkMode = GetBkMode(dc);
2159 BkColor = GetBkColor(dc);
2160 TextColor = GetTextColor(dc);
2161 if (rev) {
2162 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2163 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2164 SetBkMode(dc, OPAQUE);
2166 li = EDIT_EM_LineIndex(es, line);
2167 if (es->style & ES_MULTILINE) {
2168 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2169 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2170 } else {
2171 TextOutW(dc, x, y, es->text + li + col, count);
2172 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2173 ret = size.cx;
2175 if (rev) {
2176 SetBkColor(dc, BkColor);
2177 SetTextColor(dc, TextColor);
2178 SetBkMode(dc, BkMode);
2180 return ret;
2184 /*********************************************************************
2186 * EDIT_PaintLine
2189 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2191 INT s = 0;
2192 INT e = 0;
2193 INT li = 0;
2194 INT ll = 0;
2195 INT x;
2196 INT y;
2197 LRESULT pos;
2198 SCRIPT_STRING_ANALYSIS ssa;
2200 if (es->style & ES_MULTILINE) {
2201 INT vlc = get_vertical_line_count(es);
2203 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2204 return;
2205 } else if (line)
2206 return;
2208 TRACE("line=%d\n", line);
2210 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2211 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2212 x = (short)LOWORD(pos);
2213 y = (short)HIWORD(pos);
2215 if (es->style & ES_MULTILINE)
2217 int line_idx = line;
2218 x = -es->x_offset;
2219 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2221 LINEDEF *line_def = es->first_line_def;
2222 int w, lw;
2224 while (line_def && line_idx)
2226 line_def = line_def->next;
2227 line_idx--;
2229 w = es->format_rect.right - es->format_rect.left;
2230 lw = line_def->width;
2232 if (es->style & ES_RIGHT)
2233 x = w - (lw - x);
2234 else if (es->style & ES_CENTER)
2235 x += (w - lw) / 2;
2237 x += es->format_rect.left;
2240 if (rev)
2242 li = EDIT_EM_LineIndex(es, line);
2243 ll = EDIT_EM_LineLength(es, li);
2244 s = min(es->selection_start, es->selection_end);
2245 e = max(es->selection_start, es->selection_end);
2246 s = min(li + ll, max(li, s));
2247 e = min(li + ll, max(li, e));
2250 if (ssa)
2251 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2252 else if (rev && (s != e) &&
2253 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2254 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2255 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2256 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2257 } else
2258 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2262 /*********************************************************************
2264 * EDIT_AdjustFormatRect
2266 * Adjusts the format rectangle for the current font and the
2267 * current client rectangle.
2270 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2272 RECT ClientRect;
2274 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2275 if (es->style & ES_MULTILINE)
2277 INT fw, vlc, max_x_offset, max_y_offset;
2279 vlc = get_vertical_line_count(es);
2280 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2282 /* correct es->x_offset */
2283 fw = es->format_rect.right - es->format_rect.left;
2284 max_x_offset = es->text_width - fw;
2285 if(max_x_offset < 0) max_x_offset = 0;
2286 if(es->x_offset > max_x_offset)
2287 es->x_offset = max_x_offset;
2289 /* correct es->y_offset */
2290 max_y_offset = es->line_count - vlc;
2291 if(max_y_offset < 0) max_y_offset = 0;
2292 if(es->y_offset > max_y_offset)
2293 es->y_offset = max_y_offset;
2295 /* force scroll info update */
2296 EDIT_UpdateScrollInfo(es);
2298 else
2299 /* Windows doesn't care to fix text placement for SL controls */
2300 es->format_rect.bottom = es->format_rect.top + es->line_height;
2302 /* Always stay within the client area */
2303 GetClientRect(es->hwndSelf, &ClientRect);
2304 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2306 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2307 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2309 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2313 /*********************************************************************
2315 * EDIT_SetRectNP
2317 * note: this is not (exactly) the handler called on EM_SETRECTNP
2318 * it is also used to set the rect of a single line control
2321 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2323 LONG_PTR ExStyle;
2324 INT bw, bh;
2325 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2327 CopyRect(&es->format_rect, rc);
2329 if (ExStyle & WS_EX_CLIENTEDGE) {
2330 es->format_rect.left++;
2331 es->format_rect.right--;
2333 if (es->format_rect.bottom - es->format_rect.top
2334 >= es->line_height + 2)
2336 es->format_rect.top++;
2337 es->format_rect.bottom--;
2340 else if (es->style & WS_BORDER) {
2341 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2342 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2343 InflateRect(&es->format_rect, -bw, 0);
2344 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2345 InflateRect(&es->format_rect, 0, -bh);
2348 es->format_rect.left += es->left_margin;
2349 es->format_rect.right -= es->right_margin;
2350 EDIT_AdjustFormatRect(es);
2354 /*********************************************************************
2356 * EM_CHARFROMPOS
2358 * returns line number (not index) in high-order word of result.
2359 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2360 * to Richedit, not to the edit control. Original documentation is valid.
2361 * FIXME: do the specs mean to return -1 if outside client area or
2362 * if outside formatting rectangle ???
2365 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2367 POINT pt;
2368 RECT rc;
2369 INT index;
2371 pt.x = x;
2372 pt.y = y;
2373 GetClientRect(es->hwndSelf, &rc);
2374 if (!PtInRect(&rc, pt))
2375 return -1;
2377 index = EDIT_CharFromPos(es, x, y, NULL);
2378 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2382 /*********************************************************************
2384 * EM_FMTLINES
2386 * Enable or disable soft breaks.
2388 * This means: insert or remove the soft linebreak character (\r\r\n).
2389 * Take care to check if the text still fits the buffer after insertion.
2390 * If not, notify with EN_ERRSPACE.
2393 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2395 es->flags &= ~EF_USE_SOFTBRK;
2396 if (add_eol) {
2397 es->flags |= EF_USE_SOFTBRK;
2398 FIXME("soft break enabled, not implemented\n");
2400 return add_eol;
2404 /*********************************************************************
2406 * EM_GETHANDLE
2408 * Hopefully this won't fire back at us.
2409 * We always start with a fixed buffer in the local heap.
2410 * Despite of the documentation says that the local heap is used
2411 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2412 * buffer on the local heap.
2415 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2417 HLOCAL hLocal;
2419 if (!(es->style & ES_MULTILINE))
2420 return 0;
2422 if(es->is_unicode)
2423 hLocal = es->hloc32W;
2424 else
2426 if(!es->hloc32A)
2428 CHAR *textA;
2429 UINT countA, alloc_size;
2430 TRACE("Allocating 32-bit ANSI alias buffer\n");
2431 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2432 alloc_size = ROUND_TO_GROW(countA);
2433 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2435 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2436 return 0;
2438 textA = LocalLock(es->hloc32A);
2439 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2440 LocalUnlock(es->hloc32A);
2442 hLocal = es->hloc32A;
2445 EDIT_UnlockBuffer(es, TRUE);
2447 /* The text buffer handle belongs to the app */
2448 es->hlocapp = hLocal;
2450 TRACE("Returning %p, LocalSize() = %Id\n", hLocal, LocalSize(hLocal));
2451 return hLocal;
2455 /*********************************************************************
2457 * EM_GETLINE
2460 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2462 LPWSTR src;
2463 INT line_len, dst_len;
2464 INT i;
2466 if (es->style & ES_MULTILINE) {
2467 if (line >= es->line_count)
2468 return 0;
2469 } else
2470 line = 0;
2471 i = EDIT_EM_LineIndex(es, line);
2472 src = es->text + i;
2473 line_len = EDIT_EM_LineLength(es, i);
2474 dst_len = *(WORD *)dst;
2475 if(unicode)
2477 if(dst_len <= line_len)
2479 memcpy(dst, src, dst_len * sizeof(WCHAR));
2480 return dst_len;
2482 else /* Append 0 if enough space */
2484 memcpy(dst, src, line_len * sizeof(WCHAR));
2485 dst[line_len] = 0;
2486 return line_len;
2489 else
2491 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2492 if(!ret && line_len) /* Insufficient buffer size */
2493 return dst_len;
2494 if(ret < dst_len) /* Append 0 if enough space */
2495 ((LPSTR)dst)[ret] = 0;
2496 return ret;
2501 /*********************************************************************
2503 * EM_GETSEL
2506 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2508 UINT s = es->selection_start;
2509 UINT e = es->selection_end;
2511 ORDER_UINT(s, e);
2512 if (start)
2513 *start = s;
2514 if (end)
2515 *end = e;
2516 return MAKELONG(s, e);
2520 /*********************************************************************
2522 * EM_REPLACESEL
2524 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2527 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2528 BOOL send_update, BOOL honor_limit)
2530 UINT tl = get_text_length(es);
2531 UINT utl;
2532 UINT s;
2533 UINT e;
2534 UINT i;
2535 UINT size;
2536 LPWSTR p;
2537 HRGN hrgn = 0;
2538 LPWSTR buf = NULL;
2539 UINT bufl;
2541 TRACE("%s, can_undo %d, send_update %d\n",
2542 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2544 s = es->selection_start;
2545 e = es->selection_end;
2547 EDIT_InvalidateUniscribeData(es);
2548 if ((s == e) && !strl)
2549 return;
2551 ORDER_UINT(s, e);
2553 size = tl - (e - s) + strl;
2554 if (!size)
2555 es->text_width = 0;
2557 /* Issue the EN_MAXTEXT notification and continue with replacing text
2558 * so that buffer limit is honored. */
2559 if ((honor_limit) && (size > es->buffer_limit))
2561 if (!notify_parent(es, EN_MAXTEXT)) return;
2562 /* Buffer limit can be smaller than the actual length of text in combobox */
2563 if (es->buffer_limit < (tl - (e-s)))
2564 strl = 0;
2565 else
2566 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2569 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2570 return;
2572 if (e != s) {
2573 /* there is something to be deleted */
2574 TRACE("deleting stuff.\n");
2575 bufl = e - s;
2576 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2577 if (!buf) return;
2578 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2579 buf[bufl] = 0; /* ensure 0 termination */
2580 /* now delete */
2581 lstrcpyW(es->text + s, es->text + e);
2582 text_buffer_changed(es);
2584 if (strl) {
2585 /* there is an insertion */
2586 tl = get_text_length(es);
2587 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));
2588 for (p = es->text + tl ; p >= es->text + s ; p--)
2589 p[strl] = p[0];
2590 for (i = 0 , p = es->text + s ; i < strl ; i++)
2591 p[i] = lpsz_replace[i];
2592 if(es->style & ES_UPPERCASE)
2593 CharUpperBuffW(p, strl);
2594 else if(es->style & ES_LOWERCASE)
2595 CharLowerBuffW(p, strl);
2596 text_buffer_changed(es);
2598 if (es->style & ES_MULTILINE)
2600 INT st = min(es->selection_start, es->selection_end);
2601 INT vlc = get_vertical_line_count(es);
2603 hrgn = CreateRectRgn(0, 0, 0, 0);
2604 EDIT_BuildLineDefs_ML(es, st, st + strl,
2605 strl - abs(es->selection_end - es->selection_start), hrgn);
2606 /* if text is too long undo all changes */
2607 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2608 if (strl)
2609 lstrcpyW(es->text + e, es->text + e + strl);
2610 if (e != s)
2611 for (i = 0 , p = es->text ; i < e - s ; i++)
2612 p[i + s] = buf[i];
2613 text_buffer_changed(es);
2614 EDIT_BuildLineDefs_ML(es, s, e,
2615 abs(es->selection_end - es->selection_start) - strl, hrgn);
2616 strl = 0;
2617 e = s;
2618 SetRectRgn(hrgn, 0, 0, 0, 0);
2619 if (!notify_parent(es, EN_MAXTEXT)) return;
2622 else {
2623 INT fw = es->format_rect.right - es->format_rect.left;
2624 EDIT_InvalidateUniscribeData(es);
2625 EDIT_CalcLineWidth_SL(es);
2626 /* remove chars that don't fit */
2627 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2628 while ((es->text_width > fw) && s + strl >= s) {
2629 lstrcpyW(es->text + s + strl - 1, es->text + s + strl);
2630 strl--;
2631 es->text_length = -1;
2632 EDIT_InvalidateUniscribeData(es);
2633 EDIT_CalcLineWidth_SL(es);
2635 text_buffer_changed(es);
2636 if (!notify_parent(es, EN_MAXTEXT)) return;
2640 if (e != s) {
2641 if (can_undo) {
2642 utl = lstrlenW(es->undo_text);
2643 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2644 /* undo-buffer is extended to the right */
2645 EDIT_MakeUndoFit(es, utl + e - s);
2646 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2647 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2648 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2649 /* undo-buffer is extended to the left */
2650 EDIT_MakeUndoFit(es, utl + e - s);
2651 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2652 p[e - s] = p[0];
2653 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2654 p[i] = buf[i];
2655 es->undo_position = s;
2656 } else {
2657 /* new undo-buffer */
2658 EDIT_MakeUndoFit(es, e - s);
2659 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2660 es->undo_text[e - s] = 0; /* ensure 0 termination */
2661 es->undo_position = s;
2663 /* any deletion makes the old insertion-undo invalid */
2664 es->undo_insert_count = 0;
2665 } else
2666 EDIT_EM_EmptyUndoBuffer(es);
2668 if (strl) {
2669 if (can_undo) {
2670 if ((s == es->undo_position) ||
2671 ((es->undo_insert_count) &&
2672 (s == es->undo_position + es->undo_insert_count)))
2674 * insertion is new and at delete position or
2675 * an extension to either left or right
2677 es->undo_insert_count += strl;
2678 else {
2679 /* new insertion undo */
2680 es->undo_position = s;
2681 es->undo_insert_count = strl;
2682 /* new insertion makes old delete-buffer invalid */
2683 *es->undo_text = '\0';
2685 } else
2686 EDIT_EM_EmptyUndoBuffer(es);
2689 HeapFree(GetProcessHeap(), 0, buf);
2691 s += strl;
2693 /* If text has been deleted and we're right or center aligned then scroll rightward */
2694 if (es->style & (ES_RIGHT | ES_CENTER))
2696 INT delta = strl - abs(es->selection_end - es->selection_start);
2698 if (delta < 0 && es->x_offset)
2700 if (abs(delta) > es->x_offset)
2701 es->x_offset = 0;
2702 else
2703 es->x_offset += delta;
2707 EDIT_EM_SetSel(es, s, s, FALSE);
2708 es->flags |= EF_MODIFIED;
2709 if (send_update) es->flags |= EF_UPDATE;
2710 if (hrgn)
2712 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2713 DeleteObject(hrgn);
2715 else
2716 EDIT_UpdateText(es, NULL, TRUE);
2718 EDIT_EM_ScrollCaret(es);
2720 /* force scroll info update */
2721 EDIT_UpdateScrollInfo(es);
2724 if(send_update || (es->flags & EF_UPDATE))
2726 es->flags &= ~EF_UPDATE;
2727 if (!notify_parent(es, EN_CHANGE)) return;
2729 EDIT_InvalidateUniscribeData(es);
2733 /*********************************************************************
2735 * EM_SETHANDLE
2737 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2740 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2742 if (!(es->style & ES_MULTILINE))
2743 return;
2745 if (!hloc) {
2746 WARN("called with NULL handle\n");
2747 return;
2750 EDIT_UnlockBuffer(es, TRUE);
2752 if(es->is_unicode)
2754 if(es->hloc32A)
2756 LocalFree(es->hloc32A);
2757 es->hloc32A = NULL;
2759 es->hloc32W = hloc;
2761 else
2763 INT countW, countA;
2764 HLOCAL hloc32W_new;
2765 WCHAR *textW;
2766 CHAR *textA;
2768 countA = LocalSize(hloc);
2769 textA = LocalLock(hloc);
2770 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2771 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2773 ERR("Could not allocate new unicode buffer\n");
2774 return;
2776 textW = LocalLock(hloc32W_new);
2777 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2778 LocalUnlock(hloc32W_new);
2779 LocalUnlock(hloc);
2781 if(es->hloc32W)
2782 LocalFree(es->hloc32W);
2784 es->hloc32W = hloc32W_new;
2785 es->hloc32A = hloc;
2788 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2790 /* The text buffer handle belongs to the control */
2791 es->hlocapp = NULL;
2793 EDIT_LockBuffer(es);
2794 text_buffer_changed(es);
2796 es->x_offset = es->y_offset = 0;
2797 es->selection_start = es->selection_end = 0;
2798 EDIT_EM_EmptyUndoBuffer(es);
2799 es->flags &= ~EF_MODIFIED;
2800 es->flags &= ~EF_UPDATE;
2801 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2802 EDIT_UpdateText(es, NULL, TRUE);
2803 EDIT_EM_ScrollCaret(es);
2804 /* force scroll info update */
2805 EDIT_UpdateScrollInfo(es);
2809 /*********************************************************************
2811 * EM_SETLIMITTEXT
2813 * NOTE: this version currently implements WinNT limits
2816 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2818 if (!limit) limit = ~0u;
2819 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2820 es->buffer_limit = limit;
2823 static BOOL is_cjk_charset(HDC dc)
2825 switch (GdiGetCodePage(dc)) {
2826 case 932: case 936: case 949: case 950: case 1361:
2827 return TRUE;
2828 default:
2829 return FALSE;
2833 static BOOL is_cjk_font(HDC dc)
2835 const DWORD FS_DBCS_MASK = FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB;
2836 FONTSIGNATURE fs;
2837 return (GetTextCharsetInfo(dc, &fs, 0) != DEFAULT_CHARSET &&
2838 (fs.fsCsb[0] & FS_DBCS_MASK));
2841 static int get_cjk_fontinfo_margin(int width, int side_bearing)
2843 int margin;
2844 if (side_bearing < 0)
2845 margin = min(-side_bearing, width/2);
2846 else
2847 margin = 0;
2848 return margin;
2851 struct char_width_info {
2852 INT min_lsb, min_rsb, unknown;
2855 /* Undocumented gdi32 export */
2856 extern BOOL WINAPI GetCharWidthInfo(HDC, struct char_width_info *);
2858 /*********************************************************************
2860 * EM_SETMARGINS
2862 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2863 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2864 * margin according to the textmetrics of the current font.
2866 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2867 * equal to or larger than a certain size. The empty client rect is treated as
2868 * 80 pixels width.
2870 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2871 WORD left, WORD right, BOOL repaint)
2873 TEXTMETRICW tm;
2874 INT default_left_margin = 0; /* in pixels */
2875 INT default_right_margin = 0; /* in pixels */
2877 /* Set the default margins depending on the font */
2878 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2879 HDC dc = NtUserGetDC(es->hwndSelf);
2880 HFONT old_font = SelectObject(dc, es->font);
2881 LONG width = GdiGetCharDimensions(dc, &tm, NULL), rc_width;
2882 RECT rc;
2884 /* The default margins are only non zero for TrueType or Vector fonts */
2885 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2886 struct char_width_info width_info;
2888 if ((is_cjk_charset(dc) || is_cjk_font(dc)) &&
2889 GetCharWidthInfo(dc, &width_info))
2891 default_left_margin = get_cjk_fontinfo_margin(width, width_info.min_lsb);
2892 default_right_margin = get_cjk_fontinfo_margin(width, width_info.min_rsb);
2894 else
2896 default_left_margin = width / 2;
2897 default_right_margin = width / 2;
2900 GetClientRect(es->hwndSelf, &rc);
2901 rc_width = !IsRectEmpty(&rc) ? rc.right - rc.left : 80;
2902 if (rc_width < default_left_margin + default_right_margin + width * 2) {
2903 default_left_margin = es->left_margin;
2904 default_right_margin = es->right_margin;
2907 SelectObject(dc, old_font);
2908 NtUserReleaseDC( es->hwndSelf, dc );
2911 if (action & EC_LEFTMARGIN) {
2912 es->format_rect.left -= es->left_margin;
2913 if (left != EC_USEFONTINFO)
2914 es->left_margin = left;
2915 else
2916 es->left_margin = default_left_margin;
2917 es->format_rect.left += es->left_margin;
2920 if (action & EC_RIGHTMARGIN) {
2921 es->format_rect.right += es->right_margin;
2922 if (right != EC_USEFONTINFO)
2923 es->right_margin = right;
2924 else
2925 es->right_margin = default_right_margin;
2926 es->format_rect.right -= es->right_margin;
2929 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2930 EDIT_AdjustFormatRect(es);
2931 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2934 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2938 /*********************************************************************
2940 * EM_SETPASSWORDCHAR
2943 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2945 LONG style;
2947 if (es->style & ES_MULTILINE)
2948 return;
2950 if (es->password_char == c)
2951 return;
2953 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2954 es->password_char = c;
2955 if (c) {
2956 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2957 es->style |= ES_PASSWORD;
2958 } else {
2959 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2960 es->style &= ~ES_PASSWORD;
2962 EDIT_InvalidateUniscribeData(es);
2963 EDIT_UpdateText(es, NULL, TRUE);
2967 /*********************************************************************
2969 * EM_SETTABSTOPS
2972 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2974 if (!(es->style & ES_MULTILINE))
2975 return FALSE;
2976 HeapFree(GetProcessHeap(), 0, es->tabs);
2977 es->tabs_count = count;
2978 if (!count)
2979 es->tabs = NULL;
2980 else {
2981 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2982 memcpy(es->tabs, tabs, count * sizeof(INT));
2984 EDIT_InvalidateUniscribeData(es);
2985 return TRUE;
2989 /*********************************************************************
2991 * EM_SETWORDBREAKPROC
2994 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
2996 if (es->word_break_proc == wbp)
2997 return;
2999 es->word_break_proc = wbp;
3001 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3002 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3003 EDIT_UpdateText(es, NULL, TRUE);
3008 /*********************************************************************
3010 * EM_UNDO / WM_UNDO
3013 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3015 INT ulength;
3016 LPWSTR utext;
3018 /* As per MSDN spec, for a single-line edit control,
3019 the return value is always TRUE */
3020 if( es->style & ES_READONLY )
3021 return !(es->style & ES_MULTILINE);
3023 ulength = lstrlenW(es->undo_text);
3025 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3027 lstrcpyW(utext, es->undo_text);
3029 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3030 es->undo_insert_count, debugstr_w(utext));
3032 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3033 EDIT_EM_EmptyUndoBuffer(es);
3034 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
3035 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3036 /* send the notification after the selection start and end are set */
3037 if (!notify_parent(es, EN_CHANGE)) return TRUE;
3038 EDIT_EM_ScrollCaret(es);
3039 HeapFree(GetProcessHeap(), 0, utext);
3041 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3042 es->undo_insert_count, debugstr_w(es->undo_text));
3043 return TRUE;
3047 /* Helper function for WM_CHAR
3049 * According to an MSDN blog article titled "Just because you're a control
3050 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3051 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3052 * a dialog box or not.
3054 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3056 return (es->flags & EF_DIALOGMODE);
3060 /*********************************************************************
3062 * WM_PASTE
3065 static void EDIT_WM_Paste(EDITSTATE *es)
3067 HGLOBAL hsrc;
3068 LPWSTR src, ptr;
3069 int len;
3071 /* Protect read-only edit control from modification */
3072 if(es->style & ES_READONLY)
3073 return;
3075 OpenClipboard(es->hwndSelf);
3076 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3077 src = GlobalLock(hsrc);
3078 len = lstrlenW(src);
3079 /* Protect single-line edit against pasting new line character */
3080 if (!(es->style & ES_MULTILINE) && ((ptr = wcschr(src, '\n')))) {
3081 len = ptr - src;
3082 if (len && src[len - 1] == '\r')
3083 --len;
3085 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3086 GlobalUnlock(hsrc);
3088 else if (es->style & ES_PASSWORD) {
3089 /* clear selected text in password edit box even with empty clipboard */
3090 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3092 NtUserCloseClipboard();
3096 /*********************************************************************
3098 * WM_COPY
3101 static void EDIT_WM_Copy(EDITSTATE *es)
3103 INT s = min(es->selection_start, es->selection_end);
3104 INT e = max(es->selection_start, es->selection_end);
3105 HGLOBAL hdst;
3106 LPWSTR dst;
3107 DWORD len;
3109 if (e == s) return;
3111 len = e - s;
3112 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3113 dst = GlobalLock(hdst);
3114 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3115 dst[len] = 0; /* ensure 0 termination */
3116 TRACE("%s\n", debugstr_w(dst));
3117 GlobalUnlock(hdst);
3118 OpenClipboard(es->hwndSelf);
3119 NtUserEmptyClipboard();
3120 SetClipboardData(CF_UNICODETEXT, hdst);
3121 NtUserCloseClipboard();
3125 /*********************************************************************
3127 * WM_CLEAR
3130 static inline void EDIT_WM_Clear(EDITSTATE *es)
3132 /* Protect read-only edit control from modification */
3133 if(es->style & ES_READONLY)
3134 return;
3136 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3140 /*********************************************************************
3142 * WM_CUT
3145 static inline void EDIT_WM_Cut(EDITSTATE *es)
3147 EDIT_WM_Copy(es);
3148 EDIT_WM_Clear(es);
3152 /*********************************************************************
3154 * WM_CHAR
3157 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3159 BOOL control;
3161 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3163 switch (c) {
3164 case '\r':
3165 /* If it's not a multiline edit box, it would be ignored below.
3166 * For multiline edit without ES_WANTRETURN, we have to make a
3167 * special case.
3169 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3170 if (EDIT_IsInsideDialog(es))
3171 break;
3172 case '\n':
3173 if (es->style & ES_MULTILINE) {
3174 if (es->style & ES_READONLY) {
3175 EDIT_MoveHome(es, FALSE, FALSE);
3176 EDIT_MoveDown_ML(es, FALSE);
3177 } else {
3178 EDIT_EM_ReplaceSel(es, TRUE, L"\r\n", 2, TRUE, TRUE);
3181 break;
3182 case '\t':
3183 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3185 if (EDIT_IsInsideDialog(es))
3186 break;
3187 EDIT_EM_ReplaceSel(es, TRUE, L"\t", 1, TRUE, TRUE);
3189 break;
3190 case VK_BACK:
3191 if (!(es->style & ES_READONLY) && !control) {
3192 if (es->selection_start != es->selection_end)
3193 EDIT_WM_Clear(es);
3194 else {
3195 /* delete character left of caret */
3196 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3197 EDIT_MoveBackward(es, TRUE);
3198 EDIT_WM_Clear(es);
3201 break;
3202 case 0x03: /* ^C */
3203 if (!(es->style & ES_PASSWORD))
3204 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3205 break;
3206 case 0x16: /* ^V */
3207 if (!(es->style & ES_READONLY))
3208 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3209 break;
3210 case 0x18: /* ^X */
3211 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3212 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3213 break;
3214 case 0x1A: /* ^Z */
3215 if (!(es->style & ES_READONLY))
3216 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3217 break;
3219 default:
3220 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3221 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3222 break;
3224 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3225 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3226 break;
3228 return 1;
3232 /*********************************************************************
3234 * EDIT_ContextMenuCommand
3237 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3239 switch (id) {
3240 case EM_UNDO:
3241 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3242 break;
3243 case WM_CUT:
3244 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3245 break;
3246 case WM_COPY:
3247 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3248 break;
3249 case WM_PASTE:
3250 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3251 break;
3252 case WM_CLEAR:
3253 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3254 break;
3255 case EM_SETSEL:
3256 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3257 break;
3258 default:
3259 ERR("unknown menu item, please report\n");
3260 break;
3265 /*********************************************************************
3267 * WM_CONTEXTMENU
3269 * Note: the resource files resource/sysres_??.rc cannot define a
3270 * single popup menu. Hence we use a (dummy) menubar
3271 * containing the single popup menu as its first item.
3273 * FIXME: the message identifiers have been chosen arbitrarily,
3274 * hence we use MF_BYPOSITION.
3275 * We might as well use the "real" values (anybody knows ?)
3276 * The menu definition is in resources/sysres_??.rc.
3277 * Once these are OK, we better use MF_BYCOMMAND here
3278 * (as we do in EDIT_WM_Command()).
3281 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3283 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3284 HMENU popup = GetSubMenu(menu, 0);
3285 UINT start = es->selection_start;
3286 UINT end = es->selection_end;
3287 BOOL enabled;
3288 UINT cmd;
3290 ORDER_UINT(start, end);
3292 /* undo */
3293 enabled = EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY);
3294 NtUserEnableMenuItem( popup, 0, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3295 /* cut */
3296 enabled = (end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY);
3297 NtUserEnableMenuItem( popup, 2, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3298 /* copy */
3299 enabled = (end - start) && !(es->style & ES_PASSWORD);
3300 NtUserEnableMenuItem( popup, 3, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3301 /* paste */
3302 enabled = NtUserIsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY);
3303 NtUserEnableMenuItem( popup, 4, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3304 /* delete */
3305 enabled = (end - start) && !(es->style & ES_READONLY);
3306 NtUserEnableMenuItem( popup, 5, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3307 /* select all */
3308 enabled = start || end != get_text_length(es);
3309 NtUserEnableMenuItem( popup, 7, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3311 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3313 POINT pt;
3314 RECT rc;
3316 /* Windows places the menu at the edit's center in this case */
3317 GetClientRect( es->hwndSelf, &rc );
3318 pt.x = rc.right / 2;
3319 pt.y = rc.bottom / 2;
3320 ClientToScreen( es->hwndSelf, &pt );
3321 x = pt.x;
3322 y = pt.y;
3325 if (!(es->flags & EF_FOCUSED))
3326 NtUserSetFocus(es->hwndSelf);
3328 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3329 x, y, 0, es->hwndSelf, NULL);
3331 if (cmd)
3332 EDIT_ContextMenuCommand(es, cmd);
3334 NtUserDestroyMenu(menu);
3338 /*********************************************************************
3340 * WM_GETTEXT
3343 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3345 if(!count) return 0;
3347 if(unicode)
3349 lstrcpynW(dst, es->text, count);
3350 return lstrlenW(dst);
3352 else
3354 LPSTR textA = (LPSTR)dst;
3355 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3356 textA[count - 1] = 0; /* ensure 0 termination */
3357 return strlen(textA);
3361 /*********************************************************************
3363 * EDIT_CheckCombo
3366 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3368 HWND hLBox = es->hwndListBox;
3369 HWND hCombo;
3370 BOOL bDropped;
3371 int nEUI;
3373 if (!hLBox)
3374 return FALSE;
3376 hCombo = GetParent(es->hwndSelf);
3377 bDropped = TRUE;
3378 nEUI = 0;
3380 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3382 if (key == VK_UP || key == VK_DOWN)
3384 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3385 nEUI = 1;
3387 if (msg == WM_KEYDOWN || nEUI)
3388 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3391 switch (msg)
3393 case WM_KEYDOWN:
3394 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3396 /* make sure ComboLBox pops up */
3397 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3398 key = VK_F4;
3399 nEUI = 2;
3402 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3403 break;
3405 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3406 if (nEUI)
3407 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3408 else
3409 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3410 break;
3413 if(nEUI == 2)
3414 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3416 return TRUE;
3420 /*********************************************************************
3422 * WM_KEYDOWN
3424 * Handling of special keys that don't produce a WM_CHAR
3425 * (i.e. non-printable keys) & Backspace & Delete
3428 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3430 BOOL shift;
3431 BOOL control;
3433 if (NtUserGetKeyState(VK_MENU) & 0x8000)
3434 return 0;
3436 shift = NtUserGetKeyState(VK_SHIFT) & 0x8000;
3437 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3439 switch (key) {
3440 case VK_F4:
3441 case VK_UP:
3442 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3443 break;
3445 /* fall through */
3446 case VK_LEFT:
3447 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3448 EDIT_MoveUp_ML(es, shift);
3449 else
3450 if (control)
3451 EDIT_MoveWordBackward(es, shift);
3452 else
3453 EDIT_MoveBackward(es, shift);
3454 break;
3455 case VK_DOWN:
3456 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3457 break;
3458 /* fall through */
3459 case VK_RIGHT:
3460 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3461 EDIT_MoveDown_ML(es, shift);
3462 else if (control)
3463 EDIT_MoveWordForward(es, shift);
3464 else
3465 EDIT_MoveForward(es, shift);
3466 break;
3467 case VK_HOME:
3468 EDIT_MoveHome(es, shift, control);
3469 break;
3470 case VK_END:
3471 EDIT_MoveEnd(es, shift, control);
3472 break;
3473 case VK_PRIOR:
3474 if (es->style & ES_MULTILINE)
3475 EDIT_MovePageUp_ML(es, shift);
3476 else
3477 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3478 break;
3479 case VK_NEXT:
3480 if (es->style & ES_MULTILINE)
3481 EDIT_MovePageDown_ML(es, shift);
3482 else
3483 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3484 break;
3485 case VK_DELETE:
3486 if (!(es->style & ES_READONLY) && !(shift && control)) {
3487 if (es->selection_start != es->selection_end) {
3488 if (shift)
3489 EDIT_WM_Cut(es);
3490 else
3491 EDIT_WM_Clear(es);
3492 } else {
3493 EDIT_EM_SetSel(es, ~0u, 0, FALSE);
3494 if (shift)
3495 /* delete character left of caret */
3496 EDIT_MoveBackward(es, TRUE);
3497 else if (control)
3498 /* delete to end of line */
3499 EDIT_MoveEnd(es, TRUE, FALSE);
3500 else
3501 /* delete character right of caret */
3502 EDIT_MoveForward(es, TRUE);
3503 EDIT_WM_Clear(es);
3506 break;
3507 case VK_INSERT:
3508 if (shift) {
3509 if (!(es->style & ES_READONLY))
3510 EDIT_WM_Paste(es);
3511 } else if (control)
3512 EDIT_WM_Copy(es);
3513 break;
3514 case VK_RETURN:
3515 /* If the edit doesn't want the return send a message to the default object */
3516 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3518 DWORD dw;
3520 if (!EDIT_IsInsideDialog(es)) break;
3521 if (control) break;
3522 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3523 if (HIWORD(dw) == DC_HASDEFID)
3525 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3526 if (hwDefCtrl)
3528 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3529 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3533 break;
3534 case VK_ESCAPE:
3535 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3536 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3537 break;
3538 case VK_TAB:
3539 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3540 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3541 break;
3543 return TRUE;
3547 /*********************************************************************
3549 * WM_KILLFOCUS
3552 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3554 es->flags &= ~EF_FOCUSED;
3555 DestroyCaret();
3556 if(!(es->style & ES_NOHIDESEL))
3557 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3558 if (!notify_parent(es, EN_KILLFOCUS)) return 0;
3559 /* throw away left over scroll when we lose focus */
3560 es->wheelDeltaRemainder = 0;
3561 return 0;
3565 /*********************************************************************
3567 * WM_LBUTTONDBLCLK
3569 * The caret position has been set on the WM_LBUTTONDOWN message
3572 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3574 INT s;
3575 INT e = es->selection_end;
3576 INT l;
3577 INT li;
3578 INT ll;
3580 es->bCaptureState = TRUE;
3581 NtUserSetCapture(es->hwndSelf);
3583 l = EDIT_EM_LineFromChar(es, e);
3584 li = EDIT_EM_LineIndex(es, l);
3585 ll = EDIT_EM_LineLength(es, e);
3586 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3587 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3588 EDIT_EM_SetSel(es, s, e, FALSE);
3589 EDIT_EM_ScrollCaret(es);
3590 es->region_posx = es->region_posy = 0;
3591 SetTimer(es->hwndSelf, 0, 100, NULL);
3592 return 0;
3596 /*********************************************************************
3598 * WM_LBUTTONDOWN
3601 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3603 INT e;
3604 BOOL after_wrap;
3606 es->bCaptureState = TRUE;
3607 NtUserSetCapture(es->hwndSelf);
3608 EDIT_ConfinePoint(es, &x, &y);
3609 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3610 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3611 EDIT_EM_ScrollCaret(es);
3612 es->region_posx = es->region_posy = 0;
3613 SetTimer(es->hwndSelf, 0, 100, NULL);
3615 if (!(es->flags & EF_FOCUSED))
3616 NtUserSetFocus(es->hwndSelf);
3618 return 0;
3622 /*********************************************************************
3624 * WM_LBUTTONUP
3627 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3629 if (es->bCaptureState) {
3630 NtUserKillTimer(es->hwndSelf, 0);
3631 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3633 es->bCaptureState = FALSE;
3634 return 0;
3638 /*********************************************************************
3640 * WM_MBUTTONDOWN
3643 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3645 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3646 return 0;
3650 /*********************************************************************
3652 * WM_MOUSEMOVE
3655 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3657 INT e;
3658 BOOL after_wrap;
3659 INT prex, prey;
3661 /* If the mouse has been captured by process other than the edit control itself,
3662 * the windows edit controls will not select the strings with mouse move.
3664 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3665 return 0;
3668 * FIXME: gotta do some scrolling if outside client
3669 * area. Maybe reset the timer ?
3671 prex = x; prey = y;
3672 EDIT_ConfinePoint(es, &x, &y);
3673 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3674 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3675 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3676 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3677 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3678 return 0;
3682 /*********************************************************************
3684 * WM_PAINT
3687 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3689 PAINTSTRUCT ps;
3690 INT i;
3691 HDC dc;
3692 HFONT old_font = 0;
3693 RECT rc;
3694 RECT rcClient;
3695 RECT rcLine;
3696 RECT rcRgn;
3697 HBRUSH brush;
3698 HBRUSH old_brush;
3699 INT bw, bh;
3700 BOOL rev = es->bEnableState &&
3701 ((es->flags & EF_FOCUSED) ||
3702 (es->style & ES_NOHIDESEL));
3703 dc = hdc ? hdc : NtUserBeginPaint( es->hwndSelf, &ps );
3705 /* The dc we use for calculating may not be the one we paint into.
3706 This is the safest action. */
3707 EDIT_InvalidateUniscribeData(es);
3708 GetClientRect(es->hwndSelf, &rcClient);
3710 /* get the background brush */
3711 brush = EDIT_NotifyCtlColor(es, dc);
3713 /* paint the border and the background */
3714 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3716 if(es->style & WS_BORDER) {
3717 bw = GetSystemMetrics(SM_CXBORDER);
3718 bh = GetSystemMetrics(SM_CYBORDER);
3719 rc = rcClient;
3720 if(es->style & ES_MULTILINE) {
3721 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3722 if(es->style & WS_VSCROLL) rc.right+=bw;
3725 /* Draw the frame. Same code as in nonclient.c */
3726 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3727 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3728 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3729 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3730 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3731 SelectObject(dc, old_brush);
3733 /* Keep the border clean */
3734 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3735 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3738 GetClipBox(dc, &rc);
3739 FillRect(dc, &rc, brush);
3741 IntersectClipRect(dc, es->format_rect.left,
3742 es->format_rect.top,
3743 es->format_rect.right,
3744 es->format_rect.bottom);
3745 if (es->style & ES_MULTILINE) {
3746 rc = rcClient;
3747 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3749 if (es->font)
3750 old_font = SelectObject(dc, es->font);
3752 if (!es->bEnableState)
3753 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3754 GetClipBox(dc, &rcRgn);
3755 if (es->style & ES_MULTILINE) {
3756 INT vlc = get_vertical_line_count(es);
3757 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3758 EDIT_UpdateUniscribeData(es, dc, i);
3759 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3760 if (IntersectRect(&rc, &rcRgn, &rcLine))
3761 EDIT_PaintLine(es, dc, i, rev);
3763 } else {
3764 EDIT_UpdateUniscribeData(es, dc, 0);
3765 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3766 if (IntersectRect(&rc, &rcRgn, &rcLine))
3767 EDIT_PaintLine(es, dc, 0, rev);
3769 if (es->font)
3770 SelectObject(dc, old_font);
3772 if (!hdc)
3773 NtUserEndPaint( es->hwndSelf, &ps );
3777 /*********************************************************************
3779 * WM_SETFOCUS
3782 static void EDIT_WM_SetFocus(EDITSTATE *es)
3784 es->flags |= EF_FOCUSED;
3786 if (!(es->style & ES_NOHIDESEL))
3787 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3789 /* single line edit updates itself */
3790 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3792 HDC hdc = NtUserGetDC(es->hwndSelf);
3793 EDIT_WM_Paint(es, hdc);
3794 NtUserReleaseDC( es->hwndSelf, hdc );
3797 NtUserCreateCaret( es->hwndSelf, 0, 1, es->line_height );
3798 EDIT_SetCaretPos(es, es->selection_end,
3799 es->flags & EF_AFTER_WRAP);
3800 NtUserShowCaret( es->hwndSelf );
3801 notify_parent(es, EN_SETFOCUS);
3804 static DWORD get_font_margins(HDC hdc, const TEXTMETRICW *tm, BOOL unicode)
3806 ABC abc[256];
3807 SHORT left, right;
3808 UINT i;
3810 if (!(tm->tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE)))
3811 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3813 if (unicode) {
3814 if (!is_cjk_charset(hdc) && !is_cjk_font(hdc))
3815 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3816 if (!GetCharABCWidthsW(hdc, 0, 255, abc))
3817 return 0;
3819 else if (is_cjk_charset(hdc)) {
3820 if (!GetCharABCWidthsA(hdc, 0, 255, abc))
3821 return 0;
3823 else
3824 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3826 left = right = 0;
3827 for (i = 0; i < ARRAY_SIZE(abc); i++) {
3828 if (-abc[i].abcA > right) right = -abc[i].abcA;
3829 if (-abc[i].abcC > left ) left = -abc[i].abcC;
3831 return MAKELONG(left, right);
3834 static void EDIT_UpdateImmCompositionFont(EDITSTATE *es)
3836 LOGFONTW composition_font;
3837 HIMC himc = ImmGetContext(es->hwndSelf);
3838 GetObjectW(es->font, sizeof(LOGFONTW), &composition_font);
3839 ImmSetCompositionFontW(himc, &composition_font);
3840 ImmReleaseContext(es->hwndSelf, himc);
3843 /*********************************************************************
3845 * WM_SETFONT
3847 * With Win95 look the margins are set to default font value unless
3848 * the system font (font == 0) is being set, in which case they are left
3849 * unchanged.
3852 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3854 TEXTMETRICW tm;
3855 HDC dc;
3856 HFONT old_font = 0;
3857 RECT clientRect;
3858 DWORD margins;
3860 es->font = font;
3861 EDIT_InvalidateUniscribeData(es);
3862 dc = NtUserGetDC(es->hwndSelf);
3863 if (font)
3864 old_font = SelectObject(dc, font);
3865 GetTextMetricsW(dc, &tm);
3866 es->line_height = tm.tmHeight;
3867 es->char_width = tm.tmAveCharWidth;
3868 margins = get_font_margins(dc, &tm, es->is_unicode);
3869 if (font)
3870 SelectObject(dc, old_font);
3871 NtUserReleaseDC( es->hwndSelf, dc );
3873 /* Reset the format rect and the margins */
3874 GetClientRect(es->hwndSelf, &clientRect);
3875 EDIT_SetRectNP(es, &clientRect);
3876 if (margins)
3877 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3878 LOWORD(margins), HIWORD(margins), FALSE);
3880 if (es->style & ES_MULTILINE)
3881 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3882 else
3883 EDIT_CalcLineWidth_SL(es);
3885 if (redraw)
3886 EDIT_UpdateText(es, NULL, TRUE);
3887 if (es->flags & EF_FOCUSED) {
3888 DestroyCaret();
3889 NtUserCreateCaret( es->hwndSelf, 0, 1, es->line_height );
3890 EDIT_SetCaretPos(es, es->selection_end,
3891 es->flags & EF_AFTER_WRAP);
3892 NtUserShowCaret( es->hwndSelf );
3895 EDIT_UpdateImmCompositionFont(es);
3899 /*********************************************************************
3901 * WM_SETTEXT
3903 * NOTES
3904 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3905 * The modified flag is reset. No notifications are sent.
3907 * For single-line controls, reception of WM_SETTEXT triggers:
3908 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3911 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3913 LPWSTR textW = NULL;
3914 if (!unicode && text)
3916 LPCSTR textA = (LPCSTR)text;
3917 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3918 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3919 if (textW)
3920 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3921 text = textW;
3924 if (es->flags & EF_UPDATE)
3925 /* fixed this bug once; complain if we see it about to happen again. */
3926 ERR("SetSel may generate UPDATE message whose handler may reset "
3927 "selection.\n");
3929 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3930 if (text)
3932 TRACE("%s\n", debugstr_w(text));
3933 EDIT_EM_ReplaceSel(es, FALSE, text, lstrlenW(text), FALSE, FALSE);
3934 if(!unicode)
3935 HeapFree(GetProcessHeap(), 0, textW);
3937 else
3939 TRACE("<NULL>\n");
3940 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3942 es->x_offset = 0;
3943 es->flags &= ~EF_MODIFIED;
3944 EDIT_EM_SetSel(es, 0, 0, FALSE);
3946 /* Send the notification after the selection start and end have been set
3947 * edit control doesn't send notification on WM_SETTEXT
3948 * if it is multiline, or it is part of combobox
3950 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3952 if (!notify_parent(es, EN_UPDATE)) return;
3953 if (!notify_parent(es, EN_CHANGE)) return;
3955 EDIT_EM_ScrollCaret(es);
3956 EDIT_UpdateScrollInfo(es);
3957 EDIT_InvalidateUniscribeData(es);
3961 /*********************************************************************
3963 * WM_SIZE
3966 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3968 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3969 RECT rc;
3970 GetClientRect(es->hwndSelf, &rc);
3971 EDIT_SetRectNP(es, &rc);
3972 EDIT_UpdateText(es, NULL, TRUE);
3977 /*********************************************************************
3979 * WM_STYLECHANGED
3981 * This message is sent by SetWindowLong on having changed either the Style
3982 * or the extended style.
3984 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3986 * See also EDIT_WM_NCCreate
3988 * It appears that the Windows version of the edit control allows the style
3989 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3990 * style variable which will generally be different. In this function we
3991 * update the internal style based on what changed in the externally visible
3992 * style.
3994 * Much of this content as based upon the MSDN, especially:
3995 * Platform SDK Documentation -> User Interface Services ->
3996 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3997 * Edit Control Styles
3999 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4001 if (GWL_STYLE == which) {
4002 DWORD style_change_mask;
4003 DWORD new_style;
4004 /* Only a subset of changes can be applied after the control
4005 * has been created.
4007 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4008 ES_NUMBER;
4009 if (es->style & ES_MULTILINE)
4010 style_change_mask |= ES_WANTRETURN;
4012 new_style = style->styleNew & style_change_mask;
4014 /* Number overrides lowercase overrides uppercase (at least it
4015 * does in Win95). However I'll bet that ES_NUMBER would be
4016 * invalid under Win 3.1.
4018 if (new_style & ES_NUMBER) {
4019 ; /* do not override the ES_NUMBER */
4020 } else if (new_style & ES_LOWERCASE) {
4021 new_style &= ~ES_UPPERCASE;
4024 es->style = (es->style & ~style_change_mask) | new_style;
4025 } else if (GWL_EXSTYLE == which) {
4026 ; /* FIXME - what is needed here */
4027 } else {
4028 WARN ("Invalid style change %Id\n",which);
4031 return 0;
4034 /*********************************************************************
4036 * WM_SYSKEYDOWN
4039 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4041 if ((key == VK_BACK) && (key_data & 0x2000)) {
4042 if (EDIT_EM_CanUndo(es))
4043 EDIT_EM_Undo(es);
4044 return 0;
4045 } else if (key == VK_UP || key == VK_DOWN) {
4046 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4047 return 0;
4049 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4053 /*********************************************************************
4055 * WM_TIMER
4058 static void EDIT_WM_Timer(EDITSTATE *es)
4060 if (es->region_posx < 0) {
4061 EDIT_MoveBackward(es, TRUE);
4062 } else if (es->region_posx > 0) {
4063 EDIT_MoveForward(es, TRUE);
4066 * FIXME: gotta do some vertical scrolling here, like
4067 * EDIT_EM_LineScroll(hwnd, 0, 1);
4071 /*********************************************************************
4073 * WM_HSCROLL
4076 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4078 INT dx;
4079 INT fw;
4081 if (!(es->style & ES_MULTILINE))
4082 return 0;
4084 if (!(es->style & ES_AUTOHSCROLL))
4085 return 0;
4087 dx = 0;
4088 fw = es->format_rect.right - es->format_rect.left;
4089 switch (action) {
4090 case SB_LINELEFT:
4091 TRACE("SB_LINELEFT\n");
4092 if (es->x_offset)
4093 dx = -es->char_width;
4094 break;
4095 case SB_LINERIGHT:
4096 TRACE("SB_LINERIGHT\n");
4097 if (es->x_offset < es->text_width)
4098 dx = es->char_width;
4099 break;
4100 case SB_PAGELEFT:
4101 TRACE("SB_PAGELEFT\n");
4102 if (es->x_offset)
4103 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4104 break;
4105 case SB_PAGERIGHT:
4106 TRACE("SB_PAGERIGHT\n");
4107 if (es->x_offset < es->text_width)
4108 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4109 break;
4110 case SB_LEFT:
4111 TRACE("SB_LEFT\n");
4112 if (es->x_offset)
4113 dx = -es->x_offset;
4114 break;
4115 case SB_RIGHT:
4116 TRACE("SB_RIGHT\n");
4117 if (es->x_offset < es->text_width)
4118 dx = es->text_width - es->x_offset;
4119 break;
4120 case SB_THUMBTRACK:
4121 TRACE("SB_THUMBTRACK %d\n", pos);
4122 es->flags |= EF_HSCROLL_TRACK;
4123 if(es->style & WS_HSCROLL)
4124 dx = pos - es->x_offset;
4125 else
4127 INT fw, new_x;
4128 /* Sanity check */
4129 if(pos < 0 || pos > 100) return 0;
4130 /* Assume default scroll range 0-100 */
4131 fw = es->format_rect.right - es->format_rect.left;
4132 new_x = pos * (es->text_width - fw) / 100;
4133 dx = es->text_width ? (new_x - es->x_offset) : 0;
4135 break;
4136 case SB_THUMBPOSITION:
4137 TRACE("SB_THUMBPOSITION %d\n", pos);
4138 es->flags &= ~EF_HSCROLL_TRACK;
4139 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4140 dx = pos - es->x_offset;
4141 else
4143 INT fw, new_x;
4144 /* Sanity check */
4145 if(pos < 0 || pos > 100) return 0;
4146 /* Assume default scroll range 0-100 */
4147 fw = es->format_rect.right - es->format_rect.left;
4148 new_x = pos * (es->text_width - fw) / 100;
4149 dx = es->text_width ? (new_x - es->x_offset) : 0;
4151 if (!dx) {
4152 /* force scroll info update */
4153 EDIT_UpdateScrollInfo(es);
4154 notify_parent(es, EN_HSCROLL);
4156 break;
4157 case SB_ENDSCROLL:
4158 TRACE("SB_ENDSCROLL\n");
4159 break;
4161 * FIXME : the next two are undocumented !
4162 * Are we doing the right thing ?
4163 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4164 * although it's also a regular control message.
4166 case EM_GETTHUMB: /* this one is used by NT notepad */
4168 LRESULT ret;
4169 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4170 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4171 else
4173 /* Assume default scroll range 0-100 */
4174 INT fw = es->format_rect.right - es->format_rect.left;
4175 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4177 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4178 return ret;
4180 case EM_LINESCROLL:
4181 TRACE("EM_LINESCROLL16\n");
4182 dx = pos;
4183 break;
4185 default:
4186 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4187 action, action);
4188 return 0;
4190 if (dx)
4192 INT fw = es->format_rect.right - es->format_rect.left;
4193 /* check if we are going to move too far */
4194 if(es->x_offset + dx + fw > es->text_width)
4195 dx = es->text_width - fw - es->x_offset;
4196 if(dx)
4197 EDIT_EM_LineScroll_internal(es, dx, 0);
4199 return 0;
4203 /*********************************************************************
4205 * WM_VSCROLL
4208 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4210 INT dy;
4212 if (!(es->style & ES_MULTILINE))
4213 return 0;
4215 if (!(es->style & ES_AUTOVSCROLL))
4216 return 0;
4218 dy = 0;
4219 switch (action) {
4220 case SB_LINEUP:
4221 case SB_LINEDOWN:
4222 case SB_PAGEUP:
4223 case SB_PAGEDOWN:
4224 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4225 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4226 (action == SB_PAGEUP ? "SB_PAGEUP" :
4227 "SB_PAGEDOWN"))));
4228 EDIT_EM_Scroll(es, action);
4229 return 0;
4230 case SB_TOP:
4231 TRACE("SB_TOP\n");
4232 dy = -es->y_offset;
4233 break;
4234 case SB_BOTTOM:
4235 TRACE("SB_BOTTOM\n");
4236 dy = es->line_count - 1 - es->y_offset;
4237 break;
4238 case SB_THUMBTRACK:
4239 TRACE("SB_THUMBTRACK %d\n", pos);
4240 es->flags |= EF_VSCROLL_TRACK;
4241 if(es->style & WS_VSCROLL)
4242 dy = pos - es->y_offset;
4243 else
4245 /* Assume default scroll range 0-100 */
4246 INT vlc, new_y;
4247 /* Sanity check */
4248 if(pos < 0 || pos > 100) return 0;
4249 vlc = get_vertical_line_count(es);
4250 new_y = pos * (es->line_count - vlc) / 100;
4251 dy = es->line_count ? (new_y - es->y_offset) : 0;
4252 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4253 es->line_count, es->y_offset, pos, dy);
4255 break;
4256 case SB_THUMBPOSITION:
4257 TRACE("SB_THUMBPOSITION %d\n", pos);
4258 es->flags &= ~EF_VSCROLL_TRACK;
4259 if(es->style & WS_VSCROLL)
4260 dy = pos - es->y_offset;
4261 else
4263 /* Assume default scroll range 0-100 */
4264 INT vlc, new_y;
4265 /* Sanity check */
4266 if(pos < 0 || pos > 100) return 0;
4267 vlc = get_vertical_line_count(es);
4268 new_y = pos * (es->line_count - vlc) / 100;
4269 dy = es->line_count ? (new_y - es->y_offset) : 0;
4270 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4271 es->line_count, es->y_offset, pos, dy);
4273 if (!dy)
4275 /* force scroll info update */
4276 EDIT_UpdateScrollInfo(es);
4277 notify_parent(es, EN_VSCROLL);
4279 break;
4280 case SB_ENDSCROLL:
4281 TRACE("SB_ENDSCROLL\n");
4282 break;
4284 * FIXME : the next two are undocumented !
4285 * Are we doing the right thing ?
4286 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4287 * although it's also a regular control message.
4289 case EM_GETTHUMB: /* this one is used by NT notepad */
4291 LRESULT ret;
4292 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4293 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4294 else
4296 /* Assume default scroll range 0-100 */
4297 INT vlc = get_vertical_line_count(es);
4298 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4300 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4301 return ret;
4303 case EM_LINESCROLL:
4304 TRACE("EM_LINESCROLL %d\n", pos);
4305 dy = pos;
4306 break;
4308 default:
4309 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4310 action, action);
4311 return 0;
4313 if (dy)
4314 EDIT_EM_LineScroll(es, 0, dy);
4315 return 0;
4318 /*********************************************************************
4320 * EM_GETTHUMB
4322 * FIXME: is this right ? (or should it be only VSCROLL)
4323 * (and maybe only for edit controls that really have their
4324 * own scrollbars) (and maybe only for multiline controls ?)
4325 * All in all: very poorly documented
4328 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4330 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4331 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4335 /********************************************************************
4337 * The Following code is to handle inline editing from IMEs
4340 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4342 LONG buflen;
4343 LPWSTR lpResultStr;
4345 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4346 if (buflen <= 0)
4348 return;
4351 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4352 if (!lpResultStr)
4354 ERR("Unable to alloc buffer for IME string\n");
4355 return;
4358 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4359 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4360 HeapFree(GetProcessHeap(),0,lpResultStr);
4363 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4365 HIMC hIMC;
4367 hIMC = ImmGetContext(hwnd);
4368 if (!hIMC)
4369 return;
4371 if (CompFlag & GCS_RESULTSTR)
4372 EDIT_GetResultStr(hIMC, es);
4374 ImmReleaseContext(hwnd, hIMC);
4378 /*********************************************************************
4380 * WM_NCCREATE
4382 * See also EDIT_WM_StyleChanged
4384 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4386 EDITSTATE *es;
4387 UINT alloc_size;
4389 TRACE("Creating %s edit control, style = %08lx\n",
4390 unicode ? "Unicode" : "ANSI", lpcs->style);
4392 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4393 return FALSE;
4394 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4397 * Note: since the EDITSTATE has not been fully initialized yet,
4398 * we can't use any API calls that may send
4399 * WM_XXX messages before WM_NCCREATE is completed.
4402 es->is_unicode = unicode;
4403 es->style = lpcs->style;
4405 es->bEnableState = !(es->style & WS_DISABLED);
4407 es->hwndSelf = hwnd;
4408 /* Save parent, which will be notified by EN_* messages */
4409 es->hwndParent = lpcs->hwndParent;
4411 if (es->style & ES_COMBO)
4412 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4414 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4415 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4417 /* Number overrides lowercase overrides uppercase (at least it
4418 * does in Win95). However I'll bet that ES_NUMBER would be
4419 * invalid under Win 3.1.
4421 if (es->style & ES_NUMBER) {
4422 ; /* do not override the ES_NUMBER */
4423 } else if (es->style & ES_LOWERCASE) {
4424 es->style &= ~ES_UPPERCASE;
4426 if (es->style & ES_MULTILINE) {
4427 es->buffer_limit = BUFLIMIT_INITIAL;
4428 if (es->style & WS_VSCROLL)
4429 es->style |= ES_AUTOVSCROLL;
4430 if (es->style & WS_HSCROLL)
4431 es->style |= ES_AUTOHSCROLL;
4432 es->style &= ~ES_PASSWORD;
4433 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4434 /* Confirmed - RIGHT overrides CENTER */
4435 if (es->style & ES_RIGHT)
4436 es->style &= ~ES_CENTER;
4437 es->style &= ~WS_HSCROLL;
4438 es->style &= ~ES_AUTOHSCROLL;
4440 } else {
4441 es->buffer_limit = BUFLIMIT_INITIAL;
4442 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4443 es->style &= ~ES_CENTER;
4444 es->style &= ~WS_HSCROLL;
4445 es->style &= ~WS_VSCROLL;
4446 if (es->style & ES_PASSWORD)
4447 es->password_char = '*';
4450 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4451 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4452 goto cleanup;
4453 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4455 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4456 goto cleanup;
4457 es->undo_buffer_size = es->buffer_size;
4459 if (es->style & ES_MULTILINE)
4460 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4461 goto cleanup;
4462 es->line_count = 1;
4465 * In Win95 look and feel, the WS_BORDER style is replaced by the
4466 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4467 * control a nonclient area so we don't need to draw the border.
4468 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4469 * a nonclient area and we should handle painting the border ourselves.
4471 * When making modifications please ensure that the code still works
4472 * for edit controls created directly with style 0x50800000, exStyle 0
4473 * (which should have a single pixel border)
4475 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4476 es->style &= ~WS_BORDER;
4477 else if (es->style & WS_BORDER)
4478 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4480 return TRUE;
4482 cleanup:
4483 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4484 EDIT_InvalidateUniscribeData(es);
4485 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4486 HeapFree(GetProcessHeap(), 0, es->undo_text);
4487 if (es->hloc32W) LocalFree(es->hloc32W);
4488 HeapFree(GetProcessHeap(), 0, es->logAttr);
4489 HeapFree(GetProcessHeap(), 0, es);
4490 return FALSE;
4494 /*********************************************************************
4496 * WM_CREATE
4499 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4501 RECT clientRect;
4503 TRACE("%s\n", debugstr_w(name));
4505 * To initialize some final structure members, we call some helper
4506 * functions. However, since the EDITSTATE is not consistent (i.e.
4507 * not fully initialized), we should be very careful which
4508 * functions can be called, and in what order.
4510 EDIT_WM_SetFont(es, 0, FALSE);
4511 EDIT_EM_EmptyUndoBuffer(es);
4513 /* We need to calculate the format rect
4514 (applications may send EM_SETMARGINS before the control gets visible) */
4515 GetClientRect(es->hwndSelf, &clientRect);
4516 EDIT_SetRectNP(es, &clientRect);
4518 if (name && *name) {
4519 EDIT_EM_ReplaceSel(es, FALSE, name, lstrlenW(name), FALSE, FALSE);
4520 /* if we insert text to the editline, the text scrolls out
4521 * of the window, as the caret is placed after the insert
4522 * pos normally; thus we reset es->selection... to 0 and
4523 * update caret
4525 es->selection_start = es->selection_end = 0;
4526 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4527 * Messages are only to be sent when the USER does something to
4528 * change the contents. So I am removing this EN_CHANGE
4530 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4532 EDIT_EM_ScrollCaret(es);
4534 /* force scroll info update */
4535 EDIT_UpdateScrollInfo(es);
4536 /* The rule seems to return 1 here for success */
4537 /* Power Builder masked edit controls will crash */
4538 /* if not. */
4539 /* FIXME: is that in all cases so ? */
4540 return 1;
4544 /*********************************************************************
4546 * WM_NCDESTROY
4549 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4551 LINEDEF *pc, *pp;
4553 /* The app can own the text buffer handle */
4554 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4555 LocalFree(es->hloc32W);
4557 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4558 LocalFree(es->hloc32A);
4560 EDIT_InvalidateUniscribeData(es);
4561 pc = es->first_line_def;
4562 while (pc)
4564 pp = pc->next;
4565 HeapFree(GetProcessHeap(), 0, pc);
4566 pc = pp;
4569 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4570 HeapFree(GetProcessHeap(), 0, es->undo_text);
4571 HeapFree(GetProcessHeap(), 0, es);
4573 return 0;
4577 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4579 if(unicode)
4580 return DefWindowProcW(hwnd, msg, wParam, lParam);
4581 else
4582 return DefWindowProcA(hwnd, msg, wParam, lParam);
4585 /*********************************************************************
4587 * EditWndProc_common
4589 * The messages are in the order of the actual integer values
4590 * (which can be found in include/windows.h)
4592 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4594 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4595 LRESULT result = 0;
4596 POINT pt;
4598 TRACE("hwnd=%p msg=%x (%s) wparam=%Ix lparam=%Ix\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4600 if (!es && msg != WM_NCCREATE)
4601 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4603 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4605 switch (msg) {
4606 case EM_GETSEL:
4607 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4608 break;
4610 case EM_SETSEL:
4611 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4612 EDIT_EM_ScrollCaret(es);
4613 result = 1;
4614 break;
4616 case EM_GETRECT:
4617 if (lParam)
4618 *((LPRECT)lParam) = es->format_rect;
4619 break;
4621 case EM_SETRECT:
4622 if ((es->style & ES_MULTILINE) && lParam) {
4623 EDIT_SetRectNP(es, (LPRECT)lParam);
4624 EDIT_UpdateText(es, NULL, TRUE);
4626 break;
4628 case EM_SETRECTNP:
4629 if ((es->style & ES_MULTILINE) && lParam)
4630 EDIT_SetRectNP(es, (LPRECT)lParam);
4631 break;
4633 case EM_SCROLL:
4634 result = EDIT_EM_Scroll(es, (INT)wParam);
4635 break;
4637 case EM_LINESCROLL:
4638 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4639 break;
4641 case EM_SCROLLCARET:
4642 EDIT_EM_ScrollCaret(es);
4643 result = 1;
4644 break;
4646 case EM_GETMODIFY:
4647 result = ((es->flags & EF_MODIFIED) != 0);
4648 break;
4650 case EM_SETMODIFY:
4651 if (wParam)
4652 es->flags |= EF_MODIFIED;
4653 else
4654 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4655 break;
4657 case EM_GETLINECOUNT:
4658 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4659 break;
4661 case EM_LINEINDEX:
4662 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4663 break;
4665 case EM_SETHANDLE:
4666 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4667 break;
4669 case EM_GETHANDLE:
4670 result = (LRESULT)EDIT_EM_GetHandle(es);
4671 break;
4673 case EM_GETTHUMB:
4674 result = EDIT_EM_GetThumb(es);
4675 break;
4677 /* these messages missing from specs */
4678 case 0x00bf:
4679 case 0x00c0:
4680 case 0x00c3:
4681 case 0x00ca:
4682 FIXME("undocumented message 0x%x, please report\n", msg);
4683 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4684 break;
4686 case EM_LINELENGTH:
4687 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4688 break;
4690 case EM_REPLACESEL:
4692 LPWSTR textW;
4694 if(unicode)
4695 textW = (LPWSTR)lParam;
4696 else
4698 LPSTR textA = (LPSTR)lParam;
4699 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4700 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4701 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4704 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, lstrlenW(textW), TRUE, TRUE);
4705 result = 1;
4707 if(!unicode)
4708 HeapFree(GetProcessHeap(), 0, textW);
4709 break;
4712 case EM_GETLINE:
4713 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4714 break;
4716 case EM_SETLIMITTEXT:
4717 EDIT_EM_SetLimitText(es, wParam);
4718 break;
4720 case EM_CANUNDO:
4721 result = (LRESULT)EDIT_EM_CanUndo(es);
4722 break;
4724 case EM_UNDO:
4725 case WM_UNDO:
4726 result = (LRESULT)EDIT_EM_Undo(es);
4727 break;
4729 case EM_FMTLINES:
4730 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4731 break;
4733 case EM_LINEFROMCHAR:
4734 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4735 break;
4737 case EM_SETTABSTOPS:
4738 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4739 break;
4741 case EM_SETPASSWORDCHAR:
4743 WCHAR charW = 0;
4745 if(unicode)
4746 charW = (WCHAR)wParam;
4747 else
4749 CHAR charA = wParam;
4750 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4753 EDIT_EM_SetPasswordChar(es, charW);
4754 break;
4757 case EM_EMPTYUNDOBUFFER:
4758 EDIT_EM_EmptyUndoBuffer(es);
4759 break;
4761 case EM_GETFIRSTVISIBLELINE:
4762 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4763 break;
4765 case EM_SETREADONLY:
4767 DWORD old_style = es->style;
4769 if (wParam) {
4770 SetWindowLongW( hwnd, GWL_STYLE,
4771 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4772 es->style |= ES_READONLY;
4773 } else {
4774 SetWindowLongW( hwnd, GWL_STYLE,
4775 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4776 es->style &= ~ES_READONLY;
4779 if (old_style ^ es->style)
4780 NtUserInvalidateRect(es->hwndSelf, NULL, TRUE);
4782 result = 1;
4783 break;
4786 case EM_SETWORDBREAKPROC:
4787 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4788 result = 1;
4789 break;
4791 case EM_GETWORDBREAKPROC:
4792 result = (LRESULT)es->word_break_proc;
4793 break;
4795 case EM_GETPASSWORDCHAR:
4797 if(unicode)
4798 result = es->password_char;
4799 else
4801 WCHAR charW = es->password_char;
4802 CHAR charA = 0;
4803 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4804 result = charA;
4806 break;
4809 case EM_SETMARGINS:
4810 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4811 break;
4813 case EM_GETMARGINS:
4814 result = MAKELONG(es->left_margin, es->right_margin);
4815 break;
4817 case EM_GETLIMITTEXT:
4818 result = es->buffer_limit;
4819 break;
4821 case EM_POSFROMCHAR:
4822 if ((INT)wParam >= get_text_length(es)) result = -1;
4823 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4824 break;
4826 case EM_CHARFROMPOS:
4827 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4828 break;
4830 case EM_SETIMESTATUS:
4831 if (wParam == EMSIS_COMPOSITIONSTRING)
4832 es->ime_status = lParam & 0xFFFF;
4834 result = 1;
4835 break;
4837 case EM_GETIMESTATUS:
4838 result = wParam == EMSIS_COMPOSITIONSTRING ? es->ime_status : 1;
4839 break;
4841 /* End of the EM_ messages which were in numerical order; what order
4842 * are these in? vaguely alphabetical?
4845 case WM_NCCREATE:
4846 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4847 break;
4849 case WM_NCDESTROY:
4850 result = EDIT_WM_NCDestroy(es);
4851 es = NULL;
4852 break;
4854 case WM_GETDLGCODE:
4855 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4857 if (es->style & ES_MULTILINE)
4858 result |= DLGC_WANTALLKEYS;
4860 if (lParam)
4862 es->flags|=EF_DIALOGMODE;
4864 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4866 int vk = (int)((LPMSG)lParam)->wParam;
4868 if (es->hwndListBox)
4870 if (vk == VK_RETURN || vk == VK_ESCAPE)
4871 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4872 result |= DLGC_WANTMESSAGE;
4876 break;
4878 case WM_CHAR:
4880 WCHAR charW;
4882 if(unicode)
4883 charW = wParam;
4884 else
4886 BYTE low = wParam;
4887 DWORD cp = get_input_codepage();
4888 if (es->lead_byte)
4890 char ch[2];
4891 ch[0] = es->lead_byte;
4892 ch[1] = low;
4893 MultiByteToWideChar(cp, 0, ch, 2, &charW, 1);
4895 else if (IsDBCSLeadByteEx(cp, low))
4897 es->lead_byte = low;
4898 result = 1;
4899 break;
4901 else
4902 MultiByteToWideChar(cp, 0, (char *)&low, 1, &charW, 1);
4903 es->lead_byte = 0;
4906 if (es->hwndListBox)
4908 if (charW == VK_RETURN || charW == VK_ESCAPE)
4910 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4911 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4912 break;
4915 result = EDIT_WM_Char(es, charW);
4916 break;
4919 case WM_UNICHAR:
4920 if (unicode)
4922 if (wParam == UNICODE_NOCHAR) return TRUE;
4923 if (wParam <= 0x000fffff)
4925 if(wParam > 0xffff) /* convert to surrogates */
4927 wParam -= 0x10000;
4928 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4929 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4931 else EDIT_WM_Char(es, wParam);
4933 return 0;
4935 break;
4937 case WM_CLEAR:
4938 EDIT_WM_Clear(es);
4939 break;
4941 case WM_CONTEXTMENU:
4942 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4943 break;
4945 case WM_COPY:
4946 EDIT_WM_Copy(es);
4947 break;
4949 case WM_CREATE:
4950 if(unicode)
4951 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4952 else
4954 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4955 LPWSTR nameW = NULL;
4956 if(nameA)
4958 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
4959 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4960 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
4962 result = EDIT_WM_Create(es, nameW);
4963 HeapFree(GetProcessHeap(), 0, nameW);
4965 break;
4967 case WM_CUT:
4968 EDIT_WM_Cut(es);
4969 break;
4971 case WM_ENABLE:
4972 es->bEnableState = (BOOL) wParam;
4973 EDIT_UpdateText(es, NULL, TRUE);
4974 break;
4976 case WM_ERASEBKGND:
4977 /* we do the proper erase in EDIT_WM_Paint */
4978 result = 1;
4979 break;
4981 case WM_GETFONT:
4982 result = (LRESULT)es->font;
4983 break;
4985 case WM_GETTEXT:
4986 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
4987 break;
4989 case WM_GETTEXTLENGTH:
4990 if (unicode) result = get_text_length(es);
4991 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
4992 NULL, 0, NULL, NULL );
4993 break;
4995 case WM_HSCROLL:
4996 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4997 break;
4999 case WM_KEYDOWN:
5000 result = EDIT_WM_KeyDown(es, (INT)wParam);
5001 break;
5003 case WM_KILLFOCUS:
5004 result = EDIT_WM_KillFocus(es);
5005 break;
5007 case WM_LBUTTONDBLCLK:
5008 result = EDIT_WM_LButtonDblClk(es);
5009 break;
5011 case WM_LBUTTONDOWN:
5012 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5013 break;
5015 case WM_LBUTTONUP:
5016 result = EDIT_WM_LButtonUp(es);
5017 break;
5019 case WM_MBUTTONDOWN:
5020 result = EDIT_WM_MButtonDown(es);
5021 break;
5023 case WM_MOUSEMOVE:
5024 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5025 break;
5027 case WM_PRINTCLIENT:
5028 case WM_PAINT:
5029 EDIT_WM_Paint(es, (HDC)wParam);
5030 break;
5032 case WM_PASTE:
5033 EDIT_WM_Paste(es);
5034 break;
5036 case WM_SETFOCUS:
5037 EDIT_WM_SetFocus(es);
5038 break;
5040 case WM_SETFONT:
5041 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5042 break;
5044 case WM_SETREDRAW:
5045 /* FIXME: actually set an internal flag and behave accordingly */
5046 break;
5048 case WM_SETTEXT:
5049 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5050 result = TRUE;
5051 break;
5053 case WM_SIZE:
5054 EDIT_WM_Size(es, (UINT)wParam);
5055 break;
5057 case WM_STYLECHANGED:
5058 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5059 break;
5061 case WM_STYLECHANGING:
5062 result = 0; /* See EDIT_WM_StyleChanged */
5063 break;
5065 case WM_SYSKEYDOWN:
5066 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5067 break;
5069 case WM_TIMER:
5070 EDIT_WM_Timer(es);
5071 break;
5073 case WM_VSCROLL:
5074 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5075 break;
5077 case WM_MOUSEWHEEL:
5079 int wheelDelta;
5080 INT pulScrollLines = 3;
5081 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5083 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5084 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5085 break;
5087 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5088 /* if scrolling changes direction, ignore left overs */
5089 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5090 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5091 es->wheelDeltaRemainder += wheelDelta;
5092 else
5093 es->wheelDeltaRemainder = wheelDelta;
5094 if (es->wheelDeltaRemainder && pulScrollLines)
5096 int cLineScroll;
5097 pulScrollLines = min(es->line_count, pulScrollLines);
5098 cLineScroll = pulScrollLines * es->wheelDeltaRemainder / WHEEL_DELTA;
5099 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
5100 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5103 break;
5106 /* IME messages to make the edit control IME aware */
5107 case WM_IME_SETCONTEXT:
5108 NtUserGetCaretPos(&pt);
5109 EDIT_UpdateImmCompositionWindow(es, pt.x, pt.y);
5110 EDIT_UpdateImmCompositionFont(es);
5111 break;
5113 case WM_IME_COMPOSITION:
5114 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5115 if ((lParam & GCS_RESULTSTR) && (es->ime_status & EIMES_GETCOMPSTRATONCE))
5116 EDIT_ImeComposition(hwnd, lParam, es);
5117 return DefWindowProcW(hwnd, msg, wParam, lParam);
5119 case WM_IME_SELECT:
5120 break;
5122 case WM_IME_CONTROL:
5123 break;
5125 case WM_IME_REQUEST:
5126 switch (wParam)
5128 case IMR_QUERYCHARPOSITION:
5130 LRESULT pos;
5131 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5133 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5134 chpos->pt.x = LOWORD(pos);
5135 chpos->pt.y = HIWORD(pos);
5136 chpos->cLineHeight = es->line_height;
5137 chpos->rcDocument = es->format_rect;
5138 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5139 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5140 result = 1;
5141 break;
5144 break;
5146 default:
5147 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5148 break;
5151 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5152 EDIT_UnlockBuffer(es, FALSE);
5154 TRACE("hwnd=%p msg=%x (%s) -- 0x%08Ix\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5156 return result;