mfplat: Add MFCreateAMMediaTypeFromMFMediaType stub.
[wine.git] / dlls / user32 / edit.c
blob39f429813d633d1e3aab313f4e4c156eff139595
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 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
104 INT line_count; /* number of lines */
105 INT y_offset; /* scroll offset in number of lines */
106 BOOL bCaptureState; /* flag indicating whether mouse was captured */
107 BOOL bEnableState; /* flag keeping the enable state */
108 HWND hwndSelf; /* the our window handle */
109 HWND hwndParent; /* Handle of parent for sending EN_* messages.
110 Even if parent will change, EN_* messages
111 should be sent to the first parent. */
112 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
113 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
114 BYTE lead_byte; /* DBCS lead byte store for WM_CHAR */
116 * only for multi line controls
118 INT lock_count; /* amount of re-entries in the EditWndProc */
119 INT tabs_count;
120 LPINT tabs;
121 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
122 HLOCAL hloc32W; /* our unicode local memory block */
123 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
124 or EM_SETHANDLE */
125 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
127 * IME Data
129 UINT ime_status; /* IME status flag */
132 * Uniscribe Data
134 SCRIPT_LOGATTR *logAttr;
135 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
136 } EDITSTATE;
139 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
140 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
142 static inline BOOL notify_parent(const EDITSTATE *es, INT code)
144 HWND hwnd = es->hwndSelf;
145 TRACE("notification %d sent to %p.\n", code, es->hwndParent);
146 SendMessageW(es->hwndParent, WM_COMMAND, MAKEWPARAM(GetWindowLongPtrW(es->hwndSelf, GWLP_ID), code), (LPARAM)es->hwndSelf);
147 return IsWindow(hwnd);
150 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
152 /*********************************************************************
154 * EM_CANUNDO
157 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
159 return (es->undo_insert_count || lstrlenW(es->undo_text));
163 /*********************************************************************
165 * EM_EMPTYUNDOBUFFER
168 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
170 es->undo_insert_count = 0;
171 *es->undo_text = '\0';
175 /**********************************************************************
176 * get_app_version
178 * Returns the window version in case Wine emulates a later version
179 * of windows than the application expects.
181 * In a number of cases when windows runs an application that was
182 * designed for an earlier windows version, windows reverts
183 * to "old" behaviour of that earlier version.
185 * An example is a disabled edit control that needs to be painted.
186 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
187 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
188 * applications with an expected version 0f 4.0 or higher.
191 static DWORD get_app_version(void)
193 static DWORD version;
194 if (!version)
196 DWORD dwEmulatedVersion;
197 OSVERSIONINFOW info;
198 DWORD dwProcVersion = GetProcessVersion(0);
200 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
201 GetVersionExW( &info );
202 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
203 /* FIXME: this may not be 100% correct; see discussion on the
204 * wine developer list in Nov 1999 */
205 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
207 return version;
210 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
212 HBRUSH hbrush;
213 UINT msg;
215 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
216 msg = WM_CTLCOLORSTATIC;
217 else
218 msg = WM_CTLCOLOREDIT;
220 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
221 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
222 if (!hbrush)
223 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
224 return hbrush;
228 static inline UINT get_text_length(EDITSTATE *es)
230 if(es->text_length == (UINT)-1)
231 es->text_length = lstrlenW(es->text);
232 return es->text_length;
236 /*********************************************************************
238 * EDIT_WordBreakProc
240 * Find the beginning of words.
241 * Note: unlike the specs for a WordBreakProc, this function can
242 * only be called without linebreaks between s[0] up to
243 * s[count - 1]. Remember it is only called
244 * internally, so we can decide this for ourselves.
245 * Additionally we will always be breaking the full string.
248 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
250 INT ret = 0;
252 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
254 if(!s) return 0;
256 if (!es->logAttr)
258 SCRIPT_ANALYSIS psa;
260 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
261 psa.eScript = SCRIPT_UNDEFINED;
263 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
264 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
267 switch (action) {
268 case WB_LEFT:
269 if (index)
270 index--;
271 while (index && !es->logAttr[index].fSoftBreak)
272 index--;
273 ret = index;
274 break;
275 case WB_RIGHT:
276 if (!count)
277 break;
278 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
279 index++;
280 ret = index;
281 break;
282 case WB_ISDELIMITER:
283 ret = es->logAttr[index].fWhiteSpace;
284 break;
285 default:
286 ERR("unknown action code, please report !\n");
287 break;
289 return ret;
293 /*********************************************************************
295 * EDIT_CallWordBreakProc
297 * Call appropriate WordBreakProc (internal or external).
299 * Note: The "start" argument should always be an index referring
300 * to es->text. The actual wordbreak proc might be
301 * 16 bit, so we can't always pass any 32 bit LPSTR.
302 * Hence we assume that es->text is the buffer that holds
303 * the string under examination (we can decide this for ourselves).
306 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
308 INT ret;
310 if (es->word_break_proc)
312 if(es->is_unicode)
314 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
316 TRACE_(relay)("\1Call wordbreakW %p (str=%s,idx=%d,cnt=%d,act=%d)\n",
317 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
318 ret = wbpW(es->text + start, index, count, action);
319 TRACE_(relay)("\1Ret wordbreakW %p retval=%d\n", es->word_break_proc, ret );
321 else
323 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
324 INT countA;
325 CHAR *textA;
327 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
328 textA = HeapAlloc(GetProcessHeap(), 0, countA);
329 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
330 TRACE_(relay)("\1Call wordbreakA %p(str=%s,idx=%d,cnt=%d,act=%d)\n",
331 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
332 ret = wbpA(textA, index, countA, action);
333 HeapFree(GetProcessHeap(), 0, textA);
334 TRACE_(relay)("\1Ret wordbreakA %p retval=%d\n", es->word_break_proc, ret );
337 else
338 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
340 return ret;
343 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
345 if (line_def->ssa)
347 ScriptStringFree(&line_def->ssa);
348 line_def->ssa = NULL;
352 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
354 LINEDEF *line_def = es->first_line_def;
355 while (line_def)
357 EDIT_InvalidateUniscribeData_linedef(line_def);
358 line_def = line_def->next;
360 if (es->ssa)
362 ScriptStringFree(&es->ssa);
363 es->ssa = NULL;
367 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
369 if (!line_def)
370 return NULL;
372 if (line_def->net_length && !line_def->ssa)
374 int index = line_def->index;
375 HFONT old_font = NULL;
376 HDC udc = dc;
377 SCRIPT_TABDEF tabdef;
378 HRESULT hr;
380 if (!udc)
381 udc = NtUserGetDC(es->hwndSelf);
382 if (es->font)
383 old_font = SelectObject(udc, es->font);
385 tabdef.cTabStops = es->tabs_count;
386 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
387 tabdef.pTabStops = es->tabs;
388 tabdef.iTabOrigin = 0;
390 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
391 (1.5*line_def->net_length+16), -1,
392 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
393 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
394 if (FAILED(hr))
396 WARN("ScriptStringAnalyse failed (%lx)\n",hr);
397 line_def->ssa = NULL;
400 if (es->font)
401 SelectObject(udc, old_font);
402 if (udc != dc)
403 NtUserReleaseDC( es->hwndSelf, udc );
406 return line_def->ssa;
409 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
411 LINEDEF *line_def;
413 if (!(es->style & ES_MULTILINE))
415 if (!es->ssa)
417 INT length = get_text_length(es);
418 HFONT old_font = NULL;
419 HDC udc = dc;
421 if (!udc)
422 udc = NtUserGetDC(es->hwndSelf);
423 if (es->font)
424 old_font = SelectObject(udc, es->font);
426 if (es->style & ES_PASSWORD)
427 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);
428 else
429 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
431 if (es->font)
432 SelectObject(udc, old_font);
433 if (udc != dc)
434 NtUserReleaseDC( es->hwndSelf, udc );
436 return es->ssa;
438 else
440 line_def = es->first_line_def;
441 while (line_def && line)
443 line_def = line_def->next;
444 line--;
447 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
451 static inline INT get_vertical_line_count(EDITSTATE *es)
453 INT vlc = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
454 return max(1,vlc);
457 /*********************************************************************
459 * EDIT_BuildLineDefs_ML
461 * Build linked list of text lines.
462 * Lines can end with '\0' (last line), a character (if it is wrapped),
463 * a soft return '\r\r\n' or a hard return '\r\n'
466 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
468 LPWSTR current_position, cp;
469 INT fw;
470 LINEDEF *current_line;
471 LINEDEF *previous_line;
472 LINEDEF *start_line;
473 INT line_index = 0, nstart_line, nstart_index;
474 INT line_count = es->line_count;
475 INT orig_net_length;
476 RECT rc;
477 INT vlc;
479 if (istart == iend && delta == 0)
480 return;
482 previous_line = NULL;
483 current_line = es->first_line_def;
485 /* Find starting line. istart must lie inside an existing line or
486 * at the end of buffer */
487 do {
488 if (istart < current_line->index + current_line->length ||
489 current_line->ending == END_0)
490 break;
492 previous_line = current_line;
493 current_line = current_line->next;
494 line_index++;
495 } while (current_line);
497 if (!current_line) /* Error occurred start is not inside previous buffer */
499 FIXME(" modification occurred outside buffer\n");
500 return;
503 /* Remember start of modifications in order to calculate update region */
504 nstart_line = line_index;
505 nstart_index = current_line->index;
507 /* We must start to reformat from the previous line since the modifications
508 * may have caused the line to wrap upwards. */
509 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
511 line_index--;
512 current_line = previous_line;
514 start_line = current_line;
516 fw = es->format_rect.right - es->format_rect.left;
517 current_position = es->text + current_line->index;
518 vlc = get_vertical_line_count(es);
519 do {
520 if (current_line != start_line)
522 if (!current_line || current_line->index + delta > current_position - es->text)
524 /* The buffer has been expanded, create a new line and
525 insert it into the link list */
526 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
527 new_line->next = previous_line->next;
528 previous_line->next = new_line;
529 current_line = new_line;
530 es->line_count++;
532 else if (current_line->index + delta < current_position - es->text)
534 /* The previous line merged with this line so we delete this extra entry */
535 previous_line->next = current_line->next;
536 HeapFree(GetProcessHeap(), 0, current_line);
537 current_line = previous_line->next;
538 es->line_count--;
539 continue;
541 else /* current_line->index + delta == current_position */
543 if (current_position - es->text > iend)
544 break; /* We reached end of line modifications */
545 /* else recalculate this line */
549 current_line->index = current_position - es->text;
550 orig_net_length = current_line->net_length;
552 /* Find end of line */
553 cp = current_position;
554 while (*cp) {
555 if (*cp == '\n') break;
556 if ((*cp == '\r') && (*(cp + 1) == '\n'))
557 break;
558 cp++;
561 /* Mark type of line termination */
562 if (!(*cp)) {
563 current_line->ending = END_0;
564 current_line->net_length = lstrlenW(current_position);
565 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
566 current_line->ending = END_SOFT;
567 current_line->net_length = cp - current_position - 1;
568 } else if (*cp == '\n') {
569 current_line->ending = END_RICH;
570 current_line->net_length = cp - current_position;
571 } else {
572 current_line->ending = END_HARD;
573 current_line->net_length = cp - current_position;
576 if (current_line->net_length)
578 const SIZE *sz;
579 EDIT_InvalidateUniscribeData_linedef(current_line);
580 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
581 if (current_line->ssa)
583 sz = ScriptString_pSize(current_line->ssa);
584 /* Calculate line width */
585 current_line->width = sz->cx;
587 else current_line->width = es->char_width * current_line->net_length;
589 else current_line->width = 0;
591 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
593 /* Line breaks just look back from the end and find the next break and try that. */
595 if (!(es->style & ES_AUTOHSCROLL)) {
596 if (current_line->width > fw && fw > es->char_width) {
598 INT prev, next;
599 int w;
600 const SIZE *sz;
601 float d;
603 prev = current_line->net_length - 1;
604 w = current_line->net_length;
605 d = (float)current_line->width/(float)fw;
606 if (d > 1.2f) d -= 0.2f;
607 next = prev/d;
608 if (next >= prev) next = prev-1;
609 do {
610 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
611 next, current_line->net_length, WB_LEFT);
612 current_line->net_length = prev;
613 EDIT_InvalidateUniscribeData_linedef(current_line);
614 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
615 if (current_line->ssa)
616 sz = ScriptString_pSize(current_line->ssa);
617 else sz = 0;
618 if (sz)
619 current_line->width = sz->cx;
620 else
621 prev = 0;
622 next = prev - 1;
623 } while (prev && current_line->width > fw);
624 current_line->net_length = w;
626 if (prev == 0) { /* Didn't find a line break so force a break */
627 INT *piDx;
628 const INT *count;
630 EDIT_InvalidateUniscribeData_linedef(current_line);
631 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
633 if (current_line->ssa)
635 count = ScriptString_pcOutChars(current_line->ssa);
636 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
637 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
639 prev = current_line->net_length-1;
640 do {
641 current_line->width -= piDx[prev];
642 prev--;
643 } while ( prev > 0 && current_line->width > fw);
644 if (prev<=0)
645 prev = 1;
646 HeapFree(GetProcessHeap(),0,piDx);
648 else
649 prev = (fw / es->char_width);
652 /* If the first line we are calculating, wrapped before istart, we must
653 * adjust istart in order for this to be reflected in the update region. */
654 if (current_line->index == nstart_index && istart > current_line->index + prev)
655 istart = current_line->index + prev;
656 /* else if we are updating the previous line before the first line we
657 * are re-calculating and it expanded */
658 else if (current_line == start_line &&
659 current_line->index != nstart_index && orig_net_length < prev)
661 /* Line expanded due to an upwards line wrap so we must partially include
662 * previous line in update region */
663 nstart_line = line_index;
664 nstart_index = current_line->index;
665 istart = current_line->index + orig_net_length;
668 current_line->net_length = prev;
669 current_line->ending = END_WRAP;
671 if (current_line->net_length > 0)
673 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
674 if (current_line->ssa)
676 sz = ScriptString_pSize(current_line->ssa);
677 current_line->width = sz->cx;
679 else
680 current_line->width = 0;
682 else current_line->width = 0;
684 else if (current_line == start_line &&
685 current_line->index != nstart_index &&
686 orig_net_length < current_line->net_length) {
687 /* The previous line expanded but it's still not as wide as the client rect */
688 /* The expansion is due to an upwards line wrap so we must partially include
689 it in the update region */
690 nstart_line = line_index;
691 nstart_index = current_line->index;
692 istart = current_line->index + orig_net_length;
697 /* Adjust length to include line termination */
698 switch (current_line->ending) {
699 case END_SOFT:
700 current_line->length = current_line->net_length + 3;
701 break;
702 case END_RICH:
703 current_line->length = current_line->net_length + 1;
704 break;
705 case END_HARD:
706 current_line->length = current_line->net_length + 2;
707 break;
708 case END_WRAP:
709 case END_0:
710 current_line->length = current_line->net_length;
711 break;
713 es->text_width = max(es->text_width, current_line->width);
714 current_position += current_line->length;
715 previous_line = current_line;
717 /* Discard data for non-visible lines. It will be calculated as needed */
718 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
719 EDIT_InvalidateUniscribeData_linedef(current_line);
721 current_line = current_line->next;
722 line_index++;
723 } while (previous_line->ending != END_0);
725 /* Finish adjusting line indexes by delta or remove hanging lines */
726 if (previous_line->ending == END_0)
728 LINEDEF *pnext = NULL;
730 previous_line->next = NULL;
731 while (current_line)
733 pnext = current_line->next;
734 EDIT_InvalidateUniscribeData_linedef(current_line);
735 HeapFree(GetProcessHeap(), 0, current_line);
736 current_line = pnext;
737 es->line_count--;
740 else if (delta != 0)
742 while (current_line)
744 current_line->index += delta;
745 current_line = current_line->next;
749 /* Calculate rest of modification rectangle */
750 if (hrgn)
752 HRGN tmphrgn;
754 * We calculate two rectangles. One for the first line which may have
755 * an indent with respect to the format rect. The other is a format-width
756 * rectangle that spans the rest of the lines that changed or moved.
758 rc.top = es->format_rect.top + nstart_line * es->line_height -
759 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
760 rc.bottom = rc.top + es->line_height;
761 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
762 rc.left = es->format_rect.left;
763 else
764 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
765 rc.right = es->format_rect.right;
766 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
768 rc.top = rc.bottom;
769 rc.left = es->format_rect.left;
770 rc.right = es->format_rect.right;
772 * If lines were added or removed we must re-paint the remainder of the
773 * lines since the remaining lines were either shifted up or down.
775 if (line_count < es->line_count) /* We added lines */
776 rc.bottom = es->line_count * es->line_height;
777 else if (line_count > es->line_count) /* We removed lines */
778 rc.bottom = line_count * es->line_height;
779 else
780 rc.bottom = line_index * es->line_height;
781 rc.bottom += es->format_rect.top;
782 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
783 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
784 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
785 DeleteObject(tmphrgn);
789 /*********************************************************************
791 * EDIT_CalcLineWidth_SL
794 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
796 EDIT_UpdateUniscribeData(es, NULL, 0);
797 if (es->ssa)
799 const SIZE *size;
800 size = ScriptString_pSize(es->ssa);
801 es->text_width = size->cx;
803 else
804 es->text_width = 0;
807 /*********************************************************************
809 * EDIT_CharFromPos
811 * Beware: This is not the function called on EM_CHARFROMPOS
812 * The position _can_ be outside the formatting / client
813 * rectangle
814 * The return value is only the character index
817 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
819 INT index;
821 if (es->style & ES_MULTILINE) {
822 int trailing;
823 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
824 INT line_index = 0;
825 LINEDEF *line_def = es->first_line_def;
826 EDIT_UpdateUniscribeData(es, NULL, line);
827 while ((line > 0) && line_def->next) {
828 line_index += line_def->length;
829 line_def = line_def->next;
830 line--;
833 x += es->x_offset - es->format_rect.left;
834 if (es->style & ES_RIGHT)
835 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
836 else if (es->style & ES_CENTER)
837 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
838 if (x >= line_def->width) {
839 if (after_wrap)
840 *after_wrap = (line_def->ending == END_WRAP);
841 return line_index + line_def->net_length;
843 if (x <= 0 || !line_def->ssa) {
844 if (after_wrap)
845 *after_wrap = FALSE;
846 return line_index;
849 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
850 if (trailing) index++;
851 index += line_index;
852 if (after_wrap)
853 *after_wrap = ((index == line_index + line_def->net_length) &&
854 (line_def->ending == END_WRAP));
855 } else {
856 INT xoff = 0;
857 INT trailing;
858 if (after_wrap)
859 *after_wrap = FALSE;
860 x -= es->format_rect.left;
861 if (!x)
862 return es->x_offset;
864 if (!es->x_offset)
866 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
867 if (es->style & ES_RIGHT)
868 x -= indent;
869 else if (es->style & ES_CENTER)
870 x -= indent / 2;
873 EDIT_UpdateUniscribeData(es, NULL, 0);
874 if (es->x_offset)
876 if (es->ssa)
878 if (es->x_offset>= get_text_length(es))
880 const SIZE *size;
881 size = ScriptString_pSize(es->ssa);
882 xoff = size->cx;
884 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
886 else
887 xoff = 0;
889 if (x < 0)
891 if (x + xoff > 0 || !es->ssa)
893 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
894 if (trailing) index++;
896 else
897 index = 0;
899 else
901 if (x)
903 const SIZE *size = NULL;
904 if (es->ssa)
905 size = ScriptString_pSize(es->ssa);
906 if (!size)
907 index = 0;
908 else if (x > size->cx)
909 index = get_text_length(es);
910 else if (es->ssa)
912 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
913 if (trailing) index++;
915 else
916 index = 0;
918 else
919 index = es->x_offset;
922 return index;
926 /*********************************************************************
928 * EDIT_ConfinePoint
930 * adjusts the point to be within the formatting rectangle
931 * (so CharFromPos returns the nearest _visible_ character)
934 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
936 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
937 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
941 /*********************************************************************
943 * EM_LINEFROMCHAR
946 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
948 INT line;
949 LINEDEF *line_def;
951 if (!(es->style & ES_MULTILINE))
952 return 0;
953 if (index > (INT)get_text_length(es))
954 return es->line_count - 1;
955 if (index == -1)
956 index = min(es->selection_start, es->selection_end);
958 line = 0;
959 line_def = es->first_line_def;
960 index -= line_def->length;
961 while ((index >= 0) && line_def->next) {
962 line++;
963 line_def = line_def->next;
964 index -= line_def->length;
966 return line;
970 /*********************************************************************
972 * EM_LINEINDEX
975 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
977 INT line_index;
978 const LINEDEF *line_def;
980 if (!(es->style & ES_MULTILINE))
981 return 0;
982 if (line >= es->line_count)
983 return -1;
985 line_index = 0;
986 line_def = es->first_line_def;
987 if (line == -1) {
988 INT index = es->selection_end - line_def->length;
989 while ((index >= 0) && line_def->next) {
990 line_index += line_def->length;
991 line_def = line_def->next;
992 index -= line_def->length;
994 } else {
995 while (line > 0) {
996 line_index += line_def->length;
997 line_def = line_def->next;
998 line--;
1001 return line_index;
1005 /*********************************************************************
1007 * EM_LINELENGTH
1010 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1012 LINEDEF *line_def;
1014 if (!(es->style & ES_MULTILINE))
1015 return get_text_length(es);
1017 if (index == -1) {
1018 /* get the number of remaining non-selected chars of selected lines */
1019 INT32 l; /* line number */
1020 INT32 li; /* index of first char in line */
1021 INT32 count;
1022 l = EDIT_EM_LineFromChar(es, es->selection_start);
1023 /* # chars before start of selection area */
1024 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1025 l = EDIT_EM_LineFromChar(es, es->selection_end);
1026 /* # chars after end of selection */
1027 li = EDIT_EM_LineIndex(es, l);
1028 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1029 return count;
1031 line_def = es->first_line_def;
1032 index -= line_def->length;
1033 while ((index >= 0) && line_def->next) {
1034 line_def = line_def->next;
1035 index -= line_def->length;
1037 return line_def->net_length;
1041 /*********************************************************************
1043 * EM_POSFROMCHAR
1046 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1048 INT len = get_text_length(es);
1049 INT l;
1050 INT li;
1051 INT x = 0;
1052 INT y = 0;
1053 INT w;
1054 INT lw;
1055 LINEDEF *line_def;
1057 index = min(index, len);
1058 if (es->style & ES_MULTILINE) {
1059 l = EDIT_EM_LineFromChar(es, index);
1060 EDIT_UpdateUniscribeData(es, NULL, l);
1062 y = (l - es->y_offset) * es->line_height;
1063 li = EDIT_EM_LineIndex(es, l);
1064 if (after_wrap && (li == index) && l) {
1065 INT l2 = l - 1;
1066 line_def = es->first_line_def;
1067 while (l2) {
1068 line_def = line_def->next;
1069 l2--;
1071 if (line_def->ending == END_WRAP) {
1072 l--;
1073 y -= es->line_height;
1074 li = EDIT_EM_LineIndex(es, l);
1078 line_def = es->first_line_def;
1079 while (line_def->index != li)
1080 line_def = line_def->next;
1082 lw = line_def->width;
1083 w = es->format_rect.right - es->format_rect.left;
1084 if (line_def->ssa)
1085 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1086 x -= es->x_offset;
1088 if (es->style & ES_RIGHT)
1089 x = w - (lw - x);
1090 else if (es->style & ES_CENTER)
1091 x += (w - lw) / 2;
1092 } else {
1093 INT xoff = 0;
1094 INT xi = 0;
1095 EDIT_UpdateUniscribeData(es, NULL, 0);
1096 if (es->x_offset)
1098 if (es->ssa)
1100 if (es->x_offset >= get_text_length(es))
1102 int leftover = es->x_offset - get_text_length(es);
1103 if (es->ssa)
1105 const SIZE *size;
1106 size = ScriptString_pSize(es->ssa);
1107 xoff = size->cx;
1109 else
1110 xoff = 0;
1111 xoff += es->char_width * leftover;
1113 else
1114 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1116 else
1117 xoff = 0;
1119 if (index)
1121 if (index >= get_text_length(es))
1123 if (es->ssa)
1125 const SIZE *size;
1126 size = ScriptString_pSize(es->ssa);
1127 xi = size->cx;
1129 else
1130 xi = 0;
1132 else if (es->ssa)
1133 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1134 else
1135 xi = 0;
1137 x = xi - xoff;
1139 if (index >= es->x_offset) {
1140 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1142 w = es->format_rect.right - es->format_rect.left;
1143 if (w > es->text_width)
1145 if (es->style & ES_RIGHT)
1146 x += w - es->text_width;
1147 else if (es->style & ES_CENTER)
1148 x += (w - es->text_width) / 2;
1152 y = 0;
1154 x += es->format_rect.left;
1155 y += es->format_rect.top;
1156 return MAKELONG((INT16)x, (INT16)y);
1160 /*********************************************************************
1162 * EDIT_GetLineRect
1164 * Calculates the bounding rectangle for a line from a starting
1165 * column to an ending column.
1168 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1170 SCRIPT_STRING_ANALYSIS ssa;
1171 INT line_index = 0;
1172 INT pt1, pt2, pt3;
1174 if (es->style & ES_MULTILINE)
1176 const LINEDEF *line_def = NULL;
1177 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1178 if (line >= es->line_count)
1179 return;
1181 line_def = es->first_line_def;
1182 if (line == -1) {
1183 INT index = es->selection_end - line_def->length;
1184 while ((index >= 0) && line_def->next) {
1185 line_index += line_def->length;
1186 line_def = line_def->next;
1187 index -= line_def->length;
1189 } else {
1190 while (line > 0) {
1191 line_index += line_def->length;
1192 line_def = line_def->next;
1193 line--;
1196 ssa = line_def->ssa;
1198 else
1200 line_index = 0;
1201 rc->top = es->format_rect.top;
1202 ssa = es->ssa;
1205 rc->bottom = rc->top + es->line_height;
1206 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1207 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1208 if (ssa)
1210 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1211 pt3+=es->format_rect.left;
1213 else pt3 = pt1;
1214 rc->right = max(max(pt1 , pt2),pt3);
1215 rc->left = min(min(pt1, pt2),pt3);
1219 static inline void text_buffer_changed(EDITSTATE *es)
1221 es->text_length = (UINT)-1;
1223 HeapFree( GetProcessHeap(), 0, es->logAttr );
1224 es->logAttr = NULL;
1225 EDIT_InvalidateUniscribeData(es);
1228 /*********************************************************************
1229 * EDIT_LockBuffer
1232 static void EDIT_LockBuffer(EDITSTATE *es)
1234 if (!es->text) {
1236 if(!es->hloc32W) return;
1238 if(es->hloc32A)
1240 CHAR *textA = LocalLock(es->hloc32A);
1241 HLOCAL hloc32W_new;
1242 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1243 if(countW_new > es->buffer_size + 1)
1245 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1246 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1247 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1248 if(hloc32W_new)
1250 es->hloc32W = hloc32W_new;
1251 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1252 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1254 else
1255 WARN("FAILED! Will synchronize partially\n");
1257 es->text = LocalLock(es->hloc32W);
1258 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1259 LocalUnlock(es->hloc32A);
1261 else es->text = LocalLock(es->hloc32W);
1263 es->lock_count++;
1267 /*********************************************************************
1269 * EDIT_UnlockBuffer
1272 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1274 /* Edit window might be already destroyed */
1275 if(!IsWindow(es->hwndSelf))
1277 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1278 return;
1281 if (!es->lock_count) {
1282 ERR("lock_count == 0 ... please report\n");
1283 return;
1285 if (!es->text) {
1286 ERR("es->text == 0 ... please report\n");
1287 return;
1289 if (force || (es->lock_count == 1)) {
1290 if (es->hloc32W) {
1291 UINT countA = 0;
1292 UINT countW = get_text_length(es) + 1;
1294 if(es->hloc32A)
1296 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1297 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1298 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1299 countA = LocalSize(es->hloc32A);
1300 if(countA_new > countA)
1302 HLOCAL hloc32A_new;
1303 UINT alloc_size = ROUND_TO_GROW(countA_new);
1304 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1305 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1306 if(hloc32A_new)
1308 es->hloc32A = hloc32A_new;
1309 countA = LocalSize(hloc32A_new);
1310 TRACE("Real new size %d bytes\n", countA);
1312 else
1313 WARN("FAILED! Will synchronize partially\n");
1315 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1316 LocalLock(es->hloc32A), countA, NULL, NULL);
1317 LocalUnlock(es->hloc32A);
1320 LocalUnlock(es->hloc32W);
1321 es->text = NULL;
1323 else {
1324 ERR("no buffer ... please report\n");
1325 return;
1328 es->lock_count--;
1332 /*********************************************************************
1334 * EDIT_MakeFit
1336 * Try to fit size + 1 characters in the buffer.
1338 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1340 HLOCAL hNew32W;
1342 if (size <= es->buffer_size)
1343 return TRUE;
1345 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1347 /* Force edit to unlock its buffer. es->text now NULL */
1348 EDIT_UnlockBuffer(es, TRUE);
1350 if (es->hloc32W) {
1351 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1352 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1353 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1354 es->hloc32W = hNew32W;
1355 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1359 EDIT_LockBuffer(es);
1361 if (es->buffer_size < size) {
1362 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1363 notify_parent(es, EN_ERRSPACE);
1364 return FALSE;
1365 } else {
1366 TRACE("We now have %d+1\n", es->buffer_size);
1367 return TRUE;
1372 /*********************************************************************
1374 * EDIT_MakeUndoFit
1376 * Try to fit size + 1 bytes in the undo buffer.
1379 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1381 UINT alloc_size;
1383 if (size <= es->undo_buffer_size)
1384 return TRUE;
1386 TRACE("trying to ReAlloc to %d+1\n", size);
1388 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1389 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1390 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1391 return TRUE;
1393 else
1395 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1396 return FALSE;
1401 /*********************************************************************
1403 * EDIT_UpdateTextRegion
1406 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1408 if (es->flags & EF_UPDATE) {
1409 es->flags &= ~EF_UPDATE;
1410 if (!notify_parent(es, EN_UPDATE)) return;
1412 NtUserInvalidateRgn(es->hwndSelf, hrgn, bErase);
1416 /*********************************************************************
1418 * EDIT_UpdateText
1421 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1423 if (es->flags & EF_UPDATE) {
1424 es->flags &= ~EF_UPDATE;
1425 if (!notify_parent(es, EN_UPDATE)) return;
1427 NtUserInvalidateRect(es->hwndSelf, rc, bErase);
1430 /*********************************************************************
1432 * EDIT_SL_InvalidateText
1434 * Called from EDIT_InvalidateText().
1435 * Does the job for single-line controls only.
1438 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1440 RECT line_rect;
1441 RECT rc;
1443 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1444 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1445 EDIT_UpdateText(es, &rc, TRUE);
1448 /*********************************************************************
1450 * EDIT_ML_InvalidateText
1452 * Called from EDIT_InvalidateText().
1453 * Does the job for multi-line controls only.
1456 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1458 INT vlc = get_vertical_line_count(es);
1459 INT sl = EDIT_EM_LineFromChar(es, start);
1460 INT el = EDIT_EM_LineFromChar(es, end);
1461 INT sc;
1462 INT ec;
1463 RECT rc1;
1464 RECT rcWnd;
1465 RECT rcLine;
1466 RECT rcUpdate;
1467 INT l;
1469 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1470 return;
1472 sc = start - EDIT_EM_LineIndex(es, sl);
1473 ec = end - EDIT_EM_LineIndex(es, el);
1474 if (sl < es->y_offset) {
1475 sl = es->y_offset;
1476 sc = 0;
1478 if (el > es->y_offset + vlc) {
1479 el = es->y_offset + vlc;
1480 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1482 GetClientRect(es->hwndSelf, &rc1);
1483 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1484 if (sl == el) {
1485 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1486 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1487 EDIT_UpdateText(es, &rcUpdate, TRUE);
1488 } else {
1489 EDIT_GetLineRect(es, sl, sc,
1490 EDIT_EM_LineLength(es,
1491 EDIT_EM_LineIndex(es, sl)),
1492 &rcLine);
1493 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1494 EDIT_UpdateText(es, &rcUpdate, TRUE);
1495 for (l = sl + 1 ; l < el ; l++) {
1496 EDIT_GetLineRect(es, l, 0,
1497 EDIT_EM_LineLength(es,
1498 EDIT_EM_LineIndex(es, l)),
1499 &rcLine);
1500 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1501 EDIT_UpdateText(es, &rcUpdate, TRUE);
1503 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1504 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1505 EDIT_UpdateText(es, &rcUpdate, TRUE);
1510 /*********************************************************************
1512 * EDIT_InvalidateText
1514 * Invalidate the text from offset start up to, but not including,
1515 * offset end. Useful for (re)painting the selection.
1516 * Regions outside the linewidth are not invalidated.
1517 * end == -1 means end == TextLength.
1518 * start and end need not be ordered.
1521 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1523 if (end == start)
1524 return;
1526 if (end == -1)
1527 end = get_text_length(es);
1529 if (end < start) {
1530 INT tmp = start;
1531 start = end;
1532 end = tmp;
1535 if (es->style & ES_MULTILINE)
1536 EDIT_ML_InvalidateText(es, start, end);
1537 else
1538 EDIT_SL_InvalidateText(es, start, end);
1542 /*********************************************************************
1544 * EDIT_EM_SetSel
1546 * note: unlike the specs say: the order of start and end
1547 * _is_ preserved in Windows. (i.e. start can be > end)
1548 * In other words: this handler is OK
1551 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1553 UINT old_start = es->selection_start;
1554 UINT old_end = es->selection_end;
1555 UINT len = get_text_length(es);
1557 if (start == (UINT)-1) {
1558 start = es->selection_end;
1559 end = es->selection_end;
1560 } else {
1561 start = min(start, len);
1562 end = min(end, len);
1564 es->selection_start = start;
1565 es->selection_end = end;
1566 if (after_wrap)
1567 es->flags |= EF_AFTER_WRAP;
1568 else
1569 es->flags &= ~EF_AFTER_WRAP;
1570 /* Compute the necessary invalidation region. */
1571 /* Note that we don't need to invalidate regions which have
1572 * "never" been selected, or those which are "still" selected.
1573 * In fact, every time we hit a selection boundary, we can
1574 * *toggle* whether we need to invalidate. Thus we can optimize by
1575 * *sorting* the interval endpoints. Let's assume that we sort them
1576 * in this order:
1577 * start <= end <= old_start <= old_end
1578 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1579 * in 5 comparisons; i.e. it is impossible to do better than the
1580 * following: */
1581 ORDER_UINT(end, old_end);
1582 ORDER_UINT(start, old_start);
1583 ORDER_UINT(old_start, old_end);
1584 ORDER_UINT(start, end);
1585 /* Note that at this point 'end' and 'old_start' are not in order, but
1586 * start is definitely the min. and old_end is definitely the max. */
1587 if (end != old_start)
1590 * One can also do
1591 * ORDER_UINT32(end, old_start);
1592 * EDIT_InvalidateText(es, start, end);
1593 * EDIT_InvalidateText(es, old_start, old_end);
1594 * in place of the following if statement.
1595 * (That would complete the optimal five-comparison four-element sort.)
1597 if (old_start > end )
1599 EDIT_InvalidateText(es, start, end);
1600 EDIT_InvalidateText(es, old_start, old_end);
1602 else
1604 EDIT_InvalidateText(es, start, old_start);
1605 EDIT_InvalidateText(es, end, old_end);
1608 else EDIT_InvalidateText(es, start, old_end);
1612 /*********************************************************************
1614 * EDIT_UpdateScrollInfo
1617 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1619 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1621 SCROLLINFO si;
1622 si.cbSize = sizeof(SCROLLINFO);
1623 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1624 si.nMin = 0;
1625 si.nMax = es->line_count - 1;
1626 si.nPage = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
1627 si.nPos = es->y_offset;
1628 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1629 si.nMin, si.nMax, si.nPage, si.nPos);
1630 NtUserSetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1633 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1635 SCROLLINFO si;
1636 si.cbSize = sizeof(SCROLLINFO);
1637 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1638 si.nMin = 0;
1639 si.nMax = es->text_width - 1;
1640 si.nPage = es->format_rect.right - es->format_rect.left;
1641 si.nPos = es->x_offset;
1642 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1643 si.nMin, si.nMax, si.nPage, si.nPos);
1644 NtUserSetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1649 /*********************************************************************
1651 * EDIT_EM_LineScroll_internal
1653 * Version of EDIT_EM_LineScroll for internal use.
1654 * It doesn't refuse if ES_MULTILINE is set and assumes that
1655 * dx is in pixels, dy - in lines.
1658 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1660 INT nyoff;
1661 INT x_offset_in_pixels;
1662 INT lines_per_page;
1664 if (!es->line_height || !es->char_width)
1665 return TRUE;
1667 lines_per_page = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1669 if (es->style & ES_MULTILINE)
1671 x_offset_in_pixels = es->x_offset;
1673 else
1675 dy = 0;
1676 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1679 if (-dx > x_offset_in_pixels)
1680 dx = -x_offset_in_pixels;
1681 if (dx > es->text_width - x_offset_in_pixels)
1682 dx = es->text_width - x_offset_in_pixels;
1683 nyoff = max(0, es->y_offset + dy);
1684 if (nyoff >= es->line_count - lines_per_page)
1685 nyoff = max(0, es->line_count - lines_per_page);
1686 dy = (es->y_offset - nyoff) * es->line_height;
1687 if (dx || dy) {
1688 RECT rc1;
1689 RECT rc;
1691 es->y_offset = nyoff;
1692 if(es->style & ES_MULTILINE)
1693 es->x_offset += dx;
1694 else
1695 es->x_offset += dx / es->char_width;
1697 GetClientRect(es->hwndSelf, &rc1);
1698 IntersectRect(&rc, &rc1, &es->format_rect);
1699 NtUserScrollWindowEx(es->hwndSelf, -dx, dy,
1700 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1701 /* force scroll info update */
1702 EDIT_UpdateScrollInfo(es);
1704 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1705 notify_parent(es, EN_HSCROLL);
1706 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1707 notify_parent(es, EN_VSCROLL);
1708 return TRUE;
1711 /*********************************************************************
1713 * EM_LINESCROLL
1715 * NOTE: dx is in average character widths, dy - in lines;
1718 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1720 if (!(es->style & ES_MULTILINE))
1721 return FALSE;
1723 dx *= es->char_width;
1724 return EDIT_EM_LineScroll_internal(es, dx, dy);
1728 /*********************************************************************
1730 * EM_SCROLL
1733 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1735 INT dy;
1737 if (!(es->style & ES_MULTILINE))
1738 return (LRESULT)FALSE;
1740 dy = 0;
1742 switch (action) {
1743 case SB_LINEUP:
1744 if (es->y_offset)
1745 dy = -1;
1746 break;
1747 case SB_LINEDOWN:
1748 if (es->y_offset < es->line_count - 1)
1749 dy = 1;
1750 break;
1751 case SB_PAGEUP:
1752 if (es->y_offset)
1753 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1754 break;
1755 case SB_PAGEDOWN:
1756 if (es->y_offset < es->line_count - 1)
1757 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1758 break;
1759 default:
1760 return (LRESULT)FALSE;
1762 if (dy) {
1763 INT vlc = get_vertical_line_count(es);
1764 /* check if we are going to move too far */
1765 if(es->y_offset + dy > es->line_count - vlc)
1766 dy = max(es->line_count - vlc, 0) - es->y_offset;
1768 /* Notification is done in EDIT_EM_LineScroll */
1769 if(dy) {
1770 EDIT_EM_LineScroll(es, 0, dy);
1771 return MAKELONG(dy, TRUE);
1775 return (LRESULT)FALSE;
1779 static void EDIT_UpdateImmCompositionWindow(EDITSTATE *es, UINT x, UINT y)
1781 COMPOSITIONFORM form =
1783 .dwStyle = CFS_RECT,
1784 .ptCurrentPos = {.x = x, .y = y},
1785 .rcArea = es->format_rect,
1787 HIMC himc = ImmGetContext(es->hwndSelf);
1788 ImmSetCompositionWindow(himc, &form);
1789 ImmReleaseContext(es->hwndSelf, himc);
1793 /*********************************************************************
1795 * EDIT_SetCaretPos
1798 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1799 BOOL after_wrap)
1801 LRESULT res;
1803 if (es->flags & EF_FOCUSED)
1805 res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1806 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1807 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1808 EDIT_UpdateImmCompositionWindow(es, (short)LOWORD(res), (short)HIWORD(res));
1813 /*********************************************************************
1815 * EM_SCROLLCARET
1818 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1820 if (es->style & ES_MULTILINE) {
1821 INT l;
1822 INT vlc;
1823 INT ww;
1824 INT cw = es->char_width;
1825 INT x;
1826 INT dy = 0;
1827 INT dx = 0;
1829 l = EDIT_EM_LineFromChar(es, es->selection_end);
1830 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1831 vlc = get_vertical_line_count(es);
1832 if (l >= es->y_offset + vlc)
1833 dy = l - vlc + 1 - es->y_offset;
1834 if (l < es->y_offset)
1835 dy = l - es->y_offset;
1836 ww = es->format_rect.right - es->format_rect.left;
1837 if (x < es->format_rect.left)
1838 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1839 if (x > es->format_rect.right)
1840 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1841 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1843 /* check if we are going to move too far */
1844 if(es->x_offset + dx + ww > es->text_width)
1845 dx = es->text_width - ww - es->x_offset;
1846 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1847 EDIT_EM_LineScroll_internal(es, dx, dy);
1849 } else {
1850 INT x;
1851 INT goal;
1852 INT format_width;
1854 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1855 format_width = es->format_rect.right - es->format_rect.left;
1856 if (x < es->format_rect.left) {
1857 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1858 do {
1859 es->x_offset--;
1860 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1861 } while ((x < goal) && es->x_offset);
1862 /* FIXME: use ScrollWindow() somehow to improve performance */
1863 EDIT_UpdateText(es, NULL, TRUE);
1864 } else if (x > es->format_rect.right) {
1865 INT x_last;
1866 INT len = get_text_length(es);
1867 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1868 do {
1869 es->x_offset++;
1870 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1871 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1872 } while ((x > goal) && (x_last > es->format_rect.right));
1873 /* FIXME: use ScrollWindow() somehow to improve performance */
1874 EDIT_UpdateText(es, NULL, TRUE);
1878 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1882 /*********************************************************************
1884 * EDIT_MoveBackward
1887 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1889 INT e = es->selection_end;
1891 if (e) {
1892 e--;
1893 if ((es->style & ES_MULTILINE) && e &&
1894 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1895 e--;
1896 if (e && (es->text[e - 1] == '\r'))
1897 e--;
1900 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1901 EDIT_EM_ScrollCaret(es);
1905 /*********************************************************************
1907 * EDIT_MoveDown_ML
1909 * Only for multi line controls
1910 * Move the caret one line down, on a column with the nearest
1911 * x coordinate on the screen (might be a different column).
1914 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1916 INT s = es->selection_start;
1917 INT e = es->selection_end;
1918 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1919 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1920 INT x = (short)LOWORD(pos);
1921 INT y = (short)HIWORD(pos);
1923 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1924 if (!extend)
1925 s = e;
1926 EDIT_EM_SetSel(es, s, e, after_wrap);
1927 EDIT_EM_ScrollCaret(es);
1931 /*********************************************************************
1933 * EDIT_MoveEnd
1936 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1938 BOOL after_wrap = FALSE;
1939 INT e;
1941 /* Pass a high value in x to make sure of receiving the end of the line */
1942 if (!ctrl && (es->style & ES_MULTILINE))
1943 e = EDIT_CharFromPos(es, 0x3fffffff,
1944 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1945 else
1946 e = get_text_length(es);
1947 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1948 EDIT_EM_ScrollCaret(es);
1952 /*********************************************************************
1954 * EDIT_MoveForward
1957 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1959 INT e = es->selection_end;
1961 if (es->text[e]) {
1962 e++;
1963 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1964 if (es->text[e] == '\n')
1965 e++;
1966 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1967 e += 2;
1970 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1971 EDIT_EM_ScrollCaret(es);
1975 /*********************************************************************
1977 * EDIT_MoveHome
1979 * Home key: move to beginning of line.
1982 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1984 INT e;
1986 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1987 if (!ctrl && (es->style & ES_MULTILINE))
1988 e = EDIT_CharFromPos(es, -es->x_offset,
1989 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1990 else
1991 e = 0;
1992 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1993 EDIT_EM_ScrollCaret(es);
1997 /*********************************************************************
1999 * EDIT_MovePageDown_ML
2001 * Only for multi line controls
2002 * Move the caret one page down, on a column with the nearest
2003 * x coordinate on the screen (might be a different column).
2006 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2008 INT s = es->selection_start;
2009 INT e = es->selection_end;
2010 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2011 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2012 INT x = (short)LOWORD(pos);
2013 INT y = (short)HIWORD(pos);
2015 e = EDIT_CharFromPos(es, x,
2016 y + (es->format_rect.bottom - es->format_rect.top),
2017 &after_wrap);
2018 if (!extend)
2019 s = e;
2020 EDIT_EM_SetSel(es, s, e, after_wrap);
2021 EDIT_EM_ScrollCaret(es);
2025 /*********************************************************************
2027 * EDIT_MovePageUp_ML
2029 * Only for multi line controls
2030 * Move the caret one page up, on a column with the nearest
2031 * x coordinate on the screen (might be a different column).
2034 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2036 INT s = es->selection_start;
2037 INT e = es->selection_end;
2038 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2039 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2040 INT x = (short)LOWORD(pos);
2041 INT y = (short)HIWORD(pos);
2043 e = EDIT_CharFromPos(es, x,
2044 y - (es->format_rect.bottom - es->format_rect.top),
2045 &after_wrap);
2046 if (!extend)
2047 s = e;
2048 EDIT_EM_SetSel(es, s, e, after_wrap);
2049 EDIT_EM_ScrollCaret(es);
2053 /*********************************************************************
2055 * EDIT_MoveUp_ML
2057 * Only for multi line controls
2058 * Move the caret one line up, on a column with the nearest
2059 * x coordinate on the screen (might be a different column).
2062 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2064 INT s = es->selection_start;
2065 INT e = es->selection_end;
2066 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2067 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2068 INT x = (short)LOWORD(pos);
2069 INT y = (short)HIWORD(pos);
2071 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2072 if (!extend)
2073 s = e;
2074 EDIT_EM_SetSel(es, s, e, after_wrap);
2075 EDIT_EM_ScrollCaret(es);
2079 /*********************************************************************
2081 * EDIT_MoveWordBackward
2084 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2086 INT s = es->selection_start;
2087 INT e = es->selection_end;
2088 INT l;
2089 INT ll;
2090 INT li;
2092 l = EDIT_EM_LineFromChar(es, e);
2093 ll = EDIT_EM_LineLength(es, e);
2094 li = EDIT_EM_LineIndex(es, l);
2095 if (e - li == 0) {
2096 if (l) {
2097 li = EDIT_EM_LineIndex(es, l - 1);
2098 e = li + EDIT_EM_LineLength(es, li);
2100 } else {
2101 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2103 if (!extend)
2104 s = e;
2105 EDIT_EM_SetSel(es, s, e, FALSE);
2106 EDIT_EM_ScrollCaret(es);
2110 /*********************************************************************
2112 * EDIT_MoveWordForward
2115 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2117 INT s = es->selection_start;
2118 INT e = es->selection_end;
2119 INT l;
2120 INT ll;
2121 INT li;
2123 l = EDIT_EM_LineFromChar(es, e);
2124 ll = EDIT_EM_LineLength(es, e);
2125 li = EDIT_EM_LineIndex(es, l);
2126 if (e - li == ll) {
2127 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2128 e = EDIT_EM_LineIndex(es, l + 1);
2129 } else {
2130 e = li + EDIT_CallWordBreakProc(es,
2131 li, e - li + 1, ll, WB_RIGHT);
2133 if (!extend)
2134 s = e;
2135 EDIT_EM_SetSel(es, s, e, FALSE);
2136 EDIT_EM_ScrollCaret(es);
2140 /*********************************************************************
2142 * EDIT_PaintText
2145 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2147 COLORREF BkColor;
2148 COLORREF TextColor;
2149 INT ret;
2150 INT li;
2151 INT BkMode;
2152 SIZE size;
2154 if (!count)
2155 return 0;
2156 BkMode = GetBkMode(dc);
2157 BkColor = GetBkColor(dc);
2158 TextColor = GetTextColor(dc);
2159 if (rev) {
2160 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2161 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2162 SetBkMode(dc, OPAQUE);
2164 li = EDIT_EM_LineIndex(es, line);
2165 if (es->style & ES_MULTILINE) {
2166 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2167 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2168 } else {
2169 TextOutW(dc, x, y, es->text + li + col, count);
2170 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2171 ret = size.cx;
2173 if (rev) {
2174 SetBkColor(dc, BkColor);
2175 SetTextColor(dc, TextColor);
2176 SetBkMode(dc, BkMode);
2178 return ret;
2182 /*********************************************************************
2184 * EDIT_PaintLine
2187 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2189 INT s = 0;
2190 INT e = 0;
2191 INT li = 0;
2192 INT ll = 0;
2193 INT x;
2194 INT y;
2195 LRESULT pos;
2196 SCRIPT_STRING_ANALYSIS ssa;
2198 if (es->style & ES_MULTILINE) {
2199 INT vlc = get_vertical_line_count(es);
2201 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2202 return;
2203 } else if (line)
2204 return;
2206 TRACE("line=%d\n", line);
2208 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2209 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2210 x = (short)LOWORD(pos);
2211 y = (short)HIWORD(pos);
2213 if (es->style & ES_MULTILINE)
2215 int line_idx = line;
2216 x = -es->x_offset;
2217 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2219 LINEDEF *line_def = es->first_line_def;
2220 int w, lw;
2222 while (line_def && line_idx)
2224 line_def = line_def->next;
2225 line_idx--;
2227 w = es->format_rect.right - es->format_rect.left;
2228 lw = line_def->width;
2230 if (es->style & ES_RIGHT)
2231 x = w - (lw - x);
2232 else if (es->style & ES_CENTER)
2233 x += (w - lw) / 2;
2235 x += es->format_rect.left;
2238 if (rev)
2240 li = EDIT_EM_LineIndex(es, line);
2241 ll = EDIT_EM_LineLength(es, li);
2242 s = min(es->selection_start, es->selection_end);
2243 e = max(es->selection_start, es->selection_end);
2244 s = min(li + ll, max(li, s));
2245 e = min(li + ll, max(li, e));
2248 if (ssa)
2249 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2250 else if (rev && (s != e) &&
2251 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2252 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2253 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2254 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2255 } else
2256 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2260 /*********************************************************************
2262 * EDIT_AdjustFormatRect
2264 * Adjusts the format rectangle for the current font and the
2265 * current client rectangle.
2268 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2270 RECT ClientRect;
2272 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2273 if (es->style & ES_MULTILINE)
2275 INT fw, vlc, max_x_offset, max_y_offset;
2277 vlc = get_vertical_line_count(es);
2278 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2280 /* correct es->x_offset */
2281 fw = es->format_rect.right - es->format_rect.left;
2282 max_x_offset = es->text_width - fw;
2283 if(max_x_offset < 0) max_x_offset = 0;
2284 if(es->x_offset > max_x_offset)
2285 es->x_offset = max_x_offset;
2287 /* correct es->y_offset */
2288 max_y_offset = es->line_count - vlc;
2289 if(max_y_offset < 0) max_y_offset = 0;
2290 if(es->y_offset > max_y_offset)
2291 es->y_offset = max_y_offset;
2293 /* force scroll info update */
2294 EDIT_UpdateScrollInfo(es);
2296 else
2297 /* Windows doesn't care to fix text placement for SL controls */
2298 es->format_rect.bottom = es->format_rect.top + es->line_height;
2300 /* Always stay within the client area */
2301 GetClientRect(es->hwndSelf, &ClientRect);
2302 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2304 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2305 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2307 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2311 /*********************************************************************
2313 * EDIT_SetRectNP
2315 * note: this is not (exactly) the handler called on EM_SETRECTNP
2316 * it is also used to set the rect of a single line control
2319 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2321 LONG_PTR ExStyle;
2322 INT bw, bh;
2323 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2325 CopyRect(&es->format_rect, rc);
2327 if (ExStyle & WS_EX_CLIENTEDGE) {
2328 es->format_rect.left++;
2329 es->format_rect.right--;
2331 if (es->format_rect.bottom - es->format_rect.top
2332 >= es->line_height + 2)
2334 es->format_rect.top++;
2335 es->format_rect.bottom--;
2338 else if (es->style & WS_BORDER) {
2339 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2340 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2341 InflateRect(&es->format_rect, -bw, 0);
2342 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2343 InflateRect(&es->format_rect, 0, -bh);
2346 es->format_rect.left += es->left_margin;
2347 es->format_rect.right -= es->right_margin;
2348 EDIT_AdjustFormatRect(es);
2352 /*********************************************************************
2354 * EM_CHARFROMPOS
2356 * returns line number (not index) in high-order word of result.
2357 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2358 * to Richedit, not to the edit control. Original documentation is valid.
2359 * FIXME: do the specs mean to return -1 if outside client area or
2360 * if outside formatting rectangle ???
2363 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2365 POINT pt;
2366 RECT rc;
2367 INT index;
2369 pt.x = x;
2370 pt.y = y;
2371 GetClientRect(es->hwndSelf, &rc);
2372 if (!PtInRect(&rc, pt))
2373 return -1;
2375 index = EDIT_CharFromPos(es, x, y, NULL);
2376 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2380 /*********************************************************************
2382 * EM_FMTLINES
2384 * Enable or disable soft breaks.
2386 * This means: insert or remove the soft linebreak character (\r\r\n).
2387 * Take care to check if the text still fits the buffer after insertion.
2388 * If not, notify with EN_ERRSPACE.
2391 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2393 es->flags &= ~EF_USE_SOFTBRK;
2394 if (add_eol) {
2395 es->flags |= EF_USE_SOFTBRK;
2396 FIXME("soft break enabled, not implemented\n");
2398 return add_eol;
2402 /*********************************************************************
2404 * EM_GETHANDLE
2406 * Hopefully this won't fire back at us.
2407 * We always start with a fixed buffer in the local heap.
2408 * Despite of the documentation says that the local heap is used
2409 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2410 * buffer on the local heap.
2413 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2415 HLOCAL hLocal;
2417 if (!(es->style & ES_MULTILINE))
2418 return 0;
2420 if(es->is_unicode)
2421 hLocal = es->hloc32W;
2422 else
2424 if(!es->hloc32A)
2426 CHAR *textA;
2427 UINT countA, alloc_size;
2428 TRACE("Allocating 32-bit ANSI alias buffer\n");
2429 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2430 alloc_size = ROUND_TO_GROW(countA);
2431 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2433 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2434 return 0;
2436 textA = LocalLock(es->hloc32A);
2437 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2438 LocalUnlock(es->hloc32A);
2440 hLocal = es->hloc32A;
2443 EDIT_UnlockBuffer(es, TRUE);
2445 /* The text buffer handle belongs to the app */
2446 es->hlocapp = hLocal;
2448 TRACE("Returning %p, LocalSize() = %Id\n", hLocal, LocalSize(hLocal));
2449 return hLocal;
2453 /*********************************************************************
2455 * EM_GETLINE
2458 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2460 LPWSTR src;
2461 INT line_len, dst_len;
2462 INT i;
2464 if (es->style & ES_MULTILINE) {
2465 if (line >= es->line_count)
2466 return 0;
2467 } else
2468 line = 0;
2469 i = EDIT_EM_LineIndex(es, line);
2470 src = es->text + i;
2471 line_len = EDIT_EM_LineLength(es, i);
2472 dst_len = *(WORD *)dst;
2473 if(unicode)
2475 if(dst_len <= line_len)
2477 memcpy(dst, src, dst_len * sizeof(WCHAR));
2478 return dst_len;
2480 else /* Append 0 if enough space */
2482 memcpy(dst, src, line_len * sizeof(WCHAR));
2483 dst[line_len] = 0;
2484 return line_len;
2487 else
2489 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2490 if(!ret && line_len) /* Insufficient buffer size */
2491 return dst_len;
2492 if(ret < dst_len) /* Append 0 if enough space */
2493 ((LPSTR)dst)[ret] = 0;
2494 return ret;
2499 /*********************************************************************
2501 * EM_GETSEL
2504 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2506 UINT s = es->selection_start;
2507 UINT e = es->selection_end;
2509 ORDER_UINT(s, e);
2510 if (start)
2511 *start = s;
2512 if (end)
2513 *end = e;
2514 return MAKELONG(s, e);
2518 /*********************************************************************
2520 * EM_REPLACESEL
2522 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2525 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2526 BOOL send_update, BOOL honor_limit)
2528 UINT tl = get_text_length(es);
2529 UINT utl;
2530 UINT s;
2531 UINT e;
2532 UINT i;
2533 UINT size;
2534 LPWSTR p;
2535 HRGN hrgn = 0;
2536 LPWSTR buf = NULL;
2537 UINT bufl;
2539 TRACE("%s, can_undo %d, send_update %d\n",
2540 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2542 s = es->selection_start;
2543 e = es->selection_end;
2545 EDIT_InvalidateUniscribeData(es);
2546 if ((s == e) && !strl)
2547 return;
2549 ORDER_UINT(s, e);
2551 size = tl - (e - s) + strl;
2552 if (!size)
2553 es->text_width = 0;
2555 /* Issue the EN_MAXTEXT notification and continue with replacing text
2556 * so that buffer limit is honored. */
2557 if ((honor_limit) && (size > es->buffer_limit))
2559 if (!notify_parent(es, EN_MAXTEXT)) return;
2560 /* Buffer limit can be smaller than the actual length of text in combobox */
2561 if (es->buffer_limit < (tl - (e-s)))
2562 strl = 0;
2563 else
2564 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2567 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2568 return;
2570 if (e != s) {
2571 /* there is something to be deleted */
2572 TRACE("deleting stuff.\n");
2573 bufl = e - s;
2574 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2575 if (!buf) return;
2576 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2577 buf[bufl] = 0; /* ensure 0 termination */
2578 /* now delete */
2579 lstrcpyW(es->text + s, es->text + e);
2580 text_buffer_changed(es);
2582 if (strl) {
2583 /* there is an insertion */
2584 tl = get_text_length(es);
2585 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));
2586 for (p = es->text + tl ; p >= es->text + s ; p--)
2587 p[strl] = p[0];
2588 for (i = 0 , p = es->text + s ; i < strl ; i++)
2589 p[i] = lpsz_replace[i];
2590 if(es->style & ES_UPPERCASE)
2591 CharUpperBuffW(p, strl);
2592 else if(es->style & ES_LOWERCASE)
2593 CharLowerBuffW(p, strl);
2594 text_buffer_changed(es);
2596 if (es->style & ES_MULTILINE)
2598 INT st = min(es->selection_start, es->selection_end);
2599 INT vlc = get_vertical_line_count(es);
2601 hrgn = CreateRectRgn(0, 0, 0, 0);
2602 EDIT_BuildLineDefs_ML(es, st, st + strl,
2603 strl - abs(es->selection_end - es->selection_start), hrgn);
2604 /* if text is too long undo all changes */
2605 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2606 if (strl)
2607 lstrcpyW(es->text + e, es->text + e + strl);
2608 if (e != s)
2609 for (i = 0 , p = es->text ; i < e - s ; i++)
2610 p[i + s] = buf[i];
2611 text_buffer_changed(es);
2612 EDIT_BuildLineDefs_ML(es, s, e,
2613 abs(es->selection_end - es->selection_start) - strl, hrgn);
2614 strl = 0;
2615 e = s;
2616 SetRectRgn(hrgn, 0, 0, 0, 0);
2617 if (!notify_parent(es, EN_MAXTEXT)) return;
2620 else {
2621 INT fw = es->format_rect.right - es->format_rect.left;
2622 EDIT_InvalidateUniscribeData(es);
2623 EDIT_CalcLineWidth_SL(es);
2624 /* remove chars that don't fit */
2625 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2626 while ((es->text_width > fw) && s + strl >= s) {
2627 lstrcpyW(es->text + s + strl - 1, es->text + s + strl);
2628 strl--;
2629 es->text_length = -1;
2630 EDIT_InvalidateUniscribeData(es);
2631 EDIT_CalcLineWidth_SL(es);
2633 text_buffer_changed(es);
2634 if (!notify_parent(es, EN_MAXTEXT)) return;
2638 if (e != s) {
2639 if (can_undo) {
2640 utl = lstrlenW(es->undo_text);
2641 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2642 /* undo-buffer is extended to the right */
2643 EDIT_MakeUndoFit(es, utl + e - s);
2644 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2645 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2646 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2647 /* undo-buffer is extended to the left */
2648 EDIT_MakeUndoFit(es, utl + e - s);
2649 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2650 p[e - s] = p[0];
2651 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2652 p[i] = buf[i];
2653 es->undo_position = s;
2654 } else {
2655 /* new undo-buffer */
2656 EDIT_MakeUndoFit(es, e - s);
2657 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2658 es->undo_text[e - s] = 0; /* ensure 0 termination */
2659 es->undo_position = s;
2661 /* any deletion makes the old insertion-undo invalid */
2662 es->undo_insert_count = 0;
2663 } else
2664 EDIT_EM_EmptyUndoBuffer(es);
2666 if (strl) {
2667 if (can_undo) {
2668 if ((s == es->undo_position) ||
2669 ((es->undo_insert_count) &&
2670 (s == es->undo_position + es->undo_insert_count)))
2672 * insertion is new and at delete position or
2673 * an extension to either left or right
2675 es->undo_insert_count += strl;
2676 else {
2677 /* new insertion undo */
2678 es->undo_position = s;
2679 es->undo_insert_count = strl;
2680 /* new insertion makes old delete-buffer invalid */
2681 *es->undo_text = '\0';
2683 } else
2684 EDIT_EM_EmptyUndoBuffer(es);
2687 HeapFree(GetProcessHeap(), 0, buf);
2689 s += strl;
2691 /* If text has been deleted and we're right or center aligned then scroll rightward */
2692 if (es->style & (ES_RIGHT | ES_CENTER))
2694 INT delta = strl - abs(es->selection_end - es->selection_start);
2696 if (delta < 0 && es->x_offset)
2698 if (abs(delta) > es->x_offset)
2699 es->x_offset = 0;
2700 else
2701 es->x_offset += delta;
2705 EDIT_EM_SetSel(es, s, s, FALSE);
2706 es->flags |= EF_MODIFIED;
2707 if (send_update) es->flags |= EF_UPDATE;
2708 if (hrgn)
2710 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2711 DeleteObject(hrgn);
2713 else
2714 EDIT_UpdateText(es, NULL, TRUE);
2716 EDIT_EM_ScrollCaret(es);
2718 /* force scroll info update */
2719 EDIT_UpdateScrollInfo(es);
2722 if(send_update || (es->flags & EF_UPDATE))
2724 es->flags &= ~EF_UPDATE;
2725 if (!notify_parent(es, EN_CHANGE)) return;
2727 EDIT_InvalidateUniscribeData(es);
2731 /*********************************************************************
2733 * EM_SETHANDLE
2735 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2738 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2740 if (!(es->style & ES_MULTILINE))
2741 return;
2743 if (!hloc) {
2744 WARN("called with NULL handle\n");
2745 return;
2748 EDIT_UnlockBuffer(es, TRUE);
2750 if(es->is_unicode)
2752 if(es->hloc32A)
2754 LocalFree(es->hloc32A);
2755 es->hloc32A = NULL;
2757 es->hloc32W = hloc;
2759 else
2761 INT countW, countA;
2762 HLOCAL hloc32W_new;
2763 WCHAR *textW;
2764 CHAR *textA;
2766 countA = LocalSize(hloc);
2767 textA = LocalLock(hloc);
2768 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2769 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2771 ERR("Could not allocate new unicode buffer\n");
2772 return;
2774 textW = LocalLock(hloc32W_new);
2775 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2776 LocalUnlock(hloc32W_new);
2777 LocalUnlock(hloc);
2779 if(es->hloc32W)
2780 LocalFree(es->hloc32W);
2782 es->hloc32W = hloc32W_new;
2783 es->hloc32A = hloc;
2786 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2788 /* The text buffer handle belongs to the control */
2789 es->hlocapp = NULL;
2791 EDIT_LockBuffer(es);
2792 text_buffer_changed(es);
2794 es->x_offset = es->y_offset = 0;
2795 es->selection_start = es->selection_end = 0;
2796 EDIT_EM_EmptyUndoBuffer(es);
2797 es->flags &= ~EF_MODIFIED;
2798 es->flags &= ~EF_UPDATE;
2799 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2800 EDIT_UpdateText(es, NULL, TRUE);
2801 EDIT_EM_ScrollCaret(es);
2802 /* force scroll info update */
2803 EDIT_UpdateScrollInfo(es);
2807 /*********************************************************************
2809 * EM_SETLIMITTEXT
2811 * NOTE: this version currently implements WinNT limits
2814 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2816 if (!limit) limit = ~0u;
2817 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2818 es->buffer_limit = limit;
2821 static BOOL is_cjk_charset(HDC dc)
2823 switch (GdiGetCodePage(dc)) {
2824 case 932: case 936: case 949: case 950: case 1361:
2825 return TRUE;
2826 default:
2827 return FALSE;
2831 static BOOL is_cjk_font(HDC dc)
2833 const DWORD FS_DBCS_MASK = FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB;
2834 FONTSIGNATURE fs;
2835 return (GetTextCharsetInfo(dc, &fs, 0) != DEFAULT_CHARSET &&
2836 (fs.fsCsb[0] & FS_DBCS_MASK));
2839 static int get_cjk_fontinfo_margin(int width, int side_bearing)
2841 int margin;
2842 if (side_bearing < 0)
2843 margin = min(-side_bearing, width/2);
2844 else
2845 margin = 0;
2846 return margin;
2849 struct char_width_info {
2850 INT min_lsb, min_rsb, unknown;
2853 /* Undocumented gdi32 export */
2854 extern BOOL WINAPI GetCharWidthInfo(HDC, struct char_width_info *);
2856 /*********************************************************************
2858 * EM_SETMARGINS
2860 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2861 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2862 * margin according to the textmetrics of the current font.
2864 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2865 * equal to or larger than a certain size. The empty client rect is treated as
2866 * 80 pixels width.
2868 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2869 WORD left, WORD right, BOOL repaint)
2871 TEXTMETRICW tm;
2872 INT default_left_margin = 0; /* in pixels */
2873 INT default_right_margin = 0; /* in pixels */
2875 /* Set the default margins depending on the font */
2876 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2877 HDC dc = NtUserGetDC(es->hwndSelf);
2878 HFONT old_font = SelectObject(dc, es->font);
2879 LONG width = GdiGetCharDimensions(dc, &tm, NULL), rc_width;
2880 RECT rc;
2882 /* The default margins are only non zero for TrueType or Vector fonts */
2883 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2884 struct char_width_info width_info;
2886 if ((is_cjk_charset(dc) || is_cjk_font(dc)) &&
2887 GetCharWidthInfo(dc, &width_info))
2889 default_left_margin = get_cjk_fontinfo_margin(width, width_info.min_lsb);
2890 default_right_margin = get_cjk_fontinfo_margin(width, width_info.min_rsb);
2892 else
2894 default_left_margin = width / 2;
2895 default_right_margin = width / 2;
2898 GetClientRect(es->hwndSelf, &rc);
2899 rc_width = !IsRectEmpty(&rc) ? rc.right - rc.left : 80;
2900 if (rc_width < default_left_margin + default_right_margin + width * 2) {
2901 default_left_margin = es->left_margin;
2902 default_right_margin = es->right_margin;
2905 SelectObject(dc, old_font);
2906 NtUserReleaseDC( es->hwndSelf, dc );
2909 if (action & EC_LEFTMARGIN) {
2910 es->format_rect.left -= es->left_margin;
2911 if (left != EC_USEFONTINFO)
2912 es->left_margin = left;
2913 else
2914 es->left_margin = default_left_margin;
2915 es->format_rect.left += es->left_margin;
2918 if (action & EC_RIGHTMARGIN) {
2919 es->format_rect.right += es->right_margin;
2920 if (right != EC_USEFONTINFO)
2921 es->right_margin = right;
2922 else
2923 es->right_margin = default_right_margin;
2924 es->format_rect.right -= es->right_margin;
2927 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2928 EDIT_AdjustFormatRect(es);
2929 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2932 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2936 /*********************************************************************
2938 * EM_SETPASSWORDCHAR
2941 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2943 LONG style;
2945 if (es->style & ES_MULTILINE)
2946 return;
2948 if (es->password_char == c)
2949 return;
2951 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2952 es->password_char = c;
2953 if (c) {
2954 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2955 es->style |= ES_PASSWORD;
2956 } else {
2957 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2958 es->style &= ~ES_PASSWORD;
2960 EDIT_InvalidateUniscribeData(es);
2961 EDIT_UpdateText(es, NULL, TRUE);
2965 /*********************************************************************
2967 * EM_SETTABSTOPS
2970 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2972 if (!(es->style & ES_MULTILINE))
2973 return FALSE;
2974 HeapFree(GetProcessHeap(), 0, es->tabs);
2975 es->tabs_count = count;
2976 if (!count)
2977 es->tabs = NULL;
2978 else {
2979 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2980 memcpy(es->tabs, tabs, count * sizeof(INT));
2982 EDIT_InvalidateUniscribeData(es);
2983 return TRUE;
2987 /*********************************************************************
2989 * EM_SETWORDBREAKPROC
2992 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
2994 if (es->word_break_proc == wbp)
2995 return;
2997 es->word_break_proc = wbp;
2999 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3000 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3001 EDIT_UpdateText(es, NULL, TRUE);
3006 /*********************************************************************
3008 * EM_UNDO / WM_UNDO
3011 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3013 INT ulength;
3014 LPWSTR utext;
3016 /* As per MSDN spec, for a single-line edit control,
3017 the return value is always TRUE */
3018 if( es->style & ES_READONLY )
3019 return !(es->style & ES_MULTILINE);
3021 ulength = lstrlenW(es->undo_text);
3023 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3025 lstrcpyW(utext, es->undo_text);
3027 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3028 es->undo_insert_count, debugstr_w(utext));
3030 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3031 EDIT_EM_EmptyUndoBuffer(es);
3032 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
3033 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3034 /* send the notification after the selection start and end are set */
3035 if (!notify_parent(es, EN_CHANGE)) return TRUE;
3036 EDIT_EM_ScrollCaret(es);
3037 HeapFree(GetProcessHeap(), 0, utext);
3039 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3040 es->undo_insert_count, debugstr_w(es->undo_text));
3041 return TRUE;
3045 /* Helper function for WM_CHAR
3047 * According to an MSDN blog article titled "Just because you're a control
3048 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3049 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3050 * a dialog box or not.
3052 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3054 return (es->flags & EF_DIALOGMODE);
3058 /*********************************************************************
3060 * WM_PASTE
3063 static void EDIT_WM_Paste(EDITSTATE *es)
3065 HGLOBAL hsrc;
3066 LPWSTR src, ptr;
3067 int len;
3069 /* Protect read-only edit control from modification */
3070 if(es->style & ES_READONLY)
3071 return;
3073 OpenClipboard(es->hwndSelf);
3074 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3075 src = GlobalLock(hsrc);
3076 len = lstrlenW(src);
3077 /* Protect single-line edit against pasting new line character */
3078 if (!(es->style & ES_MULTILINE) && ((ptr = wcschr(src, '\n')))) {
3079 len = ptr - src;
3080 if (len && src[len - 1] == '\r')
3081 --len;
3083 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
3084 GlobalUnlock(hsrc);
3086 else if (es->style & ES_PASSWORD) {
3087 /* clear selected text in password edit box even with empty clipboard */
3088 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3090 NtUserCloseClipboard();
3094 /*********************************************************************
3096 * WM_COPY
3099 static void EDIT_WM_Copy(EDITSTATE *es)
3101 INT s = min(es->selection_start, es->selection_end);
3102 INT e = max(es->selection_start, es->selection_end);
3103 HGLOBAL hdst;
3104 LPWSTR dst;
3105 DWORD len;
3107 if (e == s) return;
3109 len = e - s;
3110 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3111 dst = GlobalLock(hdst);
3112 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3113 dst[len] = 0; /* ensure 0 termination */
3114 TRACE("%s\n", debugstr_w(dst));
3115 GlobalUnlock(hdst);
3116 OpenClipboard(es->hwndSelf);
3117 NtUserEmptyClipboard();
3118 SetClipboardData(CF_UNICODETEXT, hdst);
3119 NtUserCloseClipboard();
3123 /*********************************************************************
3125 * WM_CLEAR
3128 static inline void EDIT_WM_Clear(EDITSTATE *es)
3130 /* Protect read-only edit control from modification */
3131 if(es->style & ES_READONLY)
3132 return;
3134 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
3138 /*********************************************************************
3140 * WM_CUT
3143 static inline void EDIT_WM_Cut(EDITSTATE *es)
3145 EDIT_WM_Copy(es);
3146 EDIT_WM_Clear(es);
3150 /*********************************************************************
3152 * WM_CHAR
3155 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3157 BOOL control;
3159 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3161 switch (c) {
3162 case '\r':
3163 /* If it's not a multiline edit box, it would be ignored below.
3164 * For multiline edit without ES_WANTRETURN, we have to make a
3165 * special case.
3167 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3168 if (EDIT_IsInsideDialog(es))
3169 break;
3170 case '\n':
3171 if (es->style & ES_MULTILINE) {
3172 if (es->style & ES_READONLY) {
3173 EDIT_MoveHome(es, FALSE, FALSE);
3174 EDIT_MoveDown_ML(es, FALSE);
3175 } else {
3176 EDIT_EM_ReplaceSel(es, TRUE, L"\r\n", 2, TRUE, TRUE);
3179 break;
3180 case '\t':
3181 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3183 if (EDIT_IsInsideDialog(es))
3184 break;
3185 EDIT_EM_ReplaceSel(es, TRUE, L"\t", 1, TRUE, TRUE);
3187 break;
3188 case VK_BACK:
3189 if (!(es->style & ES_READONLY) && !control) {
3190 if (es->selection_start != es->selection_end)
3191 EDIT_WM_Clear(es);
3192 else {
3193 /* delete character left of caret */
3194 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3195 EDIT_MoveBackward(es, TRUE);
3196 EDIT_WM_Clear(es);
3199 break;
3200 case 0x03: /* ^C */
3201 if (!(es->style & ES_PASSWORD))
3202 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3203 break;
3204 case 0x16: /* ^V */
3205 if (!(es->style & ES_READONLY))
3206 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3207 break;
3208 case 0x18: /* ^X */
3209 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3210 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3211 break;
3212 case 0x1A: /* ^Z */
3213 if (!(es->style & ES_READONLY))
3214 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3215 break;
3217 default:
3218 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3219 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3220 break;
3222 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3223 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3224 break;
3226 return 1;
3230 /*********************************************************************
3232 * EDIT_ContextMenuCommand
3235 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3237 switch (id) {
3238 case EM_UNDO:
3239 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3240 break;
3241 case WM_CUT:
3242 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3243 break;
3244 case WM_COPY:
3245 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3246 break;
3247 case WM_PASTE:
3248 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3249 break;
3250 case WM_CLEAR:
3251 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3252 break;
3253 case EM_SETSEL:
3254 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3255 break;
3256 default:
3257 ERR("unknown menu item, please report\n");
3258 break;
3263 /*********************************************************************
3265 * WM_CONTEXTMENU
3267 * Note: the resource files resource/sysres_??.rc cannot define a
3268 * single popup menu. Hence we use a (dummy) menubar
3269 * containing the single popup menu as its first item.
3271 * FIXME: the message identifiers have been chosen arbitrarily,
3272 * hence we use MF_BYPOSITION.
3273 * We might as well use the "real" values (anybody knows ?)
3274 * The menu definition is in resources/sysres_??.rc.
3275 * Once these are OK, we better use MF_BYCOMMAND here
3276 * (as we do in EDIT_WM_Command()).
3279 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3281 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3282 HMENU popup = GetSubMenu(menu, 0);
3283 UINT start = es->selection_start;
3284 UINT end = es->selection_end;
3285 BOOL enabled;
3286 UINT cmd;
3288 ORDER_UINT(start, end);
3290 /* undo */
3291 enabled = EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY);
3292 NtUserEnableMenuItem( popup, 0, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3293 /* cut */
3294 enabled = (end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY);
3295 NtUserEnableMenuItem( popup, 2, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3296 /* copy */
3297 enabled = (end - start) && !(es->style & ES_PASSWORD);
3298 NtUserEnableMenuItem( popup, 3, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3299 /* paste */
3300 enabled = NtUserIsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY);
3301 NtUserEnableMenuItem( popup, 4, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3302 /* delete */
3303 enabled = (end - start) && !(es->style & ES_READONLY);
3304 NtUserEnableMenuItem( popup, 5, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3305 /* select all */
3306 enabled = start || end != get_text_length(es);
3307 NtUserEnableMenuItem( popup, 7, MF_BYPOSITION | (enabled ? MF_ENABLED : MF_GRAYED) );
3309 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3311 POINT pt;
3312 RECT rc;
3314 /* Windows places the menu at the edit's center in this case */
3315 GetClientRect( es->hwndSelf, &rc );
3316 pt.x = rc.right / 2;
3317 pt.y = rc.bottom / 2;
3318 ClientToScreen( es->hwndSelf, &pt );
3319 x = pt.x;
3320 y = pt.y;
3323 if (!(es->flags & EF_FOCUSED))
3324 NtUserSetFocus(es->hwndSelf);
3326 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3327 x, y, 0, es->hwndSelf, NULL);
3329 if (cmd)
3330 EDIT_ContextMenuCommand(es, cmd);
3332 NtUserDestroyMenu(menu);
3336 /*********************************************************************
3338 * WM_GETTEXT
3341 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3343 if(!count) return 0;
3345 if(unicode)
3347 lstrcpynW(dst, es->text, count);
3348 return lstrlenW(dst);
3350 else
3352 LPSTR textA = (LPSTR)dst;
3353 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3354 textA[count - 1] = 0; /* ensure 0 termination */
3355 return strlen(textA);
3359 /*********************************************************************
3361 * EDIT_CheckCombo
3364 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3366 HWND hLBox = es->hwndListBox;
3367 HWND hCombo;
3368 BOOL bDropped;
3369 int nEUI;
3371 if (!hLBox)
3372 return FALSE;
3374 hCombo = GetParent(es->hwndSelf);
3375 bDropped = TRUE;
3376 nEUI = 0;
3378 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3380 if (key == VK_UP || key == VK_DOWN)
3382 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3383 nEUI = 1;
3385 if (msg == WM_KEYDOWN || nEUI)
3386 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3389 switch (msg)
3391 case WM_KEYDOWN:
3392 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3394 /* make sure ComboLBox pops up */
3395 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3396 key = VK_F4;
3397 nEUI = 2;
3400 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3401 break;
3403 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3404 if (nEUI)
3405 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3406 else
3407 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3408 break;
3411 if(nEUI == 2)
3412 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3414 return TRUE;
3418 /*********************************************************************
3420 * WM_KEYDOWN
3422 * Handling of special keys that don't produce a WM_CHAR
3423 * (i.e. non-printable keys) & Backspace & Delete
3426 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3428 BOOL shift;
3429 BOOL control;
3431 if (NtUserGetKeyState(VK_MENU) & 0x8000)
3432 return 0;
3434 shift = NtUserGetKeyState(VK_SHIFT) & 0x8000;
3435 control = NtUserGetKeyState(VK_CONTROL) & 0x8000;
3437 switch (key) {
3438 case VK_F4:
3439 case VK_UP:
3440 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3441 break;
3443 /* fall through */
3444 case VK_LEFT:
3445 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3446 EDIT_MoveUp_ML(es, shift);
3447 else
3448 if (control)
3449 EDIT_MoveWordBackward(es, shift);
3450 else
3451 EDIT_MoveBackward(es, shift);
3452 break;
3453 case VK_DOWN:
3454 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3455 break;
3456 /* fall through */
3457 case VK_RIGHT:
3458 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3459 EDIT_MoveDown_ML(es, shift);
3460 else if (control)
3461 EDIT_MoveWordForward(es, shift);
3462 else
3463 EDIT_MoveForward(es, shift);
3464 break;
3465 case VK_HOME:
3466 EDIT_MoveHome(es, shift, control);
3467 break;
3468 case VK_END:
3469 EDIT_MoveEnd(es, shift, control);
3470 break;
3471 case VK_PRIOR:
3472 if (es->style & ES_MULTILINE)
3473 EDIT_MovePageUp_ML(es, shift);
3474 else
3475 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3476 break;
3477 case VK_NEXT:
3478 if (es->style & ES_MULTILINE)
3479 EDIT_MovePageDown_ML(es, shift);
3480 else
3481 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3482 break;
3483 case VK_DELETE:
3484 if (!(es->style & ES_READONLY) && !(shift && control)) {
3485 if (es->selection_start != es->selection_end) {
3486 if (shift)
3487 EDIT_WM_Cut(es);
3488 else
3489 EDIT_WM_Clear(es);
3490 } else {
3491 EDIT_EM_SetSel(es, ~0u, 0, FALSE);
3492 if (shift)
3493 /* delete character left of caret */
3494 EDIT_MoveBackward(es, TRUE);
3495 else if (control)
3496 /* delete to end of line */
3497 EDIT_MoveEnd(es, TRUE, FALSE);
3498 else
3499 /* delete character right of caret */
3500 EDIT_MoveForward(es, TRUE);
3501 EDIT_WM_Clear(es);
3504 break;
3505 case VK_INSERT:
3506 if (shift) {
3507 if (!(es->style & ES_READONLY))
3508 EDIT_WM_Paste(es);
3509 } else if (control)
3510 EDIT_WM_Copy(es);
3511 break;
3512 case VK_RETURN:
3513 /* If the edit doesn't want the return send a message to the default object */
3514 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3516 DWORD dw;
3518 if (!EDIT_IsInsideDialog(es)) break;
3519 if (control) break;
3520 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3521 if (HIWORD(dw) == DC_HASDEFID)
3523 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3524 if (hwDefCtrl)
3526 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3527 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3531 break;
3532 case VK_ESCAPE:
3533 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3534 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3535 break;
3536 case VK_TAB:
3537 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3538 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3539 break;
3541 return TRUE;
3545 /*********************************************************************
3547 * WM_KILLFOCUS
3550 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3552 es->flags &= ~EF_FOCUSED;
3553 DestroyCaret();
3554 if(!(es->style & ES_NOHIDESEL))
3555 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3556 if (!notify_parent(es, EN_KILLFOCUS)) return 0;
3557 /* throw away left over scroll when we lose focus */
3558 es->wheelDeltaRemainder = 0;
3559 return 0;
3563 /*********************************************************************
3565 * WM_LBUTTONDBLCLK
3567 * The caret position has been set on the WM_LBUTTONDOWN message
3570 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3572 INT s;
3573 INT e = es->selection_end;
3574 INT l;
3575 INT li;
3576 INT ll;
3578 es->bCaptureState = TRUE;
3579 NtUserSetCapture(es->hwndSelf);
3581 l = EDIT_EM_LineFromChar(es, e);
3582 li = EDIT_EM_LineIndex(es, l);
3583 ll = EDIT_EM_LineLength(es, e);
3584 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3585 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3586 EDIT_EM_SetSel(es, s, e, FALSE);
3587 EDIT_EM_ScrollCaret(es);
3588 return 0;
3592 /*********************************************************************
3594 * WM_LBUTTONDOWN
3597 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3599 INT e;
3600 BOOL after_wrap;
3602 es->bCaptureState = TRUE;
3603 NtUserSetCapture(es->hwndSelf);
3604 EDIT_ConfinePoint(es, &x, &y);
3605 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3606 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3607 EDIT_EM_ScrollCaret(es);
3609 if (!(es->flags & EF_FOCUSED))
3610 NtUserSetFocus(es->hwndSelf);
3612 return 0;
3616 /*********************************************************************
3618 * WM_LBUTTONUP
3621 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3623 if (es->bCaptureState) {
3624 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3626 es->bCaptureState = FALSE;
3627 return 0;
3631 /*********************************************************************
3633 * WM_MBUTTONDOWN
3636 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3638 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3639 return 0;
3643 /*********************************************************************
3645 * WM_MOUSEMOVE
3648 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3650 INT e;
3651 BOOL after_wrap;
3653 /* If the mouse has been captured by process other than the edit control itself,
3654 * the windows edit controls will not select the strings with mouse move.
3656 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3657 return 0;
3659 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3660 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3661 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3662 EDIT_EM_ScrollCaret(es);
3663 return 0;
3667 /*********************************************************************
3669 * WM_PAINT
3672 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3674 PAINTSTRUCT ps;
3675 INT i;
3676 HDC dc;
3677 HFONT old_font = 0;
3678 RECT rc;
3679 RECT rcClient;
3680 RECT rcLine;
3681 RECT rcRgn;
3682 HBRUSH brush;
3683 HBRUSH old_brush;
3684 INT bw, bh;
3685 BOOL rev = es->bEnableState &&
3686 ((es->flags & EF_FOCUSED) ||
3687 (es->style & ES_NOHIDESEL));
3688 dc = hdc ? hdc : NtUserBeginPaint( es->hwndSelf, &ps );
3690 /* The dc we use for calculating may not be the one we paint into.
3691 This is the safest action. */
3692 EDIT_InvalidateUniscribeData(es);
3693 GetClientRect(es->hwndSelf, &rcClient);
3695 /* get the background brush */
3696 brush = EDIT_NotifyCtlColor(es, dc);
3698 /* paint the border and the background */
3699 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3701 if(es->style & WS_BORDER) {
3702 bw = GetSystemMetrics(SM_CXBORDER);
3703 bh = GetSystemMetrics(SM_CYBORDER);
3704 rc = rcClient;
3705 if(es->style & ES_MULTILINE) {
3706 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3707 if(es->style & WS_VSCROLL) rc.right+=bw;
3710 /* Draw the frame. Same code as in nonclient.c */
3711 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3712 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3713 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3714 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3715 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3716 SelectObject(dc, old_brush);
3718 /* Keep the border clean */
3719 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3720 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3723 GetClipBox(dc, &rc);
3724 FillRect(dc, &rc, brush);
3726 IntersectClipRect(dc, es->format_rect.left,
3727 es->format_rect.top,
3728 es->format_rect.right,
3729 es->format_rect.bottom);
3730 if (es->style & ES_MULTILINE) {
3731 rc = rcClient;
3732 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3734 if (es->font)
3735 old_font = SelectObject(dc, es->font);
3737 if (!es->bEnableState)
3738 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3739 GetClipBox(dc, &rcRgn);
3740 if (es->style & ES_MULTILINE) {
3741 INT vlc = get_vertical_line_count(es);
3742 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3743 EDIT_UpdateUniscribeData(es, dc, i);
3744 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3745 if (IntersectRect(&rc, &rcRgn, &rcLine))
3746 EDIT_PaintLine(es, dc, i, rev);
3748 } else {
3749 EDIT_UpdateUniscribeData(es, dc, 0);
3750 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3751 if (IntersectRect(&rc, &rcRgn, &rcLine))
3752 EDIT_PaintLine(es, dc, 0, rev);
3754 if (es->font)
3755 SelectObject(dc, old_font);
3757 if (!hdc)
3758 NtUserEndPaint( es->hwndSelf, &ps );
3762 /*********************************************************************
3764 * WM_SETFOCUS
3767 static void EDIT_WM_SetFocus(EDITSTATE *es)
3769 es->flags |= EF_FOCUSED;
3771 if (!(es->style & ES_NOHIDESEL))
3772 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3774 /* single line edit updates itself */
3775 if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3777 HDC hdc = NtUserGetDC(es->hwndSelf);
3778 EDIT_WM_Paint(es, hdc);
3779 NtUserReleaseDC( es->hwndSelf, hdc );
3782 NtUserCreateCaret( es->hwndSelf, 0, 1, es->line_height );
3783 EDIT_SetCaretPos(es, es->selection_end,
3784 es->flags & EF_AFTER_WRAP);
3785 NtUserShowCaret( es->hwndSelf );
3786 notify_parent(es, EN_SETFOCUS);
3789 static DWORD get_font_margins(HDC hdc, const TEXTMETRICW *tm, BOOL unicode)
3791 ABC abc[256];
3792 SHORT left, right;
3793 UINT i;
3795 if (!(tm->tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE)))
3796 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3798 if (unicode) {
3799 if (!is_cjk_charset(hdc) && !is_cjk_font(hdc))
3800 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3801 if (!GetCharABCWidthsW(hdc, 0, 255, abc))
3802 return 0;
3804 else if (is_cjk_charset(hdc)) {
3805 if (!GetCharABCWidthsA(hdc, 0, 255, abc))
3806 return 0;
3808 else
3809 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3811 left = right = 0;
3812 for (i = 0; i < ARRAY_SIZE(abc); i++) {
3813 if (-abc[i].abcA > right) right = -abc[i].abcA;
3814 if (-abc[i].abcC > left ) left = -abc[i].abcC;
3816 return MAKELONG(left, right);
3819 static void EDIT_UpdateImmCompositionFont(EDITSTATE *es)
3821 LOGFONTW composition_font;
3822 HIMC himc = ImmGetContext(es->hwndSelf);
3823 GetObjectW(es->font, sizeof(LOGFONTW), &composition_font);
3824 ImmSetCompositionFontW(himc, &composition_font);
3825 ImmReleaseContext(es->hwndSelf, himc);
3828 /*********************************************************************
3830 * WM_SETFONT
3832 * With Win95 look the margins are set to default font value unless
3833 * the system font (font == 0) is being set, in which case they are left
3834 * unchanged.
3837 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3839 TEXTMETRICW tm;
3840 HDC dc;
3841 HFONT old_font = 0;
3842 RECT clientRect;
3843 DWORD margins;
3845 es->font = font;
3846 EDIT_InvalidateUniscribeData(es);
3847 dc = NtUserGetDC(es->hwndSelf);
3848 if (font)
3849 old_font = SelectObject(dc, font);
3850 GetTextMetricsW(dc, &tm);
3851 es->line_height = tm.tmHeight;
3852 es->char_width = tm.tmAveCharWidth;
3853 margins = get_font_margins(dc, &tm, es->is_unicode);
3854 if (font)
3855 SelectObject(dc, old_font);
3856 NtUserReleaseDC( es->hwndSelf, dc );
3858 /* Reset the format rect and the margins */
3859 GetClientRect(es->hwndSelf, &clientRect);
3860 EDIT_SetRectNP(es, &clientRect);
3861 if (margins)
3862 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3863 LOWORD(margins), HIWORD(margins), FALSE);
3865 if (es->style & ES_MULTILINE)
3866 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3867 else
3868 EDIT_CalcLineWidth_SL(es);
3870 if (redraw)
3871 EDIT_UpdateText(es, NULL, TRUE);
3872 if (es->flags & EF_FOCUSED) {
3873 DestroyCaret();
3874 NtUserCreateCaret( es->hwndSelf, 0, 1, es->line_height );
3875 EDIT_SetCaretPos(es, es->selection_end,
3876 es->flags & EF_AFTER_WRAP);
3877 NtUserShowCaret( es->hwndSelf );
3880 EDIT_UpdateImmCompositionFont(es);
3884 /*********************************************************************
3886 * WM_SETTEXT
3888 * NOTES
3889 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3890 * The modified flag is reset. No notifications are sent.
3892 * For single-line controls, reception of WM_SETTEXT triggers:
3893 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3896 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3898 LPWSTR textW = NULL;
3899 if (!unicode && text)
3901 LPCSTR textA = (LPCSTR)text;
3902 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3903 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3904 if (textW)
3905 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3906 text = textW;
3909 if (es->flags & EF_UPDATE)
3910 /* fixed this bug once; complain if we see it about to happen again. */
3911 ERR("SetSel may generate UPDATE message whose handler may reset "
3912 "selection.\n");
3914 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3915 if (text)
3917 TRACE("%s\n", debugstr_w(text));
3918 EDIT_EM_ReplaceSel(es, FALSE, text, lstrlenW(text), FALSE, FALSE);
3919 if(!unicode)
3920 HeapFree(GetProcessHeap(), 0, textW);
3922 else
3924 TRACE("<NULL>\n");
3925 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3927 es->x_offset = 0;
3928 es->flags &= ~EF_MODIFIED;
3929 EDIT_EM_SetSel(es, 0, 0, FALSE);
3931 /* Send the notification after the selection start and end have been set
3932 * edit control doesn't send notification on WM_SETTEXT
3933 * if it is multiline, or it is part of combobox
3935 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3937 if (!notify_parent(es, EN_UPDATE)) return;
3938 if (!notify_parent(es, EN_CHANGE)) return;
3940 EDIT_EM_ScrollCaret(es);
3941 EDIT_UpdateScrollInfo(es);
3942 EDIT_InvalidateUniscribeData(es);
3946 /*********************************************************************
3948 * WM_SIZE
3951 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3953 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3954 RECT rc;
3955 GetClientRect(es->hwndSelf, &rc);
3956 EDIT_SetRectNP(es, &rc);
3957 EDIT_UpdateText(es, NULL, TRUE);
3962 /*********************************************************************
3964 * WM_STYLECHANGED
3966 * This message is sent by SetWindowLong on having changed either the Style
3967 * or the extended style.
3969 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3971 * See also EDIT_WM_NCCreate
3973 * It appears that the Windows version of the edit control allows the style
3974 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3975 * style variable which will generally be different. In this function we
3976 * update the internal style based on what changed in the externally visible
3977 * style.
3979 * Much of this content as based upon the MSDN, especially:
3980 * Platform SDK Documentation -> User Interface Services ->
3981 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3982 * Edit Control Styles
3984 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3986 if (GWL_STYLE == which) {
3987 DWORD style_change_mask;
3988 DWORD new_style;
3989 /* Only a subset of changes can be applied after the control
3990 * has been created.
3992 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3993 ES_NUMBER;
3994 if (es->style & ES_MULTILINE)
3995 style_change_mask |= ES_WANTRETURN;
3997 new_style = style->styleNew & style_change_mask;
3999 /* Number overrides lowercase overrides uppercase (at least it
4000 * does in Win95). However I'll bet that ES_NUMBER would be
4001 * invalid under Win 3.1.
4003 if (new_style & ES_NUMBER) {
4004 ; /* do not override the ES_NUMBER */
4005 } else if (new_style & ES_LOWERCASE) {
4006 new_style &= ~ES_UPPERCASE;
4009 es->style = (es->style & ~style_change_mask) | new_style;
4010 } else if (GWL_EXSTYLE == which) {
4011 ; /* FIXME - what is needed here */
4012 } else {
4013 WARN ("Invalid style change %Id\n",which);
4016 return 0;
4019 /*********************************************************************
4021 * WM_SYSKEYDOWN
4024 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4026 if ((key == VK_BACK) && (key_data & 0x2000)) {
4027 if (EDIT_EM_CanUndo(es))
4028 EDIT_EM_Undo(es);
4029 return 0;
4030 } else if (key == VK_UP || key == VK_DOWN) {
4031 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4032 return 0;
4034 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4037 /*********************************************************************
4039 * WM_HSCROLL
4042 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4044 INT dx;
4045 INT fw;
4047 if (!(es->style & ES_MULTILINE))
4048 return 0;
4050 if (!(es->style & ES_AUTOHSCROLL))
4051 return 0;
4053 dx = 0;
4054 fw = es->format_rect.right - es->format_rect.left;
4055 switch (action) {
4056 case SB_LINELEFT:
4057 TRACE("SB_LINELEFT\n");
4058 if (es->x_offset)
4059 dx = -es->char_width;
4060 break;
4061 case SB_LINERIGHT:
4062 TRACE("SB_LINERIGHT\n");
4063 if (es->x_offset < es->text_width)
4064 dx = es->char_width;
4065 break;
4066 case SB_PAGELEFT:
4067 TRACE("SB_PAGELEFT\n");
4068 if (es->x_offset)
4069 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4070 break;
4071 case SB_PAGERIGHT:
4072 TRACE("SB_PAGERIGHT\n");
4073 if (es->x_offset < es->text_width)
4074 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4075 break;
4076 case SB_LEFT:
4077 TRACE("SB_LEFT\n");
4078 if (es->x_offset)
4079 dx = -es->x_offset;
4080 break;
4081 case SB_RIGHT:
4082 TRACE("SB_RIGHT\n");
4083 if (es->x_offset < es->text_width)
4084 dx = es->text_width - es->x_offset;
4085 break;
4086 case SB_THUMBTRACK:
4087 TRACE("SB_THUMBTRACK %d\n", pos);
4088 es->flags |= EF_HSCROLL_TRACK;
4089 if(es->style & WS_HSCROLL)
4090 dx = pos - es->x_offset;
4091 else
4093 INT fw, new_x;
4094 /* Sanity check */
4095 if(pos < 0 || pos > 100) return 0;
4096 /* Assume default scroll range 0-100 */
4097 fw = es->format_rect.right - es->format_rect.left;
4098 new_x = pos * (es->text_width - fw) / 100;
4099 dx = es->text_width ? (new_x - es->x_offset) : 0;
4101 break;
4102 case SB_THUMBPOSITION:
4103 TRACE("SB_THUMBPOSITION %d\n", pos);
4104 es->flags &= ~EF_HSCROLL_TRACK;
4105 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4106 dx = pos - es->x_offset;
4107 else
4109 INT fw, new_x;
4110 /* Sanity check */
4111 if(pos < 0 || pos > 100) return 0;
4112 /* Assume default scroll range 0-100 */
4113 fw = es->format_rect.right - es->format_rect.left;
4114 new_x = pos * (es->text_width - fw) / 100;
4115 dx = es->text_width ? (new_x - es->x_offset) : 0;
4117 if (!dx) {
4118 /* force scroll info update */
4119 EDIT_UpdateScrollInfo(es);
4120 notify_parent(es, EN_HSCROLL);
4122 break;
4123 case SB_ENDSCROLL:
4124 TRACE("SB_ENDSCROLL\n");
4125 break;
4127 * FIXME : the next two are undocumented !
4128 * Are we doing the right thing ?
4129 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4130 * although it's also a regular control message.
4132 case EM_GETTHUMB: /* this one is used by NT notepad */
4134 LRESULT ret;
4135 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4136 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4137 else
4139 /* Assume default scroll range 0-100 */
4140 INT fw = es->format_rect.right - es->format_rect.left;
4141 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4143 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4144 return ret;
4146 case EM_LINESCROLL:
4147 TRACE("EM_LINESCROLL16\n");
4148 dx = pos;
4149 break;
4151 default:
4152 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4153 action, action);
4154 return 0;
4156 if (dx)
4158 INT fw = es->format_rect.right - es->format_rect.left;
4159 /* check if we are going to move too far */
4160 if(es->x_offset + dx + fw > es->text_width)
4161 dx = es->text_width - fw - es->x_offset;
4162 if(dx)
4163 EDIT_EM_LineScroll_internal(es, dx, 0);
4165 return 0;
4169 /*********************************************************************
4171 * WM_VSCROLL
4174 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4176 INT dy;
4178 if (!(es->style & ES_MULTILINE))
4179 return 0;
4181 if (!(es->style & ES_AUTOVSCROLL))
4182 return 0;
4184 dy = 0;
4185 switch (action) {
4186 case SB_LINEUP:
4187 case SB_LINEDOWN:
4188 case SB_PAGEUP:
4189 case SB_PAGEDOWN:
4190 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4191 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4192 (action == SB_PAGEUP ? "SB_PAGEUP" :
4193 "SB_PAGEDOWN"))));
4194 EDIT_EM_Scroll(es, action);
4195 return 0;
4196 case SB_TOP:
4197 TRACE("SB_TOP\n");
4198 dy = -es->y_offset;
4199 break;
4200 case SB_BOTTOM:
4201 TRACE("SB_BOTTOM\n");
4202 dy = es->line_count - 1 - es->y_offset;
4203 break;
4204 case SB_THUMBTRACK:
4205 TRACE("SB_THUMBTRACK %d\n", pos);
4206 es->flags |= EF_VSCROLL_TRACK;
4207 if(es->style & WS_VSCROLL)
4208 dy = pos - es->y_offset;
4209 else
4211 /* Assume default scroll range 0-100 */
4212 INT vlc, new_y;
4213 /* Sanity check */
4214 if(pos < 0 || pos > 100) return 0;
4215 vlc = get_vertical_line_count(es);
4216 new_y = pos * (es->line_count - vlc) / 100;
4217 dy = es->line_count ? (new_y - es->y_offset) : 0;
4218 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4219 es->line_count, es->y_offset, pos, dy);
4221 break;
4222 case SB_THUMBPOSITION:
4223 TRACE("SB_THUMBPOSITION %d\n", pos);
4224 es->flags &= ~EF_VSCROLL_TRACK;
4225 if(es->style & WS_VSCROLL)
4226 dy = pos - es->y_offset;
4227 else
4229 /* Assume default scroll range 0-100 */
4230 INT vlc, new_y;
4231 /* Sanity check */
4232 if(pos < 0 || pos > 100) return 0;
4233 vlc = get_vertical_line_count(es);
4234 new_y = pos * (es->line_count - vlc) / 100;
4235 dy = es->line_count ? (new_y - es->y_offset) : 0;
4236 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4237 es->line_count, es->y_offset, pos, dy);
4239 if (!dy)
4241 /* force scroll info update */
4242 EDIT_UpdateScrollInfo(es);
4243 notify_parent(es, EN_VSCROLL);
4245 break;
4246 case SB_ENDSCROLL:
4247 TRACE("SB_ENDSCROLL\n");
4248 break;
4250 * FIXME : the next two are undocumented !
4251 * Are we doing the right thing ?
4252 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4253 * although it's also a regular control message.
4255 case EM_GETTHUMB: /* this one is used by NT notepad */
4257 LRESULT ret;
4258 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4259 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4260 else
4262 /* Assume default scroll range 0-100 */
4263 INT vlc = get_vertical_line_count(es);
4264 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4266 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4267 return ret;
4269 case EM_LINESCROLL:
4270 TRACE("EM_LINESCROLL %d\n", pos);
4271 dy = pos;
4272 break;
4274 default:
4275 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4276 action, action);
4277 return 0;
4279 if (dy)
4280 EDIT_EM_LineScroll(es, 0, dy);
4281 return 0;
4284 /*********************************************************************
4286 * EM_GETTHUMB
4288 * FIXME: is this right ? (or should it be only VSCROLL)
4289 * (and maybe only for edit controls that really have their
4290 * own scrollbars) (and maybe only for multiline controls ?)
4291 * All in all: very poorly documented
4294 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4296 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4297 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4301 /********************************************************************
4303 * The Following code is to handle inline editing from IMEs
4306 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4308 LONG buflen;
4309 LPWSTR lpResultStr;
4311 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4312 if (buflen <= 0)
4314 return;
4317 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen);
4318 if (!lpResultStr)
4320 ERR("Unable to alloc buffer for IME string\n");
4321 return;
4324 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4325 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4326 HeapFree(GetProcessHeap(),0,lpResultStr);
4329 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4331 HIMC hIMC;
4333 hIMC = ImmGetContext(hwnd);
4334 if (!hIMC)
4335 return;
4337 if (CompFlag & GCS_RESULTSTR)
4338 EDIT_GetResultStr(hIMC, es);
4340 ImmReleaseContext(hwnd, hIMC);
4344 /*********************************************************************
4346 * WM_NCCREATE
4348 * See also EDIT_WM_StyleChanged
4350 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4352 EDITSTATE *es;
4353 UINT alloc_size;
4355 TRACE("Creating %s edit control, style = %08lx\n",
4356 unicode ? "Unicode" : "ANSI", lpcs->style);
4358 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4359 return FALSE;
4360 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4363 * Note: since the EDITSTATE has not been fully initialized yet,
4364 * we can't use any API calls that may send
4365 * WM_XXX messages before WM_NCCREATE is completed.
4368 es->is_unicode = unicode;
4369 es->style = lpcs->style;
4371 es->bEnableState = !(es->style & WS_DISABLED);
4373 es->hwndSelf = hwnd;
4374 /* Save parent, which will be notified by EN_* messages */
4375 es->hwndParent = lpcs->hwndParent;
4377 if (es->style & ES_COMBO)
4378 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4380 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4381 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4383 /* Number overrides lowercase overrides uppercase (at least it
4384 * does in Win95). However I'll bet that ES_NUMBER would be
4385 * invalid under Win 3.1.
4387 if (es->style & ES_NUMBER) {
4388 ; /* do not override the ES_NUMBER */
4389 } else if (es->style & ES_LOWERCASE) {
4390 es->style &= ~ES_UPPERCASE;
4392 if (es->style & ES_MULTILINE) {
4393 es->buffer_limit = BUFLIMIT_INITIAL;
4394 if (es->style & WS_VSCROLL)
4395 es->style |= ES_AUTOVSCROLL;
4396 if (es->style & WS_HSCROLL)
4397 es->style |= ES_AUTOHSCROLL;
4398 es->style &= ~ES_PASSWORD;
4399 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4400 /* Confirmed - RIGHT overrides CENTER */
4401 if (es->style & ES_RIGHT)
4402 es->style &= ~ES_CENTER;
4403 es->style &= ~WS_HSCROLL;
4404 es->style &= ~ES_AUTOHSCROLL;
4406 } else {
4407 es->buffer_limit = BUFLIMIT_INITIAL;
4408 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4409 es->style &= ~ES_CENTER;
4410 es->style &= ~WS_HSCROLL;
4411 es->style &= ~WS_VSCROLL;
4412 if (es->style & ES_PASSWORD)
4413 es->password_char = '*';
4416 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4417 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4418 goto cleanup;
4419 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4421 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4422 goto cleanup;
4423 es->undo_buffer_size = es->buffer_size;
4425 if (es->style & ES_MULTILINE)
4426 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4427 goto cleanup;
4428 es->line_count = 1;
4431 * In Win95 look and feel, the WS_BORDER style is replaced by the
4432 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4433 * control a nonclient area so we don't need to draw the border.
4434 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4435 * a nonclient area and we should handle painting the border ourselves.
4437 * When making modifications please ensure that the code still works
4438 * for edit controls created directly with style 0x50800000, exStyle 0
4439 * (which should have a single pixel border)
4441 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4442 es->style &= ~WS_BORDER;
4443 else if (es->style & WS_BORDER)
4444 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4446 return TRUE;
4448 cleanup:
4449 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4450 EDIT_InvalidateUniscribeData(es);
4451 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4452 HeapFree(GetProcessHeap(), 0, es->undo_text);
4453 if (es->hloc32W) LocalFree(es->hloc32W);
4454 HeapFree(GetProcessHeap(), 0, es->logAttr);
4455 HeapFree(GetProcessHeap(), 0, es);
4456 return FALSE;
4460 /*********************************************************************
4462 * WM_CREATE
4465 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4467 RECT clientRect;
4469 TRACE("%s\n", debugstr_w(name));
4471 * To initialize some final structure members, we call some helper
4472 * functions. However, since the EDITSTATE is not consistent (i.e.
4473 * not fully initialized), we should be very careful which
4474 * functions can be called, and in what order.
4476 EDIT_WM_SetFont(es, 0, FALSE);
4477 EDIT_EM_EmptyUndoBuffer(es);
4479 /* We need to calculate the format rect
4480 (applications may send EM_SETMARGINS before the control gets visible) */
4481 GetClientRect(es->hwndSelf, &clientRect);
4482 EDIT_SetRectNP(es, &clientRect);
4484 if (name && *name) {
4485 EDIT_EM_ReplaceSel(es, FALSE, name, lstrlenW(name), FALSE, FALSE);
4486 /* if we insert text to the editline, the text scrolls out
4487 * of the window, as the caret is placed after the insert
4488 * pos normally; thus we reset es->selection... to 0 and
4489 * update caret
4491 es->selection_start = es->selection_end = 0;
4492 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4493 * Messages are only to be sent when the USER does something to
4494 * change the contents. So I am removing this EN_CHANGE
4496 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4498 EDIT_EM_ScrollCaret(es);
4500 /* force scroll info update */
4501 EDIT_UpdateScrollInfo(es);
4502 /* The rule seems to return 1 here for success */
4503 /* Power Builder masked edit controls will crash */
4504 /* if not. */
4505 /* FIXME: is that in all cases so ? */
4506 return 1;
4510 /*********************************************************************
4512 * WM_NCDESTROY
4515 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4517 LINEDEF *pc, *pp;
4519 /* The app can own the text buffer handle */
4520 if (es->hloc32W && (es->hloc32W != es->hlocapp)) {
4521 LocalFree(es->hloc32W);
4523 if (es->hloc32A && (es->hloc32A != es->hlocapp)) {
4524 LocalFree(es->hloc32A);
4526 EDIT_InvalidateUniscribeData(es);
4527 pc = es->first_line_def;
4528 while (pc)
4530 pp = pc->next;
4531 HeapFree(GetProcessHeap(), 0, pc);
4532 pc = pp;
4535 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4536 HeapFree(GetProcessHeap(), 0, es->undo_text);
4537 HeapFree(GetProcessHeap(), 0, es);
4539 return 0;
4543 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4545 if(unicode)
4546 return DefWindowProcW(hwnd, msg, wParam, lParam);
4547 else
4548 return DefWindowProcA(hwnd, msg, wParam, lParam);
4551 /*********************************************************************
4553 * EditWndProc_common
4555 * The messages are in the order of the actual integer values
4556 * (which can be found in include/windows.h)
4558 LRESULT EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4560 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4561 LRESULT result = 0;
4562 POINT pt;
4564 TRACE("hwnd=%p msg=%x (%s) wparam=%Ix lparam=%Ix\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4566 if (!es && msg != WM_NCCREATE)
4567 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4569 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4571 switch (msg) {
4572 case EM_GETSEL:
4573 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4574 break;
4576 case EM_SETSEL:
4577 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4578 EDIT_EM_ScrollCaret(es);
4579 result = 1;
4580 break;
4582 case EM_GETRECT:
4583 if (lParam)
4584 *((LPRECT)lParam) = es->format_rect;
4585 break;
4587 case EM_SETRECT:
4588 if ((es->style & ES_MULTILINE) && lParam) {
4589 EDIT_SetRectNP(es, (LPRECT)lParam);
4590 EDIT_UpdateText(es, NULL, TRUE);
4592 break;
4594 case EM_SETRECTNP:
4595 if ((es->style & ES_MULTILINE) && lParam)
4596 EDIT_SetRectNP(es, (LPRECT)lParam);
4597 break;
4599 case EM_SCROLL:
4600 result = EDIT_EM_Scroll(es, (INT)wParam);
4601 break;
4603 case EM_LINESCROLL:
4604 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4605 break;
4607 case EM_SCROLLCARET:
4608 EDIT_EM_ScrollCaret(es);
4609 result = 1;
4610 break;
4612 case EM_GETMODIFY:
4613 result = ((es->flags & EF_MODIFIED) != 0);
4614 break;
4616 case EM_SETMODIFY:
4617 if (wParam)
4618 es->flags |= EF_MODIFIED;
4619 else
4620 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4621 break;
4623 case EM_GETLINECOUNT:
4624 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4625 break;
4627 case EM_LINEINDEX:
4628 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4629 break;
4631 case EM_SETHANDLE:
4632 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4633 break;
4635 case EM_GETHANDLE:
4636 result = (LRESULT)EDIT_EM_GetHandle(es);
4637 break;
4639 case EM_GETTHUMB:
4640 result = EDIT_EM_GetThumb(es);
4641 break;
4643 /* these messages missing from specs */
4644 case 0x00bf:
4645 case 0x00c0:
4646 case 0x00c3:
4647 case 0x00ca:
4648 FIXME("undocumented message 0x%x, please report\n", msg);
4649 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4650 break;
4652 case EM_LINELENGTH:
4653 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4654 break;
4656 case EM_REPLACESEL:
4658 LPWSTR textW;
4660 if(unicode)
4661 textW = (LPWSTR)lParam;
4662 else
4664 LPSTR textA = (LPSTR)lParam;
4665 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4666 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4667 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4670 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, lstrlenW(textW), TRUE, TRUE);
4671 result = 1;
4673 if(!unicode)
4674 HeapFree(GetProcessHeap(), 0, textW);
4675 break;
4678 case EM_GETLINE:
4679 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4680 break;
4682 case EM_SETLIMITTEXT:
4683 EDIT_EM_SetLimitText(es, wParam);
4684 break;
4686 case EM_CANUNDO:
4687 result = (LRESULT)EDIT_EM_CanUndo(es);
4688 break;
4690 case EM_UNDO:
4691 case WM_UNDO:
4692 result = (LRESULT)EDIT_EM_Undo(es);
4693 break;
4695 case EM_FMTLINES:
4696 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4697 break;
4699 case EM_LINEFROMCHAR:
4700 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4701 break;
4703 case EM_SETTABSTOPS:
4704 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4705 break;
4707 case EM_SETPASSWORDCHAR:
4709 WCHAR charW = 0;
4711 if(unicode)
4712 charW = (WCHAR)wParam;
4713 else
4715 CHAR charA = wParam;
4716 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4719 EDIT_EM_SetPasswordChar(es, charW);
4720 break;
4723 case EM_EMPTYUNDOBUFFER:
4724 EDIT_EM_EmptyUndoBuffer(es);
4725 break;
4727 case EM_GETFIRSTVISIBLELINE:
4728 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4729 break;
4731 case EM_SETREADONLY:
4733 DWORD old_style = es->style;
4735 if (wParam) {
4736 SetWindowLongW( hwnd, GWL_STYLE,
4737 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4738 es->style |= ES_READONLY;
4739 } else {
4740 SetWindowLongW( hwnd, GWL_STYLE,
4741 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4742 es->style &= ~ES_READONLY;
4745 if (old_style ^ es->style)
4746 NtUserInvalidateRect(es->hwndSelf, NULL, TRUE);
4748 result = 1;
4749 break;
4752 case EM_SETWORDBREAKPROC:
4753 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4754 result = 1;
4755 break;
4757 case EM_GETWORDBREAKPROC:
4758 result = (LRESULT)es->word_break_proc;
4759 break;
4761 case EM_GETPASSWORDCHAR:
4763 if(unicode)
4764 result = es->password_char;
4765 else
4767 WCHAR charW = es->password_char;
4768 CHAR charA = 0;
4769 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4770 result = charA;
4772 break;
4775 case EM_SETMARGINS:
4776 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4777 break;
4779 case EM_GETMARGINS:
4780 result = MAKELONG(es->left_margin, es->right_margin);
4781 break;
4783 case EM_GETLIMITTEXT:
4784 result = es->buffer_limit;
4785 break;
4787 case EM_POSFROMCHAR:
4788 if ((INT)wParam >= get_text_length(es)) result = -1;
4789 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4790 break;
4792 case EM_CHARFROMPOS:
4793 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4794 break;
4796 case EM_SETIMESTATUS:
4797 if (wParam == EMSIS_COMPOSITIONSTRING)
4798 es->ime_status = lParam & 0xFFFF;
4800 result = 1;
4801 break;
4803 case EM_GETIMESTATUS:
4804 result = wParam == EMSIS_COMPOSITIONSTRING ? es->ime_status : 1;
4805 break;
4807 /* End of the EM_ messages which were in numerical order; what order
4808 * are these in? vaguely alphabetical?
4811 case WM_NCCREATE:
4812 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4813 break;
4815 case WM_NCDESTROY:
4816 result = EDIT_WM_NCDestroy(es);
4817 es = NULL;
4818 break;
4820 case WM_GETDLGCODE:
4821 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4823 if (es->style & ES_MULTILINE)
4824 result |= DLGC_WANTALLKEYS;
4826 if (lParam)
4828 es->flags|=EF_DIALOGMODE;
4830 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4832 int vk = (int)((LPMSG)lParam)->wParam;
4834 if (es->hwndListBox)
4836 if (vk == VK_RETURN || vk == VK_ESCAPE)
4837 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4838 result |= DLGC_WANTMESSAGE;
4842 break;
4844 case WM_CHAR:
4846 WCHAR charW;
4848 if(unicode)
4849 charW = wParam;
4850 else
4852 BYTE low = wParam;
4853 DWORD cp = get_input_codepage();
4854 if (es->lead_byte)
4856 char ch[2];
4857 ch[0] = es->lead_byte;
4858 ch[1] = low;
4859 MultiByteToWideChar(cp, 0, ch, 2, &charW, 1);
4861 else if (IsDBCSLeadByteEx(cp, low))
4863 es->lead_byte = low;
4864 result = 1;
4865 break;
4867 else
4868 MultiByteToWideChar(cp, 0, (char *)&low, 1, &charW, 1);
4869 es->lead_byte = 0;
4872 if (es->hwndListBox)
4874 if (charW == VK_RETURN || charW == VK_ESCAPE)
4876 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4877 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4878 break;
4881 result = EDIT_WM_Char(es, charW);
4882 break;
4885 case WM_UNICHAR:
4886 if (unicode)
4888 if (wParam == UNICODE_NOCHAR) return TRUE;
4889 if (wParam <= 0x000fffff)
4891 if(wParam > 0xffff) /* convert to surrogates */
4893 wParam -= 0x10000;
4894 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4895 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4897 else EDIT_WM_Char(es, wParam);
4899 return 0;
4901 break;
4903 case WM_CLEAR:
4904 EDIT_WM_Clear(es);
4905 break;
4907 case WM_CONTEXTMENU:
4908 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4909 break;
4911 case WM_COPY:
4912 EDIT_WM_Copy(es);
4913 break;
4915 case WM_CREATE:
4916 if(unicode)
4917 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4918 else
4920 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4921 LPWSTR nameW = NULL;
4922 if(nameA)
4924 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
4925 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4926 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
4928 result = EDIT_WM_Create(es, nameW);
4929 HeapFree(GetProcessHeap(), 0, nameW);
4931 break;
4933 case WM_CUT:
4934 EDIT_WM_Cut(es);
4935 break;
4937 case WM_ENABLE:
4938 es->bEnableState = (BOOL) wParam;
4939 EDIT_UpdateText(es, NULL, TRUE);
4940 break;
4942 case WM_ERASEBKGND:
4943 /* we do the proper erase in EDIT_WM_Paint */
4944 result = 1;
4945 break;
4947 case WM_GETFONT:
4948 result = (LRESULT)es->font;
4949 break;
4951 case WM_GETTEXT:
4952 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
4953 break;
4955 case WM_GETTEXTLENGTH:
4956 if (unicode) result = get_text_length(es);
4957 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
4958 NULL, 0, NULL, NULL );
4959 break;
4961 case WM_HSCROLL:
4962 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4963 break;
4965 case WM_KEYDOWN:
4966 result = EDIT_WM_KeyDown(es, (INT)wParam);
4967 break;
4969 case WM_KILLFOCUS:
4970 result = EDIT_WM_KillFocus(es);
4971 break;
4973 case WM_LBUTTONDBLCLK:
4974 result = EDIT_WM_LButtonDblClk(es);
4975 break;
4977 case WM_LBUTTONDOWN:
4978 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
4979 break;
4981 case WM_LBUTTONUP:
4982 result = EDIT_WM_LButtonUp(es);
4983 break;
4985 case WM_MBUTTONDOWN:
4986 result = EDIT_WM_MButtonDown(es);
4987 break;
4989 case WM_MOUSEMOVE:
4990 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4991 break;
4993 case WM_PRINTCLIENT:
4994 case WM_PAINT:
4995 EDIT_WM_Paint(es, (HDC)wParam);
4996 break;
4998 case WM_PASTE:
4999 EDIT_WM_Paste(es);
5000 break;
5002 case WM_SETFOCUS:
5003 EDIT_WM_SetFocus(es);
5004 break;
5006 case WM_SETFONT:
5007 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5008 break;
5010 case WM_SETREDRAW:
5011 /* FIXME: actually set an internal flag and behave accordingly */
5012 break;
5014 case WM_SETTEXT:
5015 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5016 result = TRUE;
5017 break;
5019 case WM_SIZE:
5020 EDIT_WM_Size(es, (UINT)wParam);
5021 break;
5023 case WM_STYLECHANGED:
5024 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5025 break;
5027 case WM_STYLECHANGING:
5028 result = 0; /* See EDIT_WM_StyleChanged */
5029 break;
5031 case WM_SYSKEYDOWN:
5032 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5033 break;
5035 case WM_VSCROLL:
5036 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5037 break;
5039 case WM_MOUSEWHEEL:
5041 int wheelDelta;
5042 INT pulScrollLines = 3;
5043 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5045 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5046 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5047 break;
5049 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5050 /* if scrolling changes direction, ignore left overs */
5051 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5052 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5053 es->wheelDeltaRemainder += wheelDelta;
5054 else
5055 es->wheelDeltaRemainder = wheelDelta;
5056 if (es->wheelDeltaRemainder && pulScrollLines)
5058 int cLineScroll;
5059 pulScrollLines = min(es->line_count, pulScrollLines);
5060 cLineScroll = pulScrollLines * es->wheelDeltaRemainder / WHEEL_DELTA;
5061 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
5062 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5065 break;
5068 /* IME messages to make the edit control IME aware */
5069 case WM_IME_SETCONTEXT:
5070 NtUserGetCaretPos(&pt);
5071 EDIT_UpdateImmCompositionWindow(es, pt.x, pt.y);
5072 EDIT_UpdateImmCompositionFont(es);
5073 break;
5075 case WM_IME_COMPOSITION:
5076 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5077 if ((lParam & GCS_RESULTSTR) && (es->ime_status & EIMES_GETCOMPSTRATONCE))
5078 EDIT_ImeComposition(hwnd, lParam, es);
5079 return DefWindowProcW(hwnd, msg, wParam, lParam);
5081 case WM_IME_SELECT:
5082 break;
5084 case WM_IME_CONTROL:
5085 break;
5087 case WM_IME_REQUEST:
5088 switch (wParam)
5090 case IMR_QUERYCHARPOSITION:
5092 LRESULT pos;
5093 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5095 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5096 chpos->pt.x = LOWORD(pos);
5097 chpos->pt.y = HIWORD(pos);
5098 chpos->cLineHeight = es->line_height;
5099 chpos->rcDocument = es->format_rect;
5100 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5101 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5102 result = 1;
5103 break;
5106 break;
5108 default:
5109 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5110 break;
5113 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5114 EDIT_UnlockBuffer(es, FALSE);
5116 TRACE("hwnd=%p msg=%x (%s) -- 0x%08Ix\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5118 return result;